]>
git.ipfire.org Git - thirdparty/dhcp.git/blob - omapip/convert.c
3 Safe copying of option values into and out of the option buffer, which
4 can't be assumed to be aligned. */
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.
24 * Redwood City, CA 94063
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''.
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";
43 #include <omapip/omapip_p.h>
45 u_int32_t
getULong (buf
)
46 const unsigned char *buf
;
50 memcpy (&ibuf
, buf
, sizeof (u_int32_t
));
55 const unsigned char *buf
;
59 memcpy (&ibuf
, buf
, sizeof (int32_t));
63 u_int32_t
getUShort (buf
)
64 const unsigned char *buf
;
68 memcpy (&ibuf
, buf
, sizeof (u_int16_t
));
72 int32_t getShort (buf
)
73 const unsigned char *buf
;
77 memcpy (&ibuf
, buf
, sizeof (int16_t));
81 void putULong (obuf
, val
)
85 u_int32_t tmp
= htonl (val
);
86 memcpy (obuf
, &tmp
, sizeof tmp
);
89 void putLong (obuf
, val
)
93 int32_t tmp
= htonl (val
);
94 memcpy (obuf
, &tmp
, sizeof tmp
);
97 void putUShort (obuf
, val
)
101 u_int16_t tmp
= htons (val
);
102 memcpy (obuf
, &tmp
, sizeof tmp
);
105 void putShort (obuf
, val
)
109 int16_t tmp
= htons (val
);
110 memcpy (obuf
, &tmp
, sizeof tmp
);
113 void putUChar (obuf
, val
)
120 u_int32_t
getUChar (obuf
)
121 const unsigned char *obuf
;
126 int converted_length (buf
, base
, width
)
127 const unsigned char *buf
;
134 u_int32_t newcolumn
= base
;
140 number
= getUChar (buf
);
142 number
= getUShort (buf
);
144 number
= getULong (buf
);
154 newcolumn
= column
* base
;
155 /* If we wrap around, it must be the next power of two up. */
156 } while (newcolumn
> column
);
161 int binary_to_ascii (outbuf
, inbuf
, base
, width
)
162 unsigned char *outbuf
;
163 const unsigned char *inbuf
;
168 static char h2a
[] = "0123456789abcdef";
169 int power
= converted_length (inbuf
, base
, width
);
176 number
= getUChar (inbuf
);
178 number
= getUShort (inbuf
);
180 number
= getULong (inbuf
);
184 for (i
= power
- 1 ; i
>= 0; i
--) {
185 outbuf
[i
] = h2a
[number
% base
];