libssh: /build/libssh/src/libssh-0.9.0/include/libssh/bytearray.h Source File

libssh 0.9.0 The SSH library

bytearray.h
1 /*
2 * This file is part of the SSH Library
3 *
4 * Copyright (c) 2018 Andreas Schneider <asn@cryptomilk.org>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20 #ifndef _BYTEARRAY_H
21 #define _BYTEARRAY_H
22
23 #define _DATA_BYTE_CONST(data, pos) \
24 ((uint8_t)(((const uint8_t *)(data))[(pos)]))
25
26 #define _DATA_BYTE(data, pos) \
27 (((uint8_t *)(data))[(pos)])
28
29 /*
30 * These macros pull or push integer values from byte arrays stored in
31 * little-endian byte order.
32 */
33 #define PULL_LE_U8(data, pos) \
34 (_DATA_BYTE_CONST(data, pos))
35
36 #define PULL_LE_U16(data, pos) \
37 ((uint16_t)PULL_LE_U8(data, pos) | ((uint16_t)(PULL_LE_U8(data, (pos) + 1))) << 8)
38
39 #define PULL_LE_U32(data, pos) \
40 ((uint32_t)(PULL_LE_U16(data, pos) | ((uint32_t)PULL_LE_U16(data, (pos) + 2)) << 16))
41
42 #define PULL_LE_U64(data, pos) \
43 ((uint64_t)(PULL_LE_U32(data, pos) | ((uint64_t)PULL_LE_U32(data, (pos) + 4)) << 32))
44
45
46 #define PUSH_LE_U8(data, pos, val) \
47 (_DATA_BYTE(data, pos) = ((uint8_t)(val)))
48
49 #define PUSH_LE_U16(data, pos, val) \
50 (PUSH_LE_U8((data), (pos), (uint8_t)((uint16_t)(val) & 0xff)), PUSH_LE_U8((data), (pos) + 1, (uint8_t)((uint16_t)(val) >> 8)))
51
52 #define PUSH_LE_U32(data, pos, val) \
53 (PUSH_LE_U16((data), (pos), (uint16_t)((uint32_t)(val) & 0xffff)), PUSH_LE_U16((data), (pos) + 2, (uint16_t)((uint32_t)(val) >> 16)))
54
55 #define PUSH_LE_U64(data, pos, val) \
56 (PUSH_LE_U32((data), (pos), (uint32_t)((uint64_t)(val) & 0xffffffff)), PUSH_LE_U32((data), (pos) + 4, (uint32_t)((uint64_t)(val) >> 32)))
57
58
59
60 /*
61 * These macros pull or push integer values from byte arrays stored in
62 * big-endian byte order (network byte order).
63 */
64 #define PULL_BE_U8(data, pos) \
65 (_DATA_BYTE_CONST(data, pos))
66
67 #define PULL_BE_U16(data, pos) \
68 ((((uint16_t)(PULL_BE_U8(data, pos))) << 8) | (uint16_t)PULL_BE_U8(data, (pos) + 1))
69
70 #define PULL_BE_U32(data, pos) \
71 ((((uint32_t)PULL_BE_U16(data, pos)) << 16) | (uint32_t)(PULL_BE_U16(data, (pos) + 2)))
72
73 #define PULL_BE_U64(data, pos) \
74 ((((uint64_t)PULL_BE_U32(data, pos)) << 32) | (uint64_t)(PULL_BE_U32(data, (pos) + 4)))
75
76
77
78 #define PUSH_BE_U8(data, pos, val) \
79 (_DATA_BYTE(data, pos) = ((uint8_t)(val)))
80
81 #define PUSH_BE_U16(data, pos, val) \
82 (PUSH_BE_U8((data), (pos), (uint8_t)(((uint16_t)(val)) >> 8)), PUSH_BE_U8((data), (pos) + 1, (uint8_t)((val) & 0xff)))
83
84 #define PUSH_BE_U32(data, pos, val) \
85 (PUSH_BE_U16((data), (pos), (uint16_t)(((uint32_t)(val)) >> 16)), PUSH_BE_U16((data), (pos) + 2, (uint16_t)((val) & 0xffff)))
86
87 #define PUSH_BE_U64(data, pos, val) \
88 (PUSH_BE_U32((data), (pos), (uint32_t)(((uint64_t)(val)) >> 32)), PUSH_BE_U32((data), (pos) + 4, (uint32_t)((val) & 0xffffffff)))
89
90 #endif /* _BYTEARRAY_H */