maxminddb 2.5.0__tar.gz → 2.5.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (93) hide show
  1. {maxminddb-2.5.0 → maxminddb-2.5.2}/HISTORY.rst +13 -0
  2. {maxminddb-2.5.0 → maxminddb-2.5.2}/MANIFEST.in +1 -0
  3. {maxminddb-2.5.0 → maxminddb-2.5.2}/PKG-INFO +1 -1
  4. {maxminddb-2.5.0 → maxminddb-2.5.2}/examples/benchmark.py +1 -1
  5. maxminddb-2.5.2/extension/libmaxminddb/include/maxminddb.h +243 -0
  6. maxminddb-2.5.2/extension/libmaxminddb/src/data-pool.c +171 -0
  7. maxminddb-2.5.2/extension/libmaxminddb/src/data-pool.h +53 -0
  8. maxminddb-2.5.2/extension/libmaxminddb/src/maxminddb-compat-util.h +160 -0
  9. maxminddb-2.5.2/extension/libmaxminddb/src/maxminddb.c +2182 -0
  10. {maxminddb-2.5.0 → maxminddb-2.5.2}/maxminddb/__init__.py +1 -1
  11. {maxminddb-2.5.0 → maxminddb-2.5.2}/maxminddb.egg-info/PKG-INFO +1 -1
  12. maxminddb-2.5.2/maxminddb.egg-info/SOURCES.txt +77 -0
  13. {maxminddb-2.5.0 → maxminddb-2.5.2}/pyproject.toml +4 -1
  14. maxminddb-2.5.2/tests/data/bad-data/libmaxminddb/libmaxminddb-offset-integer-overflow.mmdb +0 -0
  15. maxminddb-2.5.2/tests/data/bad-data/maxminddb-golang/cyclic-data-structure.mmdb +0 -0
  16. maxminddb-2.5.2/tests/data/bad-data/maxminddb-golang/invalid-bytes-length.mmdb +1 -0
  17. maxminddb-2.5.2/tests/data/bad-data/maxminddb-golang/invalid-data-record-offset.mmdb +0 -0
  18. maxminddb-2.5.2/tests/data/bad-data/maxminddb-golang/invalid-map-key-length.mmdb +0 -0
  19. maxminddb-2.5.2/tests/data/bad-data/maxminddb-golang/invalid-string-length.mmdb +1 -0
  20. maxminddb-2.5.2/tests/data/bad-data/maxminddb-golang/metadata-is-an-uint128.mmdb +1 -0
  21. maxminddb-2.5.2/tests/data/bad-data/maxminddb-golang/unexpected-bytes.mmdb +0 -0
  22. maxminddb-2.5.2/tests/data/bad-data/maxminddb-python/bad-unicode-in-map-key.mmdb +0 -0
  23. maxminddb-2.5.2/tests/data/test-data/GeoIP2-Anonymous-IP-Test.mmdb +0 -0
  24. maxminddb-2.5.2/tests/data/test-data/GeoIP2-City-Test-Broken-Double-Format.mmdb +0 -0
  25. maxminddb-2.5.2/tests/data/test-data/GeoIP2-City-Test-Invalid-Node-Count.mmdb +0 -0
  26. maxminddb-2.5.2/tests/data/test-data/GeoIP2-City-Test.mmdb +0 -0
  27. maxminddb-2.5.2/tests/data/test-data/GeoIP2-Connection-Type-Test.mmdb +0 -0
  28. maxminddb-2.5.2/tests/data/test-data/GeoIP2-Country-Test.mmdb +0 -0
  29. maxminddb-2.5.2/tests/data/test-data/GeoIP2-DensityIncome-Test.mmdb +0 -0
  30. maxminddb-2.5.2/tests/data/test-data/GeoIP2-Domain-Test.mmdb +0 -0
  31. maxminddb-2.5.2/tests/data/test-data/GeoIP2-Enterprise-Test.mmdb +0 -0
  32. maxminddb-2.5.2/tests/data/test-data/GeoIP2-ISP-Test.mmdb +0 -0
  33. maxminddb-2.5.2/tests/data/test-data/GeoIP2-Precision-Enterprise-Test.mmdb +0 -0
  34. maxminddb-2.5.2/tests/data/test-data/GeoIP2-Static-IP-Score-Test.mmdb +0 -0
  35. maxminddb-2.5.2/tests/data/test-data/GeoIP2-User-Count-Test.mmdb +0 -0
  36. maxminddb-2.5.2/tests/data/test-data/GeoLite2-ASN-Test.mmdb +0 -0
  37. maxminddb-2.5.2/tests/data/test-data/GeoLite2-City-Test.mmdb +0 -0
  38. maxminddb-2.5.2/tests/data/test-data/GeoLite2-Country-Test.mmdb +0 -0
  39. maxminddb-2.5.2/tests/data/test-data/MaxMind-DB-no-ipv4-search-tree.mmdb +0 -0
  40. maxminddb-2.5.2/tests/data/test-data/MaxMind-DB-string-value-entries.mmdb +0 -0
  41. maxminddb-2.5.2/tests/data/test-data/MaxMind-DB-test-broken-pointers-24.mmdb +0 -0
  42. maxminddb-2.5.2/tests/data/test-data/MaxMind-DB-test-broken-search-tree-24.mmdb +0 -0
  43. maxminddb-2.5.2/tests/data/test-data/MaxMind-DB-test-decoder.mmdb +0 -0
  44. maxminddb-2.5.2/tests/data/test-data/MaxMind-DB-test-ipv4-24.mmdb +0 -0
  45. maxminddb-2.5.2/tests/data/test-data/MaxMind-DB-test-ipv4-28.mmdb +0 -0
  46. maxminddb-2.5.2/tests/data/test-data/MaxMind-DB-test-ipv4-32.mmdb +0 -0
  47. maxminddb-2.5.2/tests/data/test-data/MaxMind-DB-test-ipv6-24.mmdb +0 -0
  48. maxminddb-2.5.2/tests/data/test-data/MaxMind-DB-test-ipv6-28.mmdb +0 -0
  49. maxminddb-2.5.2/tests/data/test-data/MaxMind-DB-test-ipv6-32.mmdb +0 -0
  50. maxminddb-2.5.2/tests/data/test-data/MaxMind-DB-test-metadata-pointers.mmdb +0 -0
  51. maxminddb-2.5.2/tests/data/test-data/MaxMind-DB-test-mixed-24.mmdb +0 -0
  52. maxminddb-2.5.2/tests/data/test-data/MaxMind-DB-test-mixed-28.mmdb +0 -0
  53. maxminddb-2.5.2/tests/data/test-data/MaxMind-DB-test-mixed-32.mmdb +0 -0
  54. maxminddb-2.5.2/tests/data/test-data/MaxMind-DB-test-nested.mmdb +0 -0
  55. maxminddb-2.5.2/tests/data/test-data/MaxMind-DB-test-pointer-decoder.mmdb +0 -0
  56. maxminddb-2.5.2/tests/data/test-data/maps-with-pointers.raw +0 -0
  57. {maxminddb-2.5.0 → maxminddb-2.5.2}/tests/reader_test.py +1 -1
  58. maxminddb-2.5.0/.clang-format +0 -5
  59. maxminddb-2.5.0/.github/dependabot.yml +0 -11
  60. maxminddb-2.5.0/.github/workflows/address-sanitizer.yml +0 -37
  61. maxminddb-2.5.0/.github/workflows/clang-analyzer.yml +0 -38
  62. maxminddb-2.5.0/.github/workflows/codeql-analysis.yml +0 -54
  63. maxminddb-2.5.0/.github/workflows/release.yml +0 -62
  64. maxminddb-2.5.0/.github/workflows/test-libmaxminddb.yml +0 -57
  65. maxminddb-2.5.0/.github/workflows/test.yml +0 -40
  66. maxminddb-2.5.0/.gitignore +0 -24
  67. maxminddb-2.5.0/.gitmodules +0 -6
  68. maxminddb-2.5.0/.pylintrc +0 -4
  69. maxminddb-2.5.0/.readthedocs.yaml +0 -6
  70. maxminddb-2.5.0/dev-bin/clang-format-all.sh +0 -15
  71. maxminddb-2.5.0/dev-bin/release.sh +0 -65
  72. maxminddb-2.5.0/maxminddb.egg-info/SOURCES.txt +0 -43
  73. {maxminddb-2.5.0 → maxminddb-2.5.2}/LICENSE +0 -0
  74. {maxminddb-2.5.0 → maxminddb-2.5.2}/README.rst +0 -0
  75. {maxminddb-2.5.0 → maxminddb-2.5.2}/docs/Makefile +0 -0
  76. {maxminddb-2.5.0 → maxminddb-2.5.2}/docs/conf.py +0 -0
  77. {maxminddb-2.5.0 → maxminddb-2.5.2}/docs/index.rst +0 -0
  78. {maxminddb-2.5.0 → maxminddb-2.5.2}/extension/maxminddb.c +0 -0
  79. {maxminddb-2.5.0 → maxminddb-2.5.2}/extension/maxminddb_config.h +0 -0
  80. {maxminddb-2.5.0 → maxminddb-2.5.2}/maxminddb/const.py +0 -0
  81. {maxminddb-2.5.0 → maxminddb-2.5.2}/maxminddb/decoder.py +0 -0
  82. {maxminddb-2.5.0 → maxminddb-2.5.2}/maxminddb/errors.py +0 -0
  83. {maxminddb-2.5.0 → maxminddb-2.5.2}/maxminddb/extension.pyi +0 -0
  84. {maxminddb-2.5.0 → maxminddb-2.5.2}/maxminddb/file.py +0 -0
  85. {maxminddb-2.5.0 → maxminddb-2.5.2}/maxminddb/py.typed +0 -0
  86. {maxminddb-2.5.0 → maxminddb-2.5.2}/maxminddb/reader.py +0 -0
  87. {maxminddb-2.5.0 → maxminddb-2.5.2}/maxminddb/types.py +0 -0
  88. {maxminddb-2.5.0 → maxminddb-2.5.2}/maxminddb.egg-info/dependency_links.txt +0 -0
  89. {maxminddb-2.5.0 → maxminddb-2.5.2}/maxminddb.egg-info/requires.txt +0 -0
  90. {maxminddb-2.5.0 → maxminddb-2.5.2}/maxminddb.egg-info/top_level.txt +0 -0
  91. {maxminddb-2.5.0 → maxminddb-2.5.2}/setup.cfg +0 -0
  92. {maxminddb-2.5.0 → maxminddb-2.5.2}/setup.py +0 -0
  93. {maxminddb-2.5.0 → maxminddb-2.5.2}/tests/decoder_test.py +0 -0
@@ -3,6 +3,19 @@
3
3
  History
4
4
  -------
5
5
 
6
+ 2.5.2 (2024-01-09)
7
+ ++++++++++++++++++
8
+
9
+ * The vendored ``libmaxminddb`` version was updated to 1.9.0. This fixes
10
+ an issue when reading databases with a search tree exceeding 2 GB.
11
+ Reported by Sami Salonen. GitHub #146.
12
+
13
+ 2.5.1 (2023-11-09)
14
+ ++++++++++++++++++
15
+
16
+ * This is a re-release of 2.5.0 to address missing files from the sdist.
17
+ Reported by Lumír 'Frenzy' Balhar. GitHub #132.
18
+
6
19
  2.5.0 (2023-11-08)
7
20
  ++++++++++++++++++
8
21
 
@@ -1,3 +1,4 @@
1
+ exclude .* .github/**/* dev-bin/*
1
2
  include HISTORY.rst README.rst LICENSE maxminddb/py.typed maxminddb/extension.pyi
2
3
  recursive-include extension/libmaxminddb/include *.h
3
4
  recursive-include extension/libmaxminddb/src *.c *.h
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: maxminddb
3
- Version: 2.5.0
3
+ Version: 2.5.2
4
4
  Summary: Reader for the MaxMind DB format
5
5
  Author-email: Gregory Oschwald <goschwald@maxmind.com>
6
6
  License: Apache License, Version 2.0
@@ -30,4 +30,4 @@ elapsed = timeit.timeit(
30
30
  number=args.count,
31
31
  )
32
32
 
33
- print(args.count / elapsed, "lookups per second")
33
+ print("{:,}".format(int(args.count / elapsed)), "lookups per second")
@@ -0,0 +1,243 @@
1
+ #ifdef __cplusplus
2
+ extern "C" {
3
+ #endif
4
+
5
+ #ifndef MAXMINDDB_H
6
+ #define MAXMINDDB_H
7
+
8
+ #include "maxminddb_config.h"
9
+ #include <stdarg.h>
10
+ #include <stdbool.h>
11
+ #include <stdint.h>
12
+ #include <stdio.h>
13
+ #include <sys/types.h>
14
+
15
+ #ifdef _WIN32
16
+ #include <winsock2.h>
17
+ #include <ws2tcpip.h>
18
+ /* libmaxminddb package version from configure */
19
+
20
+ #if defined(_MSC_VER)
21
+ /* MSVC doesn't define signed size_t, copy it from configure */
22
+ #define ssize_t SSIZE_T
23
+
24
+ /* MSVC doesn't support restricted pointers */
25
+ #define restrict
26
+ #endif
27
+ #else
28
+ #include <netdb.h>
29
+ #include <netinet/in.h>
30
+ #include <sys/socket.h>
31
+ #endif
32
+
33
+ #define MMDB_DATA_TYPE_EXTENDED (0)
34
+ #define MMDB_DATA_TYPE_POINTER (1)
35
+ #define MMDB_DATA_TYPE_UTF8_STRING (2)
36
+ #define MMDB_DATA_TYPE_DOUBLE (3)
37
+ #define MMDB_DATA_TYPE_BYTES (4)
38
+ #define MMDB_DATA_TYPE_UINT16 (5)
39
+ #define MMDB_DATA_TYPE_UINT32 (6)
40
+ #define MMDB_DATA_TYPE_MAP (7)
41
+ #define MMDB_DATA_TYPE_INT32 (8)
42
+ #define MMDB_DATA_TYPE_UINT64 (9)
43
+ #define MMDB_DATA_TYPE_UINT128 (10)
44
+ #define MMDB_DATA_TYPE_ARRAY (11)
45
+ #define MMDB_DATA_TYPE_CONTAINER (12)
46
+ #define MMDB_DATA_TYPE_END_MARKER (13)
47
+ #define MMDB_DATA_TYPE_BOOLEAN (14)
48
+ #define MMDB_DATA_TYPE_FLOAT (15)
49
+
50
+ #define MMDB_RECORD_TYPE_SEARCH_NODE (0)
51
+ #define MMDB_RECORD_TYPE_EMPTY (1)
52
+ #define MMDB_RECORD_TYPE_DATA (2)
53
+ #define MMDB_RECORD_TYPE_INVALID (3)
54
+
55
+ /* flags for open */
56
+ #define MMDB_MODE_MMAP (1)
57
+ #define MMDB_MODE_MASK (7)
58
+
59
+ /* error codes */
60
+ #define MMDB_SUCCESS (0)
61
+ #define MMDB_FILE_OPEN_ERROR (1)
62
+ #define MMDB_CORRUPT_SEARCH_TREE_ERROR (2)
63
+ #define MMDB_INVALID_METADATA_ERROR (3)
64
+ #define MMDB_IO_ERROR (4)
65
+ #define MMDB_OUT_OF_MEMORY_ERROR (5)
66
+ #define MMDB_UNKNOWN_DATABASE_FORMAT_ERROR (6)
67
+ #define MMDB_INVALID_DATA_ERROR (7)
68
+ #define MMDB_INVALID_LOOKUP_PATH_ERROR (8)
69
+ #define MMDB_LOOKUP_PATH_DOES_NOT_MATCH_DATA_ERROR (9)
70
+ #define MMDB_INVALID_NODE_NUMBER_ERROR (10)
71
+ #define MMDB_IPV6_LOOKUP_IN_IPV4_DATABASE_ERROR (11)
72
+
73
+ #if !(MMDB_UINT128_IS_BYTE_ARRAY)
74
+ #if MMDB_UINT128_USING_MODE
75
+ typedef unsigned int mmdb_uint128_t __attribute__((__mode__(TI)));
76
+ #else
77
+ typedef unsigned __int128 mmdb_uint128_t;
78
+ #endif
79
+ #endif
80
+
81
+ /* This is a pointer into the data section for a given IP address lookup */
82
+ typedef struct MMDB_entry_s {
83
+ const struct MMDB_s *mmdb;
84
+ uint32_t offset;
85
+ } MMDB_entry_s;
86
+
87
+ typedef struct MMDB_lookup_result_s {
88
+ bool found_entry;
89
+ MMDB_entry_s entry;
90
+ uint16_t netmask;
91
+ } MMDB_lookup_result_s;
92
+
93
+ typedef struct MMDB_entry_data_s {
94
+ bool has_data;
95
+ union {
96
+ uint32_t pointer;
97
+ const char *utf8_string;
98
+ double double_value;
99
+ const uint8_t *bytes;
100
+ uint16_t uint16;
101
+ uint32_t uint32;
102
+ int32_t int32;
103
+ uint64_t uint64;
104
+ #if MMDB_UINT128_IS_BYTE_ARRAY
105
+ uint8_t uint128[16];
106
+ #else
107
+ mmdb_uint128_t uint128;
108
+ #endif
109
+ bool boolean;
110
+ float float_value;
111
+ };
112
+ /* This is a 0 if a given entry cannot be found. This can only happen
113
+ * when a call to MMDB_(v)get_value() asks for hash keys or array
114
+ * indices that don't exist. */
115
+ uint32_t offset;
116
+ /* This is the next entry in the data section, but it's really only
117
+ * relevant for entries that part of a larger map or array
118
+ * struct. There's no good reason for an end user to look at this
119
+ * directly. */
120
+ uint32_t offset_to_next;
121
+ /* This is only valid for strings, utf8_strings or binary data */
122
+ uint32_t data_size;
123
+ /* This is an MMDB_DATA_TYPE_* constant */
124
+ uint32_t type;
125
+ } MMDB_entry_data_s;
126
+
127
+ /* This is the return type when someone asks for all the entry data in a map or
128
+ * array */
129
+ typedef struct MMDB_entry_data_list_s {
130
+ MMDB_entry_data_s entry_data;
131
+ struct MMDB_entry_data_list_s *next;
132
+ void *pool;
133
+ } MMDB_entry_data_list_s;
134
+
135
+ typedef struct MMDB_description_s {
136
+ const char *language;
137
+ const char *description;
138
+ } MMDB_description_s;
139
+
140
+ /* WARNING: do not add new fields to this struct without bumping the SONAME.
141
+ * The struct is allocated by the users of this library and increasing the
142
+ * size will cause existing users to allocate too little space when the shared
143
+ * library is upgraded */
144
+ typedef struct MMDB_metadata_s {
145
+ uint32_t node_count;
146
+ uint16_t record_size;
147
+ uint16_t ip_version;
148
+ const char *database_type;
149
+ struct {
150
+ size_t count;
151
+ const char **names;
152
+ } languages;
153
+ uint16_t binary_format_major_version;
154
+ uint16_t binary_format_minor_version;
155
+ uint64_t build_epoch;
156
+ struct {
157
+ size_t count;
158
+ MMDB_description_s **descriptions;
159
+ } description;
160
+ /* See above warning before adding fields */
161
+ } MMDB_metadata_s;
162
+
163
+ /* WARNING: do not add new fields to this struct without bumping the SONAME.
164
+ * The struct is allocated by the users of this library and increasing the
165
+ * size will cause existing users to allocate too little space when the shared
166
+ * library is upgraded */
167
+ typedef struct MMDB_ipv4_start_node_s {
168
+ uint16_t netmask;
169
+ uint32_t node_value;
170
+ /* See above warning before adding fields */
171
+ } MMDB_ipv4_start_node_s;
172
+
173
+ /* WARNING: do not add new fields to this struct without bumping the SONAME.
174
+ * The struct is allocated by the users of this library and increasing the
175
+ * size will cause existing users to allocate too little space when the shared
176
+ * library is upgraded */
177
+ typedef struct MMDB_s {
178
+ uint32_t flags;
179
+ const char *filename;
180
+ ssize_t file_size;
181
+ const uint8_t *file_content;
182
+ const uint8_t *data_section;
183
+ uint32_t data_section_size;
184
+ const uint8_t *metadata_section;
185
+ uint32_t metadata_section_size;
186
+ uint16_t full_record_byte_size;
187
+ uint16_t depth;
188
+ MMDB_ipv4_start_node_s ipv4_start_node;
189
+ MMDB_metadata_s metadata;
190
+ /* See above warning before adding fields */
191
+ } MMDB_s;
192
+
193
+ typedef struct MMDB_search_node_s {
194
+ uint64_t left_record;
195
+ uint64_t right_record;
196
+ uint8_t left_record_type;
197
+ uint8_t right_record_type;
198
+ MMDB_entry_s left_record_entry;
199
+ MMDB_entry_s right_record_entry;
200
+ } MMDB_search_node_s;
201
+
202
+ extern int
203
+ MMDB_open(const char *const filename, uint32_t flags, MMDB_s *const mmdb);
204
+ extern MMDB_lookup_result_s MMDB_lookup_string(const MMDB_s *const mmdb,
205
+ const char *const ipstr,
206
+ int *const gai_error,
207
+ int *const mmdb_error);
208
+ extern MMDB_lookup_result_s
209
+ MMDB_lookup_sockaddr(const MMDB_s *const mmdb,
210
+ const struct sockaddr *const sockaddr,
211
+ int *const mmdb_error);
212
+ extern int MMDB_read_node(const MMDB_s *const mmdb,
213
+ uint32_t node_number,
214
+ MMDB_search_node_s *const node);
215
+ extern int MMDB_get_value(MMDB_entry_s *const start,
216
+ MMDB_entry_data_s *const entry_data,
217
+ ...);
218
+ extern int MMDB_vget_value(MMDB_entry_s *const start,
219
+ MMDB_entry_data_s *const entry_data,
220
+ va_list va_path);
221
+ extern int MMDB_aget_value(MMDB_entry_s *const start,
222
+ MMDB_entry_data_s *const entry_data,
223
+ const char *const *const path);
224
+ extern int MMDB_get_metadata_as_entry_data_list(
225
+ const MMDB_s *const mmdb, MMDB_entry_data_list_s **const entry_data_list);
226
+ extern int
227
+ MMDB_get_entry_data_list(MMDB_entry_s *start,
228
+ MMDB_entry_data_list_s **const entry_data_list);
229
+ extern void
230
+ MMDB_free_entry_data_list(MMDB_entry_data_list_s *const entry_data_list);
231
+ extern void MMDB_close(MMDB_s *const mmdb);
232
+ extern const char *MMDB_lib_version(void);
233
+ extern int
234
+ MMDB_dump_entry_data_list(FILE *const stream,
235
+ MMDB_entry_data_list_s *const entry_data_list,
236
+ int indent);
237
+ extern const char *MMDB_strerror(int error_code);
238
+
239
+ #endif /* MAXMINDDB_H */
240
+
241
+ #ifdef __cplusplus
242
+ }
243
+ #endif
@@ -0,0 +1,171 @@
1
+ #ifndef _POSIX_C_SOURCE
2
+ #define _POSIX_C_SOURCE 200809L
3
+ #endif
4
+
5
+ #include "data-pool.h"
6
+ #include "maxminddb.h"
7
+
8
+ #include <stdbool.h>
9
+ #include <stddef.h>
10
+ #include <stdlib.h>
11
+
12
+ // Allocate an MMDB_data_pool_s. It initially has space for size
13
+ // MMDB_entry_data_list_s structs.
14
+ MMDB_data_pool_s *data_pool_new(size_t const size) {
15
+ MMDB_data_pool_s *const pool = calloc(1, sizeof(MMDB_data_pool_s));
16
+ if (!pool) {
17
+ return NULL;
18
+ }
19
+
20
+ if (size == 0 ||
21
+ !can_multiply(SIZE_MAX, size, sizeof(MMDB_entry_data_list_s))) {
22
+ data_pool_destroy(pool);
23
+ return NULL;
24
+ }
25
+ pool->size = size;
26
+ pool->blocks[0] = calloc(pool->size, sizeof(MMDB_entry_data_list_s));
27
+ if (!pool->blocks[0]) {
28
+ data_pool_destroy(pool);
29
+ return NULL;
30
+ }
31
+ pool->blocks[0]->pool = pool;
32
+
33
+ pool->sizes[0] = size;
34
+
35
+ pool->block = pool->blocks[0];
36
+
37
+ return pool;
38
+ }
39
+
40
+ // Determine if we can multiply m*n. We can do this if the result will be below
41
+ // the given max. max will typically be SIZE_MAX.
42
+ //
43
+ // We want to know if we'll wrap around.
44
+ bool can_multiply(size_t const max, size_t const m, size_t const n) {
45
+ if (m == 0) {
46
+ return false;
47
+ }
48
+
49
+ return n <= max / m;
50
+ }
51
+
52
+ // Clean up the data pool.
53
+ void data_pool_destroy(MMDB_data_pool_s *const pool) {
54
+ if (!pool) {
55
+ return;
56
+ }
57
+
58
+ for (size_t i = 0; i <= pool->index; i++) {
59
+ free(pool->blocks[i]);
60
+ }
61
+
62
+ free(pool);
63
+ }
64
+
65
+ // Claim a new struct from the pool. Doing this may cause the pool's size to
66
+ // grow.
67
+ MMDB_entry_data_list_s *data_pool_alloc(MMDB_data_pool_s *const pool) {
68
+ if (!pool) {
69
+ return NULL;
70
+ }
71
+
72
+ if (pool->used < pool->size) {
73
+ MMDB_entry_data_list_s *const element = pool->block + pool->used;
74
+ pool->used++;
75
+ return element;
76
+ }
77
+
78
+ // Take it from a new block of memory.
79
+
80
+ size_t const new_index = pool->index + 1;
81
+ if (new_index == DATA_POOL_NUM_BLOCKS) {
82
+ // See the comment about not growing this on DATA_POOL_NUM_BLOCKS.
83
+ return NULL;
84
+ }
85
+
86
+ if (!can_multiply(SIZE_MAX, pool->size, 2)) {
87
+ return NULL;
88
+ }
89
+ size_t const new_size = pool->size * 2;
90
+
91
+ if (!can_multiply(SIZE_MAX, new_size, sizeof(MMDB_entry_data_list_s))) {
92
+ return NULL;
93
+ }
94
+ pool->blocks[new_index] = calloc(new_size, sizeof(MMDB_entry_data_list_s));
95
+ if (!pool->blocks[new_index]) {
96
+ return NULL;
97
+ }
98
+
99
+ // We don't need to set this, but it's useful for introspection in tests.
100
+ pool->blocks[new_index]->pool = pool;
101
+
102
+ pool->index = new_index;
103
+ pool->block = pool->blocks[pool->index];
104
+
105
+ pool->size = new_size;
106
+ pool->sizes[pool->index] = pool->size;
107
+
108
+ MMDB_entry_data_list_s *const element = pool->block;
109
+ pool->used = 1;
110
+ return element;
111
+ }
112
+
113
+ // Turn the structs in the array-like pool into a linked list.
114
+ //
115
+ // Before calling this function, the list isn't linked up.
116
+ MMDB_entry_data_list_s *data_pool_to_list(MMDB_data_pool_s *const pool) {
117
+ if (!pool) {
118
+ return NULL;
119
+ }
120
+
121
+ if (pool->index == 0 && pool->used == 0) {
122
+ return NULL;
123
+ }
124
+
125
+ for (size_t i = 0; i <= pool->index; i++) {
126
+ MMDB_entry_data_list_s *const block = pool->blocks[i];
127
+
128
+ size_t size = pool->sizes[i];
129
+ if (i == pool->index) {
130
+ size = pool->used;
131
+ }
132
+
133
+ for (size_t j = 0; j < size - 1; j++) {
134
+ MMDB_entry_data_list_s *const cur = block + j;
135
+ cur->next = block + j + 1;
136
+ }
137
+
138
+ if (i < pool->index) {
139
+ MMDB_entry_data_list_s *const last = block + size - 1;
140
+ last->next = pool->blocks[i + 1];
141
+ }
142
+ }
143
+
144
+ return pool->blocks[0];
145
+ }
146
+
147
+ #ifdef TEST_DATA_POOL
148
+
149
+ #include <libtap/tap.h>
150
+ #include <maxminddb_test_helper.h>
151
+
152
+ static void test_can_multiply(void);
153
+
154
+ int main(void) {
155
+ plan(NO_PLAN);
156
+ test_can_multiply();
157
+ done_testing();
158
+ }
159
+
160
+ static void test_can_multiply(void) {
161
+ { ok(can_multiply(SIZE_MAX, 1, SIZE_MAX), "1*SIZE_MAX is ok"); }
162
+
163
+ { ok(!can_multiply(SIZE_MAX, 2, SIZE_MAX), "2*SIZE_MAX is not ok"); }
164
+
165
+ {
166
+ ok(can_multiply(SIZE_MAX, 10240, sizeof(MMDB_entry_data_list_s)),
167
+ "1024 entry_data_list_s's are okay");
168
+ }
169
+ }
170
+
171
+ #endif
@@ -0,0 +1,53 @@
1
+ #ifndef DATA_POOL_H
2
+ #define DATA_POOL_H
3
+
4
+ #include "maxminddb.h"
5
+
6
+ #include <stdbool.h>
7
+ #include <stddef.h>
8
+
9
+ // This should be large enough that we never need to grow the array of pointers
10
+ // to blocks. 32 is enough. Even starting out of with size 1 (1 struct), the
11
+ // 32nd element alone will provide 2**32 structs as we exponentially increase
12
+ // the number in each block. Being confident that we do not have to grow the
13
+ // array lets us avoid writing code to do that. That code would be risky as it
14
+ // would rarely be hit and likely not be well tested.
15
+ #define DATA_POOL_NUM_BLOCKS 32
16
+
17
+ // A pool of memory for MMDB_entry_data_list_s structs. This is so we can
18
+ // allocate multiple up front rather than one at a time for performance
19
+ // reasons.
20
+ //
21
+ // The order you add elements to it (by calling data_pool_alloc()) ends up as
22
+ // the order of the list.
23
+ //
24
+ // The memory only grows. There is no support for releasing an element you take
25
+ // back to the pool.
26
+ typedef struct MMDB_data_pool_s {
27
+ // Index of the current block we're allocating out of.
28
+ size_t index;
29
+
30
+ // The size of the current block, counting by structs.
31
+ size_t size;
32
+
33
+ // How many used in the current block, counting by structs.
34
+ size_t used;
35
+
36
+ // The current block we're allocating out of.
37
+ MMDB_entry_data_list_s *block;
38
+
39
+ // The size of each block.
40
+ size_t sizes[DATA_POOL_NUM_BLOCKS];
41
+
42
+ // An array of pointers to blocks of memory holding space for list
43
+ // elements.
44
+ MMDB_entry_data_list_s *blocks[DATA_POOL_NUM_BLOCKS];
45
+ } MMDB_data_pool_s;
46
+
47
+ bool can_multiply(size_t const, size_t const, size_t const);
48
+ MMDB_data_pool_s *data_pool_new(size_t const);
49
+ void data_pool_destroy(MMDB_data_pool_s *const);
50
+ MMDB_entry_data_list_s *data_pool_alloc(MMDB_data_pool_s *const);
51
+ MMDB_entry_data_list_s *data_pool_to_list(MMDB_data_pool_s *const);
52
+
53
+ #endif
@@ -0,0 +1,160 @@
1
+ #include <stdlib.h>
2
+ #include <string.h>
3
+
4
+ /* *INDENT-OFF* */
5
+
6
+ /* The memmem, strdup, and strndup functions were all copied from the
7
+ * FreeBSD source, along with the relevant copyright notice.
8
+ *
9
+ * It'd be nicer to simply use the functions available on the system if they
10
+ * exist, but there doesn't seem to be a good way to detect them without also
11
+ * defining things like _GNU_SOURCE, which we want to avoid, because then we
12
+ * end up _accidentally_ using GNU features without noticing, which then
13
+ * breaks on systems like OSX.
14
+ *
15
+ * C is fun! */
16
+
17
+ /* Applies to memmem implementation */
18
+ /*-
19
+ * Copyright (c) 2005 Pascal Gloor <pascal.gloor@spale.com>
20
+ *
21
+ * Redistribution and use in source and binary forms, with or without
22
+ * modification, are permitted provided that the following conditions
23
+ * are met:
24
+ * 1. Redistributions of source code must retain the above copyright
25
+ * notice, this list of conditions and the following disclaimer.
26
+ * 2. Redistributions in binary form must reproduce the above copyright
27
+ * notice, this list of conditions and the following disclaimer in the
28
+ * documentation and/or other materials provided with the distribution.
29
+ * 3. The name of the author may not be used to endorse or promote
30
+ * products derived from this software without specific prior written
31
+ * permission.
32
+ *
33
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
34
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
35
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
37
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
38
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
39
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
41
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
42
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43
+ * SUCH DAMAGE.
44
+ */
45
+ static const void *
46
+ mmdb_memmem(const void *l, size_t l_len, const void *s, size_t s_len) {
47
+ const char *cur, *last;
48
+ const char *cl = (const char *)l;
49
+ const char *cs = (const char *)s;
50
+
51
+ /* we need something to compare */
52
+ if (l_len == 0 || s_len == 0)
53
+ return NULL;
54
+
55
+ /* "s" must be smaller or equal to "l" */
56
+ if (l_len < s_len)
57
+ return NULL;
58
+
59
+ /* special case where s_len == 1 */
60
+ if (s_len == 1)
61
+ return memchr(l, (int)*cs, l_len);
62
+
63
+ /* the last position where its possible to find "s" in "l" */
64
+ last = cl + l_len - s_len;
65
+
66
+ for (cur = cl; cur <= last; cur++)
67
+ if (cur[0] == cs[0] && memcmp(cur, cs, s_len) == 0)
68
+ return cur;
69
+
70
+ return NULL;
71
+ }
72
+
73
+ /* Applies to strnlen implementation */
74
+ /*-
75
+ * Copyright (c) 2009 David Schultz <das@FreeBSD.org>
76
+ * All rights reserved.
77
+ *
78
+ * Redistribution and use in source and binary forms, with or without
79
+ * modification, are permitted provided that the following conditions
80
+ * are met:
81
+ * 1. Redistributions of source code must retain the above copyright
82
+ * notice, this list of conditions and the following disclaimer.
83
+ * 2. Redistributions in binary form must reproduce the above copyright
84
+ * notice, this list of conditions and the following disclaimer in the
85
+ * documentation and/or other materials provided with the distribution.
86
+ *
87
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
88
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
89
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
90
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
91
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
92
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
93
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
94
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
95
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
96
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
97
+ * SUCH DAMAGE.
98
+ */
99
+ static size_t mmdb_strnlen(const char *s, size_t maxlen) {
100
+ size_t len;
101
+
102
+ for (len = 0; len < maxlen; len++, s++) {
103
+ if (!*s)
104
+ break;
105
+ }
106
+ return (len);
107
+ }
108
+
109
+ /* Applies to strdup and strndup implementation */
110
+ /*
111
+ * Copyright (c) 1988, 1993
112
+ * The Regents of the University of California. All rights reserved.
113
+ *
114
+ * Redistribution and use in source and binary forms, with or without
115
+ * modification, are permitted provided that the following conditions
116
+ * are met:
117
+ * 1. Redistributions of source code must retain the above copyright
118
+ * notice, this list of conditions and the following disclaimer.
119
+ * 2. Redistributions in binary form must reproduce the above copyright
120
+ * notice, this list of conditions and the following disclaimer in the
121
+ * documentation and/or other materials provided with the distribution.
122
+ * 3. Neither the name of the University nor the names of its contributors
123
+ * may be used to endorse or promote products derived from this software
124
+ * without specific prior written permission.
125
+ *
126
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
127
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
128
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
129
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
130
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
131
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
132
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
133
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
134
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
135
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
136
+ * SUCH DAMAGE.
137
+ */
138
+ static char *mmdb_strdup(const char *str) {
139
+ size_t len;
140
+ char *copy;
141
+
142
+ len = strlen(str) + 1;
143
+ if ((copy = malloc(len)) == NULL)
144
+ return (NULL);
145
+ memcpy(copy, str, len);
146
+ return (copy);
147
+ }
148
+
149
+ static char *mmdb_strndup(const char *str, size_t n) {
150
+ size_t len;
151
+ char *copy;
152
+
153
+ len = mmdb_strnlen(str, n);
154
+ if ((copy = malloc(len + 1)) == NULL)
155
+ return (NULL);
156
+ memcpy(copy, str, len);
157
+ copy[len] = '\0';
158
+ return (copy);
159
+ }
160
+ /* *INDENT-ON* */