Remove building with NOCRYPTO option
[minix.git] / external / bsd / dhcp / dist / omapip / convert.c
blob5b14e29bc1c232458abd2ceaf0ed55aa63670182
1 /* $NetBSD: convert.c,v 1.1.1.2 2014/07/12 11:57:58 spz Exp $ */
2 /* convert.c
4 Safe copying of option values into and out of the option buffer, which
5 can't be assumed to be aligned. */
7 /*
8 * Copyright (c) 2004,2007,2009,2014 by Internet Systems Consortium, Inc. ("ISC")
9 * Copyright (c) 1996-2003 by Internet Software Consortium
11 * Permission to use, copy, modify, and distribute this software for any
12 * purpose with or without fee is hereby granted, provided that the above
13 * copyright notice and this permission notice appear in all copies.
15 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
16 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
17 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
18 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 * Internet Systems Consortium, Inc.
24 * 950 Charter Street
25 * Redwood City, CA 94063
26 * <info@isc.org>
27 * https://www.isc.org/
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: convert.c,v 1.1.1.2 2014/07/12 11:57:58 spz Exp $");
34 #include "dhcpd.h"
36 #include <omapip/omapip_p.h>
38 u_int32_t getULong (buf)
39 const unsigned char *buf;
41 u_int32_t ibuf;
43 memcpy (&ibuf, buf, sizeof (u_int32_t));
44 return ntohl (ibuf);
47 int32_t getLong (buf)
48 const unsigned char *buf;
50 int32_t ibuf;
52 memcpy (&ibuf, buf, sizeof (int32_t));
53 return ntohl (ibuf);
56 u_int32_t getUShort (buf)
57 const unsigned char *buf;
59 unsigned short ibuf;
61 memcpy (&ibuf, buf, sizeof (u_int16_t));
62 return ntohs (ibuf);
65 int32_t getShort (buf)
66 const unsigned char *buf;
68 short ibuf;
70 memcpy (&ibuf, buf, sizeof (int16_t));
71 return ntohs (ibuf);
74 void putULong (obuf, val)
75 unsigned char *obuf;
76 u_int32_t val;
78 u_int32_t tmp = htonl (val);
79 memcpy (obuf, &tmp, sizeof tmp);
82 void putLong (obuf, val)
83 unsigned char *obuf;
84 int32_t val;
86 int32_t tmp = htonl (val);
87 memcpy (obuf, &tmp, sizeof tmp);
90 void putUShort (obuf, val)
91 unsigned char *obuf;
92 u_int32_t val;
94 u_int16_t tmp = htons (val);
95 memcpy (obuf, &tmp, sizeof tmp);
98 void putShort (obuf, val)
99 unsigned char *obuf;
100 int32_t val;
102 int16_t tmp = htons (val);
103 memcpy (obuf, &tmp, sizeof tmp);
106 void putUChar (obuf, val)
107 unsigned char *obuf;
108 u_int32_t val;
110 *obuf = val;
113 u_int32_t getUChar (obuf)
114 const unsigned char *obuf;
116 return obuf [0];
119 int converted_length (buf, base, width)
120 const unsigned char *buf;
121 unsigned int base;
122 unsigned int width;
124 u_int32_t number;
125 u_int32_t column;
126 int power = 1;
127 u_int32_t newcolumn = base;
129 if (base > 16)
130 return 0;
132 if (width == 1)
133 number = getUChar (buf);
134 else if (width == 2)
135 number = getUShort (buf);
136 else if (width == 4)
137 number = getULong (buf);
138 else
139 return 0;
141 do {
142 column = newcolumn;
144 if (number < column)
145 return power;
146 power++;
147 newcolumn = column * base;
148 /* If we wrap around, it must be the next power of two up. */
149 } while (newcolumn > column);
151 return power;
154 int binary_to_ascii (outbuf, inbuf, base, width)
155 unsigned char *outbuf;
156 const unsigned char *inbuf;
157 unsigned int base;
158 unsigned int width;
160 u_int32_t number;
161 static char h2a [] = "0123456789abcdef";
162 int power = converted_length (inbuf, base, width);
163 int i;
165 if (base > 16)
166 return 0;
168 if (width == 1)
169 number = getUChar (inbuf);
170 else if (width == 2)
171 number = getUShort (inbuf);
172 else if (width == 4)
173 number = getULong (inbuf);
174 else
175 return 0;
177 for (i = power - 1 ; i >= 0; i--) {
178 outbuf [i] = h2a [number % base];
179 number /= base;
182 return power;