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