1/* 2 Unix SMB/CIFS implementation. 3 SMB Byte handling 4 Copyright (C) Andrew Tridgell 1992-1998 5 6 This program is free software; you can redistribute it and/or modify 7 it under the terms of the GNU General Public License as published by 8 the Free Software Foundation; either version 2 of the License, or 9 (at your option) any later version. 10 11 This program is distributed in the hope that it will be useful, 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU General Public License for more details. 15 16 You should have received a copy of the GNU General Public License 17 along with this program; if not, write to the Free Software 18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 19*/ 20 21#ifndef _BYTEORDER_H 22#define _BYTEORDER_H 23 24/* 25 This file implements macros for machine independent short and 26 int manipulation 27 28Here is a description of this file that I emailed to the samba list once: 29 30> I am confused about the way that byteorder.h works in Samba. I have 31> looked at it, and I would have thought that you might make a distinction 32> between LE and BE machines, but you only seem to distinguish between 386 33> and all other architectures. 34> 35> Can you give me a clue? 36 37sure. 38 39The distinction between 386 and other architectures is only there as 40an optimisation. You can take it out completely and it will make no 41difference. The routines (macros) in byteorder.h are totally byteorder 42independent. The 386 optimsation just takes advantage of the fact that 43the x86 processors don't care about alignment, so we don't have to 44align ints on int boundaries etc. If there are other processors out 45there that aren't alignment sensitive then you could also define 46CAREFUL_ALIGNMENT=0 on those processors as well. 47 48Ok, now to the macros themselves. I'll take a simple example, say we 49want to extract a 2 byte integer from a SMB packet and put it into a 50type called uint16 that is in the local machines byte order, and you 51want to do it with only the assumption that uint16 is _at_least_ 16 52bits long (this last condition is very important for architectures 53that don't have any int types that are 2 bytes long) 54 55You do this: 56 57#define CVAL(buf,pos) (((unsigned char *)(buf))[pos]) 58#define PVAL(buf,pos) ((unsigned)CVAL(buf,pos)) 59#define SVAL(buf,pos) (PVAL(buf,pos)|PVAL(buf,(pos)+1)<<8) 60 61then to extract a uint16 value at offset 25 in a buffer you do this: 62 63char *buffer = foo_bar(); 64uint16 xx = SVAL(buffer,25); 65 66We are using the byteoder independence of the ANSI C bitshifts to do 67the work. A good optimising compiler should turn this into efficient 68code, especially if it happens to have the right byteorder :-) 69 70I know these macros can be made a bit tidier by removing some of the 71casts, but you need to look at byteorder.h as a whole to see the 72reasoning behind them. byteorder.h defines the following macros: 73 74SVAL(buf,pos) - extract a 2 byte SMB value 75IVAL(buf,pos) - extract a 4 byte SMB value 76SVALS(buf,pos) signed version of SVAL() 77IVALS(buf,pos) signed version of IVAL() 78 79SSVAL(buf,pos,val) - put a 2 byte SMB value into a buffer 80SIVAL(buf,pos,val) - put a 4 byte SMB value into a buffer 81SSVALS(buf,pos,val) - signed version of SSVAL() 82SIVALS(buf,pos,val) - signed version of SIVAL() 83 84RSVAL(buf,pos) - like SVAL() but for NMB byte ordering 85RSVALS(buf,pos) - like SVALS() but for NMB byte ordering 86RIVAL(buf,pos) - like IVAL() but for NMB byte ordering 87RIVALS(buf,pos) - like IVALS() but for NMB byte ordering 88RSSVAL(buf,pos,val) - like SSVAL() but for NMB ordering 89RSIVAL(buf,pos,val) - like SIVAL() but for NMB ordering 90RSIVALS(buf,pos,val) - like SIVALS() but for NMB ordering 91 92it also defines lots of intermediate macros, just ignore those :-) 93 94*/ 95 96#undef CAREFUL_ALIGNMENT 97 98/* we know that the 386 can handle misalignment and has the "right" 99 byteorder */ 100#ifdef __i386__ 101#define CAREFUL_ALIGNMENT 0 102#endif 103 104#ifndef CAREFUL_ALIGNMENT 105#define CAREFUL_ALIGNMENT 1 106#endif 107 108#define CVAL(buf,pos) ((unsigned)(((const unsigned char *)(buf))[pos])) 109#define CVAL_NC(buf,pos) (((unsigned char *)(buf))[pos]) /* Non-const version of CVAL */ 110#define PVAL(buf,pos) ((unsigned)CVAL(buf,pos)) 111#define SCVAL(buf,pos,val) (CVAL_NC(buf,pos) = (val)) 112 113 114#if CAREFUL_ALIGNMENT 115 116#define SVAL(buf,pos) (PVAL(buf,pos)|PVAL(buf,(pos)+1)<<8) 117#define IVAL(buf,pos) (SVAL(buf,pos)|SVAL(buf,(pos)+2)<<16) 118#define SSVALX(buf,pos,val) (CVAL_NC(buf,pos)=(unsigned char)((val)&0xFF),CVAL_NC(buf,pos+1)=(unsigned char)((val)>>8)) 119#define SIVALX(buf,pos,val) (SSVALX(buf,pos,val&0xFFFF),SSVALX(buf,pos+2,val>>16)) 120#define SVALS(buf,pos) ((int16)SVAL(buf,pos)) 121#define IVALS(buf,pos) ((int32)IVAL(buf,pos)) 122#define SSVAL(buf,pos,val) SSVALX((buf),(pos),((uint16)(val))) 123#define SIVAL(buf,pos,val) SIVALX((buf),(pos),((uint32)(val))) 124#define SSVALS(buf,pos,val) SSVALX((buf),(pos),((int16)(val))) 125#define SIVALS(buf,pos,val) SIVALX((buf),(pos),((int32)(val))) 126 127#else /* CAREFUL_ALIGNMENT */ 128 129/* this handles things for architectures like the 386 that can handle 130 alignment errors */ 131/* 132 WARNING: This section is dependent on the length of int16 and int32 133 being correct 134*/ 135 136/* get single value from an SMB buffer */ 137#define SVAL(buf,pos) (*(const uint16 *)((const char *)(buf) + (pos))) 138#define SVAL_NC(buf,pos) (*(uint16 *)((char *)(buf) + (pos))) /* Non const version of above. */ 139#define IVAL(buf,pos) (*(const uint32 *)((const char *)(buf) + (pos))) 140#define IVAL_NC(buf,pos) (*(uint32 *)((char *)(buf) + (pos))) /* Non const version of above. */ 141#define SVALS(buf,pos) (*(const int16 *)((const char *)(buf) + (pos))) 142#define SVALS_NC(buf,pos) (*(int16 *)((char *)(buf) + (pos))) /* Non const version of above. */ 143#define IVALS(buf,pos) (*(const int32 *)((const char *)(buf) + (pos))) 144#define IVALS_NC(buf,pos) (*(int32 *)((char *)(buf) + (pos))) /* Non const version of above. */ 145 146/* store single value in an SMB buffer */ 147#define SSVAL(buf,pos,val) SVAL_NC(buf,pos)=((uint16)(val)) 148#define SIVAL(buf,pos,val) IVAL_NC(buf,pos)=((uint32)(val)) 149#define SSVALS(buf,pos,val) SVALS_NC(buf,pos)=((int16)(val)) 150#define SIVALS(buf,pos,val) IVALS_NC(buf,pos)=((int32)(val)) 151 152#endif /* CAREFUL_ALIGNMENT */ 153 154/* now the reverse routines - these are used in nmb packets (mostly) */ 155#define SREV(x) ((((x)&0xFF)<<8) | (((x)>>8)&0xFF)) 156#define IREV(x) ((SREV(x)<<16) | (SREV((x)>>16))) 157 158#define RSVAL(buf,pos) SREV(SVAL(buf,pos)) 159#define RSVALS(buf,pos) SREV(SVALS(buf,pos)) 160#define RIVAL(buf,pos) IREV(IVAL(buf,pos)) 161#define RIVALS(buf,pos) IREV(IVALS(buf,pos)) 162#define RSSVAL(buf,pos,val) SSVAL(buf,pos,SREV(val)) 163#define RSSVALS(buf,pos,val) SSVALS(buf,pos,SREV(val)) 164#define RSIVAL(buf,pos,val) SIVAL(buf,pos,IREV(val)) 165#define RSIVALS(buf,pos,val) SIVALS(buf,pos,IREV(val)) 166 167/* Alignment macros. */ 168#define ALIGN4(p,base) ((p) + ((4 - (PTR_DIFF((p), (base)) & 3)) & 3)) 169#define ALIGN2(p,base) ((p) + ((2 - (PTR_DIFF((p), (base)) & 1)) & 1)) 170 171#endif /* _BYTEORDER_H */ 172