1 /* $NetBSD: convert.c,v 1.1.1.2 2014/07/12 11:57:58 spz Exp $ */
4 Safe copying of option values into and out of the option buffer, which
5 can't be assumed to be aligned. */
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.
25 * Redwood City, CA 94063
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 $");
36 #include <omapip/omapip_p.h>
38 u_int32_t
getULong (buf
)
39 const unsigned char *buf
;
43 memcpy (&ibuf
, buf
, sizeof (u_int32_t
));
48 const unsigned char *buf
;
52 memcpy (&ibuf
, buf
, sizeof (int32_t));
56 u_int32_t
getUShort (buf
)
57 const unsigned char *buf
;
61 memcpy (&ibuf
, buf
, sizeof (u_int16_t
));
65 int32_t getShort (buf
)
66 const unsigned char *buf
;
70 memcpy (&ibuf
, buf
, sizeof (int16_t));
74 void putULong (obuf
, val
)
78 u_int32_t tmp
= htonl (val
);
79 memcpy (obuf
, &tmp
, sizeof tmp
);
82 void putLong (obuf
, val
)
86 int32_t tmp
= htonl (val
);
87 memcpy (obuf
, &tmp
, sizeof tmp
);
90 void putUShort (obuf
, val
)
94 u_int16_t tmp
= htons (val
);
95 memcpy (obuf
, &tmp
, sizeof tmp
);
98 void putShort (obuf
, val
)
102 int16_t tmp
= htons (val
);
103 memcpy (obuf
, &tmp
, sizeof tmp
);
106 void putUChar (obuf
, val
)
113 u_int32_t
getUChar (obuf
)
114 const unsigned char *obuf
;
119 int converted_length (buf
, base
, width
)
120 const unsigned char *buf
;
127 u_int32_t newcolumn
= base
;
133 number
= getUChar (buf
);
135 number
= getUShort (buf
);
137 number
= getULong (buf
);
147 newcolumn
= column
* base
;
148 /* If we wrap around, it must be the next power of two up. */
149 } while (newcolumn
> column
);
154 int binary_to_ascii (outbuf
, inbuf
, base
, width
)
155 unsigned char *outbuf
;
156 const unsigned char *inbuf
;
161 static char h2a
[] = "0123456789abcdef";
162 int power
= converted_length (inbuf
, base
, width
);
169 number
= getUChar (inbuf
);
171 number
= getUShort (inbuf
);
173 number
= getULong (inbuf
);
177 for (i
= power
- 1 ; i
>= 0; i
--) {
178 outbuf
[i
] = h2a
[number
% base
];