]> git.ipfire.org Git - thirdparty/dhcp.git/blame - omapip/convert.c
[master]
[thirdparty/dhcp.git] / omapip / convert.c
CommitLineData
d7837182
TL
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/*
a47d6336 7 * Copyright (c) 2004,2007,2009 by Internet Systems Consortium, Inc. ("ISC")
98311e4b 8 * Copyright (c) 1996-2003 by Internet Software Consortium
d7837182 9 *
98311e4b
DH
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.
d7837182 13 *
98311e4b
DH
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.
d7837182 21 *
98311e4b
DH
22 * Internet Systems Consortium, Inc.
23 * 950 Charter Street
24 * Redwood City, CA 94063
25 * <info@isc.org>
2c85ac9b 26 * https://www.isc.org/
49733f31 27 *
98311e4b 28 * This software has been written for Internet Systems Consortium
49733f31 29 * by Ted Lemon in cooperation with Vixie Enterprises and Nominum, Inc.
98311e4b 30 * To learn more about Internet Systems Consortium, see
2c85ac9b 31 * ``https://www.isc.org/''. To learn more about Vixie Enterprises,
49733f31
TL
32 * see ``http://www.vix.com''. To learn more about Nominum, Inc., see
33 * ``http://www.nominum.com''.
d7837182
TL
34 */
35
fe5b0fdd
DH
36#include "dhcpd.h"
37
c8d531a6 38#include <omapip/omapip_p.h>
d7837182 39
a5ab762b 40u_int32_t getULong (buf)
b1b7b521 41 const unsigned char *buf;
d7837182 42{
98311e4b 43 u_int32_t ibuf;
d7837182 44
a5ab762b 45 memcpy (&ibuf, buf, sizeof (u_int32_t));
d7837182
TL
46 return ntohl (ibuf);
47}
48
a5ab762b 49int32_t getLong (buf)
b1b7b521 50 const unsigned char *buf;
d7837182 51{
98311e4b 52 int32_t ibuf;
d7837182 53
a5ab762b 54 memcpy (&ibuf, buf, sizeof (int32_t));
d7837182
TL
55 return ntohl (ibuf);
56}
57
ee83c9fe 58u_int32_t getUShort (buf)
b1b7b521 59 const unsigned char *buf;
d7837182
TL
60{
61 unsigned short ibuf;
62
a5ab762b 63 memcpy (&ibuf, buf, sizeof (u_int16_t));
d7837182
TL
64 return ntohs (ibuf);
65}
66
ee83c9fe 67int32_t getShort (buf)
b1b7b521 68 const unsigned char *buf;
d7837182
TL
69{
70 short ibuf;
71
a5ab762b 72 memcpy (&ibuf, buf, sizeof (int16_t));
d7837182
TL
73 return ntohs (ibuf);
74}
75
76void putULong (obuf, val)
77 unsigned char *obuf;
a5ab762b 78 u_int32_t val;
d7837182 79{
a5ab762b 80 u_int32_t tmp = htonl (val);
d7837182
TL
81 memcpy (obuf, &tmp, sizeof tmp);
82}
83
84void putLong (obuf, val)
85 unsigned char *obuf;
a5ab762b 86 int32_t val;
d7837182 87{
a5ab762b 88 int32_t tmp = htonl (val);
d7837182
TL
89 memcpy (obuf, &tmp, sizeof tmp);
90}
91
92void putUShort (obuf, val)
93 unsigned char *obuf;
15c1fd2c 94 u_int32_t val;
d7837182 95{
46809edf 96 u_int16_t tmp = htons (val);
d7837182
TL
97 memcpy (obuf, &tmp, sizeof tmp);
98}
99
100void putShort (obuf, val)
101 unsigned char *obuf;
15c1fd2c 102 int32_t val;
d7837182 103{
46809edf 104 int16_t tmp = htons (val);
d7837182
TL
105 memcpy (obuf, &tmp, sizeof tmp);
106}
107
ee83c9fe
TL
108void putUChar (obuf, val)
109 unsigned char *obuf;
110 u_int32_t val;
111{
112 *obuf = val;
113}
114
115u_int32_t getUChar (obuf)
b1b7b521 116 const unsigned char *obuf;
ee83c9fe
TL
117{
118 return obuf [0];
119}
da38df14
TL
120
121int converted_length (buf, base, width)
b1b7b521 122 const unsigned char *buf;
da38df14
TL
123 unsigned int base;
124 unsigned int width;
125{
126 u_int32_t number;
5fc9b051 127 u_int32_t column;
da38df14 128 int power = 1;
5fc9b051 129 u_int32_t newcolumn = base;
da38df14
TL
130
131 if (base > 16)
132 return 0;
133
134 if (width == 1)
135 number = getUChar (buf);
136 else if (width == 2)
137 number = getUShort (buf);
138 else if (width == 4)
139 number = getULong (buf);
98311e4b
DH
140 else
141 return 0;
da38df14
TL
142
143 do {
144 column = newcolumn;
145
146 if (number < column)
147 return power;
148 power++;
149 newcolumn = column * base;
150 /* If we wrap around, it must be the next power of two up. */
5fc9b051 151 } while (newcolumn > column);
da38df14
TL
152
153 return power;
154}
155
156int binary_to_ascii (outbuf, inbuf, base, width)
157 unsigned char *outbuf;
b1b7b521 158 const unsigned char *inbuf;
da38df14
TL
159 unsigned int base;
160 unsigned int width;
161{
162 u_int32_t number;
163 static char h2a [] = "0123456789abcdef";
5fc9b051 164 int power = converted_length (inbuf, base, width);
28868515 165 int i;
da38df14
TL
166
167 if (base > 16)
168 return 0;
169
170 if (width == 1)
171 number = getUChar (inbuf);
172 else if (width == 2)
173 number = getUShort (inbuf);
174 else if (width == 4)
175 number = getULong (inbuf);
98311e4b
DH
176 else
177 return 0;
da38df14 178
5fc9b051 179 for (i = power - 1 ; i >= 0; i--) {
da38df14
TL
180 outbuf [i] = h2a [number % base];
181 number /= base;
da38df14 182 }
3c80312f 183
da38df14
TL
184 return power;
185}