123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- /*
- * This file is part of the UCB release of Plan 9. It is subject to the license
- * terms in the LICENSE file found in the top-level directory of this
- * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
- * part of the UCB release of Plan 9, including this file, may be copied,
- * modified, propagated, or distributed except according to the terms contained
- * in the LICENSE file.
- */
- #include "os.h"
- #include <mp.h>
- #include "dat.h"
- // convert a big-endian byte array (most significant byte first) to an mpint
- mpint*
- betomp(uint8_t *p, uint n, mpint *b)
- {
- int m, s;
- mpdigit x;
- if(b == nil){
- b = mpnew(0);
- setmalloctag(b, getcallerpc());
- }
- // 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;
- }
|