-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathutils.c
More file actions
51 lines (43 loc) · 1.12 KB
/
Copy pathutils.c
File metadata and controls
51 lines (43 loc) · 1.12 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
/*
* Copyright (c) 2016-2018 . All Rights Reserved.
*/
#include "utils.h"
int size_of_bytes(int str_len) {
int out_len = (str_len & 1) ? (str_len + 1) / 2 : str_len / 2;
return out_len;
}
uint8_t strtohex(char c) {
if (c >= '0' && c <= '9')
return c - '0';
if (c >= 'a' && c <= 'f')
return c - 'a' + 10;
if (c >= 'A' && c <= 'F')
return c - 'A' + 10;
return 255;
}
int hex2byte_arr(char *buf, int len, uint8_t *out, int outbuf_size) {
int i = len - 1;
int out_len = (len & 1) ? (len + 1) / 2 : len / 2;
int j = out_len - 1;
if (j > outbuf_size)
return -1; /* Output buffer is smaller than need */
while (i >= 0) {
out[j] = strtohex(buf[i--]);
if (i >= 0) {
out[j--] |= strtohex(buf[i--]) << 4;
}
}
return out_len;
}
void int8_to_char(uint8_t *buffer, int len, char *out) {
const char hex[] = "0123456789abcdef";
int max = 2 * len;
int i = 0;
int j = 0;
while (j < len) {
out[i++] = hex[(buffer[j] >> 4) & 0xF];
out[i++] = hex[buffer[j] & 0xF];
j++;
}
out[i] = '\0';
}