Martin gave his approval to use GPLv3 with this code.
[rsync/rsync.git] / byteorder.h
CommitLineData
0f78b815
WD
1/*
2 * Simple byteorder handling.
3 *
4 * Copyright (C) 1992-1995 Andrew Tridgell
ba2133d6 5 * Copyright (C) 2007 Wayne Davison
0f78b815
WD
6 *
7 * This program is free software; you can redistribute it and/or modify
4fd842f9 8 * it under the terms of the GNU General Public License version 3 as
ba2133d6 9 * published by the Free Software Foundation.
0f78b815
WD
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 *
e7c67065 16 * You should have received a copy of the GNU General Public License along
4fd842f9 17 * with this program; if not, visit the http://fsf.org website.
0f78b815 18 */
c627d613
AT
19
20#undef CAREFUL_ALIGNMENT
21
36e6594d
WD
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. */
c627d613
AT
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])
36e6594d 33#define UVAL(buf,pos) ((uint32)CVAL(buf,pos))
c627d613
AT
34#define SCVAL(buf,pos,val) (CVAL(buf,pos) = (val))
35
c627d613 36#if CAREFUL_ALIGNMENT
36e6594d
WD
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)
c627d613
AT
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