Commit | Line | Data |
---|---|---|
c215a4bc IJ |
1 | /* |
2 | * This file is part of secnet. | |
3 | * See README for full list of copyright holders. | |
4 | * | |
5 | * secnet is free software; you can redistribute it and/or modify it | |
6 | * under the terms of the GNU General Public License as published by | |
9c6a8729 | 7 | * the Free Software Foundation; either version 3 of the License, or |
c215a4bc IJ |
8 | * (at your option) any later version. |
9 | * | |
10 | * secnet is distributed in the hope that it will be useful, but | |
11 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | * General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * version 3 along with secnet; if not, see | |
17 | * https://www.gnu.org/licenses/gpl.html. | |
18 | */ | |
19 | ||
59635212 SE |
20 | #ifndef unaligned_h |
21 | #define unaligned_h | |
22 | ||
6fbd4b99 | 23 | #include <stdint.h> |
993db2a6 | 24 | #include "util.h" |
6fbd4b99 | 25 | |
59635212 SE |
26 | /* Parts of the secnet key-exchange protocol require access to |
27 | unaligned big-endian quantities in buffers. These macros provide | |
28 | convenient access, even on architectures that don't support unaligned | |
29 | accesses. */ | |
30 | ||
31 | #define put_uint32(a,v) do { (a)[0]=(v)>>24; (a)[1]=((v)&0xff0000)>>16; \ | |
32 | (a)[2]=((v)&0xff00)>>8; (a)[3]=(v)&0xff; } while(0) | |
33 | ||
a9aa5d13 MW |
34 | #define put_uint32_le(a, v) do { (a)[0]=(v)&0xff; (a)[1]=((v)&0xff00)>>8; \ |
35 | (a)[2]=((v)&0xff0000)>>16; (a)[3]=(v)>>24; } while(0) | |
36 | ||
59635212 SE |
37 | #define put_uint16(a,v) do {(a)[0]=((v)&0xff00)>>8; (a)[1]=(v)&0xff;} while(0) |
38 | ||
993db2a6 IJ |
39 | #define put_uint8(a,v) do {(a)[0]=((v)&0xff);} while(0) |
40 | ||
6fbd4b99 IJ |
41 | #define get_uint32(a) \ |
42 | (((uint32_t)(a)[0]<<24) | ((uint32_t)(a)[1]<<16) | \ | |
43 | ((uint32_t)(a)[2]<<8) | (uint32_t)(a)[3]) | |
59635212 | 44 | |
a9aa5d13 MW |
45 | #define get_uint32_le(a) \ |
46 | (((uint32_t)(a)[0]) | ((uint32_t)(a)[1]<<8) | \ | |
47 | ((uint32_t)(a)[2]<<16) | (uint32_t)(a)[3]<<24) | |
48 | ||
6fbd4b99 | 49 | #define get_uint16(a) (((uint16_t)(a)[0]<<8)|(uint16_t)(a)[1]) |
59635212 | 50 | |
993db2a6 IJ |
51 | #define get_uint8(a) (((uint8_t)(a)[0])) |
52 | ||
53 | #define UNALIGNED_DEF_FORTYPE(type,appre) \ | |
54 | static inline void buf_##appre##_##type(struct buffer_if *buf, type##_t v) \ | |
55 | { \ | |
56 | uint8_t *c=buf_##appre(buf,sizeof(type##_t)); \ | |
57 | put_##type(c,v); \ | |
58 | } \ | |
59 | static inline type##_t buf_un##appre##_##type(struct buffer_if *buf) \ | |
60 | { \ | |
61 | const uint8_t *c=buf_un##appre(buf,sizeof(type##_t)); \ | |
62 | return get_##type(c); \ | |
63 | } | |
64 | ||
65 | UNALIGNED_DEF_FORTYPE(uint32,append) | |
66 | UNALIGNED_DEF_FORTYPE(uint16,append) | |
67 | UNALIGNED_DEF_FORTYPE(uint8,append) | |
68 | UNALIGNED_DEF_FORTYPE(uint32,prepend) | |
69 | UNALIGNED_DEF_FORTYPE(uint16,prepend) | |
70 | UNALIGNED_DEF_FORTYPE(uint8,prepend) | |
59635212 SE |
71 | |
72 | #endif /* unaligned_h */ |