- got fed up of vuser_db spewing 150 lines of trash, made it possible
[Samba.git] / source / include / byteorder.h
blobb36649989eb516d3bf01200c1c343c2c2708f008
1 /*
2 Unix SMB/Netbios implementation.
3 Version 1.9.
4 SMB Byte handling
5 Copyright (C) Andrew Tridgell 1992-1998
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.
22 #ifndef _BYTEORDER_H
23 #define _BYTEORDER_H
26 This file implements macros for machine independent short and
27 int manipulation
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?
38 sure.
40 The distinction between 386 and other architectures is only there as
41 an optimisation. You can take it out completely and it will make no
42 difference. The routines (macros) in byteorder.h are totally byteorder
43 independent. The 386 optimsation just takes advantage of the fact that
44 the x86 processors don't care about alignment, so we don't have to
45 align ints on int boundaries etc. If there are other processors out
46 there that aren't alignment sensitive then you could also define
47 CAREFUL_ALIGNMENT=0 on those processors as well.
49 Ok, now to the macros themselves. I'll take a simple example, say we
50 want to extract a 2 byte integer from a SMB packet and put it into a
51 type called uint16 that is in the local machines byte order, and you
52 want to do it with only the assumption that uint16 is _at_least_ 16
53 bits long (this last condition is very important for architectures
54 that don't have any int types that are 2 bytes long)
56 You do this:
58 #define CVAL(buf,pos) (((unsigned char *)(buf))[pos])
59 #define PVAL(buf,pos) ((unsigned)CVAL(buf,pos))
60 #define SVAL(buf,pos) (PVAL(buf,pos)|PVAL(buf,(pos)+1)<<8)
62 then to extract a uint16 value at offset 25 in a buffer you do this:
64 char *buffer = foo_bar();
65 uint16 xx = SVAL(buffer,25);
67 We are using the byteoder independence of the ANSI C bitshifts to do
68 the work. A good optimising compiler should turn this into efficient
69 code, especially if it happens to have the right byteorder :-)
71 I know these macros can be made a bit tidier by removing some of the
72 casts, but you need to look at byteorder.h as a whole to see the
73 reasoning behind them. byteorder.h defines the following macros:
75 SVAL(buf,pos) - extract a 2 byte SMB value
76 IVAL(buf,pos) - extract a 4 byte SMB value
77 SVALS(buf,pos) signed version of SVAL()
78 IVALS(buf,pos) signed version of IVAL()
80 SSVAL(buf,pos,val) - put a 2 byte SMB value into a buffer
81 SIVAL(buf,pos,val) - put a 4 byte SMB value into a buffer
82 SSVALS(buf,pos,val) - signed version of SSVAL()
83 SIVALS(buf,pos,val) - signed version of SIVAL()
85 RSVAL(buf,pos) - like SVAL() but for NMB byte ordering
86 RSVALS(buf,pos) - like SVALS() but for NMB byte ordering
87 RIVAL(buf,pos) - like IVAL() but for NMB byte ordering
88 RIVALS(buf,pos) - like IVALS() but for NMB byte ordering
89 RSSVAL(buf,pos,val) - like SSVAL() but for NMB ordering
90 RSIVAL(buf,pos,val) - like SIVAL() but for NMB ordering
91 RSIVALS(buf,pos,val) - like SIVALS() but for NMB ordering
93 it also defines lots of intermediate macros, just ignore those :-)
97 /* some switch macros that do both store and read to and from SMB buffers */
99 #define RW_PCVAL(read,inbuf,outbuf,len) \
100 { if (read) { PCVAL (inbuf,0,outbuf,len); } \
101 else { PSCVAL(inbuf,0,outbuf,len); } }
103 #define RW_PIVAL(read,big_endian,inbuf,outbuf,len) \
104 { if (read) { if (big_endian) { RPIVAL(inbuf,0,outbuf,len); } else { PIVAL(inbuf,0,outbuf,len); } } \
105 else { if (big_endian) { RPSIVAL(inbuf,0,outbuf,len); } else { PSIVAL(inbuf,0,outbuf,len); } } }
107 #define RW_PSVAL(read,big_endian,inbuf,outbuf,len) \
108 { if (read) { if (big_endian) { RPSVAL(inbuf,0,outbuf,len); } else { PSVAL(inbuf,0,outbuf,len); } } \
109 else { if (big_endian) { RPSSVAL(inbuf,0,outbuf,len); } else { PSSVAL(inbuf,0,outbuf,len); } } }
111 #define RW_CVAL(read, inbuf, outbuf, offset) \
112 { if (read) { (outbuf) = CVAL (inbuf,offset); } \
113 else { SCVAL(inbuf,offset,outbuf); } }
115 #define RW_IVAL(read, big_endian, inbuf, outbuf, offset) \
116 { if (read) { (outbuf) = ((big_endian) ? RIVAL(inbuf,offset) : IVAL (inbuf,offset)); } \
117 else { if (big_endian) { RSIVAL(inbuf,offset,outbuf); } else { SIVAL(inbuf,offset,outbuf); } } }
119 #define RW_SVAL(read, big_endian, inbuf, outbuf, offset) \
120 { if (read) { (outbuf) = ((big_endian) ? RSVAL(inbuf,offset) : SVAL (inbuf,offset)); } \
121 else { if (big_endian) { RSSVAL(inbuf,offset,outbuf); } else { SSVAL(inbuf,offset,outbuf); } } }
123 #undef CAREFUL_ALIGNMENT
125 /* we know that the 386 can handle misalignment and has the "right"
126 byteorder */
127 #ifdef __i386__
128 #define CAREFUL_ALIGNMENT 0
129 #endif
131 #ifndef CAREFUL_ALIGNMENT
132 #define CAREFUL_ALIGNMENT 1
133 #endif
135 #define CVAL(buf,pos) (((unsigned char *)(buf))[pos])
136 #define PVAL(buf,pos) ((unsigned)CVAL(buf,pos))
137 #define SCVAL(buf,pos,val) (CVAL(buf,pos) = (val))
139 #define CVALCONST(buf,pos) (((const unsigned char *)(buf))[pos])
140 #define PVALCONST(buf,pos) ((const unsigned)CVALCONST(buf,pos))
142 #if CAREFUL_ALIGNMENT
144 #define SVAL(buf,pos) (PVALCONST(buf,pos)|PVALCONST(buf,(pos)+1)<<8)
145 #define IVAL(buf,pos) (SVAL(buf,pos)|SVAL(buf,(pos)+2)<<16)
146 #define SSVALX(buf,pos,val) (CVAL(buf,pos)=(val)&0xFF,CVAL(buf,pos+1)=(val)>>8)
147 #define SIVALX(buf,pos,val) (SSVALX(buf,pos,val&0xFFFF),SSVALX(buf,pos+2,val>>16))
148 #define SVALS(buf,pos) ((int16)SVAL(buf,pos))
149 #define IVALS(buf,pos) ((int32)IVAL(buf,pos))
150 #define SSVAL(buf,pos,val) SSVALX((buf),(pos),((uint16)(val)))
151 #define SIVAL(buf,pos,val) SIVALX((buf),(pos),((uint32)(val)))
152 #define SSVALS(buf,pos,val) SSVALX((buf),(pos),((int16)(val)))
153 #define SIVALS(buf,pos,val) SIVALX((buf),(pos),((int32)(val)))
155 #define SVALMOD(buf,pos) (PVAL(buf,pos)|PVAL(buf,(pos)+1)<<8)
156 #define IVALMOD(buf,pos) (SVALMOD(buf,pos)|SVALMOD(buf,(pos)+2)<<16)
157 #define SVALSMOD(buf,pos) ((int16)SVALMOD(buf,pos))
158 #define IVALSMOD(buf,pos) ((int32)IVALMOD(buf,pos))
160 #else /* CAREFUL_ALIGNMENT */
162 /* this handles things for architectures like the 386 that can handle
163 alignment errors */
165 WARNING: This section is dependent on the length of int16 and int32
166 being correct
169 /* get single value from an SMB buffer */
170 #define SVAL(buf,pos) (*(const uint16 *)((const char *)(buf) + (pos)))
171 #define IVAL(buf,pos) (*(const uint32 *)((const char *)(buf) + (pos)))
172 #define SVALS(buf,pos) (*(const int16 *)((const char *)(buf) + (pos)))
173 #define IVALS(buf,pos) (*(const int32 *)((const char *)(buf) + (pos)))
175 /* store single value in an SMB buffer */
176 #define SVALMOD(buf,pos) (*(uint16 *)((char *)(buf) + (pos)))
177 #define IVALMOD(buf,pos) (*(uint32 *)((char *)(buf) + (pos)))
178 #define SVALSMOD(buf,pos) (*(int16 *)((char *)(buf) + (pos)))
179 #define IVALSMOD(buf,pos) (*(int32 *)((char *)(buf) + (pos)))
181 #define SSVAL(buf,pos,val) SVALMOD(buf,pos)=((uint16)(val))
182 #define SIVAL(buf,pos,val) IVALMOD(buf,pos)=((uint32)(val))
183 #define SSVALS(buf,pos,val) SVALSMOD(buf,pos)=((int16)(val))
184 #define SIVALS(buf,pos,val) IVALSMOD(buf,pos)=((int32)(val))
186 #endif /* CAREFUL_ALIGNMENT */
188 /* macros for reading / writing arrays */
190 #define SMBMACRO(macro,buf,pos,val,len,size) \
191 { uint32 l; for (l = 0; l < (uint32)(len); l++) (val)[l] = macro((buf), (pos) + (size)*l); }
193 #define SSMBMACRO(macro,buf,pos,val,len,size) \
194 { uint32 l; for (l = 0; l < (uint32)(len); l++) macro((buf), (pos) + (size)*l, (val)[l]); }
196 /* reads multiple data from an SMB buffer */
197 #define PCVAL(buf,pos,val,len) SMBMACRO(CVAL,buf,pos,val,len,1)
198 #define PSVAL(buf,pos,val,len) SMBMACRO(SVAL,buf,pos,val,len,2)
199 #define PIVAL(buf,pos,val,len) SMBMACRO(IVAL,buf,pos,val,len,4)
200 #define PCVALS(buf,pos,val,len) SMBMACRO(CVALS,buf,pos,val,len,1)
201 #define PSVALS(buf,pos,val,len) SMBMACRO(SVALS,buf,pos,val,len,2)
202 #define PIVALS(buf,pos,val,len) SMBMACRO(IVALS,buf,pos,val,len,4)
204 /* stores multiple data in an SMB buffer */
205 #define PSCVAL(buf,pos,val,len) SSMBMACRO(SCVAL,buf,pos,val,len,1)
206 #define PSSVAL(buf,pos,val,len) SSMBMACRO(SSVAL,buf,pos,val,len,2)
207 #define PSIVAL(buf,pos,val,len) SSMBMACRO(SIVAL,buf,pos,val,len,4)
208 #define PSCVALS(buf,pos,val,len) SSMBMACRO(SCVALS,buf,pos,val,len,1)
209 #define PSSVALS(buf,pos,val,len) SSMBMACRO(SSVALS,buf,pos,val,len,2)
210 #define PSIVALS(buf,pos,val,len) SSMBMACRO(SIVALS,buf,pos,val,len,4)
213 /* now the reverse routines - these are used in nmb packets (mostly) */
214 #define SREV(x) ((((x)&0xFF)<<8) | (((x)>>8)&0xFF))
215 #define IREV(x) ((SREV(x)<<16) | (SREV((x)>>16)))
217 #define RSVAL(buf,pos) SREV(SVAL(buf,pos))
218 #define RSVALS(buf,pos) SREV(SVALS(buf,pos))
219 #define RIVAL(buf,pos) IREV(IVAL(buf,pos))
220 #define RIVALS(buf,pos) IREV(IVALS(buf,pos))
221 #define RSSVAL(buf,pos,val) SSVAL(buf,pos,SREV(val))
222 #define RSSVALS(buf,pos,val) SSVALS(buf,pos,SREV(val))
223 #define RSIVAL(buf,pos,val) SIVAL(buf,pos,IREV(val))
224 #define RSIVALS(buf,pos,val) SIVALS(buf,pos,IREV(val))
226 /* reads multiple data from an SMB buffer (big-endian) */
227 #define RPSVAL(buf,pos,val,len) SMBMACRO(RSVAL,buf,pos,val,len,2)
228 #define RPIVAL(buf,pos,val,len) SMBMACRO(RIVAL,buf,pos,val,len,4)
229 #define RPSVALS(buf,pos,val,len) SMBMACRO(RSVALS,buf,pos,val,len,2)
230 #define RPIVALS(buf,pos,val,len) SMBMACRO(RIVALS,buf,pos,val,len,4)
232 /* stores multiple data in an SMB buffer (big-endian) */
233 #define RPSSVAL(buf,pos,val,len) SSMBMACRO(RSSVAL,buf,pos,val,len,2)
234 #define RPSIVAL(buf,pos,val,len) SSMBMACRO(RSIVAL,buf,pos,val,len,4)
235 #define RPSSVALS(buf,pos,val,len) SSMBMACRO(RSSVALS,buf,pos,val,len,2)
236 #define RPSIVALS(buf,pos,val,len) SSMBMACRO(RSIVALS,buf,pos,val,len,4)
238 #define DBG_RW_PCVAL(charmode,string,depth,base,read,inbuf,outbuf,len) \
239 { RW_PCVAL(read,inbuf,outbuf,len) \
240 DEBUG(5+depth,("%s%04x %s: ", \
241 tab_depth(depth), base,string)); \
242 if (charmode) print_asc(depth+5, (unsigned char*)(outbuf), (len)); else \
243 { uint32 idx; for (idx = 0; idx < len; idx++) { DEBUG(5+depth,("%02x ", (outbuf)[idx])); } } \
244 DEBUG(5+depth,("\n")); }
246 #define DBG_RW_PSVAL(charmode,string,depth,base,read,big_endian,inbuf,outbuf,len) \
247 { RW_PSVAL(read,big_endian,inbuf,outbuf,len) \
248 DEBUG(5+depth,("%s%04x %s: ", \
249 tab_depth(depth), base,string)); \
250 if (charmode) print_asc(depth+5, (unsigned char*)(outbuf), 2*(len)); else \
251 { uint32 idx; for (idx = 0; idx < len; idx++) { DEBUG(5+depth,("%04x ", (outbuf)[idx])); } } \
252 DEBUG(5+depth,("\n")); }
254 #define DBG_RW_PIVAL(charmode,string,depth,base,read,big_endian,inbuf,outbuf,len) \
255 { RW_PIVAL(read,big_endian,inbuf,outbuf,len) \
256 DEBUG(5+depth,("%s%04x %s: ", \
257 tab_depth(depth), base,string)); \
258 if (charmode) print_asc(depth+5, (unsigned char*)(outbuf), 4*(len)); else \
259 { uint32 idx; for (idx = 0; idx < len; idx++) { DEBUG(5+depth,("%08x ", (outbuf)[idx])); } } \
260 DEBUG(5+depth,("\n")); }
262 #define DBG_RW_CVAL(string,depth,base,read,inbuf,outbuf) \
263 { RW_CVAL(read,inbuf,outbuf,0) \
264 DEBUG(5+depth,("%s%04x %s: %02x\n", \
265 tab_depth(depth), base, string, outbuf)); }
267 #define DBG_RW_SVAL(string,depth,base,read,big_endian,inbuf,outbuf) \
268 { RW_SVAL(read,big_endian,inbuf,outbuf,0) \
269 DEBUG(5+depth,("%s%04x %s: %04x\n", \
270 tab_depth(depth), base, string, outbuf)); }
272 #define DBG_RW_IVAL(string,depth,base,read,big_endian,inbuf,outbuf) \
273 { RW_IVAL(read,big_endian,inbuf,outbuf,0) \
274 DEBUG(5+depth,("%s%04x %s: %08x\n", \
275 tab_depth(depth), base, string, outbuf)); }
277 /* Alignment macros. */
278 #define ALIGN4(p,base) ((p) + ((4 - (PTR_DIFF((p), (base)) & 3)) & 3))
279 #define ALIGN2(p,base) ((p) + ((2 - (PTR_DIFF((p), (base)) & 1)) & 1))
281 #endif /* _BYTEORDER_H */