-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.c
45 lines (40 loc) · 1.12 KB
/
log.c
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
#include "log.h"
#include <assert.h>
#include <stdio.h>
#ifndef NO_LOGS_ON_STDERR
int g_log_level = LOG_DEBUG;
const char* g_errname;
#endif
void init_log(void) {
#ifdef NO_LOGS_ON_STDERR
xfclose(stderr);
#endif
}
char* to_printable(const unsigned char* const p, const unsigned char* const pe,
const size_t s, char b[static const s]) {
// TODO: Add support for regional characters, ie from 0x80 to 0xFF
// TODO: Rewrite to_printable using libicu
// TODO: Experiment with https://en.wikipedia.org/wiki/ISO_2047
unsigned char* o = (unsigned char*)b;
for (const unsigned char* x = p; x != pe; x++) {
assert(o < (unsigned char*)b + s);
const unsigned char c = *x;
if (c <= 0x20) {
*o++ = 0xE2;
*o++ = 0x90;
*o++ = 0x80 + c;
} else if (c == 0x7F) {
*o++ = 0xE2;
*o++ = 0x90;
*o++ = 0x80 + 0x31;
} else if (c & 0x80) {
*o++ = 0xE2;
*o++ = 0x90;
*o++ = 0x36;
} else {
*o++ = c;
}
}
*o = 0x00;
return b;
}