xref: /titanic_50/usr/src/lib/libmp/common/util.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
2*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
3*7c478bd9Sstevel@tonic-gate 
4*7c478bd9Sstevel@tonic-gate 
5*7c478bd9Sstevel@tonic-gate /*
6*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1980 Regents of the University of California.
7*7c478bd9Sstevel@tonic-gate  * All rights reserved.  The Berkeley software License Agreement
8*7c478bd9Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
9*7c478bd9Sstevel@tonic-gate  */
10*7c478bd9Sstevel@tonic-gate /* 	Portions Copyright(c) 1988, Sun Microsystems Inc.	*/
11*7c478bd9Sstevel@tonic-gate /*	All Rights Reserved					*/
12*7c478bd9Sstevel@tonic-gate 
13*7c478bd9Sstevel@tonic-gate /*
14*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1997, by Sun Microsystems, Inc.
15*7c478bd9Sstevel@tonic-gate  * All rights reserved.
16*7c478bd9Sstevel@tonic-gate  */
17*7c478bd9Sstevel@tonic-gate 
18*7c478bd9Sstevel@tonic-gate #ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.1	*/
19*7c478bd9Sstevel@tonic-gate 
20*7c478bd9Sstevel@tonic-gate /* LINTLIBRARY */
21*7c478bd9Sstevel@tonic-gate 
22*7c478bd9Sstevel@tonic-gate #include <stdio.h>
23*7c478bd9Sstevel@tonic-gate #include <mp.h>
24*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
25*7c478bd9Sstevel@tonic-gate #include "libmp.h"
26*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
27*7c478bd9Sstevel@tonic-gate #include <unistd.h>
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate void
_mp_move(MINT * a,MINT * b)30*7c478bd9Sstevel@tonic-gate _mp_move(MINT *a, MINT *b)
31*7c478bd9Sstevel@tonic-gate {
32*7c478bd9Sstevel@tonic-gate 	int i, j;
33*7c478bd9Sstevel@tonic-gate 
34*7c478bd9Sstevel@tonic-gate 	_mp_xfree(b);
35*7c478bd9Sstevel@tonic-gate 	b->len = a->len;
36*7c478bd9Sstevel@tonic-gate 	if ((i = a->len) < 0) {
37*7c478bd9Sstevel@tonic-gate 		i = -i;
38*7c478bd9Sstevel@tonic-gate 	}
39*7c478bd9Sstevel@tonic-gate 	if (i == 0) {
40*7c478bd9Sstevel@tonic-gate 		return;
41*7c478bd9Sstevel@tonic-gate 	}
42*7c478bd9Sstevel@tonic-gate 	b->val = _mp_xalloc(i, "_mp_move");
43*7c478bd9Sstevel@tonic-gate 	for (j = 0; j < i; j++) {
44*7c478bd9Sstevel@tonic-gate 		b->val[j] = a->val[j];
45*7c478bd9Sstevel@tonic-gate 	}
46*7c478bd9Sstevel@tonic-gate }
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
49*7c478bd9Sstevel@tonic-gate /* VARARGS */
50*7c478bd9Sstevel@tonic-gate short *
_mp_xalloc(int nint,char * s)51*7c478bd9Sstevel@tonic-gate _mp_xalloc(int nint, char *s)
52*7c478bd9Sstevel@tonic-gate {
53*7c478bd9Sstevel@tonic-gate 	short *i;
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate 	i = malloc(sizeof (short) * ((unsigned)nint + 2)); /* ??? 2 ??? */
56*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
57*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: %p\n", s, i);
58*7c478bd9Sstevel@tonic-gate #endif
59*7c478bd9Sstevel@tonic-gate 	if (i == NULL) {
60*7c478bd9Sstevel@tonic-gate 		_mp_fatal("mp: no free space");
61*7c478bd9Sstevel@tonic-gate 	}
62*7c478bd9Sstevel@tonic-gate 	return (i);
63*7c478bd9Sstevel@tonic-gate }
64*7c478bd9Sstevel@tonic-gate 
65*7c478bd9Sstevel@tonic-gate void
_mp_fatal(char * s)66*7c478bd9Sstevel@tonic-gate _mp_fatal(char *s)
67*7c478bd9Sstevel@tonic-gate {
68*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s\n", s);
69*7c478bd9Sstevel@tonic-gate 	(void) fflush(stdout);
70*7c478bd9Sstevel@tonic-gate 	(void) sleep(2);
71*7c478bd9Sstevel@tonic-gate 	abort();
72*7c478bd9Sstevel@tonic-gate }
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate void
_mp_xfree(MINT * c)75*7c478bd9Sstevel@tonic-gate _mp_xfree(MINT *c)
76*7c478bd9Sstevel@tonic-gate {
77*7c478bd9Sstevel@tonic-gate #ifdef DBG
78*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "xfree ");
79*7c478bd9Sstevel@tonic-gate #endif
80*7c478bd9Sstevel@tonic-gate 	if (c->len != 0) {
81*7c478bd9Sstevel@tonic-gate 		free(c->val);
82*7c478bd9Sstevel@tonic-gate 		c->len = 0;
83*7c478bd9Sstevel@tonic-gate 	}
84*7c478bd9Sstevel@tonic-gate }
85*7c478bd9Sstevel@tonic-gate 
86*7c478bd9Sstevel@tonic-gate void
_mp_mcan(MINT * a)87*7c478bd9Sstevel@tonic-gate _mp_mcan(MINT *a)
88*7c478bd9Sstevel@tonic-gate {
89*7c478bd9Sstevel@tonic-gate 	int i, j;
90*7c478bd9Sstevel@tonic-gate 
91*7c478bd9Sstevel@tonic-gate 	if ((i = a->len) == 0) {
92*7c478bd9Sstevel@tonic-gate 		return;
93*7c478bd9Sstevel@tonic-gate 	}
94*7c478bd9Sstevel@tonic-gate 	if (i < 0) {
95*7c478bd9Sstevel@tonic-gate 		i = -i;
96*7c478bd9Sstevel@tonic-gate 	}
97*7c478bd9Sstevel@tonic-gate 	for (j = i; j > 0 && a->val[j-1] == 0; j--)
98*7c478bd9Sstevel@tonic-gate 		;
99*7c478bd9Sstevel@tonic-gate 	if (j == i) {
100*7c478bd9Sstevel@tonic-gate 		return;
101*7c478bd9Sstevel@tonic-gate 	}
102*7c478bd9Sstevel@tonic-gate 	if (j == 0) {
103*7c478bd9Sstevel@tonic-gate 		_mp_xfree(a);
104*7c478bd9Sstevel@tonic-gate 		return;
105*7c478bd9Sstevel@tonic-gate 	}
106*7c478bd9Sstevel@tonic-gate 	if (a->len > 0) {
107*7c478bd9Sstevel@tonic-gate 		a->len = j;
108*7c478bd9Sstevel@tonic-gate 	} else {
109*7c478bd9Sstevel@tonic-gate 		a->len = -j;
110*7c478bd9Sstevel@tonic-gate 	}
111*7c478bd9Sstevel@tonic-gate }
112*7c478bd9Sstevel@tonic-gate 
113*7c478bd9Sstevel@tonic-gate 
114*7c478bd9Sstevel@tonic-gate MINT *
mp_itom(short n)115*7c478bd9Sstevel@tonic-gate mp_itom(short n)
116*7c478bd9Sstevel@tonic-gate {
117*7c478bd9Sstevel@tonic-gate 	MINT *a;
118*7c478bd9Sstevel@tonic-gate 
119*7c478bd9Sstevel@tonic-gate 	a = malloc(sizeof (MINT));
120*7c478bd9Sstevel@tonic-gate 	if (n > 0) {
121*7c478bd9Sstevel@tonic-gate 		a->len = 1;
122*7c478bd9Sstevel@tonic-gate 		a->val = _mp_xalloc(1, "mp_itom1");
123*7c478bd9Sstevel@tonic-gate 		*a->val = n;
124*7c478bd9Sstevel@tonic-gate 	} else if (n < 0) {
125*7c478bd9Sstevel@tonic-gate 		a->len = -1;
126*7c478bd9Sstevel@tonic-gate 		a->val = _mp_xalloc(1, "mp_itom2");
127*7c478bd9Sstevel@tonic-gate 		*a->val = -n;
128*7c478bd9Sstevel@tonic-gate 	} else {
129*7c478bd9Sstevel@tonic-gate 		a->len = 0;
130*7c478bd9Sstevel@tonic-gate 	}
131*7c478bd9Sstevel@tonic-gate 	return (a);
132*7c478bd9Sstevel@tonic-gate }
133*7c478bd9Sstevel@tonic-gate 
134*7c478bd9Sstevel@tonic-gate int
mp_mcmp(MINT * a,MINT * b)135*7c478bd9Sstevel@tonic-gate mp_mcmp(MINT *a, MINT *b)
136*7c478bd9Sstevel@tonic-gate {
137*7c478bd9Sstevel@tonic-gate 	MINT c;
138*7c478bd9Sstevel@tonic-gate 	int res;
139*7c478bd9Sstevel@tonic-gate 
140*7c478bd9Sstevel@tonic-gate 	_mp_mcan(a);
141*7c478bd9Sstevel@tonic-gate 	_mp_mcan(b);
142*7c478bd9Sstevel@tonic-gate 	if (a->len != b->len) {
143*7c478bd9Sstevel@tonic-gate 		return (a->len - b->len);
144*7c478bd9Sstevel@tonic-gate 	}
145*7c478bd9Sstevel@tonic-gate 	c.len = 0;
146*7c478bd9Sstevel@tonic-gate 	mp_msub(a, b, &c);
147*7c478bd9Sstevel@tonic-gate 	res = c.len;
148*7c478bd9Sstevel@tonic-gate 	_mp_xfree(&c);
149*7c478bd9Sstevel@tonic-gate 	return (res);
150*7c478bd9Sstevel@tonic-gate }
151*7c478bd9Sstevel@tonic-gate 
152*7c478bd9Sstevel@tonic-gate /*
153*7c478bd9Sstevel@tonic-gate  * Convert hex digit to binary value
154*7c478bd9Sstevel@tonic-gate  */
155*7c478bd9Sstevel@tonic-gate static short
xtoi(char c)156*7c478bd9Sstevel@tonic-gate xtoi(char c)
157*7c478bd9Sstevel@tonic-gate {
158*7c478bd9Sstevel@tonic-gate 	if (c >= '0' && c <= '9') {
159*7c478bd9Sstevel@tonic-gate 		return (c - '0');
160*7c478bd9Sstevel@tonic-gate 	} else if (c >= 'a' && c <= 'f') {
161*7c478bd9Sstevel@tonic-gate 		return (c - 'a' + 10);
162*7c478bd9Sstevel@tonic-gate 	} else if (c >= 'A' && c <= 'F') {
163*7c478bd9Sstevel@tonic-gate 		return (c - 'A' + 10);
164*7c478bd9Sstevel@tonic-gate 	} else {
165*7c478bd9Sstevel@tonic-gate 		return (-1);
166*7c478bd9Sstevel@tonic-gate 	}
167*7c478bd9Sstevel@tonic-gate }
168*7c478bd9Sstevel@tonic-gate 
169*7c478bd9Sstevel@tonic-gate 
170*7c478bd9Sstevel@tonic-gate /*
171*7c478bd9Sstevel@tonic-gate  * Convert hex key to MINT key
172*7c478bd9Sstevel@tonic-gate  */
173*7c478bd9Sstevel@tonic-gate MINT *
mp_xtom(char * key)174*7c478bd9Sstevel@tonic-gate mp_xtom(char *key)
175*7c478bd9Sstevel@tonic-gate {
176*7c478bd9Sstevel@tonic-gate 	short digit;
177*7c478bd9Sstevel@tonic-gate 	MINT *m = mp_itom(0);
178*7c478bd9Sstevel@tonic-gate 	MINT *d;
179*7c478bd9Sstevel@tonic-gate 	MINT *sixteen;
180*7c478bd9Sstevel@tonic-gate 
181*7c478bd9Sstevel@tonic-gate 	sixteen = mp_itom(16);
182*7c478bd9Sstevel@tonic-gate 	for (; *key; key++) {
183*7c478bd9Sstevel@tonic-gate 		digit = xtoi(*key);
184*7c478bd9Sstevel@tonic-gate 		if (digit < 0) {
185*7c478bd9Sstevel@tonic-gate 			return (NULL);
186*7c478bd9Sstevel@tonic-gate 		}
187*7c478bd9Sstevel@tonic-gate 		d = mp_itom(digit);
188*7c478bd9Sstevel@tonic-gate 		mp_mult(m, sixteen, m);
189*7c478bd9Sstevel@tonic-gate 		mp_madd(m, d, m);
190*7c478bd9Sstevel@tonic-gate 		mp_mfree(d);
191*7c478bd9Sstevel@tonic-gate 	}
192*7c478bd9Sstevel@tonic-gate 	mp_mfree(sixteen);
193*7c478bd9Sstevel@tonic-gate 	return (m);
194*7c478bd9Sstevel@tonic-gate }
195*7c478bd9Sstevel@tonic-gate 
196*7c478bd9Sstevel@tonic-gate static char
itox(short d)197*7c478bd9Sstevel@tonic-gate itox(short d)
198*7c478bd9Sstevel@tonic-gate {
199*7c478bd9Sstevel@tonic-gate 	d &= 15;
200*7c478bd9Sstevel@tonic-gate 	if (d < 10) {
201*7c478bd9Sstevel@tonic-gate 		return ('0' + d);
202*7c478bd9Sstevel@tonic-gate 	} else {
203*7c478bd9Sstevel@tonic-gate 		return ('a' - 10 + d);
204*7c478bd9Sstevel@tonic-gate 	}
205*7c478bd9Sstevel@tonic-gate }
206*7c478bd9Sstevel@tonic-gate 
207*7c478bd9Sstevel@tonic-gate /*
208*7c478bd9Sstevel@tonic-gate  * Convert MINT key to hex key
209*7c478bd9Sstevel@tonic-gate  */
210*7c478bd9Sstevel@tonic-gate char *
mp_mtox(MINT * key)211*7c478bd9Sstevel@tonic-gate mp_mtox(MINT *key)
212*7c478bd9Sstevel@tonic-gate {
213*7c478bd9Sstevel@tonic-gate 	MINT *m = mp_itom(0);
214*7c478bd9Sstevel@tonic-gate 	MINT *zero = mp_itom(0);
215*7c478bd9Sstevel@tonic-gate 	short r;
216*7c478bd9Sstevel@tonic-gate 	char *p;
217*7c478bd9Sstevel@tonic-gate 	char c;
218*7c478bd9Sstevel@tonic-gate 	char *s;
219*7c478bd9Sstevel@tonic-gate 	char *hex;
220*7c478bd9Sstevel@tonic-gate 	int size;
221*7c478bd9Sstevel@tonic-gate 
222*7c478bd9Sstevel@tonic-gate #define	BASEBITS	(8 * (unsigned int)sizeof (short) - 1)
223*7c478bd9Sstevel@tonic-gate 
224*7c478bd9Sstevel@tonic-gate 	if (key->len >= 0) {
225*7c478bd9Sstevel@tonic-gate 		size = key->len;
226*7c478bd9Sstevel@tonic-gate 	} else {
227*7c478bd9Sstevel@tonic-gate 		size = -key->len;
228*7c478bd9Sstevel@tonic-gate 	}
229*7c478bd9Sstevel@tonic-gate 	hex = malloc((size_t) ((size * BASEBITS + 3)) / 4 + (size ? 1 : 2));
230*7c478bd9Sstevel@tonic-gate 	if (hex == NULL) {
231*7c478bd9Sstevel@tonic-gate 		return (NULL);
232*7c478bd9Sstevel@tonic-gate 	}
233*7c478bd9Sstevel@tonic-gate 	_mp_move(key, m);
234*7c478bd9Sstevel@tonic-gate 	p = hex;
235*7c478bd9Sstevel@tonic-gate 	do {
236*7c478bd9Sstevel@tonic-gate 		mp_sdiv(m, 16, m, &r);
237*7c478bd9Sstevel@tonic-gate 		*p++ = itox(r);
238*7c478bd9Sstevel@tonic-gate 	} while (mp_mcmp(m, zero) != 0);
239*7c478bd9Sstevel@tonic-gate 	mp_mfree(m);
240*7c478bd9Sstevel@tonic-gate 	mp_mfree(zero);
241*7c478bd9Sstevel@tonic-gate 
242*7c478bd9Sstevel@tonic-gate 	*p = 0;
243*7c478bd9Sstevel@tonic-gate 	for (p--, s = hex; s < p; s++, p--) {
244*7c478bd9Sstevel@tonic-gate 		c = *p;
245*7c478bd9Sstevel@tonic-gate 		*p = *s;
246*7c478bd9Sstevel@tonic-gate 		*s = c;
247*7c478bd9Sstevel@tonic-gate 	}
248*7c478bd9Sstevel@tonic-gate 	return (hex);
249*7c478bd9Sstevel@tonic-gate }
250*7c478bd9Sstevel@tonic-gate 
251*7c478bd9Sstevel@tonic-gate /*
252*7c478bd9Sstevel@tonic-gate  * Deallocate a multiple precision integer
253*7c478bd9Sstevel@tonic-gate  */
254*7c478bd9Sstevel@tonic-gate void
mp_mfree(MINT * a)255*7c478bd9Sstevel@tonic-gate mp_mfree(MINT *a)
256*7c478bd9Sstevel@tonic-gate {
257*7c478bd9Sstevel@tonic-gate 	_mp_xfree(a);
258*7c478bd9Sstevel@tonic-gate 	free(a);
259*7c478bd9Sstevel@tonic-gate }
260