-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.h
More file actions
54 lines (50 loc) · 1.14 KB
/
util.h
File metadata and controls
54 lines (50 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
#pragma once
#include <stdint.h>
class Util {
public:
static int bitcount(unsigned int v);
static int checksum(char *data, int n);
static void print_array(char* data, int n);
template<typename T> static int dump(char *buffer, int buf_size, T data, int n);
template<typename T> static int parse(char *buffer, int buf_size, T &data, int n);
};
template<typename T> int Util::dump(char *buffer, int buf_size, T data, int n) {
int index = 0;
n -= 1;
while (n >= 0) {
if (((data >> (n * 8)) & 0xFF) == 0xF6) {
if (index > buf_size) {
return -1;
}
buffer[index++] = 0xF7;
}
if (((data >> (n * 8)) & 0xFF) == 0xF7) {
if (index > buf_size) {
return -1;
}
buffer[index++] = 0xF7;
}
if (index > buf_size) {
return -1;
}
buffer[index++] = (data >> (n * 8)) & 0xFF;
n--;
}
return index;
}
template<typename T> int Util::parse(char *buffer, int buf_size, T &data, int n) {
data = 0;
int index = 0;
while (n > 0) {
if (index >= buf_size) {
return -1;
} else if ((uint8_t)buffer[index] == 0xF7) {
index++;
} else {
data = (data << 8) + (uint8_t)buffer[index];
index++;
n--;
}
}
return index;
}