2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 1992-1998
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "bytearray.h"
26 This file implements macros for machine independent short and
29 Here is a description of this file that I emailed to the samba list once:
31 > I am confused about the way that byteorder.h works in Samba. I have
32 > looked at it, and I would have thought that you might make a distinction
33 > between LE and BE machines, but you only seem to distinguish between 386
34 > and all other architectures.
36 > Can you give me a clue?
40 Ok, now to the macros themselves. I'll take a simple example, say we
41 want to extract a 2 byte integer from a SMB packet and put it into a
42 type called uint16_t that is in the local machines byte order, and you
43 want to do it with only the assumption that uint16_t is _at_least_ 16
44 bits long (this last condition is very important for architectures
45 that don't have any int types that are 2 bytes long)
49 #define CVAL(buf,pos) (((uint8_t *)(buf))[pos])
50 #define PVAL(buf,pos) ((unsigned int)CVAL(buf,pos))
51 #define SVAL(buf,pos) (PVAL(buf,pos)|PVAL(buf,(pos)+1)<<8)
53 then to extract a uint16_t value at offset 25 in a buffer you do this:
55 char *buffer = foo_bar();
56 uint16_t xx = SVAL(buffer,25);
58 We are using the byteorder independence of the ANSI C bitshifts to do
59 the work. A good optimising compiler should turn this into efficient
60 code, especially if it happens to have the right byteorder :-)
62 I know these macros can be made a bit tidier by removing some of the
63 casts, but you need to look at byteorder.h as a whole to see the
64 reasoning behind them. byteorder.h defines the following macros:
66 SVAL(buf,pos) - extract a 2 byte SMB value
67 IVAL(buf,pos) - extract a 4 byte SMB value
68 BVAL(buf,pos) - extract a 8 byte SMB value
69 SVALS(buf,pos) - signed version of SVAL()
70 IVALS(buf,pos) - signed version of IVAL()
71 BVALS(buf,pos) - signed version of BVAL()
73 SSVAL(buf,pos,val) - put a 2 byte SMB value into a buffer
74 SIVAL(buf,pos,val) - put a 4 byte SMB value into a buffer
75 SBVAL(buf,pos,val) - put a 8 byte SMB value into a buffer
76 SSVALS(buf,pos,val) - signed version of SSVAL()
77 SIVALS(buf,pos,val) - signed version of SIVAL()
78 SBVALS(buf,pos,val) - signed version of SBVAL()
80 RSVAL(buf,pos) - like SVAL() but for NMB byte ordering
81 RSVALS(buf,pos) - like SVALS() but for NMB byte ordering
82 RIVAL(buf,pos) - like IVAL() but for NMB byte ordering
83 RIVALS(buf,pos) - like IVALS() but for NMB byte ordering
84 RSSVAL(buf,pos,val) - like SSVAL() but for NMB ordering
85 RSIVAL(buf,pos,val) - like SIVAL() but for NMB ordering
86 RSIVALS(buf,pos,val) - like SIVALS() but for NMB ordering
88 it also defines lots of intermediate macros, just ignore those :-)
93 /****************************************************************************
95 * ATTENTION: Do not use those macros anymore, use the ones from bytearray.h
97 ****************************************************************************/
99 #define CVAL(buf,pos) ((uint32_t)_DATA_BYTE_CONST(buf, pos))
100 #define CVAL_NC(buf,pos) _DATA_BYTE(buf, pos) /* Non-const version of CVAL */
101 #define PVAL(buf,pos) (CVAL(buf,pos))
102 #define SCVAL(buf,pos,val) (CVAL_NC(buf,pos) = (val))
104 /****************************************************************************
106 * ATTENTION: Do not use those macros anymore, use the ones from bytearray.h
108 ****************************************************************************/
110 #define SVAL(buf,pos) (uint32_t)PULL_LE_U16(buf, pos)
111 #define IVAL(buf,pos) PULL_LE_U32(buf, pos)
112 #define SSVALX(buf,pos,val) (CVAL_NC(buf,pos)=(uint8_t)((val)&0xFF),CVAL_NC(buf,pos+1)=(uint8_t)((val)>>8))
113 #define SIVALX(buf,pos,val) (SSVALX(buf,pos,val&0xFFFF),SSVALX(buf,pos+2,val>>16))
114 #define SVALS(buf,pos) ((int16_t)SVAL(buf,pos))
115 #define IVALS(buf,pos) ((int32_t)IVAL(buf,pos))
116 #define SSVAL(buf,pos,val) PUSH_LE_U16(buf, pos, val)
117 #define SIVAL(buf,pos,val) PUSH_LE_U32(buf, pos, val)
118 #define SSVALS(buf,pos,val) PUSH_LE_U16(buf, pos, val)
119 #define SIVALS(buf,pos,val) PUSH_LE_U32(buf, pos, val)
121 /****************************************************************************
123 * ATTENTION: Do not use those macros anymore, use the ones from bytearray.h
125 ****************************************************************************/
128 #define BVAL(p, ofs) PULL_LE_U64(p, ofs)
129 #define BVALS(p, ofs) ((int64_t)BVAL(p,ofs))
130 #define SBVAL(p, ofs, v) PUSH_LE_U64(p, ofs, v)
131 #define SBVALS(p, ofs, v) (SBVAL(p,ofs,(uint64_t)v))
133 /****************************************************************************
135 * ATTENTION: Do not use those macros anymore, use the ones from bytearray.h
137 ****************************************************************************/
139 /* now the reverse routines - these are used in nmb packets (mostly) */
140 #define SREV(x) ((((x)&0xFF)<<8) | (((x)>>8)&0xFF))
141 #define IREV(x) ((SREV((uint32_t)x)<<16) | (SREV(((uint32_t)x)>>16)))
142 #define BREV(x) ((IREV((uint64_t)x)<<32) | (IREV(((uint64_t)x)>>32)))
144 /****************************************************************************
146 * ATTENTION: Do not use those macros anymore, use the ones from bytearray.h
148 ****************************************************************************/
150 #define RSVAL(buf,pos) (uint32_t)PULL_BE_U16(buf, pos)
151 #define RSVALS(buf,pos) PULL_BE_U16(buf, pos)
152 #define RIVAL(buf,pos) PULL_BE_U32(buf, pos)
153 #define RIVALS(buf,pos) PULL_BE_U32(buf, pos)
154 #define RBVAL(buf,pos) PULL_BE_U64(buf, pos)
155 #define RBVALS(buf,pos) PULL_BE_U64(buf, pos)
156 #define RSSVAL(buf,pos,val) PUSH_BE_U16(buf, pos, val)
157 #define RSSVALS(buf,pos,val) PUSH_BE_U16(buf, pos, val)
158 #define RSIVAL(buf,pos,val) PUSH_BE_U32(buf, pos, val)
159 #define RSIVALS(buf,pos,val) PUSH_BE_U32(buf, pos, val)
160 #define RSBVAL(buf,pos,val) PUSH_BE_U64(buf, pos, val)
161 #define RSBVALS(buf,pos,val) PUSH_BE_U64(buf, pos, val)
163 /****************************************************************************
165 * ATTENTION: Do not use those macros anymore, use the ones from bytearray.h
167 ****************************************************************************/
169 #endif /* _BYTEORDER_H */