2016-11-25 17:18:40 +01:00
|
|
|
#include "os.h"
|
|
|
|
#include <mp.h>
|
|
|
|
#include "dat.h"
|
|
|
|
|
|
|
|
// convert an mpint into a big endian byte array (most significant byte first; left adjusted)
|
|
|
|
// return number of bytes converted
|
|
|
|
// if p == nil, allocate and result array
|
|
|
|
int
|
|
|
|
mptobe(mpint *b, uint8_t *p, uint32_t n, uint8_t **pp)
|
|
|
|
{
|
|
|
|
int m;
|
|
|
|
|
2017-01-18 01:59:39 +01:00
|
|
|
assert((p == nil) != (pp == nil));
|
2016-11-25 17:18:40 +01:00
|
|
|
m = (mpsignif(b)+7)/8;
|
|
|
|
if(m == 0)
|
|
|
|
m++;
|
|
|
|
if(p == nil){
|
|
|
|
n = m;
|
|
|
|
p = malloc(n);
|
|
|
|
if(p == nil)
|
|
|
|
sysfatal("mptobe: %r");
|
|
|
|
setmalloctag(p, getcallerpc());
|
|
|
|
} else {
|
|
|
|
if(n < m)
|
|
|
|
return -1;
|
|
|
|
if(n > m)
|
|
|
|
memset(p+m, 0, n-m);
|
|
|
|
}
|
|
|
|
if(pp != nil)
|
|
|
|
*pp = p;
|
|
|
|
mptober(b, p, m);
|
|
|
|
return m;
|
|
|
|
}
|