source: releases/0.12.0/include/byteorder.h @ 191

Last change on this file since 191 was 168, checked in by tim, 15 years ago

merged remaining smb_deps items into regfi

began formatting API comments for use with doxygen

  • Property svn:keywords set to Id
File size: 7.0 KB
Line 
1/*
2 * Branched from Samba project Subversion repository, version #2:
3 *  http://websvn.samba.org/cgi-bin/viewcvs.cgi/trunk/source/include/byteorder.h
4 *
5 * Unix SMB/CIFS implementation.
6 * SMB Byte handling
7 *
8 * Copyright (C) 2005 Timothy D. Morgan
9 * Copyright (C) 1992-1998 Andrew Tridgell
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; version 3 of the License.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 *
24 * $Id: byteorder.h 168 2010-03-03 00:08:42Z tim $
25 */
26
27#ifndef _BYTEORDER_H
28#define _BYTEORDER_H
29
30/**
31 * @file
32 *
33 * This file implements macros for machine independent short and
34 * int manipulation
35
36@verbatim
37
38Here is a description of this file that I emailed to the samba list once:
39
40> I am confused about the way that byteorder.h works in Samba. I have
41> looked at it, and I would have thought that you might make a distinction
42> between LE and BE machines, but you only seem to distinguish between 386
43> and all other architectures.
44>
45> Can you give me a clue?
46
47sure.
48
49The distinction between 386 and other architectures is only there as
50an optimisation. You can take it out completely and it will make no
51difference. The routines (macros) in byteorder.h are totally byteorder
52independent. The 386 optimsation just takes advantage of the fact that
53the x86 processors don't care about alignment, so we don't have to
54align ints on int boundaries etc. If there are other processors out
55there that aren't alignment sensitive then you could also define
56CAREFUL_ALIGNMENT=0 on those processors as well.
57
58Ok, now to the macros themselves. I'll take a simple example, say we
59want to extract a 2 byte integer from a SMB packet and put it into a
60type called uint16_t that is in the local machines byte order, and you
61want to do it with only the assumption that uint16_t is _at_least_ 16
62bits long (this last condition is very important for architectures
63that don't have any int types that are 2 bytes long)
64
65You do this:
66
67#define CVAL(buf,pos) (((unsigned char *)(buf))[pos])
68#define PVAL(buf,pos) ((unsigned)CVAL(buf,pos))
69#define SVAL(buf,pos) (PVAL(buf,pos)|PVAL(buf,(pos)+1)<<8)
70
71then to extract a uint16_t value at offset 25 in a buffer you do this:
72
73char *buffer = foo_bar();
74uint16_t xx = SVAL(buffer,25);
75
76We are using the byteoder independence of the ANSI C bitshifts to do
77the work. A good optimising compiler should turn this into efficient
78code, especially if it happens to have the right byteorder :-)
79
80I know these macros can be made a bit tidier by removing some of the
81casts, but you need to look at byteorder.h as a whole to see the
82reasoning behind them. byteorder.h defines the following macros:
83
84SVAL(buf,pos) - extract a 2 byte SMB value
85IVAL(buf,pos) - extract a 4 byte SMB value
86SVALS(buf,pos) signed version of SVAL()
87IVALS(buf,pos) signed version of IVAL()
88
89SSVAL(buf,pos,val) - put a 2 byte SMB value into a buffer
90SIVAL(buf,pos,val) - put a 4 byte SMB value into a buffer
91SSVALS(buf,pos,val) - signed version of SSVAL()
92SIVALS(buf,pos,val) - signed version of SIVAL()
93
94RSVAL(buf,pos) - like SVAL() but for NMB byte ordering
95RSVALS(buf,pos) - like SVALS() but for NMB byte ordering
96RIVAL(buf,pos) - like IVAL() but for NMB byte ordering
97RIVALS(buf,pos) - like IVALS() but for NMB byte ordering
98RSSVAL(buf,pos,val) - like SSVAL() but for NMB ordering
99RSIVAL(buf,pos,val) - like SIVAL() but for NMB ordering
100RSIVALS(buf,pos,val) - like SIVALS() but for NMB ordering
101
102it also defines lots of intermediate macros, just ignore those :-)
103
104@endverbatim
105
106*/
107
108#undef CAREFUL_ALIGNMENT
109
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
115
116#ifndef CAREFUL_ALIGNMENT
117#define CAREFUL_ALIGNMENT 1
118#endif
119
120#define CVAL(buf,pos) ((unsigned)(((const unsigned char *)(buf))[pos]))
121#define CVAL_NC(buf,pos) (((unsigned char *)(buf))[pos]) /* Non-const version of CVAL */
122#define PVAL(buf,pos) (CVAL(buf,pos))
123#define SCVAL(buf,pos,val) (CVAL_NC(buf,pos) = (val))
124
125
126#if CAREFUL_ALIGNMENT
127
128#define SVAL(buf,pos) (PVAL(buf,pos)|PVAL(buf,(pos)+1)<<8)
129#define IVAL(buf,pos) (SVAL(buf,pos)|SVAL(buf,(pos)+2)<<16)
130#define SSVALX(buf,pos,val) (CVAL_NC(buf,pos)=(unsigned char)((val)&0xFF),CVAL_NC(buf,pos+1)=(unsigned char)((val)>>8))
131#define SIVALX(buf,pos,val) (SSVALX(buf,pos,val&0xFFFF),SSVALX(buf,pos+2,val>>16))
132#define SVALS(buf,pos) ((int16_t)SVAL(buf,pos))
133#define IVALS(buf,pos) ((int32_t)IVAL(buf,pos))
134#define SSVAL(buf,pos,val) SSVALX((buf),(pos),((uint16_t)(val)))
135#define SIVAL(buf,pos,val) SIVALX((buf),(pos),((uint32_t)(val)))
136#define SSVALS(buf,pos,val) SSVALX((buf),(pos),((int16_t)(val)))
137#define SIVALS(buf,pos,val) SIVALX((buf),(pos),((int32_t)(val)))
138
139#else /* CAREFUL_ALIGNMENT */
140
141/* this handles things for architectures like the 386 that can handle
142   alignment errors */
143/*
144   WARNING: This section is dependent on the length of int16_t and int32_t
145   being correct
146*/
147
148/* get single value from an SMB buffer */
149#define SVAL(buf,pos) (*(const uint16_t *)((const char *)(buf) + (pos)))
150#define SVAL_NC(buf,pos) (*(uint16_t *)((char *)(buf) + (pos))) /* Non const version of above. */
151#define IVAL(buf,pos) (*(const uint32_t *)((const char *)(buf) + (pos)))
152#define IVAL_NC(buf,pos) (*(uint32_t *)((char *)(buf) + (pos))) /* Non const version of above. */
153#define SVALS(buf,pos) (*(const int16_t *)((const char *)(buf) + (pos)))
154#define SVALS_NC(buf,pos) (*(int16_t *)((char *)(buf) + (pos))) /* Non const version of above. */
155#define IVALS(buf,pos) (*(const int32_t *)((const char *)(buf) + (pos)))
156#define IVALS_NC(buf,pos) (*(int32_t *)((char *)(buf) + (pos))) /* Non const version of above. */
157
158/* store single value in an SMB buffer */
159#define SSVAL(buf,pos,val) SVAL_NC(buf,pos)=((uint16_t)(val))
160#define SIVAL(buf,pos,val) IVAL_NC(buf,pos)=((uint32_t)(val))
161#define SSVALS(buf,pos,val) SVALS_NC(buf,pos)=((int16_t)(val))
162#define SIVALS(buf,pos,val) IVALS_NC(buf,pos)=((int32_t)(val))
163
164#endif /* CAREFUL_ALIGNMENT */
165
166/* now the reverse routines - these are used in nmb packets (mostly) */
167#define SREV(x) ((((x)&0xFF)<<8) | (((x)>>8)&0xFF))
168#define IREV(x) ((SREV(x)<<16) | (SREV((x)>>16)))
169
170#define RSVAL(buf,pos) SREV(SVAL(buf,pos))
171#define RSVALS(buf,pos) SREV(SVALS(buf,pos))
172#define RIVAL(buf,pos) IREV(IVAL(buf,pos))
173#define RIVALS(buf,pos) IREV(IVALS(buf,pos))
174#define RSSVAL(buf,pos,val) SSVAL(buf,pos,SREV(val))
175#define RSSVALS(buf,pos,val) SSVALS(buf,pos,SREV(val))
176#define RSIVAL(buf,pos,val) SIVAL(buf,pos,IREV(val))
177#define RSIVALS(buf,pos,val) SIVALS(buf,pos,IREV(val))
178
179/* Alignment macros. */
180#define ALIGN4(p,base) ((p) + ((4 - (PTR_DIFF((p), (base)) & 3)) & 3))
181#define ALIGN2(p,base) ((p) + ((2 - (PTR_DIFF((p), (base)) & 1)) & 1))
182
183#endif /* _BYTEORDER_H */
Note: See TracBrowser for help on using the repository browser.