Commit | Line | Data |
---|---|---|
d3409d5e | 1 | /* -*-c-*- |
2 | * | |
d3409d5e | 3 | * Textual representation of multiprecision numbers |
4 | * | |
5 | * (c) 1999 Straylight/Edgeware | |
6 | */ | |
7 | ||
45c0fd36 | 8 | /*----- Licensing notice --------------------------------------------------* |
d3409d5e | 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. | |
45c0fd36 | 16 | * |
d3409d5e | 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. | |
45c0fd36 | 21 | * |
d3409d5e | 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 | ||
d3409d5e | 28 | /*----- Header files ------------------------------------------------------*/ |
29 | ||
30 | #include <ctype.h> | |
2b26f2d7 | 31 | #include <limits.h> |
d3409d5e | 32 | #include <stdio.h> |
33 | ||
d3409d5e | 34 | #include "mp.h" |
35 | #include "mptext.h" | |
e360a4f2 | 36 | #include "paranoia.h" |
d3409d5e | 37 | |
2b26f2d7 | 38 | /*----- Magical numbers ---------------------------------------------------*/ |
39 | ||
40 | /* --- Maximum recursion depth --- * | |
41 | * | |
45c0fd36 | 42 | * This is the number of bits in a @size_t@ object. Why? |
2b26f2d7 | 43 | * |
eaa515d8 | 44 | * To see this, let %$b = \textit{MPW\_MAX} + 1$% and let %$Z$% be the |
dd9199f0 | 45 | * largest @size_t@ value. Then the largest possible @mp@ is %$M - 1$% where |
46 | * %$M = b^Z$%. Let %$r$% be a radix to read or write. Since the recursion | |
47 | * squares the radix at each step, the highest number reached by the | |
48 | * recursion is %$d$%, where: | |
2b26f2d7 | 49 | * |
dd9199f0 | 50 | * %$r^{2^d} = b^Z$%. |
2b26f2d7 | 51 | * |
52 | * Solving gives that %$d = \lg \log_r b^Z$%. If %$r = 2$%, this is maximum, | |
53 | * so choosing %$d = \lg \lg b^Z = \lg (Z \lg b) = \lg Z + \lg \lg b$%. | |
54 | * | |
55 | * Expressing %$\lg Z$% as @CHAR_BIT * sizeof(size_t)@ yields an | |
56 | * overestimate, since a @size_t@ representation may contain `holes'. | |
57 | * Choosing to represent %$\lg \lg b$% by 10 is almost certainly sufficient | |
58 | * for `some time to come'. | |
59 | */ | |
60 | ||
61 | #define DEPTH (CHAR_BIT * sizeof(size_t) + 10) | |
62 | ||
626cd971 | 63 | /*----- Input -------------------------------------------------------------*/ |
d3409d5e | 64 | |
65 | /* --- @mp_read@ --- * | |
66 | * | |
67 | * Arguments: @mp *m@ = destination multiprecision number | |
68 | * @int radix@ = base to assume for data (or zero to guess) | |
69 | * @const mptext_ops *ops@ = pointer to operations block | |
70 | * @void *p@ = data for the operations block | |
71 | * | |
72 | * Returns: The integer read, or zero if it didn't work. | |
73 | * | |
74 | * Use: Reads an integer from some source. If the @radix@ is | |
75 | * specified, the number is assumed to be given in that radix, | |
76 | * with the letters `a' (either upper- or lower-case) upwards | |
77 | * standing for digits greater than 9. Otherwise, base 10 is | |
78 | * assumed unless the number starts with `0' (octal), `0x' (hex) | |
79 | * or `nnn_' (base `nnn'). An arbitrary amount of whitespace | |
80 | * before the number is ignored. | |
81 | */ | |
82 | ||
2b26f2d7 | 83 | /* --- About the algorithm --- * |
84 | * | |
85 | * The algorithm here is rather aggressive. I maintain an array of | |
86 | * successive squarings of the radix, and a stack of partial results, each | |
87 | * with a counter attached indicating which radix square to multiply by. | |
88 | * Once the item at the top of the stack reaches the same counter level as | |
89 | * the next item down, they are combined together and the result is given a | |
90 | * counter level one higher than either of the results. | |
91 | * | |
92 | * Gluing the results together at the end is slightly tricky. Pay attention | |
93 | * to the code. | |
94 | * | |
95 | * This is more complicated because of the need to handle the slightly | |
96 | * bizarre syntax. | |
97 | */ | |
98 | ||
d3409d5e | 99 | mp *mp_read(mp *m, int radix, const mptext_ops *ops, void *p) |
100 | { | |
2b26f2d7 | 101 | int ch; /* Current char being considered */ |
102 | unsigned f = 0; /* Flags about the current number */ | |
103 | int r; /* Radix to switch over to */ | |
104 | mpw rd; /* Radix as an @mp@ digit */ | |
105 | mp rr; /* The @mp@ for the radix */ | |
106 | unsigned nf = m ? m->f & MP_BURN : 0; /* New @mp@ flags */ | |
107 | ||
108 | /* --- Stacks --- */ | |
109 | ||
110 | mp *pow[DEPTH]; /* List of powers */ | |
111 | unsigned pows; /* Next index to fill */ | |
112 | struct { unsigned i; mp *m; } s[DEPTH]; /* Main stack */ | |
113 | unsigned sp; /* Current stack pointer */ | |
114 | ||
115 | /* --- Flags --- */ | |
d3409d5e | 116 | |
3bc9cb53 | 117 | #define f_neg 1u |
118 | #define f_ok 2u | |
a951033d | 119 | #define f_start 4u |
d3409d5e | 120 | |
2b26f2d7 | 121 | /* --- Initialize the stacks --- */ |
122 | ||
123 | mp_build(&rr, &rd, &rd + 1); | |
45c0fd36 | 124 | pow[0] = &rr; |
2b26f2d7 | 125 | pows = 1; |
126 | ||
127 | sp = 0; | |
128 | ||
d3409d5e | 129 | /* --- Initialize the destination number --- */ |
130 | ||
2b26f2d7 | 131 | if (m) |
132 | MP_DROP(m); | |
d3409d5e | 133 | |
134 | /* --- Read an initial character --- */ | |
135 | ||
136 | ch = ops->get(p); | |
572b324a MW |
137 | if (radix >= 0) { |
138 | while (isspace(ch)) | |
139 | ch = ops->get(p); | |
140 | } | |
d3409d5e | 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 | ||
626cd971 MW |
459 | /*----- Output ------------------------------------------------------------*/ |
460 | ||
d3409d5e | 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 | ||
626cd971 MW |
473 | static int digit_char(int d, int radix) |
474 | { | |
475 | if (radix < 0) return (d); | |
476 | else if (d < 10) return (d + '0'); | |
477 | else if (d < 26) return (d - 10 + 'a'); | |
478 | else return (d - 36 + 'A'); | |
479 | } | |
480 | ||
e360a4f2 | 481 | /* --- Simple case --- * |
482 | * | |
3bc9cb53 | 483 | * Use a fixed-sized buffer and single-precision arithmetic to pick off |
484 | * low-order digits. Put each digit in a buffer, working backwards from the | |
485 | * end. If the buffer becomes full, recurse to get another one. Ensure that | |
486 | * there are at least @z@ digits by writing leading zeroes if there aren't | |
487 | * enough real digits. | |
e360a4f2 | 488 | */ |
489 | ||
626cd971 MW |
490 | static int write_simple(mpw n, int radix, unsigned z, |
491 | const mptext_ops *ops, void *p) | |
e360a4f2 | 492 | { |
493 | int rc = 0; | |
494 | char buf[64]; | |
495 | unsigned i = sizeof(buf); | |
2b26f2d7 | 496 | int rd = radix > 0 ? radix : -radix; |
626cd971 | 497 | mpw x; |
e360a4f2 | 498 | |
499 | do { | |
626cd971 MW |
500 | x = n % rd; n /= rd; |
501 | buf[--i] = digit_char(x, radix); | |
502 | if (z) z--; | |
3bc9cb53 | 503 | } while (i && n); |
e360a4f2 | 504 | |
3bc9cb53 | 505 | if (n) |
626cd971 | 506 | rc = write_simple(n, radix, z, ops, p); |
e360a4f2 | 507 | else { |
a951033d | 508 | char zbuf[32]; |
509 | memset(zbuf, (radix < 0) ? 0 : '0', sizeof(zbuf)); | |
510 | while (!rc && z >= sizeof(zbuf)) { | |
511 | rc = ops->put(zbuf, sizeof(zbuf), p); | |
512 | z -= sizeof(zbuf); | |
e360a4f2 | 513 | } |
626cd971 | 514 | if (!rc && z) rc = ops->put(zbuf, z, p); |
e360a4f2 | 515 | } |
626cd971 | 516 | if (!rc) rc = ops->put(buf + i, sizeof(buf) - i, p); |
3bc9cb53 | 517 | BURN(buf); |
e360a4f2 | 518 | return (rc); |
519 | } | |
520 | ||
521 | /* --- Complicated case --- * | |
522 | * | |
523 | * If the number is small, fall back to the simple case above. Otherwise | |
524 | * divide and take remainder by current large power of the radix, and emit | |
525 | * each separately. Don't emit a zero quotient. Be very careful about | |
526 | * leading zeroes on the remainder part, because they're deeply significant. | |
527 | */ | |
528 | ||
626cd971 MW |
529 | static int write_complicated(mp *m, int radix, mp **pr, |
530 | unsigned i, unsigned z, | |
531 | const mptext_ops *ops, void *p) | |
e360a4f2 | 532 | { |
533 | int rc = 0; | |
534 | mp *q = MP_NEW; | |
535 | unsigned d = 1 << i; | |
536 | ||
3bc9cb53 | 537 | if (MP_LEN(m) < 2) |
626cd971 | 538 | return (write_simple(MP_LEN(m) ? m->v[0] : 0, radix, z, ops, p)); |
e360a4f2 | 539 | |
3bc9cb53 | 540 | assert(i); |
e360a4f2 | 541 | mp_div(&q, &m, m, pr[i]); |
626cd971 | 542 | if (MP_ZEROP(q)) d = z; |
e360a4f2 | 543 | else { |
626cd971 MW |
544 | if (z > d) z -= d; |
545 | else z = 0; | |
546 | rc = write_complicated(q, radix, pr, i - 1, z, ops, p); | |
e360a4f2 | 547 | } |
626cd971 | 548 | if (!rc) rc = write_complicated(m, radix, pr, i - 1, d, ops, p); |
e360a4f2 | 549 | mp_drop(q); |
550 | return (rc); | |
551 | } | |
552 | ||
a951033d | 553 | /* --- Binary case --- * |
554 | * | |
555 | * Special case for binary output. Goes much faster. | |
556 | */ | |
557 | ||
626cd971 MW |
558 | static int write_binary(mp *m, int bit, int radix, |
559 | const mptext_ops *ops, void *p) | |
a951033d | 560 | { |
561 | mpw *v; | |
562 | mpw a; | |
563 | int rc = 0; | |
564 | unsigned b; | |
565 | unsigned mask; | |
566 | unsigned long n; | |
567 | unsigned f = 0; | |
568 | char buf[8], *q; | |
569 | unsigned x; | |
a951033d | 570 | |
571 | #define f_out 1u | |
572 | ||
573 | /* --- Work out where to start --- */ | |
574 | ||
575 | n = mp_bits(m); | |
626cd971 | 576 | if (n % bit) n += bit - (n % bit); |
a951033d | 577 | b = n % MPW_BITS; |
578 | n /= MPW_BITS; | |
afd054c1 | 579 | |
580 | if (n >= MP_LEN(m)) { | |
a951033d | 581 | n--; |
582 | b += MPW_BITS; | |
583 | } | |
584 | ||
585 | v = m->v + n; | |
586 | a = *v; | |
587 | mask = (1 << bit) - 1; | |
588 | q = buf; | |
589 | ||
590 | /* --- Main code --- */ | |
591 | ||
592 | for (;;) { | |
593 | if (b > bit) { | |
594 | b -= bit; | |
595 | x = a >> b; | |
596 | } else { | |
597 | x = a << (bit - b); | |
598 | b += MPW_BITS - bit; | |
626cd971 | 599 | if (v == m->v) break; |
a951033d | 600 | a = *--v; |
626cd971 | 601 | if (b < MPW_BITS) x |= a >> b; |
a951033d | 602 | } |
603 | x &= mask; | |
626cd971 | 604 | if (!x && !(f & f_out)) continue; |
a951033d | 605 | |
626cd971 | 606 | *q++ = digit_char(x, radix); |
a951033d | 607 | if (q >= buf + sizeof(buf)) { |
626cd971 | 608 | if ((rc = ops->put(buf, sizeof(buf), p)) != 0) goto done; |
a951033d | 609 | q = buf; |
610 | } | |
611 | f |= f_out; | |
612 | } | |
613 | ||
614 | x &= mask; | |
626cd971 | 615 | *q++ = digit_char(x, radix); |
a951033d | 616 | rc = ops->put(buf, q - buf, p); |
617 | ||
618 | done: | |
619 | mp_drop(m); | |
620 | return (rc); | |
621 | ||
622 | #undef f_out | |
623 | } | |
624 | ||
e360a4f2 | 625 | /* --- Main driver code --- */ |
626 | ||
d3409d5e | 627 | int mp_write(mp *m, int radix, const mptext_ops *ops, void *p) |
628 | { | |
e360a4f2 | 629 | int rc; |
626cd971 MW |
630 | mp *pr[DEPTH]; |
631 | size_t target; | |
632 | unsigned i = 0; | |
633 | mp *z; | |
d3409d5e | 634 | |
afd054c1 | 635 | if (MP_EQ(m, MP_ZERO)) |
572b324a | 636 | return (ops->put(radix > 0 ? "0" : "\0", 1, p)); |
afd054c1 | 637 | |
d3409d5e | 638 | /* --- Set various things up --- */ |
639 | ||
640 | m = MP_COPY(m); | |
e360a4f2 | 641 | MP_SPLIT(m); |
d3409d5e | 642 | |
2b26f2d7 | 643 | /* --- Check the radix for sensibleness --- */ |
644 | ||
645 | if (radix > 0) | |
631673a1 | 646 | assert(((void)"ascii radix must be <= 62", radix <= 62)); |
2b26f2d7 | 647 | else if (radix < 0) |
25b5e686 | 648 | assert(((void)"binary radix must fit in a byte", -radix <= UCHAR_MAX)); |
2b26f2d7 | 649 | else |
650 | assert(((void)"radix can't be zero in mp_write", 0)); | |
651 | ||
d3409d5e | 652 | /* --- If the number is negative, sort that out --- */ |
653 | ||
a69a3efd | 654 | if (MP_NEGP(m)) { |
572b324a | 655 | assert(radix > 0); |
626cd971 | 656 | if (ops->put("-", 1, p)) return (EOF); |
2b26f2d7 | 657 | m->f &= ~MP_NEG; |
d3409d5e | 658 | } |
659 | ||
a951033d | 660 | /* --- Handle binary radix --- */ |
661 | ||
662 | switch (radix) { | |
626cd971 MW |
663 | case 2: case -2: return (write_binary(m, 1, radix, ops, p)); |
664 | case 4: case -4: return (write_binary(m, 2, radix, ops, p)); | |
665 | case 8: case -8: return (write_binary(m, 3, radix, ops, p)); | |
666 | case 16: case -16: return (write_binary(m, 4, radix, ops, p)); | |
667 | case 32: case -32: return (write_binary(m, 5, radix, ops, p)); | |
668 | case -64: return (write_binary(m, 6, radix, ops, p)); | |
669 | case -128: return (write_binary(m, 7, radix, ops, p)); | |
a951033d | 670 | } |
671 | ||
e360a4f2 | 672 | /* --- If the number is small, do it the easy way --- */ |
673 | ||
3bc9cb53 | 674 | if (MP_LEN(m) < 2) |
626cd971 | 675 | rc = write_simple(MP_LEN(m) ? m->v[0] : 0, radix, 0, ops, p); |
e360a4f2 | 676 | |
677 | /* --- Use a clever algorithm --- * | |
678 | * | |
679 | * Square the radix repeatedly, remembering old results, until I get | |
680 | * something more than half the size of the number @m@. Use this to divide | |
681 | * the number: the quotient and remainder will be approximately the same | |
682 | * size, and I'll have split them on a digit boundary, so I can just emit | |
683 | * the quotient and remainder recursively, in order. | |
e360a4f2 | 684 | */ |
685 | ||
686 | else { | |
626cd971 MW |
687 | target = (MP_LEN(m) + 1) / 2; |
688 | z = mp_new(1, 0); | |
e360a4f2 | 689 | |
690 | /* --- Set up the exponent table --- */ | |
691 | ||
2b26f2d7 | 692 | z->v[0] = (radix > 0 ? radix : -radix); |
e360a4f2 | 693 | z->f = 0; |
694 | for (;;) { | |
2b26f2d7 | 695 | assert(((void)"Number is too unimaginably huge", i < DEPTH)); |
e360a4f2 | 696 | pr[i++] = z; |
626cd971 | 697 | if (MP_LEN(z) > target) break; |
e360a4f2 | 698 | z = mp_sqr(MP_NEW, z); |
699 | } | |
d3409d5e | 700 | |
e360a4f2 | 701 | /* --- Write out the answer --- */ |
d3409d5e | 702 | |
626cd971 | 703 | rc = write_complicated(m, radix, pr, i - 1, 0, ops, p); |
d3409d5e | 704 | |
e360a4f2 | 705 | /* --- Tidy away the array --- */ |
d3409d5e | 706 | |
626cd971 | 707 | while (i > 0) mp_drop(pr[--i]); |
d3409d5e | 708 | } |
e360a4f2 | 709 | |
710 | /* --- Tidying up code --- */ | |
711 | ||
712 | MP_DROP(m); | |
713 | return (rc); | |
d3409d5e | 714 | } |
715 | ||
716 | /*----- Test rig ----------------------------------------------------------*/ | |
717 | ||
718 | #ifdef TEST_RIG | |
719 | ||
720 | #include <mLib/testrig.h> | |
721 | ||
722 | static int verify(dstr *v) | |
723 | { | |
724 | int ok = 1; | |
725 | int ib = *(int *)v[0].buf, ob = *(int *)v[2].buf; | |
726 | dstr d = DSTR_INIT; | |
50bea2af | 727 | size_t off = 0; |
728 | mp *m = mp_readdstr(MP_NEW, &v[1], &off, ib); | |
d3409d5e | 729 | if (m) { |
730 | if (!ob) { | |
731 | fprintf(stderr, "*** unexpected successful parse\n" | |
45c0fd36 | 732 | "*** input [%2i] = ", ib); |
2b26f2d7 | 733 | if (ib < 0) |
734 | type_hex.dump(&v[1], stderr); | |
735 | else | |
736 | fputs(v[1].buf, stderr); | |
d3409d5e | 737 | mp_writedstr(m, &d, 10); |
2b26f2d7 | 738 | fprintf(stderr, "\n*** (value = %s)\n", d.buf); |
d3409d5e | 739 | ok = 0; |
740 | } else { | |
741 | mp_writedstr(m, &d, ob); | |
742 | if (d.len != v[3].len || memcmp(d.buf, v[3].buf, d.len) != 0) { | |
743 | fprintf(stderr, "*** failed read or write\n" | |
45c0fd36 | 744 | "*** input [%2i] = ", ib); |
2b26f2d7 | 745 | if (ib < 0) |
746 | type_hex.dump(&v[1], stderr); | |
747 | else | |
748 | fputs(v[1].buf, stderr); | |
45c0fd36 | 749 | fprintf(stderr, "\n*** output [%2i] = ", ob); |
2b26f2d7 | 750 | if (ob < 0) |
751 | type_hex.dump(&d, stderr); | |
752 | else | |
753 | fputs(d.buf, stderr); | |
45c0fd36 | 754 | fprintf(stderr, "\n*** expected [%2i] = ", ob); |
2b26f2d7 | 755 | if (ob < 0) |
756 | type_hex.dump(&v[3], stderr); | |
757 | else | |
758 | fputs(v[3].buf, stderr); | |
759 | fputc('\n', stderr); | |
d3409d5e | 760 | ok = 0; |
761 | } | |
762 | } | |
763 | mp_drop(m); | |
764 | } else { | |
765 | if (ob) { | |
766 | fprintf(stderr, "*** unexpected parse failure\n" | |
45c0fd36 | 767 | "*** input [%2i] = ", ib); |
2b26f2d7 | 768 | if (ib < 0) |
769 | type_hex.dump(&v[1], stderr); | |
770 | else | |
771 | fputs(v[1].buf, stderr); | |
50bea2af | 772 | fprintf(stderr, "\n*** expected [%2i] = ", ob); |
2b26f2d7 | 773 | if (ob < 0) |
774 | type_hex.dump(&v[3], stderr); | |
775 | else | |
776 | fputs(v[3].buf, stderr); | |
777 | fputc('\n', stderr); | |
d3409d5e | 778 | ok = 0; |
779 | } | |
780 | } | |
781 | ||
50bea2af | 782 | if (v[1].len - off != v[4].len || |
783 | memcmp(v[1].buf + off, v[4].buf, v[4].len) != 0) { | |
784 | fprintf(stderr, "*** leftovers incorrect\n" | |
45c0fd36 | 785 | "*** input [%2i] = ", ib); |
50bea2af | 786 | if (ib < 0) |
787 | type_hex.dump(&v[1], stderr); | |
788 | else | |
789 | fputs(v[1].buf, stderr); | |
790 | fprintf(stderr, "\n*** expected `%s'\n" | |
45c0fd36 | 791 | "*** found `%s'\n", |
50bea2af | 792 | v[4].buf, v[1].buf + off); |
793 | ok = 0; | |
794 | } | |
45c0fd36 | 795 | |
d3409d5e | 796 | dstr_destroy(&d); |
9c3df6c0 | 797 | assert(mparena_count(MPARENA_GLOBAL) == 0); |
d3409d5e | 798 | return (ok); |
799 | } | |
800 | ||
801 | static test_chunk tests[] = { | |
2b26f2d7 | 802 | { "mptext-ascii", verify, |
50bea2af | 803 | { &type_int, &type_string, &type_int, &type_string, &type_string, 0 } }, |
2b26f2d7 | 804 | { "mptext-bin-in", verify, |
50bea2af | 805 | { &type_int, &type_hex, &type_int, &type_string, &type_string, 0 } }, |
2b26f2d7 | 806 | { "mptext-bin-out", verify, |
50bea2af | 807 | { &type_int, &type_string, &type_int, &type_hex, &type_string, 0 } }, |
d3409d5e | 808 | { 0, 0, { 0 } } |
809 | }; | |
810 | ||
811 | int main(int argc, char *argv[]) | |
812 | { | |
813 | sub_init(); | |
0f00dc4c | 814 | test_run(argc, argv, tests, SRCDIR "/t/mptext"); |
d3409d5e | 815 | return (0); |
816 | } | |
817 | ||
818 | #endif | |
819 | ||
820 | /*----- That's all, folks -------------------------------------------------*/ |