getting somewhere.
[kai/samba.git] / source3 / include / byteorder.h
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    SMB Byte handling
5    Copyright (C) Andrew Tridgell 1992-1997
6    
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.
11    
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.
16    
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.
20 */
21
22 /*
23    This file implements macros for machine independent short and 
24    int manipulation
25
26 Here is a description of this file that I emailed to the samba list once:
27
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.
32
33 > Can you give me a clue?
34
35 sure.
36
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.
45
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)
52
53 You do this:
54
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)
58
59 then to extract a uint16 value at offset 25 in a buffer you do this:
60
61 char *buffer = foo_bar();
62 uint16 xx = SVAL(buffer,25);
63
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 :-)
67
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:
71
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()
76
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()
81
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
86
87 it also defines lots of intermediate macros, just ignore those :-)
88
89 */
90
91 /* some switch macros that do both store and read to and from SMB buffers */
92
93 #define RW_PCVAL(read,inbuf,outbuf,len) \
94                 if (read) { PCVAL (inbuf,0,outbuf,len) } \
95                 else      { PSCVAL(inbuf,0,outbuf,len) }
96
97 #define RW_PSVAL(read,inbuf,outbuf,len) \
98                 if (read) { PSVAL (inbuf,0,outbuf,len) } \
99                 else      { PSSVAL(inbuf,0,outbuf,len) }
100
101 #define RW_CVAL(read, inbuf, outbuf, offset) \
102                 if (read) (outbuf) = CVAL (inbuf,offset); \
103                 else                 SCVAL(inbuf,offset,outbuf);
104
105 #define RW_IVAL(read, inbuf, outbuf, offset) \
106                 if (read) (outbuf)= IVAL (inbuf,offset); \
107                 else                SIVAL(inbuf,offset,outbuf);
108
109 #define RW_SVAL(read, inbuf, outbuf, offset) \
110                 if (read) (outbuf)= SVAL (inbuf,offset); \
111                 else                SSVAL(inbuf,offset,outbuf);
112
113 #undef CAREFUL_ALIGNMENT
114
115 /* we know that the 386 can handle misalignment and has the "right" 
116    byteorder */
117 #ifdef __i386__
118 #define CAREFUL_ALIGNMENT 0
119 #endif
120
121 #ifndef CAREFUL_ALIGNMENT
122 #define CAREFUL_ALIGNMENT 1
123 #endif
124
125 #define CVAL(buf,pos) (((unsigned char *)(buf))[pos])
126 #define PVAL(buf,pos) ((unsigned)CVAL(buf,pos))
127 #define SCVAL(buf,pos,val) (CVAL(buf,pos) = (val))
128
129
130 #if CAREFUL_ALIGNMENT
131
132 #define SVAL(buf,pos) (PVAL(buf,pos)|PVAL(buf,(pos)+1)<<8)
133 #define IVAL(buf,pos) (SVAL(buf,pos)|SVAL(buf,(pos)+2)<<16)
134 #define SSVALX(buf,pos,val) (CVAL(buf,pos)=(val)&0xFF,CVAL(buf,pos+1)=(val)>>8)
135 #define SIVALX(buf,pos,val) (SSVALX(buf,pos,val&0xFFFF),SSVALX(buf,pos+2,val>>16))
136 #define SVALS(buf,pos) ((int16)SVAL(buf,pos))
137 #define IVALS(buf,pos) ((int32)IVAL(buf,pos))
138 #define SSVAL(buf,pos,val) SSVALX((buf),(pos),((uint16)(val)))
139 #define SIVAL(buf,pos,val) SIVALX((buf),(pos),((uint32)(val)))
140 #define SSVALS(buf,pos,val) SSVALX((buf),(pos),((int16)(val)))
141 #define SIVALS(buf,pos,val) SIVALX((buf),(pos),((int32)(val)))
142
143 #else
144
145 /* this handles things for architectures like the 386 that can handle
146    alignment errors */
147 /*
148    WARNING: This section is dependent on the length of int16 and int32
149    being correct 
150 */
151
152 /* get single value from an SMB buffer */
153 #define SVAL(buf,pos) (*(uint16 *)((char *)(buf) + (pos)))
154 #define IVAL(buf,pos) (*(uint32 *)((char *)(buf) + (pos)))
155 #define SVALS(buf,pos) (*(int16 *)((char *)(buf) + (pos)))
156 #define IVALS(buf,pos) (*(int32 *)((char *)(buf) + (pos)))
157
158 /* store single value in an SMB buffer */
159 #define SSVAL(buf,pos,val) SVAL(buf,pos)=((uint16)(val))
160 #define SIVAL(buf,pos,val) IVAL(buf,pos)=((uint32)(val))
161 #define SSVALS(buf,pos,val) SVALS(buf,pos)=((int16)(val))
162 #define SIVALS(buf,pos,val) IVALS(buf,pos)=((int32)(val))
163
164 #endif
165
166
167 /* macros for reading / writing arrays */
168
169 #define SMBMACRO(macro,buf,pos,val,len,size) \
170 { int l; for (l = 0; l < (len); l++) (val)[l] = macro((buf), (pos) + (size)*l); }
171
172 #define SSMBMACRO(macro,buf,pos,val,len,size) \
173 { int l; for (l = 0; l < (len); l++) macro((buf), (pos) + (size)*l, (val)[l]); }
174
175 /* reads multiple data from an SMB buffer */
176 #define PCVAL(buf,pos,val,len) SMBMACRO(CVAL,buf,pos,val,len,1)
177 #define PSVAL(buf,pos,val,len) SMBMACRO(SVAL,buf,pos,val,len,2)
178 #define PIVAL(buf,pos,val,len) SMBMACRO(IVAL,buf,pos,val,len,4)
179 #define PCVALS(buf,pos,val,len) SMBMACRO(CVALS,buf,pos,val,len,1)
180 #define PSVALS(buf,pos,val,len) SMBMACRO(SVALS,buf,pos,val,len,2)
181 #define PIVALS(buf,pos,val,len) SMBMACRO(IVALS,buf,pos,val,len,4)
182
183 /* stores multiple data in an SMB buffer */
184 #define PSCVAL(buf,pos,val,len) SSMBMACRO(SCVAL,buf,pos,val,len,1)
185 #define PSSVAL(buf,pos,val,len) SSMBMACRO(SSVAL,buf,pos,val,len,2)
186 #define PSIVAL(buf,pos,val,len) SSMBMACRO(SIVAL,buf,pos,val,len,4)
187 #define PSCVALS(buf,pos,val,len) SSMBMACRO(SCVALS,buf,pos,val,len,1)
188 #define PSSVALS(buf,pos,val,len) SSMBMACRO(SSVALS,buf,pos,val,len,2)
189 #define PSIVALS(buf,pos,val,len) SSMBMACRO(SIVALS,buf,pos,val,len,4)
190
191
192 /* now the reverse routines - these are used in nmb packets (mostly) */
193 #define SREV(x) ((((x)&0xFF)<<8) | (((x)>>8)&0xFF))
194 #define IREV(x) ((SREV(x)<<16) | (SREV((x)>>16)))
195
196 #define RSVAL(buf,pos) SREV(SVAL(buf,pos))
197 #define RIVAL(buf,pos) IREV(IVAL(buf,pos))
198 #define RSSVAL(buf,pos,val) SSVAL(buf,pos,SREV(val))
199 #define RSIVAL(buf,pos,val) SIVAL(buf,pos,IREV(val))
200
201 #define DBG_RW_PCVAL(string,depth,base,read,inbuf,outbuf,len) \
202         RW_PCVAL(read,inbuf,outbuf,len) \
203         DEBUG(5,("%s %04x %s: ", \
204              tab_depth(depth), PTR_DIFF(inbuf,base),string)); \
205         { int idx; for (idx = 0; idx < len; idx++) { DEBUG(5,("%d ", (inbuf)[idx])); } } \
206         DEBUG(5,("\n"));
207
208 #define DBG_RW_PSVAL(string,depth,base,read,inbuf,outbuf,len) \
209         RW_PSVAL(read,inbuf,outbuf,len) \
210         DEBUG(5,("%s %04x %s: ", \
211              tab_depth(depth), PTR_DIFF(inbuf,base),string)); \
212         { int idx; for (idx = 0; idx < len; idx++) { DEBUG(5,("%d ", (inbuf)[idx])); } } \
213         DEBUG(5,("\n"));
214
215 #define DBG_RW_CVAL(string,depth,base,read,inbuf,outbuf) \
216         RW_CVAL(read,inbuf,outbuf,0) \
217         DEBUG(5,("%s %04x %s: %02x\n", \
218              tab_depth(depth), PTR_DIFF(inbuf,base),string, *(inbuf)));
219
220 #define DBG_RW_SVAL(string,depth,base,read,inbuf,outbuf) \
221         RW_SVAL(read,inbuf,outbuf,0) \
222         DEBUG(5,("%s %04x %s: %04x\n", \
223              tab_depth(depth), PTR_DIFF(inbuf,base),string, *(inbuf)));
224
225 #define DBG_RW_IVAL(string,depth,base,read,inbuf,outbuf) \
226         RW_IVAL(read,inbuf,outbuf,0) \
227         DEBUG(5,("%s %04x %s: %08x\n", \
228              tab_depth(depth), PTR_DIFF(inbuf,base),string, *(inbuf)));
229