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