plan9port

fork of plan9port with libvec, libstr and libsdb
Log | Files | Refs | README | LICENSE

betomp.c (589B)


      1 #include "os.h"
      2 #include <mp.h>
      3 #include "dat.h"
      4 
      5 /* convert a big-endian byte array (most significant byte first) to an mpint */
      6 mpint*
      7 betomp(uchar *p, uint n, mpint *b)
      8 {
      9 	int m, s;
     10 	mpdigit x;
     11 
     12 	if(b == nil)
     13 		b = mpnew(0);
     14 
     15 	/* dump leading zeros */
     16 	while(*p == 0 && n > 1){
     17 		p++;
     18 		n--;
     19 	}
     20 
     21 	/* get the space */
     22 	mpbits(b, n*8);
     23 	b->top = DIGITS(n*8);
     24 	m = b->top-1;
     25 
     26 	/* first digit might not be Dbytes long */
     27 	s = ((n-1)*8)%Dbits;
     28 	x = 0;
     29 	for(; n > 0; n--){
     30 		x |= ((mpdigit)(*p++)) << s;
     31 		s -= 8;
     32 		if(s < 0){
     33 			b->p[m--] = x;
     34 			s = Dbits-8;
     35 			x = 0;
     36 		}
     37 	}
     38 
     39 	return b;
     40 }