OpenVAS Libraries  4.0+rc3.SVN
nasl/byteorder.h
00001 /*
00002    Unix SMB/CIFS implementation.
00003    SMB Byte handling
00004    Copyright (C) Andrew Tridgell 1992-1998
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published by
00008    the Free Software Foundation; either version 2 of the License, or
00009    (at your option) any later version.
00010 
00011    This program is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014    GNU General Public License for more details.
00015 
00016    You should have received a copy of the GNU General Public License
00017    along with this program; if not, write to the Free Software
00018    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00019 */
00020 
00021 #ifndef _BYTEORDER_H
00022 #define _BYTEORDER_H
00023 
00024 /*
00025    This file implements macros for machine independent short and
00026    int manipulation
00027 
00028 Here is a description of this file that I emailed to the samba list once:
00029 
00030 > I am confused about the way that byteorder.h works in Samba. I have
00031 > looked at it, and I would have thought that you might make a distinction
00032 > between LE and BE machines, but you only seem to distinguish between 386
00033 > and all other architectures.
00034 >
00035 > Can you give me a clue?
00036 
00037 sure.
00038 
00039 The distinction between 386 and other architectures is only there as
00040 an optimisation. You can take it out completely and it will make no
00041 difference. The routines (macros) in byteorder.h are totally byteorder
00042 independent. The 386 optimsation just takes advantage of the fact that
00043 the x86 processors don't care about alignment, so we don't have to
00044 align ints on int boundaries etc. If there are other processors out
00045 there that aren't alignment sensitive then you could also define
00046 CAREFUL_ALIGNMENT=0 on those processors as well.
00047 
00048 Ok, now to the macros themselves. I'll take a simple example, say we
00049 want to extract a 2 byte integer from a SMB packet and put it into a
00050 type called uint16 that is in the local machines byte order, and you
00051 want to do it with only the assumption that uint16 is _at_least_ 16
00052 bits long (this last condition is very important for architectures
00053 that don't have any int types that are 2 bytes long)
00054 
00055 You do this:
00056 
00057 #define CVAL(buf,pos) (((unsigned char *)(buf))[pos])
00058 #define PVAL(buf,pos) ((unsigned)CVAL(buf,pos))
00059 #define SVAL(buf,pos) (PVAL(buf,pos)|PVAL(buf,(pos)+1)<<8)
00060 
00061 then to extract a uint16 value at offset 25 in a buffer you do this:
00062 
00063 char *buffer = foo_bar();
00064 uint16 xx = SVAL(buffer,25);
00065 
00066 We are using the byteoder independence of the ANSI C bitshifts to do
00067 the work. A good optimising compiler should turn this into efficient
00068 code, especially if it happens to have the right byteorder :-)
00069 
00070 I know these macros can be made a bit tidier by removing some of the
00071 casts, but you need to look at byteorder.h as a whole to see the
00072 reasoning behind them. byteorder.h defines the following macros:
00073 
00074 SVAL(buf,pos) - extract a 2 byte SMB value
00075 IVAL(buf,pos) - extract a 4 byte SMB value
00076 SVALS(buf,pos) signed version of SVAL()
00077 IVALS(buf,pos) signed version of IVAL()
00078 
00079 SSVAL(buf,pos,val) - put a 2 byte SMB value into a buffer
00080 SIVAL(buf,pos,val) - put a 4 byte SMB value into a buffer
00081 SSVALS(buf,pos,val) - signed version of SSVAL()
00082 SIVALS(buf,pos,val) - signed version of SIVAL()
00083 
00084 RSVAL(buf,pos) - like SVAL() but for NMB byte ordering
00085 RSVALS(buf,pos) - like SVALS() but for NMB byte ordering
00086 RIVAL(buf,pos) - like IVAL() but for NMB byte ordering
00087 RIVALS(buf,pos) - like IVALS() but for NMB byte ordering
00088 RSSVAL(buf,pos,val) - like SSVAL() but for NMB ordering
00089 RSIVAL(buf,pos,val) - like SIVAL() but for NMB ordering
00090 RSIVALS(buf,pos,val) - like SIVALS() but for NMB ordering
00091 
00092 it also defines lots of intermediate macros, just ignore those :-)
00093 
00094 */
00095 
00096 #undef CAREFUL_ALIGNMENT
00097 
00098 /* we know that the 386 can handle misalignment and has the "right"
00099    byteorder */
00100 #ifdef __i386__
00101 #define CAREFUL_ALIGNMENT 0
00102 #endif
00103 
00104 #ifndef CAREFUL_ALIGNMENT
00105 #define CAREFUL_ALIGNMENT 1
00106 #endif
00107 
00108 #define CVAL(buf,pos) ((unsigned)(((const unsigned char *)(buf))[pos]))
00109 #define CVAL_NC(buf,pos) (((unsigned char *)(buf))[pos]) /* Non-const version of CVAL */
00110 #define PVAL(buf,pos) (CVAL(buf,pos))
00111 #define SCVAL(buf,pos,val) (CVAL_NC(buf,pos) = (val))
00112 
00113 
00114 #if CAREFUL_ALIGNMENT
00115 
00116 #define SVAL(buf,pos) (PVAL(buf,pos)|PVAL(buf,(pos)+1)<<8)
00117 #define IVAL(buf,pos) (SVAL(buf,pos)|SVAL(buf,(pos)+2)<<16)
00118 #define SSVALX(buf,pos,val) (CVAL_NC(buf,pos)=(unsigned char)((val)&0xFF),CVAL_NC(buf,pos+1)=(unsigned char)((val)>>8))
00119 #define SIVALX(buf,pos,val) (SSVALX(buf,pos,val&0xFFFF),SSVALX(buf,pos+2,val>>16))
00120 #define SVALS(buf,pos) ((int16)SVAL(buf,pos))
00121 #define IVALS(buf,pos) ((int32)IVAL(buf,pos))
00122 #define SSVAL(buf,pos,val) SSVALX((buf),(pos),((uint16)(val)))
00123 #define SIVAL(buf,pos,val) SIVALX((buf),(pos),((uint32)(val)))
00124 #define SSVALS(buf,pos,val) SSVALX((buf),(pos),((int16)(val)))
00125 #define SIVALS(buf,pos,val) SIVALX((buf),(pos),((int32)(val)))
00126 
00127 #else /* CAREFUL_ALIGNMENT */
00128 
00129 /* this handles things for architectures like the 386 that can handle
00130    alignment errors */
00131 /*
00132    WARNING: This section is dependent on the length of int16 and int32
00133    being correct
00134 */
00135 
00136 /* get single value from an SMB buffer */
00137 #define SVAL(buf,pos) (*(const uint16 *)((const char *)(buf) + (pos)))
00138 #define SVAL_NC(buf,pos) (*(uint16 *)((char *)(buf) + (pos))) /* Non const version of above. */
00139 #define IVAL(buf,pos) (*(const uint32 *)((const char *)(buf) + (pos)))
00140 #define IVAL_NC(buf,pos) (*(uint32 *)((char *)(buf) + (pos))) /* Non const version of above. */
00141 #define SVALS(buf,pos) (*(const int16 *)((const char *)(buf) + (pos)))
00142 #define SVALS_NC(buf,pos) (*(int16 *)((char *)(buf) + (pos))) /* Non const version of above. */
00143 #define IVALS(buf,pos) (*(const int32 *)((const char *)(buf) + (pos)))
00144 #define IVALS_NC(buf,pos) (*(int32 *)((char *)(buf) + (pos))) /* Non const version of above. */
00145 
00146 /* store single value in an SMB buffer */
00147 #define SSVAL(buf,pos,val) SVAL_NC(buf,pos)=((uint16)(val))
00148 #define SIVAL(buf,pos,val) IVAL_NC(buf,pos)=((uint32)(val))
00149 #define SSVALS(buf,pos,val) SVALS_NC(buf,pos)=((int16)(val))
00150 #define SIVALS(buf,pos,val) IVALS_NC(buf,pos)=((int32)(val))
00151 
00152 #endif /* CAREFUL_ALIGNMENT */
00153 
00154 /* now the reverse routines - these are used in nmb packets (mostly) */
00155 #define SREV(x) ((((x)&0xFF)<<8) | (((x)>>8)&0xFF))
00156 #define IREV(x) ((SREV(x)<<16) | (SREV((x)>>16)))
00157 
00158 #define RSVAL(buf,pos) SREV(SVAL(buf,pos))
00159 #define RSVALS(buf,pos) SREV(SVALS(buf,pos))
00160 #define RIVAL(buf,pos) IREV(IVAL(buf,pos))
00161 #define RIVALS(buf,pos) IREV(IVALS(buf,pos))
00162 #define RSSVAL(buf,pos,val) SSVAL(buf,pos,SREV(val))
00163 #define RSSVALS(buf,pos,val) SSVALS(buf,pos,SREV(val))
00164 #define RSIVAL(buf,pos,val) SIVAL(buf,pos,IREV(val))
00165 #define RSIVALS(buf,pos,val) SIVALS(buf,pos,IREV(val))
00166 
00167 /* Alignment macros. */
00168 #define ALIGN4(p,base) ((p) + ((4 - (PTR_DIFF((p), (base)) & 3)) & 3))
00169 #define ALIGN2(p,base) ((p) + ((2 - (PTR_DIFF((p), (base)) & 1)) & 1))
00170 
00171 #endif /* _BYTEORDER_H */