chiark / gitweb /
Import gnupg2_2.1.17.orig.tar.bz2
[gnupg2.git] / tools / no-libgcrypt.c
1 /* no-libgcrypt.c - Replacement functions for libgcrypt.
2  *      Copyright (C) 2003 Free Software Foundation, Inc.
3  *
4  * This file is free software; as a special exception the author gives
5  * unlimited permission to copy and/or distribute it, with or without
6  * modifications, as long as this notice is preserved.
7  *
8  * This file is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY, to the extent permitted by law; without even
10  * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11  * PURPOSE.
12  */
13
14 #include <config.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <errno.h>
19
20 #include "../common/util.h"
21 #include "i18n.h"
22
23
24 /* Replace libgcrypt's malloc functions which are used by
25    ../common/libcommon.a .  ../common/util.h defines macros to map them
26    to xmalloc etc. */
27 static void
28 out_of_memory (void)
29 {
30   log_fatal (_("error allocating enough memory: %s\n"), strerror (errno));
31 }
32
33
34 void *
35 gcry_malloc (size_t n)
36 {
37   return malloc (n);
38 }
39
40 void *
41 gcry_malloc_secure (size_t n)
42 {
43   return malloc (n);
44 }
45
46 void *
47 gcry_xmalloc (size_t n)
48 {
49   void *p = malloc (n);
50   if (!p)
51     out_of_memory ();
52   return p;
53 }
54
55 char *
56 gcry_strdup (const char *string)
57 {
58   char *p = malloc (strlen (string)+1);
59   if (p)
60     strcpy (p, string);
61   return p;
62 }
63
64
65 void *
66 gcry_realloc (void *a, size_t n)
67 {
68   return realloc (a, n);
69 }
70
71 void *
72 gcry_xrealloc (void *a, size_t n)
73 {
74   void *p = realloc (a, n);
75   if (!p)
76     out_of_memory ();
77   return p;
78 }
79
80
81
82 void *
83 gcry_calloc (size_t n, size_t m)
84 {
85   return calloc (n, m);
86 }
87
88 void *
89 gcry_xcalloc (size_t n, size_t m)
90 {
91   void *p = calloc (n, m);
92   if (!p)
93     out_of_memory ();
94   return p;
95 }
96
97
98 char *
99 gcry_xstrdup (const char *string)
100 {
101   void *p = malloc (strlen (string)+1);
102   if (!p)
103     out_of_memory ();
104   strcpy( p, string );
105   return p;
106 }
107
108 void
109 gcry_free (void *a)
110 {
111   if (a)
112     free (a);
113 }
114
115
116 /* We need this dummy because exechelp.c uses gcry_control to
117    terminate the secure memeory.  */
118 gcry_error_t
119 gcry_control (enum gcry_ctl_cmds cmd, ...)
120 {
121   (void)cmd;
122   return 0;
123 }
124
125 void
126 gcry_set_outofcore_handler (gcry_handler_no_mem_t h, void *opaque)
127 {
128   (void)h;
129   (void)opaque;
130 }
131
132 void
133 gcry_set_fatalerror_handler (gcry_handler_error_t fnc, void *opaque)
134 {
135   (void)fnc;
136   (void)opaque;
137 }
138
139 void
140 gcry_set_log_handler (gcry_handler_log_t f, void *opaque)
141 {
142   (void)f;
143   (void)opaque;
144 }
145
146
147 void
148 gcry_create_nonce (void *buffer, size_t length)
149 {
150   (void)buffer;
151   (void)length;
152
153   log_fatal ("unexpected call to gcry_create_nonce\n");
154 }
155
156
157 const char *
158 gcry_cipher_algo_name (int algo)
159 {
160   (void)algo;
161   return "?";
162 }