|
1 | 1 | #include "maxminddb_test_helper.h" |
| 2 | +#include "mmdb_test_writer.h" |
2 | 3 | #include <stdlib.h> |
3 | 4 |
|
4 | | -/* MMDB binary format constants */ |
5 | | -#define METADATA_MARKER "\xab\xcd\xefMaxMind.com" |
6 | | -#define METADATA_MARKER_LEN 14 |
7 | | -#define DATA_SEPARATOR_SIZE 16 |
8 | | - |
9 | | -/* Write a map with size entries (size must be < 29) */ |
10 | | -static int write_map(uint8_t *buf, uint32_t size) { |
11 | | - buf[0] = (7 << 5) | (size & 0x1f); |
12 | | - return 1; |
13 | | -} |
14 | | - |
15 | | -/* Write a utf8_string (type 2). For short strings (len < 29). */ |
16 | | -static int write_string(uint8_t *buf, const char *str, uint32_t len) { |
17 | | - buf[0] = (2 << 5) | (len & 0x1f); |
18 | | - memcpy(buf + 1, str, len); |
19 | | - return 1 + len; |
20 | | -} |
21 | | - |
22 | | -static int write_uint16(uint8_t *buf, uint16_t value) { |
23 | | - buf[0] = (5 << 5) | 2; |
24 | | - buf[1] = (value >> 8) & 0xff; |
25 | | - buf[2] = value & 0xff; |
26 | | - return 3; |
27 | | -} |
28 | | - |
29 | | -static int write_uint32(uint8_t *buf, uint32_t value) { |
30 | | - buf[0] = (6 << 5) | 4; |
31 | | - buf[1] = (value >> 24) & 0xff; |
32 | | - buf[2] = (value >> 16) & 0xff; |
33 | | - buf[3] = (value >> 8) & 0xff; |
34 | | - buf[4] = value & 0xff; |
35 | | - return 5; |
36 | | -} |
37 | | - |
38 | | -static int write_uint64(uint8_t *buf, uint64_t value) { |
39 | | - buf[0] = (0 << 5) | 8; |
40 | | - buf[1] = 2; /* extended type: 7 + 2 = 9 (uint64) */ |
41 | | - buf[2] = (value >> 56) & 0xff; |
42 | | - buf[3] = (value >> 48) & 0xff; |
43 | | - buf[4] = (value >> 40) & 0xff; |
44 | | - buf[5] = (value >> 32) & 0xff; |
45 | | - buf[6] = (value >> 24) & 0xff; |
46 | | - buf[7] = (value >> 16) & 0xff; |
47 | | - buf[8] = (value >> 8) & 0xff; |
48 | | - buf[9] = value & 0xff; |
49 | | - return 10; |
50 | | -} |
51 | | - |
52 | | -static int write_meta_key(uint8_t *buf, const char *key) { |
53 | | - return write_string(buf, key, strlen(key)); |
54 | | -} |
55 | | - |
56 | 5 | /* |
57 | 6 | * Create a crafted MMDB file with deeply nested maps and write it to path. |
58 | 7 | * The data section contains: {"a": {"a": {"a": ... {"a": "x"} ...}}} |
|
0 commit comments