Subversion Repositories planix.SVN

Rev

Rev 2 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2 Rev 33
Line 4... Line 4...
4
 
4
 
5
// convert an mpint into a big endian byte array (most significant byte first)
5
// convert an mpint into a big endian byte array (most significant byte first; left adjusted)
6
//   return number of bytes converted
6
//   return number of bytes converted
7
//   if p == nil, allocate and result array
7
//   if p == nil, allocate and result array
8
int
8
int
9
mptobe(mpint *b, uchar *p, uint n, uchar **pp)
9
mptobe(mpint *b, uchar *p, uint n, uchar **pp)
10
{
10
{
11
	int i, j, suppress;
-
 
12
	mpdigit x;
11
	int m;
13
	uchar *e, *s, c;
-
 
14
 
12
 
-
 
13
	m = (mpsignif(b)+7)/8;
-
 
14
	if(m == 0)
-
 
15
		m++;
15
	if(p == nil){
16
	if(p == nil){
16
		n = (b->top+1)*Dbytes;
17
		n = m;
17
		p = malloc(n);
18
		p = malloc(n);
-
 
19
		if(p == nil)
-
 
20
			sysfatal("mptobe: %r");
18
		setmalloctag(p, getcallerpc(&b));
21
		setmalloctag(p, getcallerpc(&b));
19
	}
22
	} else {
20
	if(p == nil)
23
		if(n < m)
21
		return -1;
24
			return -1;
-
 
25
		if(n > m)
-
 
26
			memset(p+m, 0, n-m);
-
 
27
	}
22
	if(pp != nil)
28
	if(pp != nil)
23
		*pp = p;
29
		*pp = p;
24
	memset(p, 0, n);
30
	mptober(b, p, m);
25
 
-
 
26
	// special case 0
-
 
27
	if(b->top == 0){
-
 
28
		if(n < 1)
-
 
29
			return -1;
-
 
30
		else
-
 
31
			return 1;
31
	return m;
32
	}
-
 
33
		
-
 
34
	s = p;
-
 
35
	e = s+n;
-
 
36
	suppress = 1;
-
 
37
	for(i = b->top-1; i >= 0; i--){
-
 
38
		x = b->p[i];
-
 
39
		for(j = Dbits-8; j >= 0; j -= 8){
-
 
40
			c = x>>j;
-
 
41
			if(c == 0 && suppress)
-
 
42
				continue;
-
 
43
			if(p >= e)
-
 
44
				return -1;
-
 
45
			*p++ = c;
-
 
46
			suppress = 0;
-
 
47
		}
-
 
48
	}
-
 
49
 
-
 
50
	// guarantee at least one byte
-
 
51
	if(s == p){
-
 
52
		if(p >= e)
-
 
53
			return -1;
-
 
54
		*p++ = 0;
-
 
55
	}
-
 
56
 
-
 
57
	return p - s;
-
 
58
}
32
}