summaryrefslogtreecommitdiffstats
path: root/lib/util/bytearray.h
blob: 0af8a82c2c1d474ded45a91ea2d99e54125311cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
 * Macros for handling integer types in byte arrays
 *
 * This file is originally from the libssh.org project
 *
 * Copyright (c) 2018 Andreas Schneider <asn@cryptomilk.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */
#ifndef _BYTEARRAY_H
#define _BYTEARRAY_H

#define _DATA_BYTE_CONST(data, pos) \
    ((uint8_t)(((const uint8_t *)(data))[(pos)]))

#define _DATA_BYTE(data, pos) \
    (((uint8_t *)(data))[(pos)])

/*
 * These macros pull or push integer values from byte arrays stored in
 * little-endian byte order.
 */
#define PULL_LE_U8(data, pos) \
    (_DATA_BYTE_CONST(data, pos))
#define PULL_LE_I8(data, pos) \
    (int8_t)PULL_LE_U8(data, pos)

#define PULL_LE_U16(data, pos) \
    ((uint16_t)PULL_LE_U8(data, pos) | ((uint16_t)(PULL_LE_U8(data, (pos) + 1))) << 8)
#define PULL_LE_I16(data, pos) \
    (int16_t)PULL_LE_U16(data, pos)

#define PULL_LE_U32(data, pos) \
    ((uint32_t)(PULL_LE_U16(data, pos) | ((uint32_t)PULL_LE_U16(data, (pos) + 2)) << 16))
#define PULL_LE_I32(data, pos) \
    (int32_t)PULL_LE_U32(data, pos)

#define PULL_LE_U64(data, pos) \
    ((uint64_t)(PULL_LE_U32(data, pos) | ((uint64_t)PULL_LE_U32(data, (pos) + 4)) << 32))
#define PULL_LE_I64(data, pos) \
    (int64_t)PULL_LE_U64(data, pos)


#define PUSH_LE_U8(data, pos, val) \
    (_DATA_BYTE(data, pos) = ((uint8_t)(val)))
#define PUSH_LE_I8(data, pos, val) \
    PUSH_LE_U8(data, pos, val)

#define PUSH_LE_U16(data, pos, val) \
    (PUSH_LE_U8(data, pos, (uint16_t)(val) & 0xff), PUSH_LE_U8(data, (pos) + 1, (uint16_t)(val) >> 8))
#define PUSH_LE_I16(data, pos, val) \
    PUSH_LE_U16(data, pos, val)

#define PUSH_LE_U32(data, pos, val) \
    (PUSH_LE_U16(data, pos, (uint32_t)(val) & 0xffff), PUSH_LE_U16(data, (pos) + 2, (uint32_t)(val) >> 16))
#define PUSH_LE_I32(data, pos, val) \
    PUSH_LE_U32(data, pos, val)

#define PUSH_LE_U64(data, pos, val) \
    (PUSH_LE_U32(data, pos, (uint64_t)(val) & 0xffffffff), PUSH_LE_U32(data, (pos) + 4, (uint64_t)(val) >> 32))
#define PUSH_LE_I64(data, pos, val) \
    PUSH_LE_U64(data, pos, val)



/*
 * These macros pull or push integer values from byte arrays stored in
 * big-endian byte order (network byte order).
 */
#define PULL_BE_U8(data, pos) \
    (_DATA_BYTE_CONST(data, pos))
#define PULL_BE_I8(data, pos) \
    (int8_t)PULL_BE_U8(data, pos)

#define PULL_BE_U16(data, pos) \
    ((((uint16_t)(PULL_BE_U8(data, pos))) << 8) | (uint16_t)PULL_BE_U8(data, (pos) + 1))
#define PULL_BE_I16(data, pos) \
    (int16_t)PULL_BE_U16(data, pos)

#define PULL_BE_U32(data, pos) \
    ((((uint32_t)PULL_BE_U16(data, pos)) << 16) | (uint32_t)(PULL_BE_U16(data, (pos) + 2)))
#define PULL_BE_I32(data, pos) \
    (int32_t)PULL_BE_U32(data, pos)

#define PULL_BE_U64(data, pos) \
    ((((uint64_t)PULL_BE_U32(data, pos)) << 32) | (uint64_t)(PULL_BE_U32(data, (pos) + 4)))
#define PULL_BE_I64(data, pos) \
    (int64_t)PULL_BE_U64(data, pos)



#define PUSH_BE_U8(data, pos, val) \
    (_DATA_BYTE(data, pos) = ((uint8_t)(val)))
#define PUSH_BE_I8(data, pos, val) \
    PUSH_BE_U8(data, pos, val)

#define PUSH_BE_U16(data, pos, val) \
    (PUSH_BE_U8(data, pos, ((uint16_t)(val)) >> 8), PUSH_BE_U8(data, (pos) + 1, (uint16_t)(val) & 0xff))
#define PUSH_BE_I16(data, pos, val) \
    PUSH_BE_U16(data, pos, val)

#define PUSH_BE_U32(data, pos, val) \
    (PUSH_BE_U16(data, pos, (uint32_t)(val) >> 16), PUSH_BE_U16(data, (pos) + 2, (uint32_t)(val) & 0xffff))
#define PUSH_BE_I32(data, pos, val) \
    PUSH_BE_U32(data, pos, val)

#define PUSH_BE_U64(data, pos, val) \
    (PUSH_BE_U32(data, pos, (uint64_t)(val) >> 32), PUSH_BE_U32(data, (pos) + 4, (uint64_t)(val) & 0xffffffff))
#define PUSH_BE_I64(data, pos, val) \
    PUSH_BE_U64(data, pos, val)

#endif /* _BYTEARRAY_H */