-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbit_pack.h
153 lines (141 loc) · 4.85 KB
/
bit_pack.h
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#ifndef BIT_PACK_H
#define BIT_PACK_H
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
/*
Example 1
-------------------------
Bit offset = 3
Bit width = 13
Endanness = le
7 6 5 4 3 2 1 0
+---------------+ Address
|e|d|c|b|a| | | | 0x1000
+---------------+
| | |k|j|i|h|g|f| 0x1001
+---------------+
| | | | | | | | | 0x1002
+---------------+
| | | | | | | | | 0x1003
+---------------+
Example 2
-------------------------
Bit offset = 3
Bit width = 13
Endanness = be
7 6 5 4 3 2 1 0
+---------------+ Address
| | | | | | | | | 0x1000
+---------------+
| | | | | | | | | 0x1001
+---------------+
| | |k|j|i|h|g|f| 0x1002
+---------------+
|e|d|c|b|a| | | | 0x1003
+---------------+
*/
/**
* Pulls a 64-bit unsigned integer out of an array of bytes stored in a little
* endian byte order. The result is copied into the passed 64 bit pointer.
*
* @param[in] src Source array.
* @param[in] len Length of the source array in bytes.
* @param[in] offset_bits Offset in bits to the start of the value counting up from the LEAST significant bit of the FIRST byte of the source array.
* @param[in] width_bits Width of the value in bits.
* @param[out] dest Pointer to where to store the obtained value.
*
* @return 0 if successful, -1 on error.
*/
int unpack_uint64_le( const uint8_t * const src,
const size_t len,
const size_t offset_bits,
const size_t width_bits,
uint64_t * dest );
/**
* Pulls a 64-bit unsigned integer out of an array of bytes stored in a big
* endian byte order. The result is copied into the passed 64 bit pointer.
*
* @param[in] src Source array.
* @param[in] len Length of the source array in bytes.
* @param[in] offset_bits Offset in bits to the start of the value counting up from the LEAST significant bit of the LAST byte of the source array.
* @param[in] width_bits Width of the parameter in bits.
* @param[out] dest Pointer to where to store the obtained value.
*
* @return 0 if successful, -1 on error.
*/
int unpack_uint64_be( const uint8_t * const src,
const size_t len,
const size_t offset_bits,
const size_t width_bits,
uint64_t * dest );
/**
* Exactly the same as unpack_uint64_be() except the offset is expected from the
* most significant byte of the FIRST byte of the source array.
*
* @param[in] src Source array.
* @param[in] len Length of the source array in bytes.
* @param[in] offset_bits Offset in bits to the start of the value counting up from the MOST significant bit of the FIRST byte of the source array.
* @param[in] width_bits Width of the parameter in bits.
* @param[out] dest Pointer to where to store the obtained value.
*
* @return 0 if successful, -1 on error.
*/
int unpack_uint64_be_rev( const uint8_t * const src,
const size_t len,
const size_t offset_bits,
const size_t width_bits,
uint64_t * dest );
/**
* Packs a 64-bit unsigned integer into of an array of bytes stored in a big
* endian byte order.
*
* @param[in] dest Destination array.
* @param[in] len Length of the dest array in bytes.
* @param[in] offset_bits Offset in bits to the start of the value counting up from the LEAST significant bit of the FIRST byte of the source array.
* @param[in] width_bits Width of the value in bits.
* @param[in] src The value to pack.
*
* @return 0 if successful, -1 on error.
*/
int pack_uint64_le( uint8_t * const dest,
const size_t len,
const size_t offset_bits,
const size_t width_bits,
const uint64_t src );
/**
* Packs a 64-bit unsigned integer into of an array of bytes stored in a big
* endian byte order.
*
* @param[in] dest Destination array.
* @param[in] len Length of the dest array in bytes.
* @param[in] offset_bits Offset in bits to the start of the value counting up from the LEAST significant bit of the LAST byte of the source array.
* @param[in] width_bits Width of the value in bits.
* @param[in] src The value to pack.most
*
* @return 0 if successful, -1 on error.
*/
int pack_uint64_be( uint8_t * const dest,
const size_t len,
const size_t offset_bits,
const size_t width_bits,
const uint64_t src );
/**
* Exactly the same as pack_uint64_be() except the offset is expected from the
* most significant byte of the FIRST byte of the source array.
*
* @param[in] dest Destination array.
* @param[in] len Length of the dest array in bytes.
* @param[in] offset_bits Offset in bits to the start of the value counting up from the MOST significant bit of the FIRST byte of the source array.
* @param[in] width_bits Width of the value in bits.
* @param[in] src The value to pack.
*
* @return 0 if successful, -1 on error.
*/
int pack_uint64_be_rev( uint8_t * const dest,
const size_t len,
const size_t offset_bits,
const size_t width_bits,
const uint64_t src );
#endif