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