]> git.ipfire.org Git - thirdparty/dhcp.git/blob - omapip/convert.c
- Replaced ./configure shellscripting with GNU Autoconf. [ISC-Bugs #16405b]
[thirdparty/dhcp.git] / 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.3 2007/05/19 18:47:15 dhankins Exp $ Copyright (c) 2004 Internet Systems Consortium. All rights reserved.\n";
39 #endif /* not lint */
40
41 #include "dhcpd.h"
42
43 #include <omapip/omapip_p.h>
44
45 u_int32_t getULong (buf)
46 const unsigned char *buf;
47 {
48 u_int32_t ibuf;
49
50 memcpy (&ibuf, buf, sizeof (u_int32_t));
51 return ntohl (ibuf);
52 }
53
54 int32_t getLong (buf)
55 const unsigned char *buf;
56 {
57 int32_t ibuf;
58
59 memcpy (&ibuf, buf, sizeof (int32_t));
60 return ntohl (ibuf);
61 }
62
63 u_int32_t getUShort (buf)
64 const unsigned char *buf;
65 {
66 unsigned short ibuf;
67
68 memcpy (&ibuf, buf, sizeof (u_int16_t));
69 return ntohs (ibuf);
70 }
71
72 int32_t getShort (buf)
73 const unsigned char *buf;
74 {
75 short ibuf;
76
77 memcpy (&ibuf, buf, sizeof (int16_t));
78 return ntohs (ibuf);
79 }
80
81 void putULong (obuf, val)
82 unsigned char *obuf;
83 u_int32_t val;
84 {
85 u_int32_t tmp = htonl (val);
86 memcpy (obuf, &tmp, sizeof tmp);
87 }
88
89 void putLong (obuf, val)
90 unsigned char *obuf;
91 int32_t val;
92 {
93 int32_t tmp = htonl (val);
94 memcpy (obuf, &tmp, sizeof tmp);
95 }
96
97 void putUShort (obuf, val)
98 unsigned char *obuf;
99 u_int32_t val;
100 {
101 u_int16_t tmp = htons (val);
102 memcpy (obuf, &tmp, sizeof tmp);
103 }
104
105 void putShort (obuf, val)
106 unsigned char *obuf;
107 int32_t val;
108 {
109 int16_t tmp = htons (val);
110 memcpy (obuf, &tmp, sizeof tmp);
111 }
112
113 void putUChar (obuf, val)
114 unsigned char *obuf;
115 u_int32_t val;
116 {
117 *obuf = val;
118 }
119
120 u_int32_t getUChar (obuf)
121 const unsigned char *obuf;
122 {
123 return obuf [0];
124 }
125
126 int converted_length (buf, base, width)
127 const unsigned char *buf;
128 unsigned int base;
129 unsigned int width;
130 {
131 u_int32_t number;
132 u_int32_t column;
133 int power = 1;
134 u_int32_t newcolumn = base;
135
136 if (base > 16)
137 return 0;
138
139 if (width == 1)
140 number = getUChar (buf);
141 else if (width == 2)
142 number = getUShort (buf);
143 else if (width == 4)
144 number = getULong (buf);
145 else
146 return 0;
147
148 do {
149 column = newcolumn;
150
151 if (number < column)
152 return power;
153 power++;
154 newcolumn = column * base;
155 /* If we wrap around, it must be the next power of two up. */
156 } while (newcolumn > column);
157
158 return power;
159 }
160
161 int binary_to_ascii (outbuf, inbuf, base, width)
162 unsigned char *outbuf;
163 const unsigned char *inbuf;
164 unsigned int base;
165 unsigned int width;
166 {
167 u_int32_t number;
168 static char h2a [] = "0123456789abcdef";
169 int power = converted_length (inbuf, base, width);
170 int i, j;
171
172 if (base > 16)
173 return 0;
174
175 if (width == 1)
176 number = getUChar (inbuf);
177 else if (width == 2)
178 number = getUShort (inbuf);
179 else if (width == 4)
180 number = getULong (inbuf);
181 else
182 return 0;
183
184 for (i = power - 1 ; i >= 0; i--) {
185 outbuf [i] = h2a [number % base];
186 number /= base;
187 }
188
189 return power;
190 }