forked from keeshux/basic-blockchain-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.h
More file actions
59 lines (45 loc) · 1.14 KB
/
common.h
File metadata and controls
59 lines (45 loc) · 1.14 KB
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
#ifndef __COMMON_H
#define __COMMON_H
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include "endian.h"
void bbp_print_hex(const char *label, const uint8_t *v, size_t len) {
size_t i;
printf("%s: ", label);
for (i = 0; i < len; ++i) {
printf("%02x", v[i]);
}
printf("\n");
}
uint8_t bbp_hex2byte(const char ch) {
if ((ch >= '0') && (ch <= '9')) {
return ch - '0';
}
if ((ch >= 'a') && (ch <= 'f')) {
return ch - 'a' + 10;
}
return 0;
}
void bbp_parse_hex(uint8_t *v, const char *str) {
const size_t count = strlen(str) / 2;
size_t i;
for (i = 0; i < count; ++i) {
const char hi = bbp_hex2byte(str[i * 2]);
const char lo = bbp_hex2byte(str[i * 2 + 1]);
v[i] = hi * 16 + lo;
}
}
uint8_t *bbp_alloc_hex(const char *str, size_t *len) {
const size_t count = strlen(str) / 2;
size_t i;
uint8_t *v = malloc(count);
for (i = 0; i < count; ++i) {
const char hi = bbp_hex2byte(str[i * 2]);
const char lo = bbp_hex2byte(str[i * 2 + 1]);
v[i] = hi * 16 + lo;
}
*len = count;
return v;
}
#endif