netatalk 4.4.3
Free and Open Source Apple Filing Protocol (AFP) Server
Loading...
Searching...
No Matches
byteorder.h
Go to the documentation of this file.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19*/
20
21#ifndef _BYTEORDER_H
22#define _BYTEORDER_H
23#include <arpa/inet.h>
24
25/*
26 This file implements macros for machine independent short and
27 int manipulation
28
29 Here is a description of this file that I emailed to the samba list once:
30
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.
35>
36> Can you give me a clue?
37
38 sure.
39
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.
48
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)
55
56 You do this:
57
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)
61
62 then to extract a uint16 value at offset 25 in a buffer you do this:
63
64 char *buffer = foo_bar();
65 uint16 xx = SVAL(buffer,25);
66
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 :-)
70
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:
74
75 SVAL(buf,pos) - extract a 2 byte SMB value
76 IVAL(buf,pos) - extract a 4 byte SMB value
77 LVAL(buf,pos) - extract a 8 byte SMB value
78 SVALS(buf,pos) signed version of SVAL()
79 IVALS(buf,pos) signed version of IVAL()
80
81 SSVAL(buf,pos,val) - put a 2 byte SMB value into a buffer
82 SIVAL(buf,pos,val) - put a 4 byte SMB value into a buffer
83 SSVALS(buf,pos,val) - signed version of SSVAL()
84 SIVALS(buf,pos,val) - signed version of SIVAL()
85
86 RSVAL(buf,pos) - like SVAL() but for NMB byte ordering
87 RSVALS(buf,pos) - like SVALS() but for NMB byte ordering
88 RIVAL(buf,pos) - like IVAL() but for NMB byte ordering
89 RIVALS(buf,pos) - like IVALS() but for NMB byte ordering
90 RSSVAL(buf,pos,val) - like SSVAL() but for NMB ordering
91 RSIVAL(buf,pos,val) - like SIVAL() but for NMB ordering
92 RSIVALS(buf,pos,val) - like SIVALS() but for NMB ordering
93
94 it also defines lots of intermediate macros, just ignore those :-)
95
96*/
97
98#undef CAREFUL_ALIGNMENT
99
100/* we know that the 386 can handle misalignment and has the "right"
101 byteorder */
102#ifdef __i386__
103#define CAREFUL_ALIGNMENT 0
104#endif
105
106#ifndef CAREFUL_ALIGNMENT
107#define CAREFUL_ALIGNMENT 1
108#endif
109
110#define CVAL(buf,pos) ((unsigned)(((const unsigned char *)(buf))[pos]))
111#define CVAL_NC(buf,pos) (((unsigned char *)(buf))[pos])
112#define PVAL(buf,pos) (CVAL(buf,pos))
113#define SCVAL(buf,pos,val) (CVAL_NC(buf,pos) = (val))
114
115
116#if CAREFUL_ALIGNMENT
117
118#ifdef WORDS_BIGENDIAN
119
120#define SVAL(buf,pos) (PVAL(buf,(pos)+1)|PVAL(buf,pos)<<8)
121#define SVALS(buf,pos) ((int16)SVAL(buf,pos))
122#define IVAL(buf,pos) (SVAL(buf,pos)|SVAL(buf,(pos)+2)<<16)
123#define IVALS(buf,pos) ((int32_t)IVAL(buf,pos))
124#define LVAL(buf,pos) (IVAL(buf,pos)|IVAL(buf,(pos)+4)<<32)
125#define LVALS(buf,pos) ((int64_t)LVAL(buf,pos))
126
127#define SSVALX(buf,pos,val) (CVAL_NC(buf,pos+1)=(unsigned char)((val)&0xFF),CVAL_NC(buf,pos)=(unsigned char)((val)>>8))
128#define SIVALX(buf,pos,val) (SSVALX(buf,pos,((val)&0xFFFF)),SSVALX(buf,pos+2,(val)>>16))
129#define SLVALX(buf,pos,val) (SIVALX(buf,pos,((val)&0xFFFFFFFF)),SIVALX(buf,pos+4,(val)>>32))
130
131#define SSVAL(buf,pos,val) SSVALX((buf),(pos),((uint16_t)(val)))
132#define SSVALS(buf,pos,val) SSVALX((buf),(pos),((int16)(val)))
133#define SIVAL(buf,pos,val) SIVALX((buf),(pos),((uint32_t)(val)))
134#define SIVALS(buf,pos,val) SIVALX((buf),(pos),((int32_t)(val)))
135#define SLVAL(buf,pos,val) SLVALX((buf),(pos),((uint64_t)(val)))
136#define SLVALS(buf,pos,val) SLVALX((buf),(pos),((int64_t)(val)))
137
138#else
139
140#define SVAL(buf,pos) (PVAL(buf,pos)|PVAL(buf,(pos)+1)<<8)
141#define SVALS(buf,pos) ((int16)SVAL(buf,pos))
142#define IVAL(buf,pos) (SVAL(buf,pos)|SVAL(buf,(pos)+2)<<16)
143#define IVALS(buf,pos) ((int32_t)IVAL(buf,pos))
144#define LVAL(buf,pos) (IVAL(buf,pos)|((uint64_t)IVAL(buf,(pos)+4))<<32)
145#define LVALS(buf,pos) ((int64_t)LVAL(buf,pos))
146
147#define SSVALX(buf,pos,val) (CVAL_NC(buf,pos)=(unsigned char)((val)&0xFF),CVAL_NC(buf,pos+1)=(unsigned char)((val)>>8))
148#define SIVALX(buf,pos,val) (SSVALX(buf,pos,((val)&0xFFFF)),SSVALX(buf,pos+2,(val)>>16))
149#define SLVALX(buf,pos,val) (SIVALX(buf,pos,((val)&0xFFFFFFFF)),SIVALX(buf,pos+4,(val)>>32))
150
151#define SSVAL(buf,pos,val) SSVALX((buf),(pos),((uint16_t)(val)))
152#define SSVALS(buf,pos,val) SSVALX((buf),(pos),((int16)(val)))
153#define SIVAL(buf,pos,val) SIVALX((buf),(pos),((uint32_t)(val)))
154#define SIVALS(buf,pos,val) SIVALX((buf),(pos),((int32_t)(val)))
155#define SLVAL(buf,pos,val) SLVALX((buf),(pos),((uint64_t)(val)))
156#define SLVALS(buf,pos,val) SLVALX((buf),(pos),((int64_t)(val)))
157
158#endif
159
160#else /* CAREFUL_ALIGNMENT */
161
162/* this handles things for architectures like the 386 that can handle
163 alignment errors */
164/*
165 WARNING: This section is dependent on the length of int16 and int32
166 being correct
167*/
168
169/* get single value from an SMB buffer */
170#define SVAL(buf,pos) (*(const uint16_t *)((const char *)(buf) + (pos)))
171#define SVAL_NC(buf,pos) (*(uint16_t *)((char *)(buf) + (pos)))
172#define IVAL(buf,pos) (*(const uint32_t *)((const char *)(buf) + (pos)))
173#define IVAL_NC(buf,pos) (*(uint32_t *)((char *)(buf) + (pos)))
174#define LVAL(buf,pos) (*(const uint64_t *)((const char *)(buf) + (pos)))
175#define LVAL_NC(buf,pos) (*(uint64_t *)((char *)(buf) + (pos)))
176#define SVALS(buf,pos) (*(const int16_t *)((const char *)(buf) + (pos)))
177#define SVALS_NC(buf,pos) (*(int16 *)((char *)(buf) + (pos)))
178#define IVALS(buf,pos) (*(const int32_t *)((const char *)(buf) + (pos)))
179#define IVALS_NC(buf,pos) (*(int32_t *)((char *)(buf) + (pos)))
180#define LVALS(buf,pos) (*(const int64_t *)((const char *)(buf) + (pos)))
181#define LVALS_NC(buf,pos) (*(int64_t *)((char *)(buf) + (pos)))
182
183/* store single value in an SMB buffer */
184#define SSVAL(buf,pos,val) SVAL_NC(buf,pos)=((uint16_t)(val))
185#define SIVAL(buf,pos,val) IVAL_NC(buf,pos)=((uint32_t)(val))
186#define SLVAL(buf,pos,val) LVAL_NC(buf,pos)=((uint64_t)(val))
187#define SSVALS(buf,pos,val) SVALS_NC(buf,pos)=((int16)(val))
188#define SIVALS(buf,pos,val) IVALS_NC(buf,pos)=((int32_t)(val))
189#define SLVALS(buf,pos,val) LVALS_NC(buf,pos)=((int64_t)(val))
190
191#endif /* CAREFUL_ALIGNMENT */
192
193/* now the reverse routines - these are used in nmb packets (mostly) */
194#define SREV(x) ((((x)&0xFF)<<8) | (((x)>>8)&0xFF))
195#define IREV(x) ((SREV(x)<<16) | (SREV((x)>>16)))
196#define LREV(x) ((IREV(x)<<32) | (IREV((x)>>32)))
197
198#define RSVAL(buf,pos) SREV(SVAL(buf,pos))
199#define RSVALS(buf,pos) SREV(SVALS(buf,pos))
200#define RIVAL(buf,pos) IREV(IVAL(buf,pos))
201#define RIVALS(buf,pos) IREV(IVALS(buf,pos))
202#define RLVAL(buf,pos) LREV(LVAL(buf,pos))
203#define RLVALS(buf,pos) LREV(LVALS(buf,pos))
204
205#define RSSVAL(buf,pos,val) SSVAL(buf,pos,SREV(val))
206#define RSSVALS(buf,pos,val) SSVALS(buf,pos,SREV(val))
207#define RSIVAL(buf,pos,val) SIVAL(buf,pos,IREV(val))
208#define RSIVALS(buf,pos,val) SIVALS(buf,pos,IREV(val))
209
210#define RSLVAL(buf,pos,val) SLVAL(buf,pos,LREV(val))
211#define RSLVALS(buf,pos,val) SLVALS(buf,pos,LREV(val))
212
213/* Alignment macros. */
214#define ALIGN4(p,base) ((p) + ((4 - (PTR_DIFF((p), (base)) & 3)) & 3))
215#define ALIGN2(p,base) ((p) + ((2 - (PTR_DIFF((p), (base)) & 1)) & 1))
216
217#endif /* _BYTEORDER_H */