Imported Upstream version 1.6.5
[platform/upstream/libksba.git] / tests / t-common.h
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
23
24
25 #define digitp(p)   (*(p) >= '0' && *(p) <= '9')
26
27 #define fail_if_err(a) do { if(a) {                                       \
28                               fprintf (stderr, "%s:%d: KSBA error: %s\n", \
29                               __FILE__, __LINE__, gpg_strerror(a));   \
30                               exit (1); }                              \
31                            } while(0)
32
33
34 #define fail_if_err2(f, a) do { if(a) {\
35             fprintf (stderr, "%s:%d: KSBA error on file `%s': %s\n", \
36                        __FILE__, __LINE__, (f), gpg_strerror(a));   \
37                             exit (1); }                              \
38                            } while(0)
39
40 #define fail(s)  do { fprintf (stderr, "%s:%d: %s\n", __FILE__,__LINE__, (s));\
41                       exit (1); } while(0)
42
43 #define xfree(a)  ksba_free (a)
44
45
46 void *
47 xmalloc (size_t n)
48 {
49   char *p = ksba_malloc (n);
50   if (!p)
51     {
52       fprintf (stderr, "out of core\n");
53       exit (1);
54     }
55   return p;
56 }
57
58
59 /* Prepend FNAME with the srcdir environment variable's value and
60    retrun an allocated filename. */
61 char *
62 prepend_srcdir (const char *fname)
63 {
64   static const char *srcdir;
65   char *result;
66
67   if (!srcdir)
68     if(!(srcdir = getenv ("srcdir")))
69       srcdir = ".";
70
71   result = xmalloc (strlen (srcdir) + 1 + strlen (fname) + 1);
72   strcpy (result, srcdir);
73   strcat (result, "/");
74   strcat (result, fname);
75   return result;
76 }
77
78
79
80 void
81 print_hex (const unsigned char *p, size_t n)
82 {
83   if (!p)
84     fputs ("none", stdout);
85   else
86     {
87       for (; n; n--, p++)
88         printf ("%02X", *p);
89     }
90 }
91
92
93 void
94 print_sexp (ksba_const_sexp_t p)
95 {
96   int level = 0;
97
98   if (!p)
99     fputs ("[none]", stdout);
100   else
101     {
102       for (;;)
103         {
104           if (*p == '(')
105             {
106               putchar (*p);
107               p++;
108               level++;
109             }
110           else if (*p == ')')
111             {
112               putchar (*p);
113               p++;
114               if (--level <= 0 )
115                 return;
116             }
117           else if (!digitp (p))
118             {
119               fputs ("[invalid s-exp]", stdout);
120               return;
121             }
122           else
123             {
124               char *endp;
125               const unsigned char *s;
126               unsigned long len, n;
127
128               len = strtoul (p, &endp, 10);
129               p = endp;
130               if (*p != ':')
131                 {
132                   fputs ("[invalid s-exp]", stdout);
133                   return;
134                 }
135               p++;
136               for (s=p,n=0; n < len; n++, s++)
137                 if ( !((*s >= 'a' && *s <= 'z')
138                        || (*s >= 'A' && *s <= 'Z')
139                        || (*s >= '0' && *s <= '9')
140                        || *s == '-' || *s == '.'))
141                   break;
142               if (n < len)
143                 {
144                   putchar('#');
145                   for (n=0; n < len; n++, p++)
146                     printf ("%02X", *p);
147                   putchar('#');
148                 }
149               else
150                 {
151                   for (n=0; n < len; n++, p++)
152                     putchar (*p);
153                 }
154             }
155         }
156     }
157 }
158
159 /* Variant of print_sexp which forces printing the values in hex.  */
160 void
161 print_sexp_hex (ksba_const_sexp_t p)
162 {
163   int level = 0;
164
165   if (!p)
166     fputs ("[none]", stdout);
167   else
168     {
169       for (;;)
170         {
171           if (*p == '(')
172             {
173               putchar (*p);
174               p++;
175               level++;
176             }
177           else if (*p == ')')
178             {
179               putchar (*p);
180               p++;
181               if (--level <= 0 )
182                 return;
183             }
184           else if (!digitp (p))
185             {
186               fputs ("[invalid s-exp]", stdout);
187               return;
188             }
189           else
190             {
191               char *endp;
192               unsigned long len, n;
193
194               len = strtoul (p, &endp, 10);
195               p = endp;
196               if (*p != ':')
197                 {
198                   fputs ("[invalid s-exp]", stdout);
199                   return;
200                 }
201               p++;
202               putchar('#');
203               for (n=0; n < len; n++, p++)
204                 printf ("%02X", *p);
205               putchar('#');
206             }
207         }
208     }
209 }
210
211
212 void
213 print_dn (char *p)
214 {
215   if (!p)
216     fputs ("error", stdout);
217   else
218     printf ("`%s'", p);
219 }
220
221
222 void
223 print_time (ksba_isotime_t t)
224 {
225   if (!t || !*t)
226     fputs ("none", stdout);
227   else
228     printf ("%.4s-%.2s-%.2s %.2s:%.2s:%s", t, t+4, t+6, t+9, t+11, t+13);
229 }