Cleanups.
[u/mdw/catacomb] / mptext.c
1 /* -*-c-*-
2 *
3 * $Id$
4 *
5 * Textual representation of multiprecision numbers
6 *
7 * (c) 1999 Straylight/Edgeware
8 */
9
10 /*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of Catacomb.
13 *
14 * Catacomb is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
18 *
19 * Catacomb is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
23 *
24 * You should have received a copy of the GNU Library General Public
25 * License along with Catacomb; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
27 * MA 02111-1307, USA.
28 */
29
30 /*----- Header files ------------------------------------------------------*/
31
32 #include <ctype.h>
33 #include <limits.h>
34 #include <stdio.h>
35
36 #include "mp.h"
37 #include "mptext.h"
38 #include "paranoia.h"
39
40 /*----- Magical numbers ---------------------------------------------------*/
41
42 /* --- Maximum recursion depth --- *
43 *
44 * This is the number of bits in a @size_t@ object. Why?
45 *
46 * To see this, let %$b = \textit{MPW\_MAX} + 1$% and let %$Z$% be the
47 * largest @size_t@ value. Then the largest possible @mp@ is %$M - 1$% where
48 * %$M = b^Z$%. Let %$r$% be a radix to read or write. Since the recursion
49 * squares the radix at each step, the highest number reached by the
50 * recursion is %$d$%, where:
51 *
52 * %$r^{2^d} = b^Z$%.
53 *
54 * Solving gives that %$d = \lg \log_r b^Z$%. If %$r = 2$%, this is maximum,
55 * so choosing %$d = \lg \lg b^Z = \lg (Z \lg b) = \lg Z + \lg \lg b$%.
56 *
57 * Expressing %$\lg Z$% as @CHAR_BIT * sizeof(size_t)@ yields an
58 * overestimate, since a @size_t@ representation may contain `holes'.
59 * Choosing to represent %$\lg \lg b$% by 10 is almost certainly sufficient
60 * for `some time to come'.
61 */
62
63 #define DEPTH (CHAR_BIT * sizeof(size_t) + 10)
64
65 /*----- Main code ---------------------------------------------------------*/
66
67 /* --- @mp_read@ --- *
68 *
69 * Arguments: @mp *m@ = destination multiprecision number
70 * @int radix@ = base to assume for data (or zero to guess)
71 * @const mptext_ops *ops@ = pointer to operations block
72 * @void *p@ = data for the operations block
73 *
74 * Returns: The integer read, or zero if it didn't work.
75 *
76 * Use: Reads an integer from some source. If the @radix@ is
77 * specified, the number is assumed to be given in that radix,
78 * with the letters `a' (either upper- or lower-case) upwards
79 * standing for digits greater than 9. Otherwise, base 10 is
80 * assumed unless the number starts with `0' (octal), `0x' (hex)
81 * or `nnn_' (base `nnn'). An arbitrary amount of whitespace
82 * before the number is ignored.
83 */
84
85 /* --- About the algorithm --- *
86 *
87 * The algorithm here is rather aggressive. I maintain an array of
88 * successive squarings of the radix, and a stack of partial results, each
89 * with a counter attached indicating which radix square to multiply by.
90 * Once the item at the top of the stack reaches the same counter level as
91 * the next item down, they are combined together and the result is given a
92 * counter level one higher than either of the results.
93 *
94 * Gluing the results together at the end is slightly tricky. Pay attention
95 * to the code.
96 *
97 * This is more complicated because of the need to handle the slightly
98 * bizarre syntax.
99 */
100
101 mp *mp_read(mp *m, int radix, const mptext_ops *ops, void *p)
102 {
103 int ch; /* Current char being considered */
104 unsigned f = 0; /* Flags about the current number */
105 int r; /* Radix to switch over to */
106 mpw rd; /* Radix as an @mp@ digit */
107 mp rr; /* The @mp@ for the radix */
108 unsigned nf = m ? m->f & MP_BURN : 0; /* New @mp@ flags */
109
110 /* --- Stacks --- */
111
112 mp *pow[DEPTH]; /* List of powers */
113 unsigned pows; /* Next index to fill */
114 struct { unsigned i; mp *m; } s[DEPTH]; /* Main stack */
115 unsigned sp; /* Current stack pointer */
116
117 /* --- Flags --- */
118
119 #define f_neg 1u
120 #define f_ok 2u
121 #define f_start 4u
122
123 /* --- Initialize the stacks --- */
124
125 mp_build(&rr, &rd, &rd + 1);
126 pow[0] = &rr;
127 pows = 1;
128
129 sp = 0;
130
131 /* --- Initialize the destination number --- */
132
133 if (m)
134 MP_DROP(m);
135
136 /* --- Read an initial character --- */
137
138 ch = ops->get(p);
139 if (radix >= 0) {
140 while (isspace(ch))
141 ch = ops->get(p);
142 }
143
144 /* --- Handle an initial sign --- */
145
146 if (radix >= 0 && (ch == '-' || ch == '+')) {
147 if (ch == '-')
148 f |= f_neg;
149 do ch = ops->get(p); while isspace(ch);
150 }
151
152 /* --- If the radix is zero, look for leading zeros --- */
153
154 if (radix > 0) {
155 assert(((void)"ascii radix must be <= 62", radix <= 62));
156 rd = radix;
157 r = -1;
158 } else if (radix < 0) {
159 rd = -radix;
160 assert(((void)"binary radix must fit in a byte", rd <= UCHAR_MAX));
161 r = -1;
162 } else if (ch != '0') {
163 rd = 10;
164 r = 0;
165 } else {
166 ch = ops->get(p);
167 switch (ch) {
168 case 'x':
169 rd = 16;
170 goto prefix;
171 case 'o':
172 rd = 8;
173 goto prefix;
174 case 'b':
175 rd = 2;
176 goto prefix;
177 prefix:
178 ch = ops->get(p);
179 break;
180 default:
181 rd = 8;
182 f |= f_ok;
183 }
184 r = -1;
185 }
186
187 /* --- Use fast algorithm for binary radix --- *
188 *
189 * This is the restart point after having parsed a radix number from the
190 * input. We check whether the radix is binary, and if so use a fast
191 * algorithm which just stacks the bits up in the right order.
192 */
193
194 restart:
195 switch (rd) {
196 unsigned bit;
197
198 case 2: bit = 1; goto bin;
199 case 4: bit = 2; goto bin;
200 case 8: bit = 3; goto bin;
201 case 16: bit = 4; goto bin;
202 case 32: bit = 5; goto bin;
203 case 64: bit = 6; goto bin;
204 case 128: bit = 7; goto bin;
205 default:
206 break;
207
208 /* --- The fast binary algorithm --- *
209 *
210 * We stack bits up starting at the top end of a word. When one word is
211 * full, we write it to the integer, and start another with the left-over
212 * bits. When the array in the integer is full, we resize using low-level
213 * calls and copy the current data to the top end. Finally, we do a single
214 * bit-shift when we know where the end of the number is.
215 */
216
217 bin: {
218 mpw a = 0;
219 unsigned b = MPW_BITS;
220 size_t len, n;
221 mpw *v;
222
223 m = mp_dest(MP_NEW, 1, nf);
224 len = n = m->sz;
225 n = len;
226 v = m->v + n;
227 for (;; ch = ops->get(p)) {
228 unsigned x;
229
230 if (ch < 0)
231 break;
232
233 /* --- Check that the character is a digit and in range --- */
234
235 if (radix < 0)
236 x = ch % rd;
237 else {
238 if (!isalnum(ch))
239 break;
240 if (ch >= '0' && ch <= '9')
241 x = ch - '0';
242 else {
243 if (rd <= 36)
244 ch = tolower(ch);
245 if (ch >= 'a' && ch <= 'z') /* ASCII dependent! */
246 x = ch - 'a' + 10;
247 else if (ch >= 'A' && ch <= 'Z')
248 x = ch - 'A' + 36;
249 else
250 break;
251 }
252 }
253 if (x >= rd)
254 break;
255
256 /* --- Feed the digit into the accumulator --- */
257
258 f |= f_ok;
259 if (!x && !(f & f_start))
260 continue;
261 f |= f_start;
262 if (b > bit) {
263 b -= bit;
264 a |= MPW(x) << b;
265 } else {
266 a |= MPW(x) >> (bit - b);
267 b += MPW_BITS - bit;
268 *--v = MPW(a);
269 n--;
270 if (!n) {
271 n = len;
272 len <<= 1;
273 v = mpalloc(m->a, len);
274 memcpy(v + n, m->v, MPWS(n));
275 mpfree(m->a, m->v);
276 m->v = v;
277 v = m->v + n;
278 }
279 a = (b < MPW_BITS) ? MPW(x) << b : 0;
280 }
281 }
282
283 /* --- Finish up --- */
284
285 if (!(f & f_ok)) {
286 mp_drop(m);
287 m = 0;
288 } else {
289 *--v = MPW(a);
290 n--;
291 m->sz = len;
292 m->vl = m->v + len;
293 m->f &= ~MP_UNDEF;
294 m = mp_lsr(m, m, (unsigned long)n * MPW_BITS + b);
295 }
296 ops->unget(ch, p);
297 goto done;
298 }}
299
300 /* --- Time to start --- */
301
302 for (;; ch = ops->get(p)) {
303 unsigned x;
304
305 if (ch < 0)
306 break;
307
308 /* --- An underscore indicates a numbered base --- */
309
310 if (ch == '_' && r > 0 && r <= 62) {
311 unsigned i;
312
313 /* --- Clear out the stacks --- */
314
315 for (i = 1; i < pows; i++)
316 MP_DROP(pow[i]);
317 pows = 1;
318 for (i = 0; i < sp; i++)
319 MP_DROP(s[i].m);
320 sp = 0;
321
322 /* --- Restart the search --- */
323
324 rd = r;
325 r = -1;
326 f &= ~f_ok;
327 ch = ops->get(p);
328 goto restart;
329 }
330
331 /* --- Check that the character is a digit and in range --- */
332
333 if (radix < 0)
334 x = ch % rd;
335 else {
336 if (!isalnum(ch))
337 break;
338 if (ch >= '0' && ch <= '9')
339 x = ch - '0';
340 else {
341 if (rd <= 36)
342 ch = tolower(ch);
343 if (ch >= 'a' && ch <= 'z') /* ASCII dependent! */
344 x = ch - 'a' + 10;
345 else if (ch >= 'A' && ch <= 'Z')
346 x = ch - 'A' + 36;
347 else
348 break;
349 }
350 }
351
352 /* --- Sort out what to do with the character --- */
353
354 if (x >= 10 && r >= 0)
355 r = -1;
356 if (x >= rd)
357 break;
358
359 if (r >= 0)
360 r = r * 10 + x;
361
362 /* --- Stick the character on the end of my integer --- */
363
364 assert(((void)"Number is too unimaginably huge", sp < DEPTH));
365 s[sp].m = m = mp_new(1, nf);
366 m->v[0] = x;
367 s[sp].i = 0;
368
369 /* --- Now grind through the stack --- */
370
371 while (sp > 0 && s[sp - 1].i == s[sp].i) {
372
373 /* --- Combine the top two items --- */
374
375 sp--;
376 m = s[sp].m;
377 m = mp_mul(m, m, pow[s[sp].i]);
378 m = mp_add(m, m, s[sp + 1].m);
379 s[sp].m = m;
380 MP_DROP(s[sp + 1].m);
381 s[sp].i++;
382
383 /* --- Make a new radix power if necessary --- */
384
385 if (s[sp].i >= pows) {
386 assert(((void)"Number is too unimaginably huge", pows < DEPTH));
387 pow[pows] = mp_sqr(MP_NEW, pow[pows - 1]);
388 pows++;
389 }
390 }
391 f |= f_ok;
392 sp++;
393 }
394
395 ops->unget(ch, p);
396
397 /* --- If we're done, compute the rest of the number --- */
398
399 if (f & f_ok) {
400 if (!sp)
401 return (MP_ZERO);
402 else {
403 mp *z = MP_ONE;
404 sp--;
405
406 while (sp > 0) {
407
408 /* --- Combine the top two items --- */
409
410 sp--;
411 m = s[sp].m;
412 z = mp_mul(z, z, pow[s[sp + 1].i]);
413 m = mp_mul(m, m, z);
414 m = mp_add(m, m, s[sp + 1].m);
415 s[sp].m = m;
416 MP_DROP(s[sp + 1].m);
417
418 /* --- Make a new radix power if necessary --- */
419
420 if (s[sp].i >= pows) {
421 assert(((void)"Number is too unimaginably huge", pows < DEPTH));
422 pow[pows] = mp_sqr(MP_NEW, pow[pows - 1]);
423 pows++;
424 }
425 }
426 MP_DROP(z);
427 m = s[0].m;
428 }
429 } else {
430 unsigned i;
431 for (i = 0; i < sp; i++)
432 MP_DROP(s[i].m);
433 }
434
435 /* --- Clear the radix power list --- */
436
437 {
438 unsigned i;
439 for (i = 1; i < pows; i++)
440 MP_DROP(pow[i]);
441 }
442
443 /* --- Bail out if the number was bad --- */
444
445 done:
446 if (!(f & f_ok))
447 return (0);
448
449 /* --- Set the sign and return --- */
450
451 if (f & f_neg)
452 m->f |= MP_NEG;
453 MP_SHRINK(m);
454 return (m);
455
456 #undef f_start
457 #undef f_neg
458 #undef f_ok
459 }
460
461 /* --- @mp_write@ --- *
462 *
463 * Arguments: @mp *m@ = pointer to a multi-precision integer
464 * @int radix@ = radix to use when writing the number out
465 * @const mptext_ops *ops@ = pointer to an operations block
466 * @void *p@ = data for the operations block
467 *
468 * Returns: Zero if it worked, nonzero otherwise.
469 *
470 * Use: Writes a large integer in textual form.
471 */
472
473 /* --- Simple case --- *
474 *
475 * Use a fixed-sized buffer and single-precision arithmetic to pick off
476 * low-order digits. Put each digit in a buffer, working backwards from the
477 * end. If the buffer becomes full, recurse to get another one. Ensure that
478 * there are at least @z@ digits by writing leading zeroes if there aren't
479 * enough real digits.
480 */
481
482 static int simple(mpw n, int radix, unsigned z,
483 const mptext_ops *ops, void *p)
484 {
485 int rc = 0;
486 char buf[64];
487 unsigned i = sizeof(buf);
488 int rd = radix > 0 ? radix : -radix;
489
490 do {
491 int ch;
492 mpw x;
493
494 x = n % rd;
495 n /= rd;
496 if (radix < 0)
497 ch = x;
498 else if (x < 10)
499 ch = '0' + x;
500 else if (x < 36) /* Ascii specific */
501 ch = 'a' + x - 10;
502 else
503 ch = 'A' + x - 36;
504 buf[--i] = ch;
505 if (z)
506 z--;
507 } while (i && n);
508
509 if (n)
510 rc = simple(n, radix, z, ops, p);
511 else {
512 char zbuf[32];
513 memset(zbuf, (radix < 0) ? 0 : '0', sizeof(zbuf));
514 while (!rc && z >= sizeof(zbuf)) {
515 rc = ops->put(zbuf, sizeof(zbuf), p);
516 z -= sizeof(zbuf);
517 }
518 if (!rc && z)
519 rc = ops->put(zbuf, z, p);
520 }
521 if (!rc)
522 rc = ops->put(buf + i, sizeof(buf) - i, p);
523 BURN(buf);
524 return (rc);
525 }
526
527 /* --- Complicated case --- *
528 *
529 * If the number is small, fall back to the simple case above. Otherwise
530 * divide and take remainder by current large power of the radix, and emit
531 * each separately. Don't emit a zero quotient. Be very careful about
532 * leading zeroes on the remainder part, because they're deeply significant.
533 */
534
535 static int complicated(mp *m, int radix, mp **pr, unsigned i, unsigned z,
536 const mptext_ops *ops, void *p)
537 {
538 int rc = 0;
539 mp *q = MP_NEW;
540 unsigned d = 1 << i;
541
542 if (MP_LEN(m) < 2)
543 return (simple(MP_LEN(m) ? m->v[0] : 0, radix, z, ops, p));
544
545 assert(i);
546 mp_div(&q, &m, m, pr[i]);
547 if (MP_ZEROP(q))
548 d = z;
549 else {
550 if (z > d)
551 z -= d;
552 else
553 z = 0;
554 rc = complicated(q, radix, pr, i - 1, z, ops, p);
555 }
556 if (!rc)
557 rc = complicated(m, radix, pr, i - 1, d, ops, p);
558 mp_drop(q);
559 return (rc);
560 }
561
562 /* --- Binary case --- *
563 *
564 * Special case for binary output. Goes much faster.
565 */
566
567 static int binary(mp *m, int bit, int radix, const mptext_ops *ops, void *p)
568 {
569 mpw *v;
570 mpw a;
571 int rc = 0;
572 unsigned b;
573 unsigned mask;
574 unsigned long n;
575 unsigned f = 0;
576 char buf[8], *q;
577 unsigned x;
578 int ch;
579
580 #define f_out 1u
581
582 /* --- Work out where to start --- */
583
584 n = mp_bits(m);
585 if (n % bit)
586 n += bit - (n % bit);
587 b = n % MPW_BITS;
588 n /= MPW_BITS;
589
590 if (n >= MP_LEN(m)) {
591 n--;
592 b += MPW_BITS;
593 }
594
595 v = m->v + n;
596 a = *v;
597 mask = (1 << bit) - 1;
598 q = buf;
599
600 /* --- Main code --- */
601
602 for (;;) {
603 if (b > bit) {
604 b -= bit;
605 x = a >> b;
606 } else {
607 x = a << (bit - b);
608 b += MPW_BITS - bit;
609 if (v == m->v)
610 break;
611 a = *--v;
612 if (b < MPW_BITS)
613 x |= a >> b;
614 }
615 x &= mask;
616 if (!x && !(f & f_out))
617 continue;
618
619 if (radix < 0)
620 ch = x;
621 else if (x < 10)
622 ch = '0' + x;
623 else if (x < 36)
624 ch = 'a' + x - 10; /* Ascii specific */
625 else
626 ch = 'A' + x - 36;
627 *q++ = ch;
628 if (q >= buf + sizeof(buf)) {
629 if ((rc = ops->put(buf, sizeof(buf), p)) != 0)
630 goto done;
631 q = buf;
632 }
633 f |= f_out;
634 }
635
636 x &= mask;
637 if (radix < 0)
638 ch = x;
639 else if (x < 10)
640 ch = '0' + x;
641 else if (x < 36)
642 ch = 'a' + x - 10; /* Ascii specific */
643 else
644 ch = 'A' + x - 36;
645 *q++ = ch;
646 rc = ops->put(buf, q - buf, p);
647
648 done:
649 mp_drop(m);
650 return (rc);
651
652 #undef f_out
653 }
654
655 /* --- Main driver code --- */
656
657 int mp_write(mp *m, int radix, const mptext_ops *ops, void *p)
658 {
659 int rc;
660
661 if (MP_EQ(m, MP_ZERO))
662 return (ops->put(radix > 0 ? "0" : "\0", 1, p));
663
664 /* --- Set various things up --- */
665
666 m = MP_COPY(m);
667 MP_SPLIT(m);
668
669 /* --- Check the radix for sensibleness --- */
670
671 if (radix > 0)
672 assert(((void)"ascii radix must be <= 62", radix <= 62));
673 else if (radix < 0)
674 assert(((void)"binary radix must fit in a byte", -radix <= UCHAR_MAX));
675 else
676 assert(((void)"radix can't be zero in mp_write", 0));
677
678 /* --- If the number is negative, sort that out --- */
679
680 if (MP_NEGP(m)) {
681 assert(radix > 0);
682 if (ops->put("-", 1, p))
683 return (EOF);
684 m->f &= ~MP_NEG;
685 }
686
687 /* --- Handle binary radix --- */
688
689 switch (radix) {
690 case 2: case -2: return (binary(m, 1, radix, ops, p));
691 case 4: case -4: return (binary(m, 2, radix, ops, p));
692 case 8: case -8: return (binary(m, 3, radix, ops, p));
693 case 16: case -16: return (binary(m, 4, radix, ops, p));
694 case 32: case -32: return (binary(m, 5, radix, ops, p));
695 case -64: return (binary(m, 6, radix, ops, p));
696 case -128: return (binary(m, 7, radix, ops, p));
697 }
698
699 /* --- If the number is small, do it the easy way --- */
700
701 if (MP_LEN(m) < 2)
702 rc = simple(MP_LEN(m) ? m->v[0] : 0, radix, 0, ops, p);
703
704 /* --- Use a clever algorithm --- *
705 *
706 * Square the radix repeatedly, remembering old results, until I get
707 * something more than half the size of the number @m@. Use this to divide
708 * the number: the quotient and remainder will be approximately the same
709 * size, and I'll have split them on a digit boundary, so I can just emit
710 * the quotient and remainder recursively, in order.
711 */
712
713 else {
714 mp *pr[DEPTH];
715 size_t target = (MP_LEN(m) + 1) / 2;
716 unsigned i = 0;
717 mp *z = mp_new(1, 0);
718
719 /* --- Set up the exponent table --- */
720
721 z->v[0] = (radix > 0 ? radix : -radix);
722 z->f = 0;
723 for (;;) {
724 assert(((void)"Number is too unimaginably huge", i < DEPTH));
725 pr[i++] = z;
726 if (MP_LEN(z) > target)
727 break;
728 z = mp_sqr(MP_NEW, z);
729 }
730
731 /* --- Write out the answer --- */
732
733 rc = complicated(m, radix, pr, i - 1, 0, ops, p);
734
735 /* --- Tidy away the array --- */
736
737 while (i > 0)
738 mp_drop(pr[--i]);
739 }
740
741 /* --- Tidying up code --- */
742
743 MP_DROP(m);
744 return (rc);
745 }
746
747 /*----- Test rig ----------------------------------------------------------*/
748
749 #ifdef TEST_RIG
750
751 #include <mLib/testrig.h>
752
753 static int verify(dstr *v)
754 {
755 int ok = 1;
756 int ib = *(int *)v[0].buf, ob = *(int *)v[2].buf;
757 dstr d = DSTR_INIT;
758 size_t off = 0;
759 mp *m = mp_readdstr(MP_NEW, &v[1], &off, ib);
760 if (m) {
761 if (!ob) {
762 fprintf(stderr, "*** unexpected successful parse\n"
763 "*** input [%2i] = ", ib);
764 if (ib < 0)
765 type_hex.dump(&v[1], stderr);
766 else
767 fputs(v[1].buf, stderr);
768 mp_writedstr(m, &d, 10);
769 fprintf(stderr, "\n*** (value = %s)\n", d.buf);
770 ok = 0;
771 } else {
772 mp_writedstr(m, &d, ob);
773 if (d.len != v[3].len || memcmp(d.buf, v[3].buf, d.len) != 0) {
774 fprintf(stderr, "*** failed read or write\n"
775 "*** input [%2i] = ", ib);
776 if (ib < 0)
777 type_hex.dump(&v[1], stderr);
778 else
779 fputs(v[1].buf, stderr);
780 fprintf(stderr, "\n*** output [%2i] = ", ob);
781 if (ob < 0)
782 type_hex.dump(&d, stderr);
783 else
784 fputs(d.buf, stderr);
785 fprintf(stderr, "\n*** expected [%2i] = ", ob);
786 if (ob < 0)
787 type_hex.dump(&v[3], stderr);
788 else
789 fputs(v[3].buf, stderr);
790 fputc('\n', stderr);
791 ok = 0;
792 }
793 }
794 mp_drop(m);
795 } else {
796 if (ob) {
797 fprintf(stderr, "*** unexpected parse failure\n"
798 "*** input [%2i] = ", ib);
799 if (ib < 0)
800 type_hex.dump(&v[1], stderr);
801 else
802 fputs(v[1].buf, stderr);
803 fprintf(stderr, "\n*** expected [%2i] = ", ob);
804 if (ob < 0)
805 type_hex.dump(&v[3], stderr);
806 else
807 fputs(v[3].buf, stderr);
808 fputc('\n', stderr);
809 ok = 0;
810 }
811 }
812
813 if (v[1].len - off != v[4].len ||
814 memcmp(v[1].buf + off, v[4].buf, v[4].len) != 0) {
815 fprintf(stderr, "*** leftovers incorrect\n"
816 "*** input [%2i] = ", ib);
817 if (ib < 0)
818 type_hex.dump(&v[1], stderr);
819 else
820 fputs(v[1].buf, stderr);
821 fprintf(stderr, "\n*** expected `%s'\n"
822 "*** found `%s'\n",
823 v[4].buf, v[1].buf + off);
824 ok = 0;
825 }
826
827 dstr_destroy(&d);
828 assert(mparena_count(MPARENA_GLOBAL) == 0);
829 return (ok);
830 }
831
832 static test_chunk tests[] = {
833 { "mptext-ascii", verify,
834 { &type_int, &type_string, &type_int, &type_string, &type_string, 0 } },
835 { "mptext-bin-in", verify,
836 { &type_int, &type_hex, &type_int, &type_string, &type_string, 0 } },
837 { "mptext-bin-out", verify,
838 { &type_int, &type_string, &type_int, &type_hex, &type_string, 0 } },
839 { 0, 0, { 0 } }
840 };
841
842 int main(int argc, char *argv[])
843 {
844 sub_init();
845 test_run(argc, argv, tests, SRCDIR "/tests/mptext");
846 return (0);
847 }
848
849 #endif
850
851 /*----- That's all, folks -------------------------------------------------*/