2 * Simple byteorder handling.
4 * Copyright (C) 1992-1995 Andrew Tridgell
5 * Copyright (C) 2007-2020 Wayne Davison
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 3 of the License, or
10 * (at your option) any later version.
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.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, visit the http://fsf.org website.
21 #undef CAREFUL_ALIGNMENT
23 /* We know that the x86 can handle misalignment and has the same
24 * byte order (LSB-first) as the 32-bit numbers we transmit. */
25 #if defined __i386__ || defined __i486__ || defined __i586__ || defined __i686__ || __amd64
26 #define CAREFUL_ALIGNMENT 0
29 #ifndef CAREFUL_ALIGNMENT
30 #define CAREFUL_ALIGNMENT 1
33 #define CVAL(buf,pos) (((unsigned char *)(buf))[pos])
34 #define UVAL(buf,pos) ((uint32)CVAL(buf,pos))
39 IVALu(const uchar
*buf
, int pos
)
42 | UVAL(buf
, pos
+ 1) << 8
43 | UVAL(buf
, pos
+ 2) << 16
44 | UVAL(buf
, pos
+ 3) << 24;
48 SIVALu(uchar
*buf
, int pos
, uint32 val
)
51 CVAL(buf
, pos
+ 1) = val
>> 8;
52 CVAL(buf
, pos
+ 2) = val
>> 16;
53 CVAL(buf
, pos
+ 3) = val
>> 24;
57 IVAL64(const char *buf
, int pos
)
59 return IVALu((uchar
*)buf
, pos
) | (int64
)IVALu((uchar
*)buf
, pos
+ 4) << 32;
63 SIVAL64(char *buf
, int pos
, int64 val
)
65 SIVALu((uchar
*)buf
, pos
, val
);
66 SIVALu((uchar
*)buf
, pos
+ 4, val
>> 32);
69 #else /* !CAREFUL_ALIGNMENT */
71 /* This handles things for architectures like the 386 that can handle alignment errors.
72 * WARNING: This section is dependent on the length of an int32 (and thus a uint32)
73 * being correct (4 bytes)! Set CAREFUL_ALIGNMENT if it is not. */
76 IVALu(const uchar
*buf
, int pos
)
87 SIVALu(uchar
*buf
, int pos
, uint32 val
)
98 IVAL64(const char *buf
, int pos
)
109 SIVAL64(char *buf
, int pos
, int64 val
)
119 #endif /* !CAREFUL_ALIGNMENT */
122 IVAL(const char *buf
, int pos
)
124 return IVALu((uchar
*)buf
, pos
);
128 SIVAL(char *buf
, int pos
, uint32 val
)
130 SIVALu((uchar
*)buf
, pos
, val
);