regfi
Loading...
Searching...
No Matches
byteorder.h
Go to the documentation of this file.
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
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 */