Skip to content
Merged
103 changes: 31 additions & 72 deletions ext/tidy/tidy.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,6 @@
/* {{{ ext/tidy macros */
#define FIX_BUFFER(bptr) do { if ((bptr)->size) { (bptr)->bp[(bptr)->size-1] = '\0'; } } while(0)

#define TIDY_SET_CONTEXT \
zval *object = getThis();

#define TIDY_FETCH_OBJECT \
PHPTidyObj *obj; \
zval *object; \
Expand All @@ -65,16 +62,15 @@
TIDY_FETCH_OBJECT; \
if (!obj->ptdoc->initialized) { \
zend_throw_error(NULL, "tidy object is not initialized"); \
return; \
RETURN_THROWS(); \
}

#define TIDY_FETCH_ONLY_OBJECT \
PHPTidyObj *obj; \
TIDY_SET_CONTEXT; \
if (zend_parse_parameters_none() != SUCCESS) { \
RETURN_THROWS(); \
} \
obj = Z_TIDY_P(object); \
obj = Z_TIDY_P(ZEND_THIS); \

#define TIDY_SET_DEFAULT_CONFIG(_doc) \
if (TG(default_config) && TG(default_config)[0]) { \
Expand All @@ -99,10 +95,10 @@ typedef enum {
} tidy_base_nodetypes;

struct _PHPTidyDoc {
TidyDoc doc;
TidyBuffer *errbuf;
unsigned int ref_count;
unsigned int initialized:1;
TidyDoc doc;
TidyBuffer *errbuf;
uint32_t ref_count;
bool initialized;
};

struct _PHPTidyObj {
Expand All @@ -128,7 +124,7 @@ static zend_result tidy_doc_cast_handler(zend_object *, zval *, int);
static zend_result tidy_node_cast_handler(zend_object *, zval *, int);
static void tidy_doc_update_properties(PHPTidyObj *);
static void tidy_add_node_default_properties(PHPTidyObj *);
static void *php_tidy_get_opt_val(PHPTidyDoc *, TidyOption, TidyOptionType *);
static void *php_tidy_get_opt_val(const PHPTidyDoc *, TidyOption, TidyOptionType *);
static void php_tidy_create_node(INTERNAL_FUNCTION_PARAMETERS, tidy_base_nodetypes);
static zend_result _php_tidy_set_tidy_opt(TidyDoc, const char *, zval *, uint32_t arg);
static zend_result _php_tidy_apply_config_array(TidyDoc doc, const HashTable *ht_options, uint32_t arg);
Expand Down Expand Up @@ -412,7 +408,7 @@ static void tidy_object_free_storage(zend_object *object)
if (intern->ptdoc) {
intern->ptdoc->ref_count--;

if (intern->ptdoc->ref_count <= 0) {
if (intern->ptdoc->ref_count == 0) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice catch, seems a long forgotten change to me

tidyBufFree(intern->ptdoc->errbuf);
efree(intern->ptdoc->errbuf);
tidyRelease(intern->ptdoc->doc);
Expand All @@ -421,7 +417,7 @@ static void tidy_object_free_storage(zend_object *object)
}
}

static zend_object *tidy_object_new(zend_class_entry *class_type, zend_object_handlers *handlers, tidy_obj_type objtype)
static zend_object *tidy_object_new(zend_class_entry *class_type, const zend_object_handlers *handlers, tidy_obj_type objtype)
{
PHPTidyObj *intern;

Expand All @@ -437,7 +433,7 @@ static zend_object *tidy_object_new(zend_class_entry *class_type, zend_object_ha
intern->ptdoc = emalloc(sizeof(PHPTidyDoc));
intern->ptdoc->doc = tidyCreate();
intern->ptdoc->ref_count = 1;
intern->ptdoc->initialized = 0;
intern->ptdoc->initialized = false;
intern->ptdoc->errbuf = emalloc(sizeof(TidyBuffer));
tidyBufInit(intern->ptdoc->errbuf);

Expand Down Expand Up @@ -670,17 +666,16 @@ static void tidy_add_node_default_properties(PHPTidyObj *obj)
tempattr = tidyAttrFirst(obj->node);

if (tempattr) {
const char *name, *val;
array_init(&attribute);

do {
name = (const char *)tidyAttrName(tempattr);
val = (const char *)tidyAttrValue(tempattr);
if (name) {
const char *attr_name = tidyAttrName(tempattr);
if (attr_name) {
const char *val = tidyAttrValue(tempattr);
if (val) {
add_assoc_string(&attribute, name, val);
add_assoc_string(&attribute, attr_name, val);
} else {
add_assoc_str(&attribute, name, zend_empty_string);
add_assoc_str(&attribute, attr_name, zend_empty_string);
}
}
} while((tempattr = tidyAttrNext(tempattr)));
Expand Down Expand Up @@ -723,13 +718,13 @@ static void tidy_add_node_default_properties(PHPTidyObj *obj)
zval_ptr_dtor(&children);
}

static void *php_tidy_get_opt_val(PHPTidyDoc *ptdoc, TidyOption opt, TidyOptionType *type)
static void *php_tidy_get_opt_val(const PHPTidyDoc *ptdoc, TidyOption opt, TidyOptionType *type)
{
*type = tidyOptGetType(opt);

switch (*type) {
case TidyString: {
char *val = (char *) tidyOptGetValue(ptdoc->doc, tidyOptGetId(opt));
const char *val = tidyOptGetValue(ptdoc->doc, tidyOptGetId(opt));
if (val) {
return (void *) zend_string_init(val, strlen(val), 0);
} else {
Expand Down Expand Up @@ -803,7 +798,7 @@ static zend_result _php_tidy_apply_config_array(TidyDoc doc, const HashTable *ht
}
}

static int php_tidy_parse_string(PHPTidyObj *obj, const char *string, uint32_t len, const char *enc)
static zend_result php_tidy_parse_string(PHPTidyObj *obj, const char *string, uint32_t len, const char *enc)
{
TidyBuffer buf;

Expand All @@ -814,7 +809,7 @@ static int php_tidy_parse_string(PHPTidyObj *obj, const char *string, uint32_t l
}
}

obj->ptdoc->initialized = 1;
obj->ptdoc->initialized = true;

tidyBufInit(&buf);
tidyBufAttach(&buf, (byte *) string, len);
Expand Down Expand Up @@ -1327,19 +1322,14 @@ PHP_FUNCTION(tidy_getopt)
optval = php_tidy_get_opt_val(obj->ptdoc, opt, &optt);
switch (optt) {
case TidyString:
RETVAL_STR((zend_string*)optval);
return;
RETURN_STR((zend_string*)optval);

case TidyInteger:
RETURN_LONG((zend_long)optval);
break;

case TidyBoolean:
if (optval) {
RETURN_TRUE;
} else {
RETURN_FALSE;
}
RETURN_BOOL(optval);
break;

default:
Expand Down Expand Up @@ -1368,8 +1358,7 @@ PHP_METHOD(tidy, __construct)
Z_PARAM_BOOL(use_include_path)
ZEND_PARSE_PARAMETERS_END();

TIDY_SET_CONTEXT;
obj = Z_TIDY_P(object);
obj = Z_TIDY_P(ZEND_THIS);

if (inputfile) {
if (!(contents = php_tidy_file_to_mem(ZSTR_VAL(inputfile), use_include_path))) {
Expand Down Expand Up @@ -1415,8 +1404,7 @@ PHP_METHOD(tidy, parseFile)
Z_PARAM_BOOL(use_include_path)
ZEND_PARSE_PARAMETERS_END();

TIDY_SET_CONTEXT;
obj = Z_TIDY_P(object);
obj = Z_TIDY_P(ZEND_THIS);

if (!(contents = php_tidy_file_to_mem(ZSTR_VAL(inputfile), use_include_path))) {
php_error_docref(NULL, E_WARNING, "Cannot load \"%s\" into memory%s", ZSTR_VAL(inputfile), (use_include_path) ? " (using include path)" : "");
Expand Down Expand Up @@ -1455,8 +1443,7 @@ PHP_METHOD(tidy, parseString)
RETURN_THROWS();
}

TIDY_SET_CONTEXT;
obj = Z_TIDY_P(object);
obj = Z_TIDY_P(ZEND_THIS);

RETURN_BOOL(php_tidy_apply_config(obj->ptdoc->doc, options_str, options_ht, 2) == SUCCESS
&& php_tidy_parse_string(obj, ZSTR_VAL(input), (uint32_t)ZSTR_LEN(input), enc) == SUCCESS);
Expand Down Expand Up @@ -1496,11 +1483,7 @@ PHP_METHOD(tidyNode, hasChildren)
{
TIDY_FETCH_ONLY_OBJECT;

if (tidyGetChild(obj->node)) {
RETURN_TRUE;
} else {
RETURN_FALSE;
}
RETURN_BOOL(tidyGetChild(obj->node));
}
/* }}} */

Expand All @@ -1509,11 +1492,7 @@ PHP_METHOD(tidyNode, hasSiblings)
{
TIDY_FETCH_ONLY_OBJECT;

if (obj->node && tidyGetNext(obj->node)) {
RETURN_TRUE;
} else {
RETURN_FALSE;
}
RETURN_BOOL(obj->node && tidyGetNext(obj->node));
}
/* }}} */

Expand All @@ -1522,11 +1501,7 @@ PHP_METHOD(tidyNode, isComment)
{
TIDY_FETCH_ONLY_OBJECT;

if (tidyNodeGetType(obj->node) == TidyNode_Comment) {
RETURN_TRUE;
} else {
RETURN_FALSE;
}
RETURN_BOOL(tidyNodeGetType(obj->node) == TidyNode_Comment);
}
/* }}} */

Expand All @@ -1551,11 +1526,7 @@ PHP_METHOD(tidyNode, isText)
{
TIDY_FETCH_ONLY_OBJECT;

if (tidyNodeGetType(obj->node) == TidyNode_Text) {
RETURN_TRUE;
} else {
RETURN_FALSE;
}
RETURN_BOOL(tidyNodeGetType(obj->node) == TidyNode_Text);
}
/* }}} */

Expand All @@ -1564,11 +1535,7 @@ PHP_METHOD(tidyNode, isJste)
{
TIDY_FETCH_ONLY_OBJECT;

if (tidyNodeGetType(obj->node) == TidyNode_Jste) {
RETURN_TRUE;
} else {
RETURN_FALSE;
}
RETURN_BOOL(tidyNodeGetType(obj->node) == TidyNode_Jste);
}
/* }}} */

Expand All @@ -1577,11 +1544,7 @@ PHP_METHOD(tidyNode, isAsp)
{
TIDY_FETCH_ONLY_OBJECT;

if (tidyNodeGetType(obj->node) == TidyNode_Asp) {
RETURN_TRUE;
} else {
RETURN_FALSE;
}
RETURN_BOOL(tidyNodeGetType(obj->node) == TidyNode_Asp);
}
/* }}} */

Expand All @@ -1590,11 +1553,7 @@ PHP_METHOD(tidyNode, isPhp)
{
TIDY_FETCH_ONLY_OBJECT;

if (tidyNodeGetType(obj->node) == TidyNode_Php) {
RETURN_TRUE;
} else {
RETURN_FALSE;
}
RETURN_BOOL(tidyNodeGetType(obj->node) == TidyNode_Php);
}
/* }}} */

Expand Down