ref: e7ec1e13f5e1814f647bcbc18e73eb6be5ce4f16
dir: /3rd/mp/mptobe.c/
#include "platform.h"
#include "mp.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)
{
uint32_t m;
m = (mpsignif(b)+7)/8;
if(m == 0)
m++;
if(p == nil){
n = m;
p = MEM_ALLOC(n);
if(p == nil){
fprintf(stderr, "mptobe: no memory\n");
exit(2);
}
} 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;
}