revert between 56095 -> 55830 in arch
[AROS.git] / workbench / network / stacks / AROSTCP / dhcp / omapip / convert.c
blob4a80ac6ee3788cce966ab5084ef945a8b10dbe2b
1 /* convert.c
3 Safe copying of option values into and out of the option buffer, which
4 can't be assumed to be aligned. */
6 /*
7 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
8 * Copyright (c) 1996-2003 by Internet Software Consortium
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.
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.
22 * Internet Systems Consortium, Inc.
23 * 950 Charter Street
24 * Redwood City, CA 94063
25 * <info@isc.org>
26 * http://www.isc.org/
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''.
36 #if 0
37 static char copyright[] =
38 "$Id$ Copyright (c) 2004 Internet Systems Consortium. All rights reserved.\n";
39 #endif
41 #include <omapip/omapip_p.h>
43 u_int32_t getULong (buf)
44 const unsigned char *buf;
46 u_int32_t ibuf;
48 memcpy (&ibuf, buf, sizeof (u_int32_t));
49 return ntohl (ibuf);
52 int32_t getLong (buf)
53 const unsigned char *buf;
55 int32_t ibuf;
57 memcpy (&ibuf, buf, sizeof (int32_t));
58 return ntohl (ibuf);
61 u_int32_t getUShort (buf)
62 const unsigned char *buf;
64 unsigned short ibuf;
66 memcpy (&ibuf, buf, sizeof (u_int16_t));
67 return ntohs (ibuf);
70 int32_t getShort (buf)
71 const unsigned char *buf;
73 short ibuf;
75 memcpy (&ibuf, buf, sizeof (int16_t));
76 return ntohs (ibuf);
79 void putULong (obuf, val)
80 unsigned char *obuf;
81 u_int32_t val;
83 u_int32_t tmp = htonl (val);
84 memcpy (obuf, &tmp, sizeof tmp);
87 void putLong (obuf, val)
88 unsigned char *obuf;
89 int32_t val;
91 int32_t tmp = htonl (val);
92 memcpy (obuf, &tmp, sizeof tmp);
95 void putUShort (obuf, val)
96 unsigned char *obuf;
97 u_int32_t val;
99 u_int16_t tmp = htons (val);
100 memcpy (obuf, &tmp, sizeof tmp);
103 void putShort (obuf, val)
104 unsigned char *obuf;
105 int32_t val;
107 int16_t tmp = htons (val);
108 memcpy (obuf, &tmp, sizeof tmp);
111 void putUChar (obuf, val)
112 unsigned char *obuf;
113 u_int32_t val;
115 *obuf = val;
118 u_int32_t getUChar (obuf)
119 const unsigned char *obuf;
121 return obuf [0];
124 int converted_length (buf, base, width)
125 const unsigned char *buf;
126 unsigned int base;
127 unsigned int width;
129 u_int32_t number;
130 u_int32_t column;
131 int power = 1;
132 u_int32_t newcolumn = base;
134 if (base > 16)
135 return 0;
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;
146 do {
147 column = newcolumn;
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);
156 return power;
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;
165 u_int32_t number;
166 static char h2a [] = "0123456789abcdef";
167 int power = converted_length (inbuf, base, width);
168 int i;
169 // int j;
171 if (base > 16)
172 return 0;
174 if (width == 1)
175 number = getUChar (inbuf);
176 else if (width == 2)
177 number = getUShort (inbuf);
178 else if (width == 4)
179 number = getULong (inbuf);
180 else
181 return 0;
183 for (i = power - 1 ; i >= 0; i--) {
184 outbuf [i] = h2a [number % base];
185 number /= base;
188 return power;