941dee9e88fe647ef64f4f4a0dd9efed69f3ffaa
[jelmer/samba4-debian.git] / source / include / byteorder.h
1 /* 
2    Unix SMB/CIFS implementation.
3    SMB Byte handling
4    Copyright (C) Andrew Tridgell 1992-1998
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #ifndef _BYTEORDER_H
22 #define _BYTEORDER_H
23
24 /*
25    This file implements macros for machine independent short and 
26    int manipulation
27
28 Here is a description of this file that I emailed to the samba list once:
29
30 > I am confused about the way that byteorder.h works in Samba. I have
31 > looked at it, and I would have thought that you might make a distinction
32 > between LE and BE machines, but you only seem to distinguish between 386
33 > and all other architectures.
34
35 > Can you give me a clue?
36
37 sure.
38
39 The distinction between 386 and other architectures is only there as
40 an optimisation. You can take it out completely and it will make no
41 difference. The routines (macros) in byteorder.h are totally byteorder
42 independent. The 386 optimsation just takes advantage of the fact that
43 the x86 processors don't care about alignment, so we don't have to
44 align ints on int boundaries etc. If there are other processors out
45 there that aren't alignment sensitive then you could also define
46 CAREFUL_ALIGNMENT=0 on those processors as well.
47
48 Ok, now to the macros themselves. I'll take a simple example, say we
49 want to extract a 2 byte integer from a SMB packet and put it into a
50 type called uint16_t that is in the local machines byte order, and you
51 want to do it with only the assumption that uint16_t is _at_least_ 16
52 bits long (this last condition is very important for architectures
53 that don't have any int types that are 2 bytes long)
54
55 You do this:
56
57 #define CVAL(buf,pos) (((uint8_t *)(buf))[pos])
58 #define PVAL(buf,pos) ((uint_t)CVAL(buf,pos))
59 #define SVAL(buf,pos) (PVAL(buf,pos)|PVAL(buf,(pos)+1)<<8)
60
61 then to extract a uint16_t value at offset 25 in a buffer you do this:
62
63 char *buffer = foo_bar();
64 uint16_t xx = SVAL(buffer,25);
65
66 We are using the byteoder independence of the ANSI C bitshifts to do
67 the work. A good optimising compiler should turn this into efficient
68 code, especially if it happens to have the right byteorder :-)
69
70 I know these macros can be made a bit tidier by removing some of the
71 casts, but you need to look at byteorder.h as a whole to see the
72 reasoning behind them. byteorder.h defines the following macros:
73
74 SVAL(buf,pos) - extract a 2 byte SMB value
75 IVAL(buf,pos) - extract a 4 byte SMB value
76 SVALS(buf,pos) signed version of SVAL()
77 IVALS(buf,pos) signed version of IVAL()
78
79 SSVAL(buf,pos,val) - put a 2 byte SMB value into a buffer
80 SIVAL(buf,pos,val) - put a 4 byte SMB value into a buffer
81 SSVALS(buf,pos,val) - signed version of SSVAL()
82 SIVALS(buf,pos,val) - signed version of SIVAL()
83
84 RSVAL(buf,pos) - like SVAL() but for NMB byte ordering
85 RSVALS(buf,pos) - like SVALS() but for NMB byte ordering
86 RIVAL(buf,pos) - like IVAL() but for NMB byte ordering
87 RIVALS(buf,pos) - like IVALS() but for NMB byte ordering
88 RSSVAL(buf,pos,val) - like SSVAL() but for NMB ordering
89 RSIVAL(buf,pos,val) - like SIVAL() but for NMB ordering
90 RSIVALS(buf,pos,val) - like SIVALS() but for NMB ordering
91
92 it also defines lots of intermediate macros, just ignore those :-)
93
94 */
95
96
97 /*
98   on powerpc we can use the magic instructions to load/store
99   in little endian
100 */
101 #if (defined(__powerpc__) && defined(__GNUC__))
102 static __inline__ uint16_t ld_le16(const uint16_t *addr)
103 {
104         uint16_t val;
105         __asm__ ("lhbrx %0,0,%1" : "=r" (val) : "r" (addr), "m" (*addr));
106         return val;
107 }
108
109 static __inline__ void st_le16(uint16_t *addr, const uint16_t val)
110 {
111         __asm__ ("sthbrx %1,0,%2" : "=m" (*addr) : "r" (val), "r" (addr));
112 }
113
114 static __inline__ uint32_t ld_le32(const uint32_t *addr)
115 {
116         uint32_t val;
117         __asm__ ("lwbrx %0,0,%1" : "=r" (val) : "r" (addr), "m" (*addr));
118         return val;
119 }
120
121 static __inline__ void st_le32(uint32_t *addr, const uint32_t val)
122 {
123         __asm__ ("stwbrx %1,0,%2" : "=m" (*addr) : "r" (val), "r" (addr));
124 }
125 #define HAVE_ASM_BYTEORDER 1
126 #endif
127
128
129
130 #undef CAREFUL_ALIGNMENT
131
132 /* we know that the 386 can handle misalignment and has the "right" 
133    byteorder */
134 #if defined(__i386__)
135 #define CAREFUL_ALIGNMENT 0
136 #endif
137
138 #ifndef CAREFUL_ALIGNMENT
139 #define CAREFUL_ALIGNMENT 1
140 #endif
141
142 #define CVAL(buf,pos) ((uint_t)(((const uint8_t *)(buf))[pos]))
143 #define CVAL_NC(buf,pos) (((uint8_t *)(buf))[pos]) /* Non-const version of CVAL */
144 #define PVAL(buf,pos) (CVAL(buf,pos))
145 #define SCVAL(buf,pos,val) (CVAL_NC(buf,pos) = (val))
146
147 #if HAVE_ASM_BYTEORDER
148
149 #define  _PTRPOS(buf,pos) (((const uint8_t *)buf)+(pos))
150 #define SVAL(buf,pos) ld_le16((const uint16_t *)_PTRPOS(buf,pos))
151 #define IVAL(buf,pos) ld_le32((const uint32_t *)_PTRPOS(buf,pos))
152 #define SSVAL(buf,pos,val) st_le16((uint16_t *)_PTRPOS(buf,pos), val)
153 #define SIVAL(buf,pos,val) st_le32((uint32_t *)_PTRPOS(buf,pos), val)
154 #define SVALS(buf,pos) ((int16_t)SVAL(buf,pos))
155 #define IVALS(buf,pos) ((int32_t)IVAL(buf,pos))
156 #define SSVALS(buf,pos,val) SSVAL((buf),(pos),((int16_t)(val)))
157 #define SIVALS(buf,pos,val) SIVAL((buf),(pos),((int32_t)(val)))
158
159 #elif CAREFUL_ALIGNMENT
160
161 #define SVAL(buf,pos) (PVAL(buf,pos)|PVAL(buf,(pos)+1)<<8)
162 #define IVAL(buf,pos) (SVAL(buf,pos)|SVAL(buf,(pos)+2)<<16)
163 #define SSVALX(buf,pos,val) (CVAL_NC(buf,pos)=(uint8_t)((val)&0xFF),CVAL_NC(buf,pos+1)=(uint8_t)((val)>>8))
164 #define SIVALX(buf,pos,val) (SSVALX(buf,pos,val&0xFFFF),SSVALX(buf,pos+2,val>>16))
165 #define SVALS(buf,pos) ((int16_t)SVAL(buf,pos))
166 #define IVALS(buf,pos) ((int32_t)IVAL(buf,pos))
167 #define SSVAL(buf,pos,val) SSVALX((buf),(pos),((uint16_t)(val)))
168 #define SIVAL(buf,pos,val) SIVALX((buf),(pos),((uint32_t)(val)))
169 #define SSVALS(buf,pos,val) SSVALX((buf),(pos),((int16_t)(val)))
170 #define SIVALS(buf,pos,val) SIVALX((buf),(pos),((int32_t)(val)))
171
172 #else /* CAREFUL_ALIGNMENT */
173
174 /* this handles things for architectures like the 386 that can handle
175    alignment errors */
176 /*
177    WARNING: This section is dependent on the length of int16_t and int32_t
178    being correct 
179 */
180
181 /* get single value from an SMB buffer */
182 #define SVAL(buf,pos) (*(const uint16_t *)((const char *)(buf) + (pos)))
183 #define SVAL_NC(buf,pos) (*(uint16_t *)((char *)(buf) + (pos))) /* Non const version of above. */
184 #define IVAL(buf,pos) (*(const uint32_t *)((const char *)(buf) + (pos)))
185 #define IVAL_NC(buf,pos) (*(uint32_t *)((char *)(buf) + (pos))) /* Non const version of above. */
186 #define SVALS(buf,pos) (*(const int16_t *)((const char *)(buf) + (pos)))
187 #define SVALS_NC(buf,pos) (*(int16_t *)((char *)(buf) + (pos))) /* Non const version of above. */
188 #define IVALS(buf,pos) (*(const int32_t *)((const char *)(buf) + (pos)))
189 #define IVALS_NC(buf,pos) (*(int32_t *)((char *)(buf) + (pos))) /* Non const version of above. */
190
191 /* store single value in an SMB buffer */
192 #define SSVAL(buf,pos,val) SVAL_NC(buf,pos)=((uint16_t)(val))
193 #define SIVAL(buf,pos,val) IVAL_NC(buf,pos)=((uint32_t)(val))
194 #define SSVALS(buf,pos,val) SVALS_NC(buf,pos)=((int16_t)(val))
195 #define SIVALS(buf,pos,val) IVALS_NC(buf,pos)=((int32_t)(val))
196
197 #endif /* CAREFUL_ALIGNMENT */
198
199 /* now the reverse routines - these are used in nmb packets (mostly) */
200 #define SREV(x) ((((x)&0xFF)<<8) | (((x)>>8)&0xFF))
201 #define IREV(x) ((SREV(x)<<16) | (SREV((x)>>16)))
202
203 #define RSVAL(buf,pos) SREV(SVAL(buf,pos))
204 #define RSVALS(buf,pos) SREV(SVALS(buf,pos))
205 #define RIVAL(buf,pos) IREV(IVAL(buf,pos))
206 #define RIVALS(buf,pos) IREV(IVALS(buf,pos))
207 #define RSSVAL(buf,pos,val) SSVAL(buf,pos,SREV(val))
208 #define RSSVALS(buf,pos,val) SSVALS(buf,pos,SREV(val))
209 #define RSIVAL(buf,pos,val) SIVAL(buf,pos,IREV(val))
210 #define RSIVALS(buf,pos,val) SIVALS(buf,pos,IREV(val))
211
212 /* Alignment macros. */
213 #define ALIGN4(p,base) ((p) + ((4 - (PTR_DIFF((p), (base)) & 3)) & 3))
214 #define ALIGN2(p,base) ((p) + ((2 - (PTR_DIFF((p), (base)) & 1)) & 1))
215
216
217 /* macros for accessing SMB protocol elements */
218 #define VWV(vwv) ((vwv)*2)
219
220 /* 64 bit macros */
221 #define SBVAL(p, ofs, v) (SIVAL(p,ofs,(v)&0xFFFFFFFF), SIVAL(p,(ofs)+4,((uint64_t)(v))>>32))
222 #define BVAL(p, ofs) (IVAL(p,ofs) | (((uint64_t)IVAL(p,(ofs)+4)) << 32))
223
224 #endif /* _BYTEORDER_H */