Fix typo in error message.
[dragonfly.git] / contrib / dhcp-3.0 / omapip / convert.c
1 /* convert.c
2
3    Safe copying of option values into and out of the option buffer, which
4    can't be assumed to be aligned. */
5
6 /*
7  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
8  * Copyright (c) 1996-2003 by Internet Software Consortium
9  *
10  * Permission to use, copy, modify, and distribute this software for any
11  * purpose with or without fee is hereby granted, provided that the above
12  * copyright notice and this permission notice appear in all copies.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
15  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
16  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
17  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
19  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
20  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21  *
22  *   Internet Systems Consortium, Inc.
23  *   950 Charter Street
24  *   Redwood City, CA 94063
25  *   <info@isc.org>
26  *   http://www.isc.org/
27  *
28  * This software has been written for Internet Systems Consortium
29  * by Ted Lemon in cooperation with Vixie Enterprises and Nominum, Inc.
30  * To learn more about Internet Systems Consortium, see
31  * ``http://www.isc.org/''.  To learn more about Vixie Enterprises,
32  * see ``http://www.vix.com''.   To learn more about Nominum, Inc., see
33  * ``http://www.nominum.com''.
34  */
35
36 #ifndef lint
37 static char copyright[] =
38 "$Id: convert.c,v 1.1.2.3 2004/11/24 17:39:17 dhankins Exp $ Copyright (c) 2004 Internet Systems Consortium.  All rights reserved.\n";
39 #endif /* not lint */
40
41 #include <omapip/omapip_p.h>
42
43 u_int32_t getULong (buf)
44         const unsigned char *buf;
45 {
46         u_int32_t ibuf;
47
48         memcpy (&ibuf, buf, sizeof (u_int32_t));
49         return ntohl (ibuf);
50 }
51
52 int32_t getLong (buf)
53         const unsigned char *buf;
54 {
55         int32_t ibuf;
56
57         memcpy (&ibuf, buf, sizeof (int32_t));
58         return ntohl (ibuf);
59 }
60
61 u_int32_t getUShort (buf)
62         const unsigned char *buf;
63 {
64         unsigned short ibuf;
65
66         memcpy (&ibuf, buf, sizeof (u_int16_t));
67         return ntohs (ibuf);
68 }
69
70 int32_t getShort (buf)
71         const unsigned char *buf;
72 {
73         short ibuf;
74
75         memcpy (&ibuf, buf, sizeof (int16_t));
76         return ntohs (ibuf);
77 }
78
79 void putULong (obuf, val)
80         unsigned char *obuf;
81         u_int32_t val;
82 {
83         u_int32_t tmp = htonl (val);
84         memcpy (obuf, &tmp, sizeof tmp);
85 }
86
87 void putLong (obuf, val)
88         unsigned char *obuf;
89         int32_t val;
90 {
91         int32_t tmp = htonl (val);
92         memcpy (obuf, &tmp, sizeof tmp);
93 }
94
95 void putUShort (obuf, val)
96         unsigned char *obuf;
97         u_int32_t val;
98 {
99         u_int16_t tmp = htons (val);
100         memcpy (obuf, &tmp, sizeof tmp);
101 }
102
103 void putShort (obuf, val)
104         unsigned char *obuf;
105         int32_t val;
106 {
107         int16_t tmp = htons (val);
108         memcpy (obuf, &tmp, sizeof tmp);
109 }
110
111 void putUChar (obuf, val)
112         unsigned char *obuf;
113         u_int32_t val;
114 {
115         *obuf = val;
116 }
117
118 u_int32_t getUChar (obuf)
119         const unsigned char *obuf;
120 {
121         return obuf [0];
122 }
123
124 int converted_length (buf, base, width)
125         const unsigned char *buf;
126         unsigned int base;
127         unsigned int width;
128 {
129         u_int32_t number;
130         u_int32_t column;
131         int power = 1;
132         u_int32_t newcolumn = base;
133
134         if (base > 16)
135                 return 0;
136
137         if (width == 1)
138                 number = getUChar (buf);
139         else if (width == 2)
140                 number = getUShort (buf);
141         else if (width == 4)
142                 number = getULong (buf);
143         else
144                 return 0;
145
146         do {
147                 column = newcolumn;
148
149                 if (number < column)
150                         return power;
151                 power++;
152                 newcolumn = column * base;
153                 /* If we wrap around, it must be the next power of two up. */
154         } while (newcolumn > column);
155
156         return power;
157 }
158
159 int binary_to_ascii (outbuf, inbuf, base, width)
160         unsigned char *outbuf;
161         const unsigned char *inbuf;
162         unsigned int base;
163         unsigned int width;
164 {
165         u_int32_t number;
166         static char h2a [] = "0123456789abcdef";
167         int power = converted_length (inbuf, base, width);
168         int i, j;
169
170         if (base > 16)
171                 return 0;
172
173         if (width == 1)
174                 number = getUChar (inbuf);
175         else if (width == 2)
176                 number = getUShort (inbuf);
177         else if (width == 4)
178                 number = getULong (inbuf);
179         else
180                 return 0;
181
182         for (i = power - 1 ; i >= 0; i--) {
183                 outbuf [i] = h2a [number % base];
184                 number /= base;
185         }
186
187         return power;
188 }