Fix.
[shishi.git] / crypto / macros.h
blob89400c547b6c5e5605df988f50b4bdbfa8e94e53
1 /* macros.h
3 */
5 /* nettle, low-level cryptographics library
7 * Copyright (C) 2001 Niels Möller
8 *
9 * The nettle library is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or (at your
12 * option) any later version.
14 * The nettle library is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17 * License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with the nettle library; see the file COPYING.LIB. If not, write to
21 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
22 * MA 02111-1307, USA.
25 #ifndef NETTLE_MACROS_H_INCLUDED
26 #define NETTLE_MACROS_H_INCLUDED
28 /* Reads a 32-bit integer, in network, big-endian, byte order */
29 #define READ_UINT32(p) \
30 ( (((uint32_t) (p)[0]) << 24) \
31 | (((uint32_t) (p)[1]) << 16) \
32 | (((uint32_t) (p)[2]) << 8) \
33 | ((uint32_t) (p)[3]))
35 #define WRITE_UINT32(p, i) \
36 do { \
37 (p)[0] = ((i) >> 24) & 0xff; \
38 (p)[1] = ((i) >> 16) & 0xff; \
39 (p)[2] = ((i) >> 8) & 0xff; \
40 (p)[3] = (i) & 0xff; \
41 } while(0)
43 /* Analogous macros, for 24 and 16 bit numbers */
44 #define READ_UINT24(p) \
45 ( (((uint32_t) (p)[0]) << 16) \
46 | (((uint32_t) (p)[1]) << 8) \
47 | ((uint32_t) (p)[2]))
49 #define WRITE_UINT24(p, i) \
50 do { \
51 (p)[0] = ((i) >> 16) & 0xff; \
52 (p)[1] = ((i) >> 8) & 0xff; \
53 (p)[2] = (i) & 0xff; \
54 } while(0)
56 #define READ_UINT16(p) \
57 ( (((uint32_t) (p)[0]) << 8) \
58 | ((uint32_t) (p)[1]))
60 #define WRITE_UINT16(p, i) \
61 do { \
62 (p)[0] = ((i) >> 8) & 0xff; \
63 (p)[1] = (i) & 0xff; \
64 } while(0)
66 /* And the other, little-endian, byteorder */
67 #define LE_READ_UINT32(p) \
68 ( (((uint32_t) (p)[3]) << 24) \
69 | (((uint32_t) (p)[2]) << 16) \
70 | (((uint32_t) (p)[1]) << 8) \
71 | ((uint32_t) (p)[0]))
73 #define LE_WRITE_UINT32(p, i) \
74 do { \
75 (p)[3] = ((i) >> 24) & 0xff; \
76 (p)[2] = ((i) >> 16) & 0xff; \
77 (p)[1] = ((i) >> 8) & 0xff; \
78 (p)[0] = (i) & 0xff; \
79 } while(0)
81 /* Macro to make it easier to loop over several blocks. */
82 #define FOR_BLOCKS(length, dst, src, blocksize) \
83 assert( !((length) % (blocksize))); \
84 for (; (length); ((length) -= (blocksize), \
85 (dst) += (blocksize), \
86 (src) += (blocksize)) )
88 #endif /* NETTLE_MACROS_H_INCLUDED */