libssh 0.12.0
The SSH library
Loading...
Searching...
No Matches
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#include "config.h"
24
25#include <stdint.h>
26
27#define _DATA_BYTE_CONST(data, pos) \
28 ((uint8_t)(((const uint8_t *)(data))[(pos)]))
29
30#define _DATA_BYTE(data, pos) \
31 (((uint8_t *)(data))[(pos)])
32
33/*
34 * These macros pull or push integer values from byte arrays stored in
35 * little-endian byte order.
36 */
37#define PULL_LE_U8(data, pos) \
38 (_DATA_BYTE_CONST(data, pos))
39
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
43#define PULL_LE_U32(data, pos) \
44 ((uint32_t)(PULL_LE_U16(data, pos) | ((uint32_t)PULL_LE_U16(data, (pos) + 2)) << 16))
45
46#define PULL_LE_U64(data, pos) \
47 ((uint64_t)(PULL_LE_U32(data, pos) | ((uint64_t)PULL_LE_U32(data, (pos) + 4)) << 32))
48
49
50#define PUSH_LE_U8(data, pos, val) \
51 (_DATA_BYTE(data, pos) = ((uint8_t)(val)))
52
53#define PUSH_LE_U16(data, pos, val) \
54 (PUSH_LE_U8((data), (pos), (uint8_t)((uint16_t)(val) & 0xff)), PUSH_LE_U8((data), (pos) + 1, (uint8_t)((uint16_t)(val) >> 8)))
55
56#define PUSH_LE_U32(data, pos, val) \
57 (PUSH_LE_U16((data), (pos), (uint16_t)((uint32_t)(val) & 0xffff)), PUSH_LE_U16((data), (pos) + 2, (uint16_t)((uint32_t)(val) >> 16)))
58
59#define PUSH_LE_U64(data, pos, val) \
60 (PUSH_LE_U32((data), (pos), (uint32_t)((uint64_t)(val) & 0xffffffff)), PUSH_LE_U32((data), (pos) + 4, (uint32_t)((uint64_t)(val) >> 32)))
61
62
63
64/*
65 * These macros pull or push integer values from byte arrays stored in
66 * big-endian byte order (network byte order).
67 */
68#define PULL_BE_U8(data, pos) \
69 (_DATA_BYTE_CONST(data, pos))
70
71#define PULL_BE_U16(data, pos) \
72 ((((uint16_t)(PULL_BE_U8(data, pos))) << 8) | (uint16_t)PULL_BE_U8(data, (pos) + 1))
73
74#define PULL_BE_U32(data, pos) \
75 ((((uint32_t)PULL_BE_U16(data, pos)) << 16) | (uint32_t)(PULL_BE_U16(data, (pos) + 2)))
76
77#define PULL_BE_U64(data, pos) \
78 ((((uint64_t)PULL_BE_U32(data, pos)) << 32) | (uint64_t)(PULL_BE_U32(data, (pos) + 4)))
79
80
81
82#define PUSH_BE_U8(data, pos, val) \
83 (_DATA_BYTE(data, pos) = ((uint8_t)(val)))
84
85#define PUSH_BE_U16(data, pos, val) \
86 (PUSH_BE_U8((data), (pos), (uint8_t)(((uint16_t)(val)) >> 8)), PUSH_BE_U8((data), (pos) + 1, (uint8_t)((val) & 0xff)))
87
88#define PUSH_BE_U32(data, pos, val) \
89 (PUSH_BE_U16((data), (pos), (uint16_t)(((uint32_t)(val)) >> 16)), PUSH_BE_U16((data), (pos) + 2, (uint16_t)((val) & 0xffff)))
90
91#define PUSH_BE_U64(data, pos, val) \
92 (PUSH_BE_U32((data), (pos), (uint32_t)(((uint64_t)(val)) >> 32)), PUSH_BE_U32((data), (pos) + 4, (uint32_t)((val) & 0xffffffff)))
93
94#endif /* _BYTEARRAY_H */