Alien-libmaxminddb

 view release on metacpan or  search on metacpan

maxminddb/src/maxminddb.c  view on Meta::CPAN

        #define UNICODE
    #endif
    #include <windows.h>
    #include <ws2ipdef.h>
    #ifndef SSIZE_MAX
        #define SSIZE_MAX INTPTR_MAX
    #endif
typedef ADDRESS_FAMILY sa_family_t;
#else
    #include <arpa/inet.h>
    #include <sys/mman.h>
    #include <unistd.h>
#endif

#define MMDB_DATA_SECTION_SEPARATOR (16)
// The maximum recursive decoder depth for a single entry. This stops unbounded
// recursion, including a pointer cycle. See "Reader Resource Limits" in the
// MaxMind DB specification.
#ifndef MAXIMUM_DATA_STRUCTURE_DEPTH
    #define MAXIMUM_DATA_STRUCTURE_DEPTH (512)
#endif

#if MAXIMUM_DATA_STRUCTURE_DEPTH < 1 || MAXIMUM_DATA_STRUCTURE_DEPTH > INT_MAX
    #error "MAXIMUM_DATA_STRUCTURE_DEPTH must be between 1 and INT_MAX"
#endif

// The maximum number of data-section values decoded for a single entry. This
// bounds a pointer fan-out, where nested pointers to shared targets would
// otherwise cost 2**depth decode operations. The largest real records decode a
// few hundred values, so this leaves a wide margin. See "Reader Resource
// Limits" in the MaxMind DB specification.
#ifndef MAXIMUM_DATA_STRUCTURE_VALUES
    #define MAXIMUM_DATA_STRUCTURE_VALUES (1U << 16)
#endif

// The upper bound matters on platforms where size_t is narrower than the
// preprocessor's integer arithmetic.
#if MAXIMUM_DATA_STRUCTURE_VALUES < 1 ||                                       \
    MAXIMUM_DATA_STRUCTURE_VALUES > SIZE_MAX
    #error "MAXIMUM_DATA_STRUCTURE_VALUES must be between 1 and SIZE_MAX"
#endif

// The maximum total bytes of string and bytes payloads decoded for a single
// entry. libmaxminddb borrows payload bytes (each node points into the data
// section, it does not copy), so the value count above already bounds the
// library's own memory. But a fan-out of pointers to one large value produces
// many nodes that all reference it. A caller that copies each node into a
// language string then materializes far more than the file holds. This bounds
// that copied total. The largest real records hold about a kilobyte of
// payload, so 2 MiB leaves a wide margin while stopping the amplification. It
// can be raised at build time with -DMAXIMUM_DATA_STRUCTURE_BYTES=<n>.
#ifndef MAXIMUM_DATA_STRUCTURE_BYTES
    #define MAXIMUM_DATA_STRUCTURE_BYTES (1U << 21)
#endif

#if MAXIMUM_DATA_STRUCTURE_BYTES < 1
    #error "MAXIMUM_DATA_STRUCTURE_BYTES must be at least 1"
#endif

#ifdef MMDB_DEBUG
    #define DEBUG_MSG(msg) fprintf(stderr, msg "\n")
    #define DEBUG_MSGF(fmt, ...) fprintf(stderr, fmt "\n", __VA_ARGS__)
    #define DEBUG_BINARY(fmt, byte)                                            \
        do {                                                                   \
            char *binary = byte_to_binary(byte);                               \
            if (NULL == binary) {                                              \
                fprintf(stderr, "Calloc failed in DEBUG_BINARY\n");            \
                abort();                                                       \
            }                                                                  \
            fprintf(stderr, fmt "\n", binary);                                 \
            free(binary);                                                      \
        } while (0)
    #define DEBUG_NL fprintf(stderr, "\n")
#else
    #define DEBUG_MSG(...)
    #define DEBUG_MSGF(...)
    #define DEBUG_BINARY(...)
    #define DEBUG_NL
#endif

#ifdef MMDB_DEBUG
char *byte_to_binary(uint8_t byte) {
    char *bits = calloc(9, sizeof(char));
    if (NULL == bits) {
        return bits;
    }

    for (uint8_t i = 0; i < 8; i++) {
        bits[i] = byte & (128 >> i) ? '1' : '0';
    }
    bits[8] = '\0';

    return bits;
}

char *type_num_to_name(uint8_t num) {
    switch (num) {
        case 0:
            return "extended";
        case 1:
            return "pointer";
        case 2:
            return "utf8_string";
        case 3:
            return "double";
        case 4:
            return "bytes";
        case 5:
            return "uint16";
        case 6:
            return "uint32";
        case 7:
            return "map";
        case 8:
            return "int32";
        case 9:
            return "uint64";
        case 10:
            return "uint128";
        case 11:
            return "array";
        case 12:
            return "container";
        case 13:
            return "end_marker";
        case 14:
            return "boolean";
        case 15:
            return "float";
        default:
            return "unknown type";
    }
}



( run in 0.743 second using v1.01-cache-2.11-cpan-60884a8df76 )