mirror of
https://github.com/VCMP-SqMod/SqMod.git
synced 2025-07-15 05:17:11 +02:00
Integrate MaxmindDB module.
This commit is contained in:
0
module/Vendor/MaxmindDB/src/CMakeLists.txt
vendored
Normal file
0
module/Vendor/MaxmindDB/src/CMakeLists.txt
vendored
Normal file
25
module/Vendor/MaxmindDB/src/Makefile.am
vendored
Normal file
25
module/Vendor/MaxmindDB/src/Makefile.am
vendored
Normal file
@ -0,0 +1,25 @@
|
||||
include $(top_srcdir)/common.mk
|
||||
|
||||
lib_LTLIBRARIES = libmaxminddb.la
|
||||
|
||||
libmaxminddb_la_SOURCES = maxminddb.c maxminddb-compat-util.h \
|
||||
data-pool.c data-pool.h
|
||||
libmaxminddb_la_LDFLAGS = -version-info 0:7:0 -export-symbols-regex '^MMDB_.*'
|
||||
include_HEADERS = $(top_srcdir)/include/maxminddb.h
|
||||
|
||||
pkgconfig_DATA = libmaxminddb.pc
|
||||
|
||||
TESTS = test-data-pool
|
||||
|
||||
check_PROGRAMS = test-data-pool
|
||||
|
||||
test_data_pool_SOURCES = data-pool.c data-pool.h
|
||||
test_data_pool_CPPFLAGS = $(AM_CPPFLAGS) -I$(top_srcdir)/t -DTEST_DATA_POOL
|
||||
test_data_pool_LDADD = $(top_srcdir)/t/libmmdbtest.la \
|
||||
$(top_srcdir)/t/libtap/libtap.a
|
||||
|
||||
$(top_srcdir)/t/libmmdbtest.la:
|
||||
$(MAKE) -C $(top_srcdir)/t libmmdbtest.la
|
||||
|
||||
$(top_srcdir)/t/libtap/libtap.a:
|
||||
$(MAKE) -C $(top_srcdir)/t/libtap libtap.a
|
180
module/Vendor/MaxmindDB/src/data-pool.c
vendored
Normal file
180
module/Vendor/MaxmindDB/src/data-pool.c
vendored
Normal 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
module/Vendor/MaxmindDB/src/data-pool.h
vendored
Normal file
52
module/Vendor/MaxmindDB/src/data-pool.h
vendored
Normal 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
|
11
module/Vendor/MaxmindDB/src/libmaxminddb.pc.in
vendored
Normal file
11
module/Vendor/MaxmindDB/src/libmaxminddb.pc.in
vendored
Normal file
@ -0,0 +1,11 @@
|
||||
prefix=@prefix@
|
||||
exec_prefix=@prefix@
|
||||
libdir=@libdir@
|
||||
includedir=@includedir@
|
||||
|
||||
Name: libmaxminddb
|
||||
Description: C library for the MaxMind DB file format
|
||||
URL: http://maxmind.github.io/libmaxminddb/
|
||||
Version: @PACKAGE_VERSION@
|
||||
Libs: -L${libdir} -lmaxminddb
|
||||
Cflags: -I${includedir}
|
167
module/Vendor/MaxmindDB/src/maxminddb-compat-util.h
vendored
Normal file
167
module/Vendor/MaxmindDB/src/maxminddb-compat-util.h
vendored
Normal file
@ -0,0 +1,167 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
/* *INDENT-OFF* */
|
||||
|
||||
/* The memmem, strdup, and strndup functions were all copied from the
|
||||
* FreeBSD source, along with the relevant copyright notice.
|
||||
*
|
||||
* It'd be nicer to simply use the functions available on the system if they
|
||||
* exist, but there doesn't seem to be a good way to detect them without also
|
||||
* defining things like _GNU_SOURCE, which we want to avoid, because then we
|
||||
* end up _accidentally_ using GNU features without noticing, which then
|
||||
* breaks on systems like OSX.
|
||||
*
|
||||
* C is fun! */
|
||||
|
||||
/* Applies to memmem implementation */
|
||||
/*-
|
||||
* Copyright (c) 2005 Pascal Gloor <pascal.gloor@spale.com>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote
|
||||
* products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
static void *
|
||||
mmdb_memmem(const void *l, size_t l_len, const void *s, size_t s_len)
|
||||
{
|
||||
register char *cur, *last;
|
||||
const char *cl = (const char *)l;
|
||||
const char *cs = (const char *)s;
|
||||
|
||||
/* we need something to compare */
|
||||
if (l_len == 0 || s_len == 0)
|
||||
return NULL;
|
||||
|
||||
/* "s" must be smaller or equal to "l" */
|
||||
if (l_len < s_len)
|
||||
return NULL;
|
||||
|
||||
/* special case where s_len == 1 */
|
||||
if (s_len == 1)
|
||||
return memchr(l, (int)*cs, l_len);
|
||||
|
||||
/* the last position where its possible to find "s" in "l" */
|
||||
last = (char *)cl + l_len - s_len;
|
||||
|
||||
for (cur = (char *)cl; cur <= last; cur++)
|
||||
if (cur[0] == cs[0] && memcmp(cur, cs, s_len) == 0)
|
||||
return cur;
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Applies to strnlen implementation */
|
||||
/*-
|
||||
* Copyright (c) 2009 David Schultz <das@FreeBSD.org>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
static size_t
|
||||
mmdb_strnlen(const char *s, size_t maxlen)
|
||||
{
|
||||
size_t len;
|
||||
|
||||
for (len = 0; len < maxlen; len++, s++) {
|
||||
if (!*s)
|
||||
break;
|
||||
}
|
||||
return (len);
|
||||
}
|
||||
|
||||
/* Applies to strdup and strndup implementation */
|
||||
/*
|
||||
* Copyright (c) 1988, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
static char *
|
||||
mmdb_strdup(const char *str)
|
||||
{
|
||||
size_t len;
|
||||
char *copy;
|
||||
|
||||
len = strlen(str) + 1;
|
||||
if ((copy = malloc(len)) == NULL)
|
||||
return (NULL);
|
||||
memcpy(copy, str, len);
|
||||
return (copy);
|
||||
}
|
||||
|
||||
static char *
|
||||
mmdb_strndup(const char *str, size_t n)
|
||||
{
|
||||
size_t len;
|
||||
char *copy;
|
||||
|
||||
len = mmdb_strnlen(str, n);
|
||||
if ((copy = malloc(len + 1)) == NULL)
|
||||
return (NULL);
|
||||
memcpy(copy, str, len);
|
||||
copy[len] = '\0';
|
||||
return (copy);
|
||||
}
|
||||
/* *INDENT-ON* */
|
2154
module/Vendor/MaxmindDB/src/maxminddb.c
vendored
Normal file
2154
module/Vendor/MaxmindDB/src/maxminddb.c
vendored
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user