| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * Textual representation of multiprecision numbers |
| 4 | * |
| 5 | * (c) 1999 Straylight/Edgeware |
| 6 | */ |
| 7 | |
| 8 | /*----- Licensing notice --------------------------------------------------* |
| 9 | * |
| 10 | * This file is part of Catacomb. |
| 11 | * |
| 12 | * Catacomb is free software; you can redistribute it and/or modify |
| 13 | * it under the terms of the GNU Library General Public License as |
| 14 | * published by the Free Software Foundation; either version 2 of the |
| 15 | * License, or (at your option) any later version. |
| 16 | * |
| 17 | * Catacomb is distributed in the hope that it will be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | * GNU Library General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU Library General Public |
| 23 | * License along with Catacomb; if not, write to the Free |
| 24 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
| 25 | * MA 02111-1307, USA. |
| 26 | */ |
| 27 | |
| 28 | /*----- Header files ------------------------------------------------------*/ |
| 29 | |
| 30 | #include <ctype.h> |
| 31 | #include <limits.h> |
| 32 | #include <stdio.h> |
| 33 | |
| 34 | #include "mp.h" |
| 35 | #include "mptext.h" |
| 36 | #include "paranoia.h" |
| 37 | |
| 38 | /*----- Magical numbers ---------------------------------------------------*/ |
| 39 | |
| 40 | /* --- Maximum recursion depth --- * |
| 41 | * |
| 42 | * This is the number of bits in a @size_t@ object. Why? |
| 43 | * |
| 44 | * To see this, let %$b = \textit{MPW\_MAX} + 1$% and let %$Z$% be the |
| 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: |
| 49 | * |
| 50 | * %$r^{2^d} = b^Z$%. |
| 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 | |
| 63 | /*----- Main code ---------------------------------------------------------*/ |
| 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 | |
| 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 | |
| 99 | mp *mp_read(mp *m, int radix, const mptext_ops *ops, void *p) |
| 100 | { |
| 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 --- */ |
| 116 | |
| 117 | #define f_neg 1u |
| 118 | #define f_ok 2u |
| 119 | #define f_start 4u |
| 120 | |
| 121 | /* --- Initialize the stacks --- */ |
| 122 | |
| 123 | mp_build(&rr, &rd, &rd + 1); |
| 124 | pow[0] = &rr; |
| 125 | pows = 1; |
| 126 | |
| 127 | sp = 0; |
| 128 | |
| 129 | /* --- Initialize the destination number --- */ |
| 130 | |
| 131 | if (m) |
| 132 | MP_DROP(m); |
| 133 | |
| 134 | /* --- Read an initial character --- */ |
| 135 | |
| 136 | ch = ops->get(p); |
| 137 | if (radix >= 0) { |
| 138 | while (isspace(ch)) |
| 139 | ch = ops->get(p); |
| 140 | } |
| 141 | |
| 142 | /* --- Handle an initial sign --- */ |
| 143 | |
| 144 | if (radix >= 0 && (ch == '-' || ch == '+')) { |
| 145 | if (ch == '-') |
| 146 | f |= f_neg; |
| 147 | do ch = ops->get(p); while isspace(ch); |
| 148 | } |
| 149 | |
| 150 | /* --- If the radix is zero, look for leading zeros --- */ |
| 151 | |
| 152 | if (radix > 0) { |
| 153 | assert(((void)"ascii radix must be <= 62", radix <= 62)); |
| 154 | rd = radix; |
| 155 | r = -1; |
| 156 | } else if (radix < 0) { |
| 157 | rd = -radix; |
| 158 | assert(((void)"binary radix must fit in a byte", rd <= UCHAR_MAX)); |
| 159 | r = -1; |
| 160 | } else if (ch != '0') { |
| 161 | rd = 10; |
| 162 | r = 0; |
| 163 | } else { |
| 164 | ch = ops->get(p); |
| 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; |
| 181 | } |
| 182 | r = -1; |
| 183 | } |
| 184 | |
| 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 { |
| 241 | if (rd <= 36) |
| 242 | ch = tolower(ch); |
| 243 | if (ch >= 'a' && ch <= 'z') /* ASCII dependent! */ |
| 244 | x = ch - 'a' + 10; |
| 245 | else if (ch >= 'A' && ch <= 'Z') |
| 246 | x = ch - 'A' + 36; |
| 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); |
| 265 | b += MPW_BITS - bit; |
| 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 | } |
| 294 | ops->unget(ch, p); |
| 295 | goto done; |
| 296 | }} |
| 297 | |
| 298 | /* --- Time to start --- */ |
| 299 | |
| 300 | for (;; ch = ops->get(p)) { |
| 301 | unsigned x; |
| 302 | |
| 303 | if (ch < 0) |
| 304 | break; |
| 305 | |
| 306 | /* --- An underscore indicates a numbered base --- */ |
| 307 | |
| 308 | if (ch == '_' && r > 0 && r <= 62) { |
| 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; |
| 323 | r = -1; |
| 324 | f &= ~f_ok; |
| 325 | ch = ops->get(p); |
| 326 | goto restart; |
| 327 | } |
| 328 | |
| 329 | /* --- Check that the character is a digit and in range --- */ |
| 330 | |
| 331 | if (radix < 0) |
| 332 | x = ch % rd; |
| 333 | else { |
| 334 | if (!isalnum(ch)) |
| 335 | break; |
| 336 | if (ch >= '0' && ch <= '9') |
| 337 | x = ch - '0'; |
| 338 | else { |
| 339 | if (rd <= 36) |
| 340 | ch = tolower(ch); |
| 341 | if (ch >= 'a' && ch <= 'z') /* ASCII dependent! */ |
| 342 | x = ch - 'a' + 10; |
| 343 | else if (ch >= 'A' && ch <= 'Z') |
| 344 | x = ch - 'A' + 36; |
| 345 | else |
| 346 | break; |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | /* --- Sort out what to do with the character --- */ |
| 351 | |
| 352 | if (x >= 10 && r >= 0) |
| 353 | r = -1; |
| 354 | if (x >= rd) |
| 355 | break; |
| 356 | |
| 357 | if (r >= 0) |
| 358 | r = r * 10 + x; |
| 359 | |
| 360 | /* --- Stick the character on the end of my integer --- */ |
| 361 | |
| 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 | } |
| 389 | f |= f_ok; |
| 390 | sp++; |
| 391 | } |
| 392 | |
| 393 | ops->unget(ch, p); |
| 394 | |
| 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 | |
| 441 | /* --- Bail out if the number was bad --- */ |
| 442 | |
| 443 | done: |
| 444 | if (!(f & f_ok)) |
| 445 | return (0); |
| 446 | |
| 447 | /* --- Set the sign and return --- */ |
| 448 | |
| 449 | if (f & f_neg) |
| 450 | m->f |= MP_NEG; |
| 451 | MP_SHRINK(m); |
| 452 | return (m); |
| 453 | |
| 454 | #undef f_start |
| 455 | #undef f_neg |
| 456 | #undef f_ok |
| 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 | |
| 471 | /* --- Simple case --- * |
| 472 | * |
| 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. |
| 478 | */ |
| 479 | |
| 480 | static int simple(mpw n, int radix, unsigned z, |
| 481 | const mptext_ops *ops, void *p) |
| 482 | { |
| 483 | int rc = 0; |
| 484 | char buf[64]; |
| 485 | unsigned i = sizeof(buf); |
| 486 | int rd = radix > 0 ? radix : -radix; |
| 487 | |
| 488 | do { |
| 489 | int ch; |
| 490 | mpw x; |
| 491 | |
| 492 | x = n % rd; |
| 493 | n /= rd; |
| 494 | if (radix < 0) |
| 495 | ch = x; |
| 496 | else if (x < 10) |
| 497 | ch = '0' + x; |
| 498 | else if (x < 36) /* Ascii specific */ |
| 499 | ch = 'a' + x - 10; |
| 500 | else |
| 501 | ch = 'A' + x - 36; |
| 502 | buf[--i] = ch; |
| 503 | if (z) |
| 504 | z--; |
| 505 | } while (i && n); |
| 506 | |
| 507 | if (n) |
| 508 | rc = simple(n, radix, z, ops, p); |
| 509 | else { |
| 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); |
| 515 | } |
| 516 | if (!rc && z) |
| 517 | rc = ops->put(zbuf, z, p); |
| 518 | } |
| 519 | if (!rc) |
| 520 | rc = ops->put(buf + i, sizeof(buf) - i, p); |
| 521 | BURN(buf); |
| 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 | |
| 540 | if (MP_LEN(m) < 2) |
| 541 | return (simple(MP_LEN(m) ? m->v[0] : 0, radix, z, ops, p)); |
| 542 | |
| 543 | assert(i); |
| 544 | mp_div(&q, &m, m, pr[i]); |
| 545 | if (MP_ZEROP(q)) |
| 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 | |
| 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); |
| 583 | if (n % bit) |
| 584 | n += bit - (n % bit); |
| 585 | b = n % MPW_BITS; |
| 586 | n /= MPW_BITS; |
| 587 | |
| 588 | if (n >= MP_LEN(m)) { |
| 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; |
| 621 | else if (x < 36) |
| 622 | ch = 'a' + x - 10; /* Ascii specific */ |
| 623 | else |
| 624 | ch = 'A' + x - 36; |
| 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; |
| 639 | else if (x < 36) |
| 640 | ch = 'a' + x - 10; /* Ascii specific */ |
| 641 | else |
| 642 | ch = 'A' + x - 36; |
| 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 | |
| 653 | /* --- Main driver code --- */ |
| 654 | |
| 655 | int mp_write(mp *m, int radix, const mptext_ops *ops, void *p) |
| 656 | { |
| 657 | int rc; |
| 658 | |
| 659 | if (MP_EQ(m, MP_ZERO)) |
| 660 | return (ops->put(radix > 0 ? "0" : "\0", 1, p)); |
| 661 | |
| 662 | /* --- Set various things up --- */ |
| 663 | |
| 664 | m = MP_COPY(m); |
| 665 | MP_SPLIT(m); |
| 666 | |
| 667 | /* --- Check the radix for sensibleness --- */ |
| 668 | |
| 669 | if (radix > 0) |
| 670 | assert(((void)"ascii radix must be <= 62", radix <= 62)); |
| 671 | else if (radix < 0) |
| 672 | assert(((void)"binary radix must fit in a byte", -radix <= UCHAR_MAX)); |
| 673 | else |
| 674 | assert(((void)"radix can't be zero in mp_write", 0)); |
| 675 | |
| 676 | /* --- If the number is negative, sort that out --- */ |
| 677 | |
| 678 | if (MP_NEGP(m)) { |
| 679 | assert(radix > 0); |
| 680 | if (ops->put("-", 1, p)) |
| 681 | return (EOF); |
| 682 | m->f &= ~MP_NEG; |
| 683 | } |
| 684 | |
| 685 | /* --- Handle binary radix --- */ |
| 686 | |
| 687 | switch (radix) { |
| 688 | case 2: case -2: return (binary(m, 1, radix, ops, p)); |
| 689 | case 4: case -4: return (binary(m, 2, radix, ops, p)); |
| 690 | case 8: case -8: return (binary(m, 3, radix, ops, p)); |
| 691 | case 16: case -16: return (binary(m, 4, radix, ops, p)); |
| 692 | case 32: case -32: return (binary(m, 5, radix, ops, p)); |
| 693 | case -64: return (binary(m, 6, radix, ops, p)); |
| 694 | case -128: return (binary(m, 7, radix, ops, p)); |
| 695 | } |
| 696 | |
| 697 | /* --- If the number is small, do it the easy way --- */ |
| 698 | |
| 699 | if (MP_LEN(m) < 2) |
| 700 | rc = simple(MP_LEN(m) ? m->v[0] : 0, radix, 0, ops, p); |
| 701 | |
| 702 | /* --- Use a clever algorithm --- * |
| 703 | * |
| 704 | * Square the radix repeatedly, remembering old results, until I get |
| 705 | * something more than half the size of the number @m@. Use this to divide |
| 706 | * the number: the quotient and remainder will be approximately the same |
| 707 | * size, and I'll have split them on a digit boundary, so I can just emit |
| 708 | * the quotient and remainder recursively, in order. |
| 709 | */ |
| 710 | |
| 711 | else { |
| 712 | mp *pr[DEPTH]; |
| 713 | size_t target = (MP_LEN(m) + 1) / 2; |
| 714 | unsigned i = 0; |
| 715 | mp *z = mp_new(1, 0); |
| 716 | |
| 717 | /* --- Set up the exponent table --- */ |
| 718 | |
| 719 | z->v[0] = (radix > 0 ? radix : -radix); |
| 720 | z->f = 0; |
| 721 | for (;;) { |
| 722 | assert(((void)"Number is too unimaginably huge", i < DEPTH)); |
| 723 | pr[i++] = z; |
| 724 | if (MP_LEN(z) > target) |
| 725 | break; |
| 726 | z = mp_sqr(MP_NEW, z); |
| 727 | } |
| 728 | |
| 729 | /* --- Write out the answer --- */ |
| 730 | |
| 731 | rc = complicated(m, radix, pr, i - 1, 0, ops, p); |
| 732 | |
| 733 | /* --- Tidy away the array --- */ |
| 734 | |
| 735 | while (i > 0) |
| 736 | mp_drop(pr[--i]); |
| 737 | } |
| 738 | |
| 739 | /* --- Tidying up code --- */ |
| 740 | |
| 741 | MP_DROP(m); |
| 742 | return (rc); |
| 743 | } |
| 744 | |
| 745 | /*----- Test rig ----------------------------------------------------------*/ |
| 746 | |
| 747 | #ifdef TEST_RIG |
| 748 | |
| 749 | #include <mLib/testrig.h> |
| 750 | |
| 751 | static int verify(dstr *v) |
| 752 | { |
| 753 | int ok = 1; |
| 754 | int ib = *(int *)v[0].buf, ob = *(int *)v[2].buf; |
| 755 | dstr d = DSTR_INIT; |
| 756 | size_t off = 0; |
| 757 | mp *m = mp_readdstr(MP_NEW, &v[1], &off, ib); |
| 758 | if (m) { |
| 759 | if (!ob) { |
| 760 | fprintf(stderr, "*** unexpected successful parse\n" |
| 761 | "*** input [%2i] = ", ib); |
| 762 | if (ib < 0) |
| 763 | type_hex.dump(&v[1], stderr); |
| 764 | else |
| 765 | fputs(v[1].buf, stderr); |
| 766 | mp_writedstr(m, &d, 10); |
| 767 | fprintf(stderr, "\n*** (value = %s)\n", d.buf); |
| 768 | ok = 0; |
| 769 | } else { |
| 770 | mp_writedstr(m, &d, ob); |
| 771 | if (d.len != v[3].len || memcmp(d.buf, v[3].buf, d.len) != 0) { |
| 772 | fprintf(stderr, "*** failed read or write\n" |
| 773 | "*** input [%2i] = ", ib); |
| 774 | if (ib < 0) |
| 775 | type_hex.dump(&v[1], stderr); |
| 776 | else |
| 777 | fputs(v[1].buf, stderr); |
| 778 | fprintf(stderr, "\n*** output [%2i] = ", ob); |
| 779 | if (ob < 0) |
| 780 | type_hex.dump(&d, stderr); |
| 781 | else |
| 782 | fputs(d.buf, stderr); |
| 783 | fprintf(stderr, "\n*** expected [%2i] = ", ob); |
| 784 | if (ob < 0) |
| 785 | type_hex.dump(&v[3], stderr); |
| 786 | else |
| 787 | fputs(v[3].buf, stderr); |
| 788 | fputc('\n', stderr); |
| 789 | ok = 0; |
| 790 | } |
| 791 | } |
| 792 | mp_drop(m); |
| 793 | } else { |
| 794 | if (ob) { |
| 795 | fprintf(stderr, "*** unexpected parse failure\n" |
| 796 | "*** input [%2i] = ", ib); |
| 797 | if (ib < 0) |
| 798 | type_hex.dump(&v[1], stderr); |
| 799 | else |
| 800 | fputs(v[1].buf, stderr); |
| 801 | fprintf(stderr, "\n*** expected [%2i] = ", ob); |
| 802 | if (ob < 0) |
| 803 | type_hex.dump(&v[3], stderr); |
| 804 | else |
| 805 | fputs(v[3].buf, stderr); |
| 806 | fputc('\n', stderr); |
| 807 | ok = 0; |
| 808 | } |
| 809 | } |
| 810 | |
| 811 | if (v[1].len - off != v[4].len || |
| 812 | memcmp(v[1].buf + off, v[4].buf, v[4].len) != 0) { |
| 813 | fprintf(stderr, "*** leftovers incorrect\n" |
| 814 | "*** input [%2i] = ", ib); |
| 815 | if (ib < 0) |
| 816 | type_hex.dump(&v[1], stderr); |
| 817 | else |
| 818 | fputs(v[1].buf, stderr); |
| 819 | fprintf(stderr, "\n*** expected `%s'\n" |
| 820 | "*** found `%s'\n", |
| 821 | v[4].buf, v[1].buf + off); |
| 822 | ok = 0; |
| 823 | } |
| 824 | |
| 825 | dstr_destroy(&d); |
| 826 | assert(mparena_count(MPARENA_GLOBAL) == 0); |
| 827 | return (ok); |
| 828 | } |
| 829 | |
| 830 | static test_chunk tests[] = { |
| 831 | { "mptext-ascii", verify, |
| 832 | { &type_int, &type_string, &type_int, &type_string, &type_string, 0 } }, |
| 833 | { "mptext-bin-in", verify, |
| 834 | { &type_int, &type_hex, &type_int, &type_string, &type_string, 0 } }, |
| 835 | { "mptext-bin-out", verify, |
| 836 | { &type_int, &type_string, &type_int, &type_hex, &type_string, 0 } }, |
| 837 | { 0, 0, { 0 } } |
| 838 | }; |
| 839 | |
| 840 | int main(int argc, char *argv[]) |
| 841 | { |
| 842 | sub_init(); |
| 843 | test_run(argc, argv, tests, SRCDIR "/t/mptext"); |
| 844 | return (0); |
| 845 | } |
| 846 | |
| 847 | #endif |
| 848 | |
| 849 | /*----- That's all, folks -------------------------------------------------*/ |