Code indentation.
[midnight-commander.git] / src / vfs / smbfs / helpers / include / byteorder.h
blobfd97ccb829b842a87b69a68c4034c685e1d1cd5e
1 /*
2 Unix SMB/Netbios implementation.
3 Version 1.9.
4 SMB Byte handling
5 */
7 #ifndef _BYTEORDER_H
8 #define _BYTEORDER_H
11 This file implements macros for machine independent short and
12 int manipulation
14 Here is a description of this file that I emailed to the samba list once:
16 > I am confused about the way that byteorder.h works in Samba. I have
17 > looked at it, and I would have thought that you might make a distinction
18 > between LE and BE machines, but you only seem to distinguish between 386
19 > and all other architectures.
21 > Can you give me a clue?
23 sure.
25 The distinction between 386 and other architectures is only there as
26 an optimisation. You can take it out completely and it will make no
27 difference. The routines (macros) in byteorder.h are totally byteorder
28 independent. The 386 optimsation just takes advantage of the fact that
29 the x86 processors don't care about alignment, so we don't have to
30 align ints on int boundaries etc. If there are other processors out
31 there that aren't alignment sensitive then you could also define
32 CAREFUL_ALIGNMENT=0 on those processors as well.
34 Ok, now to the macros themselves. I'll take a simple example, say we
35 want to extract a 2 byte integer from a SMB packet and put it into a
36 type called uint16 that is in the local machines byte order, and you
37 want to do it with only the assumption that uint16 is _at_least_ 16
38 bits long (this last condition is very important for architectures
39 that don't have any int types that are 2 bytes long)
41 You do this:
43 #define CVAL(buf,pos) (((unsigned char *)(buf))[pos])
44 #define PVAL(buf,pos) ((unsigned)CVAL(buf,pos))
45 #define SVAL(buf,pos) (PVAL(buf,pos)|PVAL(buf,(pos)+1)<<8)
47 then to extract a uint16 value at offset 25 in a buffer you do this:
49 char *buffer = foo_bar();
50 uint16 xx = SVAL(buffer,25);
52 We are using the byteoder independence of the ANSI C bitshifts to do
53 the work. A good optimising compiler should turn this into efficient
54 code, especially if it happens to have the right byteorder :-)
56 I know these macros can be made a bit tidier by removing some of the
57 casts, but you need to look at byteorder.h as a whole to see the
58 reasoning behind them. byteorder.h defines the following macros:
60 SVAL(buf,pos) - extract a 2 byte SMB value
61 IVAL(buf,pos) - extract a 4 byte SMB value
62 SVALS(buf,pos) signed version of SVAL()
63 IVALS(buf,pos) signed version of IVAL()
65 SSVAL(buf,pos,val) - put a 2 byte SMB value into a buffer
66 SIVAL(buf,pos,val) - put a 4 byte SMB value into a buffer
67 SSVALS(buf,pos,val) - signed version of SSVAL()
68 SIVALS(buf,pos,val) - signed version of SIVAL()
70 RSVAL(buf,pos) - like SVAL() but for NMB byte ordering
71 RSVALS(buf,pos) - like SVALS() but for NMB byte ordering
72 RIVAL(buf,pos) - like IVAL() but for NMB byte ordering
73 RIVALS(buf,pos) - like IVALS() but for NMB byte ordering
74 RSSVAL(buf,pos,val) - like SSVAL() but for NMB ordering
75 RSIVAL(buf,pos,val) - like SIVAL() but for NMB ordering
76 RSIVALS(buf,pos,val) - like SIVALS() but for NMB ordering
78 it also defines lots of intermediate macros, just ignore those :-)
82 /* some switch macros that do both store and read to and from SMB buffers */
84 #define RW_PCVAL(read,inbuf,outbuf,len) \
85 { if (read) { PCVAL (inbuf,0,outbuf,len); } \
86 else { PSCVAL(inbuf,0,outbuf,len); } }
88 #define RW_PIVAL(read,big_endian,inbuf,outbuf,len) \
89 { if (read) { if (big_endian) { RPIVAL(inbuf,0,outbuf,len); } else { PIVAL(inbuf,0,outbuf,len); } } \
90 else { if (big_endian) { RPSIVAL(inbuf,0,outbuf,len); } else { PSIVAL(inbuf,0,outbuf,len); } } }
92 #define RW_PSVAL(read,big_endian,inbuf,outbuf,len) \
93 { if (read) { if (big_endian) { RPSVAL(inbuf,0,outbuf,len); } else { PSVAL(inbuf,0,outbuf,len); } } \
94 else { if (big_endian) { RPSSVAL(inbuf,0,outbuf,len); } else { PSSVAL(inbuf,0,outbuf,len); } } }
96 #define RW_CVAL(read, inbuf, outbuf, offset) \
97 { if (read) { (outbuf) = CVAL (inbuf,offset); } \
98 else { SCVAL(inbuf,offset,outbuf); } }
100 #define RW_IVAL(read, big_endian, inbuf, outbuf, offset) \
101 { if (read) { (outbuf) = ((big_endian) ? RIVAL(inbuf,offset) : IVAL (inbuf,offset)); } \
102 else { if (big_endian) { RSIVAL(inbuf,offset,outbuf); } else { SIVAL(inbuf,offset,outbuf); } } }
104 #define RW_SVAL(read, big_endian, inbuf, outbuf, offset) \
105 { if (read) { (outbuf) = ((big_endian) ? RSVAL(inbuf,offset) : SVAL (inbuf,offset)); } \
106 else { if (big_endian) { RSSVAL(inbuf,offset,outbuf); } else { SSVAL(inbuf,offset,outbuf); } } }
108 #undef CAREFUL_ALIGNMENT
110 /* we know that the 386 can handle misalignment and has the "right"
111 byteorder */
112 #ifdef __i386__
113 #define CAREFUL_ALIGNMENT 0
114 #endif
116 #ifndef CAREFUL_ALIGNMENT
117 #define CAREFUL_ALIGNMENT 1
118 #endif
120 #define CVAL(buf,pos) (((unsigned char *)(buf))[pos])
121 #define PVAL(buf,pos) ((unsigned)CVAL(buf,pos))
122 #define SCVAL(buf,pos,val) (CVAL(buf,pos) = (val))
125 #if CAREFUL_ALIGNMENT
127 #define SVAL(buf,pos) (PVAL(buf,pos)|PVAL(buf,(pos)+1)<<8)
128 #define IVAL(buf,pos) (SVAL(buf,pos)|SVAL(buf,(pos)+2)<<16)
129 #define SSVALX(buf,pos,val) (CVAL(buf,pos)=(val)&0xFF,CVAL(buf,pos+1)=(val)>>8)
130 #define SIVALX(buf,pos,val) (SSVALX(buf,pos,val&0xFFFF),SSVALX(buf,pos+2,val>>16))
131 #define SVALS(buf,pos) ((int16)SVAL(buf,pos))
132 #define IVALS(buf,pos) ((int32)IVAL(buf,pos))
133 #define SSVAL(buf,pos,val) SSVALX((buf),(pos),((uint16)(val)))
134 #define SIVAL(buf,pos,val) SIVALX((buf),(pos),((uint32)(val)))
135 #define SSVALS(buf,pos,val) SSVALX((buf),(pos),((int16)(val)))
136 #define SIVALS(buf,pos,val) SIVALX((buf),(pos),((int32)(val)))
138 #else /* CAREFUL_ALIGNMENT */
140 /* this handles things for architectures like the 386 that can handle
141 alignment errors */
143 WARNING: This section is dependent on the length of int16 and int32
144 being correct
147 /* get single value from an SMB buffer */
148 #define SVAL(buf,pos) (*(uint16 *)((char *)(buf) + (pos)))
149 #define IVAL(buf,pos) (*(uint32 *)((char *)(buf) + (pos)))
150 #define SVALS(buf,pos) (*(int16 *)((char *)(buf) + (pos)))
151 #define IVALS(buf,pos) (*(int32 *)((char *)(buf) + (pos)))
153 /* store single value in an SMB buffer */
154 #define SSVAL(buf,pos,val) SVAL(buf,pos)=((uint16)(val))
155 #define SIVAL(buf,pos,val) IVAL(buf,pos)=((uint32)(val))
156 #define SSVALS(buf,pos,val) SVALS(buf,pos)=((int16)(val))
157 #define SIVALS(buf,pos,val) IVALS(buf,pos)=((int32)(val))
159 #endif /* CAREFUL_ALIGNMENT */
161 /* macros for reading / writing arrays */
163 #define SMBMACRO(macro,buf,pos,val,len,size) \
164 { int l; for (l = 0; l < (len); l++) (val)[l] = macro((buf), (pos) + (size)*l); }
166 #define SSMBMACRO(macro,buf,pos,val,len,size) \
167 { int l; for (l = 0; l < (len); l++) macro((buf), (pos) + (size)*l, (val)[l]); }
169 /* reads multiple data from an SMB buffer */
170 #define PCVAL(buf,pos,val,len) SMBMACRO(CVAL,buf,pos,val,len,1)
171 #define PSVAL(buf,pos,val,len) SMBMACRO(SVAL,buf,pos,val,len,2)
172 #define PIVAL(buf,pos,val,len) SMBMACRO(IVAL,buf,pos,val,len,4)
173 #define PCVALS(buf,pos,val,len) SMBMACRO(CVALS,buf,pos,val,len,1)
174 #define PSVALS(buf,pos,val,len) SMBMACRO(SVALS,buf,pos,val,len,2)
175 #define PIVALS(buf,pos,val,len) SMBMACRO(IVALS,buf,pos,val,len,4)
177 /* stores multiple data in an SMB buffer */
178 #define PSCVAL(buf,pos,val,len) SSMBMACRO(SCVAL,buf,pos,val,len,1)
179 #define PSSVAL(buf,pos,val,len) SSMBMACRO(SSVAL,buf,pos,val,len,2)
180 #define PSIVAL(buf,pos,val,len) SSMBMACRO(SIVAL,buf,pos,val,len,4)
181 #define PSCVALS(buf,pos,val,len) SSMBMACRO(SCVALS,buf,pos,val,len,1)
182 #define PSSVALS(buf,pos,val,len) SSMBMACRO(SSVALS,buf,pos,val,len,2)
183 #define PSIVALS(buf,pos,val,len) SSMBMACRO(SIVALS,buf,pos,val,len,4)
186 /* now the reverse routines - these are used in nmb packets (mostly) */
187 #define SREV(x) ((((x)&0xFF)<<8) | (((x)>>8)&0xFF))
188 #define IREV(x) ((SREV(x)<<16) | (SREV((x)>>16)))
190 #define RSVAL(buf,pos) SREV(SVAL(buf,pos))
191 #define RSVALS(buf,pos) SREV(SVALS(buf,pos))
192 #define RIVAL(buf,pos) IREV(IVAL(buf,pos))
193 #define RIVALS(buf,pos) IREV(IVALS(buf,pos))
194 #define RSSVAL(buf,pos,val) SSVAL(buf,pos,SREV(val))
195 #define RSSVALS(buf,pos,val) SSVALS(buf,pos,SREV(val))
196 #define RSIVAL(buf,pos,val) SIVAL(buf,pos,IREV(val))
197 #define RSIVALS(buf,pos,val) SIVALS(buf,pos,IREV(val))
199 /* reads multiple data from an SMB buffer (big-endian) */
200 #define RPSVAL(buf,pos,val,len) SMBMACRO(RSVAL,buf,pos,val,len,2)
201 #define RPIVAL(buf,pos,val,len) SMBMACRO(RIVAL,buf,pos,val,len,4)
202 #define RPSVALS(buf,pos,val,len) SMBMACRO(RSVALS,buf,pos,val,len,2)
203 #define RPIVALS(buf,pos,val,len) SMBMACRO(RIVALS,buf,pos,val,len,4)
205 /* stores multiple data in an SMB buffer (big-endian) */
206 #define RPSSVAL(buf,pos,val,len) SSMBMACRO(RSSVAL,buf,pos,val,len,2)
207 #define RPSIVAL(buf,pos,val,len) SSMBMACRO(RSIVAL,buf,pos,val,len,4)
208 #define RPSSVALS(buf,pos,val,len) SSMBMACRO(RSSVALS,buf,pos,val,len,2)
209 #define RPSIVALS(buf,pos,val,len) SSMBMACRO(RSIVALS,buf,pos,val,len,4)
211 #endif /* _BYTEORDER_H */