]> git.ipfire.org Git - thirdparty/dhcp.git/blame - omapip/convert.c
copy rights update
[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/*
49a7fb58 7 * Copyright (C) 2004-2022 Internet Systems Consortium, Inc. ("ISC")
98311e4b 8 * Copyright (c) 1996-2003 by Internet Software Consortium
d7837182 9 *
7512d88b
TM
10 * This Source Code Form is subject to the terms of the Mozilla Public
11 * License, v. 2.0. If a copy of the MPL was not distributed with this
12 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
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 22 * Internet Systems Consortium, Inc.
429a56d7
TM
23 * PO Box 360
24 * Newmarket, NH 03857 USA
98311e4b 25 * <info@isc.org>
2c85ac9b 26 * https://www.isc.org/
49733f31 27 *
d7837182
TL
28 */
29
fe5b0fdd
DH
30#include "dhcpd.h"
31
c8d531a6 32#include <omapip/omapip_p.h>
d7837182 33
a5ab762b 34u_int32_t getULong (buf)
b1b7b521 35 const unsigned char *buf;
d7837182 36{
98311e4b 37 u_int32_t ibuf;
d7837182 38
a5ab762b 39 memcpy (&ibuf, buf, sizeof (u_int32_t));
d7837182
TL
40 return ntohl (ibuf);
41}
42
a5ab762b 43int32_t getLong (buf)
b1b7b521 44 const unsigned char *buf;
d7837182 45{
98311e4b 46 int32_t ibuf;
d7837182 47
a5ab762b 48 memcpy (&ibuf, buf, sizeof (int32_t));
d7837182
TL
49 return ntohl (ibuf);
50}
51
ee83c9fe 52u_int32_t getUShort (buf)
b1b7b521 53 const unsigned char *buf;
d7837182
TL
54{
55 unsigned short ibuf;
56
a5ab762b 57 memcpy (&ibuf, buf, sizeof (u_int16_t));
d7837182
TL
58 return ntohs (ibuf);
59}
60
ee83c9fe 61int32_t getShort (buf)
b1b7b521 62 const unsigned char *buf;
d7837182
TL
63{
64 short ibuf;
65
a5ab762b 66 memcpy (&ibuf, buf, sizeof (int16_t));
d7837182
TL
67 return ntohs (ibuf);
68}
69
70void putULong (obuf, val)
71 unsigned char *obuf;
a5ab762b 72 u_int32_t val;
d7837182 73{
a5ab762b 74 u_int32_t tmp = htonl (val);
d7837182
TL
75 memcpy (obuf, &tmp, sizeof tmp);
76}
77
78void putLong (obuf, val)
79 unsigned char *obuf;
a5ab762b 80 int32_t val;
d7837182 81{
a5ab762b 82 int32_t tmp = htonl (val);
d7837182
TL
83 memcpy (obuf, &tmp, sizeof tmp);
84}
85
86void putUShort (obuf, val)
87 unsigned char *obuf;
15c1fd2c 88 u_int32_t val;
d7837182 89{
46809edf 90 u_int16_t tmp = htons (val);
d7837182
TL
91 memcpy (obuf, &tmp, sizeof tmp);
92}
93
94void putShort (obuf, val)
95 unsigned char *obuf;
15c1fd2c 96 int32_t val;
d7837182 97{
46809edf 98 int16_t tmp = htons (val);
d7837182
TL
99 memcpy (obuf, &tmp, sizeof tmp);
100}
101
ee83c9fe
TL
102void putUChar (obuf, val)
103 unsigned char *obuf;
104 u_int32_t val;
105{
106 *obuf = val;
107}
108
109u_int32_t getUChar (obuf)
b1b7b521 110 const unsigned char *obuf;
ee83c9fe
TL
111{
112 return obuf [0];
113}
da38df14
TL
114
115int converted_length (buf, base, width)
b1b7b521 116 const unsigned char *buf;
da38df14
TL
117 unsigned int base;
118 unsigned int width;
119{
120 u_int32_t number;
5fc9b051 121 u_int32_t column;
da38df14 122 int power = 1;
5fc9b051 123 u_int32_t newcolumn = base;
da38df14
TL
124
125 if (base > 16)
126 return 0;
127
128 if (width == 1)
129 number = getUChar (buf);
130 else if (width == 2)
131 number = getUShort (buf);
132 else if (width == 4)
133 number = getULong (buf);
98311e4b
DH
134 else
135 return 0;
da38df14
TL
136
137 do {
138 column = newcolumn;
139
140 if (number < column)
141 return power;
142 power++;
143 newcolumn = column * base;
144 /* If we wrap around, it must be the next power of two up. */
5fc9b051 145 } while (newcolumn > column);
da38df14
TL
146
147 return power;
148}
149
150int binary_to_ascii (outbuf, inbuf, base, width)
151 unsigned char *outbuf;
b1b7b521 152 const unsigned char *inbuf;
da38df14
TL
153 unsigned int base;
154 unsigned int width;
155{
156 u_int32_t number;
157 static char h2a [] = "0123456789abcdef";
5fc9b051 158 int power = converted_length (inbuf, base, width);
28868515 159 int i;
da38df14
TL
160
161 if (base > 16)
162 return 0;
163
164 if (width == 1)
165 number = getUChar (inbuf);
166 else if (width == 2)
167 number = getUShort (inbuf);
168 else if (width == 4)
169 number = getULong (inbuf);
98311e4b
DH
170 else
171 return 0;
da38df14 172
5fc9b051 173 for (i = power - 1 ; i >= 0; i--) {
da38df14
TL
174 outbuf [i] = h2a [number % base];
175 number /= base;
da38df14 176 }
3c80312f 177
da38df14
TL
178 return power;
179}