regfi
byteorder.h
Go to the documentation of this file.
00001 /*
00002  * Branched from Samba project Subversion repository, version #2:
00003  *  http://websvn.samba.org/cgi-bin/viewcvs.cgi/trunk/source/include/byteorder.h
00004  *
00005  * Unix SMB/CIFS implementation.
00006  * SMB Byte handling
00007  *
00008  * Copyright (C) 2005 Timothy D. Morgan
00009  * Copyright (C) 1992-1998 Andrew Tridgell 
00010  * 
00011  * This program is free software; you can redistribute it and/or modify
00012  * it under the terms of the GNU General Public License as published by
00013  * the Free Software Foundation; version 3 of the License.
00014  * 
00015  * This program is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  * GNU General Public License for more details.
00019  * 
00020  * You should have received a copy of the GNU General Public License
00021  * along with this program; if not, write to the Free Software
00022  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00023  *
00024  * $Id: byteorder.h 168 2010-03-03 00:08:42Z tim $
00025  */
00026 
00027 #ifndef _BYTEORDER_H
00028 #define _BYTEORDER_H
00029 
00108 #undef CAREFUL_ALIGNMENT
00109 
00110 /* we know that the 386 can handle misalignment and has the "right" 
00111    byteorder */
00112 #ifdef __i386__
00113 #define CAREFUL_ALIGNMENT 0
00114 #endif
00115 
00116 #ifndef CAREFUL_ALIGNMENT
00117 #define CAREFUL_ALIGNMENT 1
00118 #endif
00119 
00120 #define CVAL(buf,pos) ((unsigned)(((const unsigned char *)(buf))[pos]))
00121 #define CVAL_NC(buf,pos) (((unsigned char *)(buf))[pos]) /* Non-const version of CVAL */
00122 #define PVAL(buf,pos) (CVAL(buf,pos))
00123 #define SCVAL(buf,pos,val) (CVAL_NC(buf,pos) = (val))
00124 
00125 
00126 #if CAREFUL_ALIGNMENT
00127 
00128 #define SVAL(buf,pos) (PVAL(buf,pos)|PVAL(buf,(pos)+1)<<8)
00129 #define IVAL(buf,pos) (SVAL(buf,pos)|SVAL(buf,(pos)+2)<<16)
00130 #define SSVALX(buf,pos,val) (CVAL_NC(buf,pos)=(unsigned char)((val)&0xFF),CVAL_NC(buf,pos+1)=(unsigned char)((val)>>8))
00131 #define SIVALX(buf,pos,val) (SSVALX(buf,pos,val&0xFFFF),SSVALX(buf,pos+2,val>>16))
00132 #define SVALS(buf,pos) ((int16_t)SVAL(buf,pos))
00133 #define IVALS(buf,pos) ((int32_t)IVAL(buf,pos))
00134 #define SSVAL(buf,pos,val) SSVALX((buf),(pos),((uint16_t)(val)))
00135 #define SIVAL(buf,pos,val) SIVALX((buf),(pos),((uint32_t)(val)))
00136 #define SSVALS(buf,pos,val) SSVALX((buf),(pos),((int16_t)(val)))
00137 #define SIVALS(buf,pos,val) SIVALX((buf),(pos),((int32_t)(val)))
00138 
00139 #else /* CAREFUL_ALIGNMENT */
00140 
00141 /* this handles things for architectures like the 386 that can handle
00142    alignment errors */
00143 /*
00144    WARNING: This section is dependent on the length of int16_t and int32_t
00145    being correct 
00146 */
00147 
00148 /* get single value from an SMB buffer */
00149 #define SVAL(buf,pos) (*(const uint16_t *)((const char *)(buf) + (pos)))
00150 #define SVAL_NC(buf,pos) (*(uint16_t *)((char *)(buf) + (pos))) /* Non const version of above. */
00151 #define IVAL(buf,pos) (*(const uint32_t *)((const char *)(buf) + (pos)))
00152 #define IVAL_NC(buf,pos) (*(uint32_t *)((char *)(buf) + (pos))) /* Non const version of above. */
00153 #define SVALS(buf,pos) (*(const int16_t *)((const char *)(buf) + (pos)))
00154 #define SVALS_NC(buf,pos) (*(int16_t *)((char *)(buf) + (pos))) /* Non const version of above. */
00155 #define IVALS(buf,pos) (*(const int32_t *)((const char *)(buf) + (pos)))
00156 #define IVALS_NC(buf,pos) (*(int32_t *)((char *)(buf) + (pos))) /* Non const version of above. */
00157 
00158 /* store single value in an SMB buffer */
00159 #define SSVAL(buf,pos,val) SVAL_NC(buf,pos)=((uint16_t)(val))
00160 #define SIVAL(buf,pos,val) IVAL_NC(buf,pos)=((uint32_t)(val))
00161 #define SSVALS(buf,pos,val) SVALS_NC(buf,pos)=((int16_t)(val))
00162 #define SIVALS(buf,pos,val) IVALS_NC(buf,pos)=((int32_t)(val))
00163 
00164 #endif /* CAREFUL_ALIGNMENT */
00165 
00166 /* now the reverse routines - these are used in nmb packets (mostly) */
00167 #define SREV(x) ((((x)&0xFF)<<8) | (((x)>>8)&0xFF))
00168 #define IREV(x) ((SREV(x)<<16) | (SREV((x)>>16)))
00169 
00170 #define RSVAL(buf,pos) SREV(SVAL(buf,pos))
00171 #define RSVALS(buf,pos) SREV(SVALS(buf,pos))
00172 #define RIVAL(buf,pos) IREV(IVAL(buf,pos))
00173 #define RIVALS(buf,pos) IREV(IVALS(buf,pos))
00174 #define RSSVAL(buf,pos,val) SSVAL(buf,pos,SREV(val))
00175 #define RSSVALS(buf,pos,val) SSVALS(buf,pos,SREV(val))
00176 #define RSIVAL(buf,pos,val) SIVAL(buf,pos,IREV(val))
00177 #define RSIVALS(buf,pos,val) SIVALS(buf,pos,IREV(val))
00178 
00179 /* Alignment macros. */
00180 #define ALIGN4(p,base) ((p) + ((4 - (PTR_DIFF((p), (base)) & 3)) & 3))
00181 #define ALIGN2(p,base) ((p) + ((2 - (PTR_DIFF((p), (base)) & 1)) & 1))
00182 
00183 #endif /* _BYTEORDER_H */
 All Data Structures Files Functions Variables