FreeBSD/Linux Kernel Cross Reference
sys/geom/geom_enc.c
1 /*-
2 * Copyright (c) 2002 Poul-Henning Kamp
3 * Copyright (c) 2002 Networks Associates Technology, Inc.
4 * All rights reserved.
5 *
6 * This software was developed for the FreeBSD Project by Poul-Henning Kamp
7 * and NAI Labs, the Security Research Division of Network Associates, Inc.
8 * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
9 * DARPA CHATS research program.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. The names of the authors may not be used to endorse or promote
20 * products derived from this software without specific prior written
21 * permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * $FreeBSD: releng/5.0/sys/geom/geom_enc.c 105163 2002-10-15 18:21:53Z phk $
36 */
37
38 /*
39 * This file contains routines to pack and unpack integerfields of varying
40 * width and endianess with. Ideally the C language would have this built
41 * in but I guess Dennis and Brian forgot that back in the early 70ies.
42 *
43 * The function names are systematic: g_{enc|dec}_{be|le}%d
44 * enc -> encode
45 * dec -> decode
46 * be -> big endian
47 * le -> little endian
48 * %d -> width in bytes
49 *
50 * Please keep the functions sorted:
51 * decode before encode
52 * big endian before little endian
53 * small width before larger width
54 * (this happens to be alphabetically)
55 */
56
57 #include <sys/param.h>
58 #ifdef _KERNEL
59 #include <sys/malloc.h>
60 #include <sys/systm.h>
61 #endif
62 #include <geom/geom.h>
63
64 uint16_t
65 g_dec_be2(const u_char *p)
66 {
67
68 return((p[0] << 8) | p[1]);
69 }
70
71 uint32_t
72 g_dec_be4(const u_char *p)
73 {
74
75 return((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
76 }
77
78 uint16_t
79 g_dec_le2(const u_char *p)
80 {
81
82 return((p[1] << 8) | p[0]);
83 }
84
85 void
86 g_enc_le2(u_char *p, uint16_t u)
87 {
88
89 p[0] = u & 0xff;
90 p[1] = (u >> 8) & 0xff;
91 }
92
93 uint32_t
94 g_dec_le4(const u_char *p)
95 {
96
97 return((p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]);
98 }
99
100
101 void
102 g_enc_le4(u_char *p, uint32_t u)
103 {
104
105 p[0] = u & 0xff;
106 p[1] = (u >> 8) & 0xff;
107 p[2] = (u >> 16) & 0xff;
108 p[3] = (u >> 24) & 0xff;
109 }
110
111 uint64_t
112 g_dec_le8(const u_char *p)
113 {
114
115 return(g_dec_le4(p) | ((uint64_t)(g_dec_le4(p + 4)) << 32));
116 }
117
118
119 void
120 g_enc_le8(u_char *p, uint64_t u)
121 {
122
123 g_enc_le4(p, u);
124 g_enc_le4(p + 4, u >> 32);
125 }
126
Cache object: 5e7b462cfb6ddad5dc65ddbe84cabbf1
|