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
|
#include "os.h"
#include <mp.h>
#include <libsec.h>
#include "dat.h"
static int
to64(mpint *b, char *buf, int len)
{
uchar *p;
int n, rv;
p = nil;
n = mptobe(b, nil, 0, &p);
if(n < 0)
return -1;
rv = enc64(buf, len, p, n);
free(p);
return rv;
}
static int
to32(mpint *b, char *buf, int len)
{
uchar *p;
int n, rv;
// leave room for a multiple of 5 buffer size
n = b->top*Dbytes + 5;
p = malloc(n);
if(p == nil)
return -1;
n = mptobe(b, p, n, nil);
if(n < 0)
return -1;
// round up buffer size, enc32 only accepts a multiple of 5
if(n%5)
n += 5 - (n%5);
rv = enc32(buf, len, p, n);
free(p);
return rv;
}
static char set16[] = "0123456789ABCDEF";
static int
to16(mpint *b, char *buf, int len)
{
mpdigit *p, x;
int i, j;
char *out, *eout;
if(len < 1)
return -1;
out = buf;
eout = buf+len;
for(p = &b->p[b->top-1]; p >= b->p; p--){
x = *p;
for(i = Dbits-4; i >= 0; i -= 4){
j = 0xf & (x>>i);
if(j != 0 || out != buf){
if(out >= eout)
return -1;
*out++ = set16[j];
}
}
}
if(out == buf)
*out++ = '0';
if(out >= eout)
return -1;
*out = 0;
return 0;
}
static char*
modbillion(int rem, ulong r, char *out, char *buf)
{
ulong rr;
int i;
for(i = 0; i < 9; i++){
rr = r%10;
r /= 10;
if(out <= buf)
return nil;
*--out = '0' + rr;
if(rem == 0 && r == 0)
break;
}
return out;
}
static int
to10(mpint *b, char *buf, int len)
{
mpint *d, *r, *billion;
char *out;
if(len < 1)
return -1;
d = mpcopy(b);
r = mpnew(0);
billion = uitomp(1000000000, nil);
out = buf+len;
*--out = 0;
do {
mpdiv(d, billion, d, r);
out = modbillion(d->top, r->p[0], out, buf);
if(out == nil)
break;
} while(d->top != 0);
mpfree(d);
mpfree(r);
mpfree(billion);
if(out == nil)
return -1;
len -= out-buf;
if(out != buf)
memmove(buf, out, len);
return 0;
}
int
mpfmt(Fmt *fmt)
{
mpint *b;
char *p;
b = va_arg(fmt->args, mpint*);
if(b == nil)
return fmtstrcpy(fmt, "*");
p = mptoa(b, fmt->prec, nil, 0);
fmt->flags &= ~FmtPrec;
if(p == nil)
return fmtstrcpy(fmt, "*");
else{
fmtstrcpy(fmt, p);
free(p);
return 0;
}
}
char*
mptoa(mpint *b, int base, char *buf, int len)
{
char *out;
int rv, alloced;
alloced = 0;
if(buf == nil){
len = ((b->top+1)*Dbits+2)/3 + 1;
buf = malloc(len);
if(buf == nil)
return nil;
alloced = 1;
}
if(len < 2)
return nil;
out = buf;
if(b->sign < 0){
*out++ = '-';
len--;
}
switch(base){
case 64:
rv = to64(b, out, len);
break;
case 32:
rv = to32(b, out, len);
break;
default:
case 16:
rv = to16(b, out, len);
break;
case 10:
rv = to10(b, out, len);
break;
}
if(rv < 0){
if(alloced)
free(buf);
return nil;
}
return buf;
}
|