Initial commit
[keepassx] / src / crypto / yarrow_macros.h
1 /* macros.h
2  *
3  */
4
5 /* nettle, low-level cryptographics library
6  *
7  * Copyright (C) 2001 Niels Müler
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.
13  * 
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.
18  * 
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.
23  */
24
25 #ifndef NETTLE_MACROS_H_INCLUDED
26 #define NETTLE_MACROS_H_INCLUDED
27
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]))
34
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)
42
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]))
48
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)
55
56 #define READ_UINT16(p)                          \
57 (  (((uint32_t) (p)[0]) << 8)                   \
58  |  ((uint32_t) (p)[1]))
59
60 #define WRITE_UINT16(p, i)                      \
61 do {                                            \
62   (p)[0] = ((i) >> 8) & 0xff;                   \
63   (p)[1] = (i) & 0xff;                          \
64 } while(0)
65
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]))
72
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)
80
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)) )
87
88 #endif /* NETTLE_MACROS_H_INCLUDED */