OpenVAS Scanner  7.0.0~git
byteorder.h
Go to the documentation of this file.
1 /* Copyright (C) Andrew Tridgell 1992-1998
2  *
3  * SPDX-License-Identifier: GPL-2.0-or-later
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
25 #ifndef _BYTEORDER_H
26 #define _BYTEORDER_H
27 
28 /*
29  This file implements macros for machine independent short and
30  int manipulation
31 
32 Here is a description of this file that I emailed to the samba list once:
33 
34 > I am confused about the way that byteorder.h works in Samba. I have
35 > looked at it, and I would have thought that you might make a distinction
36 > between LE and BE machines, but you only seem to distinguish between 386
37 > and all other architectures.
38 >
39 > Can you give me a clue?
40 
41 sure.
42 
43 The distinction between 386 and other architectures is only there as
44 an optimisation. You can take it out completely and it will make no
45 difference. The routines (macros) in byteorder.h are totally byteorder
46 independent. The 386 optimsation just takes advantage of the fact that
47 the x86 processors don't care about alignment, so we don't have to
48 align ints on int boundaries etc. If there are other processors out
49 there that aren't alignment sensitive then you could also define
50 CAREFUL_ALIGNMENT=0 on those processors as well.
51 
52 Ok, now to the macros themselves. I'll take a simple example, say we
53 want to extract a 2 byte integer from a SMB packet and put it into a
54 type called uint16 that is in the local machines byte order, and you
55 want to do it with only the assumption that uint16 is _at_least_ 16
56 bits long (this last condition is very important for architectures
57 that don't have any int types that are 2 bytes long)
58 
59 You do this:
60 
61 #define CVAL(buf,pos) (((unsigned char *)(buf))[pos])
62 #define PVAL(buf,pos) ((unsigned)CVAL(buf,pos))
63 #define SVAL(buf,pos) (PVAL(buf,pos)|PVAL(buf,(pos)+1)<<8)
64 
65 then to extract a uint16 value at offset 25 in a buffer you do this:
66 
67 char *buffer = foo_bar();
68 uint16 xx = SVAL(buffer,25);
69 
70 We are using the byteoder independence of the ANSI C bitshifts to do
71 the work. A good optimising compiler should turn this into efficient
72 code, especially if it happens to have the right byteorder :-)
73 
74 I know these macros can be made a bit tidier by removing some of the
75 casts, but you need to look at byteorder.h as a whole to see the
76 reasoning behind them. byteorder.h defines the following macros:
77 
78 SVAL(buf,pos) - extract a 2 byte SMB value
79 IVAL(buf,pos) - extract a 4 byte SMB value
80 SVALS(buf,pos) signed version of SVAL()
81 IVALS(buf,pos) signed version of IVAL()
82 
83 SSVAL(buf,pos,val) - put a 2 byte SMB value into a buffer
84 SIVAL(buf,pos,val) - put a 4 byte SMB value into a buffer
85 SSVALS(buf,pos,val) - signed version of SSVAL()
86 SIVALS(buf,pos,val) - signed version of SIVAL()
87 
88 RSVAL(buf,pos) - like SVAL() but for NMB byte ordering
89 RSVALS(buf,pos) - like SVALS() but for NMB byte ordering
90 RIVAL(buf,pos) - like IVAL() but for NMB byte ordering
91 RIVALS(buf,pos) - like IVALS() but for NMB byte ordering
92 RSSVAL(buf,pos,val) - like SSVAL() but for NMB ordering
93 RSIVAL(buf,pos,val) - like SIVAL() but for NMB ordering
94 RSIVALS(buf,pos,val) - like SIVALS() but for NMB ordering
95 
96 it also defines lots of intermediate macros, just ignore those :-)
97 
98 */
99 
100 #undef CAREFUL_ALIGNMENT
101 
102 /* we know that the 386 can handle misalignment and has the "right"
103  byteorder */
104 #ifdef __i386__
105 #define CAREFUL_ALIGNMENT 0
106 #endif
107 
108 #ifndef CAREFUL_ALIGNMENT
109 #define CAREFUL_ALIGNMENT 1
110 #endif
111 
112 #define CVAL(buf, pos) ((unsigned) (((const unsigned char *) (buf))[pos]))
113 #define CVAL_NC(buf, pos) \
114  (((unsigned char *) (buf))[pos]) /* Non-const version of CVAL */
115 #define PVAL(buf, pos) (CVAL (buf, pos))
116 #define SCVAL(buf, pos, val) (CVAL_NC (buf, pos) = (val))
117 
118 #if CAREFUL_ALIGNMENT
119 
120 #define SVAL(buf, pos) (PVAL (buf, pos) | PVAL (buf, (pos) + 1) << 8)
121 #define IVAL(buf, pos) (SVAL (buf, pos) | SVAL (buf, (pos) + 2) << 16)
122 #define SSVALX(buf, pos, val) \
123  (CVAL_NC (buf, pos) = (unsigned char) ((val) &0xFF), \
124  CVAL_NC (buf, pos + 1) = (unsigned char) ((val) >> 8))
125 #define SIVALX(buf, pos, val) \
126  (SSVALX (buf, pos, val & 0xFFFF), SSVALX (buf, pos + 2, val >> 16))
127 #define SVALS(buf, pos) ((int16) SVAL (buf, pos))
128 #define IVALS(buf, pos) ((int32) IVAL (buf, pos))
129 #define SSVAL(buf, pos, val) SSVALX ((buf), (pos), ((uint16) (val)))
130 #define SIVAL(buf, pos, val) SIVALX ((buf), (pos), ((uint32) (val)))
131 #define SSVALS(buf, pos, val) SSVALX ((buf), (pos), ((int16) (val)))
132 #define SIVALS(buf, pos, val) SIVALX ((buf), (pos), ((int32) (val)))
133 
134 #else /* CAREFUL_ALIGNMENT */
135 
136 /* this handles things for architectures like the 386 that can handle
137  alignment errors */
138 /*
139  WARNING: This section is dependent on the length of int16 and int32
140  being correct
141 */
142 
143 /* get single value from an SMB buffer */
144 #define SVAL(buf, pos) (*(const uint16 *) ((const char *) (buf) + (pos)))
145 #define SVAL_NC(buf, pos) \
146  (*(uint16 *) ((char *) (buf) + (pos))) /* Non const version of above. */
147 #define IVAL(buf, pos) (*(const uint32 *) ((const char *) (buf) + (pos)))
148 #define IVAL_NC(buf, pos) \
149  (*(uint32 *) ((char *) (buf) + (pos))) /* Non const version of above. */
150 #define SVALS(buf, pos) (*(const int16 *) ((const char *) (buf) + (pos)))
151 #define SVALS_NC(buf, pos) \
152  (*(int16 *) ((char *) (buf) + (pos))) /* Non const version of above. */
153 #define IVALS(buf, pos) (*(const int32 *) ((const char *) (buf) + (pos)))
154 #define IVALS_NC(buf, pos) \
155  (*(int32 *) ((char *) (buf) + (pos))) /* Non const version of above. */
156 
157 /* store single value in an SMB buffer */
158 #define SSVAL(buf, pos, val) SVAL_NC (buf, pos) = ((uint16) (val))
159 #define SIVAL(buf, pos, val) IVAL_NC (buf, pos) = ((uint32) (val))
160 #define SSVALS(buf, pos, val) SVALS_NC (buf, pos) = ((int16) (val))
161 #define SIVALS(buf, pos, val) IVALS_NC (buf, pos) = ((int32) (val))
162 
163 #endif /* CAREFUL_ALIGNMENT */
164 
165 /* now the reverse routines - these are used in nmb packets (mostly) */
166 #define SREV(x) ((((x) &0xFF) << 8) | (((x) >> 8) & 0xFF))
167 #define IREV(x) ((SREV (x) << 16) | (SREV ((x) >> 16)))
168 
169 #define RSVAL(buf, pos) SREV (SVAL (buf, pos))
170 #define RSVALS(buf, pos) SREV (SVALS (buf, pos))
171 #define RIVAL(buf, pos) IREV (IVAL (buf, pos))
172 #define RIVALS(buf, pos) IREV (IVALS (buf, pos))
173 #define RSSVAL(buf, pos, val) SSVAL (buf, pos, SREV (val))
174 #define RSSVALS(buf, pos, val) SSVALS (buf, pos, SREV (val))
175 #define RSIVAL(buf, pos, val) SIVAL (buf, pos, IREV (val))
176 #define RSIVALS(buf, pos, val) SIVALS (buf, pos, IREV (val))
177 
178 /* Alignment macros. */
179 #define ALIGN4(p, base) ((p) + ((4 - (PTR_DIFF ((p), (base)) & 3)) & 3))
180 #define ALIGN2(p, base) ((p) + ((2 - (PTR_DIFF ((p), (base)) & 1)) & 1))
181 
182 #endif /* _BYTEORDER_H */