1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
|
#include "os.h"
#include <mp.h>
#include <libsec.h>
#include "dat.h"
static struct {
int inited;
uchar t64[256];
uchar t32[256];
uchar t16[256];
uchar t10[256];
} tab;
enum {
INVAL= 255
};
static char set64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static char set32[] = "23456789abcdefghijkmnpqrstuvwxyz";
static char set16[] = "0123456789ABCDEF0123456789abcdef";
static char set10[] = "0123456789";
static void
init(void)
{
char *p;
memset(tab.t64, INVAL, sizeof(tab.t64));
memset(tab.t32, INVAL, sizeof(tab.t32));
memset(tab.t16, INVAL, sizeof(tab.t16));
memset(tab.t10, INVAL, sizeof(tab.t10));
for(p = set64; *p; p++)
tab.t64[(uchar)*p] = p-set64;
for(p = set32; *p; p++)
tab.t32[(uchar)*p] = p-set32;
for(p = set16; *p; p++)
tab.t16[(uchar)*p] = (p-set16)%16;
for(p = set10; *p; p++)
tab.t10[(uchar)*p] = (p-set10);
tab.inited = 1;
}
static char*
from16(char *a, mpint *b)
{
char *p, *next;
int i;
mpdigit x;
b->top = 0;
for(p = a; *p; p++)
if(tab.t16[*(uchar*)p] == INVAL)
break;
mpbits(b, (p-a)*4);
b->top = 0;
next = p;
while(p > a){
x = 0;
for(i = 0; i < Dbits; i += 4){
if(p <= a)
break;
x |= tab.t16[*(uchar*)--p]<<i;
}
b->p[b->top++] = x;
}
return next;
}
static ulong mppow10[] = {
1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000
};
static char*
from10(char *a, mpint *b)
{
ulong x, y;
mpint *pow, *r;
int i;
pow = mpnew(0);
r = mpnew(0);
b->top = 0;
for(;;){
// do a billion at a time in native arithmetic
x = 0;
for(i = 0; i < 9; i++){
y = tab.t10[*(uchar*)a];
if(y == INVAL)
break;
a++;
x *= 10;
x += y;
}
if(i == 0)
break;
// accumulate into mpint
uitomp(mppow10[i], pow);
uitomp(x, r);
mpmul(b, pow, b);
mpadd(b, r, b);
if(i != 9)
break;
}
mpfree(pow);
mpfree(r);
return a;
}
static char*
from64(char *a, mpint *b)
{
char *buf = a;
uchar *p;
int n, m;
for(; tab.t64[*(uchar*)a] != INVAL; a++)
;
n = a-buf;
mpbits(b, n*6);
p = malloc(n);
if(p == nil)
return a;
m = dec64(p, n, buf, n);
betomp(p, m, b);
free(p);
return a;
}
static char*
from32(char *a, mpint *b)
{
char *buf = a;
uchar *p;
int n, m;
for(; tab.t64[*(uchar*)a] != INVAL; a++)
;
n = a-buf;
mpbits(b, n*5);
p = malloc(n);
if(p == nil)
return a;
m = dec32(p, n, buf, n);
betomp(p, m, b);
free(p);
return a;
}
mpint*
strtomp(char *a, char **pp, int base, mpint *b)
{
int sign;
char *e;
if(b == nil)
b = mpnew(0);
if(tab.inited == 0)
init();
while(*a==' ' || *a=='\t')
a++;
sign = 1;
for(;; a++){
switch(*a){
case '-':
sign *= -1;
continue;
}
break;
}
switch(base){
case 10:
e = from10(a, b);
break;
default:
case 16:
e = from16(a, b);
break;
case 32:
e = from32(a, b);
break;
case 64:
e = from64(a, b);
break;
}
// if no characters parsed, there wasn't a number to convert
if(e == a)
return nil;
mpnorm(b);
b->sign = sign;
if(pp != nil)
*pp = e;
return b;
}
|