blob: 9421d5f6602e8a0d65fc2e226babce4e4d1e8f29 (
plain)
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
|
#include "os.h"
#include <mp.h>
#include "dat.h"
// convert an mpint into a little endian byte array (least significant byte first)
// return number of bytes converted
// if p == nil, allocate and result array
int
mptole(mpint *b, uchar *p, uint n, uchar **pp)
{
int i, j;
mpdigit x;
uchar *e, *s;
if(p == nil){
n = (b->top+1)*Dbytes;
p = malloc(n);
}
if(pp != nil)
*pp = p;
if(p == nil)
return -1;
memset(p, 0, n);
// special case 0
if(b->top == 0){
if(n < 1)
return -1;
else
return 0;
}
s = p;
e = s+n;
for(i = 0; i < b->top-1; i++){
x = b->p[i];
for(j = 0; j < Dbytes; j++){
if(p >= e)
return -1;
*p++ = x;
x >>= 8;
}
}
x = b->p[i];
while(x > 0){
if(p >= e)
return -1;
*p++ = x;
x >>= 8;
}
return p - s;
}
|