diff --git a/ext/xslt/README.XSLT-BACKENDS b/ext/xslt/README.XSLT-BACKENDS index 7332f4be672..fdf2b2bcf10 100644 --- a/ext/xslt/README.XSLT-BACKENDS +++ b/ext/xslt/README.XSLT-BACKENDS @@ -198,10 +198,10 @@ /* Fetch the arguments from the user into a zval ** */ /* Translate the zval array into a character array */ - make_xslt_array(&arguments_cp, arguments_zp); + xslt_make_array(&arguments_cp, arguments_zp); /* Translate the character array into an xslt_arg * structure */ - arguments = parse_xslt_arguments(arguments_cp); + arguments = xslt_parse_arguments(arguments_cp); /* Print out the resulting xslt_arg * structure */ php_printf("XML type: %s\n", types[arguments->xml.type]); diff --git a/ext/xslt/php_xslt.h b/ext/xslt/php_xslt.h index 35758044252..c94ea4a7442 100644 --- a/ext/xslt/php_xslt.h +++ b/ext/xslt/php_xslt.h @@ -45,12 +45,12 @@ typedef struct { struct _xslt_argument result; } xslt_args; -extern xslt_args *parse_xslt_arguments(char *, char *, char *, char **); -extern void free_xslt_arguments(xslt_args *to_free); +extern xslt_args *xslt_parse_arguments(char *, char *, char *, char **); +extern void xslt_free_arguments(xslt_args *); -extern void assign_xslt_handler(struct xslt_function **, zval **); -extern void free_xslt_handler(struct xslt_function *); -extern void call_xslt_function(char *, struct xslt_function *, int, zval **, zval **); +extern void xslt_assign_handler(struct xslt_function **, zval **); +extern void xslt_free_handler(struct xslt_function *); +extern void xslt_call_function(char *, struct xslt_function *, int, zval **, zval **); extern void xslt_debug(char *, char *, ...); diff --git a/ext/xslt/sablot.c b/ext/xslt/sablot.c index 12d5f8c6368..11bc55eec17 100644 --- a/ext/xslt/sablot.c +++ b/ext/xslt/sablot.c @@ -38,10 +38,6 @@ static void register_sax_handler_pair(struct xslt_function **, struct xslt_funct /* Free processor */ static void free_processor(zend_rsrc_list_entry *); -/* Create an XSLT array */ -static void make_xslt_array(zval **, char ***); -static void free_xslt_array(char **); - /* Scheme handler functions */ static int scheme_getall(void *, SablotHandle, const char *, const char *, char **, int *); static int scheme_freememory(void *, SablotHandle, char *); @@ -241,16 +237,16 @@ PHP_FUNCTION(xslt_set_sax_handlers) } /* Comment handlers, called when a comment is reached */ else if (strcasecmp(string_key, "comment") == 0) { - assign_xslt_handler(&XSLT_SAX(handle).comment, handler); + xslt_assign_handler(&XSLT_SAX(handle).comment, handler); } /* Processing instructions handler called when processing instructions () */ else if (strcasecmp(string_key, "pi") == 0) { - assign_xslt_handler(&XSLT_SAX(handle).pi, handler); + xslt_assign_handler(&XSLT_SAX(handle).pi, handler); } /* Character handler, called when data is found */ else if (strcasecmp(string_key, "character") == 0) { - assign_xslt_handler(&XSLT_SAX(handle).characters, handler); + xslt_assign_handler(&XSLT_SAX(handle).characters, handler); } /* Invalid handler name, tsk, tsk, tsk :) */ else { @@ -313,7 +309,7 @@ PHP_FUNCTION(xslt_set_scheme_handlers) } /* Actually assign the handlers, yippy! */ - assign_xslt_handler(&assign_handle, handler); + xslt_assign_handler(&assign_handle, handler); } } /* }}} */ @@ -332,7 +328,7 @@ PHP_FUNCTION(xslt_set_error_handler) } ZEND_FETCH_RESOURCE(handle, php_xslt *, processor_p, -1, le_xslt_name, le_xslt); - assign_xslt_handler(&XSLT_ERROR(handle), error_func); + xslt_assign_handler(&XSLT_ERROR(handle), error_func); } /* }}} */ @@ -456,7 +452,7 @@ PHP_FUNCTION(xslt_process) /* Translate a PHP array into a Sablotron array */ if (argc > 4) { char **p; - make_xslt_array(args_p, &args); + xslt_make_array(args_p, &args); for (p = args; *p != NULL; p += 2) { php_printf("%s: %s\n\n\n", *p, *(p + 1)); @@ -464,7 +460,7 @@ PHP_FUNCTION(xslt_process) } if (argc > 5) { - make_xslt_array(params_p, ¶ms); + xslt_make_array(params_p, ¶ms); } /* Perform transformation */ @@ -472,8 +468,8 @@ PHP_FUNCTION(xslt_process) if (error) { XSLT_ERRNO(handle) = error; - if (params) free_xslt_array(params); - if (args) free_xslt_array(args); + if (params) xslt_free_array(params); + if (args) xslt_free_array(args); RETURN_FALSE; } @@ -490,8 +486,8 @@ PHP_FUNCTION(xslt_process) XSLT_ERRNO(handle) = error; /* Cleanup */ - if (params) free_xslt_array(params); - if (args) free_xslt_array(args); + if (params) xslt_free_array(params); + if (args) xslt_free_array(args); RETURN_FALSE; } @@ -504,8 +500,8 @@ PHP_FUNCTION(xslt_process) } /* Cleanup */ - if (params) free_xslt_array(params); - if (args) free_xslt_array(args); + if (params) xslt_free_array(params); + if (args) xslt_free_array(args); } /* }}} */ @@ -576,23 +572,23 @@ static void free_processor(zend_rsrc_list_entry *rsrc) } /* Free Scheme handlers */ - free_xslt_handler(XSLT_SCHEME(handle).get_all); - free_xslt_handler(XSLT_SCHEME(handle).open); - free_xslt_handler(XSLT_SCHEME(handle).get); - free_xslt_handler(XSLT_SCHEME(handle).put); - free_xslt_handler(XSLT_SCHEME(handle).close); + xslt_free_handler(XSLT_SCHEME(handle).get_all); + xslt_free_handler(XSLT_SCHEME(handle).open); + xslt_free_handler(XSLT_SCHEME(handle).get); + xslt_free_handler(XSLT_SCHEME(handle).put); + xslt_free_handler(XSLT_SCHEME(handle).close); /* Free SAX handlers */ - free_xslt_handler(XSLT_SAX(handle).doc_start); - free_xslt_handler(XSLT_SAX(handle).element_start); - free_xslt_handler(XSLT_SAX(handle).element_end); - free_xslt_handler(XSLT_SAX(handle).namespace_start); - free_xslt_handler(XSLT_SAX(handle).namespace_end); - free_xslt_handler(XSLT_SAX(handle).comment); - free_xslt_handler(XSLT_SAX(handle).pi); - free_xslt_handler(XSLT_SAX(handle).characters); - free_xslt_handler(XSLT_SAX(handle).doc_end); + xslt_free_handler(XSLT_SAX(handle).doc_start); + xslt_free_handler(XSLT_SAX(handle).element_start); + xslt_free_handler(XSLT_SAX(handle).element_end); + xslt_free_handler(XSLT_SAX(handle).namespace_start); + xslt_free_handler(XSLT_SAX(handle).namespace_end); + xslt_free_handler(XSLT_SAX(handle).comment); + xslt_free_handler(XSLT_SAX(handle).pi); + xslt_free_handler(XSLT_SAX(handle).characters); + xslt_free_handler(XSLT_SAX(handle).doc_end); /* Free error handler */ - free_xslt_handler(XSLT_ERROR(handle)); + xslt_free_handler(XSLT_ERROR(handle)); /* Free error message, if any */ if (XSLT_ERRSTR(handle)) { @@ -625,7 +621,7 @@ static void register_sax_handler_pair(struct xslt_function **handler1, struct xs /* Grab handler 1 */ if (zend_hash_index_find(Z_ARRVAL_PP(handler), 0, (void **) ¤t) == SUCCESS) { - assign_xslt_handler(handler1, current); + xslt_assign_handler(handler1, current); } else { php_error(E_WARNING, "Wrong format of arguments to xslt_set_sax_handlers()"); @@ -634,7 +630,7 @@ static void register_sax_handler_pair(struct xslt_function **handler1, struct xs /* Grab handler 2 */ if (zend_hash_index_find(Z_ARRVAL_PP(handler), 1, (void **) ¤t) == SUCCESS) { - assign_xslt_handler(handler2, current); + xslt_assign_handler(handler2, current); } else { php_error(E_WARNING, "Wrong format of arguments to xslt_set_sax_handlers()"); @@ -643,62 +639,6 @@ static void register_sax_handler_pair(struct xslt_function **handler1, struct xs } /* }}} */ -/* {{{ free_xslt_array() - Free an XSLT-Sablotron array */ -static void free_xslt_array(char **arr) -{ - char **p; - - /* Free the individual elements */ - for (p = arr; *p != NULL; p++) { - efree(*p); - } - - /* Free the entire array */ - efree(arr); -} -/* }}} */ - -/* {{{ make_xslt_array() - Translate a PHP array into an xslt (char **) array */ -static void make_xslt_array(zval **zval_ar, char ***xslt_ar) -{ - zval **current; /* The current element we are processing */ - HashTable *php_ar; /* The actual array to loop through (derived from the passed zval) */ - int idx = 0; /* The current element of the xslt_ar to assign data to */ - - /* Grab the PHP array from the zval */ - php_ar = HASH_OF(*zval_ar); - if (!php_ar) { - php_error(E_WARNING, "Array expected"); - return; - } - - /* Allocate the xslt array */ - *xslt_ar = emalloc(sizeof(char *) * (zend_hash_num_elements(php_ar) + 2)); - - /* Loop through the array element by element */ - for (zend_hash_internal_pointer_reset(php_ar); - zend_hash_get_current_data(php_ar, (void **) ¤t) == SUCCESS; - zend_hash_move_forward(php_ar)) { - char *string_key = NULL; - ulong num_key; - - SEPARATE_ZVAL(current); - convert_to_string_ex(current); - - zend_hash_get_current_key(php_ar, &string_key, &num_key, 1); - - /* Akin to an associative array, first element key, second element is - the value */ - (*xslt_ar)[idx++] = estrdup(string_key); - (*xslt_ar)[idx++] = estrndup(Z_STRVAL_PP(current), Z_STRLEN_PP(current)); - } - - (*xslt_ar)[idx] = NULL; -} -/* }}} */ - /* {{{ scheme_getall() The getall scheme handler */ static int scheme_getall(void *user_data, SablotHandle proc, const char *scheme, const char *rest, char **buffer, int *byte_count) @@ -727,7 +667,7 @@ static int scheme_getall(void *user_data, SablotHandle proc, const char *scheme, ZVAL_STRING(argv[1], (char *) scheme, 1); ZVAL_STRING(argv[2], (char *) rest, 1); - call_xslt_function("scheme get all", XSLT_SCHEME(handle).get_all, + xslt_call_function("scheme get all", XSLT_SCHEME(handle).get_all, 3, argv, &retval); /* Save the return value in the buffer (copying it) */ @@ -804,7 +744,7 @@ static int scheme_open(void *user_data, SablotHandle proc, const char *scheme, ZVAL_STRING(argv[2], (char *) rest, 1); /* Call the function */ - call_xslt_function("scheme open", XSLT_SCHEME(handle).open, + xslt_call_function("scheme open", XSLT_SCHEME(handle).open, 3, argv, &retval); /* Return value is a resource pointer to an open file */ @@ -847,7 +787,7 @@ static int scheme_get(void *user_data, SablotHandle proc, int fd, char *buffer, ZVAL_STRINGL(argv[2], buffer, *byte_count, 0); /* Call the function */ - call_xslt_function("scheme get", XSLT_SCHEME(handle).get, + xslt_call_function("scheme get", XSLT_SCHEME(handle).get, 3, argv, &retval); /* Returns the number of bytes read */ @@ -890,7 +830,7 @@ static int scheme_put(void *user_data, SablotHandle proc, int fd, const char *b ZVAL_STRINGL(argv[2], (char *) buffer, *byte_count, 1); /* Call the scheme put function already */ - call_xslt_function("scheme put", XSLT_SCHEME(handle).put, + xslt_call_function("scheme put", XSLT_SCHEME(handle).put, 3, argv, &retval); /* The return value is the number of bytes written */ @@ -930,7 +870,7 @@ static int scheme_close(void *user_data, SablotHandle proc, int fd) zend_list_addref(fd); /* Call the scheme handler close function */ - call_xslt_function("scheme close", XSLT_SCHEME(handle).close, + xslt_call_function("scheme close", XSLT_SCHEME(handle).close, 2, argv, &retval); /* Free everything up */ @@ -962,7 +902,7 @@ static SAX_RETURN sax_startdoc(void *ctx) zend_list_addref(handle->processor.idx); /* Call the Sax start doc function */ - call_xslt_function("sax start doc", XSLT_SAX(handle).doc_start, + xslt_call_function("sax start doc", XSLT_SAX(handle).doc_start, 1, argv, &retval); /* Cleanup */ @@ -1009,7 +949,7 @@ static SAX_RETURN sax_startelement(void *ctx, } /* Call the sax element start function */ - call_xslt_function("sax start element", XSLT_SAX(handle).element_start, + xslt_call_function("sax start element", XSLT_SAX(handle).element_start, 3, argv, &retval); /* Cleanup */ @@ -1042,7 +982,7 @@ static SAX_RETURN sax_endelement(void *ctx, const char *name) ZVAL_STRING(argv[1], (char *) name, 1); /* Call the sax end element function */ - call_xslt_function("sax end element", XSLT_SAX(handle).element_end, + xslt_call_function("sax end element", XSLT_SAX(handle).element_end, 2, argv, &retval); /* Cleanup */ @@ -1079,7 +1019,7 @@ static SAX_RETURN sax_startnamespace(void *ctx, ZVAL_STRING(argv[2], (char *) uri, 1); /* Call the sax start namespace function */ - call_xslt_function("sax start namespace", XSLT_SAX(handle).namespace_start, + xslt_call_function("sax start namespace", XSLT_SAX(handle).namespace_start, 3, argv, &retval); /* Cleanup */ @@ -1112,7 +1052,7 @@ static SAX_RETURN sax_endnamespace(void *ctx, const char *prefix) ZVAL_STRING(argv[1], (char *) prefix, 1); /* Call the sax end namespace function */ - call_xslt_function("sax end namespace", XSLT_SAX(handle).namespace_end, + xslt_call_function("sax end namespace", XSLT_SAX(handle).namespace_end, 2, argv, &retval); /* Cleanup */ @@ -1145,7 +1085,7 @@ static SAX_RETURN sax_comment(void *ctx, const char *contents) ZVAL_STRING(argv[1], (char *) contents, 1); /* Call the sax comment function */ - call_xslt_function("sax comment", XSLT_SAX(handle).comment, + xslt_call_function("sax comment", XSLT_SAX(handle).comment, 2, argv, &retval); /* Cleanup */ @@ -1183,7 +1123,7 @@ static SAX_RETURN sax_pi(void *ctx, ZVAL_STRING(argv[2], (char *) contents, 1); /* Call processing instructions function */ - call_xslt_function("sax processing instructions", XSLT_SAX(handle).pi, + xslt_call_function("sax processing instructions", XSLT_SAX(handle).pi, 3, argv, &retval); /* Cleanup */ @@ -1218,7 +1158,7 @@ static SAX_RETURN sax_characters(void *ctx, ZVAL_STRINGL(argv[1], (char *) contents, length, 1); /* Call characters function */ - call_xslt_function("sax characters", XSLT_SAX(handle).characters, + xslt_call_function("sax characters", XSLT_SAX(handle).characters, 2, argv, &retval); /* Cleanup */ @@ -1248,7 +1188,7 @@ static SAX_RETURN sax_enddoc(void *ctx) zend_list_addref(handle->processor.idx); /* Call the function */ - call_xslt_function("sax end doc", XSLT_SAX(handle).doc_end, + xslt_call_function("sax end doc", XSLT_SAX(handle).doc_end, 1, argv, &retval); /* Cleanup */ @@ -1367,7 +1307,7 @@ static MH_ERROR error_log(void *user_data, SablotHandle proc, MH_ERROR code, MH_ /* open for append */ XSLT_LOG(handle).fd = open(XSLT_LOG(handle).path, O_WRONLY|O_CREAT|O_APPEND, - S_IRUSR|S_IRGRP|S_IROTH|S_IWUSR); + S_IRUSR|S_IRGRP|S_IROTH|S_IWUSR); if (XSLT_LOG(handle).fd < 0) { php_error(E_WARNING, "Cannot open log file, %s [%d]: %s", XSLT_LOG(handle).path, errno, strerror(errno)); @@ -1468,7 +1408,7 @@ static MH_ERROR error_print(void *user_data, SablotHandle proc, MH_ERROR code, M } /* Call the function */ - call_xslt_function("error handler", XSLT_ERROR(handle), + xslt_call_function("error handler", XSLT_ERROR(handle), 4, argv, &retval); /* Free up */ diff --git a/ext/xslt/xslt.c b/ext/xslt/xslt.c index dd406a1f5c8..2f59e72eb56 100644 --- a/ext/xslt/xslt.c +++ b/ext/xslt/xslt.c @@ -45,34 +45,94 @@ extern void xslt_debug(char *function_name, char *format, ...) } /* }}} */ -static char *find_xslt_argument(const char **argv, const char *key) +/* {{{ find_xslt_argument() + Find and return an xslt argument from the argument buffer */ +static char *_find_xslt_argument(const char **argv, const char *key) { - char **ptr; - char *return_value; + char **ptr; /* Pointer to the passed char ** array */ + char *return_value = NULL; /* Value to return from the function */ + /* Loop through the array searching for the value */ ptr = (char **) argv; while (ptr && *ptr) { + /* If we have a match, save the value and exit out */ if (! strcmp(*ptr, key)) { return_value = estrdup(*ptr); - return return_value; + break; } ptr++; } - if (! return_value) { - return NULL; - } + return return_value; } +/* }}} */ -/* {{{ parse_xslt_arguments() +/* {{{ xslt_make_array() + Make an XSLT array (char **) from a zval array (HashTable *) */ +extern void xslt_make_array(zval **zarr, char ***carr) +{ + zval **current; + HashTable *arr; + int idx = 0; + + arr = HASH_OF(*zarr); + if (! arr) { + php_error(E_WARNING, "Invalid argument or parameter array to %s", + get_active_function_name()); + return; + } + + *carr = emalloc((zend_hash_num_elements(arr) * 2) + 1); + + for (zend_hash_internal_pointer_reset(arr); + zend_hash_get_current_data(arr, (void **) ¤t) == SUCCESS; + zend_hash_move_forward(arr)) { + char *string_key = NULL; + ulong num_key; + int type; + + SEPARATE_ZVAL(current); + convert_to_string_ex(current); + + type = zend_hash_get_current_key(arr, &string_key, &num_key, 0); + if (type == HASH_KEY_IS_LONG) { + php_error(E_WARNING, "Invalid argument or parameter array to %s", + get_active_function_name()); + return; + } + + (*carr)[idx++] = estrdup(string_key); + (*carr)[idx++] = estrndup(Z_STRVAL_PP(current), Z_STRLEN_PP(current)); + } + + (*carr)[idx] = NULL; +} +/* }}} */ + +/* {{{ xslt_free_array() + Free an xslt array built by xslt_make_array() */ +extern void xslt_free_array(char **arr) +{ + char **ptr = arr; + + while (*ptr != NULL) { + efree(*ptr); + ptr++; + } + + efree(arr); +} +/* }}} */ + +/* {{{ xslt_parse_arguments() Parse an XSLT argument buffer */ -extern xslt_args *parse_xslt_arguments(char *xml, +extern xslt_args *xslt_parse_arguments(char *xml, char *xsl, char *result, char **argv) { - xslt_args *return_value; + xslt_args *return_value; /* The value to return from the function */ return_value = emalloc(sizeof(xslt_args)); @@ -81,8 +141,8 @@ extern xslt_args *parse_xslt_arguments(char *xml, char *key = xml + 5; return_value->xml.type = XSLT_IS_DATA; - return_value->xml.ptr = find_xslt_argument((const char **) argv, - (const char *) key); + return_value->xml.ptr = _find_xslt_argument((const char **) argv, + (const char *) key); } else { return_value->xml.type = XSLT_IS_FILE; @@ -94,8 +154,8 @@ extern xslt_args *parse_xslt_arguments(char *xml, char *key = xsl + 5; return_value->xsl.type = XSLT_IS_DATA; - return_value->xsl.ptr = find_xslt_argument((const char **) argv, - (const char *) key); + return_value->xsl.ptr = _find_xslt_argument((const char **) argv, + (const char *) key); } else { return_value->xsl.type = XSLT_IS_FILE; @@ -107,8 +167,8 @@ extern xslt_args *parse_xslt_arguments(char *xml, char *key = result + 5; return_value->result.type = XSLT_IS_DATA; - return_value->result.ptr = find_xslt_argument((const char **) argv, - (const char *) key); + return_value->result.ptr = _find_xslt_argument((const char **) argv, + (const char *) key); } else { return_value->result.type = XSLT_IS_FILE; @@ -121,7 +181,7 @@ extern xslt_args *parse_xslt_arguments(char *xml, /* {{{ free_xslt_arguments() Free's an XSLT argument list returned from parse_xslt_arguments() */ -extern void free_xslt_arguments(xslt_args *to_free) +extern void xslt_free_arguments(xslt_args *to_free) { if (to_free->xml.ptr) { efree(to_free->xml.ptr); @@ -141,7 +201,7 @@ extern void free_xslt_arguments(xslt_args *to_free) /* {{{ call_xslt_function() Call an XSLT handler */ -extern void call_xslt_function(char *name, +extern void xslt_call_function(char *name, struct xslt_function *fptr, int argc, zval **argv, @@ -173,7 +233,7 @@ extern void call_xslt_function(char *name, /* }}} */ -extern void free_xslt_handler(struct xslt_function *func) +extern void xslt_free_handler(struct xslt_function *func) { if (!func) { return; @@ -190,7 +250,7 @@ extern void free_xslt_handler(struct xslt_function *func) efree(func); } -extern void assign_xslt_handler(struct xslt_function **func, zval **zfunc) +extern void xslt_assign_handler(struct xslt_function **func, zval **zfunc) { char error[] = "Invalid function passed to %s";