preparing for release of alpha.0.0
[Samba.git] / source / include / byteorder.h
bloba3b1437dc2569affa17ee3dc659faf986969e77a
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.
23 This file implements macros for machine independent short and
24 int manipulation
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?
35 sure.
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)
53 You do this:
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"
120 byteorder */
121 #ifdef __i386__
122 #define CAREFUL_ALIGNMENT 0
123 #endif
125 #ifndef CAREFUL_ALIGNMENT
126 #define CAREFUL_ALIGNMENT 1
127 #endif
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)))
147 #else
149 /* this handles things for architectures like the 386 that can handle
150 alignment errors */
152 WARNING: This section is dependent on the length of int16 and int32
153 being correct
156 /* get single value from an SMB buffer */
157 #define SVAL(buf,pos) (*(const uint16 *)((const char *)(buf) + (pos)))
158 #define IVAL(buf,pos) (*(const uint32 *)((const char *)(buf) + (pos)))
159 #define SVALS(buf,pos) (*(const int16 *)((const char *)(buf) + (pos)))
160 #define IVALS(buf,pos) (*(const int32 *)((const char *)(buf) + (pos)))
162 #define SVALMOD(buf,pos) (*(uint16 *)((char *)(buf) + (pos)))
163 #define IVALMOD(buf,pos) (*(uint32 *)((char *)(buf) + (pos)))
164 #define SVALMODS(buf,pos) (*(int16 *)((char *)(buf) + (pos)))
165 #define IVALMODS(buf,pos) (*(int32 *)((char *)(buf) + (pos)))
167 /* store single value in an SMB buffer */
168 #define SSVAL(buf,pos,val) SVALMOD(buf,pos)=((uint16)(val))
169 #define SIVAL(buf,pos,val) IVALMOD(buf,pos)=((uint32)(val))
170 #define SSVALS(buf,pos,val) SVALMODS(buf,pos)=((int16)(val))
171 #define SIVALS(buf,pos,val) IVALMODS(buf,pos)=((int32)(val))
173 #endif
176 /* macros for reading / writing arrays */
178 #define SMBMACRO(macro,buf,pos,val,len,size) \
179 { uint32 l; for (l = 0; l < (uint32)(len); l++) (val)[l] = macro((buf), (pos) + (size)*l); }
181 #define SSMBMACRO(macro,buf,pos,val,len,size) \
182 { uint32 l; for (l = 0; l < (uint32)(len); l++) macro((buf), (pos) + (size)*l, (val)[l]); }
184 /* reads multiple data from an SMB buffer */
185 #define PCVAL(buf,pos,val,len) SMBMACRO(CVAL,buf,pos,val,len,1)
186 #define PSVAL(buf,pos,val,len) SMBMACRO(SVAL,buf,pos,val,len,2)
187 #define PIVAL(buf,pos,val,len) SMBMACRO(IVAL,buf,pos,val,len,4)
188 #define PCVALS(buf,pos,val,len) SMBMACRO(CVALS,buf,pos,val,len,1)
189 #define PSVALS(buf,pos,val,len) SMBMACRO(SVALS,buf,pos,val,len,2)
190 #define PIVALS(buf,pos,val,len) SMBMACRO(IVALS,buf,pos,val,len,4)
192 /* stores multiple data in an SMB buffer */
193 #define PSCVAL(buf,pos,val,len) SSMBMACRO(SCVAL,buf,pos,val,len,1)
194 #define PSSVAL(buf,pos,val,len) SSMBMACRO(SSVAL,buf,pos,val,len,2)
195 #define PSIVAL(buf,pos,val,len) SSMBMACRO(SIVAL,buf,pos,val,len,4)
196 #define PSCVALS(buf,pos,val,len) SSMBMACRO(SCVALS,buf,pos,val,len,1)
197 #define PSSVALS(buf,pos,val,len) SSMBMACRO(SSVALS,buf,pos,val,len,2)
198 #define PSIVALS(buf,pos,val,len) SSMBMACRO(SIVALS,buf,pos,val,len,4)
201 /* now the reverse routines - these are used in nmb packets (mostly) */
202 #define SREV(x) ((((x)&0xFF)<<8) | (((x)>>8)&0xFF))
203 #define IREV(x) ((SREV(x)<<16) | (SREV((x)>>16)))
205 #define RSVAL(buf,pos) SREV(SVAL(buf,pos))
206 #define RIVAL(buf,pos) IREV(IVAL(buf,pos))
207 #define RSSVAL(buf,pos,val) SSVAL(buf,pos,SREV(val))
208 #define RSIVAL(buf,pos,val) SIVAL(buf,pos,IREV(val))
210 #define DBG_RW_PCVAL(charmode,string,depth,base,read,inbuf,outbuf,len) \
211 RW_PCVAL(read,inbuf,outbuf,len) \
212 DEBUG(10,("%s%04x %s: ", \
213 tab_depth(depth), base,string)); \
214 if (charmode) print_asc(10, (unsigned char*)(outbuf), (len)); else \
215 { uint32 idx; for (idx = 0; idx < (uint32)(len); idx++) { DEBUGADD(10,("%02x ", (outbuf)[idx])); } } \
216 DEBUG(10,("\n"));
218 #define DBG_RW_PSVAL(charmode,string,depth,base,read,inbuf,outbuf,len) \
219 RW_PSVAL(read,inbuf,outbuf,len) \
220 DEBUG(10,("%s%04x %s: ", \
221 tab_depth(depth), base,string)); \
222 if (charmode) print_asc(10, (unsigned char*)(outbuf), 2*(len)); else \
223 { uint32 idx; for (idx = 0; idx < (uint32)(len); idx++) { DEBUGADD(10,("%04x ", (outbuf)[idx])); } } \
224 DEBUG(10,("\n"));
226 #define DBG_RW_PIVAL(charmode,string,depth,base,read,inbuf,outbuf,len) \
227 RW_PIVAL(read,inbuf,outbuf,len) \
228 DEBUG(10,("%s%04x %s: ", \
229 tab_depth(depth), base,string)); \
230 if (charmode) print_asc(10, (unsigned char*)(outbuf), 4*(len)); else \
231 { uint32 idx; for (idx = 0; idx < (uint32)(len); idx++) { DEBUGADD(10,("%08x ", (outbuf)[idx])); } } \
232 DEBUG(10,("\n"));
234 #define DBG_RW_CVAL(string,depth,base,read,inbuf,outbuf) \
235 RW_CVAL(read,inbuf,outbuf,0) \
236 DEBUG(10,("%s%04x %s: %02x\n", \
237 tab_depth(depth), base, string, outbuf));
239 #define DBG_RW_SVAL(string,depth,base,read,inbuf,outbuf) \
240 RW_SVAL(read,inbuf,outbuf,0) \
241 DEBUG(10,("%s%04x %s: %04x\n", \
242 tab_depth(depth), base, string, outbuf));
244 #define DBG_RW_IVAL(string,depth,base,read,inbuf,outbuf) \
245 RW_IVAL(read,inbuf,outbuf,0) \
246 DEBUG(10,("%s%04x %s: %08x\n", \
247 tab_depth(depth), base, string, outbuf));