Debian Codegraph @

tests/t-common.h

raw
1 /* t-common.h - Common functions for the tests.
2 * Copyright (C) 2002, 2003 g10 Code GmbH
3 *
4 * This file is part of KSBA.
5 *
6 * KSBA is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * KSBA is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20 /*-- sha1.c --*/
21 void sha1_hash_buffer (char *outbuf, const char *buffer, size_t length);
22 void sha256_hash_buffer (char *outbuf, const char *buffer, size_t length);
23
24
25
26 #define digitp(p) (*(p) >= '0' && *(p) <= '9')
27
28 #define fail_if_err(a) do { if(a) { \
29 fprintf (stderr, "%s:%d: KSBA error: %s\n", \
30 __FILE__, __LINE__, gpg_strerror(a)); \
31 exit (1); } \
32 } while(0)
33
34
35 #define fail_if_err2(f, a) do { if(a) {\
36 fprintf (stderr, "%s:%d: KSBA error on file `%s': %s\n", \
37 __FILE__, __LINE__, (f), gpg_strerror(a)); \
38 exit (1); } \
39 } while(0)
40
41 #define fail(s) do { fprintf (stderr, "%s:%d: %s\n", __FILE__,__LINE__, (s));\
42 exit (1); } while(0)
43
44 #define xfree(a) ksba_free (a)
45
46
47 void *
48 xmalloc (size_t n)
49 {
50 char *p = ksba_malloc (n);
51 if (!p)
52 {
53 fprintf (stderr, "out of core\n");
54 exit (1);
55 }
56 return p;
57 }
58
59
60 /* Prepend FNAME with the srcdir environment variable's value and
61 retrun an allocated filename. */
62 char *
63 prepend_srcdir (const char *fname)
64 {
65 static const char *srcdir;
66 char *result;
67
68 if (!srcdir)
69 if(!(srcdir = getenv ("srcdir")))
70 srcdir = ".";
71
72 result = xmalloc (strlen (srcdir) + 1 + strlen (fname) + 1);
73 strcpy (result, srcdir);
74 strcat (result, "/");
75 strcat (result, fname);
76 return result;
77 }
78
79
80
81 void
82 print_hex (const unsigned char *p, size_t n, int wrapindent)
83 {
84 int count = 0;
85
86 if (!p)
87 fputs ("none", stdout);
88 else
89 {
90 for (; n; n--, p++)
91 {
92 if (wrapindent && count && !(count % 32))
93 printf ("\n%*s", wrapindent, "");
94 printf ("%02X", *p);
95 count++;
96 }
97 }
98 }
99
100
101 void
102 print_sexp (ksba_const_sexp_t p)
103 {
104 int level = 0;
105 int delim_seen = 0;
106
107 if (!p)
108 fputs ("[none]", stdout);
109 else
110 {
111 for (;;)
112 {
113 if (*p == '(')
114 {
115 delim_seen = 1;
116 putchar (*p);
117 p++;
118 level++;
119 }
120 else if (*p == ')')
121 {
122 delim_seen = 1;
123 putchar (*p);
124 p++;
125 if (--level <= 0 )
126 return;
127 }
128 else if (!digitp (p))
129 {
130 fputs ("[invalid s-exp]", stdout);
131 return;
132 }
133 else
134 {
135 char *endp;
136 const unsigned char *s;
137 unsigned long len, n;
138
139 len = strtoul (p, &endp, 10);
140 p = endp;
141 if (*p != ':')
142 {
143 fputs ("[invalid s-exp]", stdout);
144 return;
145 }
146 p++;
147 for (s=p,n=0; n < len; n++, s++)
148 if ( !((*s >= 'a' && *s <= 'z')
149 || (*s >= 'A' && *s <= 'Z')
150 || (*s >= '0' && *s <= '9')
151 || *s == '-' || *s == '.'))
152 break;
153 if (!delim_seen)
154 putchar (' ');
155 if (n < len)
156 {
157 putchar('#');
158 for (n=0; n < len; n++, p++)
159 printf ("%02X", *p);
160 putchar('#');
161 }
162 else
163 {
164 for (n=0; n < len; n++, p++)
165 putchar (*p);
166 putchar (' ');
167 }
168 delim_seen = 0;
169 }
170 }
171 }
172 }
173
174 /* Variant of print_sexp which forces printing the values in hex. */
175 void
176 print_sexp_hex (ksba_const_sexp_t p)
177 {
178 int level = 0;
179
180 if (!p)
181 fputs ("[none]", stdout);
182 else
183 {
184 for (;;)
185 {
186 if (*p == '(')
187 {
188 putchar (*p);
189 p++;
190 level++;
191 }
192 else if (*p == ')')
193 {
194 putchar (*p);
195 p++;
196 if (--level <= 0 )
197 return;
198 }
199 else if (!digitp (p))
200 {
201 fputs ("[invalid s-exp]", stdout);
202 return;
203 }
204 else
205 {
206 char *endp;
207 unsigned long len, n;
208
209 len = strtoul (p, &endp, 10);
210 p = endp;
211 if (*p != ':')
212 {
213 fputs ("[invalid s-exp]", stdout);
214 return;
215 }
216 p++;
217 putchar('#');
218 for (n=0; n < len; n++, p++)
219 printf ("%02X", *p);
220 putchar('#');
221 }
222 }
223 }
224 }
225
226
227 void
228 print_dn (char *p)
229 {
230 if (!p)
231 fputs ("error", stdout);
232 else
233 printf ("`%s'", p);
234 }
235
236
237 void
238 print_time (ksba_isotime_t t)
239 {
240 if (!t || !*t)
241 fputs ("none", stdout);
242 else
243 printf ("%.4s-%.2s-%.2s %.2s:%.2s:%s", t, t+4, t+6, t+9, t+11, t+13);
244 }