forked from mapbox/tile-count
-
Notifications
You must be signed in to change notification settings - Fork 0
/
serial.cpp
59 lines (49 loc) · 1.21 KB
/
serial.cpp
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
#include <stdio.h>
#include <stdlib.h>
#include "serial.hpp"
void write64(FILE *out, unsigned long long v) {
// Big-endian so memcmp() sorts numerically
for (ssize_t i = 64 - 8; i >= 0; i -= 8) {
if (putc((v >> i) & 0xFF, out) == EOF) {
perror("Write data");
exit(EXIT_FAILURE);
}
}
}
void write64(unsigned char **out, unsigned long long v) {
// Big-endian so memcmp() sorts numerically
for (ssize_t i = 64 - 8; i >= 0; i -= 8) {
**out = (v >> i) & 0xFF;
(*out)++;
}
}
void write32(FILE *out, unsigned long long v) {
// Big-endian so memcmp() sorts numerically
for (ssize_t i = 32 - 8; i >= 0; i -= 8) {
if (putc((v >> i) & 0xFF, out) == EOF) {
perror("Write data");
exit(EXIT_FAILURE);
}
}
}
void write32(unsigned char **out, unsigned long long v) {
// Big-endian so memcmp() sorts numerically
for (ssize_t i = 32 - 8; i >= 0; i -= 8) {
**out = (v >> i) & 0xFF;
(*out)++;
}
}
unsigned long long read64(unsigned char *c) {
unsigned long long out = 0;
for (ssize_t i = 0; i < 8; i++) {
out = (out << 8) | c[i];
}
return out;
}
unsigned long long read32(unsigned char *c) {
unsigned long long out = 0;
for (ssize_t i = 0; i < 4; i++) {
out = (out << 8) | c[i];
}
return out;
}