release 0.92.3
[cntlm.git] / swap.h
blobfc748d0d627503e537447640af5d24b77b82472d
1 /*
2 * These are little/big endian routines for the main module of CNTLM
4 * CNTLM is free software; you can redistribute it and/or modify it under the
5 * terms of the GNU General Public License as published by the Free Software
6 * Foundation; either version 2 of the License, or (at your option) any later
7 * version.
9 * CNTLM is distributed in the hope that it will be useful, but WITHOUT ANY
10 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12 * details.
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
16 * St, Fifth Floor, Boston, MA 02110-1301, USA.
18 * Copyright (c) 2007 David Kubicek
22 #ifndef _SWAP_H
23 #define _SWAP_H
25 #include <stdint.h>
27 #include "config/config.h"
29 #define swap16(x) \
30 ((uint16_t)( \
31 (((uint16_t)(x) & (uint16_t)0x00ffU) << 8) | \
32 (((uint16_t)(x) & (uint16_t)0xff00U) >> 8) ))
34 #define swap32(x) \
35 ((uint32_t)( \
36 (((uint32_t)(x) & (uint32_t)0x000000ffUL) << 24) | \
37 (((uint32_t)(x) & (uint32_t)0x0000ff00UL) << 8) | \
38 (((uint32_t)(x) & (uint32_t)0x00ff0000UL) >> 8) | \
39 (((uint32_t)(x) & (uint32_t)0xff000000UL) >> 24) ))
41 #define swap64(x) \
42 ((uint64_t)( \
43 (((uint64_t)(x) & (uint64_t)0xff00000000000000ULL) >> 56) | \
44 (((uint64_t)(x) & (uint64_t)0x00ff000000000000ULL) >> 40) | \
45 (((uint64_t)(x) & (uint64_t)0x0000ff0000000000ULL) >> 24) | \
46 (((uint64_t)(x) & (uint64_t)0x000000ff00000000ULL) >> 8) | \
47 (((uint64_t)(x) & (uint64_t)0x00000000ff000000ULL) << 8) | \
48 (((uint64_t)(x) & (uint64_t)0x0000000000ff0000ULL) << 24) | \
49 (((uint64_t)(x) & (uint64_t)0x000000000000ff00ULL) << 40) | \
50 (((uint64_t)(x) & (uint64_t)0x00000000000000ffULL) << 56) ))
52 #if config_endian == 0
53 # define U16LE(x) swap16(x)
54 # define U32LE(x) swap32(x)
55 # define U64LE(x) swap64(x)
56 # define U16BE(x) (x)
57 # define U32BE(x) (x)
58 # define U64BE(x) (x)
59 #else
60 # define U16LE(x) (x)
61 # define U32LE(x) (x)
62 # define U64LE(x) (x)
63 # define U16BE(x) swap16(x)
64 # define U32BE(x) swap32(x)
65 # define U64BE(x) swap64(x)
66 #endif
68 #endif /* _SWAP_H */