diff options
| author | Charles.Forsyth <devnull@localhost> | 2006-12-22 17:07:39 +0000 |
|---|---|---|
| committer | Charles.Forsyth <devnull@localhost> | 2006-12-22 17:07:39 +0000 |
| commit | 37da2899f40661e3e9631e497da8dc59b971cbd0 (patch) | |
| tree | cbc6d4680e347d906f5fa7fca73214418741df72 /libmp/port/betomp.c | |
| parent | 54bc8ff236ac10b3eaa928fd6bcfc0cdb2ba46ae (diff) | |
20060303a
Diffstat (limited to 'libmp/port/betomp.c')
| -rw-r--r-- | libmp/port/betomp.c | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/libmp/port/betomp.c b/libmp/port/betomp.c new file mode 100644 index 00000000..95935fcd --- /dev/null +++ b/libmp/port/betomp.c @@ -0,0 +1,40 @@ +#include "os.h" +#include <mp.h> +#include "dat.h" + +// convert a big-endian byte array (most significant byte first) to an mpint +mpint* +betomp(uchar *p, uint n, mpint *b) +{ + int m, s; + mpdigit x; + + if(b == nil) + b = mpnew(0); + + // dump leading zeros + while(*p == 0 && n > 1){ + p++; + n--; + } + + // get the space + mpbits(b, n*8); + b->top = DIGITS(n*8); + m = b->top-1; + + // first digit might not be Dbytes long + s = ((n-1)*8)%Dbits; + x = 0; + for(; n > 0; n--){ + x |= ((mpdigit)(*p++)) << s; + s -= 8; + if(s < 0){ + b->p[m--] = x; + s = Dbits-8; + x = 0; + } + } + + return b; +} |
