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