- startet importing pluto ASN1 stuff
[strongswan.git] / Source / lib / asn1-pluto / ttodata.c
1 /*
2 * convert from text form of arbitrary data (e.g., keys) to binary
3 * Copyright (C) 2000 Henry Spencer.
4 *
5 * This library is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU Library General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/lgpl.txt>.
9 *
10 * This library is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13 * License for more details.
14 */
15
16 #include "ttodata.h"
17
18 #include <string.h>
19 #include <ctype.h>
20
21 /* converters and misc */
22 static int unhex(const char *, char *, size_t);
23 static int unb64(const char *, char *, size_t);
24 static int untext(const char *, char *, size_t);
25 static const char *badch(const char *, int, char *, size_t);
26
27 /* internal error codes for converters */
28 #define SHORT (-2) /* internal buffer too short */
29 #define BADPAD (-3) /* bad base64 padding */
30 #define BADCH0 (-4) /* invalid character 0 */
31 #define BADCH1 (-5) /* invalid character 1 */
32 #define BADCH2 (-6) /* invalid character 2 */
33 #define BADCH3 (-7) /* invalid character 3 */
34 #define BADOFF(code) (BADCH0-(code))
35
36 /*
37 - ttodatav - convert text to data, with verbose error reports
38 * If some of this looks slightly odd, it's because it has changed
39 * repeatedly (from the original atodata()) without a major rewrite.
40 */
41 const char * /* NULL on success, else literal or errp */
42 ttodatav(src, srclen, base, dst, dstlen, lenp, errp, errlen, flags)
43 const char *src;
44 size_t srclen; /* 0 means apply strlen() */
45 int base; /* 0 means figure it out */
46 char *dst; /* need not be valid if dstlen is 0 */
47 size_t dstlen;
48 size_t *lenp; /* where to record length (NULL is nowhere) */
49 char *errp; /* error buffer */
50 size_t errlen;
51 unsigned int flags;
52 {
53 size_t ingroup; /* number of input bytes converted at once */
54 char buf[4]; /* output from conversion */
55 int nbytes; /* size of output */
56 int (*decode)(const char *, char *, size_t);
57 char *stop;
58 int ndone;
59 int i;
60 int underscoreok;
61 int skipSpace = 0;
62
63 if (srclen == 0)
64 srclen = strlen(src);
65 if (dstlen == 0)
66 dst = buf; /* point it somewhere valid */
67 stop = dst + dstlen;
68
69 if (base == 0) {
70 if (srclen < 2)
71 return "input too short to be valid";
72 if (*src++ != '0')
73 return "input does not begin with format prefix";
74 switch (*src++) {
75 case 'x':
76 case 'X':
77 base = 16;
78 break;
79 case 's':
80 case 'S':
81 base = 64;
82 break;
83 case 't':
84 case 'T':
85 base = 256;
86 break;
87 default:
88 return "unknown format prefix";
89 }
90 srclen -= 2;
91 }
92 switch (base) {
93 case 16:
94 decode = unhex;
95 underscoreok = 1;
96 ingroup = 2;
97 break;
98 case 64:
99 decode = unb64;
100 underscoreok = 0;
101 ingroup = 4;
102 if(flags & TTODATAV_IGNORESPACE) {
103 skipSpace = 1;
104 }
105 break;
106
107 case 256:
108 decode = untext;
109 ingroup = 1;
110 underscoreok = 0;
111 break;
112 default:
113 return "unknown base";
114 }
115
116 /* proceed */
117 ndone = 0;
118 while (srclen > 0) {
119 char stage[4]; /* staging area for group */
120 size_t sl = 0;
121
122 /* Grab ingroup characters into stage,
123 * squeezing out blanks if we are supposed to ignore them.
124 */
125 for (sl = 0; sl < ingroup; src++, srclen--) {
126 if (srclen == 0)
127 return "input ends in mid-byte, perhaps truncated";
128 else if (!(skipSpace && (*src == ' ' || *src == '\t')))
129 stage[sl++] = *src;
130 }
131
132 nbytes = (*decode)(stage, buf, sizeof(buf));
133 switch (nbytes) {
134 case BADCH0:
135 case BADCH1:
136 case BADCH2:
137 case BADCH3:
138 return badch(stage, nbytes, errp, errlen);
139 case SHORT:
140 return "internal buffer too short (\"can't happen\")";
141 case BADPAD:
142 return "bad (non-zero) padding at end of base64 input";
143 }
144 if (nbytes <= 0)
145 return "unknown internal error";
146 for (i = 0; i < nbytes; i++) {
147 if (dst < stop)
148 *dst++ = buf[i];
149 ndone++;
150 }
151 while (srclen >= 1 && skipSpace && (*src == ' ' || *src == '\t')){
152 src++;
153 srclen--;
154 }
155 if (underscoreok && srclen > 1 && *src == '_') {
156 /* srclen > 1 means not last character */
157 src++;
158 srclen--;
159 }
160 }
161
162 if (ndone == 0)
163 return "no data bytes specified by input";
164 if (lenp != NULL)
165 *lenp = ndone;
166 return NULL;
167 }
168
169 /*
170 - ttodata - convert text to data
171 */
172 const char * /* NULL on success, else literal */
173 ttodata(src, srclen, base, dst, dstlen, lenp)
174 const char *src;
175 size_t srclen; /* 0 means apply strlen() */
176 int base; /* 0 means figure it out */
177 char *dst; /* need not be valid if dstlen is 0 */
178 size_t dstlen;
179 size_t *lenp; /* where to record length (NULL is nowhere) */
180 {
181 return ttodatav(src, srclen, base, dst, dstlen, lenp, (char *)NULL,
182 (size_t)0, TTODATAV_SPACECOUNTS);
183 }
184
185 /*
186 - atodata - convert ASCII to data
187 * backward-compatibility interface
188 */
189 size_t /* 0 for failure, true length for success */
190 atodata(src, srclen, dst, dstlen)
191 const char *src;
192 size_t srclen;
193 char *dst;
194 size_t dstlen;
195 {
196 size_t len;
197 const char *err;
198
199 err = ttodata(src, srclen, 0, dst, dstlen, &len);
200 if (err != NULL)
201 return 0;
202 return len;
203 }
204
205 /*
206 - atobytes - convert ASCII to data bytes
207 * another backward-compatibility interface
208 */
209 const char *
210 atobytes(src, srclen, dst, dstlen, lenp)
211 const char *src;
212 size_t srclen;
213 char *dst;
214 size_t dstlen;
215 size_t *lenp;
216 {
217 return ttodata(src, srclen, 0, dst, dstlen, lenp);
218 }
219
220 /*
221 - unhex - convert two ASCII hex digits to byte
222 */
223 static int /* number of result bytes, or error code */
224 unhex(src, dst, dstlen)
225 const char *src; /* known to be full length */
226 char *dst;
227 size_t dstlen; /* not large enough is a failure */
228 {
229 char *p;
230 unsigned byte;
231 static char hex[] = "0123456789abcdef";
232
233 if (dstlen < 1)
234 return SHORT;
235
236 p = strchr(hex, *src);
237 if (p == NULL)
238 p = strchr(hex, tolower(*src));
239 if (p == NULL)
240 return BADCH0;
241 byte = (p - hex) << 4;
242 src++;
243
244 p = strchr(hex, *src);
245 if (p == NULL)
246 p = strchr(hex, tolower(*src));
247 if (p == NULL)
248 return BADCH1;
249 byte |= (p - hex);
250
251 *dst = byte;
252 return 1;
253 }
254
255 /*
256 - unb64 - convert four ASCII base64 digits to three bytes
257 * Note that a base64 digit group is padded out with '=' if it represents
258 * less than three bytes: one byte is dd==, two is ddd=, three is dddd.
259 */
260 static int /* number of result bytes, or error code */
261 unb64(src, dst, dstlen)
262 const char *src; /* known to be full length */
263 char *dst;
264 size_t dstlen;
265 {
266 char *p;
267 unsigned byte1;
268 unsigned byte2;
269 static char base64[] =
270 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
271
272 if (dstlen < 3)
273 return SHORT;
274
275 p = strchr(base64, *src++);
276
277 if (p == NULL)
278 return BADCH0;
279 byte1 = (p - base64) << 2; /* first six bits */
280
281 p = strchr(base64, *src++);
282 if (p == NULL) {
283 return BADCH1;
284 }
285
286 byte2 = p - base64; /* next six: two plus four */
287 *dst++ = byte1 | (byte2 >> 4);
288 byte1 = (byte2 & 0xf) << 4;
289
290 p = strchr(base64, *src++);
291 if (p == NULL) {
292 if (*(src-1) == '=' && *src == '=') {
293 if (byte1 != 0) /* bad padding */
294 return BADPAD;
295 return 1;
296 }
297 return BADCH2;
298 }
299
300 byte2 = p - base64; /* next six: four plus two */
301 *dst++ = byte1 | (byte2 >> 2);
302 byte1 = (byte2 & 0x3) << 6;
303
304 p = strchr(base64, *src++);
305 if (p == NULL) {
306 if (*(src-1) == '=') {
307 if (byte1 != 0) /* bad padding */
308 return BADPAD;
309 return 2;
310 }
311 return BADCH3;
312 }
313 byte2 = p - base64; /* last six */
314 *dst++ = byte1 | byte2;
315
316 return 3;
317 }
318
319 /*
320 - untext - convert one ASCII character to byte
321 */
322 static int /* number of result bytes, or error code */
323 untext(src, dst, dstlen)
324 const char *src; /* known to be full length */
325 char *dst;
326 size_t dstlen; /* not large enough is a failure */
327 {
328 if (dstlen < 1)
329 return SHORT;
330
331 *dst = *src;
332 return 1;
333 }
334
335 /*
336 - badch - produce a nice complaint about an unknown character
337 *
338 * If the compiler complains that the array bigenough[] has a negative
339 * size, that means the TTODATAV_BUF constant has been set too small.
340 */
341 static const char * /* literal or errp */
342 badch(src, errcode, errp, errlen)
343 const char *src;
344 int errcode;
345 char *errp; /* might be NULL */
346 size_t errlen;
347 {
348 static const char pre[] = "unknown character (`";
349 static const char suf[] = "') in input";
350 char buf[5];
351 # define REQD (sizeof(pre) - 1 + sizeof(buf) - 1 + sizeof(suf))
352 struct sizecheck {
353 char bigenough[TTODATAV_BUF - REQD]; /* see above */
354 };
355 char ch;
356
357 if (errp == NULL || errlen < REQD)
358 return "unknown character in input";
359 strcpy(errp, pre);
360 ch = *(src + BADOFF(errcode));
361 if (isprint(ch)) {
362 buf[0] = ch;
363 buf[1] = '\0';
364 } else {
365 buf[0] = '\\';
366 buf[1] = ((ch & 0700) >> 6) + '0';
367 buf[2] = ((ch & 0070) >> 3) + '0';
368 buf[3] = ((ch & 0007) >> 0) + '0';
369 buf[4] = '\0';
370 }
371 strcat(errp, buf);
372 strcat(errp, suf);
373 return (const char *)errp;
374 }
375
376
377
378 #ifdef TTODATA_MAIN
379
380 #include <stdio.h>
381
382 struct artab;
383 static void check(struct artab *r, char *buf, size_t n, err_t oops, int *status);
384 static void regress(char *pgm);
385 static void hexout(const char *s, size_t len, FILE *f);
386
387 /*
388 - main - convert first argument to hex, or run regression
389 */
390 int
391 main(int argc, char *argv[])
392 {
393 char buf[1024];
394 char buf2[1024];
395 char err[512];
396 size_t n;
397 size_t i;
398 char *p = buf;
399 char *p2 = buf2;
400 char *pgm = argv[0];
401 const char *oops;
402
403 if (argc < 2) {
404 fprintf(stderr, "Usage: %s {0x<hex>|0s<base64>|-r}\n", pgm);
405 exit(2);
406 }
407
408 if (strcmp(argv[1], "-r") == 0) {
409 regress(pgm); /* should not return */
410 fprintf(stderr, "%s: regress() returned?!?\n", pgm);
411 exit(1);
412 }
413
414 oops = ttodatav(argv[1], 0, 0, buf, sizeof(buf), &n,
415 err, sizeof(err), TTODATAV_IGNORESPACE);
416 if (oops != NULL) {
417 fprintf(stderr, "%s: ttodata error `%s' in `%s'\n", pgm,
418 oops, argv[1]);
419 exit(1);
420 }
421
422 if (n > sizeof(buf)) {
423 p = (char *)malloc((size_t)n);
424 if (p == NULL) {
425 fprintf(stderr,
426 "%s: unable to malloc %d bytes for result\n",
427 pgm, n);
428 exit(1);
429 }
430 oops = ttodata(argv[1], 0, 0, p, n, &n);
431 if (oops != NULL) {
432 fprintf(stderr, "%s: error `%s' in ttodata retry?!?\n",
433 pgm, oops);
434 exit(1);
435 }
436 }
437
438 hexout(p, n, stdout);
439 printf("\n");
440
441 i = datatot(buf, n, 'h', buf2, sizeof(buf2));
442 if (i == 0) {
443 fprintf(stderr, "%s: datatot reports error in `%s'\n", pgm,
444 argv[1]);
445 exit(1);
446 }
447
448 if (i > sizeof(buf2)) {
449 p2 = (char *)malloc((size_t)i);
450 if (p == NULL) {
451 fprintf(stderr,
452 "%s: unable to malloc %d bytes for result\n",
453 pgm, i);
454 exit(1);
455 }
456 i = datatot(buf, n, 'h', p2, i);
457 if (i == 0) {
458 fprintf(stderr, "%s: error in datatoa retry?!?\n", pgm);
459 exit(1);
460 }
461 }
462
463 printf("%s\n", p2);
464
465 exit(0);
466 }
467
468 /*
469 - hexout - output an arbitrary-length string in hex
470 */
471 static void
472 hexout(s, len, f)
473 const char *s;
474 size_t len;
475 FILE *f;
476 {
477 size_t i;
478
479 fprintf(f, "0x");
480 for (i = 0; i < len; i++)
481 fprintf(f, "%02x", (unsigned char)s[i]);
482 }
483
484 struct artab {
485 int base;
486 # define IGNORESPACE_BIAS 1000
487 char *ascii; /* NULL for end */
488 char *data; /* NULL for error expected */
489 } atodatatab[] = {
490 { 0, "", NULL, },
491 { 0, "0", NULL, },
492 { 0, "0x", NULL, },
493 { 0, "0xa", NULL, },
494 { 0, "0xab", "\xab", },
495 { 0, "0xabc", NULL, },
496 { 0, "0xabcd", "\xab\xcd", },
497 { 0, "0x0123456789", "\x01\x23\x45\x67\x89", },
498 { 0, "0x01x", NULL, },
499 { 0, "0xabcdef", "\xab\xcd\xef", },
500 { 0, "0xABCDEF", "\xab\xcd\xef", },
501 { 0, "0XaBc0eEd81f", "\xab\xc0\xee\xd8\x1f", },
502 { 0, "0XaBc0_eEd8", "\xab\xc0\xee\xd8", },
503 { 0, "0XaBc0_", NULL, },
504 { 0, "0X_aBc0", NULL, },
505 { 0, "0Xa_Bc0", NULL, },
506 { 16, "aBc0eEd8", "\xab\xc0\xee\xd8", },
507 { 0, "0s", NULL, },
508 { 0, "0sA", NULL, },
509 { 0, "0sBA", NULL, },
510 { 0, "0sCBA", NULL, },
511 { 0, "0sDCBA", "\x0c\x20\x40", },
512 { 0, "0SDCBA", "\x0c\x20\x40", },
513 { 0, "0sDA==", "\x0c", },
514 { 0, "0sDC==", NULL, },
515 { 0, "0sDCA=", "\x0c\x20", },
516 { 0, "0sDCB=", NULL, },
517 { 0, "0sDCAZ", "\x0c\x20\x19", },
518 { 0, "0sDCAa", "\x0c\x20\x1a", },
519 { 0, "0sDCAz", "\x0c\x20\x33", },
520 { 0, "0sDCA0", "\x0c\x20\x34", },
521 { 0, "0sDCA9", "\x0c\x20\x3d", },
522 { 0, "0sDCA+", "\x0c\x20\x3e", },
523 { 0, "0sDCA/", "\x0c\x20\x3f", },
524 { 0, "0sAbraCadabra+", "\x01\xba\xda\x09\xa7\x5a\x6e\xb6\xbe", },
525 { IGNORESPACE_BIAS + 0, "0s AbraCadabra+", "\x01\xba\xda\x09\xa7\x5a\x6e\xb6\xbe", },
526 { IGNORESPACE_BIAS + 0, "0sA braCadabra+", "\x01\xba\xda\x09\xa7\x5a\x6e\xb6\xbe", },
527 { IGNORESPACE_BIAS + 0, "0sAb raCadabra+", "\x01\xba\xda\x09\xa7\x5a\x6e\xb6\xbe", },
528 { IGNORESPACE_BIAS + 0, "0sAbr aCadabra+", "\x01\xba\xda\x09\xa7\x5a\x6e\xb6\xbe", },
529 { IGNORESPACE_BIAS + 0, "0sAbra Cadabra+", "\x01\xba\xda\x09\xa7\x5a\x6e\xb6\xbe", },
530 { IGNORESPACE_BIAS + 0, "0sAbraC adabra+", "\x01\xba\xda\x09\xa7\x5a\x6e\xb6\xbe", },
531 { IGNORESPACE_BIAS + 0, "0sAbraCa dabra+", "\x01\xba\xda\x09\xa7\x5a\x6e\xb6\xbe", },
532 { IGNORESPACE_BIAS + 0, "0sAbraCad abra+", "\x01\xba\xda\x09\xa7\x5a\x6e\xb6\xbe", },
533 { IGNORESPACE_BIAS + 0, "0sAbraCada bra+", "\x01\xba\xda\x09\xa7\x5a\x6e\xb6\xbe", },
534 { IGNORESPACE_BIAS + 0, "0sAbraCadab ra+", "\x01\xba\xda\x09\xa7\x5a\x6e\xb6\xbe", },
535 { IGNORESPACE_BIAS + 0, "0sAbraCadabr a+", "\x01\xba\xda\x09\xa7\x5a\x6e\xb6\xbe", },
536 { IGNORESPACE_BIAS + 0, "0sAbraCadabra +", "\x01\xba\xda\x09\xa7\x5a\x6e\xb6\xbe", },
537 { IGNORESPACE_BIAS + 0, "0sAbraCadabra+ ", "\x01\xba\xda\x09\xa7\x5a\x6e\xb6\xbe", },
538 { 0, "0t", NULL, },
539 { 0, "0tabc_xyz", "abc_xyz", },
540 { 256, "abc_xyz", "abc_xyz", },
541 { 0, NULL, NULL, },
542 };
543
544 struct drtab {
545 char *data; /* input; NULL for end */
546 char format;
547 int buflen; /* -1 means big buffer */
548 int outlen; /* -1 means strlen(ascii)+1 */
549 char *ascii; /* NULL for error expected */
550 } datatoatab[] = {
551 { "", 'x', -1, -1, NULL, },
552 { "", 'X', -1, -1, NULL, },
553 { "", 'n', -1, -1, NULL, },
554 { "0", 'x', -1, -1, "0x30", },
555 { "0", 'x', 0, 5, "---", },
556 { "0", 'x', 1, 5, "", },
557 { "0", 'x', 2, 5, "0", },
558 { "0", 'x', 3, 5, "0x", },
559 { "0", 'x', 4, 5, "0x3", },
560 { "0", 'x', 5, 5, "0x30", },
561 { "0", 'x', 6, 5, "0x30", },
562 { "\xab\xcd", 'x', -1, -1, "0xabcd", },
563 { "\x01\x23\x45\x67\x89", 'x', -1, -1, "0x0123456789", },
564 { "\xab\xcd\xef", 'x', -1, -1, "0xabcdef", },
565 { "\xab\xc0\xee\xd8\x1f", 'x', -1, -1, "0xabc0eed81f", },
566 { "\x01\x02", 'h', -1, -1, "0x0102", },
567 { "\x01\x02\x03\x04\x05\x06", 'h', -1, -1, "0x01020304_0506", },
568 { "\xab\xc0\xee\xd8\x1f", 16, -1, -1, "abc0eed81f", },
569 { "\x0c\x20\x40", 's', -1, -1, "0sDCBA", },
570 { "\x0c\x20\x40", 's', 0, 7, "---", },
571 { "\x0c\x20\x40", 's', 1, 7, "", },
572 { "\x0c\x20\x40", 's', 2, 7, "0", },
573 { "\x0c\x20\x40", 's', 3, 7, "0s", },
574 { "\x0c\x20\x40", 's', 4, 7, "0sD", },
575 { "\x0c\x20\x40", 's', 5, 7, "0sDC", },
576 { "\x0c\x20\x40", 's', 6, 7, "0sDCB", },
577 { "\x0c\x20\x40", 's', 7, 7, "0sDCBA", },
578 { "\x0c\x20\x40", 's', 8, 7, "0sDCBA", },
579 { "\x0c", 's', -1, -1, "0sDA==", },
580 { "\x0c\x20", 's', -1, -1, "0sDCA=", },
581 { "\x0c\x20\x19", 's', -1, -1, "0sDCAZ", },
582 { "\x0c\x20\x1a", 's', -1, -1, "0sDCAa", },
583 { "\x0c\x20\x33", 's', -1, -1, "0sDCAz", },
584 { "\x0c\x20\x34", 's', -1, -1, "0sDCA0", },
585 { "\x0c\x20\x3d", 's', -1, -1, "0sDCA9", },
586 { "\x0c\x20\x3e", 's', -1, -1, "0sDCA+", },
587 { "\x0c\x20\x3f", 's', -1, -1, "0sDCA/", },
588 { "\x01\xba\xda\x09\xa7\x5a\x6e\xb6\xbe", 's', -1, -1, "0sAbraCadabra+", },
589 { "\x01\xba\xda\x09\xa7\x5a\x6e\xb6\xbe", 64, -1, -1, "AbraCadabra+", },
590 { NULL, 'x', -1, -1, NULL, },
591 };
592
593 /*
594 - regress - regression-test ttodata() and datatot()
595 */
596 static void
597 check(r, buf, n, oops, status)
598 struct artab *r;
599 char *buf;
600 size_t n;
601 err_t oops;
602 int *status;
603 {
604 if (oops != NULL && r->data == NULL)
605 {} /* error expected */
606 else if (oops != NULL) {
607 printf("`%s' gave error `%s', expecting %d `", r->ascii,
608 oops, strlen(r->data));
609 hexout(r->data, strlen(r->data), stdout);
610 printf("'\n");
611 *status = 1;
612 } else if (r->data == NULL) {
613 printf("`%s' gave %d `", r->ascii, n);
614 hexout(buf, n, stdout);
615 printf("', expecting error\n");
616 *status = 1;
617 } else if (n != strlen(r->data)) {
618 printf("length wrong in `%s': got %d `", r->ascii, n);
619 hexout(buf, n, stdout);
620 printf("', expecting %d `", strlen(r->data));
621 hexout(r->data, strlen(r->data), stdout);
622 printf("'\n");
623 *status = 1;
624 } else if (memcmp(buf, r->data, n) != 0) {
625 printf("`%s' gave %d `", r->ascii, n);
626 hexout(buf, n, stdout);
627 printf("', expecting %d `", strlen(r->data));
628 hexout(r->data, strlen(r->data), stdout);
629 printf("'\n");
630 *status = 1;
631 }
632 fflush(stdout);
633 }
634
635 static void /* should not return at all, in fact */
636 regress(pgm)
637 char *pgm;
638 {
639 struct artab *r;
640 struct drtab *dr;
641 char buf[100];
642 size_t n;
643 int status = 0;
644
645 for (r = atodatatab; r->ascii != NULL; r++) {
646 int base = r->base;
647 int xbase = 0;
648
649 if ((base == 0 || base == IGNORESPACE_BIAS + 0) && r->ascii[0] == '0') {
650 switch (r->ascii[1]) {
651 case 'x':
652 case 'X':
653 xbase = 16;
654 break;
655 case 's':
656 case 'S':
657 xbase = 64;
658 break;
659 case 't':
660 case 'T':
661 xbase = 256;
662 break;
663 }
664 }
665
666 if (base >= IGNORESPACE_BIAS) {
667 base = base - IGNORESPACE_BIAS;
668 check(r, buf, n, ttodatav(r->ascii, 0, base, buf, sizeof(buf), &n, NULL, 0, TTODATAV_IGNORESPACE), &status);
669 if (xbase != 0)
670 check(r, buf, n, ttodatav(r->ascii+2, 0, xbase, buf, sizeof(buf), &n, NULL, 0, TTODATAV_IGNORESPACE), &status);
671 } else {
672 check(r, buf, n, ttodata(r->ascii, 0, base, buf, sizeof(buf), &n), &status);
673 if (base == 64 || xbase == 64)
674 check(r, buf, n, ttodatav(r->ascii, 0, base, buf, sizeof(buf), &n, NULL, 0, TTODATAV_IGNORESPACE), &status);
675 if (xbase != 0) {
676 check(r, buf, n, ttodata(r->ascii+2, 0, xbase, buf, sizeof(buf), &n), &status);
677 if (base == 64 || xbase == 64)
678 check(r, buf, n, ttodatav(r->ascii+2, 0, xbase, buf, sizeof(buf), &n, NULL, 0, TTODATAV_IGNORESPACE), &status);
679 }
680 }
681 }
682 for (dr = datatoatab; dr->data != NULL; dr++) {
683 size_t should;
684
685 strcpy(buf, "---");
686 n = datatot(dr->data, strlen(dr->data), dr->format, buf,
687 (dr->buflen == -1) ? sizeof(buf) : dr->buflen);
688 should = (dr->ascii == NULL) ? 0 : strlen(dr->ascii) + 1;
689 if (dr->outlen != -1)
690 should = dr->outlen;
691 if (n == 0 && dr->ascii == NULL)
692 {} /* error expected */
693 else if (n == 0) {
694 printf("`");
695 hexout(dr->data, strlen(dr->data), stdout);
696 printf("' %c gave error, expecting %d `%s'\n",
697 dr->format, should, dr->ascii);
698 status = 1;
699 } else if (dr->ascii == NULL) {
700 printf("`");
701 hexout(dr->data, strlen(dr->data), stdout);
702 printf("' %c gave %d `%.*s', expecting error\n",
703 dr->format, n, (int)n, buf);
704 status = 1;
705 } else if (n != should) {
706 printf("length wrong in `");
707 hexout(dr->data, strlen(dr->data), stdout);
708 printf("': got %d `%s'", n, buf);
709 printf(", expecting %d `%s'\n", should, dr->ascii);
710 status = 1;
711 } else if (strcmp(buf, dr->ascii) != 0) {
712 printf("`");
713 hexout(dr->data, strlen(dr->data), stdout);
714 printf("' gave %d `%s'", n, buf);
715 printf(", expecting %d `%s'\n", should, dr->ascii);
716 status = 1;
717 }
718 fflush(stdout);
719 }
720 exit(status);
721 }
722
723 #endif /* TTODATA_MAIN */