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