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#include <netatalk/endian.h> 24 25/* 26 This file implements macros for machine independent short and 27 int manipulation 28 29Here 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 38sure. 39 40The distinction between 386 and other architectures is only there as 41an optimisation. You can take it out completely and it will make no 42difference. The routines (macros) in byteorder.h are totally byteorder 43independent. The 386 optimsation just takes advantage of the fact that 44the x86 processors don't care about alignment, so we don't have to 45align ints on int boundaries etc. If there are other processors out 46there that aren't alignment sensitive then you could also define 47CAREFUL_ALIGNMENT=0 on those processors as well. 48 49Ok, now to the macros themselves. I'll take a simple example, say we 50want to extract a 2 byte integer from a SMB packet and put it into a 51type called uint16 that is in the local machines byte order, and you 52want to do it with only the assumption that uint16 is _at_least_ 16 53bits long (this last condition is very important for architectures 54that don't have any int types that are 2 bytes long) 55 56You 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 62then to extract a uint16 value at offset 25 in a buffer you do this: 63 64char *buffer = foo_bar(); 65uint16 xx = SVAL(buffer,25); 66 67We are using the byteoder independence of the ANSI C bitshifts to do 68the work. A good optimising compiler should turn this into efficient 69code, especially if it happens to have the right byteorder :-) 70 71I know these macros can be made a bit tidier by removing some of the 72casts, but you need to look at byteorder.h as a whole to see the 73reasoning behind them. byteorder.h defines the following macros: 74 75SVAL(buf,pos) - extract a 2 byte SMB value 76IVAL(buf,pos) - extract a 4 byte SMB value 77SVALS(buf,pos) signed version of SVAL() 78IVALS(buf,pos) signed version of IVAL() 79 80SSVAL(buf,pos,val) - put a 2 byte SMB value into a buffer 81SIVAL(buf,pos,val) - put a 4 byte SMB value into a buffer 82SSVALS(buf,pos,val) - signed version of SSVAL() 83SIVALS(buf,pos,val) - signed version of SIVAL() 84 85RSVAL(buf,pos) - like SVAL() but for NMB byte ordering 86RSVALS(buf,pos) - like SVALS() but for NMB byte ordering 87RIVAL(buf,pos) - like IVAL() but for NMB byte ordering 88RIVALS(buf,pos) - like IVALS() but for NMB byte ordering 89RSSVAL(buf,pos,val) - like SSVAL() but for NMB ordering 90RSIVAL(buf,pos,val) - like SIVAL() but for NMB ordering 91RSIVALS(buf,pos,val) - like SIVALS() but for NMB ordering 92 93it also defines lots of intermediate macros, just ignore those :-) 94 95*/ 96 97#undef CAREFUL_ALIGNMENT 98 99/* we know that the 386 can handle misalignment and has the "right" 100 byteorder */ 101#ifdef __i386__ 102#define CAREFUL_ALIGNMENT 0 103#endif 104 105#ifndef CAREFUL_ALIGNMENT 106#define CAREFUL_ALIGNMENT 1 107#endif 108 109#define CVAL(buf,pos) ((unsigned)(((const unsigned char *)(buf))[pos])) 110#define CVAL_NC(buf,pos) (((unsigned char *)(buf))[pos]) /* Non-const version of CVAL */ 111#define PVAL(buf,pos) (CVAL(buf,pos)) 112#define SCVAL(buf,pos,val) (CVAL_NC(buf,pos) = (val)) 113 114 115#if CAREFUL_ALIGNMENT 116 117#if BYTE_ORDER==BIG_ENDIAN 118 119#define SVAL(buf,pos) (PVAL(buf,(pos)+1)|PVAL(buf,pos)<<8) 120#define IVAL(buf,pos) (SVAL(buf,pos)|SVAL(buf,(pos)+2)<<16) 121#define SSVALX(buf,pos,val) (CVAL_NC(buf,pos+1)=(unsigned char)((val)&0xFF),CVAL_NC(buf,pos)=(unsigned char)((val)>>8)) 122#define SIVALX(buf,pos,val) (SSVALX(buf,pos,val&0xFFFF),SSVALX(buf,pos+2,val>>16)) 123#define SVALS(buf,pos) ((int16)SVAL(buf,pos)) 124#define IVALS(buf,pos) ((int32_t)IVAL(buf,pos)) 125#define SSVAL(buf,pos,val) SSVALX((buf),(pos),((u_int16_t)(val))) 126#define SIVAL(buf,pos,val) SIVALX((buf),(pos),((u_int32_t)(val))) 127#define SSVALS(buf,pos,val) SSVALX((buf),(pos),((int16)(val))) 128#define SIVALS(buf,pos,val) SIVALX((buf),(pos),((int32_t)(val))) 129 130#else 131 132#define SVAL(buf,pos) (PVAL(buf,pos)|PVAL(buf,(pos)+1)<<8) 133#define IVAL(buf,pos) (SVAL(buf,pos)|SVAL(buf,(pos)+2)<<16) 134#define SSVALX(buf,pos,val) (CVAL_NC(buf,pos)=(unsigned char)((val)&0xFF),CVAL_NC(buf,pos+1)=(unsigned char)((val)>>8)) 135#define SIVALX(buf,pos,val) (SSVALX(buf,pos,val&0xFFFF),SSVALX(buf,pos+2,val>>16)) 136#define SVALS(buf,pos) ((int16)SVAL(buf,pos)) 137#define IVALS(buf,pos) ((int32_t)IVAL(buf,pos)) 138#define SSVAL(buf,pos,val) SSVALX((buf),(pos),((u_int16_t)(val))) 139#define SIVAL(buf,pos,val) SIVALX((buf),(pos),((u_int32_t)(val))) 140#define SSVALS(buf,pos,val) SSVALX((buf),(pos),((int16)(val))) 141#define SIVALS(buf,pos,val) SIVALX((buf),(pos),((int32_t)(val))) 142 143#endif 144 145#else /* CAREFUL_ALIGNMENT */ 146 147/* this handles things for architectures like the 386 that can handle 148 alignment errors */ 149/* 150 WARNING: This section is dependent on the length of int16 and int32 151 being correct 152*/ 153 154/* get single value from an SMB buffer */ 155#define SVAL(buf,pos) (*(const u_int16_t *)((const char *)(buf) + (pos))) 156#define SVAL_NC(buf,pos) (*(u_int16_t *)((char *)(buf) + (pos))) /* Non const version of above. */ 157#define IVAL(buf,pos) (*(const u_int32_t *)((const char *)(buf) + (pos))) 158#define IVAL_NC(buf,pos) (*(u_int32_t *)((char *)(buf) + (pos))) /* Non const version of above. */ 159#define SVALS(buf,pos) (*(const int16_t *)((const char *)(buf) + (pos))) 160#define SVALS_NC(buf,pos) (*(int16 *)((char *)(buf) + (pos))) /* Non const version of above. */ 161#define IVALS(buf,pos) (*(const int32_t *)((const char *)(buf) + (pos))) 162#define IVALS_NC(buf,pos) (*(int32_t *)((char *)(buf) + (pos))) /* Non const version of above. */ 163 164/* store single value in an SMB buffer */ 165#define SSVAL(buf,pos,val) SVAL_NC(buf,pos)=((u_int16_t)(val)) 166#define SIVAL(buf,pos,val) IVAL_NC(buf,pos)=((u_int32_t)(val)) 167#define SSVALS(buf,pos,val) SVALS_NC(buf,pos)=((int16)(val)) 168#define SIVALS(buf,pos,val) IVALS_NC(buf,pos)=((int32_t)(val)) 169 170#endif /* CAREFUL_ALIGNMENT */ 171 172/* now the reverse routines - these are used in nmb packets (mostly) */ 173#define SREV(x) ((((x)&0xFF)<<8) | (((x)>>8)&0xFF)) 174#define IREV(x) ((SREV(x)<<16) | (SREV((x)>>16))) 175 176#define RSVAL(buf,pos) SREV(SVAL(buf,pos)) 177#define RSVALS(buf,pos) SREV(SVALS(buf,pos)) 178#define RIVAL(buf,pos) IREV(IVAL(buf,pos)) 179#define RIVALS(buf,pos) IREV(IVALS(buf,pos)) 180#define RSSVAL(buf,pos,val) SSVAL(buf,pos,SREV(val)) 181#define RSSVALS(buf,pos,val) SSVALS(buf,pos,SREV(val)) 182#define RSIVAL(buf,pos,val) SIVAL(buf,pos,IREV(val)) 183#define RSIVALS(buf,pos,val) SIVALS(buf,pos,IREV(val)) 184 185/* Alignment macros. */ 186#define ALIGN4(p,base) ((p) + ((4 - (PTR_DIFF((p), (base)) & 3)) & 3)) 187#define ALIGN2(p,base) ((p) + ((2 - (PTR_DIFF((p), (base)) & 1)) & 1)) 188 189#endif /* _BYTEORDER_H */ 190