<commit_before>/* MaxMind, Inc., licenses this file to you under the Apache License, Version
 * 2.0 (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

#include "php_maxminddb.h"

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <php.h>
#include <zend.h>

#include "Zend/zend_exceptions.h"
#include "ext/standard/info.h"
#include <maxminddb.h>

#ifdef ZTS
#include <TSRM.h>
#endif

#define __STDC_FORMAT_MACROS
#include <inttypes.h>

#define PHP_MAXMINDDB_NS ZEND_NS_NAME("MaxMind", "Db")
#define PHP_MAXMINDDB_READER_NS ZEND_NS_NAME(PHP_MAXMINDDB_NS, "Reader")
#define PHP_MAXMINDDB_READER_EX_NS                                             \
    ZEND_NS_NAME(PHP_MAXMINDDB_READER_NS, "InvalidDatabaseException")

#ifdef ZEND_ENGINE_3
#define Z_MAXMINDDB_P(zv) php_maxminddb_fetch_object(Z_OBJ_P(zv))
#define _ZVAL_STRING ZVAL_STRING
#define _ZVAL_STRINGL ZVAL_STRINGL
typedef size_t strsize_t;
typedef zend_object free_obj_t;
#else
#define Z_MAXMINDDB_P(zv)                                                      \
    (maxminddb_obj *)zend_object_store_get_object(zv TSRMLS_CC)
#define _ZVAL_STRING(a, b) ZVAL_STRING(a, b, 1)
#define _ZVAL_STRINGL(a, b, c) ZVAL_STRINGL(a, b, c, 1)
typedef int strsize_t;
typedef void free_obj_t;
#endif

/* For PHP 8 compatibility */
#ifndef TSRMLS_C
#define TSRMLS_C
#endif
#ifndef TSRMLS_CC
#define TSRMLS_CC
#endif
#ifndef TSRMLS_DC
#define TSRMLS_DC
#endif
#ifndef ZEND_ACC_CTOR
#define ZEND_ACC_CTOR 0
#endif

#ifdef ZEND_ENGINE_3
typedef struct _maxminddb_obj {
    MMDB_s *mmdb;
    zend_object std;
} maxminddb_obj;
#else
typedef struct _maxminddb_obj {
    zend_object std;
    MMDB_s *mmdb;
} maxminddb_obj;
#endif

PHP_FUNCTION(maxminddb);

static int
get_record(INTERNAL_FUNCTION_PARAMETERS, zval *record, int *prefix_len);
static const MMDB_entry_data_list_s *
handle_entry_data_list(const MMDB_entry_data_list_s *entry_data_list,
                       zval *z_value TSRMLS_DC);
static const MMDB_entry_data_list_s *
handle_array(const MMDB_entry_data_list_s *entry_data_list,
             zval *z_value TSRMLS_DC);
static const MMDB_entry_data_list_s *
handle_map(const MMDB_entry_data_list_s *entry_data_list,
           zval *z_value TSRMLS_DC);
static void handle_uint128(const MMDB_entry_data_list_s *entry_data_list,
                           zval *z_value TSRMLS_DC);
static void handle_uint64(const MMDB_entry_data_list_s *entry_data_list,
                          zval *z_value TSRMLS_DC);
static void handle_uint32(const MMDB_entry_data_list_s *entry_data_list,
                          zval *z_value TSRMLS_DC);
static zend_class_entry *lookup_class(const char *name TSRMLS_DC);

#define CHECK_ALLOCATED(val)                                                   \
    if (!val) {                                                                \
        zend_error(E_ERROR, "Out of memory");                                  \
        return;                                                                \
    }

#define THROW_EXCEPTION(name, ...)                                             \
    {                                                                          \
        zend_class_entry *exception_ce = lookup_class(name TSRMLS_CC);         \
        zend_throw_exception_ex(exception_ce, 0 TSRMLS_CC, __VA_ARGS__);       \
    }

#if PHP_VERSION_ID < 50399
#define object_properties_init(zo, class_type)                                 \
    {                                                                          \
        zval *tmp;                                                             \
        zend_hash_copy((*zo).properties,                                       \
                       &class_type->default_properties,                        \
                       (copy_ctor_func_t)zval_add_ref,                         \
                       (void *)&tmp,                                           \
                       sizeof(zval *));                                        \
    }
#endif

static zend_object_handlers maxminddb_obj_handlers;
static zend_class_entry *maxminddb_ce;

static inline maxminddb_obj *
php_maxminddb_fetch_object(zend_object *obj TSRMLS_DC) {
#ifdef ZEND_ENGINE_3
    return (maxminddb_obj *)((char *)(obj)-XtOffsetOf(maxminddb_obj, std));
#else
    return (maxminddb_obj *)obj;
#endif
}

ZEND_BEGIN_ARG_INFO_EX(arginfo_maxmindbreader_construct, 0, 0, 1)
ZEND_ARG_INFO(0, db_file)
ZEND_END_ARG_INFO()

PHP_METHOD(MaxMind_Db_Reader, __construct) {
    char *db_file = NULL;
    strsize_t name_len;
    zval *_this_zval = NULL;

    if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
                                     getThis(),
                                     "Os",
                                     &_this_zval,
                                     maxminddb_ce,
                                     &db_file,
                                     &name_len) == FAILURE) {
        THROW_EXCEPTION("InvalidArgumentException",
                        "The constructor takes exactly one argument.");
        return;
    }

    if (0 != php_check_open_basedir(db_file TSRMLS_CC) ||
        0 != access(db_file, R_OK)) {
        THROW_EXCEPTION("InvalidArgumentException",
                        "The file \"%s\" does not exist or is not readable.",
                        db_file);
        return;
    }

    MMDB_s *mmdb = (MMDB_s *)emalloc(sizeof(MMDB_s));
    uint16_t status = MMDB_open(db_file, MMDB_MODE_MMAP, mmdb);

    if (MMDB_SUCCESS != status) {
        THROW_EXCEPTION(PHP_MAXMINDDB_READER_EX_NS,
                        "Error opening database file (%s). Is this a valid "
                        "MaxMind DB file?",
                        db_file);
        efree(mmdb);
        return;
    }

    maxminddb_obj *mmdb_obj = Z_MAXMINDDB_P(getThis());
    mmdb_obj->mmdb = mmdb;
}

ZEND_BEGIN_ARG_INFO_EX(arginfo_maxmindbreader_get, 0, 0, 1)
ZEND_ARG_INFO(0, ip_address)
ZEND_END_ARG_INFO()

PHP_METHOD(MaxMind_Db_Reader, get) {
    int prefix_len = 0;
    get_record(INTERNAL_FUNCTION_PARAM_PASSTHRU, return_value, &prefix_len);
}
<commit_msg>Use ecalloc rather than emalloc<commit_after>/* MaxMind, Inc., licenses this file to you under the Apache License, Version
 * 2.0 (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

#include "php_maxminddb.h"

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <php.h>
#include <zend.h>

#include "Zend/zend_exceptions.h"
#include "ext/standard/info.h"
#include <maxminddb.h>

#ifdef ZTS
#include <TSRM.h>
#endif

#define __STDC_FORMAT_MACROS
#include <inttypes.h>

#define PHP_MAXMINDDB_NS ZEND_NS_NAME("MaxMind", "Db")
#define PHP_MAXMINDDB_READER_NS ZEND_NS_NAME(PHP_MAXMINDDB_NS, "Reader")
#define PHP_MAXMINDDB_READER_EX_NS                                             \
    ZEND_NS_NAME(PHP_MAXMINDDB_READER_NS, "InvalidDatabaseException")

#ifdef ZEND_ENGINE_3
#define Z_MAXMINDDB_P(zv) php_maxminddb_fetch_object(Z_OBJ_P(zv))
#define _ZVAL_STRING ZVAL_STRING
#define _ZVAL_STRINGL ZVAL_STRINGL
typedef size_t strsize_t;
typedef zend_object free_obj_t;
#else
#define Z_MAXMINDDB_P(zv)                                                      \
    (maxminddb_obj *)zend_object_store_get_object(zv TSRMLS_CC)
#define _ZVAL_STRING(a, b) ZVAL_STRING(a, b, 1)
#define _ZVAL_STRINGL(a, b, c) ZVAL_STRINGL(a, b, c, 1)
typedef int strsize_t;
typedef void free_obj_t;
#endif

/* For PHP 8 compatibility */
#ifndef TSRMLS_C
#define TSRMLS_C
#endif
#ifndef TSRMLS_CC
#define TSRMLS_CC
#endif
#ifndef TSRMLS_DC
#define TSRMLS_DC
#endif
#ifndef ZEND_ACC_CTOR
#define ZEND_ACC_CTOR 0
#endif

#ifdef ZEND_ENGINE_3
typedef struct _maxminddb_obj {
    MMDB_s *mmdb;
    zend_object std;
} maxminddb_obj;
#else
typedef struct _maxminddb_obj {
    zend_object std;
    MMDB_s *mmdb;
} maxminddb_obj;
#endif

PHP_FUNCTION(maxminddb);

static int
get_record(INTERNAL_FUNCTION_PARAMETERS, zval *record, int *prefix_len);
static const MMDB_entry_data_list_s *
handle_entry_data_list(const MMDB_entry_data_list_s *entry_data_list,
                       zval *z_value TSRMLS_DC);
static const MMDB_entry_data_list_s *
handle_array(const MMDB_entry_data_list_s *entry_data_list,
             zval *z_value TSRMLS_DC);
static const MMDB_entry_data_list_s *
handle_map(const MMDB_entry_data_list_s *entry_data_list,
           zval *z_value TSRMLS_DC);
static void handle_uint128(const MMDB_entry_data_list_s *entry_data_list,
                           zval *z_value TSRMLS_DC);
static void handle_uint64(const MMDB_entry_data_list_s *entry_data_list,
                          zval *z_value TSRMLS_DC);
static void handle_uint32(const MMDB_entry_data_list_s *entry_data_list,
                          zval *z_value TSRMLS_DC);
static zend_class_entry *lookup_class(const char *name TSRMLS_DC);

#define CHECK_ALLOCATED(val)                                                   \
    if (!val) {                                                                \
        zend_error(E_ERROR, "Out of memory");                                  \
        return;                                                                \
    }

#define THROW_EXCEPTION(name, ...)                                             \
    {                                                                          \
        zend_class_entry *exception_ce = lookup_class(name TSRMLS_CC);         \
        zend_throw_exception_ex(exception_ce, 0 TSRMLS_CC, __VA_ARGS__);       \
    }

#if PHP_VERSION_ID < 50399
#define object_properties_init(zo, class_type)                                 \
    {                                                                          \
        zval *tmp;                                                             \
        zend_hash_copy((*zo).properties,                                       \
                       &class_type->default_properties,                        \
                       (copy_ctor_func_t)zval_add_ref,                         \
                       (void *)&tmp,                                           \
                       sizeof(zval *));                                        \
    }
#endif

static zend_object_handlers maxminddb_obj_handlers;
static zend_class_entry *maxminddb_ce;

static inline maxminddb_obj *
php_maxminddb_fetch_object(zend_object *obj TSRMLS_DC) {
#ifdef ZEND_ENGINE_3
    return (maxminddb_obj *)((char *)(obj)-XtOffsetOf(maxminddb_obj, std));
#else
    return (maxminddb_obj *)obj;
#endif
}

ZEND_BEGIN_ARG_INFO_EX(arginfo_maxmindbreader_construct, 0, 0, 1)
ZEND_ARG_INFO(0, db_file)
ZEND_END_ARG_INFO()

PHP_METHOD(MaxMind_Db_Reader, __construct) {
    char *db_file = NULL;
    strsize_t name_len;
    zval *_this_zval = NULL;

    if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
                                     getThis(),
                                     "Os",
                                     &_this_zval,
                                     maxminddb_ce,
                                     &db_file,
                                     &name_len) == FAILURE) {
        THROW_EXCEPTION("InvalidArgumentException",
                        "The constructor takes exactly one argument.");
        return;
    }

    if (0 != php_check_open_basedir(db_file TSRMLS_CC) ||
        0 != access(db_file, R_OK)) {
        THROW_EXCEPTION("InvalidArgumentException",
                        "The file \"%s\" does not exist or is not readable.",
                        db_file);
        return;
    }

    MMDB_s *mmdb = (MMDB_s *)ecalloc(1, sizeof(MMDB_s));
    uint16_t status = MMDB_open(db_file, MMDB_MODE_MMAP, mmdb);

    if (MMDB_SUCCESS != status) {
        THROW_EXCEPTION(PHP_MAXMINDDB_READER_EX_NS,
                        "Error opening database file (%s). Is this a valid "
                        "MaxMind DB file?",
                        db_file);
        efree(mmdb);
        return;
    }

    maxminddb_obj *mmdb_obj = Z_MAXMINDDB_P(getThis());
    mmdb_obj->mmdb = mmdb;
}

ZEND_BEGIN_ARG_INFO_EX(arginfo_maxmindbreader_get, 0, 0, 1)
ZEND_ARG_INFO(0, ip_address)
ZEND_END_ARG_INFO()

PHP_METHOD(MaxMind_Db_Reader, get) {
    int prefix_len = 0;
    get_record(INTERNAL_FUNCTION_PARAM_PASSTHRU, return_value, &prefix_len);
}

{'fraction_non_alphanumeric': 0.07794084100068435, 'fraction_numerical': 0.0057029883659037335, 'mean_word_length': 1.9986320109439124, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 60, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7412100', 'n_tokens_mistral': 4350, 'n_tokens_neox': 4177, 'n_words': 881}