This repository has been archived by the owner on Jul 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUtils.cpp
398 lines (338 loc) · 9.83 KB
/
Utils.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/**
* Digital Voice Modem - Transcode Software
* GPLv2 Open Source. Use is subject to license terms.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* @package DVM / Transcode Software
*
*/
//
// Based on code from the MMDVMHost project. (https://github.com/g4klx/MMDVMHost)
// Licensed under the GPLv2 License (https://opensource.org/licenses/GPL-2.0)
//
/*
* Copyright (C) 2009,2014,2015,2016 Jonathan Naylor, G4KLX
* Copyright (C) 2018-2020 Bryan Biedenkapp N2PLL
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*
* 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 "Utils.h"
#include "Log.h"
#include <cstdio>
#include <cassert>
// ---------------------------------------------------------------------------
// Constants/Macros
// ---------------------------------------------------------------------------
const uint8_t BITS_TABLE[] = {
# define B2(n) n, n+1, n+1, n+2
# define B4(n) B2(n), B2(n+1), B2(n+1), B2(n+2)
# define B6(n) B4(n), B4(n+1), B4(n+1), B4(n+2)
B6(0), B6(1), B6(1), B6(2)
};
// ---------------------------------------------------------------------------
// Global Functions
// ---------------------------------------------------------------------------
/// <summary>
/// Displays the host version.
/// </summary>
void getHostVersion()
{
LogInfo(__PROG_NAME__ " %s (built %s)", __VER__, __BUILD__);
LogInfo("Copyright (c) 2017-2022 DVMProject (https://github.com/dvmproject) Authors.");
LogInfo("Portions Copyright (c) 2015-2021 by Jonathan Naylor, G4KLX and others");
}
/// <summary>
///
/// </summary>
/// <param name="level"></param>
/// <param name="title"></param>
/// <param name="data"></param>
/// <param name="length"></param>
void cDump(int level, const char* title, const uint8_t* data, uint32_t length)
{
assert(data != NULL);
::Log(level, "DUMP", "%s", title);
uint32_t offset = 0U;
while (length > 0U) {
std::string output;
uint32_t bytes = (length > 16U) ? 16U : length;
for (unsigned i = 0U; i < bytes; i++) {
char temp[10U];
::sprintf(temp, "%02X ", data[offset + i]);
output += temp;
}
for (uint32_t i = bytes; i < 16U; i++)
output += " ";
output += " *";
for (unsigned i = 0U; i < bytes; i++) {
uint8_t c = data[offset + i];
if (::isprint(c))
output += c;
else
output += '.';
}
output += '*';
::Log(level, "DUMP", "%04X: %s", offset, output.c_str());
offset += 16U;
if (length >= 16U)
length -= 16U;
else
length = 0U;
}
}
// ---------------------------------------------------------------------------
// Static Class Members
// ---------------------------------------------------------------------------
/// <summary>
///
/// </summary>
/// <param name="title"></param>
/// <param name="data"></param>
/// <param name="length"></param>
void Utils::dump(const std::string& title, const uint8_t* data, uint32_t length)
{
assert(data != NULL);
dump(2U, title, data, length);
}
/// <summary>
///
/// </summary>
/// <param name="level"></param>
/// <param name="title"></param>
/// <param name="data"></param>
/// <param name="length"></param>
void Utils::dump(int level, const std::string& title, const uint8_t* data, uint32_t length)
{
assert(data != NULL);
::Log(level, "DUMP", "%s", title.c_str());
uint32_t offset = 0U;
while (length > 0U) {
std::string output;
uint32_t bytes = (length > 16U) ? 16U : length;
for (unsigned i = 0U; i < bytes; i++) {
char temp[10U];
::sprintf(temp, "%02X ", data[offset + i]);
output += temp;
}
for (uint32_t i = bytes; i < 16U; i++)
output += " ";
output += " *";
for (unsigned i = 0U; i < bytes; i++) {
uint8_t c = data[offset + i];
if (::isprint(c))
output += c;
else
output += '.';
}
output += '*';
::Log(level, "DUMP", "%04X: %s", offset, output.c_str());
offset += 16U;
if (length >= 16U)
length -= 16U;
else
length = 0U;
}
}
/// <summary>
///
/// </summary>
/// <param name="title"></param>
/// <param name="bits"></param>
/// <param name="length"></param>
void Utils::dump(const std::string& title, const bool* bits, uint32_t length)
{
assert(bits != NULL);
dump(2U, title, bits, length);
}
/// <summary>
///
/// </summary>
/// <param name="level"></param>
/// <param name="title"></param>
/// <param name="bits"></param>
/// <param name="length"></param>
void Utils::dump(int level, const std::string& title, const bool* bits, uint32_t length)
{
assert(bits != NULL);
uint8_t bytes[100U];
uint32_t nBytes = 0U;
for (uint32_t n = 0U; n < length; n += 8U, nBytes++)
bitsToByteBE(bits + n, bytes[nBytes]);
dump(level, title, bytes, nBytes);
}
/// <summary>
///
/// </summary>
/// <param name="byte"></param>
/// <param name="bits"></param>
void Utils::byteToBitsBE(uint8_t byte, bool* bits)
{
assert(bits != NULL);
bits[0U] = (byte & 0x80U) == 0x80U;
bits[1U] = (byte & 0x40U) == 0x40U;
bits[2U] = (byte & 0x20U) == 0x20U;
bits[3U] = (byte & 0x10U) == 0x10U;
bits[4U] = (byte & 0x08U) == 0x08U;
bits[5U] = (byte & 0x04U) == 0x04U;
bits[6U] = (byte & 0x02U) == 0x02U;
bits[7U] = (byte & 0x01U) == 0x01U;
}
/// <summary>
///
/// </summary>
/// <param name="byte"></param>
/// <param name="bits"></param>
void Utils::byteToBitsLE(uint8_t byte, bool* bits)
{
assert(bits != NULL);
bits[0U] = (byte & 0x01U) == 0x01U;
bits[1U] = (byte & 0x02U) == 0x02U;
bits[2U] = (byte & 0x04U) == 0x04U;
bits[3U] = (byte & 0x08U) == 0x08U;
bits[4U] = (byte & 0x10U) == 0x10U;
bits[5U] = (byte & 0x20U) == 0x20U;
bits[6U] = (byte & 0x40U) == 0x40U;
bits[7U] = (byte & 0x80U) == 0x80U;
}
/// <summary>
///
/// </summary>
/// <param name="bits"></param>
/// <param name="byte"></param>
void Utils::bitsToByteBE(const bool* bits, uint8_t& byte)
{
assert(bits != NULL);
byte = bits[0U] ? 0x80U : 0x00U;
byte |= bits[1U] ? 0x40U : 0x00U;
byte |= bits[2U] ? 0x20U : 0x00U;
byte |= bits[3U] ? 0x10U : 0x00U;
byte |= bits[4U] ? 0x08U : 0x00U;
byte |= bits[5U] ? 0x04U : 0x00U;
byte |= bits[6U] ? 0x02U : 0x00U;
byte |= bits[7U] ? 0x01U : 0x00U;
}
/// <summary>
///
/// </summary>
/// <param name="bits"></param>
/// <param name="byte"></param>
void Utils::bitsToByteLE(const bool* bits, uint8_t& byte)
{
assert(bits != NULL);
byte = bits[0U] ? 0x01U : 0x00U;
byte |= bits[1U] ? 0x02U : 0x00U;
byte |= bits[2U] ? 0x04U : 0x00U;
byte |= bits[3U] ? 0x08U : 0x00U;
byte |= bits[4U] ? 0x10U : 0x00U;
byte |= bits[5U] ? 0x20U : 0x00U;
byte |= bits[6U] ? 0x40U : 0x00U;
byte |= bits[7U] ? 0x80U : 0x00U;
}
/// <summary>
///
/// </summary>
/// <param name="in"></param>
/// <param name="out"></param>
/// <param name="start"></param>
/// <param name="stop"></param>
uint32_t Utils::getBits(const uint8_t* in, uint8_t* out, uint32_t start, uint32_t stop)
{
assert(in != NULL);
assert(out != NULL);
uint32_t n = 0U;
for (uint32_t i = start; i < stop; i++, n++) {
bool b = READ_BIT(in, i);
WRITE_BIT(out, n, b);
}
return n;
}
/// <summary>
///
/// </summary>
/// <param name="in"></param>
/// <param name="out"></param>
/// <param name="start"></param>
/// <param name="length"></param>
uint32_t Utils::getBitRange(const uint8_t* in, uint8_t* out, uint32_t start, uint32_t length)
{
return getBits(in, out, start, start + length);
}
/// <summary>
///
/// </summary>
/// <param name="in"></param>
/// <param name="out"></param>
/// <param name="start"></param>
/// <param name="stop"></param>
uint32_t Utils::setBits(const uint8_t* in, uint8_t* out, uint32_t start, uint32_t stop)
{
assert(in != NULL);
assert(out != NULL);
uint32_t n = 0U;
for (uint32_t i = start; i < stop; i++, n++) {
bool b = READ_BIT(in, n);
WRITE_BIT(out, i, b);
}
return n;
}
/// <summary>
///
/// </summary>
/// <param name="in"></param>
/// <param name="out"></param>
/// <param name="start"></param>
/// <param name="length"></param>
uint32_t Utils::setBitRange(const uint8_t* in, uint8_t* out, uint32_t start, uint32_t length)
{
return setBits(in, out, start, start + length);
}
/// <summary>
/// Returns the count of bits in the passed 8 byte value.
/// </summary>
/// <param name="bits"></param>
/// <returns></returns>
uint8_t Utils::countBits8(uint8_t bits)
{
return BITS_TABLE[bits];
}
/// <summary>
/// Returns the count of bits in the passed 32 byte value.
/// </summary>
/// <param name="bits"></param>
/// <returns></returns>
uint8_t Utils::countBits32(uint32_t bits)
{
uint8_t* p = (uint8_t*)&bits;
uint8_t n = 0U;
n += BITS_TABLE[p[0U]];
n += BITS_TABLE[p[1U]];
n += BITS_TABLE[p[2U]];
n += BITS_TABLE[p[3U]];
return n;
}
/// <summary>
/// Returns the count of bits in the passed 64 byte value.
/// </summary>
/// <param name="bits"></param>
/// <returns></returns>
uint8_t Utils::countBits64(ulong64_t bits)
{
uint8_t* p = (uint8_t*)&bits;
uint8_t n = 0U;
n += BITS_TABLE[p[0U]];
n += BITS_TABLE[p[1U]];
n += BITS_TABLE[p[2U]];
n += BITS_TABLE[p[3U]];
n += BITS_TABLE[p[4U]];
n += BITS_TABLE[p[5U]];
n += BITS_TABLE[p[6U]];
n += BITS_TABLE[p[7U]];
return n;
}