source: branches/samba-3.3.x/source/include/byteorder.h

Last change on this file was 206, checked in by Herwig Bauernfeind, 16 years ago

Import Samba 3.3 branch at 3.0.0 level (psmedley's port)

File size: 7.0 KB
Line 
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 3 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, see <http://www.gnu.org/licenses/>.
18*/
19
20#ifndef _BYTEORDER_H
21#define _BYTEORDER_H
22
23/*
24 This file implements macros for machine independent short and
25 int manipulation
26
27Here is a description of this file that I emailed to the samba list once:
28
29> I am confused about the way that byteorder.h works in Samba. I have
30> looked at it, and I would have thought that you might make a distinction
31> between LE and BE machines, but you only seem to distinguish between 386
32> and all other architectures.
33>
34> Can you give me a clue?
35
36sure.
37
38The distinction between 386 and other architectures is only there as
39an optimisation. You can take it out completely and it will make no
40difference. The routines (macros) in byteorder.h are totally byteorder
41independent. The 386 optimsation just takes advantage of the fact that
42the x86 processors don't care about alignment, so we don't have to
43align ints on int boundaries etc. If there are other processors out
44there that aren't alignment sensitive then you could also define
45CAREFUL_ALIGNMENT=0 on those processors as well.
46
47Ok, now to the macros themselves. I'll take a simple example, say we
48want to extract a 2 byte integer from a SMB packet and put it into a
49type called uint16 that is in the local machines byte order, and you
50want to do it with only the assumption that uint16 is _at_least_ 16
51bits long (this last condition is very important for architectures
52that don't have any int types that are 2 bytes long)
53
54You do this:
55
56#define CVAL(buf,pos) (((unsigned char *)(buf))[pos])
57#define PVAL(buf,pos) ((unsigned)CVAL(buf,pos))
58#define SVAL(buf,pos) (PVAL(buf,pos)|PVAL(buf,(pos)+1)<<8)
59
60then to extract a uint16 value at offset 25 in a buffer you do this:
61
62char *buffer = foo_bar();
63uint16 xx = SVAL(buffer,25);
64
65We are using the byteoder independence of the ANSI C bitshifts to do
66the work. A good optimising compiler should turn this into efficient
67code, especially if it happens to have the right byteorder :-)
68
69I know these macros can be made a bit tidier by removing some of the
70casts, but you need to look at byteorder.h as a whole to see the
71reasoning behind them. byteorder.h defines the following macros:
72
73SVAL(buf,pos) - extract a 2 byte SMB value
74IVAL(buf,pos) - extract a 4 byte SMB value
75SVALS(buf,pos) signed version of SVAL()
76IVALS(buf,pos) signed version of IVAL()
77
78SSVAL(buf,pos,val) - put a 2 byte SMB value into a buffer
79SIVAL(buf,pos,val) - put a 4 byte SMB value into a buffer
80SSVALS(buf,pos,val) - signed version of SSVAL()
81SIVALS(buf,pos,val) - signed version of SIVAL()
82
83RSVAL(buf,pos) - like SVAL() but for NMB byte ordering
84RSVALS(buf,pos) - like SVALS() but for NMB byte ordering
85RIVAL(buf,pos) - like IVAL() but for NMB byte ordering
86RIVALS(buf,pos) - like IVALS() but for NMB byte ordering
87RSSVAL(buf,pos,val) - like SSVAL() but for NMB ordering
88RSIVAL(buf,pos,val) - like SIVAL() but for NMB ordering
89RSIVALS(buf,pos,val) - like SIVALS() but for NMB ordering
90
91it also defines lots of intermediate macros, just ignore those :-)
92
93*/
94
95#undef CAREFUL_ALIGNMENT
96
97/* we know that the 386 can handle misalignment and has the "right"
98 byteorder */
99#ifdef __i386__
100#define CAREFUL_ALIGNMENT 0
101#endif
102
103#ifndef CAREFUL_ALIGNMENT
104#define CAREFUL_ALIGNMENT 1
105#endif
106
107#define CVAL(buf,pos) ((unsigned)(((const unsigned char *)(buf))[pos]))
108#define CVAL_NC(buf,pos) (((unsigned char *)(buf))[pos]) /* Non-const version of CVAL */
109#define PVAL(buf,pos) (CVAL(buf,pos))
110#define SCVAL(buf,pos,val) (CVAL_NC(buf,pos) = (val))
111
112
113#if CAREFUL_ALIGNMENT
114
115#define SVAL(buf,pos) (PVAL(buf,pos)|PVAL(buf,(pos)+1)<<8)
116#define IVAL(buf,pos) (SVAL(buf,pos)|SVAL(buf,(pos)+2)<<16)
117#define SSVALX(buf,pos,val) (CVAL_NC(buf,pos)=(unsigned char)((val)&0xFF),CVAL_NC(buf,pos+1)=(unsigned char)((val)>>8))
118#define SIVALX(buf,pos,val) (SSVALX(buf,pos,val&0xFFFF),SSVALX(buf,pos+2,val>>16))
119#define SVALS(buf,pos) ((int16)SVAL(buf,pos))
120#define IVALS(buf,pos) ((int32)IVAL(buf,pos))
121#define SSVAL(buf,pos,val) SSVALX((buf),(pos),((uint16)(val)))
122#define SIVAL(buf,pos,val) SIVALX((buf),(pos),((uint32)(val)))
123#define SSVALS(buf,pos,val) SSVALX((buf),(pos),((int16)(val)))
124#define SIVALS(buf,pos,val) SIVALX((buf),(pos),((int32)(val)))
125
126#else /* CAREFUL_ALIGNMENT */
127
128/* this handles things for architectures like the 386 that can handle
129 alignment errors */
130/*
131 WARNING: This section is dependent on the length of int16 and int32
132 being correct
133*/
134
135/* get single value from an SMB buffer */
136#define SVAL(buf,pos) (*(const uint16 *)((const char *)(buf) + (pos)))
137#define SVAL_NC(buf,pos) (*(uint16 *)((char *)(buf) + (pos))) /* Non const version of above. */
138#define IVAL(buf,pos) (*(const uint32 *)((const char *)(buf) + (pos)))
139#define IVAL_NC(buf,pos) (*(uint32 *)((char *)(buf) + (pos))) /* Non const version of above. */
140#define SVALS(buf,pos) (*(const int16 *)((const char *)(buf) + (pos)))
141#define SVALS_NC(buf,pos) (*(int16 *)((char *)(buf) + (pos))) /* Non const version of above. */
142#define IVALS(buf,pos) (*(const int32 *)((const char *)(buf) + (pos)))
143#define IVALS_NC(buf,pos) (*(int32 *)((char *)(buf) + (pos))) /* Non const version of above. */
144
145/* store single value in an SMB buffer */
146#define SSVAL(buf,pos,val) SVAL_NC(buf,pos)=((uint16)(val))
147#define SIVAL(buf,pos,val) IVAL_NC(buf,pos)=((uint32)(val))
148#define SSVALS(buf,pos,val) SVALS_NC(buf,pos)=((int16)(val))
149#define SIVALS(buf,pos,val) IVALS_NC(buf,pos)=((int32)(val))
150
151#endif /* CAREFUL_ALIGNMENT */
152
153/* now the reverse routines - these are used in nmb packets (mostly) */
154#define SREV(x) ((((x)&0xFF)<<8) | (((x)>>8)&0xFF))
155#define IREV(x) ((SREV(x)<<16) | (SREV((x)>>16)))
156
157#define RSVAL(buf,pos) SREV(SVAL(buf,pos))
158#define RSVALS(buf,pos) SREV(SVALS(buf,pos))
159#define RIVAL(buf,pos) IREV(IVAL(buf,pos))
160#define RIVALS(buf,pos) IREV(IVALS(buf,pos))
161#define RSSVAL(buf,pos,val) SSVAL(buf,pos,SREV(val))
162#define RSSVALS(buf,pos,val) SSVALS(buf,pos,SREV(val))
163#define RSIVAL(buf,pos,val) SIVAL(buf,pos,IREV(val))
164#define RSIVALS(buf,pos,val) SIVALS(buf,pos,IREV(val))
165
166/* Alignment macros. */
167#define ALIGN4(p,base) ((p) + ((4 - (PTR_DIFF((p), (base)) & 3)) & 3))
168#define ALIGN2(p,base) ((p) + ((2 - (PTR_DIFF((p), (base)) & 1)) & 1))
169
170/* 64 bit macros */
171#define BVAL(p, ofs) (IVAL(p,ofs) | (((uint64_t)IVAL(p,(ofs)+4)) << 32))
172#define BVALS(p, ofs) ((int64_t)BVAL(p,ofs))
173#define SBVAL(p, ofs, v) (SIVAL(p,ofs,(v)&0xFFFFFFFF), SIVAL(p,(ofs)+4,((uint64_t)(v))>>32))
174#define SBVALS(p, ofs, v) (SBVAL(p,ofs,(uint64_t)v))
175
176#endif /* _BYTEORDER_H */
Note: See TracBrowser for help on using the repository browser.