-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathutil.c
65 lines (59 loc) · 1.55 KB
/
util.c
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
/*
* nrfdfu - Nordic DFU Upgrade Utility
*
* Copyright (C) 2020 Bruno Randolf (br1@einfach.org)
*
* 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, either version 3 of the License, or
* (at your option) any later version.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include "util.h"
/* dump data in same format as nrfutil (integer) */
void dump_data(const char* txt, const uint8_t* data, size_t len)
{
printf("[ %s", txt);
for (int i = 0; i < len; i++) {
printf("%d ", *(data + i));
}
printf("]\n");
}
static uint8_t hex_digit(char ch)
{
if (('0' <= ch) && (ch <= '9')) {
ch -= '0';
} else {
if (('a' <= ch) && (ch <= 'f')) {
ch += 10 - 'a';
} else {
if (('A' <= ch) && (ch <= 'F')) {
ch += 10 - 'A';
} else {
ch = 16;
}
}
}
return ch;
}
bool hex_to_bin(const char* hex, uint8_t* bin, size_t len)
{
if (hex == NULL || bin == NULL) {
return false;
}
for (size_t idx = 0; idx < len; ++idx) {
bin[idx] = hex_digit(hex[2 * idx]) << 4;
bin[idx] |= hex_digit(hex[1 + 2 * idx]);
}
return true;
}