s3: VFS: solarisacl: Add solarisacl_sys_acl_delete_def_fd().
[Samba.git] / lib / util / bytearray.h
blobecab90b067d4c490b240d478b56d05551d33420d
1 /*
2 * Macros for handling integer types in byte arrays
4 * This file is originally from the libssh.org project
6 * Copyright (c) 2018 Andreas Schneider <asn@cryptomilk.org>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #ifndef _BYTEARRAY_H
23 #define _BYTEARRAY_H
25 #define _DATA_BYTE_CONST(data, pos) \
26 ((uint8_t)(((const uint8_t *)(data))[(pos)]))
28 #define _DATA_BYTE(data, pos) \
29 (((uint8_t *)(data))[(pos)])
32 * These macros pull or push integer values from byte arrays stored in
33 * little-endian byte order.
35 #define PULL_LE_U8(data, pos) \
36 (_DATA_BYTE_CONST(data, pos))
37 #define PULL_LE_I8(data, pos) \
38 (int8_t)PULL_LE_U8(data, pos)
40 #define PULL_LE_U16(data, pos) \
41 ((uint16_t)PULL_LE_U8(data, pos) | ((uint16_t)(PULL_LE_U8(data, (pos) + 1))) << 8)
42 #define PULL_LE_I16(data, pos) \
43 (int16_t)PULL_LE_U16(data, pos)
45 #define PULL_LE_U32(data, pos) \
46 ((uint32_t)(PULL_LE_U16(data, pos) | ((uint32_t)PULL_LE_U16(data, (pos) + 2)) << 16))
47 #define PULL_LE_I32(data, pos) \
48 (int32_t)PULL_LE_U32(data, pos)
50 #define PULL_LE_U64(data, pos) \
51 ((uint64_t)(PULL_LE_U32(data, pos) | ((uint64_t)PULL_LE_U32(data, (pos) + 4)) << 32))
52 #define PULL_LE_I64(data, pos) \
53 (int64_t)PULL_LE_U64(data, pos)
56 #define PUSH_LE_U8(data, pos, val) \
57 (_DATA_BYTE(data, pos) = ((uint8_t)(val)))
58 #define PUSH_LE_I8(data, pos, val) \
59 PUSH_LE_U8(data, pos, val)
61 #define PUSH_LE_U16(data, pos, val) \
62 (PUSH_LE_U8((data), (pos), (uint8_t)((uint16_t)(val) & 0xff)), PUSH_LE_U8((data), (pos) + 1, (uint8_t)((uint16_t)(val) >> 8)))
63 #define PUSH_LE_I16(data, pos, val) \
64 PUSH_LE_U16(data, pos, val)
66 #define PUSH_LE_U32(data, pos, val) \
67 (PUSH_LE_U16((data), (pos), (uint16_t)((uint32_t)(val) & 0xffff)), PUSH_LE_U16((data), (pos) + 2, (uint16_t)((uint32_t)(val) >> 16)))
68 #define PUSH_LE_I32(data, pos, val) \
69 PUSH_LE_U32(data, pos, val)
71 #define PUSH_LE_U64(data, pos, val) \
72 (PUSH_LE_U32((data), (pos), (uint32_t)((uint64_t)(val) & 0xffffffff)), PUSH_LE_U32((data), (pos) + 4, (uint32_t)((uint64_t)(val) >> 32)))
73 #define PUSH_LE_I64(data, pos, val) \
74 PUSH_LE_U64(data, pos, val)
79 * These macros pull or push integer values from byte arrays stored in
80 * big-endian byte order (network byte order).
82 #define PULL_BE_U8(data, pos) \
83 (_DATA_BYTE_CONST(data, pos))
84 #define PULL_BE_I8(data, pos) \
85 (int8_t)PULL_BE_U8(data, pos)
87 #define PULL_BE_U16(data, pos) \
88 ((((uint16_t)(PULL_BE_U8(data, pos))) << 8) | (uint16_t)PULL_BE_U8(data, (pos) + 1))
89 #define PULL_BE_I16(data, pos) \
90 (int16_t)PULL_BE_U16(data, pos)
92 #define PULL_BE_U32(data, pos) \
93 ((((uint32_t)PULL_BE_U16(data, pos)) << 16) | (uint32_t)(PULL_BE_U16(data, (pos) + 2)))
94 #define PULL_BE_I32(data, pos) \
95 (int32_t)PULL_BE_U32(data, pos)
97 #define PULL_BE_U64(data, pos) \
98 ((((uint64_t)PULL_BE_U32(data, pos)) << 32) | (uint64_t)(PULL_BE_U32(data, (pos) + 4)))
99 #define PULL_BE_I64(data, pos) \
100 (int64_t)PULL_BE_U64(data, pos)
104 #define PUSH_BE_U8(data, pos, val) \
105 (_DATA_BYTE(data, pos) = ((uint8_t)(val)))
106 #define PUSH_BE_I8(data, pos, val) \
107 PUSH_BE_U8(data, pos, val)
109 #define PUSH_BE_U16(data, pos, val) \
110 (PUSH_BE_U8((data), (pos), (uint8_t)(((uint16_t)(val)) >> 8)), PUSH_BE_U8((data), (pos) + 1, (uint8_t)((val) & 0xff)))
111 #define PUSH_BE_I16(data, pos, val) \
112 PUSH_BE_U16(data, pos, val)
114 #define PUSH_BE_U32(data, pos, val) \
115 (PUSH_BE_U16((data), (pos), (uint16_t)(((uint32_t)(val)) >> 16)), PUSH_BE_U16((data), (pos) + 2, (uint16_t)((val) & 0xffff)))
116 #define PUSH_BE_I32(data, pos, val) \
117 PUSH_BE_U32(data, pos, val)
119 #define PUSH_BE_U64(data, pos, val) \
120 (PUSH_BE_U32((data), (pos), (uint32_t)(((uint64_t)(val)) >> 32)), PUSH_BE_U32((data), (pos) + 4, (uint32_t)((val) & 0xffffffff)))
121 #define PUSH_BE_I64(data, pos, val) \
122 PUSH_BE_U64(data, pos, val)
124 #endif /* _BYTEARRAY_H */