blob: d49225e9edbc4bd91bfb4ebf111b82c12134d529 [file] [log] [blame]
rscb3f61792004-03-21 14:06:38 +00001#include "os.h"
2#include <mp.h>
3#include "dat.h"
4
rsccbeb0b22006-04-01 19:24:03 +00005/* convert an mpint into a big endian byte array (most significant byte first) */
6/* return number of bytes converted */
7/* if p == nil, allocate and result array */
rscb3f61792004-03-21 14:06:38 +00008int
9mptobe(mpint *b, uchar *p, uint n, uchar **pp)
10{
11 int i, j, suppress;
12 mpdigit x;
13 uchar *e, *s, c;
14
15 if(p == nil){
16 n = (b->top+1)*Dbytes;
17 p = malloc(n);
18 }
19 if(p == nil)
20 return -1;
21 if(pp != nil)
22 *pp = p;
23 memset(p, 0, n);
24
rsccbeb0b22006-04-01 19:24:03 +000025 /* special case 0 */
rscb3f61792004-03-21 14:06:38 +000026 if(b->top == 0){
27 if(n < 1)
28 return -1;
29 else
30 return 1;
31 }
32
33 s = p;
34 e = s+n;
35 suppress = 1;
36 for(i = b->top-1; i >= 0; i--){
37 x = b->p[i];
38 for(j = Dbits-8; j >= 0; j -= 8){
39 c = x>>j;
40 if(c == 0 && suppress)
41 continue;
42 if(p >= e)
43 return -1;
44 *p++ = c;
45 suppress = 0;
46 }
47 }
48
rsccbeb0b22006-04-01 19:24:03 +000049 /* guarantee at least one byte */
rscb3f61792004-03-21 14:06:38 +000050 if(s == p){
51 if(p >= e)
52 return -1;
53 *p++ = 0;
54 }
55
56 return p - s;
57}