preparing for release of 1.9.16alpha5
[samba.git] / source / include / byteorder.h
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    SMB Byte handling
5    Copyright (C) Andrew Tridgell 1992-1995
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 /*
23    This file implements macros for machine independent short and 
24    int manipulation
25 */
26
27 #undef CAREFUL_ALIGNMENT
28
29 /* we know that the 386 can handle misalignment and has the "right" 
30    byteorder */
31 #ifdef __i386__
32 #define CAREFUL_ALIGNMENT 0
33 #endif
34
35 #ifndef CAREFUL_ALIGNMENT
36 #define CAREFUL_ALIGNMENT 1
37 #endif
38
39 #define CVAL(buf,pos) (((unsigned char *)(buf))[pos])
40 #define PVAL(buf,pos) ((unsigned)CVAL(buf,pos))
41 #define SCVAL(buf,pos,val) (CVAL(buf,pos) = (val))
42
43
44 #if CAREFUL_ALIGNMENT
45 #define SVAL(buf,pos) (PVAL(buf,pos)|PVAL(buf,(pos)+1)<<8)
46 #define IVAL(buf,pos) (SVAL(buf,pos)|SVAL(buf,(pos)+2)<<16)
47 #define SSVALX(buf,pos,val) (CVAL(buf,pos)=(val)&0xFF,CVAL(buf,pos+1)=(val)>>8)
48 #define SIVALX(buf,pos,val) (SSVALX(buf,pos,val&0xFFFF),SSVALX(buf,pos+2,val>>16))
49 #define SVALS(buf,pos) ((int16)SVAL(buf,pos))
50 #define IVALS(buf,pos) ((int32)IVAL(buf,pos))
51 #define SSVAL(buf,pos,val) SSVALX((buf),(pos),((uint16)(val)))
52 #define SIVAL(buf,pos,val) SIVALX((buf),(pos),((uint32)(val)))
53 #define SSVALS(buf,pos,val) SSVALX((buf),(pos),((int16)(val)))
54 #define SIVALS(buf,pos,val) SIVALX((buf),(pos),((int32)(val)))
55 #else
56 /* this handles things for architectures like the 386 that can handle
57    alignment errors */
58 /*
59    WARNING: This section is dependent on the length of int16 and int32
60    being correct 
61 */
62 #define SVAL(buf,pos) (*(uint16 *)((char *)(buf) + (pos)))
63 #define IVAL(buf,pos) (*(uint32 *)((char *)(buf) + (pos)))
64 #define SVALS(buf,pos) (*(int16 *)((char *)(buf) + (pos)))
65 #define IVALS(buf,pos) (*(int32 *)((char *)(buf) + (pos)))
66 #define SSVAL(buf,pos,val) SVAL(buf,pos)=((uint16)(val))
67 #define SIVAL(buf,pos,val) IVAL(buf,pos)=((uint32)(val))
68 #define SSVALS(buf,pos,val) SVALS(buf,pos)=((int16)(val))
69 #define SIVALS(buf,pos,val) IVALS(buf,pos)=((int32)(val))
70 #endif
71
72
73 /* now the reverse routines - these are used in nmb packets (mostly) */
74 #define SREV(x) ((((x)&0xFF)<<8) | (((x)>>8)&0xFF))
75 #define IREV(x) ((SREV(x)<<16) | (SREV((x)>>16)))
76
77 #define RSVAL(buf,pos) SREV(SVAL(buf,pos))
78 #define RIVAL(buf,pos) IREV(IVAL(buf,pos))
79 #define RSSVAL(buf,pos,val) SSVAL(buf,pos,SREV(val))
80 #define RSIVAL(buf,pos,val) SIVAL(buf,pos,IREV(val))