-
-
Notifications
You must be signed in to change notification settings - Fork 374
/
Copy pathyaml_bits.cpp
236 lines (188 loc) · 4.96 KB
/
yaml_bits.cpp
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
* Copyright (C) EdgeTX
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program 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 General Public License for more details.
*/
#include <cinttypes>
#include <stdio.h>
#include "yaml_bits.h"
#include "yaml_parser.h"
#include <limits.h> /* CHAR_BIT */
#define _IS_ALIGNED(addr) (((intptr_t)addr & 0x3) == 0)
#define BIT_MASK(__TYPE__, __ONE_COUNT__) \
((__TYPE__) (-((__ONE_COUNT__) != 0))) \
& (((__TYPE__) -1) >> ((sizeof(__TYPE__) * CHAR_BIT) - (__ONE_COUNT__)))
#define MASK_LOWER(bits) BIT_MASK(uint32_t, bits)
#define MASK_UPPER(bits) (0xFF << bits)
void yaml_put_bits(uint8_t* dst, uint32_t i, uint32_t bit_ofs, uint32_t bits)
{
i &= MASK_LOWER(bits);
if (bit_ofs) {
*dst &= ~((MASK_LOWER(bits) << bit_ofs) & 0xFF);
*(dst++) |= (i << bit_ofs) & 0xFF;
if (bits <= 8 - bit_ofs)
return;
bits -= 8 - bit_ofs;
i = i >> (8 - bit_ofs);
}
while(bits >= 8) {
*(dst++) = i & 0xFF;
bits -= 8;
i = i >> 8;
}
if (bits) {
uint8_t mask = MASK_UPPER(bits);
*dst &= mask;
*dst |= i & ~mask;
}
}
uint32_t yaml_get_bits(uint8_t* src, uint32_t bit_ofs, uint32_t bits)
{
uint32_t i = 0;
uint32_t bit_shift = 0;
if (bit_ofs) {
i = (*(src++) & MASK_UPPER(bit_ofs)) >> bit_ofs;
if (bits <= 8 - bit_ofs) {
i &= MASK_LOWER(bits);
return i;
}
bit_shift = 8 - bit_ofs;
bits -= bit_shift;
}
while(bits >= 8) {
i |= (uint32_t)*(src++) << bit_shift;
bits -= 8;
bit_shift += 8;
}
if (bits) {
i |= ((uint32_t)*src & MASK_LOWER(bits)) << bit_shift;
}
return i;
}
// if the start address is not aligned on a byte,
// checking max 32 bits is supported.
bool yaml_is_zero(uint8_t* data, uint32_t bitoffs, uint32_t bits)
{
data += bitoffs >> 3;
bitoffs &= 0x7;
if (bitoffs) {
return !yaml_get_bits(data, bitoffs, bits);
}
if(_IS_ALIGNED(data)) {
while (bits >= 32) {
if (*(uint32_t*)data) return false;
data += 4;
bits -= 32;
}
}
while (bits >= 8) {
if (*data) return false;
data++;
bits -= 8;
}
if (bits) {
return !yaml_get_bits(data, 0, bits);
}
return true;
}
int32_t yaml_str2int_ref(const char*& val, uint8_t& val_len)
{
bool neg = false;
int i_val = 0;
while (val_len > 0) {
if (*val == '-') {
neg = true;
} else if (*val >= '0' && *val <= '9') {
i_val = i_val * 10 + (*val - '0');
} else {
break;
}
val++; val_len--;
}
return neg ? -i_val : i_val;
}
int32_t yaml_str2int(const char* val, uint8_t val_len)
{
return yaml_str2int_ref(val, val_len);
}
uint32_t yaml_str2uint_ref(const char*& val, uint8_t& val_len)
{
uint32_t i_val = 0;
while(val_len > 0) {
if (*val >= '0' && *val <= '9') {
i_val = i_val * 10 + (*val - '0');
} else {
break;
}
val++; val_len--;
}
return i_val;
}
uint32_t yaml_str2uint(const char* val, uint8_t val_len)
{
return yaml_str2uint_ref(val, val_len);
}
uint32_t yaml_hex2uint(const char* val, uint8_t val_len)
{
uint32_t i_val = 0;
while(val_len > 0) {
if (*val >= '0' && *val <= '9') {
i_val <<= 4;
i_val |= (*val - '0') & 0xF;
} else if (*val >= 'A' && *val <= 'F') {
i_val <<= 4;
i_val |= (*val - 'A' + 10) & 0xF;
} else if (*val >= 'a' && *val <= 'f') {
i_val <<= 4;
i_val |= (*val - 'a' + 10) & 0xF;
} else {
break;
}
val++; val_len--;
}
return i_val;
}
constexpr int INT2STR_LEN = 16;
static char int2str_buffer[INT2STR_LEN] = {0};
char* yaml_unsigned2str(uint32_t i)
{
snprintf(int2str_buffer, INT2STR_LEN, "%" PRIu32, i);
return int2str_buffer;
}
char* yaml_signed2str(int32_t i)
{
snprintf(int2str_buffer, INT2STR_LEN, "%" PRId32, i);
return int2str_buffer;
}
char* yaml_unsigned2hex(uint32_t i)
{
snprintf(int2str_buffer, INT2STR_LEN, "%08" PRIX32, i);
return int2str_buffer;
}
char* yaml_rgb2hex(uint32_t i)
{
snprintf(int2str_buffer, INT2STR_LEN, "%06" PRIX32, i);
return int2str_buffer;
}
int32_t yaml_to_signed(uint32_t i, uint32_t bits)
{
if (bits < 32 && (i & (1 << (bits-1)))) {
i |= 0xFFFFFFFF << bits;
}
return i;
}