forked from douban/libmc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtility.cpp
More file actions
70 lines (61 loc) · 1.56 KB
/
Copy pathUtility.cpp
File metadata and controls
70 lines (61 loc) · 1.56 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
#include "Utility.h"
#include "Common.h"
namespace douban {
namespace mc {
namespace utility {
bool isValidKey(const char* key, const size_t keylen) {
#define HANDLE_BAD_MC_KEY() \
do { \
log_warn("invalid mc key of length %zu: \"%.*s\"", keylen, static_cast<int>(keylen), key); \
return false; \
} while (0)
if (keylen > MC_MAX_KEY_LENGTH) {
HANDLE_BAD_MC_KEY();
}
for (size_t i = 0; i < keylen; i++) {
switch (key[i]) {
case ' ':
HANDLE_BAD_MC_KEY();
break;
case '\r':
HANDLE_BAD_MC_KEY();
break;
case '\n':
HANDLE_BAD_MC_KEY();
break;
case 0:
HANDLE_BAD_MC_KEY();
break;
default:
break;
}
}
return true;
}
// credits for: https://gist.github.com/sergot/1333837
void fprintBuffer(std::FILE* file, const char *data_buffer_, const unsigned int length) {
const unsigned char* data_buffer = reinterpret_cast<const unsigned char*>(data_buffer_);
unsigned int i, j;
for (i = 0; i < length; i++) {
unsigned char byte = data_buffer[i];
fprintf(file, "%02x ", data_buffer[i]);
if (((i%16) == 15) || (i == length-1)) {
for (j = 0; j < 15-(i%16); j++) {
fprintf(file, " ");
}
fprintf(file, "| ");
for (j=(i-(i%16)); j <= i; j++) {
byte = data_buffer[j];
if ((byte > 31) && (byte < 127)) {
fprintf(file, "%c", byte);
} else {
fprintf(file, ".");
}
}
fprintf(file, "\n");
}
}
}
} // namespace utility
} // namespace mc
} // namespace douban