2 Unix SMB/Netbios implementation.
5 Copyright (C) Andrew Tridgell 1992-1997
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 This file implements macros for machine independent short and
26 Here is a description of this file that I emailed to the samba list once:
28 > I am confused about the way that byteorder.h works in Samba. I have
29 > looked at it, and I would have thought that you might make a distinction
30 > between LE and BE machines, but you only seem to distinguish between 386
31 > and all other architectures.
33 > Can you give me a clue?
37 The distinction between 386 and other architectures is only there as
38 an optimisation. You can take it out completely and it will make no
39 difference. The routines (macros) in byteorder.h are totally byteorder
40 independent. The 386 optimsation just takes advantage of the fact that
41 the x86 processors don't care about alignment, so we don't have to
42 align ints on int boundaries etc. If there are other processors out
43 there that aren't alignment sensitive then you could also define
44 CAREFUL_ALIGNMENT=0 on those processors as well.
46 Ok, now to the macros themselves. I'll take a simple example, say we
47 want to extract a 2 byte integer from a SMB packet and put it into a
48 type called uint16 that is in the local machines byte order, and you
49 want to do it with only the assumption that uint16 is _at_least_ 16
50 bits long (this last condition is very important for architectures
51 that don't have any int types that are 2 bytes long)
55 #define CVAL(buf,pos) (((unsigned char *)(buf))[pos])
56 #define PVAL(buf,pos) ((unsigned)CVAL(buf,pos))
57 #define SVAL(buf,pos) (PVAL(buf,pos)|PVAL(buf,(pos)+1)<<8)
59 then to extract a uint16 value at offset 25 in a buffer you do this:
61 char *buffer = foo_bar();
62 uint16 xx = SVAL(buffer,25);
64 We are using the byteoder independence of the ANSI C bitshifts to do
65 the work. A good optimising compiler should turn this into efficient
66 code, especially if it happens to have the right byteorder :-)
68 I know these macros can be made a bit tidier by removing some of the
69 casts, but you need to look at byteorder.h as a whole to see the
70 reasoning behind them. byteorder.h defines the following macros:
72 SVAL(buf,pos) - extract a 2 byte SMB value
73 IVAL(buf,pos) - extract a 4 byte SMB value
74 SVALS(buf,pos) signed version of SVAL()
75 IVALS(buf,pos) signed version of IVAL()
77 SSVAL(buf,pos,val) - put a 2 byte SMB value into a buffer
78 SIVAL(buf,pos,val) - put a 4 byte SMB value into a buffer
79 SSVALS(buf,pos,val) - signed version of SSVAL()
80 SIVALS(buf,pos,val) - signed version of SIVAL()
82 RSVAL(buf,pos) - like SVAL() but for NMB byte ordering
83 RIVAL(buf,pos) - like IVAL() 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
87 it also defines lots of intermediate macros, just ignore those :-)
91 /* some switch macros that do both store and read to and from SMB buffers */
93 #define RW_PCVAL(read,inbuf,outbuf,len) \
94 if (read) { PCVAL (inbuf,0,outbuf,len) } \
95 else { PSCVAL(inbuf,0,outbuf,len) }
97 #define RW_PIVAL(read,inbuf,outbuf,len) \
98 if (read) { PIVAL (inbuf,0,outbuf,len) } \
99 else { PSIVAL(inbuf,0,outbuf,len) }
101 #define RW_PSVAL(read,inbuf,outbuf,len) \
102 if (read) { PSVAL (inbuf,0,outbuf,len) } \
103 else { PSSVAL(inbuf,0,outbuf,len) }
105 #define RW_CVAL(read, inbuf, outbuf, offset) \
106 if (read) (outbuf) = CVAL (inbuf,offset); \
107 else SCVAL(inbuf,offset,outbuf);
109 #define RW_IVAL(read, inbuf, outbuf, offset) \
110 if (read) (outbuf)= IVAL (inbuf,offset); \
111 else SIVAL(inbuf,offset,outbuf);
113 #define RW_SVAL(read, inbuf, outbuf, offset) \
114 if (read) (outbuf)= SVAL (inbuf,offset); \
115 else SSVAL(inbuf,offset,outbuf);
117 #undef CAREFUL_ALIGNMENT
119 /* we know that the 386 can handle misalignment and has the "right"
122 #define CAREFUL_ALIGNMENT 0
125 #ifndef CAREFUL_ALIGNMENT
126 #define CAREFUL_ALIGNMENT 1
129 #define CVAL(buf,pos) (((unsigned char *)(buf))[pos])
130 #define PVAL(buf,pos) ((unsigned)CVAL(buf,pos))
131 #define SCVAL(buf,pos,val) (CVAL(buf,pos) = (val))
134 #if CAREFUL_ALIGNMENT
136 #define SVAL(buf,pos) (PVAL(buf,pos)|PVAL(buf,(pos)+1)<<8)
137 #define IVAL(buf,pos) (SVAL(buf,pos)|SVAL(buf,(pos)+2)<<16)
138 #define SSVALX(buf,pos,val) (CVAL(buf,pos)=(val)&0xFF,CVAL(buf,pos+1)=(val)>>8)
139 #define SIVALX(buf,pos,val) (SSVALX(buf,pos,val&0xFFFF),SSVALX(buf,pos+2,val>>16))
140 #define SVALS(buf,pos) ((int16)SVAL(buf,pos))
141 #define IVALS(buf,pos) ((int32)IVAL(buf,pos))
142 #define SSVAL(buf,pos,val) SSVALX((buf),(pos),((uint16)(val)))
143 #define SIVAL(buf,pos,val) SIVALX((buf),(pos),((uint32)(val)))
144 #define SSVALS(buf,pos,val) SSVALX((buf),(pos),((int16)(val)))
145 #define SIVALS(buf,pos,val) SIVALX((buf),(pos),((int32)(val)))
149 /* this handles things for architectures like the 386 that can handle
152 WARNING: This section is dependent on the length of int16 and int32
156 /* get single value from an SMB buffer */
157 #define SVAL(buf,pos) (*(uint16 *)((char *)(buf) + (pos)))
158 #define IVAL(buf,pos) (*(uint32 *)((char *)(buf) + (pos)))
159 #define SVALS(buf,pos) (*(int16 *)((char *)(buf) + (pos)))
160 #define IVALS(buf,pos) (*(int32 *)((char *)(buf) + (pos)))
162 /* store single value in an SMB buffer */
163 #define SSVAL(buf,pos,val) SVAL(buf,pos)=((uint16)(val))
164 #define SIVAL(buf,pos,val) IVAL(buf,pos)=((uint32)(val))
165 #define SSVALS(buf,pos,val) SVALS(buf,pos)=((int16)(val))
166 #define SIVALS(buf,pos,val) IVALS(buf,pos)=((int32)(val))
171 /* macros for reading / writing arrays */
173 #define SMBMACRO(macro,buf,pos,val,len,size) \
174 { int l; for (l = 0; l < (len); l++) (val)[l] = macro((buf), (pos) + (size)*l); }
176 #define SSMBMACRO(macro,buf,pos,val,len,size) \
177 { int l; for (l = 0; l < (len); l++) macro((buf), (pos) + (size)*l, (val)[l]); }
179 /* reads multiple data from an SMB buffer */
180 #define PCVAL(buf,pos,val,len) SMBMACRO(CVAL,buf,pos,val,len,1)
181 #define PSVAL(buf,pos,val,len) SMBMACRO(SVAL,buf,pos,val,len,2)
182 #define PIVAL(buf,pos,val,len) SMBMACRO(IVAL,buf,pos,val,len,4)
183 #define PCVALS(buf,pos,val,len) SMBMACRO(CVALS,buf,pos,val,len,1)
184 #define PSVALS(buf,pos,val,len) SMBMACRO(SVALS,buf,pos,val,len,2)
185 #define PIVALS(buf,pos,val,len) SMBMACRO(IVALS,buf,pos,val,len,4)
187 /* stores multiple data in an SMB buffer */
188 #define PSCVAL(buf,pos,val,len) SSMBMACRO(SCVAL,buf,pos,val,len,1)
189 #define PSSVAL(buf,pos,val,len) SSMBMACRO(SSVAL,buf,pos,val,len,2)
190 #define PSIVAL(buf,pos,val,len) SSMBMACRO(SIVAL,buf,pos,val,len,4)
191 #define PSCVALS(buf,pos,val,len) SSMBMACRO(SCVALS,buf,pos,val,len,1)
192 #define PSSVALS(buf,pos,val,len) SSMBMACRO(SSVALS,buf,pos,val,len,2)
193 #define PSIVALS(buf,pos,val,len) SSMBMACRO(SIVALS,buf,pos,val,len,4)
196 /* now the reverse routines - these are used in nmb packets (mostly) */
197 #define SREV(x) ((((x)&0xFF)<<8) | (((x)>>8)&0xFF))
198 #define IREV(x) ((SREV(x)<<16) | (SREV((x)>>16)))
200 #define RSVAL(buf,pos) SREV(SVAL(buf,pos))
201 #define RIVAL(buf,pos) IREV(IVAL(buf,pos))
202 #define RSSVAL(buf,pos,val) SSVAL(buf,pos,SREV(val))
203 #define RSIVAL(buf,pos,val) SIVAL(buf,pos,IREV(val))
205 #define DBG_RW_PCVAL(charmode,string,depth,base,read,inbuf,outbuf,len) \
206 RW_PCVAL(read,inbuf,outbuf,len) \
207 DEBUG(5,("%s%04x %s: ", \
208 tab_depth(depth), PTR_DIFF(inbuf,base),string)); \
209 if (charmode) print_asc(5, (char*)(outbuf), (len)); else \
210 { int idx; for (idx = 0; idx < len; idx++) { DEBUG(5,("%02x ", CVAL(&((outbuf)[idx]), 0))); } } \
213 #define DBG_RW_PSVAL(charmode,string,depth,base,read,inbuf,outbuf,len) \
214 RW_PSVAL(read,inbuf,outbuf,len) \
215 DEBUG(5,("%s%04x %s: ", \
216 tab_depth(depth), PTR_DIFF(inbuf,base),string)); \
217 if (charmode) print_asc(5, (char*)(outbuf), 2*(len)); else \
218 { int idx; for (idx = 0; idx < len; idx++) { DEBUG(5,("%04x ", SVAL(&((outbuf)[idx]), 0))); } } \
221 #define DBG_RW_PIVAL(charmode,string,depth,base,read,inbuf,outbuf,len) \
222 RW_PIVAL(read,inbuf,outbuf,len) \
223 DEBUG(5,("%s%04x %s: ", \
224 tab_depth(depth), PTR_DIFF(inbuf,base),string)); \
225 if (charmode) print_asc(5, (char*)(outbuf), 4*(len)); else \
226 { int idx; for (idx = 0; idx < len; idx++) { DEBUG(5,("%08x ", IVAL(&((outbuf)[idx]), 0))); } } \
229 #define DBG_RW_CVAL(string,depth,base,read,inbuf,outbuf) \
230 RW_CVAL(read,inbuf,outbuf,0) \
231 DEBUG(5,("%s%04x %s: %02x\n", \
232 tab_depth(depth), PTR_DIFF(inbuf,base), string, outbuf));
234 #define DBG_RW_SVAL(string,depth,base,read,inbuf,outbuf) \
235 RW_SVAL(read,inbuf,outbuf,0) \
236 DEBUG(5,("%s%04x %s: %04x\n", \
237 tab_depth(depth), PTR_DIFF(inbuf,base), string, outbuf));
239 #define DBG_RW_IVAL(string,depth,base,read,inbuf,outbuf) \
240 RW_IVAL(read,inbuf,outbuf,0) \
241 DEBUG(5,("%s%04x %s: %08x\n", \
242 tab_depth(depth), PTR_DIFF(inbuf,base), string, outbuf));