Start development on 386BSD 0.0
[unix-history] / .ref-BSD-4_3_Net_2 / usr / src / lib / librpc / secure_rpc / keyserv / mp.c
CommitLineData
af359dea
C
1#ifndef lint
2static char sccsid[] = "@(#)mp.c 2.1 88/08/15 4.0 RPCSRC Copyr 1988 Sun Micro";
3#endif
4/*
5 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
6 * unrestricted use provided that this legend is included on all tape
7 * media and as a part of the software program in whole or part. Users
8 * may copy or modify Sun RPC without charge, but are not authorized
9 * to license or distribute it to anyone else except as part of a product or
10 * program developed by the user.
11 *
12 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
13 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
14 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
15 *
16 * Sun RPC is provided with no support and without any obligation on the
17 * part of Sun Microsystems, Inc. to assist in its use, correction,
18 * modification or enhancement.
19 *
20 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
21 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
22 * OR ANY PART THEREOF.
23 *
24 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
25 * or profits or other special, indirect and consequential damages, even if
26 * Sun has been advised of the possibility of such damages.
27 *
28 * Sun Microsystems, Inc.
29 * 2550 Garcia Avenue
30 * Mountain View, California 94043
31 */
32
33/*
34 * These routines add hexadecimal functionality to the multiple-precision
35 * library.
36 */
37#include <stdio.h>
38#include <mp.h>
39
40void mfree();
41
42/*
43 * Convert hex digit to binary value
44 */
45static int
46xtoi(c)
47 char c;
48{
49 if (c >= '0' && c <= '9') {
50 return(c - '0');
51 } else if (c >= 'a' && c <= 'f') {
52 return(c - 'a' + 10);
53 } else {
54 return(-1);
55 }
56}
57
58/*
59 * Convert hex key to MINT key
60 */
61MINT *
62xtom(key)
63 char *key;
64{
65 int digit;
66 MINT *m = itom(0);
67 MINT *d;
68 MINT *sixteen;
69 sixteen = itom(16);
70 for (; *key; key++) {
71 digit = xtoi(*key);
72 if (digit < 0) {
73 return(NULL);
74 }
75 d = itom(digit);
76 mult(m,sixteen,m);
77 madd(m,d,m);
78 mfree(d);
79 }
80 mfree(sixteen);
81 return(m);
82}
83static char
84itox(d)
85 short d;
86{
87 d &= 15;
88 if (d < 10) {
89 return('0' + d);
90 } else {
91 return('a' - 10 + d);
92 }
93}
94/*
95 * Convert MINT key to hex key
96 */
97char *
98mtox(key)
99 MINT *key;
100{
101 MINT *m = itom(0);
102 MINT *zero = itom(0);
103 short r;
104 char *p;
105 char c;
106 char *s;
107 char *hex;
108 int size;
109#define BASEBITS (8*sizeof(short) - 1)
110 if (key->len >= 0) {
111 size = key->len;
112 } else {
113 size = -key->len;
114 }
115 hex = malloc((unsigned) ((size * BASEBITS + 3)) / 4 + 1);
116 if (hex == NULL) {
117 return(NULL);
118 }
119 move(key,m);
120 p = hex;
121 do {
122 sdiv(m,16,m,&r);
123 *p++ = itox(r);
124 } while (mcmp(m,zero) != 0);
125 mfree(m);
126 mfree(zero);
127 *p = 0;
128 for (p--, s = hex; s < p; s++, p--) {
129 c = *p;
130 *p = *s;
131 *s = c;
132 }
133 return(hex);
134}
135/*
136 * Deallocate a multiple precision integer
137 */
138void
139mfree(a)
140 MINT *a;
141{
142 xfree(a);
143 free((char *)a);
144}
145