Main Page | Class Hierarchy | Class List | Directories | File List | Class Members | File Members | Related Pages

Smbbyteorder.h

00001 /* 
00002    Unix SMB/Netbios implementation.
00003    Version 1.9.
00004    SMB Byte handling
00005    Copyright (C) Andrew Tridgell 1992-1998
00006    
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 2 of the License, or
00010    (at your option) any later version.
00011    
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016    
00017    You should have received a copy of the GNU General Public License
00018    along with this program; if not, write to the Free Software
00019    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00020 */
00021 
00022 #ifndef _BYTEORDER_H
00023 #define _BYTEORDER_H
00024 
00025 /*
00026    This file implements macros for machine independent short and 
00027    int manipulation
00028 
00029 Here is a description of this file that I emailed to the samba list once:
00030 
00031 > I am confused about the way that byteorder.h works in Samba. I have
00032 > looked at it, and I would have thought that you might make a distinction
00033 > between LE and BE machines, but you only seem to distinguish between 386
00034 > and all other architectures.
00035 > 
00036 > Can you give me a clue?
00037 
00038 sure.
00039 
00040 The distinction between 386 and other architectures is only there as
00041 an optimisation. You can take it out completely and it will make no
00042 difference. The routines (macros) in byteorder.h are totally byteorder
00043 independent. The 386 optimsation just takes advantage of the fact that
00044 the x86 processors don't care about alignment, so we don't have to
00045 align ints on int boundaries etc. If there are other processors out
00046 there that aren't alignment sensitive then you could also define
00047 CAREFUL_ALIGNMENT=0 on those processors as well.
00048 
00049 Ok, now to the macros themselves. I'll take a simple example, say we
00050 want to extract a 2 byte integer from a SMB packet and put it into a
00051 type called uint16 that is in the local machines byte order, and you
00052 want to do it with only the assumption that uint16 is _at_least_ 16
00053 bits long (this last condition is very important for architectures
00054 that don't have any int types that are 2 bytes long)
00055 
00056 You do this:
00057 
00058 #define CVAL(buf,pos) (((unsigned char *)(buf))[pos])
00059 #define PVAL(buf,pos) ((unsigned)CVAL(buf,pos))
00060 #define SVAL(buf,pos) (PVAL(buf,pos)|PVAL(buf,(pos)+1)<<8)
00061 
00062 then to extract a uint16 value at offset 25 in a buffer you do this:
00063 
00064 char *buffer = foo_bar();
00065 uint16 xx = SVAL(buffer,25);
00066 
00067 We are using the byteoder independence of the ANSI C bitshifts to do
00068 the work. A good optimising compiler should turn this into efficient
00069 code, especially if it happens to have the right byteorder :-)
00070 
00071 I know these macros can be made a bit tidier by removing some of the
00072 casts, but you need to look at byteorder.h as a whole to see the
00073 reasoning behind them. byteorder.h defines the following macros:
00074 
00075 SVAL(buf,pos) - extract a 2 byte SMB value
00076 IVAL(buf,pos) - extract a 4 byte SMB value
00077 SVALS(buf,pos) signed version of SVAL()
00078 IVALS(buf,pos) signed version of IVAL()
00079 
00080 SSVAL(buf,pos,val) - put a 2 byte SMB value into a buffer
00081 SIVAL(buf,pos,val) - put a 4 byte SMB value into a buffer
00082 SSVALS(buf,pos,val) - signed version of SSVAL()
00083 SIVALS(buf,pos,val) - signed version of SIVAL()
00084 
00085 RSVAL(buf,pos) - like SVAL() but for NMB byte ordering
00086 RSVALS(buf,pos) - like SVALS() but for NMB byte ordering
00087 RIVAL(buf,pos) - like IVAL() but for NMB byte ordering
00088 RIVALS(buf,pos) - like IVALS() but for NMB byte ordering
00089 RSSVAL(buf,pos,val) - like SSVAL() but for NMB ordering
00090 RSIVAL(buf,pos,val) - like SIVAL() but for NMB ordering
00091 RSIVALS(buf,pos,val) - like SIVALS() but for NMB ordering
00092 
00093 it also defines lots of intermediate macros, just ignore those :-)
00094 
00095 */
00096 
00097 /* some switch macros that do both store and read to and from SMB buffers */
00098 
00099 #define RW_PCVAL(read,inbuf,outbuf,len) { if (read) { PCVAL (inbuf,0,outbuf,len); } else { PSCVAL(inbuf,0,outbuf,len); } }
00100 #define RW_PIVAL(read,big_endian,inbuf,outbuf,len) { if (read) { if (big_endian) { RPIVAL(inbuf,0,outbuf,len); } else { PIVAL(inbuf,0,outbuf,len); } } else { if (big_endian) { RPSIVAL(inbuf,0,outbuf,len); } else { PSIVAL(inbuf,0,outbuf,len); } } }
00101 #define RW_PSVAL(read,big_endian,inbuf,outbuf,len) { if (read) { if (big_endian) { RPSVAL(inbuf,0,outbuf,len); } else { PSVAL(inbuf,0,outbuf,len); } } else { if (big_endian) { RPSSVAL(inbuf,0,outbuf,len); } else { PSSVAL(inbuf,0,outbuf,len); } } }
00102 #define RW_CVAL(read, inbuf, outbuf, offset) { if (read) { (outbuf) = CVAL (inbuf,offset); } else { SCVAL(inbuf,offset,outbuf); } }
00103 #define RW_IVAL(read, big_endian, inbuf, outbuf, offset) { if (read) { (outbuf) = ((big_endian) ? RIVAL(inbuf,offset) : IVAL (inbuf,offset)); } else { if (big_endian) { RSIVAL(inbuf,offset,outbuf); } else { SIVAL(inbuf,offset,outbuf); } } }
00104 #define RW_SVAL(read, big_endian, inbuf, outbuf, offset) { if (read) { (outbuf) = ((big_endian) ? RSVAL(inbuf,offset) : SVAL (inbuf,offset)); } else { if (big_endian) { RSSVAL(inbuf,offset,outbuf); } else { SSVAL(inbuf,offset,outbuf); } } }
00105 
00106 #undef CAREFUL_ALIGNMENT
00107 
00108 /* we know that the 386 can handle misalignment and has the "right" 
00109    byteorder */
00110 #ifdef __i386__
00111 #define CAREFUL_ALIGNMENT 0
00112 #endif
00113 
00114 #ifndef CAREFUL_ALIGNMENT
00115 #define CAREFUL_ALIGNMENT 1
00116 #endif
00117 
00118 #define CVAL(buf,pos) (((unsigned char *)(buf))[pos])
00119 #define PVAL(buf,pos) ((unsigned)CVAL(buf,pos))
00120 #define SCVAL(buf,pos,val) (CVAL(buf,pos) = (val))
00121 
00122 
00123 #if CAREFUL_ALIGNMENT
00124 
00125 #define SVAL(buf,pos) (PVAL(buf,pos)|PVAL(buf,(pos)+1)<<8)
00126 #define IVAL(buf,pos) (SVAL(buf,pos)|SVAL(buf,(pos)+2)<<16)
00127 #define SSVALX(buf,pos,val) (CVAL(buf,pos)=(val)&0xFF,CVAL(buf,pos+1)=(val)>>8)
00128 #define SIVALX(buf,pos,val) (SSVALX(buf,pos,val&0xFFFF),SSVALX(buf,pos+2,val>>16))
00129 #define SVALS(buf,pos) ((int16)SVAL(buf,pos))
00130 #define IVALS(buf,pos) ((int32)IVAL(buf,pos))
00131 #define SSVAL(buf,pos,val) SSVALX((buf),(pos),((uint16)(val)))
00132 #define SIVAL(buf,pos,val) SIVALX((buf),(pos),((uint32)(val)))
00133 #define SSVALS(buf,pos,val) SSVALX((buf),(pos),((int16)(val)))
00134 #define SIVALS(buf,pos,val) SIVALX((buf),(pos),((int32)(val)))
00135 
00136 #else /* CAREFUL_ALIGNMENT */
00137 
00138 /* this handles things for architectures like the 386 that can handle
00139    alignment errors */
00140 /*
00141    WARNING: This section is dependent on the length of int16 and int32
00142    being correct 
00143 */
00144 
00145 /* get single value from an SMB buffer */
00146 #define SVAL(buf,pos) (*(uint16 *)((char *)(buf) + (pos)))
00147 #define IVAL(buf,pos) (*(uint32 *)((char *)(buf) + (pos)))
00148 #define SVALS(buf,pos) (*(int16 *)((char *)(buf) + (pos)))
00149 #define IVALS(buf,pos) (*(int32 *)((char *)(buf) + (pos)))
00150 
00151 /* store single value in an SMB buffer */
00152 #define SSVAL(buf,pos,val) SVAL(buf,pos)=((uint16)(val))
00153 #define SIVAL(buf,pos,val) IVAL(buf,pos)=((uint32)(val))
00154 #define SSVALS(buf,pos,val) SVALS(buf,pos)=((int16)(val))
00155 #define SIVALS(buf,pos,val) IVALS(buf,pos)=((int32)(val))
00156 
00157 #endif /* CAREFUL_ALIGNMENT */
00158 
00159 /* macros for reading / writing arrays */
00160 
00161 #define SMBMACRO(macro,buf,pos,val,len,size) { int l; for (l = 0; l < (len); l++) (val)[l] = macro((buf), (pos) + (size)*l); }
00162 #define SSMBMACRO(macro,buf,pos,val,len,size) { int l; for (l = 0; l < (len); l++) macro((buf), (pos) + (size)*l, (val)[l]); }
00163 
00164 /* reads multiple data from an SMB buffer */
00165 #define PCVAL(buf,pos,val,len) SMBMACRO(CVAL,buf,pos,val,len,1)
00166 #define PSVAL(buf,pos,val,len) SMBMACRO(SVAL,buf,pos,val,len,2)
00167 #define PIVAL(buf,pos,val,len) SMBMACRO(IVAL,buf,pos,val,len,4)
00168 #define PCVALS(buf,pos,val,len) SMBMACRO(CVALS,buf,pos,val,len,1)
00169 #define PSVALS(buf,pos,val,len) SMBMACRO(SVALS,buf,pos,val,len,2)
00170 #define PIVALS(buf,pos,val,len) SMBMACRO(IVALS,buf,pos,val,len,4)
00171 
00172 /* stores multiple data in an SMB buffer */
00173 #define PSCVAL(buf,pos,val,len) SSMBMACRO(SCVAL,buf,pos,val,len,1)
00174 #define PSSVAL(buf,pos,val,len) SSMBMACRO(SSVAL,buf,pos,val,len,2)
00175 #define PSIVAL(buf,pos,val,len) SSMBMACRO(SIVAL,buf,pos,val,len,4)
00176 #define PSCVALS(buf,pos,val,len) SSMBMACRO(SCVALS,buf,pos,val,len,1)
00177 #define PSSVALS(buf,pos,val,len) SSMBMACRO(SSVALS,buf,pos,val,len,2)
00178 #define PSIVALS(buf,pos,val,len) SSMBMACRO(SIVALS,buf,pos,val,len,4)
00179 
00180 
00181 /* now the reverse routines - these are used in nmb packets (mostly) */
00182 #define SREV(x) ((((x)&0xFF)<<8) | (((x)>>8)&0xFF))
00183 #define IREV(x) ((SREV(x)<<16) | (SREV((x)>>16)))
00184 
00185 #define RSVAL(buf,pos) SREV(SVAL(buf,pos))
00186 #define RSVALS(buf,pos) SREV(SVALS(buf,pos))
00187 #define RIVAL(buf,pos) IREV(IVAL(buf,pos))
00188 #define RIVALS(buf,pos) IREV(IVALS(buf,pos))
00189 #define RSSVAL(buf,pos,val) SSVAL(buf,pos,SREV(val))
00190 #define RSSVALS(buf,pos,val) SSVALS(buf,pos,SREV(val))
00191 #define RSIVAL(buf,pos,val) SIVAL(buf,pos,IREV(val))
00192 #define RSIVALS(buf,pos,val) SIVALS(buf,pos,IREV(val))
00193 
00194 /* reads multiple data from an SMB buffer (big-endian) */
00195 #define RPSVAL(buf,pos,val,len) SMBMACRO(RSVAL,buf,pos,val,len,2)
00196 #define RPIVAL(buf,pos,val,len) SMBMACRO(RIVAL,buf,pos,val,len,4)
00197 #define RPSVALS(buf,pos,val,len) SMBMACRO(RSVALS,buf,pos,val,len,2)
00198 #define RPIVALS(buf,pos,val,len) SMBMACRO(RIVALS,buf,pos,val,len,4)
00199 
00200 /* stores multiple data in an SMB buffer (big-endian) */
00201 #define RPSSVAL(buf,pos,val,len) SSMBMACRO(RSSVAL,buf,pos,val,len,2)
00202 #define RPSIVAL(buf,pos,val,len) SSMBMACRO(RSIVAL,buf,pos,val,len,4)
00203 #define RPSSVALS(buf,pos,val,len) SSMBMACRO(RSSVALS,buf,pos,val,len,2)
00204 #define RPSIVALS(buf,pos,val,len) SSMBMACRO(RSIVALS,buf,pos,val,len,4)
00205 
00206 #define DBG_RW_PCVAL(charmode,string,depth,base,read,inbuf,outbuf,len) { RW_PCVAL(read,inbuf,outbuf,len) DEBUG(5,("%s%04x %s: ", tab_depth(depth), base,string)); if (charmode) print_asc(5, (unsigned char*)(outbuf), (len)); else { int idx; for (idx = 0; idx < len; idx++) { DEBUG(5,("%02x ", (outbuf)[idx])); } } DEBUG(5,("\n")); } 
00207 #define DBG_RW_PSVAL(charmode,string,depth,base,read,big_endian,inbuf,outbuf,len) { RW_PSVAL(read,big_endian,inbuf,outbuf,len) DEBUG(5,("%s%04x %s: ", tab_depth(depth), base,string)); if (charmode) print_asc(5, (unsigned char*)(outbuf), 2*(len)); else { int idx; for (idx = 0; idx < len; idx++) { DEBUG(5,("%04x ", (outbuf)[idx])); } } DEBUG(5,("\n")); }
00208 #define DBG_RW_PIVAL(charmode,string,depth,base,read,big_endian,inbuf,outbuf,len) { RW_PIVAL(read,big_endian,inbuf,outbuf,len) DEBUG(5,("%s%04x %s: ", tab_depth(depth), base,string)); if (charmode) print_asc(5, (unsigned char*)(outbuf), 4*(len)); else { int idx; for (idx = 0; idx < len; idx++) { DEBUG(5,("%08x ", (outbuf)[idx])); } } DEBUG(5,("\n")); }
00209 #define DBG_RW_CVAL(string,depth,base,read,inbuf,outbuf) { RW_CVAL(read,inbuf,outbuf,0) DEBUG(5,("%s%04x %s: %02x\n", tab_depth(depth), base, string, outbuf)); }
00210 #define DBG_RW_SVAL(string,depth,base,read,big_endian,inbuf,outbuf) { RW_SVAL(read,big_endian,inbuf,outbuf,0) DEBUG(5,("%s%04x %s: %04x\n", tab_depth(depth), base, string, outbuf)); }
00211 #define DBG_RW_IVAL(string,depth,base,read,big_endian,inbuf,outbuf) { RW_IVAL(read,big_endian,inbuf,outbuf,0) DEBUG(5,("%s%04x %s: %08x\n", tab_depth(depth), base, string, outbuf)); }
00212 
00213 #endif /* _BYTEORDER_H */

Generated on Wed Oct 26 14:46:51 2005 for Lgi by  doxygen 1.4.1