source: releases/0.3.0/include/byteorder.h @ 246

Last change on this file since 246 was 30, checked in by tim, 19 years ago

Added initial version of regfio interface, borrowed from Samba project.

Library has all write capability stripped, and all Samba dependency has
either been removed, or included in the smb_deps code.

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