Commit 189c8e58 authored by Frederick Zhang's avatar Frederick Zhang

Merge branch 'dev'

parents 514319d3 f89d8112
Pipeline #348 passed with stage
in 1 minute and 23 seconds
.vscode/
autom4te.cache/
build/
include/
......
......@@ -3,7 +3,7 @@ image: php:latest
before_script:
- export NO_INTERACTION=1
- apt-get -y update
- apt-get -y install autoconf build-essential gcc g++
- apt-get -y install autoconf build-essential gcc g++ valgrind
build:
stage: build
......@@ -11,7 +11,7 @@ build:
- phpize
- ./configure --enable-bencode
- make -j$(nproc)
- make test
- make test TESTS="-m"
artifacts:
name: "${CI_BUILD_NAME}_${CI_BUILD_REF_NAME}"
expire_in: 2 weeks
......
......@@ -12,6 +12,7 @@ addons:
- build-essential
- gcc-5
- g++-5
- valgrind
php:
- '7.1'
......@@ -25,4 +26,4 @@ script:
- phpize
- ./configure --enable-bencode
- make -j$(nproc)
- make test
- make test TESTS="-m"
This diff is collapsed.
This diff is collapsed.
......@@ -13,7 +13,7 @@
void zend_container::bclass##_free_storage(zend_object *object TSRMLS_DC) \
{ \
bclass##_object *intern = zend_container::bclass##_fetch_object(object); \
delete intern->bclass##_data; \
delete intern->bnode_data; \
zend_object_std_dtor(&intern->std TSRMLS_CC); \
} \
zend_object * zend_container::bclass##_object_new(zend_class_entry *ce TSRMLS_DC) \
......@@ -31,8 +31,8 @@
bclass##_object *old_object = zend_container::bclass##_fetch_object(Z_OBJ_P(object)); \
zend_object *new_zend_object = zend_container::bclass##_object_new(zend_container::bclass##_ce); \
bclass##_object *new_object = zend_container::bclass##_fetch_object(new_zend_object); \
bclass *new_object_data = new bclass(old_object->bclass##_data); \
new_object->bclass##_data = new_object_data; \
bclass *new_object_data = new bclass(old_object->bnode_data); \
new_object->bnode_data = new_object_data; \
return &new_object->std; \
}
......
......@@ -27,17 +27,16 @@ size_t bint::length() const {
zval * bint::parse(const std::string &ben, size_t &pt) {
if (ben[pt] != 'i')
return bitem::throw_general_exception("Error parsing bint");
++pt;
std::string strint = "";
while (ben[pt] != 'e') strint += ben[pt++];
long result = std::stoll(strint);
size_t start = ++pt;
while (ben[pt] != 'e') ++pt;
long result = std::stoll(ben.substr(start, pt - start));
++pt;
zval *zv = new zval();
zend_object *zo = zend_container::bint_object_new(zend_container::bint_ce);
ZVAL_OBJ(zv, zo);
bint_object *intern = zend_container::bint_fetch_object(Z_OBJ_P(zv));
intern->bint_data = new bint(result);
intern->bnode_data = new bint(result);
return zv;
}
......@@ -49,19 +48,21 @@ zval * bint::to_array(const bool include_meta) const {
zval *zv = new zval();
if (include_meta) {
array_init(zv);
char *_type = estrdup("_type");
char *_type_data = estrdup("bint");
char *_length = estrdup("_length");
char *_data = estrdup("_data");
add_assoc_string(zv, _type, _type_data);
add_assoc_long(zv, _length, length());
add_assoc_long(zv, _data, _value);
efree(_type);
efree(_type_data);
efree(_length);
efree(_data);
add_assoc_string(zv, (char *)"_type", (char *)"bint");
add_assoc_long(zv, (char *)"_length", length());
add_assoc_long(zv, (char *)"_data", _value);
} else {
ZVAL_LONG(zv, _value);
}
return zv;
}
zval * bint::search(const std::string &needle, const long &mode, const std::string path) const {
zval *zv = new zval();
array_init(zv);
if (mode == 1 && bitem::is_ll(needle) && _value == std::stoll(needle))
add_next_index_stringl(zv, path.c_str(), path.length());
return zv;
}
......@@ -28,6 +28,7 @@ class bint : public bitem {
static zval * parse(const std::string &ben, size_t &pt);
std::string encode() const;
zval * to_array(const bool include_meta) const;
zval * search(const std::string &needle, const long &mode, const std::string path) const;
};
#endif
......@@ -23,7 +23,7 @@ std::string bitem::get_current_key(const std::string &path, size_t &pt) {
std::string current_key = path.substr(start, pt - start);
++pt;
size_t escape = current_key.find("\\/");
while (escape >= 0 && escape < current_key.length()) {
while (escape < current_key.length()) {
current_key.replace(escape, 2, "/");
escape = current_key.find("\\/");
}
......@@ -66,9 +66,9 @@ void bitem::zend_hash_append_strings(HashTable *target, HashTable *source) {
for(zend_hash_internal_pointer_reset(source);
zend_hash_has_more_elements(source) == SUCCESS;
zend_hash_move_forward(source)) {
zend_string *_str_index;
zend_string *str_index;
zend_ulong num_index;
zend_hash_get_current_key(source, &_str_index, &num_index);
zend_hash_get_current_key(source, &str_index, &num_index);
zval *value = zend_hash_get_current_data(source);
zend_hash_next_index_insert(target, value);
}
......@@ -114,7 +114,3 @@ void bitem::save(const std::string &file_path) const {
ben_file << encode();
ben_file.close();
}
zval * bitem::search(const std::string &needle, const long &mode, const std::string path) const {
return nullptr;
}
......@@ -30,8 +30,9 @@ class bitem {
virtual std::string encode() const = 0;
static zval * load(const std::string &file_path);
void save(const std::string &file_path) const;
virtual zval * to_array(const bool include_meta) const = 0;
// mode 0: search in keys; mode 1: search in values
virtual zval * search(const std::string &needle, const long &mode, const std::string path) const;
virtual zval * search(const std::string &needle, const long &mode, const std::string path) const = 0;
};
#endif
This diff is collapsed.
......@@ -28,15 +28,15 @@ zval * bstr::parse(const std::string &ben, size_t &pt) {
return bitem::throw_general_exception("Error parsing bstr");
const size_t start = pt;
while (isdigit(ben[pt])) ++pt;
std::string len = ben.substr(start, pt - start);
size_t len = std::stoull(ben.substr(start, pt - start));
++pt;
zval *zv = new zval();
zend_object *zo = zend_container::bstr_object_new(zend_container::bstr_ce);
ZVAL_OBJ(zv, zo);
bstr_object *intern = zend_container::bstr_fetch_object(Z_OBJ_P(zv));
intern->bstr_data = new bstr(ben.substr(pt, std::stoull(len)));
pt += std::stoull(len);
intern->bnode_data = new bstr(ben.substr(pt, len));
pt += len;
return zv;
}
......@@ -46,28 +46,27 @@ std::string bstr::encode() const {
zval * bstr::to_array(const bool include_meta) const {
zval *zv = new zval();
char *_data = (char *)emalloc(_value.length() + 1);
memcpy(_data, _value.c_str(), _value.length());
if (include_meta) {
array_init(zv);
if (_value.length() == 0) return zv;
char *_type = estrdup("_type");
char *_type_data = estrdup("bstr");
char *_length = estrdup("_length");
char *_data = estrdup("_data");
char *_data_data = (char*)emalloc(_value.length() + 1);
memcpy(_data_data, _value.c_str(), _value.length());
add_assoc_string(zv, _type, _type_data);
add_assoc_long(zv, _length, length());
add_assoc_stringl(zv, _data, _data_data, _value.length());
efree(_type);
efree(_type_data);
efree(_length);
efree(_data);
efree(_data_data);
add_assoc_string(zv, (char *)"_type", (char *)"bstr");
add_assoc_long(zv, (char *)"_length", length());
add_assoc_stringl(zv, (char *)"_data", _data, _value.length());
} else {
char *_data_data = (char *)emalloc(_value.length() + 1);
memcpy(_data_data, _value.c_str(), _value.length());
ZVAL_STRINGL(zv, _data_data, _value.length());
efree(_data_data);
ZVAL_STRINGL(zv, _data, _value.length());
}
efree(_data);
return zv;
}
zval * bstr::search(const std::string &needle, const long &mode, const std::string path) const {
zval *zv = new zval();
array_init(zv);
if (mode == 1 && _value.find(needle) != std::string::npos)
add_next_index_stringl(zv, path.c_str(), path.length());
return zv;
}
......@@ -28,6 +28,7 @@ class bstr : public bitem {
static zval * parse(const std::string &ben, size_t &pt);
std::string encode() const;
zval * to_array(const bool include_meta) const;
zval * search(const std::string &needle, const long &mode, const std::string path) const;
};
#endif
*.diff
*.exp
*.log
*.out
*.php
*.sh
......@@ -13,7 +13,7 @@ extern "C" {
#define ZEND_CONTAINER_PRE(bclass) \
typedef struct { \
bclass *bclass##_data; \
bclass *bnode_data; \
zend_object std; \
} bclass##_object;
......@@ -39,9 +39,7 @@ public:
ZEND_CONTAINER_INIT(bstr)
ZEND_CONTAINER_INIT(bint)
static std::string bnode_object_get_class_name(zval *object) {
char *ini_ns_key = estrdup("bencode.namespace");
zend_bool ini_ns = zend_ini_long(ini_ns_key, strlen(ini_ns_key), 0);
efree(ini_ns_key);
zend_bool ini_ns = zend_ini_long((char *)"bencode.namespace", 17, 0);
std::string class_name(ZSTR_VAL(Z_OBJ_P(object)->ce->name));
if (ini_ns) {
return class_name.substr(8);
......@@ -54,6 +52,9 @@ public:
ZVAL_OBJ(new_object, Z_OBJ_P(object)->handlers->clone_obj(object));
return Z_OBJ_P(new_object);
}
static inline bitem * bnode_fetch_object_data(zend_object *obj) {
return (bitem *)(*((uintptr_t *)((char *)obj - sizeof(uintptr_t))));
}
};
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment