-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.c
More file actions
88 lines (73 loc) · 2.04 KB
/
Copy pathutils.c
File metadata and controls
88 lines (73 loc) · 2.04 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
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h> /*strtol*/
#include <ctype.h> /*Needed for isspace*/
#include "tracer.h"
#include "utils.h"
inline uint8_t* __attribute__((always_inline)) multi_strchr(const uint8_t* str, uint8_t* keys){
uint8_t* result = NULL;
int i = strcspn ((const char*)str, (const char*)keys);
if (str[i]) {
result = (uint8_t*)str + i;
}
return result;
}
const uint8_t* read_hex(const uint8_t* buf, uint32_t* out_value) {
uint8_t* result = NULL;
*out_value = strtol((const char*)buf, (char **)&result, 16);
if (buf == result) {
result = NULL;
}
return result;
}
const uint8_t* skip_whitespace(const uint8_t* buf) {
for (; buf && *buf; buf++) {
if (isspace(*buf)) {
continue;
} else {
break;
}
}
return buf;
}
const uint8_t* skip_blank_lines(const uint8_t* buf) {
for (; buf && *buf; buf++) {
if (*buf == '\n' || *buf == '\r') {
continue;
} else {
break;
}
}
return buf;
}
//mirror bits of each byte, i.e. bit n <-> bit 7-n
//can be done in place, i.e. dest_buf = src_buf
void mirror_bytes(uint8_t* dest_buf, uint8_t* src_buf, int size) {
int ix, jx;
uint8_t src, dst, lsb;
for (ix = 0; ix < size; ix++) {
src = *src_buf++;
dst = 0;
for (jx = 0; jx < 8; jx++) {
lsb = src&1;
src >>= 1;
dst <<= 1;
dst |= lsb;
}
*dest_buf++ = dst;
}
}
//swap the endianness of the 32 bit integer
uint32_t swap_uint32(uint32_t input) {
uint32_t swapped =
((input >> 24) & 0xff) | ((input << 8) & 0xff0000)
| ((input >> 8) & 0xff00) | ((input << 24) & 0xff000000);
return swapped;
}
//Mirror bits of 32 bit number, i.e. bit n <-> bit 31-n
uint32_t mirror_uint32(uint32_t input) {
uint32_t mirrored = 0;
mirror_bytes((uint8_t*)&mirrored, (uint8_t*)&input, sizeof(uint32_t));
mirrored = swap_uint32(mirrored);
return mirrored;
}