Imported Upstream version 2.2.15
[platform/upstream/gpg2.git] / common / t-support.c
1 /* t-support.c - helper functions for the regression tests.
2  * Copyright (C) 2007 Free Software Foundation, Inc.
3  *
4  * This file is part of GnuPG.
5  *
6  * GnuPG is free software; you can redistribute and/or modify this
7  * part of GnuPG under the terms of either
8  *
9  *   - the GNU Lesser General Public License as published by the Free
10  *     Software Foundation; either version 3 of the License, or (at
11  *     your option) any later version.
12  *
13  * or
14  *
15  *   - the GNU General Public License as published by the Free
16  *     Software Foundation; either version 2 of the License, or (at
17  *     your option) any later version.
18  *
19  * or both in parallel, as here.
20  *
21  * GnuPG is distributed in the hope that it will be useful, but
22  * WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
24  * General Public License for more details.
25  *
26  * You should have received a copies of the GNU General Public License
27  * and the GNU Lesser General Public License along with this program;
28  * if not, see <https://www.gnu.org/licenses/>.
29  */
30
31 #include <config.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <errno.h>
36 #include <assert.h>
37
38 #include "t-support.h"
39
40 \f
41 /* Replacements for the malloc functions as used here. */
42
43 static void
44 out_of_memory (void)
45 {
46   fprintf (stderr,"error: out of core in regression tests: %s\n",
47            strerror (errno));
48   exit (2);
49 }
50
51
52 void *
53 gcry_malloc (size_t n)
54 {
55   return malloc (n);
56 }
57
58 void *
59 gcry_xmalloc (size_t n)
60 {
61   void *p = malloc (n);
62   if (!p)
63     out_of_memory ();
64   return p;
65 }
66
67 char *
68 gcry_strdup (const char *string)
69 {
70   char *p = malloc (strlen (string)+1);
71   if (p)
72     strcpy (p, string);
73   return p;
74 }
75
76
77 void *
78 gcry_realloc (void *a, size_t n)
79 {
80   return realloc (a, n);
81 }
82
83 void *
84 gcry_xrealloc (void *a, size_t n)
85 {
86   void *p = realloc (a, n);
87   if (!p)
88     out_of_memory ();
89   return p;
90 }
91
92
93
94 void *
95 gcry_calloc (size_t n, size_t m)
96 {
97   return calloc (n, m);
98 }
99
100 void *
101 gcry_xcalloc (size_t n, size_t m)
102 {
103   void *p = calloc (n, m);
104   if (!p)
105     out_of_memory ();
106   return p;
107 }
108
109
110 char *
111 gcry_xstrdup (const char *string)
112 {
113   void *p = malloc (strlen (string)+1);
114   if (!p)
115     out_of_memory ();
116   strcpy (p, string);
117   return p;
118 }
119
120 void
121 gcry_free (void *a)
122 {
123   if (a)
124     free (a);
125 }
126
127
128 \f
129 /* Stubs for gpg-error functions required because some compilers do
130    not eliminate the supposed-to-be-unused-inline-functions and thus
131    require functions called from these inline functions.  */
132 #ifndef GPG_ERROR_H /* Don't do this if gpg-error.h has been included.  */
133 int
134 gpg_err_code_from_errno (int err)
135 {
136   (void)err;
137   assert (!"stub function");
138   return -1;
139 }
140 #endif /*GPG_ERROR_H*/
141
142
143 /* Retrieve the error code directly from the ERRNO variable.  This
144    returns GPG_ERR_UNKNOWN_ERRNO if the system error is not mapped
145    (report this) and GPG_ERR_MISSING_ERRNO if ERRNO has the value 0. */
146 #ifndef GPG_ERROR_H /* Don't do this if gpg-error.h has been included.  */
147 int
148 gpg_err_code_from_syserror (void)
149 {
150   assert (!"stub function");
151   return -1;
152 }
153 #endif /*GPG_ERROR_H*/