<commit_before>
#include "php.h"
#include "php_ini.h"
#include "ext/standard/php_smart_string.h"
#include "ext/standard/php_incomplete_class.h"
#include "ext/standard/php_var.h"

#include "php_msgpack.h"
#include "msgpack_pack.h"
#include "msgpack_errors.h"

#include "msgpack/pack_define.h"
#define msgpack_pack_user smart_string*
#define msgpack_pack_inline_func(name) \
    static inline void msgpack_pack ## name
#define msgpack_pack_inline_func_cint(name) \
    static inline void msgpack_pack ## name
#define msgpack_pack_append_buffer(user, buf, len) \
    smart_string_appendl(user, (const void*)buf, len)
#include "msgpack/pack_template.h"

#if ZEND_MODULE_API_NO < 20090626
#   define Z_ISREF_P(pz) PZVAL_IS_REF(pz)
#endif

inline static int msgpack_check_ht_is_map(zval *array)
{
    int count = zend_hash_num_elements(Z_ARRVAL_P(array));

    if (count != (Z_ARRVAL_P(array))->nNextFreeElement) {
        return 1;
    } else {
        int i;
        HashPosition pos = {0};
        zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(array), &pos);
        for (i = 0; i < count; i++) {
            if (zend_hash_get_current_key_type_ex(Z_ARRVAL_P(array), &pos) != HASH_KEY_IS_LONG) {
                return 1;
            }
            zend_hash_move_forward_ex(Z_ARRVAL_P(array), &pos);
        }
    }
    return 0;
}

inline static int msgpack_var_add(
    HashTable *var_hash, zval *var, void *var_old TSRMLS_DC)
{
    ulong var_no;
    char id[32], *p;
    int len;
    zend_string *zstring;

    if ((Z_TYPE_P(var) == IS_OBJECT) && Z_OBJ_P(var)->ce) {
        p = zend_print_long_to_buf(
            id + sizeof(id) - 1,
            (((size_t)Z_OBJCE_P(var) << 5)
             | ((size_t)Z_OBJCE_P(var) >> (sizeof(long) * 8 - 5)))
            + (long)Z_OBJ_HANDLE_P(var));
        len = id + sizeof(id) - 1 - p;
    }
    else if (Z_TYPE_P(var) == IS_ARRAY) {
        p = zend_print_long_to_buf(id + sizeof(id) - 1, (long)var);
        len = id + sizeof(id) - 1 - p;
    }
    else {
        return FAILURE;
    }

    zstring =  zend_string_init(p, len, 1);
    if (var_old && (var_old = zend_hash_find(var_hash, zstring)) != NULL) {
        if (!Z_ISREF_P(var)) {
            zend_hash_next_index_insert(var_hash, NULL);
        }
        return FAILURE;
    }

    var_no = zend_hash_num_elements(var_hash) + 1;

    zend_hash_add(var_hash, zstring, NULL);

    return SUCCESS;
}

inline static void msgpack_serialize_string(
    smart_string *buf, char *str, size_t len)
{
    msgpack_pack_raw(buf, len);
    msgpack_pack_raw_body(buf, str, len);
}

inline static void msgpack_serialize_class(
    smart_string *buf, zval *val, zval *retval_ptr, HashTable *var_hash,
    char *class_name, uint32_t name_len, zend_bool incomplete_class TSRMLS_DC)
{
    int count;
    HashTable *ht = HASH_OF(retval_ptr);

    count = zend_hash_num_elements(ht);
    if (incomplete_class)
    {
        --count;
    }

    if (count > 0)
    {
        msgpack_pack_map(buf, count + 1);<commit_msg>Update msgpack_check_ht_is_map to properly call zend_hash_add<commit_after>
#include "php.h"
#include "php_ini.h"
#include "ext/standard/php_smart_string.h"
#include "ext/standard/php_incomplete_class.h"
#include "ext/standard/php_var.h"

#include "php_msgpack.h"
#include "msgpack_pack.h"
#include "msgpack_errors.h"

#include "msgpack/pack_define.h"
#define msgpack_pack_user smart_string*
#define msgpack_pack_inline_func(name) \
    static inline void msgpack_pack ## name
#define msgpack_pack_inline_func_cint(name) \
    static inline void msgpack_pack ## name
#define msgpack_pack_append_buffer(user, buf, len) \
    smart_string_appendl(user, (const void*)buf, len)
#include "msgpack/pack_template.h"

#if ZEND_MODULE_API_NO < 20090626
#   define Z_ISREF_P(pz) PZVAL_IS_REF(pz)
#endif

inline static int msgpack_check_ht_is_map(zval *array)
{
    int count = zend_hash_num_elements(Z_ARRVAL_P(array));

    if (count != (Z_ARRVAL_P(array))->nNextFreeElement) {
        return 1;
    } else {
        int i;
        HashPosition pos = {0};
        zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(array), &pos);
        for (i = 0; i < count; i++) {
            if (zend_hash_get_current_key_type_ex(Z_ARRVAL_P(array), &pos) != HASH_KEY_IS_LONG) {
                return 1;
            }
            zend_hash_move_forward_ex(Z_ARRVAL_P(array), &pos);
        }
    }
    return 0;
}

inline static int msgpack_var_add(
    HashTable *var_hash, zval *var, void *var_old TSRMLS_DC)
{
    char id[32], *p;
    int len;
    zend_string *zstring;
    zval zv;

    if ((Z_TYPE_P(var) == IS_OBJECT) && Z_OBJ_P(var)->ce) {
        p = zend_print_long_to_buf(
            id + sizeof(id) - 1,
            (((size_t)Z_OBJCE_P(var) << 5)
             | ((size_t)Z_OBJCE_P(var) >> (sizeof(long) * 8 - 5)))
            + (long)Z_OBJ_HANDLE_P(var));
        len = id + sizeof(id) - 1 - p;
    }
    else if (Z_TYPE_P(var) == IS_ARRAY) {
        p = zend_print_long_to_buf(id + sizeof(id) - 1, (long)var);
        len = id + sizeof(id) - 1 - p;
    }
    else {
        return FAILURE;
    }

    zstring =  zend_string_init(p, len, 1);
    if (var_old && (var_old = zend_hash_find(var_hash, zstring)) != NULL) {
        if (!Z_ISREF_P(var)) {
            zend_hash_next_index_insert(var_hash, NULL);
        }
        return FAILURE;
    }

    ZVAL_LONG(&zv, zend_hash_num_elements(var_hash) + 1);
    zend_hash_add(var_hash, zstring, &zv);

    return SUCCESS;
}

inline static void msgpack_serialize_string(
    smart_string *buf, char *str, size_t len)
{
    msgpack_pack_raw(buf, len);
    msgpack_pack_raw_body(buf, str, len);
}

inline static void msgpack_serialize_class(
    smart_string *buf, zval *val, zval *retval_ptr, HashTable *var_hash,
    char *class_name, uint32_t name_len, zend_bool incomplete_class TSRMLS_DC)
{
    int count;
    HashTable *ht = HASH_OF(retval_ptr);

    count = zend_hash_num_elements(ht);
    if (incomplete_class)
    {
        --count;
    }

    if (count > 0)
    {
        msgpack_pack_map(buf, count + 1);
{'fraction_non_alphanumeric': 0.12111184645929848, 'fraction_numerical': 0.009265387160820648, 'mean_word_length': 3.0844594594594597, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7099047', 'n_tokens_mistral': 2420, 'n_tokens_neox': 2332, 'n_words': 448}