xref: /freebsd/sbin/dhclient/convert.c (revision 389e4940069316fe667ffa263fa7d6390d0a960f)
1 /*	$OpenBSD: convert.c,v 1.5 2004/02/07 11:35:59 henning Exp $	*/
2 
3 /*
4  * Safe copying of option values into and out of the option buffer,
5  * which can't be assumed to be aligned.
6  */
7 
8 /*-
9  * SPDX-License-Identifier: BSD-3-Clause
10  *
11  * Copyright (c) 1995, 1996 The Internet Software Consortium.
12  * All rights reserved.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  *
18  * 1. Redistributions of source code must retain the above copyright
19  *    notice, this list of conditions and the following disclaimer.
20  * 2. Redistributions in binary form must reproduce the above copyright
21  *    notice, this list of conditions and the following disclaimer in the
22  *    documentation and/or other materials provided with the distribution.
23  * 3. Neither the name of The Internet Software Consortium nor the names
24  *    of its contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND
28  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
29  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
30  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
31  * DISCLAIMED.  IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR
32  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
35  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
36  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
37  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
38  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39  * SUCH DAMAGE.
40  *
41  * This software has been written for the Internet Software Consortium
42  * by Ted Lemon <mellon@fugue.com> in cooperation with Vixie
43  * Enterprises.  To learn more about the Internet Software Consortium,
44  * see ``http://www.vix.com/isc''.  To learn more about Vixie
45  * Enterprises, see ``http://www.vix.com''.
46  */
47 
48 #include <sys/cdefs.h>
49 __FBSDID("$FreeBSD$");
50 
51 #include "dhcpd.h"
52 
53 u_int32_t
54 getULong(unsigned char *buf)
55 {
56 	u_int32_t ibuf;
57 
58 	memcpy(&ibuf, buf, sizeof(ibuf));
59 	return (ntohl(ibuf));
60 }
61 
62 int32_t
63 getLong(unsigned char *(buf))
64 {
65 	int32_t ibuf;
66 
67 	memcpy(&ibuf, buf, sizeof(ibuf));
68 	return (ntohl(ibuf));
69 }
70 
71 u_int16_t
72 getUShort(unsigned char *buf)
73 {
74 	u_int16_t ibuf;
75 
76 	memcpy(&ibuf, buf, sizeof(ibuf));
77 	return (ntohs(ibuf));
78 }
79 
80 int16_t
81 getShort(unsigned char *buf)
82 {
83 	int16_t ibuf;
84 
85 	memcpy(&ibuf, buf, sizeof(ibuf));
86 	return (ntohs(ibuf));
87 }
88 
89 void
90 putULong(unsigned char *obuf, u_int32_t val)
91 {
92 	u_int32_t tmp = htonl(val);
93 
94 	memcpy(obuf, &tmp, sizeof(tmp));
95 }
96 
97 void
98 putLong(unsigned char *obuf, int32_t val)
99 {
100 	int32_t tmp = htonl(val);
101 
102 	memcpy(obuf, &tmp, sizeof(tmp));
103 }
104 
105 void
106 putUShort(unsigned char *obuf, unsigned int val)
107 {
108 	u_int16_t tmp = htons(val);
109 
110 	memcpy(obuf, &tmp, sizeof(tmp));
111 }
112 
113 void
114 putShort(unsigned char *obuf, int val)
115 {
116 	int16_t tmp = htons(val);
117 
118 	memcpy(obuf, &tmp, sizeof(tmp));
119 }
120