| 1 |
/* |
| 2 |
Unix SMB/Netbios implementation. |
| 3 |
Version 1.9. |
| 4 |
SMB Byte handling |
| 5 |
Copyright (C) Andrew Tridgell 1992-1998 |
| 6 |
|
| 7 |
This program is free software; you can redistribute it and/or modify |
| 8 |
it under the terms of the GNU General Public License as published by |
| 9 |
the Free Software Foundation; either version 2 of the License, or |
| 10 |
(at your option) any later version. |
| 11 |
|
| 12 |
This program is distributed in the hope that it will be useful, |
| 13 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
GNU General Public License for more details. |
| 16 |
|
| 17 |
You should have received a copy of the GNU General Public License |
| 18 |
along with this program; if not, write to the Free Software |
| 19 |
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 20 |
*/ |
| 21 |
|
| 22 |
#ifndef _BYTEORDER_H |
| 23 |
#define _BYTEORDER_H |
| 24 |
|
| 25 |
/* |
| 26 |
This file implements macros for machine independent short and |
| 27 |
int manipulation |
| 28 |
|
| 29 |
Here is a description of this file that I emailed to the samba list once: |
| 30 |
|
| 31 |
> I am confused about the way that byteorder.h works in Samba. I have |
| 32 |
> looked at it, and I would have thought that you might make a distinction |
| 33 |
> between LE and BE machines, but you only seem to distinguish between 386 |
| 34 |
> and all other architectures. |
| 35 |
> |
| 36 |
> Can you give me a clue? |
| 37 |
|
| 38 |
sure. |
| 39 |
|
| 40 |
The distinction between 386 and other architectures is only there as |
| 41 |
an optimisation. You can take it out completely and it will make no |
| 42 |
difference. The routines (macros) in byteorder.h are totally byteorder |
| 43 |
independent. The 386 optimsation just takes advantage of the fact that |
| 44 |
the x86 processors don't care about alignment, so we don't have to |
| 45 |
align ints on int boundaries etc. If there are other processors out |
| 46 |
there that aren't alignment sensitive then you could also define |
| 47 |
CAREFUL_ALIGNMENT=0 on those processors as well. |
| 48 |
|
| 49 |
Ok, now to the macros themselves. I'll take a simple example, say we |
| 50 |
want to extract a 2 byte integer from a SMB packet and put it into a |
| 51 |
type called uint16 that is in the local machines byte order, and you |
| 52 |
want to do it with only the assumption that uint16 is _at_least_ 16 |
| 53 |
bits long (this last condition is very important for architectures |
| 54 |
that don't have any int types that are 2 bytes long) |
| 55 |
|
| 56 |
You do this: |
| 57 |
|
| 58 |
#define CVAL(buf,pos) (((unsigned char *)(buf))[pos]) |
| 59 |
#define PVAL(buf,pos) ((unsigned)CVAL(buf,pos)) |
| 60 |
#define SVAL(buf,pos) (PVAL(buf,pos)|PVAL(buf,(pos)+1)<<8) |
| 61 |
|
| 62 |
then to extract a uint16 value at offset 25 in a buffer you do this: |
| 63 |
|
| 64 |
char *buffer = foo_bar(); |
| 65 |
uint16 xx = SVAL(buffer,25); |
| 66 |
|
| 67 |
We are using the byteoder independence of the ANSI C bitshifts to do |
| 68 |
the work. A good optimising compiler should turn this into efficient |
| 69 |
code, especially if it happens to have the right byteorder :-) |
| 70 |
|
| 71 |
I know these macros can be made a bit tidier by removing some of the |
| 72 |
casts, but you need to look at byteorder.h as a whole to see the |
| 73 |
reasoning behind them. byteorder.h defines the following macros: |
| 74 |
|
| 75 |
SVAL(buf,pos) - extract a 2 byte SMB value |
| 76 |
IVAL(buf,pos) - extract a 4 byte SMB value |
| 77 |
SVALS(buf,pos) signed version of SVAL() |
| 78 |
IVALS(buf,pos) signed version of IVAL() |
| 79 |
|
| 80 |
SSVAL(buf,pos,val) - put a 2 byte SMB value into a buffer |
| 81 |
SIVAL(buf,pos,val) - put a 4 byte SMB value into a buffer |
| 82 |
SSVALS(buf,pos,val) - signed version of SSVAL() |
| 83 |
SIVALS(buf,pos,val) - signed version of SIVAL() |
| 84 |
|
| 85 |
RSVAL(buf,pos) - like SVAL() but for NMB byte ordering |
| 86 |
RSVALS(buf,pos) - like SVALS() but for NMB byte ordering |
| 87 |
RIVAL(buf,pos) - like IVAL() but for NMB byte ordering |
| 88 |
RIVALS(buf,pos) - like IVALS() but for NMB byte ordering |
| 89 |
RSSVAL(buf,pos,val) - like SSVAL() but for NMB ordering |
| 90 |
RSIVAL(buf,pos,val) - like SIVAL() but for NMB ordering |
| 91 |
RSIVALS(buf,pos,val) - like SIVALS() but for NMB ordering |
| 92 |
|
| 93 |
it also defines lots of intermediate macros, just ignore those :-) |
| 94 |
|
| 95 |
*/ |
| 96 |
|
| 97 |
/* some switch macros that do both store and read to and from SMB buffers */ |
| 98 |
|
| 99 |
#define RW_PCVAL(read,inbuf,outbuf,len) \ |
| 100 |
{ if (read) { PCVAL (inbuf,0,outbuf,len); } \ |
| 101 |
else { PSCVAL(inbuf,0,outbuf,len); } } |
| 102 |
|
| 103 |
#define RW_PIVAL(read,big_endian,inbuf,outbuf,len) \ |
| 104 |
{ if (read) { if (big_endian) { RPIVAL(inbuf,0,outbuf,len); } else { PIVAL(inbuf,0,outbuf,len); } } \ |
| 105 |
else { if (big_endian) { RPSIVAL(inbuf,0,outbuf,len); } else { PSIVAL(inbuf,0,outbuf,len); } } } |
| 106 |
|
| 107 |
#define RW_PSVAL(read,big_endian,inbuf,outbuf,len) \ |
| 108 |
{ if (read) { if (big_endian) { RPSVAL(inbuf,0,outbuf,len); } else { PSVAL(inbuf,0,outbuf,len); } } \ |
| 109 |
else { if (big_endian) { RPSSVAL(inbuf,0,outbuf,len); } else { PSSVAL(inbuf,0,outbuf,len); } } } |
| 110 |
|
| 111 |
#define RW_CVAL(read, inbuf, outbuf, offset) \ |
| 112 |
{ if (read) { (outbuf) = CVAL (inbuf,offset); } \ |
| 113 |
else { SCVAL(inbuf,offset,outbuf); } } |
| 114 |
|
| 115 |
#define RW_IVAL(read, big_endian, inbuf, outbuf, offset) \ |
| 116 |
{ if (read) { (outbuf) = ((big_endian) ? RIVAL(inbuf,offset) : IVAL (inbuf,offset)); } \ |
| 117 |
else { if (big_endian) { RSIVAL(inbuf,offset,outbuf); } else { SIVAL(inbuf,offset,outbuf); } } } |
| 118 |
|
| 119 |
#define RW_SVAL(read, big_endian, inbuf, outbuf, offset) \ |
| 120 |
{ if (read) { (outbuf) = ((big_endian) ? RSVAL(inbuf,offset) : SVAL (inbuf,offset)); } \ |
| 121 |
else { if (big_endian) { RSSVAL(inbuf,offset,outbuf); } else { SSVAL(inbuf,offset,outbuf); } } } |
| 122 |
|
| 123 |
#undef CAREFUL_ALIGNMENT |
| 124 |
|
| 125 |
/* we know that the 386 can handle misalignment and has the "right" |
| 126 |
byteorder */ |
| 127 |
#ifdef __i386__ |
| 128 |
#define CAREFUL_ALIGNMENT 0 |
| 129 |
#endif |
| 130 |
|
| 131 |
#ifndef CAREFUL_ALIGNMENT |
| 132 |
#define CAREFUL_ALIGNMENT 1 |
| 133 |
#endif |
| 134 |
|
| 135 |
#define CVAL(buf,pos) (((unsigned char *)(buf))[pos]) |
| 136 |
#define PVAL(buf,pos) ((unsigned)CVAL(buf,pos)) |
| 137 |
#define SCVAL(buf,pos,val) (CVAL(buf,pos) = (val)) |
| 138 |
|
| 139 |
|
| 140 |
#if CAREFUL_ALIGNMENT |
| 141 |
|
| 142 |
#define SVAL(buf,pos) (PVAL(buf,pos)|PVAL(buf,(pos)+1)<<8) |
| 143 |
#define IVAL(buf,pos) (SVAL(buf,pos)|SVAL(buf,(pos)+2)<<16) |
| 144 |
#define SSVALX(buf,pos,val) (CVAL(buf,pos)=(val)&0xFF,CVAL(buf,pos+1)=(val)>>8) |
| 145 |
#define SIVALX(buf,pos,val) (SSVALX(buf,pos,val&0xFFFF),SSVALX(buf,pos+2,val>>16)) |
| 146 |
#define SVALS(buf,pos) ((int16)SVAL(buf,pos)) |
| 147 |
#define IVALS(buf,pos) ((int32)IVAL(buf,pos)) |
| 148 |
#define SSVAL(buf,pos,val) SSVALX((buf),(pos),((uint16)(val))) |
| 149 |
#define SIVAL(buf,pos,val) SIVALX((buf),(pos),((uint32)(val))) |
| 150 |
#define SSVALS(buf,pos,val) SSVALX((buf),(pos),((int16)(val))) |
| 151 |
#define SIVALS(buf,pos,val) SIVALX((buf),(pos),((int32)(val))) |
| 152 |
|
| 153 |
#else /* CAREFUL_ALIGNMENT */ |
| 154 |
|
| 155 |
/* this handles things for architectures like the 386 that can handle |
| 156 |
alignment errors */ |
| 157 |
/* |
| 158 |
WARNING: This section is dependent on the length of int16 and int32 |
| 159 |
being correct |
| 160 |
*/ |
| 161 |
|
| 162 |
/* get single value from an SMB buffer */ |
| 163 |
#define SVAL(buf,pos) (*(uint16 *)((char *)(buf) + (pos))) |
| 164 |
#define IVAL(buf,pos) (*(uint32 *)((char *)(buf) + (pos))) |
| 165 |
#define SVALS(buf,pos) (*(int16 *)((char *)(buf) + (pos))) |
| 166 |
#define IVALS(buf,pos) (*(int32 *)((char *)(buf) + (pos))) |
| 167 |
|
| 168 |
/* store single value in an SMB buffer */ |
| 169 |
#define SSVAL(buf,pos,val) SVAL(buf,pos)=((uint16)(val)) |
| 170 |
#define SIVAL(buf,pos,val) IVAL(buf,pos)=((uint32)(val)) |
| 171 |
#define SSVALS(buf,pos,val) SVALS(buf,pos)=((int16)(val)) |
| 172 |
#define SIVALS(buf,pos,val) IVALS(buf,pos)=((int32)(val)) |
| 173 |
|
| 174 |
#endif /* CAREFUL_ALIGNMENT */ |
| 175 |
|
| 176 |
/* macros for reading / writing arrays */ |
| 177 |
|
| 178 |
#define SMBMACRO(macro,buf,pos,val,len,size) \ |
| 179 |
{ int l; for (l = 0; l < (len); l++) (val)[l] = macro((buf), (pos) + (size)*l); } |
| 180 |
|
| 181 |
#define SSMBMACRO(macro,buf,pos,val,len,size) \ |
| 182 |
{ int l; for (l = 0; l < (len); l++) macro((buf), (pos) + (size)*l, (val)[l]); } |
| 183 |
|
| 184 |
/* reads multiple data from an SMB buffer */ |
| 185 |
#define PCVAL(buf,pos,val,len) SMBMACRO(CVAL,buf,pos,val,len,1) |
| 186 |
#define PSVAL(buf,pos,val,len) SMBMACRO(SVAL,buf,pos,val,len,2) |
| 187 |
#define PIVAL(buf,pos,val,len) SMBMACRO(IVAL,buf,pos,val,len,4) |
| 188 |
#define PCVALS(buf,pos,val,len) SMBMACRO(CVALS,buf,pos,val,len,1) |
| 189 |
#define PSVALS(buf,pos,val,len) SMBMACRO(SVALS,buf,pos,val,len,2) |
| 190 |
#define PIVALS(buf,pos,val,len) SMBMACRO(IVALS,buf,pos,val,len,4) |
| 191 |
|
| 192 |
/* stores multiple data in an SMB buffer */ |
| 193 |
#define PSCVAL(buf,pos,val,len) SSMBMACRO(SCVAL,buf,pos,val,len,1) |
| 194 |
#define PSSVAL(buf,pos,val,len) SSMBMACRO(SSVAL,buf,pos,val,len,2) |
| 195 |
#define PSIVAL(buf,pos,val,len) SSMBMACRO(SIVAL,buf,pos,val,len,4) |
| 196 |
#define PSCVALS(buf,pos,val,len) SSMBMACRO(SCVALS,buf,pos,val,len,1) |
| 197 |
#define PSSVALS(buf,pos,val,len) SSMBMACRO(SSVALS,buf,pos,val,len,2) |
| 198 |
#define PSIVALS(buf,pos,val,len) SSMBMACRO(SIVALS,buf,pos,val,len,4) |
| 199 |
|
| 200 |
|
| 201 |
/* now the reverse routines - these are used in nmb packets (mostly) */ |
| 202 |
#define SREV(x) ((((x)&0xFF)<<8) | (((x)>>8)&0xFF)) |
| 203 |
#define IREV(x) ((SREV(x)<<16) | (SREV((x)>>16))) |
| 204 |
|
| 205 |
#define RSVAL(buf,pos) SREV(SVAL(buf,pos)) |
| 206 |
#define RSVALS(buf,pos) SREV(SVALS(buf,pos)) |
| 207 |
#define RIVAL(buf,pos) IREV(IVAL(buf,pos)) |
| 208 |
#define RIVALS(buf,pos) IREV(IVALS(buf,pos)) |
| 209 |
#define RSSVAL(buf,pos,val) SSVAL(buf,pos,SREV(val)) |
| 210 |
#define RSSVALS(buf,pos,val) SSVALS(buf,pos,SREV(val)) |
| 211 |
#define RSIVAL(buf,pos,val) SIVAL(buf,pos,IREV(val)) |
| 212 |
#define RSIVALS(buf,pos,val) SIVALS(buf,pos,IREV(val)) |
| 213 |
|
| 214 |
/* reads multiple data from an SMB buffer (big-endian) */ |
| 215 |
#define RPSVAL(buf,pos,val,len) SMBMACRO(RSVAL,buf,pos,val,len,2) |
| 216 |
#define RPIVAL(buf,pos,val,len) SMBMACRO(RIVAL,buf,pos,val,len,4) |
| 217 |
#define RPSVALS(buf,pos,val,len) SMBMACRO(RSVALS,buf,pos,val,len,2) |
| 218 |
#define RPIVALS(buf,pos,val,len) SMBMACRO(RIVALS,buf,pos,val,len,4) |
| 219 |
|
| 220 |
/* stores multiple data in an SMB buffer (big-endian) */ |
| 221 |
#define RPSSVAL(buf,pos,val,len) SSMBMACRO(RSSVAL,buf,pos,val,len,2) |
| 222 |
#define RPSIVAL(buf,pos,val,len) SSMBMACRO(RSIVAL,buf,pos,val,len,4) |
| 223 |
#define RPSSVALS(buf,pos,val,len) SSMBMACRO(RSSVALS,buf,pos,val,len,2) |
| 224 |
#define RPSIVALS(buf,pos,val,len) SSMBMACRO(RSIVALS,buf,pos,val,len,4) |
| 225 |
|
| 226 |
#define DBG_RW_PCVAL(charmode,string,depth,base,read,inbuf,outbuf,len) \ |
| 227 |
{ RW_PCVAL(read,inbuf,outbuf,len) \ |
| 228 |
DEBUG(5,("%s%04x %s: ", \ |
| 229 |
tab_depth(depth), base,string)); \ |
| 230 |
if (charmode) print_asc(5, (unsigned char*)(outbuf), (len)); else \ |
| 231 |
{ int idx; for (idx = 0; idx < len; idx++) { DEBUG(5,("%02x ", (outbuf)[idx])); } } \ |
| 232 |
DEBUG(5,("\n")); } |
| 233 |
|
| 234 |
#define DBG_RW_PSVAL(charmode,string,depth,base,read,big_endian,inbuf,outbuf,len) \ |
| 235 |
{ RW_PSVAL(read,big_endian,inbuf,outbuf,len) \ |
| 236 |
DEBUG(5,("%s%04x %s: ", \ |
| 237 |
tab_depth(depth), base,string)); \ |
| 238 |
if (charmode) print_asc(5, (unsigned char*)(outbuf), 2*(len)); else \ |
| 239 |
{ int idx; for (idx = 0; idx < len; idx++) { DEBUG(5,("%04x ", (outbuf)[idx])); } } \ |
| 240 |
DEBUG(5,("\n")); } |
| 241 |
|
| 242 |
#define DBG_RW_PIVAL(charmode,string,depth,base,read,big_endian,inbuf,outbuf,len) \ |
| 243 |
{ RW_PIVAL(read,big_endian,inbuf,outbuf,len) \ |
| 244 |
DEBUG(5,("%s%04x %s: ", \ |
| 245 |
tab_depth(depth), base,string)); \ |
| 246 |
if (charmode) print_asc(5, (unsigned char*)(outbuf), 4*(len)); else \ |
| 247 |
{ int idx; for (idx = 0; idx < len; idx++) { DEBUG(5,("%08x ", (outbuf)[idx])); } } \ |
| 248 |
DEBUG(5,("\n")); } |
| 249 |
|
| 250 |
#define DBG_RW_CVAL(string,depth,base,read,inbuf,outbuf) \ |
| 251 |
{ RW_CVAL(read,inbuf,outbuf,0) \ |
| 252 |
DEBUG(5,("%s%04x %s: %02x\n", \ |
| 253 |
tab_depth(depth), base, string, outbuf)); } |
| 254 |
|
| 255 |
#define DBG_RW_SVAL(string,depth,base,read,big_endian,inbuf,outbuf) \ |
| 256 |
{ RW_SVAL(read,big_endian,inbuf,outbuf,0) \ |
| 257 |
DEBUG(5,("%s%04x %s: %04x\n", \ |
| 258 |
tab_depth(depth), base, string, outbuf)); } |
| 259 |
|
| 260 |
#define DBG_RW_IVAL(string,depth,base,read,big_endian,inbuf,outbuf) \ |
| 261 |
{ RW_IVAL(read,big_endian,inbuf,outbuf,0) \ |
| 262 |
DEBUG(5,("%s%04x %s: %08x\n", \ |
| 263 |
tab_depth(depth), base, string, outbuf)); } |
| 264 |
|
| 265 |
#endif /* _BYTEORDER_H */ |