progs/perftest.c: Use from Glibc syscall numbers.
[catacomb] / math / mptext.c
1 /* -*-c-*-
2 *
3 * Textual representation of multiprecision numbers
4 *
5 * (c) 1999 Straylight/Edgeware
6 */
7
8 /*----- Licensing notice --------------------------------------------------*
9 *
10 * This file is part of Catacomb.
11 *
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
16 *
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
21 *
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25 * MA 02111-1307, USA.
26 */
27
28 /*----- Header files ------------------------------------------------------*/
29
30 #include <ctype.h>
31 #include <limits.h>
32 #include <stdio.h>
33
34 #include <mLib/macros.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 /*----- Input -------------------------------------------------------------*/
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 static int char_digit(int ch, int radix)
102 {
103 int r = radix < 0 ? -radix : radix;
104 int d;
105
106 if (ch < 0) return (-1);
107 if (radix < 0) d = ch;
108 else if ('0' <= ch && ch <= '9') d = ch - '0';
109 else if ('a' <= ch && ch <= 'z') d = ch - 'a' + 10;
110 else if ('A' <= ch && ch <= 'Z') d = ch - 'A' + (radix > 36 ? 36 : 10);
111 else return (-1);
112 if (d >= r) return (-1);
113 return (d);
114 }
115
116 static mp *read_binary(int radix, unsigned bit, unsigned nf,
117 const mptext_ops *ops, void *p)
118 {
119 mpw a = 0;
120 unsigned b = MPW_BITS;
121 int any = 0, nz = 0;
122 int ch, d;
123 size_t len, n;
124 mpw *v;
125 mp *m;
126
127 /* --- The fast binary algorithm --- *
128 *
129 * We stack bits up starting at the top end of a word. When one word is
130 * full, we write it to the integer, and start another with the left-over
131 * bits. When the array in the integer is full, we resize using low-level
132 * calls and copy the current data to the top end. Finally, we do a single
133 * bit-shift when we know where the end of the number is.
134 */
135
136 m = mp_dest(MP_NEW, 1, nf);
137 len = n = m->sz;
138 n = len;
139 v = m->v + n;
140
141 for (;;) {
142 ch = ops->get(p);
143 if ((d = char_digit(ch, radix)) < 0) break;
144
145 /* --- Ignore leading zeroes, but notice that the number is valid --- */
146
147 any = 1;
148 if (!d && !nz) continue;
149 nz = 1;
150
151 /* --- Feed the digit into the accumulator --- */
152
153 if (b > bit) {
154 b -= bit;
155 a |= MPW(d) << b;
156 } else {
157 a |= MPW(d) >> (bit - b);
158 b += MPW_BITS - bit;
159 *--v = MPW(a); n--;
160 if (!n) {
161 n = len; len <<= 1;
162 v = mpalloc(m->a, len);
163 memcpy(v + n, m->v, MPWS(n));
164 mpfree(m->a, m->v);
165 m->v = v; v = m->v + n;
166 }
167 a = (b < MPW_BITS) ? MPW(d) << b : 0;
168 }
169 }
170
171 /* --- Finish up --- */
172
173 ops->unget(ch, p);
174 if (!any) { mp_drop(m); return (0); }
175
176 *--v = MPW(a); n--;
177 m->sz = len;
178 m->vl = m->v + len;
179 m->f &= ~MP_UNDEF;
180 m = mp_lsr(m, m, (unsigned long)n * MPW_BITS + b);
181
182 return (m);
183 }
184
185 struct readstate {
186
187 /* --- State for the general-base reader --- *
188 *
189 * There are two arrays. The @pow@ array is set so that @pow[i]@ contains
190 * %$R^{2^i}$% for @i < pows@. The stack @s@ contains partial results:
191 * each entry contains a value @m@ corresponding to %$2^i$% digits.
192 * Inductively, an empty stack represents zero; if a stack represents %$x$%
193 * then pushing a new entry on the top causes the stack to represent
194 * %$R^{2^i} x + m$%.
195 *
196 * It is an invariant that each entry has a strictly smaller @i@ than the
197 * items beneath it. This is achieved by coaslescing entries at the top if
198 * they have equal %$i$% values: if the top items are %$(m, i)$%, and
199 * %$(M', i)$%, and the rest of the stack represents the integer %$x$%,
200 * then %$R^{2^i} (R^{2^i} x + M) + m = R^{2^{i+1}} x + (R^{2^i} M + m)$%,
201 * so we replace the top two items by %$((R^{2^i} M + m), i + 1)$%, and
202 * repeat if necessary.
203 */
204
205 unsigned pows, sp;
206 struct { unsigned i; mp *m; } s[DEPTH];
207 mp *pow[DEPTH];
208 };
209
210 static void ensure_power(struct readstate *rs)
211 {
212 /* --- Make sure we have the necessary %$R^{2^i}$% computed --- */
213
214 if (rs->s[rs->sp].i >= rs->pows) {
215 assert(rs->pows < DEPTH);
216 rs->pow[rs->pows] = mp_sqr(MP_NEW, rs->pow[rs->pows - 1]);
217 rs->pows++;
218 }
219 }
220
221 static void read_digit(struct readstate *rs, unsigned nf, int d)
222 {
223 mp *m = mp_new(1, nf);
224 m->v[0] = d;
225
226 /* --- Put the new digit on top --- */
227
228 assert(rs->sp < DEPTH);
229 rs->s[rs->sp].m = m;
230 rs->s[rs->sp].i = 0;
231
232 /* --- Restore the stack invariant --- */
233
234 while (rs->sp && rs->s[rs->sp - 1].i <= rs->s[rs->sp].i) {
235 assert(rs->sp > 0);
236 ensure_power(rs);
237 rs->sp--;
238
239 m = rs->s[rs->sp].m;
240 m = mp_mul(m, m, rs->pow[rs->s[rs->sp + 1].i]);
241 m = mp_add(m, m, rs->s[rs->sp + 1].m);
242 MP_DROP(rs->s[rs->sp + 1].m);
243 rs->s[rs->sp].m = m;
244 rs->s[rs->sp].i++;
245 }
246
247 /* --- Leave the stack pointer at an empty item --- */
248
249 rs->sp++;
250 }
251
252 static mp *read_general(int radix, unsigned t, unsigned nf,
253 const mptext_ops *ops, void *p)
254 {
255 struct readstate rs;
256 unsigned char v[4];
257 unsigned i;
258 mpw r;
259 int any = 0;
260 int ch, d;
261 mp rr;
262 mp *m, *z, *n;
263
264 /* --- Prepare the stack --- */
265
266 r = radix < 0 ? -radix : radix;
267 mp_build(&rr, &r, &r + 1);
268 rs.pow[0] = &rr;
269 rs.pows = 1;
270 rs.sp = 0;
271
272 /* --- If we've partially parsed some input then feed it in --- *
273 *
274 * Unfortunately, what we've got is backwards. Fortunately there's a
275 * fairly tight upper bound on how many digits @t@ might be, since we
276 * aborted that loop once it got too large.
277 */
278
279 if (t) {
280 i = 0;
281 while (t) { assert(i < sizeof(v)); v[i++] = t%r; t /= r; }
282 while (i) read_digit(&rs, nf, v[--i]);
283 any = 1;
284 }
285
286 /* --- Read more stuff --- */
287
288 for (;;) {
289 ch = ops->get(p);
290 if ((d = char_digit(ch, radix)) < 0) break;
291 read_digit(&rs, nf, d); any = 1;
292 }
293 ops->unget(ch, p);
294
295 /* --- Stitch all of the numbers together --- *
296 *
297 * This is not the same code as @read_digit@. In particular, here we must
298 * cope with the partial result being some inconvenient power of %$R$%,
299 * rather than %$R^{2^i}$%.
300 */
301
302 if (!any) return (0);
303 m = MP_ZERO; z = MP_ONE;
304 while (rs.sp) {
305 rs.sp--;
306 ensure_power(&rs);
307 n = rs.s[rs.sp].m;
308 n = mp_mul(n, n, z);
309 m = mp_add(m, m, n);
310 z = mp_mul(z, z, rs.pow[rs.s[rs.sp].i]);
311 MP_DROP(n);
312 }
313 for (i = 0; i < rs.pows; i++) MP_DROP(rs.pow[i]);
314 MP_DROP(z);
315 return (m);
316 }
317
318 mp *mp_read(mp *m, int radix, const mptext_ops *ops, void *p)
319 {
320 unsigned t = 0;
321 unsigned nf = 0;
322 int ch, d, rd;
323
324 unsigned f = 0;
325 #define f_neg 1u
326 #define f_ok 2u
327
328 /* --- We don't actually need a destination so throw it away --- *
329 *
330 * But note the flags before we lose it entirely.
331 */
332
333 if (m) {
334 nf = m->f & MP_BURN;
335 MP_DROP(m);
336 }
337
338 /* --- Maintain a lookahead character --- */
339
340 ch = ops->get(p);
341
342 /* --- If we're reading text, skip leading space, and maybe a sign --- */
343
344 if (radix >= 0) {
345 while (ISSPACE(ch)) ch = ops->get(p);
346 switch (ch) {
347 case '-': f |= f_neg; /* and on */
348 case '+': do ch = ops->get(p); while (ISSPACE(ch));
349 }
350 }
351
352 /* --- If we don't have a fixed radix, then parse one from the input --- *
353 *
354 * This is moderately easy if the input starts with `0x' or similar. If it
355 * starts with `0' and something else, then it might be octal, or just a
356 * plain old zero. Finally, it might start with a leading `NN_', in which
357 * case we carefully collect the decimal number until we're sure it's
358 * either a radix prefix (in which case we accept it and start over) or it
359 * isn't (in which case it's actually the start of a large number we need
360 * to read).
361 */
362
363 if (radix == 0) {
364 if (ch == '0') {
365 ch = ops->get(p);
366 switch (ch) {
367 case 'x': case 'X': radix = 16; goto fetch;
368 case 'o': case 'O': radix = 8; goto fetch;
369 case 'b': case 'B': radix = 2; goto fetch;
370 fetch: ch = ops->get(p); break;
371 default: radix = 8; f |= f_ok; break;
372 }
373 } else {
374 if ((d = char_digit(ch, 10)) < 0) { ops->unget(ch, p); return (0); }
375 for (;;) {
376 t = 10*t + d;
377 ch = ops->get(p);
378 if (t > 52) break;
379 if ((d = char_digit(ch, 10)) < 0) break;
380 }
381 if (ch != '_' || t > 52) radix = 10;
382 else {
383 radix = t; t = 0;
384 ch = ops->get(p);
385 }
386 }
387 }
388
389 /* --- We're now ready to dispatch to the correct handler --- */
390
391 rd = radix < 0 ? -radix : radix;
392 ops->unget(ch, p);
393 switch (rd) {
394 case 2: m = read_binary(radix, 1, nf, ops, p); break;
395 case 4: m = read_binary(radix, 2, nf, ops, p); break;
396 case 8: m = read_binary(radix, 3, nf, ops, p); break;
397 case 16: m = read_binary(radix, 4, nf, ops, p); break;
398 case 32: m = read_binary(radix, 5, nf, ops, p); break;
399 case 64: m = read_binary(radix, 6, nf, ops, p); break;
400 case 128: m = read_binary(radix, 7, nf, ops, p); break;
401 default: m = read_general(radix, t, nf, ops, p); break;
402 }
403
404 /* --- That didn't work --- *
405 *
406 * If we've already read something then return that. Otherwise it's an
407 * error.
408 */
409
410 if (!m) {
411 if (f & f_ok) return (MP_ZERO);
412 else return (0);
413 }
414
415 /* --- Negate the result if we should do that --- */
416
417 if (f & f_neg) m = mp_neg(m, m);
418
419 /* --- And we're all done --- */
420
421 return (m);
422
423 #undef f_neg
424 #undef f_ok
425 }
426
427 /*----- Output ------------------------------------------------------------*/
428
429 /* --- @mp_write@ --- *
430 *
431 * Arguments: @mp *m@ = pointer to a multi-precision integer
432 * @int radix@ = radix to use when writing the number out
433 * @const mptext_ops *ops@ = pointer to an operations block
434 * @void *p@ = data for the operations block
435 *
436 * Returns: Zero if it worked, nonzero otherwise.
437 *
438 * Use: Writes a large integer in textual form.
439 */
440
441 static int digit_char(int d, int radix)
442 {
443 if (radix < 0) return (d);
444 else if (d < 10) return (d + '0');
445 else if (d < 26) return (d - 10 + 'a');
446 else return (d - 36 + 'A');
447 }
448
449 /* --- Simple case --- *
450 *
451 * Use a fixed-sized buffer and single-precision arithmetic to pick off
452 * low-order digits. Put each digit in a buffer, working backwards from the
453 * end. If the buffer becomes full, recurse to get another one. Ensure that
454 * there are at least @z@ digits by writing leading zeroes if there aren't
455 * enough real digits.
456 */
457
458 static int write_simple(mpw n, int radix, unsigned z,
459 const mptext_ops *ops, void *p)
460 {
461 int rc = 0;
462 char buf[64];
463 unsigned i = sizeof(buf);
464 int rd = radix > 0 ? radix : -radix;
465 mpw x;
466
467 do {
468 x = n % rd; n /= rd;
469 buf[--i] = digit_char(x, radix);
470 if (z) z--;
471 } while (i && n);
472
473 if (n)
474 rc = write_simple(n, radix, z, ops, p);
475 else {
476 char zbuf[32];
477 memset(zbuf, (radix < 0) ? 0 : '0', sizeof(zbuf));
478 while (!rc && z >= sizeof(zbuf)) {
479 rc = ops->put(zbuf, sizeof(zbuf), p);
480 z -= sizeof(zbuf);
481 }
482 if (!rc && z) rc = ops->put(zbuf, z, p);
483 }
484 if (!rc) rc = ops->put(buf + i, sizeof(buf) - i, p);
485 BURN(buf);
486 return (rc);
487 }
488
489 /* --- Complicated case --- *
490 *
491 * If the number is small, fall back to the simple case above. Otherwise
492 * divide and take remainder by current large power of the radix, and emit
493 * each separately. Don't emit a zero quotient. Be very careful about
494 * leading zeroes on the remainder part, because they're deeply significant.
495 */
496
497 static int write_complicated(mp *m, int radix, mp **pr,
498 unsigned i, unsigned z,
499 const mptext_ops *ops, void *p)
500 {
501 int rc = 0;
502 mp *q = MP_NEW;
503 unsigned d = 1 << i;
504
505 if (MP_LEN(m) < 2)
506 return (write_simple(MP_LEN(m) ? m->v[0] : 0, radix, z, ops, p));
507
508 assert(i);
509 mp_div(&q, &m, m, pr[i]);
510 if (MP_ZEROP(q)) d = z;
511 else {
512 if (z > d) z -= d;
513 else z = 0;
514 rc = write_complicated(q, radix, pr, i - 1, z, ops, p);
515 }
516 if (!rc) rc = write_complicated(m, radix, pr, i - 1, d, ops, p);
517 mp_drop(q);
518 return (rc);
519 }
520
521 /* --- Binary case --- *
522 *
523 * Special case for binary output. Goes much faster.
524 */
525
526 static int write_binary(mp *m, int bit, int radix,
527 const mptext_ops *ops, void *p)
528 {
529 mpw *v;
530 mpw a;
531 int rc = 0;
532 unsigned b;
533 unsigned mask;
534 unsigned long n;
535 unsigned f = 0;
536 char buf[8], *q;
537 unsigned x;
538
539 #define f_out 1u
540
541 /* --- Work out where to start --- */
542
543 n = mp_bits(m);
544 if (n % bit) n += bit - (n % bit);
545 b = n % MPW_BITS;
546 n /= MPW_BITS;
547
548 if (n >= MP_LEN(m)) {
549 n--;
550 b += MPW_BITS;
551 }
552
553 v = m->v + n;
554 a = *v;
555 mask = (1 << bit) - 1;
556 q = buf;
557
558 /* --- Main code --- */
559
560 for (;;) {
561 if (b > bit) {
562 b -= bit;
563 x = a >> b;
564 } else {
565 x = a << (bit - b);
566 b += MPW_BITS - bit;
567 if (v == m->v) break;
568 a = *--v;
569 if (b < MPW_BITS) x |= a >> b;
570 }
571 x &= mask;
572 if (!x && !(f & f_out)) continue;
573
574 *q++ = digit_char(x, radix);
575 if (q >= buf + sizeof(buf)) {
576 if ((rc = ops->put(buf, sizeof(buf), p)) != 0) goto done;
577 q = buf;
578 }
579 f |= f_out;
580 }
581
582 x &= mask;
583 *q++ = digit_char(x, radix);
584 rc = ops->put(buf, q - buf, p);
585
586 done:
587 mp_drop(m);
588 return (rc);
589
590 #undef f_out
591 }
592
593 /* --- Main driver code --- */
594
595 int mp_write(mp *m, int radix, const mptext_ops *ops, void *p)
596 {
597 int rc;
598 mp *pr[DEPTH];
599 size_t target;
600 unsigned i = 0;
601 mp *z;
602
603 if (MP_EQ(m, MP_ZERO))
604 return (ops->put(radix > 0 ? "0" : "\0", 1, p));
605
606 /* --- Set various things up --- */
607
608 m = MP_COPY(m);
609 MP_SPLIT(m);
610
611 /* --- Check the radix for sensibleness --- */
612
613 if (radix > 0)
614 assert(((void)"ascii radix must be <= 62", radix <= 62));
615 else if (radix < 0)
616 assert(((void)"binary radix must fit in a byte", -radix <= UCHAR_MAX));
617 else
618 assert(((void)"radix can't be zero in mp_write", 0));
619
620 /* --- If the number is negative, sort that out --- */
621
622 if (MP_NEGP(m)) {
623 assert(radix > 0);
624 if (ops->put("-", 1, p)) return (EOF);
625 m->f &= ~MP_NEG;
626 }
627
628 /* --- Handle binary radix --- */
629
630 switch (radix) {
631 case 2: case -2: return (write_binary(m, 1, radix, ops, p));
632 case 4: case -4: return (write_binary(m, 2, radix, ops, p));
633 case 8: case -8: return (write_binary(m, 3, radix, ops, p));
634 case 16: case -16: return (write_binary(m, 4, radix, ops, p));
635 case 32: case -32: return (write_binary(m, 5, radix, ops, p));
636 case -64: return (write_binary(m, 6, radix, ops, p));
637 case -128: return (write_binary(m, 7, radix, ops, p));
638 }
639
640 /* --- If the number is small, do it the easy way --- */
641
642 if (MP_LEN(m) < 2)
643 rc = write_simple(MP_LEN(m) ? m->v[0] : 0, radix, 0, ops, p);
644
645 /* --- Use a clever algorithm --- *
646 *
647 * Square the radix repeatedly, remembering old results, until I get
648 * something more than half the size of the number @m@. Use this to divide
649 * the number: the quotient and remainder will be approximately the same
650 * size, and I'll have split them on a digit boundary, so I can just emit
651 * the quotient and remainder recursively, in order.
652 */
653
654 else {
655 target = (MP_LEN(m) + 1) / 2;
656 z = mp_new(1, 0);
657
658 /* --- Set up the exponent table --- */
659
660 z->v[0] = (radix > 0 ? radix : -radix);
661 z->f = 0;
662 for (;;) {
663 assert(((void)"Number is too unimaginably huge", i < DEPTH));
664 pr[i++] = z;
665 if (MP_LEN(z) > target) break;
666 z = mp_sqr(MP_NEW, z);
667 }
668
669 /* --- Write out the answer --- */
670
671 rc = write_complicated(m, radix, pr, i - 1, 0, ops, p);
672
673 /* --- Tidy away the array --- */
674
675 while (i > 0) mp_drop(pr[--i]);
676 }
677
678 /* --- Tidying up code --- */
679
680 MP_DROP(m);
681 return (rc);
682 }
683
684 /*----- Test rig ----------------------------------------------------------*/
685
686 #ifdef TEST_RIG
687
688 #include <mLib/testrig.h>
689
690 static int verify(dstr *v)
691 {
692 int ok = 1;
693 int ib = *(int *)v[0].buf, ob = *(int *)v[2].buf;
694 dstr d = DSTR_INIT;
695 size_t off = 0;
696 mp *m = mp_readdstr(MP_NEW, &v[1], &off, ib);
697 if (m) {
698 if (!ob) {
699 fprintf(stderr, "*** unexpected successful parse\n"
700 "*** input [%2i] = ", ib);
701 if (ib < 0)
702 type_hex.dump(&v[1], stderr);
703 else
704 fputs(v[1].buf, stderr);
705 mp_writedstr(m, &d, 10);
706 fprintf(stderr, "\n*** (value = %s)\n", d.buf);
707 ok = 0;
708 } else {
709 mp_writedstr(m, &d, ob);
710 if (d.len != v[3].len || MEMCMP(d.buf, !=, v[3].buf, d.len)) {
711 fprintf(stderr, "*** failed read or write\n"
712 "*** input [%2i] = ", ib);
713 if (ib < 0)
714 type_hex.dump(&v[1], stderr);
715 else
716 fputs(v[1].buf, stderr);
717 fprintf(stderr, "\n*** output [%2i] = ", ob);
718 if (ob < 0)
719 type_hex.dump(&d, stderr);
720 else
721 fputs(d.buf, stderr);
722 fprintf(stderr, "\n*** expected [%2i] = ", ob);
723 if (ob < 0)
724 type_hex.dump(&v[3], stderr);
725 else
726 fputs(v[3].buf, stderr);
727 fputc('\n', stderr);
728 ok = 0;
729 }
730 }
731 mp_drop(m);
732 } else {
733 if (ob) {
734 fprintf(stderr, "*** unexpected parse failure\n"
735 "*** input [%2i] = ", ib);
736 if (ib < 0)
737 type_hex.dump(&v[1], stderr);
738 else
739 fputs(v[1].buf, stderr);
740 fprintf(stderr, "\n*** expected [%2i] = ", ob);
741 if (ob < 0)
742 type_hex.dump(&v[3], stderr);
743 else
744 fputs(v[3].buf, stderr);
745 fputc('\n', stderr);
746 ok = 0;
747 }
748 }
749
750 if (v[1].len - off != v[4].len ||
751 MEMCMP(v[1].buf + off, !=, v[4].buf, v[4].len)) {
752 fprintf(stderr, "*** leftovers incorrect\n"
753 "*** input [%2i] = ", ib);
754 if (ib < 0)
755 type_hex.dump(&v[1], stderr);
756 else
757 fputs(v[1].buf, stderr);
758 fprintf(stderr, "\n*** expected `%s'\n"
759 "*** found `%s'\n",
760 v[4].buf, v[1].buf + off);
761 ok = 0;
762 }
763
764 dstr_destroy(&d);
765 assert(mparena_count(MPARENA_GLOBAL) == 0);
766 return (ok);
767 }
768
769 static test_chunk tests[] = {
770 { "mptext-ascii", verify,
771 { &type_int, &type_string, &type_int, &type_string, &type_string, 0 } },
772 { "mptext-bin-in", verify,
773 { &type_int, &type_hex, &type_int, &type_string, &type_string, 0 } },
774 { "mptext-bin-out", verify,
775 { &type_int, &type_string, &type_int, &type_hex, &type_string, 0 } },
776 { 0, 0, { 0 } }
777 };
778
779 int main(int argc, char *argv[])
780 {
781 sub_init();
782 test_run(argc, argv, tests, SRCDIR "/t/mptext");
783 return (0);
784 }
785
786 #endif
787
788 /*----- That's all, folks -------------------------------------------------*/