32d2715245dd7ea3dbfc5aee2cb9c68082d93a66
[rsync/rsync.git] / byteorder.h
1 /*
2  * Simple byteorder handling.
3  *
4  * Copyright (C) 1992-1995 Andrew Tridgell
5  * Copyright (C) 2007 Wayne Davison
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 version 3 as
9  * published by the Free Software Foundation.
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 along
17  * with this program; if not, visit the http://fsf.org website.
18  */
19
20 #undef CAREFUL_ALIGNMENT
21
22 /* We know that the x86 can handle misalignment and has the same
23  * byte order (LSB-first) as the 32-bit numbers we transmit. */
24 #ifdef __i386__
25 #define CAREFUL_ALIGNMENT 0
26 #endif
27
28 #ifndef CAREFUL_ALIGNMENT
29 #define CAREFUL_ALIGNMENT 1
30 #endif
31
32 #define CVAL(buf,pos) (((unsigned char *)(buf))[pos])
33 #define UVAL(buf,pos) ((uint32)CVAL(buf,pos))
34 #define SCVAL(buf,pos,val) (CVAL(buf,pos) = (val))
35
36 #if CAREFUL_ALIGNMENT
37 #define PVAL(buf,pos) (UVAL(buf,pos)|UVAL(buf,(pos)+1)<<8)
38 #define IVAL(buf,pos) (PVAL(buf,pos)|PVAL(buf,(pos)+2)<<16)
39 #define SSVALX(buf,pos,val) (CVAL(buf,pos)=(val)&0xFF,CVAL(buf,pos+1)=(val)>>8)
40 #define SIVALX(buf,pos,val) (SSVALX(buf,pos,val&0xFFFF),SSVALX(buf,pos+2,val>>16))
41 #define SIVAL(buf,pos,val) SIVALX((buf),(pos),((uint32)(val)))
42 #else
43 /* this handles things for architectures like the 386 that can handle
44    alignment errors */
45 /*
46    WARNING: This section is dependent on the length of int32
47    being correct. set CAREFUL_ALIGNMENT if it is not.
48 */
49 #define IVAL(buf,pos) (*(uint32 *)((char *)(buf) + (pos)))
50 #define SIVAL(buf,pos,val) IVAL(buf,pos)=((uint32)(val))
51 #endif