Alexandre Lision | 7fd5d3d | 2013-12-04 13:06:40 -0500 | [diff] [blame] | 1 | /* |
| 2 | * bnprint.c - Print a bignum, for debugging purposes. |
| 3 | * |
| 4 | * Copyright (c) 1995 Colin Plumb. All rights reserved. |
| 5 | * For licensing and other legal details, see the file legal.c. |
| 6 | */ |
| 7 | #ifndef HAVE_CONFIG_H |
| 8 | #define HAVE_CONFIG_H 0 |
| 9 | #endif |
| 10 | #if HAVE_CONFIG_H |
| 11 | #include "bnconfig.h" |
| 12 | #endif |
| 13 | |
| 14 | /* |
| 15 | * Some compilers complain about #if FOO if FOO isn't defined, |
| 16 | * so do the ANSI-mandated thing explicitly... |
| 17 | */ |
| 18 | #ifndef NO_STRING_H |
| 19 | #define NO_STRING_H 0 |
| 20 | #endif |
| 21 | #ifndef HAVE_STRINGS_H |
| 22 | #define HAVE_STRINGS_H 0 |
| 23 | #endif |
| 24 | |
| 25 | #include <stdio.h> |
| 26 | #include <stdint.h> |
| 27 | |
| 28 | #if !NO_STRING_H |
| 29 | #include <string.h> |
| 30 | #elif HAVE_STRINGS_H |
| 31 | #include <strings.h> |
| 32 | #endif |
| 33 | |
| 34 | #include "bn.h" |
| 35 | #include "bnprint.h" |
| 36 | |
| 37 | #include "kludge.h" |
| 38 | |
| 39 | int |
| 40 | bnPrint(FILE *f, char const *prefix, struct BigNum const *bn, |
| 41 | char const *suffix) |
| 42 | { |
| 43 | unsigned char temp[32]; /* How much to print on one line */ |
| 44 | unsigned len; |
| 45 | size_t i; |
| 46 | |
| 47 | if (prefix && fputs(prefix, f) < 0) |
| 48 | return EOF; |
| 49 | |
| 50 | len = (bnBits(bn) + 7)/ 8; |
| 51 | |
| 52 | if (!len) { |
| 53 | if (putc('0', f) < 0) |
| 54 | return EOF; |
| 55 | } else { |
| 56 | while (len > sizeof(temp)) { |
| 57 | len -= sizeof(temp); |
| 58 | bnExtractBigBytes(bn, temp, len, sizeof(temp)); |
| 59 | for (i = 0; i < sizeof(temp); i++) |
| 60 | if (fprintf(f, "%02X", temp[i]) < 0) |
| 61 | return EOF; |
| 62 | if (putc('\\', f) < 0 || putc('\n', f) < 0) |
| 63 | return EOF; |
| 64 | if (prefix) { |
| 65 | i = strlen(prefix); |
| 66 | while (i--) |
| 67 | if (putc(' ', f) < 0) |
| 68 | return EOF; |
| 69 | } |
| 70 | } |
| 71 | bnExtractBigBytes(bn, temp, 0, len); |
| 72 | for (i = 0; i < len; i++) |
| 73 | if (fprintf(f, "%02X", temp[i]) < 0) |
| 74 | return EOF; |
| 75 | } |
| 76 | return suffix ? fputs(suffix, f) : 0; |
| 77 | } |
| 78 | |
| 79 | /* |
| 80 | * Convert an ASCII character to digit value |
| 81 | */ |
| 82 | static int getAsciiDigit( uint32_t *d, int radix, char c ) |
| 83 | { |
| 84 | *d = 255; |
| 85 | |
| 86 | if( c >= 0x30 && c <= 0x39 ) |
| 87 | *d = c - 0x30; |
| 88 | if( c >= 0x41 && c <= 0x46 ) |
| 89 | *d = c - 0x37; |
| 90 | if( c >= 0x61 && c <= 0x66 ) |
| 91 | *d = c - 0x57; |
| 92 | |
| 93 | if( *d >= (uint32_t)radix ) |
| 94 | return( -1 ); |
| 95 | |
| 96 | return( 0 ); |
| 97 | } |
| 98 | |
| 99 | int |
| 100 | bnReadAscii(struct BigNum *X, char *s, int radix) |
| 101 | { |
| 102 | int slen = strlen(s); |
| 103 | int i, neg = 0; |
| 104 | uint32_t d; |
| 105 | |
| 106 | bnSetQ(X, 0); |
| 107 | for( i = 0; i < slen; i++ ) { |
| 108 | if(i == 0 && s[i] == '-') { |
| 109 | neg = 1; |
| 110 | continue; |
| 111 | } |
| 112 | getAsciiDigit(&d, radix, s[i]); |
| 113 | bnMulQ(X, X, radix); |
| 114 | |
| 115 | bnAddQ(X, d); |
| 116 | } |
| 117 | return(neg); |
| 118 | } |