Update libmaxminddb to v1.3.2 (#526)

* Update libmaxminddb to v1.3.2

* Move PACKAGE_VERSION declaration
This commit is contained in:
Vincent Herbet 2018-08-27 15:42:35 +02:00 committed by GitHub
parent c820db4dc7
commit 9e2c76abdc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 809 additions and 219 deletions

View File

@ -13,6 +13,7 @@ binary.compiler.defines += [
binary.sources = [
'../../public/sdk/amxxmodule.cpp',
'../../third_party/libmaxminddb/data-pool.c',
'../../third_party/libmaxminddb/maxminddb.c',
'geoip_main.cpp',
'geoip_natives.cpp',

View File

@ -102,6 +102,7 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="..\..\..\third_party\libmaxminddb\data-pool.c" />
<ClCompile Include="..\..\..\third_party\libmaxminddb\maxminddb.c" />
<ClCompile Include="..\geoip_main.cpp" />
<ClCompile Include="..\geoip_natives.cpp" />
@ -109,6 +110,7 @@
<ClCompile Include="..\..\..\public\sdk\amxxmodule.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\..\third_party\libmaxminddb\data-pool.h" />
<ClInclude Include="..\..\..\third_party\libmaxminddb\maxminddb-compat-util.h" />
<ClInclude Include="..\..\..\third_party\libmaxminddb\maxminddb.h" />
<ClInclude Include="..\..\..\third_party\libmaxminddb\maxminddb_config.h" />

View File

@ -38,6 +38,9 @@
<ClCompile Include="..\..\..\third_party\libmaxminddb\maxminddb.c">
<Filter>GeoIP2</Filter>
</ClCompile>
<ClCompile Include="..\..\..\third_party\libmaxminddb\data-pool.c">
<Filter>GeoIP2</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\geoip_util.h">
@ -64,6 +67,9 @@
<ClInclude Include="..\..\..\third_party\libmaxminddb\maxminddb-compat-util.h">
<Filter>GeoIP2</Filter>
</ClInclude>
<ClInclude Include="..\..\..\third_party\libmaxminddb\data-pool.h">
<Filter>GeoIP2</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="..\..\..\plugins\include\geoip.inc">

180
third_party/libmaxminddb/data-pool.c vendored Normal file
View File

@ -0,0 +1,180 @@
#include "data-pool.h"
#include "maxminddb.h"
#include <stdbool.h>
#include <stddef.h>
#include <stdlib.h>
static bool can_multiply(size_t const, size_t const, size_t const);
// Allocate an MMDB_data_pool_s. It initially has space for size
// MMDB_entry_data_list_s structs.
MMDB_data_pool_s *data_pool_new(size_t const size)
{
MMDB_data_pool_s *const pool = calloc(1, sizeof(MMDB_data_pool_s));
if (!pool) {
return NULL;
}
if (size == 0 ||
!can_multiply(SIZE_MAX, size, sizeof(MMDB_entry_data_list_s))) {
data_pool_destroy(pool);
return NULL;
}
pool->size = size;
pool->blocks[0] = calloc(pool->size, sizeof(MMDB_entry_data_list_s));
if (!pool->blocks[0]) {
data_pool_destroy(pool);
return NULL;
}
pool->blocks[0]->pool = pool;
pool->sizes[0] = size;
pool->block = pool->blocks[0];
return pool;
}
// Determine if we can multiply m*n. We can do this if the result will be below
// the given max. max will typically be SIZE_MAX.
//
// We want to know if we'll wrap around.
static bool can_multiply(size_t const max, size_t const m, size_t const n)
{
if (m == 0) {
return false;
}
return n <= max / m;
}
// Clean up the data pool.
void data_pool_destroy(MMDB_data_pool_s *const pool)
{
if (!pool) {
return;
}
for (size_t i = 0; i <= pool->index; i++) {
free(pool->blocks[i]);
}
free(pool);
}
// Claim a new struct from the pool. Doing this may cause the pool's size to
// grow.
MMDB_entry_data_list_s *data_pool_alloc(MMDB_data_pool_s *const pool)
{
if (!pool) {
return NULL;
}
if (pool->used < pool->size) {
MMDB_entry_data_list_s *const element = pool->block + pool->used;
pool->used++;
return element;
}
// Take it from a new block of memory.
size_t const new_index = pool->index + 1;
if (new_index == DATA_POOL_NUM_BLOCKS) {
// See the comment about not growing this on DATA_POOL_NUM_BLOCKS.
return NULL;
}
if (!can_multiply(SIZE_MAX, pool->size, 2)) {
return NULL;
}
size_t const new_size = pool->size * 2;
if (!can_multiply(SIZE_MAX, new_size, sizeof(MMDB_entry_data_list_s))) {
return NULL;
}
pool->blocks[new_index] = calloc(new_size, sizeof(MMDB_entry_data_list_s));
if (!pool->blocks[new_index]) {
return NULL;
}
// We don't need to set this, but it's useful for introspection in tests.
pool->blocks[new_index]->pool = pool;
pool->index = new_index;
pool->block = pool->blocks[pool->index];
pool->size = new_size;
pool->sizes[pool->index] = pool->size;
MMDB_entry_data_list_s *const element = pool->block;
pool->used = 1;
return element;
}
// Turn the structs in the array-like pool into a linked list.
//
// Before calling this function, the list isn't linked up.
MMDB_entry_data_list_s *data_pool_to_list(MMDB_data_pool_s *const pool)
{
if (!pool) {
return NULL;
}
if (pool->index == 0 && pool->used == 0) {
return NULL;
}
for (size_t i = 0; i <= pool->index; i++) {
MMDB_entry_data_list_s *const block = pool->blocks[i];
size_t size = pool->sizes[i];
if (i == pool->index) {
size = pool->used;
}
for (size_t j = 0; j < size - 1; j++) {
MMDB_entry_data_list_s *const cur = block + j;
cur->next = block + j + 1;
}
if (i < pool->index) {
MMDB_entry_data_list_s *const last = block + size - 1;
last->next = pool->blocks[i + 1];
}
}
return pool->blocks[0];
}
#ifdef TEST_DATA_POOL
#include <libtap/tap.h>
#include <maxminddb_test_helper.h>
static void test_can_multiply(void);
int main(void)
{
plan(NO_PLAN);
test_can_multiply();
done_testing();
}
static void test_can_multiply(void)
{
{
ok(can_multiply(SIZE_MAX, 1, SIZE_MAX), "1*SIZE_MAX is ok");
}
{
ok(!can_multiply(SIZE_MAX, 2, SIZE_MAX), "2*SIZE_MAX is not ok");
}
{
ok(can_multiply(SIZE_MAX, 10240, sizeof(MMDB_entry_data_list_s)),
"1024 entry_data_list_s's are okay");
}
}
#endif

52
third_party/libmaxminddb/data-pool.h vendored Normal file
View File

@ -0,0 +1,52 @@
#ifndef DATA_POOL_H
#define DATA_POOL_H
#include "maxminddb.h"
#include <stdbool.h>
#include <stddef.h>
// This should be large enough that we never need to grow the array of pointers
// to blocks. 32 is enough. Even starting out of with size 1 (1 struct), the
// 32nd element alone will provide 2**32 structs as we exponentially increase
// the number in each block. Being confident that we do not have to grow the
// array lets us avoid writing code to do that. That code would be risky as it
// would rarely be hit and likely not be well tested.
#define DATA_POOL_NUM_BLOCKS 32
// A pool of memory for MMDB_entry_data_list_s structs. This is so we can
// allocate multiple up front rather than one at a time for performance
// reasons.
//
// The order you add elements to it (by calling data_pool_alloc()) ends up as
// the order of the list.
//
// The memory only grows. There is no support for releasing an element you take
// back to the pool.
typedef struct MMDB_data_pool_s {
// Index of the current block we're allocating out of.
size_t index;
// The size of the current block, counting by structs.
size_t size;
// How many used in the current block, counting by structs.
size_t used;
// The current block we're allocating out of.
MMDB_entry_data_list_s *block;
// The size of each block.
size_t sizes[DATA_POOL_NUM_BLOCKS];
// An array of pointers to blocks of memory holding space for list
// elements.
MMDB_entry_data_list_s *blocks[DATA_POOL_NUM_BLOCKS];
} MMDB_data_pool_s;
MMDB_data_pool_s *data_pool_new(size_t const);
void data_pool_destroy(MMDB_data_pool_s *const);
MMDB_entry_data_list_s *data_pool_alloc(MMDB_data_pool_s *const);
MMDB_entry_data_list_s *data_pool_to_list(MMDB_data_pool_s *const);
#endif

File diff suppressed because it is too large Load Diff

View File

@ -5,10 +5,21 @@ extern "C" {
#ifndef MAXMINDDB_H
#define MAXMINDDB_H
/* Request POSIX.1-2008. However, we want to remain compatible with
* POSIX.1-2001 (since we have been historically and see no reason to drop
* compatibility). By requesting POSIX.1-2008, we can conditionally use
* features provided by that standard if the implementation provides it. We can
* check for what the implementation provides by checking the _POSIX_VERSION
* macro after including unistd.h. If a feature is in POSIX.1-2008 but not
* POSIX.1-2001, check that macro before using the feature (or check for the
* feature directly if possible). */
#ifndef _POSIX_C_SOURCE
#define _POSIX_C_SOURCE 200112L
#define _POSIX_C_SOURCE 200809L
#endif
/* libmaxminddb package version from configure */
#define PACKAGE_VERSION "1.3.2"
#include "maxminddb_config.h"
#include <stdarg.h>
#include <stdbool.h>
@ -24,7 +35,7 @@ typedef ADDRESS_FAMILY sa_family_t;
#if defined(_MSC_VER)
/* MSVC doesn't define signed size_t, copy it from configure */
#define ssize_t int
#define ssize_t SSIZE_T
/* MSVC doesn't support restricted pointers */
#define restrict
@ -35,9 +46,6 @@ typedef ADDRESS_FAMILY sa_family_t;
#include <sys/socket.h>
#endif
/* libmaxminddb package version from configure */
#define PACKAGE_VERSION "1.1.0"
#define MMDB_DATA_TYPE_EXTENDED (0)
#define MMDB_DATA_TYPE_POINTER (1)
#define MMDB_DATA_TYPE_UTF8_STRING (2)
@ -55,6 +63,11 @@ typedef ADDRESS_FAMILY sa_family_t;
#define MMDB_DATA_TYPE_BOOLEAN (14)
#define MMDB_DATA_TYPE_FLOAT (15)
#define MMDB_RECORD_TYPE_SEARCH_NODE (0)
#define MMDB_RECORD_TYPE_EMPTY (1)
#define MMDB_RECORD_TYPE_DATA (2)
#define MMDB_RECORD_TYPE_INVALID (3)
/* flags for open */
#define MMDB_MODE_MMAP (1)
#define MMDB_MODE_MASK (7)
@ -131,6 +144,7 @@ typedef struct MMDB_entry_data_s {
typedef struct MMDB_entry_data_list_s {
MMDB_entry_data_s entry_data;
struct MMDB_entry_data_list_s *next;
void *pool;
} MMDB_entry_data_list_s;
typedef struct MMDB_description_s {
@ -179,6 +193,10 @@ typedef struct MMDB_s {
typedef struct MMDB_search_node_s {
uint64_t left_record;
uint64_t right_record;
uint8_t left_record_type;
uint8_t right_record_type;
MMDB_entry_s left_record_entry;
MMDB_entry_s right_record_entry;
} MMDB_search_node_s;
/* *INDENT-OFF* */