jlibtool depends on HEADERS
[freeradius.git] / src / modules / rlm_smb / byteorder.h
1 /*
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    SMB Byte handling
5    Copyright (C) Andrew Tridgell 1992-1995
6    Copyright 2006 The FreeRADIUS server project
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 /*
24    This file implements macros for machine independent short and
25    int manipulation
26 */
27
28 #include <freeradius-devel/ident.h>
29 RCSIDH(byteorder_h, "$Id$")
30
31 #undef CAREFUL_ALIGNMENT
32
33 /* we know that the 386 can handle misalignment and has the "right"
34    byteorder */
35 #ifdef __i386__
36 #define CAREFUL_ALIGNMENT 0
37 #endif
38
39 #ifndef CAREFUL_ALIGNMENT
40 #define CAREFUL_ALIGNMENT 1
41 #endif
42
43 #define CVAL(buf,pos) (((unsigned char *)(buf))[pos])
44 #define PVAL(buf,pos) ((unsigned)CVAL(buf,pos))
45 #define SCVAL(buf,pos,val) (CVAL(buf,pos) = (val))
46
47
48 #if CAREFUL_ALIGNMENT
49 #define SVAL(buf,pos) (PVAL(buf,pos)|PVAL(buf,(pos)+1)<<8)
50 #define IVAL(buf,pos) (SVAL(buf,pos)|SVAL(buf,(pos)+2)<<16)
51 #define SSVALX(buf,pos,val) (CVAL(buf,pos)=(val)&0xFF,CVAL(buf,pos+1)=(val)>>8)
52 #define SIVALX(buf,pos,val) (SSVALX(buf,pos,val&0xFFFF),SSVALX(buf,pos+2,val>>16))
53 #define SVALS(buf,pos) ((int16)SVAL(buf,pos))
54 #define IVALS(buf,pos) ((int32)IVAL(buf,pos))
55 #define SSVAL(buf,pos,val) SSVALX((buf),(pos),((uint16)(val)))
56 #define SIVAL(buf,pos,val) SIVALX((buf),(pos),((uint32)(val)))
57 #define SSVALS(buf,pos,val) SSVALX((buf),(pos),((int16)(val)))
58 #define SIVALS(buf,pos,val) SIVALX((buf),(pos),((int32)(val)))
59 #else
60 /* this handles things for architectures like the 386 that can handle
61    alignment errors */
62 /*
63    WARNING: This section is dependent on the length of int16 and int32
64    being correct
65 */
66 #define SVAL(buf,pos) (*(uint16 *)((char *)(buf) + (pos)))
67 #define IVAL(buf,pos) (*(uint32 *)((char *)(buf) + (pos)))
68 #define SVALS(buf,pos) (*(int16 *)((char *)(buf) + (pos)))
69 #define IVALS(buf,pos) (*(int32 *)((char *)(buf) + (pos)))
70 #define SSVAL(buf,pos,val) SVAL(buf,pos)=((uint16)(val))
71 #define SIVAL(buf,pos,val) IVAL(buf,pos)=((uint32)(val))
72 #define SSVALS(buf,pos,val) SVALS(buf,pos)=((int16)(val))
73 #define SIVALS(buf,pos,val) IVALS(buf,pos)=((int32)(val))
74 #endif
75
76
77 /* now the reverse routines - these are used in nmb packets (mostly) */
78 #define SREV(x) ((((x)&0xFF)<<8) | (((x)>>8)&0xFF))
79 #define IREV(x) ((SREV(x)<<16) | (SREV((x)>>16)))
80
81 #define RSVAL(buf,pos) SREV(SVAL(buf,pos))
82 #define RIVAL(buf,pos) IREV(IVAL(buf,pos))
83 #define RSSVAL(buf,pos,val) SSVAL(buf,pos,SREV(val))
84 #define RSIVAL(buf,pos,val) SIVAL(buf,pos,IREV(val))