com php-src: Merge branch 'master' into jsond: ext/json/config.m4 ext/json/config.w32 ext/json/json.c ext/json/json_encoder.c ext/json/json_parser.y
ext/json/json_scanner.re ext/json/php_json.h ext/json/php_json_encoder.h e xt/json/php_json_parser.h ext/json/php_json_scanner .h
| From: | Jakub Zelenka | Date: | Sat, 27 Dec 2014 19:42:04 +0000 |
| Subject: | com php-src: Merge branch 'master' into jsond: ext/json/config.m4 ext/json/config.w32 ext/json/json.c ext/json/json_encoder.c ext/json/json_parser.y ext/json/json_scanner.re ext/json/php_json.h ext/json/php_json_encoder.h e xt/json/php_json_parser.h ext/json/php_json_scanner .h |
||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-84708@lists.php.net to get a copy of this message | ||
Commit: b68da91d52801fb2cc4d2a4a44c859ea5b6c622b
Author: Jakub Zelenka <bukka@php.net> Sat, 27 Dec 2014 19:42:04 +0000
Parents: 4f6539bdaf62f0343f4ec67638d2f96e8c1c00ae a9e86957c70c181e830ba05bb030c468d0cb15e1
Branches: master
Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=b68da91d52801fb2cc4d2a4a44c859ea5b6c622b
Log:
Merge branch 'master' into jsond
Conflicts:
ext/json/JSON_parser.c
ext/json/JSON_parser.h
ext/json/config.m4
ext/json/config.w32
ext/json/json.c
ext/json/php_json.h
Changed paths:
MM ext/json/config.m4
MM ext/json/config.w32
MM ext/json/json.c
MA ext/json/json_encoder.c
MA ext/json/json_parser.y
MA ext/json/json_scanner.re
MM ext/json/php_json.h
MA ext/json/php_json_encoder.h
MA ext/json/php_json_parser.h
MA ext/json/php_json_scanner.h
diff --cc ext/json/config.m4 index 805330c,9c83a5d..fb87a93 --- a/ext/json/config.m4 +++ b/ext/json/config.m4 @@@ -9,13 -9,7 +9,13 @@@ if test "$PHP_JSON" != "no"; the AC_DEFINE([HAVE_JSON],1 ,[whether to enable JavaScript Object Serialization support]) AC_HEADER_STDC - PHP_NEW_EXTENSION(json, json.c utf8_decode.c JSON_parser.c, $ext_shared,, -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1) +PHP_NEW_EXTENSION(json, + json.c \ + json_encoder.c \ + json_parser.tab.c \ + json_scanner.c, - $ext_shared) ++ $ext_shared,, -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1) PHP_INSTALL_HEADERS([ext/json], [php_json.h]) + PHP_ADD_MAKEFILE_FRAGMENT() PHP_SUBST(JSON_SHARED_LIBADD) fi diff --cc ext/json/config.w32 index a6c54e6,60ccf91..996b868 --- a/ext/json/config.w32 +++ b/ext/json/config.w32 @@@ -4,13 -4,8 +4,8 @@@ ARG_ENABLE("json", "JavaScript Object Serialization support", "yes"); if (PHP_JSON != "no") { - EXTENSION('json', 'json.c', PHP_JSON_SHARED, ""); - PHP_NEW_EXTENSION(json, - json.c \ - json_encoder.c \ - json_parser.tab.c \ - json_scanner.c, - $ext_shared) + EXTENSION('json', 'json.c', PHP_JSON_SHARED, "/DZEND_ENABLE_STATIC_TSRMLS_CACHE=1"); - ADD_SOURCES(configure_module_dirname, "JSON_parser.c utf8_decode.c", "json"); ++ ADD_SOURCES(configure_module_dirname, "json_encoder.c json_parser.tab.c json_scanner.c", "json"); PHP_INSTALL_HEADERS("ext/json/", "php_json.h"); } diff --cc ext/json/json.c index f59dace,481ab75..819a6df --- a/ext/json/json.c +++ b/ext/json/json.c @@@ -166,23 -173,601 +172,22 @@@ static PHP_MINFO_FUNCTION(json } /* }}} */ -static void json_escape_string(smart_str *buf, char *s, size_t len, int options); -- - PHP_JSON_API void php_json_encode(smart_str *buf, zval *val, int options TSRMLS_DC) /* {{{ */ -static int json_determine_array_type(zval *val) /* {{{ */ -{ - int i; - HashTable *myht = HASH_OF(val); - - i = myht ? zend_hash_num_elements(myht) : 0; - if (i > 0) { - zend_string *key; - zend_ulong index, idx; - - idx = 0; - ZEND_HASH_FOREACH_KEY(myht, index, key) { - if (key) { - return PHP_JSON_OUTPUT_OBJECT; - } else { - if (index != idx) { - return PHP_JSON_OUTPUT_OBJECT; - } - } - idx++; - } ZEND_HASH_FOREACH_END(); - } - - return PHP_JSON_OUTPUT_ARRAY; -} -/* }}} */ - -/* {{{ Pretty printing support functions */ - -static inline void json_pretty_print_char(smart_str *buf, int options, char c) /* {{{ */ -{ - if (options & PHP_JSON_PRETTY_PRINT) { - smart_str_appendc(buf, c); - } -} -/* }}} */ - -static inline void json_pretty_print_indent(smart_str *buf, int options) /* {{{ */ -{ - int i; - - if (options & PHP_JSON_PRETTY_PRINT) { - for (i = 0; i < JSON_G(encoder_depth); ++i) { - smart_str_appendl(buf, " ", 4); - } - } -} -/* }}} */ - -/* }}} */ - -static void json_encode_array(smart_str *buf, zval *val, int options) /* {{{ */ -{ - int i, r, need_comma = 0; - HashTable *myht; - - if (Z_TYPE_P(val) == IS_ARRAY) { - myht = HASH_OF(val); - r = (options & PHP_JSON_FORCE_OBJECT) ? PHP_JSON_OUTPUT_OBJECT : json_determine_array_type(val); - } else { - myht = Z_OBJPROP_P(val); - r = PHP_JSON_OUTPUT_OBJECT; - } - - if (myht && ZEND_HASH_GET_APPLY_COUNT(myht) > 1) { - JSON_G(error_code) = PHP_JSON_ERROR_RECURSION; - smart_str_appendl(buf, "null", 4); - return; - } - - if (r == PHP_JSON_OUTPUT_ARRAY) { - smart_str_appendc(buf, '['); - } else { - smart_str_appendc(buf, '{'); - } - - ++JSON_G(encoder_depth); - - i = myht ? zend_hash_num_elements(myht) : 0; - - if (i > 0) { - zend_string *key; - zval *data; - zend_ulong index; - HashTable *tmp_ht; - - ZEND_HASH_FOREACH_KEY_VAL_IND(myht, index, key, data) { - ZVAL_DEREF(data); - tmp_ht = HASH_OF(data); - if (tmp_ht && ZEND_HASH_APPLY_PROTECTION(tmp_ht)) { - ZEND_HASH_INC_APPLY_COUNT(tmp_ht); - } - - if (r == PHP_JSON_OUTPUT_ARRAY) { - if (need_comma) { - smart_str_appendc(buf, ','); - } else { - need_comma = 1; - } - - json_pretty_print_char(buf, options, '\n'); - json_pretty_print_indent(buf, options); - php_json_encode(buf, data, options); - } else if (r == PHP_JSON_OUTPUT_OBJECT) { - if (key) { - if (key->val[0] == '\0' && Z_TYPE_P(val) == IS_OBJECT) { - /* Skip protected and private members. */ - if (tmp_ht && ZEND_HASH_APPLY_PROTECTION(tmp_ht)) { - ZEND_HASH_DEC_APPLY_COUNT(tmp_ht); - } - continue; - } - - if (need_comma) { - smart_str_appendc(buf, ','); - } else { - need_comma = 1; - } - - json_pretty_print_char(buf, options, '\n'); - json_pretty_print_indent(buf, options); - - json_escape_string(buf, key->val, key->len, options & ~PHP_JSON_NUMERIC_CHECK); - smart_str_appendc(buf, ':'); - - json_pretty_print_char(buf, options, ' '); - - php_json_encode(buf, data, options); - } else { - if (need_comma) { - smart_str_appendc(buf, ','); - } else { - need_comma = 1; - } - - json_pretty_print_char(buf, options, '\n'); - json_pretty_print_indent(buf, options); - - smart_str_appendc(buf, '"'); - smart_str_append_long(buf, (zend_long) index); - smart_str_appendc(buf, '"'); - smart_str_appendc(buf, ':'); - - json_pretty_print_char(buf, options, ' '); - - php_json_encode(buf, data, options); - } - } - - if (tmp_ht && ZEND_HASH_APPLY_PROTECTION(tmp_ht)) { - ZEND_HASH_DEC_APPLY_COUNT(tmp_ht); - } - } ZEND_HASH_FOREACH_END(); - } - - if (JSON_G(encoder_depth) > JSON_G(encode_max_depth)) { - JSON_G(error_code) = PHP_JSON_ERROR_DEPTH; - } - --JSON_G(encoder_depth); - - /* Only keep closing bracket on same line for empty arrays/objects */ - if (need_comma) { - json_pretty_print_char(buf, options, '\n'); - json_pretty_print_indent(buf, options); - } - - if (r == PHP_JSON_OUTPUT_ARRAY) { - smart_str_appendc(buf, ']'); - } else { - smart_str_appendc(buf, '}'); - } -} -/* }}} */ - -static int json_utf8_to_utf16(unsigned short *utf16, char utf8[], int len) /* {{{ */ -{ - size_t pos = 0, us; - int j, status; - - if (utf16) { - /* really convert the utf8 string */ - for (j=0 ; pos < len ; j++) { - us = php_next_utf8_char((const unsigned char *)utf8, len, &pos, &status); - if (status != SUCCESS) { - return -1; - } - /* From http://en.wikipedia.org/wiki/UTF16 */ - if (us >= 0x10000) { - us -= 0x10000; - utf16[j++] = (unsigned short)((us >> 10) | 0xd800); - utf16[j] = (unsigned short)((us & 0x3ff) | 0xdc00); - } else { - utf16[j] = (unsigned short)us; - } - } - } else { - /* Only check if utf8 string is valid, and compute utf16 length */ - for (j=0 ; pos < len ; j++) { - us = php_next_utf8_char((const unsigned char *)utf8, len, &pos, &status); - if (status != SUCCESS) { - return -1; - } - if (us >= 0x10000) { - j++; - } - } - } - return j; -} -/* }}} */ - -static void json_escape_string(smart_str *buf, char *s, size_t len, int options) /* {{{ */ -{ - int status; - unsigned int us, next_us = 0; - size_t pos, checkpoint; - - if (len == 0) { - smart_str_appendl(buf, "\"\"", 2); - return; - } - - if (options & PHP_JSON_NUMERIC_CHECK) { - double d; - int type; - zend_long p; - - if ((type = is_numeric_string(s, len, &p, &d, 0)) != 0) { - if (type == IS_LONG) { - smart_str_append_long(buf, p); - } else if (type == IS_DOUBLE) { - if (!zend_isinf(d) && !zend_isnan(d)) { - char *tmp; - int l = spprintf(&tmp, 0, "%.*k", (int) EG(precision), d); - smart_str_appendl(buf, tmp, l); - efree(tmp); - } else { - JSON_G(error_code) = PHP_JSON_ERROR_INF_OR_NAN; - smart_str_appendc(buf, '0'); - } - } - return; - } - - } - - if (options & PHP_JSON_UNESCAPED_UNICODE) { - /* validate UTF-8 string first */ - if (json_utf8_to_utf16(NULL, s, len) < 0) { - JSON_G(error_code) = PHP_JSON_ERROR_UTF8; - smart_str_appendl(buf, "null", 4); - return; - } - } - - pos = 0; - checkpoint = buf->s ? buf->s->len : 0; - - /* pre-allocate for string length plus 2 quotes */ - smart_str_alloc(buf, len+2, 0); - smart_str_appendc(buf, '"'); - - do { - if (UNEXPECTED(next_us)) { - us = next_us; - next_us = 0; - } else { - us = (unsigned char)s[pos]; - if (!(options & PHP_JSON_UNESCAPED_UNICODE) && us >= 0x80) { - /* UTF-8 character */ - us = php_next_utf8_char((const unsigned char *)s, len, &pos, &status); - if (status != SUCCESS) { - if (buf->s) { - buf->s->len = checkpoint; - } - JSON_G(error_code) = PHP_JSON_ERROR_UTF8; - smart_str_appendl(buf, "null", 4); - return; - } - /* From http://en.wikipedia.org/wiki/UTF16 */ - if (us >= 0x10000) { - us -= 0x10000; - next_us = (unsigned short)((us & 0x3ff) | 0xdc00); - us = (unsigned short)((us >> 10) | 0xd800); - } - } else { - pos++; - } - } - - switch (us) { - case '"': - if (options & PHP_JSON_HEX_QUOT) { - smart_str_appendl(buf, "\\u0022", 6); - } else { - smart_str_appendl(buf, "\\\"", 2); - } - break; - - case '\\': - smart_str_appendl(buf, "\\\\", 2); - break; - - case '/': - if (options & PHP_JSON_UNESCAPED_SLASHES) { - smart_str_appendc(buf, '/'); - } else { - smart_str_appendl(buf, "\\/", 2); - } - break; - - case '\b': - smart_str_appendl(buf, "\\b", 2); - break; - - case '\f': - smart_str_appendl(buf, "\\f", 2); - break; - - case '\n': - smart_str_appendl(buf, "\\n", 2); - break; - - case '\r': - smart_str_appendl(buf, "\\r", 2); - break; - - case '\t': - smart_str_appendl(buf, "\\t", 2); - break; - - case '<': - if (options & PHP_JSON_HEX_TAG) { - smart_str_appendl(buf, "\\u003C", 6); - } else { - smart_str_appendc(buf, '<'); - } - break; - - case '>': - if (options & PHP_JSON_HEX_TAG) { - smart_str_appendl(buf, "\\u003E", 6); - } else { - smart_str_appendc(buf, '>'); - } - break; - - case '&': - if (options & PHP_JSON_HEX_AMP) { - smart_str_appendl(buf, "\\u0026", 6); - } else { - smart_str_appendc(buf, '&'); - } - break; - - case '\'': - if (options & PHP_JSON_HEX_APOS) { - smart_str_appendl(buf, "\\u0027", 6); - } else { - smart_str_appendc(buf, '\''); - } - break; - - default: - if (us >= ' ' && ((options & PHP_JSON_UNESCAPED_UNICODE) || (us & 127) == us)) { - smart_str_appendc(buf, (unsigned char) us); - } else { - smart_str_appendl(buf, "\\u", 2); - smart_str_appendc(buf, digits[(us & 0xf000) >> 12]); - smart_str_appendc(buf, digits[(us & 0xf00) >> 8]); - smart_str_appendc(buf, digits[(us & 0xf0) >> 4]); - smart_str_appendc(buf, digits[(us & 0xf)]); - } - break; - } - } while (pos < len || next_us); - - smart_str_appendc(buf, '"'); -} -/* }}} */ - -static void json_encode_serializable_object(smart_str *buf, zval *val, int options) /* {{{ */ -{ - zend_class_entry *ce = Z_OBJCE_P(val); - zval retval, fname; - HashTable* myht; - - if (Z_TYPE_P(val) == IS_ARRAY) { - myht = HASH_OF(val); - } else { - myht = Z_OBJPROP_P(val); - } - - if (myht && ZEND_HASH_GET_APPLY_COUNT(myht) > 1) { - JSON_G(error_code) = PHP_JSON_ERROR_RECURSION; - smart_str_appendl(buf, "null", 4); - return; - } - - ZVAL_STRING(&fname, "jsonSerialize"); - - if (FAILURE == call_user_function_ex(EG(function_table), val, &fname, &retval, 0, NULL, 1, NULL) || Z_TYPE(retval) == IS_UNDEF) { - zend_throw_exception_ex(NULL, 0, "Failed calling %s::jsonSerialize()", ce->name->val); - smart_str_appendl(buf, "null", sizeof("null") - 1); - zval_ptr_dtor(&fname); - return; - } - - if (EG(exception)) { - /* Error already raised */ - zval_ptr_dtor(&retval); - zval_ptr_dtor(&fname); - smart_str_appendl(buf, "null", sizeof("null") - 1); - return; - } - - if ((Z_TYPE(retval) == IS_OBJECT) && - (Z_OBJ_HANDLE(retval) == Z_OBJ_HANDLE_P(val))) { - /* Handle the case where jsonSerialize does: return $this; by going straight to encode array */ - json_encode_array(buf, &retval, options); - } else { - /* All other types, encode as normal */ - php_json_encode(buf, &retval, options); - } - - zval_ptr_dtor(&retval); - zval_ptr_dtor(&fname); -} -/* }}} */ - + PHP_JSON_API void php_json_encode(smart_str *buf, zval *val, int options) /* {{{ */ { - php_json_encode_zval(buf, val, options TSRMLS_CC); -again: - switch (Z_TYPE_P(val)) - { - case IS_NULL: - smart_str_appendl(buf, "null", 4); - break; - - case IS_TRUE: - smart_str_appendl(buf, "true", 4); - break; - case IS_FALSE: - smart_str_appendl(buf, "false", 5); - break; - - case IS_LONG: - smart_str_append_long(buf, Z_LVAL_P(val)); - break; - - case IS_DOUBLE: - { - char *d = NULL; - int len; - double dbl = Z_DVAL_P(val); - - if (!zend_isinf(dbl) && !zend_isnan(dbl)) { - len = spprintf(&d, 0, "%.*k", (int) EG(precision), dbl); - smart_str_appendl(buf, d, len); - efree(d); - } else { - JSON_G(error_code) = PHP_JSON_ERROR_INF_OR_NAN; - smart_str_appendc(buf, '0'); - } - } - break; - - case IS_STRING: - json_escape_string(buf, Z_STRVAL_P(val), Z_STRLEN_P(val), options); - break; - - case IS_OBJECT: - if (instanceof_function(Z_OBJCE_P(val), php_json_serializable_ce)) { - json_encode_serializable_object(buf, val, options); - break; - } - /* fallthrough -- Non-serializable object */ - case IS_ARRAY: - json_encode_array(buf, val, options); - break; - - case IS_REFERENCE: - val = Z_REFVAL_P(val); - goto again; - - default: - JSON_G(error_code) = PHP_JSON_ERROR_UNSUPPORTED_TYPE; - smart_str_appendl(buf, "null", 4); - break; - } - - return; ++ php_json_encode_zval(buf, val, options); } /* }}} */ - PHP_JSON_API void php_json_decode_ex(zval *return_value, char *str, size_t str_len, zend_long options, zend_long depth TSRMLS_DC) /* {{{ */ + PHP_JSON_API void php_json_decode_ex(zval *return_value, char *str, size_t str_len, zend_long options, zend_long depth) /* {{{ */ { - size_t utf16_len; - unsigned short *utf16; - JSON_parser jp; - - utf16 = (unsigned short *) safe_emalloc((str_len+1), sizeof(unsigned short), 1); - - utf16_len = json_utf8_to_utf16(utf16, str, str_len); - if (utf16_len <= 0) { - if (utf16) { - efree(utf16); - } - JSON_G(error_code) = PHP_JSON_ERROR_UTF8; - RETURN_NULL(); - } + php_json_parser parser; - php_json_parser_init(&parser, return_value, str, str_len, options, depth TSRMLS_CC); - if (depth <= 0) { - php_error_docref(NULL, E_WARNING, "Depth must be greater than zero"); - efree(utf16); - RETURN_NULL(); - } ++ php_json_parser_init(&parser, return_value, str, str_len, options, depth); - jp = new_JSON_parser(depth); - if (!parse_JSON_ex(jp, return_value, utf16, utf16_len, options)) { - double d; - int type, overflow_info; - zend_long p; - char *trim = str; - int trim_len = str_len; - - zval_dtor(return_value); - - /* Increment trimmed string pointer to strip leading whitespace */ - /* JSON RFC says to consider as whitespace: space, tab, LF or CR */ - while (trim_len && (*trim == ' ' || *trim == '\t' || *trim == '\n' || *trim == '\r')) { - trim++; - trim_len--; - } - - /* Decrement trimmed string length to strip trailing whitespace */ - while (trim_len && (trim[trim_len - 1] == ' ' || trim[trim_len - 1] == '\t' || trim[trim_len - 1] == '\n' || trim[trim_len - 1] == '\r')) { - trim_len--; - } - - RETVAL_NULL(); - if (trim_len == 4) { - if (!strncmp(trim, "null", trim_len)) { - /* We need to explicitly clear the error because its an actual NULL and not an error */ - jp->error_code = PHP_JSON_ERROR_NONE; - RETVAL_NULL(); - } else if (!strncmp(trim, "true", trim_len)) { - RETVAL_BOOL(1); - } - } else if (trim_len == 5 && !strncmp(trim, "false", trim_len)) { - RETVAL_BOOL(0); - } - - if ((type = is_numeric_string_ex(trim, trim_len, &p, &d, 0, &overflow_info)) != 0) { - if (type == IS_LONG) { - RETVAL_LONG(p); - } else if (type == IS_DOUBLE) { - if (options & PHP_JSON_BIGINT_AS_STRING && overflow_info) { - /* Within an object or array, a numeric literal is assumed - * to be an integer if and only if it's entirely made up of - * digits (exponent notation will result in the number - * being treated as a double). We'll match that behaviour - * here. */ - int i; - zend_bool is_float = 0; - - for (i = (trim[0] == '-' ? 1 : 0); i < trim_len; i++) { - /* Not using isdigit() because it's locale specific, - * but we expect JSON input to always be UTF-8. */ - if (trim[i] < '0' || trim[i] > '9') { - is_float = 1; - break; - } - } - - if (is_float) { - RETVAL_DOUBLE(d); - } else { - RETVAL_STRINGL(trim, trim_len); - } - } else { - RETVAL_DOUBLE(d); - } - } - } - - if (Z_TYPE_P(return_value) != IS_NULL) { - jp->error_code = PHP_JSON_ERROR_NONE; - } + if (php_json_yyparse(&parser)) { + JSON_G(error_code) = php_json_parser_error_code(&parser); + RETURN_NULL(); } - efree(utf16); - JSON_G(error_code) = jp->error_code; - free_JSON_parser(jp); } /* }}} */ @@@ -193,9 -778,9 +198,9 @@@ static PHP_FUNCTION(json_encode zval *parameter; smart_str buf = {0}; zend_long options = 0; - zend_long depth = JSON_PARSER_DEFAULT_DEPTH; + zend_long depth = PHP_JSON_PARSER_DEFAULT_DEPTH; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|ll", ¶meter, &options, &depth) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "z|ll", ¶meter, &options, &depth) == FAILURE) { return; } @@@ -222,10 -807,10 +227,10 @@@ static PHP_FUNCTION(json_decode char *str; size_t str_len; zend_bool assoc = 0; /* return JS objects as PHP objects by default */ - zend_long depth = JSON_PARSER_DEFAULT_DEPTH; + zend_long depth = PHP_JSON_PARSER_DEFAULT_DEPTH; zend_long options = 0; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|bll", &str, &str_len, &assoc, &depth, &options) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|bll", &str, &str_len, &assoc, &depth, &options) == FAILURE) { return; } diff --cc ext/json/json_encoder.c index 6976d90,0000000..b9fb355 mode 100644,000000..100644 --- a/ext/json/json_encoder.c +++ b/ext/json/json_encoder.c @@@ -1,541 -1,0 +1,541 @@@ +/* + +----------------------------------------------------------------------+ + | PHP Version 7 | + +----------------------------------------------------------------------+ + | Copyright (c) 1997-2014 The PHP Group | + +----------------------------------------------------------------------+ + | This source file is subject to version 3.01 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available through the world-wide-web at the following url: | + | http://www.php.net/license/3_01.txt | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Author: Omar Kilani <omar@php.net> | + +----------------------------------------------------------------------+ +*/ + +/* $Id$ */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "php.h" +#include "php_ini.h" +#include "ext/standard/info.h" +#include "ext/standard/html.h" +#include "zend_smart_str.h" +#include "php_json.h" +#include <zend_exceptions.h> + +ZEND_DECLARE_MODULE_GLOBALS(json) + +static const char digits[] = "0123456789abcdef"; + - static void json_escape_string(smart_str *buf, char *s, size_t len, int options TSRMLS_DC); ++static void json_escape_string(smart_str *buf, char *s, size_t len, int options); + - static int json_determine_array_type(zval *val TSRMLS_DC) /* {{{ */ ++static int json_determine_array_type(zval *val) /* {{{ */ +{ + int i; + HashTable *myht = HASH_OF(val); + + i = myht ? zend_hash_num_elements(myht) : 0; + if (i > 0) { + zend_string *key; + zend_ulong index, idx; + + idx = 0; + ZEND_HASH_FOREACH_KEY(myht, index, key) { + if (key) { + return PHP_JSON_OUTPUT_OBJECT; + } else { + if (index != idx) { + return PHP_JSON_OUTPUT_OBJECT; + } + } + idx++; + } ZEND_HASH_FOREACH_END(); + } + + return PHP_JSON_OUTPUT_ARRAY; +} +/* }}} */ + +/* {{{ Pretty printing support functions */ + - static inline void json_pretty_print_char(smart_str *buf, int options, char c TSRMLS_DC) /* {{{ */ ++static inline void json_pretty_print_char(smart_str *buf, int options, char c) /* {{{ */ +{ + if (options & PHP_JSON_PRETTY_PRINT) { + smart_str_appendc(buf, c); + } +} +/* }}} */ + - static inline void json_pretty_print_indent(smart_str *buf, int options TSRMLS_DC) /* {{{ */ ++static inline void json_pretty_print_indent(smart_str *buf, int options) /* {{{ */ +{ + int i; + + if (options & PHP_JSON_PRETTY_PRINT) { + for (i = 0; i < JSON_G(encoder_depth); ++i) { + smart_str_appendl(buf, " ", 4); + } + } +} +/* }}} */ + +/* }}} */ + - static void json_encode_array(smart_str *buf, zval *val, int options TSRMLS_DC) /* {{{ */ ++static void json_encode_array(smart_str *buf, zval *val, int options) /* {{{ */ +{ + int i, r, need_comma = 0; + HashTable *myht; + + if (Z_TYPE_P(val) == IS_ARRAY) { + myht = HASH_OF(val); - r = (options & PHP_JSON_FORCE_OBJECT) ? PHP_JSON_OUTPUT_OBJECT : json_determine_array_type(val TSRMLS_CC); ++ r = (options & PHP_JSON_FORCE_OBJECT) ? PHP_JSON_OUTPUT_OBJECT : json_determine_array_type(val); + } else { + myht = Z_OBJPROP_P(val); + r = PHP_JSON_OUTPUT_OBJECT; + } + + if (myht && ZEND_HASH_GET_APPLY_COUNT(myht) > 1) { + JSON_G(error_code) = PHP_JSON_ERROR_RECURSION; + smart_str_appendl(buf, "null", 4); + return; + } + + if (r == PHP_JSON_OUTPUT_ARRAY) { + smart_str_appendc(buf, '['); + } else { + smart_str_appendc(buf, '{'); + } + + ++JSON_G(encoder_depth); + + i = myht ? zend_hash_num_elements(myht) : 0; + + if (i > 0) { + zend_string *key; + zval *data; + zend_ulong index; + HashTable *tmp_ht; + + ZEND_HASH_FOREACH_KEY_VAL_IND(myht, index, key, data) { + ZVAL_DEREF(data); + tmp_ht = HASH_OF(data); + if (tmp_ht && ZEND_HASH_APPLY_PROTECTION(tmp_ht)) { + ZEND_HASH_INC_APPLY_COUNT(tmp_ht); + } + + if (r == PHP_JSON_OUTPUT_ARRAY) { + if (need_comma) { + smart_str_appendc(buf, ','); + } else { + need_comma = 1; + } + - json_pretty_print_char(buf, options, '\n' TSRMLS_CC); - json_pretty_print_indent(buf, options TSRMLS_CC); - php_json_encode(buf, data, options TSRMLS_CC); ++ json_pretty_print_char(buf, options, '\n'); ++ json_pretty_print_indent(buf, options); ++ php_json_encode(buf, data, options); + } else if (r == PHP_JSON_OUTPUT_OBJECT) { + if (key) { + if (key->val[0] == '\0' && Z_TYPE_P(val) == IS_OBJECT) { + /* Skip protected and private members. */ + if (tmp_ht && ZEND_HASH_APPLY_PROTECTION(tmp_ht)) { + ZEND_HASH_DEC_APPLY_COUNT(tmp_ht); + } + continue; + } + + if (need_comma) { + smart_str_appendc(buf, ','); + } else { + need_comma = 1; + } + - json_pretty_print_char(buf, options, '\n' TSRMLS_CC); - json_pretty_print_indent(buf, options TSRMLS_CC); ++ json_pretty_print_char(buf, options, '\n'); ++ json_pretty_print_indent(buf, options); + - json_escape_string(buf, key->val, key->len, options & ~PHP_JSON_NUMERIC_CHECK TSRMLS_CC); ++ json_escape_string(buf, key->val, key->len, options & ~PHP_JSON_NUMERIC_CHECK); + smart_str_appendc(buf, ':'); + - json_pretty_print_char(buf, options, ' ' TSRMLS_CC); ++ json_pretty_print_char(buf, options, ' '); + - php_json_encode(buf, data, options TSRMLS_CC); ++ php_json_encode(buf, data, options); + } else { + if (need_comma) { + smart_str_appendc(buf, ','); + } else { + need_comma = 1; + } + - json_pretty_print_char(buf, options, '\n' TSRMLS_CC); - json_pretty_print_indent(buf, options TSRMLS_CC); ++ json_pretty_print_char(buf, options, '\n'); ++ json_pretty_print_indent(buf, options); + + smart_str_appendc(buf, '"'); + smart_str_append_long(buf, (zend_long) index); + smart_str_appendc(buf, '"'); + smart_str_appendc(buf, ':'); + - json_pretty_print_char(buf, options, ' ' TSRMLS_CC); ++ json_pretty_print_char(buf, options, ' '); + - php_json_encode(buf, data, options TSRMLS_CC); ++ php_json_encode(buf, data, options); + } + } + + if (tmp_ht && ZEND_HASH_APPLY_PROTECTION(tmp_ht)) { + ZEND_HASH_DEC_APPLY_COUNT(tmp_ht); + } + } ZEND_HASH_FOREACH_END(); + } + + if (JSON_G(encoder_depth) > JSON_G(encode_max_depth)) { + JSON_G(error_code) = PHP_JSON_ERROR_DEPTH; + } + --JSON_G(encoder_depth); + + /* Only keep closing bracket on same line for empty arrays/objects */ + if (need_comma) { - json_pretty_print_char(buf, options, '\n' TSRMLS_CC); - json_pretty_print_indent(buf, options TSRMLS_CC); ++ json_pretty_print_char(buf, options, '\n'); ++ json_pretty_print_indent(buf, options); + } + + if (r == PHP_JSON_OUTPUT_ARRAY) { + smart_str_appendc(buf, ']'); + } else { + smart_str_appendc(buf, '}'); + } +} +/* }}} */ + +static int json_utf8_to_utf16(unsigned short *utf16, char utf8[], int len) /* {{{ */ +{ + size_t pos = 0, us; + int j, status; + + if (utf16) { + /* really convert the utf8 string */ + for (j=0 ; pos < len ; j++) { + us = php_next_utf8_char((const unsigned char *)utf8, len, &pos, &status); + if (status != SUCCESS) { + return -1; + } + /* From http://en.wikipedia.org/wiki/UTF16 */ + if (us >= 0x10000) { + us -= 0x10000; + utf16[j++] = (unsigned short)((us >> 10) | 0xd800); + utf16[j] = (unsigned short)((us & 0x3ff) | 0xdc00); + } else { + utf16[j] = (unsigned short)us; + } + } + } else { + /* Only check if utf8 string is valid, and compute utf16 length */ + for (j=0 ; pos < len ; j++) { + us = php_next_utf8_char((const unsigned char *)utf8, len, &pos, &status); + if (status != SUCCESS) { + return -1; + } + if (us >= 0x10000) { + j++; + } + } + } + return j; +} +/* }}} */ + - static void json_escape_string(smart_str *buf, char *s, size_t len, int options TSRMLS_DC) /* {{{ */ ++static void json_escape_string(smart_str *buf, char *s, size_t len, int options) /* {{{ */ +{ + int status; + unsigned int us, next_us = 0; + size_t pos, checkpoint; + + if (len == 0) { + smart_str_appendl(buf, "\"\"", 2); + return; + } + + if (options & PHP_JSON_NUMERIC_CHECK) { + double d; + int type; + zend_long p; + + if ((type = is_numeric_string(s, len, &p, &d, 0)) != 0) { + if (type == IS_LONG) { + smart_str_append_long(buf, p); + } else if (type == IS_DOUBLE) { + if (!zend_isinf(d) && !zend_isnan(d)) { + char *tmp; + int l = spprintf(&tmp, 0, "%.*k", (int) EG(precision), d); + smart_str_appendl(buf, tmp, l); + efree(tmp); + } else { + JSON_G(error_code) = PHP_JSON_ERROR_INF_OR_NAN; + smart_str_appendc(buf, '0'); + } + } + return; + } + + } + + if (options & PHP_JSON_UNESCAPED_UNICODE) { + /* validate UTF-8 string first */ + if (json_utf8_to_utf16(NULL, s, len) < 0) { + JSON_G(error_code) = PHP_JSON_ERROR_UTF8; + smart_str_appendl(buf, "null", 4); - return; ++ return; + } + } + + pos = 0; + checkpoint = buf->s ? buf->s->len : 0; + + /* pre-allocate for string length plus 2 quotes */ + smart_str_alloc(buf, len+2, 0); + smart_str_appendc(buf, '"'); + + do { + if (UNEXPECTED(next_us)) { + us = next_us; + next_us = 0; + } else { + us = (unsigned char)s[pos]; + if (!(options & PHP_JSON_UNESCAPED_UNICODE) && us >= 0x80) { + /* UTF-8 character */ + us = php_next_utf8_char((const unsigned char *)s, len, &pos, &status); + if (status != SUCCESS) { + if (buf->s) { + buf->s->len = checkpoint; + } + JSON_G(error_code) = PHP_JSON_ERROR_UTF8; + smart_str_appendl(buf, "null", 4); + return; + } + /* From http://en.wikipedia.org/wiki/UTF16 */ + if (us >= 0x10000) { + us -= 0x10000; + next_us = (unsigned short)((us & 0x3ff) | 0xdc00); + us = (unsigned short)((us >> 10) | 0xd800); + } + } else { + pos++; + } + } + + switch (us) { + case '"': + if (options & PHP_JSON_HEX_QUOT) { + smart_str_appendl(buf, "\\u0022", 6); + } else { + smart_str_appendl(buf, "\\\"", 2); + } + break; + + case '\\': + smart_str_appendl(buf, "\\\\", 2); + break; + + case '/': + if (options & PHP_JSON_UNESCAPED_SLASHES) { + smart_str_appendc(buf, '/'); + } else { + smart_str_appendl(buf, "\\/", 2); + } + break; + + case '\b': + smart_str_appendl(buf, "\\b", 2); + break; + + case '\f': + smart_str_appendl(buf, "\\f", 2); + break; + + case '\n': + smart_str_appendl(buf, "\\n", 2); + break; + + case '\r': + smart_str_appendl(buf, "\\r", 2); + break; + + case '\t': + smart_str_appendl(buf, "\\t", 2); + break; + + case '<': + if (options & PHP_JSON_HEX_TAG) { + smart_str_appendl(buf, "\\u003C", 6); + } else { + smart_str_appendc(buf, '<'); + } + break; + + case '>': + if (options & PHP_JSON_HEX_TAG) { + smart_str_appendl(buf, "\\u003E", 6); + } else { + smart_str_appendc(buf, '>'); + } + break; + + case '&': + if (options & PHP_JSON_HEX_AMP) { + smart_str_appendl(buf, "\\u0026", 6); + } else { + smart_str_appendc(buf, '&'); + } + break; + + case '\'': + if (options & PHP_JSON_HEX_APOS) { + smart_str_appendl(buf, "\\u0027", 6); + } else { + smart_str_appendc(buf, '\''); + } + break; + + default: + if (us >= ' ' && ((options & PHP_JSON_UNESCAPED_UNICODE) || (us & 127) == us)) { + smart_str_appendc(buf, (unsigned char) us); + } else { + smart_str_appendl(buf, "\\u", 2); + smart_str_appendc(buf, digits[(us & 0xf000) >> 12]); + smart_str_appendc(buf, digits[(us & 0xf00) >> 8]); + smart_str_appendc(buf, digits[(us & 0xf0) >> 4]); + smart_str_appendc(buf, digits[(us & 0xf)]); + } + break; + } + } while (pos < len || next_us); - ++ + smart_str_appendc(buf, '"'); +} +/* }}} */ + - static void json_encode_serializable_object(smart_str *buf, zval *val, int options TSRMLS_DC) /* {{{ */ ++static void json_encode_serializable_object(smart_str *buf, zval *val, int options) /* {{{ */ +{ + zend_class_entry *ce = Z_OBJCE_P(val); + zval retval, fname; + HashTable* myht; + + if (Z_TYPE_P(val) == IS_ARRAY) { + myht = HASH_OF(val); + } else { + myht = Z_OBJPROP_P(val); + } + + if (myht && ZEND_HASH_GET_APPLY_COUNT(myht) > 1) { + JSON_G(error_code) = PHP_JSON_ERROR_RECURSION; + smart_str_appendl(buf, "null", 4); + return; + } + + ZVAL_STRING(&fname, "jsonSerialize"); + - if (FAILURE == call_user_function_ex(EG(function_table), val, &fname, &retval, 0, NULL, 1, NULL TSRMLS_CC) || Z_TYPE(retval) == IS_UNDEF) { - zend_throw_exception_ex(NULL, 0 TSRMLS_CC, "Failed calling %s::jsonSerialize()", ce->name->val); ++ if (FAILURE == call_user_function_ex(EG(function_table), val, &fname, &retval, 0, NULL, 1, NULL) || Z_TYPE(retval) == IS_UNDEF) { ++ zend_throw_exception_ex(NULL, 0, "Failed calling %s::jsonSerialize()", ce->name->val); + smart_str_appendl(buf, "null", sizeof("null") - 1); + zval_ptr_dtor(&fname); + return; - } ++ } + + if (EG(exception)) { + /* Error already raised */ + zval_ptr_dtor(&retval); + zval_ptr_dtor(&fname); + smart_str_appendl(buf, "null", sizeof("null") - 1); + return; + } + + if ((Z_TYPE(retval) == IS_OBJECT) && + (Z_OBJ_HANDLE(retval) == Z_OBJ_HANDLE_P(val))) { + /* Handle the case where jsonSerialize does: return $this; by going straight to encode array */ - json_encode_array(buf, &retval, options TSRMLS_CC); ++ json_encode_array(buf, &retval, options); + } else { + /* All other types, encode as normal */ - php_json_encode(buf, &retval, options TSRMLS_CC); ++ php_json_encode(buf, &retval, options); + } + + zval_ptr_dtor(&retval); + zval_ptr_dtor(&fname); +} +/* }}} */ + - void php_json_encode_zval(smart_str *buf, zval *val, int options TSRMLS_DC) /* {{{ */ ++void php_json_encode_zval(smart_str *buf, zval *val, int options) /* {{{ */ +{ +again: + switch (Z_TYPE_P(val)) + { + case IS_NULL: + smart_str_appendl(buf, "null", 4); + break; + + case IS_TRUE: + smart_str_appendl(buf, "true", 4); + break; + case IS_FALSE: + smart_str_appendl(buf, "false", 5); + break; + + case IS_LONG: + smart_str_append_long(buf, Z_LVAL_P(val)); + break; + + case IS_DOUBLE: + { + char *d = NULL; + int len; + double dbl = Z_DVAL_P(val); + + if (!zend_isinf(dbl) && !zend_isnan(dbl)) { + len = spprintf(&d, 0, "%.*k", (int) EG(precision), dbl); + smart_str_appendl(buf, d, len); + efree(d); + } else { + JSON_G(error_code) = PHP_JSON_ERROR_INF_OR_NAN; + smart_str_appendc(buf, '0'); + } + } + break; + + case IS_STRING: - json_escape_string(buf, Z_STRVAL_P(val), Z_STRLEN_P(val), options TSRMLS_CC); ++ json_escape_string(buf, Z_STRVAL_P(val), Z_STRLEN_P(val), options); + break; + + case IS_OBJECT: - if (instanceof_function(Z_OBJCE_P(val), php_json_serializable_ce TSRMLS_CC)) { - json_encode_serializable_object(buf, val, options TSRMLS_CC); ++ if (instanceof_function(Z_OBJCE_P(val), php_json_serializable_ce)) { ++ json_encode_serializable_object(buf, val, options); + break; + } + /* fallthrough -- Non-serializable object */ + case IS_ARRAY: - json_encode_array(buf, val, options TSRMLS_CC); ++ json_encode_array(buf, val, options); + break; + + case IS_REFERENCE: + val = Z_REFVAL_P(val); + goto again; + + default: + JSON_G(error_code) = PHP_JSON_ERROR_UNSUPPORTED_TYPE; + smart_str_appendl(buf, "null", 4); + break; + } + + return; +} +/* }}} */ + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + * vim600: noet sw=4 ts=4 fdm=marker + * vim<600: noet sw=4 ts=4 + */ diff --cc ext/json/json_parser.y index dbacc3d,0000000..4af46ec mode 100644,000000..100644 --- a/ext/json/json_parser.y +++ b/ext/json/json_parser.y @@@ -1,231 -1,0 +1,227 @@@ +%code top { +/* + +----------------------------------------------------------------------+ + | PHP Version 5 | + +----------------------------------------------------------------------+ + | Copyright (c) 1997-2014 The PHP Group | + +----------------------------------------------------------------------+ + | This source file is subject to version 3.01 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available through the world-wide-web at the following url: | + | http://www.php.net/license/3_01.txt | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Author: Jakub Zelenka <bukka@php.net> | + +----------------------------------------------------------------------+ +*/ + +#include "php.h" +#include "php_json.h" +#include "php_json_parser.h" + +#define YYDEBUG 0 + +#if YYDEBUG +int json_yydebug = 1; +#endif + +#define PHP_JSON_USE(uv) ((void) (uv)) +#define PHP_JSON_USE_1(uvr, uv1) PHP_JSON_USE(uvr); PHP_JSON_USE(uv1) +#define PHP_JSON_USE_2(uvr, uv1, uv2) PHP_JSON_USE(uvr); PHP_JSON_USE(uv1); PHP_JSON_USE(uv2) + +} + +%pure-parser +%name-prefix "php_json_yy" +%lex-param { php_json_parser *parser } +%parse-param { php_json_parser *parser } + +%union { + zval value; + struct { + zval key; + zval val; + } pair; +} + + +%token <value> PHP_JSON_T_NUL +%token <value> PHP_JSON_T_TRUE +%token <value> PHP_JSON_T_FALSE +%token <value> PHP_JSON_T_INT +%token <value> PHP_JSON_T_DOUBLE +%token <value> PHP_JSON_T_STRING +%token <value> PHP_JSON_T_ESTRING +%token <value> PHP_JSON_T_EOI +%token <value> PHP_JSON_T_ERROR + +%type <value> start object key value array errlex +%type <value> members member elements element +%type <pair> pair + +%destructor { zval_dtor(&$$); } <value> +%destructor { zend_hash_destroy($$); FREE_HASHTABLE($$); } <ht> +%destructor { zval_dtor(&$$.key); zval_dtor(&$$.val); } <pair> + +%code { +int php_json_yylex(union YYSTYPE *value, php_json_parser *parser); +void php_json_yyerror(php_json_parser *parser, char const *msg); +void php_json_parser_object_init(php_json_parser *parser, zval *object); +void php_json_parser_object_update(php_json_parser *parser, zval *object, zval *zkey, zval *zvalue); +void php_json_parser_array_init(zval *object); +void php_json_parser_array_append(zval *array, zval *zvalue); + +#define PHP_JSON_DEPTH_DEC --parser->depth +#define PHP_JSON_DEPTH_INC \ + if (parser->max_depth && parser->depth >= parser->max_depth) { \ + parser->scanner.errcode = PHP_JSON_ERROR_DEPTH; \ + YYERROR; \ + } \ + ++parser->depth +} + +%% /* Rules */ + +start: + value PHP_JSON_T_EOI { $$ = $1; *parser->return_value = $1; PHP_JSON_USE($2); YYACCEPT; } + | value errlex { PHP_JSON_USE_2($$, $1, $2); } +; + +object: + '{' { PHP_JSON_DEPTH_INC; } members object_end { PHP_JSON_DEPTH_DEC; $$ = $3; } +; + +object_end: + '}' + | ']' { parser->scanner.errcode = PHP_JSON_ERROR_STATE_MISMATCH; YYERROR; } +; + +members: + /* empty */ { php_json_parser_object_init(parser, &$$); } + | member +; + +member: + pair { php_json_parser_object_init(parser, &$$); php_json_parser_object_update(parser, &$$, &$1.key, &$1.val); } + | member ',' pair { php_json_parser_object_update(parser, &$1, &$3.key, &$3.val); $$ = $1; } + | member errlex { PHP_JSON_USE_2($$, $1, $2); } +; + +pair: + key ':' value { $$.key = $1; $$.val = $3; } + | key errlex { PHP_JSON_USE_2($$, $1, $2); } +; + +array: + '[' { PHP_JSON_DEPTH_INC; } elements array_end { PHP_JSON_DEPTH_DEC; $$ = $3; } +; + +array_end: + ']' + | '}' { parser->scanner.errcode = PHP_JSON_ERROR_STATE_MISMATCH; YYERROR; } +; + +elements: + /* empty */ { php_json_parser_array_init(&$$); } + | element +; + +element: + value { php_json_parser_array_init(&$$); php_json_parser_array_append(&$$, &$1); } + | element ',' value { php_json_parser_array_append(&$1, &$3); $$ = $1; } + | element errlex { PHP_JSON_USE_2($$, $1, $2); } +; + +key: + PHP_JSON_T_STRING + | PHP_JSON_T_ESTRING +; + +value: + object + | array + | PHP_JSON_T_STRING + | PHP_JSON_T_ESTRING + | PHP_JSON_T_INT + | PHP_JSON_T_DOUBLE + | PHP_JSON_T_NUL + | PHP_JSON_T_TRUE + | PHP_JSON_T_FALSE + | errlex +; + +errlex: + PHP_JSON_T_ERROR { PHP_JSON_USE_1($$, $1); YYERROR; } +; + +%% /* Functions */ + - void php_json_parser_init(php_json_parser *parser, zval *return_value, char *str, int str_len, long options, long max_depth TSRMLS_DC) ++void php_json_parser_init(php_json_parser *parser, zval *return_value, char *str, int str_len, long options, long max_depth) +{ + memset(parser, 0, sizeof(php_json_parser)); + php_json_scanner_init(&parser->scanner, str, str_len, options); + parser->depth = 1; + parser->max_depth = max_depth; + parser->return_value = return_value; - TSRMLS_SET_CTX(parser->zts_ctx); +} + +php_json_error_code php_json_parser_error_code(php_json_parser *parser) +{ + return parser->scanner.errcode; +} + +void php_json_parser_object_init(php_json_parser *parser, zval *object) +{ - TSRMLS_FETCH_FROM_CTX(parser->zts_ctx); + if (parser->scanner.options & PHP_JSON_OBJECT_AS_ARRAY) { + array_init(object); + } else { + object_init(object); + } +} + +void php_json_parser_object_update(php_json_parser *parser, zval *object, zval *zkey, zval *zvalue) +{ + char *key = Z_STRVAL_P(zkey); + int key_len = Z_STRLEN_P(zkey); - TSRMLS_FETCH_FROM_CTX(parser->zts_ctx); + + if (parser->scanner.options & PHP_JSON_OBJECT_AS_ARRAY) { + add_assoc_zval_ex(object, key, key_len, zvalue); + } else { + if (key_len == 0) { + key = "_empty_"; + key_len = sizeof("_empty_") - 1; + } + add_property_zval_ex(object, key, key_len, zvalue TSRMLS_CC); + + if (Z_REFCOUNTED_P(zvalue)) { + Z_DELREF_P(zvalue); + } + } + zval_dtor(zkey); +} + +void php_json_parser_array_init(zval *array) +{ + array_init(array); +} + +void php_json_parser_array_append(zval *array, zval *zvalue) +{ + add_next_index_zval(array, zvalue); +} + +int php_json_yylex(union YYSTYPE *value, php_json_parser *parser) +{ - TSRMLS_FETCH_FROM_CTX(parser->zts_ctx); - int token = php_json_scan(&parser->scanner TSRMLS_CC); ++ int token = php_json_scan(&parser->scanner); + value->value = parser->scanner.value; + return token; +} + +void php_json_yyerror(php_json_parser *parser, char const *msg) +{ + if (!parser->scanner.errcode) { + parser->scanner.errcode = PHP_JSON_ERROR_SYNTAX; + } +} diff --cc ext/json/json_scanner.re index 79df124,0000000..0592da8 mode 100644,000000..100644 --- a/ext/json/json_scanner.re +++ b/ext/json/json_scanner.re @@@ -1,347 -1,0 +1,347 @@@ +/* + +----------------------------------------------------------------------+ + | PHP Version 5 | + +----------------------------------------------------------------------+ + | Copyright (c) 1997-2014 The PHP Group | + +----------------------------------------------------------------------+ + | This source file is subject to version 3.01 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available through the world-wide-web at the following url: | + | http://www.php.net/license/3_01.txt | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Author: Jakub Zelenka <bukka@php.net> | + +----------------------------------------------------------------------+ +*/ + +#include "php.h" +#include "php_json_scanner.h" +#include "php_json_scanner_defs.h" +#include "php_json_parser.h" +#include "json_parser.tab.h" + +#define YYCTYPE php_json_ctype +#define YYCURSOR s->cursor +#define YYLIMIT s->limit +#define YYMARKER s->marker +#define YYCTXMARKER s->ctxmarker + +#define YYGETCONDITION() s->state +#define YYSETCONDITION(yystate) s->state = yystate + +#define YYFILL(n) + +#define PHP_JSON_CONDITION_SET(condition) YYSETCONDITION(yyc##condition) +#define PHP_JSON_CONDITION_GOTO(condition) goto yyc_##condition + +#define PHP_JSON_SCANNER_COPY_ESC() php_json_scanner_copy_string(s, 0) +#define PHP_JSON_SCANNER_COPY_UTF() php_json_scanner_copy_string(s, 5) +#define PHP_JSON_SCANNER_COPY_UTF_SP() php_json_scanner_copy_string(s, 11) + +#define PHP_JSON_INT_MAX_LENGTH (MAX_LENGTH_OF_LONG - 1) + + +static void php_json_scanner_copy_string(php_json_scanner *s, int esc_size) +{ + size_t len = s->cursor - s->str_start - esc_size - 1; + if (len) { + memcpy(s->pstr, s->str_start, len); + s->pstr += len; + } +} + +static int php_json_hex_to_int(char code) +{ + if (code >= '0' && code <= '9') { + return code - '0'; + } else if (code >= 'A' && code <= 'F') { + return code - ('A' - 10); + } else if (code >= 'a' && code <= 'f') { + return code - ('a' - 10); + } else { + /* this should never happened (just to suppress compiler warning) */ + return -1; + } +} + +static int php_json_ucs2_to_int_ex(php_json_scanner *s, int size, int start) +{ + int i, code = 0; + php_json_ctype *pc = s->cursor - start; + for (i = 0; i < size; i++) { + code |= php_json_hex_to_int(*(pc--)) << (i * 4); + } + return code; +} + +static int php_json_ucs2_to_int(php_json_scanner *s, int size) +{ + return php_json_ucs2_to_int_ex(s, size, 1); +} + +void php_json_scanner_init(php_json_scanner *s, char *str, int str_len, long options) +{ + s->cursor = (php_json_ctype *) str; + s->limit = (php_json_ctype *) str + str_len; + s->options = options; + PHP_JSON_CONDITION_SET(JS); +} + - int php_json_scan(php_json_scanner *s TSRMLS_DC) ++int php_json_scan(php_json_scanner *s) +{ + ZVAL_NULL(&s->value); + +std: + s->token = s->cursor; + +/*!re2c + re2c:indent:top = 1; + re2c:yyfill:enable = 0; + + DIGIT = [0-9] ; + DIGITNZ = [1-9] ; + UINT = "0" | ( DIGITNZ DIGIT* ) ; + INT = "-"? UINT ; + HEX = DIGIT | [a-fA-F] ; + HEXNZ = DIGITNZ | [a-fA-F] ; + HEX7 = [0-7] ; + HEXC = DIGIT | [a-cA-C] ; + FLOAT = INT "." DIGIT+ ; + EXP = ( INT | FLOAT ) [eE] [+-]? DIGIT+ ; + NL = "\r"? "\n" ; + WS = [ \t\r]+ ; + EOI = "\000"; + CTRL = [\x00-\x1F] ; + UTF8T = [\x80-\xBF] ; + UTF8_1 = [\x00-\x7F] ; + UTF8_2 = [\xC2-\xDF] UTF8T ; + UTF8_3A = "\xE0" [\xA0-\xBF] UTF8T ; + UTF8_3B = [\xE1-\xEC] UTF8T{2} ; + UTF8_3C = "\xED" [\x80-\x9F] UTF8T ; + UTF8_3D = [\xEE-\xEF] UTF8T{2} ; + UTF8_3 = UTF8_3A | UTF8_3B | UTF8_3C | UTF8_3D ; + UTF8_4A = "\xF0"[\x90-\xBF] UTF8T{2} ; + UTF8_4B = [\xF1-\xF3] UTF8T{3} ; + UTF8_4C = "\xF4" [\x80-\x8F] UTF8T{2} ; + UTF8_4 = UTF8_4A | UTF8_4B | UTF8_4C ; + UTF8 = UTF8_1 | UTF8_2 | UTF8_3 | UTF8_4 ; + ANY = [^] ; + ESCPREF = "\\" ; + ESCSYM = ( "\"" | "\\" | "/" | [bfnrt] ) ; + ESC = ESCPREF ESCSYM ; + UTFSYM = "u" ; + UTFPREF = ESCPREF UTFSYM ; + UCS2 = UTFPREF HEX{4} ; + UTF16_1 = UTFPREF "00" HEX7 HEX ; + UTF16_2 = UTFPREF "0" HEX7 HEX{2} ; + UTF16_3 = UTFPREF ( ( ( HEXC | [efEF] ) HEX ) | ( [dD] HEX7 ) ) HEX{2} ; + UTF16_4 = UTFPREF [dD] [89abAB] HEX{2} UTFPREF [dD] [c-fC-F] HEX{2} ; + + <JS>"{" { return '{'; } + <JS>"}" { return '}'; } + <JS>"[" { return '['; } + <JS>"]" { return ']'; } + <JS>":" { return ':'; } + <JS>"," { return ','; } + <JS>"null" { + ZVAL_NULL(&s->value); + return PHP_JSON_T_NUL; + } + <JS>"true" { + ZVAL_TRUE(&s->value); + return PHP_JSON_T_TRUE; + } + <JS>"false" { + ZVAL_FALSE(&s->value); + return PHP_JSON_T_FALSE; + } + <JS>INT { + zend_bool bigint = 0, negative = s->token[0] == '-'; + size_t digits = (size_t) (s->cursor - s->token - negative); + if (digits >= PHP_JSON_INT_MAX_LENGTH) { + if (digits == PHP_JSON_INT_MAX_LENGTH) { + int cmp = strncmp((char *) (s->token + negative), LONG_MIN_DIGITS, PHP_JSON_INT_MAX_LENGTH); + if (!(cmp < 0 || (cmp == 0 && negative))) { + bigint = 1; + } + } else { + bigint = 1; + } + } + if (!bigint) { + ZVAL_LONG(&s->value, strtol((char *) s->token, NULL, 10)); + return PHP_JSON_T_INT; + } else if (s->options & PHP_JSON_BIGINT_AS_STRING) { + ZVAL_STRINGL(&s->value, (char *) s->token, s->cursor - s->token); + return PHP_JSON_T_STRING; + } else { + ZVAL_DOUBLE(&s->value, zend_strtod((char *) s->token, NULL)); + return PHP_JSON_T_DOUBLE; + } + } + <JS>FLOAT|EXP { + ZVAL_DOUBLE(&s->value, zend_strtod((char *) s->token, NULL)); + return PHP_JSON_T_DOUBLE; + } + <JS>NL|WS { goto std; } + <JS>EOI { + if (s->limit < s->cursor) { + return PHP_JSON_T_EOI; + } else { + s->errcode = PHP_JSON_ERROR_SYNTAX; + return PHP_JSON_T_ERROR; + } + } + <JS>["] { + s->str_start = s->cursor; + s->str_esc = 0; + PHP_JSON_CONDITION_SET(STR_P1); + PHP_JSON_CONDITION_GOTO(STR_P1); + } + + <STR_P1>CTRL { + s->errcode = PHP_JSON_ERROR_CTRL_CHAR; + return PHP_JSON_T_ERROR; + } + <STR_P1>UTF16_1 { + s->str_esc += 5; + PHP_JSON_CONDITION_GOTO(STR_P1); + } + <STR_P1>UTF16_2 { + s->str_esc += 4; + PHP_JSON_CONDITION_GOTO(STR_P1); + } + <STR_P1>UTF16_3 { + s->str_esc += 3; + PHP_JSON_CONDITION_GOTO(STR_P1); + } + <STR_P1>UTF16_4 { + s->str_esc += 8; + PHP_JSON_CONDITION_GOTO(STR_P1); + } + <STR_P1>UCS2 { + s->errcode = PHP_JSON_ERROR_UTF16; + return PHP_JSON_T_ERROR; + } + <STR_P1>ESC { + s->str_esc++; + PHP_JSON_CONDITION_GOTO(STR_P1); + } + <STR_P1>ESCPREF { + s->errcode = PHP_JSON_ERROR_SYNTAX; + return PHP_JSON_T_ERROR; + } + <STR_P1>["] { + zend_string *str; + size_t len = s->cursor - s->str_start - s->str_esc - 1; + if (len == 0) { + PHP_JSON_CONDITION_SET(JS); + ZVAL_EMPTY_STRING(&s->value); + return PHP_JSON_T_ESTRING; + } + str = zend_string_alloc(len, 0); + str->val[len] = '\0'; + ZVAL_STR(&s->value, str); + if (s->str_esc) { + s->pstr = (php_json_ctype *) Z_STRVAL(s->value); + s->cursor = s->str_start; + PHP_JSON_CONDITION_SET(STR_P2); + PHP_JSON_CONDITION_GOTO(STR_P2); + } else { + memcpy(Z_STRVAL(s->value), s->str_start, len); + PHP_JSON_CONDITION_SET(JS); + return PHP_JSON_T_STRING; + } + } + <STR_P1>UTF8 { PHP_JSON_CONDITION_GOTO(STR_P1); } + <STR_P1>ANY { + s->errcode = PHP_JSON_ERROR_UTF8; + return PHP_JSON_T_ERROR; + } + + <STR_P2>UTF16_1 { + int utf16 = php_json_ucs2_to_int(s, 2); + PHP_JSON_SCANNER_COPY_UTF(); + *(s->pstr++) = (char) utf16; + s->str_start = s->cursor; + PHP_JSON_CONDITION_GOTO(STR_P2); + } + <STR_P2>UTF16_2 { + int utf16 = php_json_ucs2_to_int(s, 3); + PHP_JSON_SCANNER_COPY_UTF(); + *(s->pstr++) = (char) (0xc0 | (utf16 >> 6)); + *(s->pstr++) = (char) (0x80 | (utf16 & 0x3f)); + s->str_start = s->cursor; + PHP_JSON_CONDITION_GOTO(STR_P2); + } + <STR_P2>UTF16_3 { + int utf16 = php_json_ucs2_to_int(s, 4); + PHP_JSON_SCANNER_COPY_UTF(); + *(s->pstr++) = (char) (0xe0 | (utf16 >> 12)); + *(s->pstr++) = (char) (0x80 | ((utf16 >> 6) & 0x3f)); + *(s->pstr++) = (char) (0x80 | (utf16 & 0x3f)); + s->str_start = s->cursor; + PHP_JSON_CONDITION_GOTO(STR_P2); + } + <STR_P2>UTF16_4 { + int utf32, utf16_hi, utf16_lo; + utf16_hi = php_json_ucs2_to_int(s, 4); + utf16_lo = php_json_ucs2_to_int_ex(s, 4, 7); + utf32 = ((utf16_lo & 0x3FF) << 10) + (utf16_hi & 0x3FF) + 0x10000; + PHP_JSON_SCANNER_COPY_UTF_SP(); + *(s->pstr++) = (char) (0xf0 | (utf32 >> 18)); + *(s->pstr++) = (char) (0x80 | ((utf32 >> 12) & 0x3f)); + *(s->pstr++) = (char) (0x80 | ((utf32 >> 6) & 0x3f)); + *(s->pstr++) = (char) (0x80 | (utf32 & 0x3f)); + s->str_start = s->cursor; + PHP_JSON_CONDITION_GOTO(STR_P2); + } + <STR_P2>ESCPREF { + char esc; + PHP_JSON_SCANNER_COPY_ESC(); + switch (*s->cursor) { + case 'b': + esc = '\b'; + break; + case 'f': + esc = '\f'; + break; + case 'n': + esc = '\n'; + break; + case 'r': + esc = '\r'; + break; + case 't': + esc = '\t'; + break; + case '\\': + case '/': + case '"': + esc = *s->cursor; + break; + default: + s->errcode = PHP_JSON_ERROR_SYNTAX; + return PHP_JSON_T_ERROR; + } + *(s->pstr++) = esc; + ++YYCURSOR; + s->str_start = s->cursor; + PHP_JSON_CONDITION_GOTO(STR_P2); + } + <STR_P2>["] => JS { + PHP_JSON_SCANNER_COPY_ESC(); + return PHP_JSON_T_STRING; + } + <STR_P2>ANY { PHP_JSON_CONDITION_GOTO(STR_P2); } + + <*>ANY { + s->errcode = PHP_JSON_ERROR_SYNTAX; + return PHP_JSON_T_ERROR; + } +*/ + +} + diff --cc ext/json/php_json.h index 2e0bc1b,62eca73..25c799b --- a/ext/json/php_json.h +++ b/ext/json/php_json.h @@@ -69,31 -73,13 +69,35 @@@ typedef enum #define PHP_JSON_OUTPUT_ARRAY 0 #define PHP_JSON_OUTPUT_OBJECT 1 +/* default depth */ +#define PHP_JSON_PARSER_DEFAULT_DEPTH 512 + +ZEND_BEGIN_MODULE_GLOBALS(json) + int encoder_depth; + int encode_max_depth; + php_json_error_code error_code; +ZEND_END_MODULE_GLOBALS(json) + ++ +#ifdef ZTS - # define JSON_G(v) TSRMG(json_globals_id, zend_json_globals *, v) ++# define JSON_G(v) ZEND_TSRMG(json_globals_id, zend_json_globals *, v) ++# ifdef COMPILE_DL_JSON ++ZEND_TSRMLS_CACHE_EXTERN; ++# endif +#else +# define JSON_G(v) (json_globals.v) +#endif + /* json_decode() options */ #define PHP_JSON_OBJECT_AS_ARRAY (1<<0) #define PHP_JSON_BIGINT_AS_STRING (1<<1) - PHP_JSON_API void php_json_encode(smart_str *buf, zval *val, int options TSRMLS_DC); - PHP_JSON_API void php_json_decode_ex(zval *return_value, char *str, size_t str_len, zend_long options, zend_long depth TSRMLS_DC); ++PHP_JSON_API void php_json_encode(smart_str *buf, zval *val, int options); ++PHP_JSON_API void php_json_decode_ex(zval *return_value, char *str, size_t str_len, zend_long options, zend_long depth); + - static inline void php_json_decode(zval *return_value, char *str, int str_len, zend_bool assoc, zend_long depth TSRMLS_DC) + static inline void php_json_decode(zval *return_value, char *str, int str_len, zend_bool assoc, zend_long depth) { - php_json_decode_ex(return_value, str, str_len, assoc ? PHP_JSON_OBJECT_AS_ARRAY : 0, depth TSRMLS_CC); + php_json_decode_ex(return_value, str, str_len, assoc ? PHP_JSON_OBJECT_AS_ARRAY : 0, depth); } diff --cc ext/json/php_json_encoder.h index 4f2b9cc,0000000..db81262 mode 100644,000000..100644 --- a/ext/json/php_json_encoder.h +++ b/ext/json/php_json_encoder.h @@@ -1,27 -1,0 +1,27 @@@ +/* + +----------------------------------------------------------------------+ + | PHP Version 5 | + +----------------------------------------------------------------------+ + | Copyright (c) 1997-2014 The PHP Group | + +----------------------------------------------------------------------+ + | This source file is subject to version 3.01 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available through the world-wide-web at the following url: | + | http://www.php.net/license/3_01.txt | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Author: Jakub Zelenka <bukka@php.net> | + +----------------------------------------------------------------------+ +*/ + +#ifndef PHP_JSON_ENCODER_H +#define PHP_JSON_ENCODER_H + +#include "php.h" +#include "zend_smart_str.h" + - void php_json_encode_zval(smart_str *buf, zval *val, int options TSRMLS_DC); ++void php_json_encode_zval(smart_str *buf, zval *val, int options); + +#endif /* PHP_JSON_ENCODER_H */ diff --cc ext/json/php_json_parser.h index 7912b79,0000000..08c3ee1 mode 100644,000000..100644 --- a/ext/json/php_json_parser.h +++ b/ext/json/php_json_parser.h @@@ -1,42 -1,0 +1,39 @@@ +/* + +----------------------------------------------------------------------+ + | PHP Version 5 | + +----------------------------------------------------------------------+ + | Copyright (c) 1997-2014 The PHP Group | + +----------------------------------------------------------------------+ + | This source file is subject to version 3.01 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available through the world-wide-web at the following url: | + | http://www.php.net/license/3_01.txt | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Author: Jakub Zelenka <bukka@php.net> | + +----------------------------------------------------------------------+ +*/ + +#ifndef PHP_JSON_PARSER_H +#define PHP_JSON_PARSER_H + +#include "php.h" +#include "php_json_scanner.h" + +typedef struct _php_json_parser { + php_json_scanner scanner; + zval *return_value; + long depth; + long max_depth; - #if ZTS - void *zts_ctx; - #endif +} php_json_parser; + - void php_json_parser_init(php_json_parser *parser, zval *return_value, char *str, int str_len, long options, long max_depth TSRMLS_DC); ++void php_json_parser_init(php_json_parser *parser, zval *return_value, char *str, int str_len, long options, long max_depth); + +php_json_error_code php_json_parser_error_code(php_json_parser *parser); + +int php_json_yyparse(php_json_parser *parser); + +#endif /* PHP_JSON_PARSER_H */ + diff --cc ext/json/php_json_scanner.h index 2034654,0000000..fbf65ea mode 100644,000000..100644 --- a/ext/json/php_json_scanner.h +++ b/ext/json/php_json_scanner.h @@@ -1,47 -1,0 +1,47 @@@ +/* + +----------------------------------------------------------------------+ + | PHP Version 5 | + +----------------------------------------------------------------------+ + | Copyright (c) 1997-2014 The PHP Group | + +----------------------------------------------------------------------+ + | This source file is subject to version 3.01 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available through the world-wide-web at the following url: | + | http://www.php.net/license/3_01.txt | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Author: Jakub Zelenka <bukka@php.net> | + +----------------------------------------------------------------------+ +*/ + +#ifndef PHP_JSON_SCANNER_H +#define PHP_JSON_SCANNER_H + +#include "php.h" +#include "php_json.h" + +typedef unsigned char php_json_ctype; + +typedef struct _php_json_scanner { + php_json_ctype *cursor; /* cursor position */ + php_json_ctype *token; /* token position */ + php_json_ctype *limit; /* the last read character + 1 position */ + php_json_ctype *marker; /* marker position for backtracking */ + php_json_ctype *ctxmarker; /* marker position for context backtracking */ + php_json_ctype *str_start; /* start position of the string */ + php_json_ctype *pstr; /* string pointer for escapes conversion */ + int str_esc; /* number of extra characters for escaping */ + int state; /* condition state */ + zval value; /* value */ + long options; /* options */ + php_json_error_code errcode; /* error type if there is an error */ +} php_json_scanner; + + +void php_json_scanner_init(php_json_scanner *scanner, char *str, int str_len, long options); - int php_json_scan(php_json_scanner *s TSRMLS_DC); ++int php_json_scan(php_json_scanner *s); + +#endif /* PHP_JSON_SCANNER_H */ +
diff --cc ext/json/config.m4 index 805330c,9c83a5d..fb87a93 --- a/ext/json/config.m4 +++ b/ext/json/config.m4 @@@ -9,13 -9,7 +9,13 @@@ if test "$PHP_JSON" != "no"; the AC_DEFINE([HAVE_JSON],1 ,[whether to enable JavaScript Object Serialization support]) AC_HEADER_STDC - PHP_NEW_EXTENSION(json, json.c utf8_decode.c JSON_parser.c, $ext_shared,, -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1) +PHP_NEW_EXTENSION(json, + json.c \ + json_encoder.c \ + json_parser.tab.c \ + json_scanner.c, - $ext_shared) ++ $ext_shared,, -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1) PHP_INSTALL_HEADERS([ext/json], [php_json.h]) + PHP_ADD_MAKEFILE_FRAGMENT() PHP_SUBST(JSON_SHARED_LIBADD) fi diff --cc ext/json/config.w32 index a6c54e6,60ccf91..996b868 --- a/ext/json/config.w32 +++ b/ext/json/config.w32 @@@ -4,13 -4,8 +4,8 @@@ ARG_ENABLE("json", "JavaScript Object Serialization support", "yes"); if (PHP_JSON != "no") { - EXTENSION('json', 'json.c', PHP_JSON_SHARED, ""); - PHP_NEW_EXTENSION(json, - json.c \ - json_encoder.c \ - json_parser.tab.c \ - json_scanner.c, - $ext_shared) + EXTENSION('json', 'json.c', PHP_JSON_SHARED, "/DZEND_ENABLE_STATIC_TSRMLS_CACHE=1"); - ADD_SOURCES(configure_module_dirname, "JSON_parser.c utf8_decode.c", "json"); ++ ADD_SOURCES(configure_module_dirname, "json_encoder.c json_parser.tab.c json_scanner.c", "json"); PHP_INSTALL_HEADERS("ext/json/", "php_json.h"); } diff --cc ext/json/json.c index f59dace,481ab75..819a6df --- a/ext/json/json.c +++ b/ext/json/json.c @@@ -166,23 -173,601 +172,22 @@@ static PHP_MINFO_FUNCTION(json } /* }}} */ -static void json_escape_string(smart_str *buf, char *s, size_t len, int options); -- - PHP_JSON_API void php_json_encode(smart_str *buf, zval *val, int options TSRMLS_DC) /* {{{ */ -static int json_determine_array_type(zval *val) /* {{{ */ -{ - int i; - HashTable *myht = HASH_OF(val); - - i = myht ? zend_hash_num_elements(myht) : 0; - if (i > 0) { - zend_string *key; - zend_ulong index, idx; - - idx = 0; - ZEND_HASH_FOREACH_KEY(myht, index, key) { - if (key) { - return PHP_JSON_OUTPUT_OBJECT; - } else { - if (index != idx) { - return PHP_JSON_OUTPUT_OBJECT; - } - } - idx++; - } ZEND_HASH_FOREACH_END(); - } - - return PHP_JSON_OUTPUT_ARRAY; -} -/* }}} */ - -/* {{{ Pretty printing support functions */ - -static inline void json_pretty_print_char(smart_str *buf, int options, char c) /* {{{ */ -{ - if (options & PHP_JSON_PRETTY_PRINT) { - smart_str_appendc(buf, c); - } -} -/* }}} */ - -static inline void json_pretty_print_indent(smart_str *buf, int options) /* {{{ */ -{ - int i; - - if (options & PHP_JSON_PRETTY_PRINT) { - for (i = 0; i < JSON_G(encoder_depth); ++i) { - smart_str_appendl(buf, " ", 4); - } - } -} -/* }}} */ - -/* }}} */ - -static void json_encode_array(smart_str *buf, zval *val, int options) /* {{{ */ -{ - int i, r, need_comma = 0; - HashTable *myht; - - if (Z_TYPE_P(val) == IS_ARRAY) { - myht = HASH_OF(val); - r = (options & PHP_JSON_FORCE_OBJECT) ? PHP_JSON_OUTPUT_OBJECT : json_determine_array_type(val); - } else { - myht = Z_OBJPROP_P(val); - r = PHP_JSON_OUTPUT_OBJECT; - } - - if (myht && ZEND_HASH_GET_APPLY_COUNT(myht) > 1) { - JSON_G(error_code) = PHP_JSON_ERROR_RECURSION; - smart_str_appendl(buf, "null", 4); - return; - } - - if (r == PHP_JSON_OUTPUT_ARRAY) { - smart_str_appendc(buf, '['); - } else { - smart_str_appendc(buf, '{'); - } - - ++JSON_G(encoder_depth); - - i = myht ? zend_hash_num_elements(myht) : 0; - - if (i > 0) { - zend_string *key; - zval *data; - zend_ulong index; - HashTable *tmp_ht; - - ZEND_HASH_FOREACH_KEY_VAL_IND(myht, index, key, data) { - ZVAL_DEREF(data); - tmp_ht = HASH_OF(data); - if (tmp_ht && ZEND_HASH_APPLY_PROTECTION(tmp_ht)) { - ZEND_HASH_INC_APPLY_COUNT(tmp_ht); - } - - if (r == PHP_JSON_OUTPUT_ARRAY) { - if (need_comma) { - smart_str_appendc(buf, ','); - } else { - need_comma = 1; - } - - json_pretty_print_char(buf, options, '\n'); - json_pretty_print_indent(buf, options); - php_json_encode(buf, data, options); - } else if (r == PHP_JSON_OUTPUT_OBJECT) { - if (key) { - if (key->val[0] == '\0' && Z_TYPE_P(val) == IS_OBJECT) { - /* Skip protected and private members. */ - if (tmp_ht && ZEND_HASH_APPLY_PROTECTION(tmp_ht)) { - ZEND_HASH_DEC_APPLY_COUNT(tmp_ht); - } - continue; - } - - if (need_comma) { - smart_str_appendc(buf, ','); - } else { - need_comma = 1; - } - - json_pretty_print_char(buf, options, '\n'); - json_pretty_print_indent(buf, options); - - json_escape_string(buf, key->val, key->len, options & ~PHP_JSON_NUMERIC_CHECK); - smart_str_appendc(buf, ':'); - - json_pretty_print_char(buf, options, ' '); - - php_json_encode(buf, data, options); - } else { - if (need_comma) { - smart_str_appendc(buf, ','); - } else { - need_comma = 1; - } - - json_pretty_print_char(buf, options, '\n'); - json_pretty_print_indent(buf, options); - - smart_str_appendc(buf, '"'); - smart_str_append_long(buf, (zend_long) index); - smart_str_appendc(buf, '"'); - smart_str_appendc(buf, ':'); - - json_pretty_print_char(buf, options, ' '); - - php_json_encode(buf, data, options); - } - } - - if (tmp_ht && ZEND_HASH_APPLY_PROTECTION(tmp_ht)) { - ZEND_HASH_DEC_APPLY_COUNT(tmp_ht); - } - } ZEND_HASH_FOREACH_END(); - } - - if (JSON_G(encoder_depth) > JSON_G(encode_max_depth)) { - JSON_G(error_code) = PHP_JSON_ERROR_DEPTH; - } - --JSON_G(encoder_depth); - - /* Only keep closing bracket on same line for empty arrays/objects */ - if (need_comma) { - json_pretty_print_char(buf, options, '\n'); - json_pretty_print_indent(buf, options); - } - - if (r == PHP_JSON_OUTPUT_ARRAY) { - smart_str_appendc(buf, ']'); - } else { - smart_str_appendc(buf, '}'); - } -} -/* }}} */ - -static int json_utf8_to_utf16(unsigned short *utf16, char utf8[], int len) /* {{{ */ -{ - size_t pos = 0, us; - int j, status; - - if (utf16) { - /* really convert the utf8 string */ - for (j=0 ; pos < len ; j++) { - us = php_next_utf8_char((const unsigned char *)utf8, len, &pos, &status); - if (status != SUCCESS) { - return -1; - } - /* From http://en.wikipedia.org/wiki/UTF16 */ - if (us >= 0x10000) { - us -= 0x10000; - utf16[j++] = (unsigned short)((us >> 10) | 0xd800); - utf16[j] = (unsigned short)((us & 0x3ff) | 0xdc00); - } else { - utf16[j] = (unsigned short)us; - } - } - } else { - /* Only check if utf8 string is valid, and compute utf16 length */ - for (j=0 ; pos < len ; j++) { - us = php_next_utf8_char((const unsigned char *)utf8, len, &pos, &status); - if (status != SUCCESS) { - return -1; - } - if (us >= 0x10000) { - j++; - } - } - } - return j; -} -/* }}} */ - -static void json_escape_string(smart_str *buf, char *s, size_t len, int options) /* {{{ */ -{ - int status; - unsigned int us, next_us = 0; - size_t pos, checkpoint; - - if (len == 0) { - smart_str_appendl(buf, "\"\"", 2); - return; - } - - if (options & PHP_JSON_NUMERIC_CHECK) { - double d; - int type; - zend_long p; - - if ((type = is_numeric_string(s, len, &p, &d, 0)) != 0) { - if (type == IS_LONG) { - smart_str_append_long(buf, p); - } else if (type == IS_DOUBLE) { - if (!zend_isinf(d) && !zend_isnan(d)) { - char *tmp; - int l = spprintf(&tmp, 0, "%.*k", (int) EG(precision), d); - smart_str_appendl(buf, tmp, l); - efree(tmp); - } else { - JSON_G(error_code) = PHP_JSON_ERROR_INF_OR_NAN; - smart_str_appendc(buf, '0'); - } - } - return; - } - - } - - if (options & PHP_JSON_UNESCAPED_UNICODE) { - /* validate UTF-8 string first */ - if (json_utf8_to_utf16(NULL, s, len) < 0) { - JSON_G(error_code) = PHP_JSON_ERROR_UTF8; - smart_str_appendl(buf, "null", 4); - return; - } - } - - pos = 0; - checkpoint = buf->s ? buf->s->len : 0; - - /* pre-allocate for string length plus 2 quotes */ - smart_str_alloc(buf, len+2, 0); - smart_str_appendc(buf, '"'); - - do { - if (UNEXPECTED(next_us)) { - us = next_us; - next_us = 0; - } else { - us = (unsigned char)s[pos]; - if (!(options & PHP_JSON_UNESCAPED_UNICODE) && us >= 0x80) { - /* UTF-8 character */ - us = php_next_utf8_char((const unsigned char *)s, len, &pos, &status); - if (status != SUCCESS) { - if (buf->s) { - buf->s->len = checkpoint; - } - JSON_G(error_code) = PHP_JSON_ERROR_UTF8; - smart_str_appendl(buf, "null", 4); - return; - } - /* From http://en.wikipedia.org/wiki/UTF16 */ - if (us >= 0x10000) { - us -= 0x10000; - next_us = (unsigned short)((us & 0x3ff) | 0xdc00); - us = (unsigned short)((us >> 10) | 0xd800); - } - } else { - pos++; - } - } - - switch (us) { - case '"': - if (options & PHP_JSON_HEX_QUOT) { - smart_str_appendl(buf, "\\u0022", 6); - } else { - smart_str_appendl(buf, "\\\"", 2); - } - break; - - case '\\': - smart_str_appendl(buf, "\\\\", 2); - break; - - case '/': - if (options & PHP_JSON_UNESCAPED_SLASHES) { - smart_str_appendc(buf, '/'); - } else { - smart_str_appendl(buf, "\\/", 2); - } - break; - - case '\b': - smart_str_appendl(buf, "\\b", 2); - break; - - case '\f': - smart_str_appendl(buf, "\\f", 2); - break; - - case '\n': - smart_str_appendl(buf, "\\n", 2); - break; - - case '\r': - smart_str_appendl(buf, "\\r", 2); - break; - - case '\t': - smart_str_appendl(buf, "\\t", 2); - break; - - case '<': - if (options & PHP_JSON_HEX_TAG) { - smart_str_appendl(buf, "\\u003C", 6); - } else { - smart_str_appendc(buf, '<'); - } - break; - - case '>': - if (options & PHP_JSON_HEX_TAG) { - smart_str_appendl(buf, "\\u003E", 6); - } else { - smart_str_appendc(buf, '>'); - } - break; - - case '&': - if (options & PHP_JSON_HEX_AMP) { - smart_str_appendl(buf, "\\u0026", 6); - } else { - smart_str_appendc(buf, '&'); - } - break; - - case '\'': - if (options & PHP_JSON_HEX_APOS) { - smart_str_appendl(buf, "\\u0027", 6); - } else { - smart_str_appendc(buf, '\''); - } - break; - - default: - if (us >= ' ' && ((options & PHP_JSON_UNESCAPED_UNICODE) || (us & 127) == us)) { - smart_str_appendc(buf, (unsigned char) us); - } else { - smart_str_appendl(buf, "\\u", 2); - smart_str_appendc(buf, digits[(us & 0xf000) >> 12]); - smart_str_appendc(buf, digits[(us & 0xf00) >> 8]); - smart_str_appendc(buf, digits[(us & 0xf0) >> 4]); - smart_str_appendc(buf, digits[(us & 0xf)]); - } - break; - } - } while (pos < len || next_us); - - smart_str_appendc(buf, '"'); -} -/* }}} */ - -static void json_encode_serializable_object(smart_str *buf, zval *val, int options) /* {{{ */ -{ - zend_class_entry *ce = Z_OBJCE_P(val); - zval retval, fname; - HashTable* myht; - - if (Z_TYPE_P(val) == IS_ARRAY) { - myht = HASH_OF(val); - } else { - myht = Z_OBJPROP_P(val); - } - - if (myht && ZEND_HASH_GET_APPLY_COUNT(myht) > 1) { - JSON_G(error_code) = PHP_JSON_ERROR_RECURSION; - smart_str_appendl(buf, "null", 4); - return; - } - - ZVAL_STRING(&fname, "jsonSerialize"); - - if (FAILURE == call_user_function_ex(EG(function_table), val, &fname, &retval, 0, NULL, 1, NULL) || Z_TYPE(retval) == IS_UNDEF) { - zend_throw_exception_ex(NULL, 0, "Failed calling %s::jsonSerialize()", ce->name->val); - smart_str_appendl(buf, "null", sizeof("null") - 1); - zval_ptr_dtor(&fname); - return; - } - - if (EG(exception)) { - /* Error already raised */ - zval_ptr_dtor(&retval); - zval_ptr_dtor(&fname); - smart_str_appendl(buf, "null", sizeof("null") - 1); - return; - } - - if ((Z_TYPE(retval) == IS_OBJECT) && - (Z_OBJ_HANDLE(retval) == Z_OBJ_HANDLE_P(val))) { - /* Handle the case where jsonSerialize does: return $this; by going straight to encode array */ - json_encode_array(buf, &retval, options); - } else { - /* All other types, encode as normal */ - php_json_encode(buf, &retval, options); - } - - zval_ptr_dtor(&retval); - zval_ptr_dtor(&fname); -} -/* }}} */ - + PHP_JSON_API void php_json_encode(smart_str *buf, zval *val, int options) /* {{{ */ { - php_json_encode_zval(buf, val, options TSRMLS_CC); -again: - switch (Z_TYPE_P(val)) - { - case IS_NULL: - smart_str_appendl(buf, "null", 4); - break; - - case IS_TRUE: - smart_str_appendl(buf, "true", 4); - break; - case IS_FALSE: - smart_str_appendl(buf, "false", 5); - break; - - case IS_LONG: - smart_str_append_long(buf, Z_LVAL_P(val)); - break; - - case IS_DOUBLE: - { - char *d = NULL; - int len; - double dbl = Z_DVAL_P(val); - - if (!zend_isinf(dbl) && !zend_isnan(dbl)) { - len = spprintf(&d, 0, "%.*k", (int) EG(precision), dbl); - smart_str_appendl(buf, d, len); - efree(d); - } else { - JSON_G(error_code) = PHP_JSON_ERROR_INF_OR_NAN; - smart_str_appendc(buf, '0'); - } - } - break; - - case IS_STRING: - json_escape_string(buf, Z_STRVAL_P(val), Z_STRLEN_P(val), options); - break; - - case IS_OBJECT: - if (instanceof_function(Z_OBJCE_P(val), php_json_serializable_ce)) { - json_encode_serializable_object(buf, val, options); - break; - } - /* fallthrough -- Non-serializable object */ - case IS_ARRAY: - json_encode_array(buf, val, options); - break; - - case IS_REFERENCE: - val = Z_REFVAL_P(val); - goto again; - - default: - JSON_G(error_code) = PHP_JSON_ERROR_UNSUPPORTED_TYPE; - smart_str_appendl(buf, "null", 4); - break; - } - - return; ++ php_json_encode_zval(buf, val, options); } /* }}} */ - PHP_JSON_API void php_json_decode_ex(zval *return_value, char *str, size_t str_len, zend_long options, zend_long depth TSRMLS_DC) /* {{{ */ + PHP_JSON_API void php_json_decode_ex(zval *return_value, char *str, size_t str_len, zend_long options, zend_long depth) /* {{{ */ { - size_t utf16_len; - unsigned short *utf16; - JSON_parser jp; - - utf16 = (unsigned short *) safe_emalloc((str_len+1), sizeof(unsigned short), 1); - - utf16_len = json_utf8_to_utf16(utf16, str, str_len); - if (utf16_len <= 0) { - if (utf16) { - efree(utf16); - } - JSON_G(error_code) = PHP_JSON_ERROR_UTF8; - RETURN_NULL(); - } + php_json_parser parser; - php_json_parser_init(&parser, return_value, str, str_len, options, depth TSRMLS_CC); - if (depth <= 0) { - php_error_docref(NULL, E_WARNING, "Depth must be greater than zero"); - efree(utf16); - RETURN_NULL(); - } ++ php_json_parser_init(&parser, return_value, str, str_len, options, depth); - jp = new_JSON_parser(depth); - if (!parse_JSON_ex(jp, return_value, utf16, utf16_len, options)) { - double d; - int type, overflow_info; - zend_long p; - char *trim = str; - int trim_len = str_len; - - zval_dtor(return_value); - - /* Increment trimmed string pointer to strip leading whitespace */ - /* JSON RFC says to consider as whitespace: space, tab, LF or CR */ - while (trim_len && (*trim == ' ' || *trim == '\t' || *trim == '\n' || *trim == '\r')) { - trim++; - trim_len--; - } - - /* Decrement trimmed string length to strip trailing whitespace */ - while (trim_len && (trim[trim_len - 1] == ' ' || trim[trim_len - 1] == '\t' || trim[trim_len - 1] == '\n' || trim[trim_len - 1] == '\r')) { - trim_len--; - } - - RETVAL_NULL(); - if (trim_len == 4) { - if (!strncmp(trim, "null", trim_len)) { - /* We need to explicitly clear the error because its an actual NULL and not an error */ - jp->error_code = PHP_JSON_ERROR_NONE; - RETVAL_NULL(); - } else if (!strncmp(trim, "true", trim_len)) { - RETVAL_BOOL(1); - } - } else if (trim_len == 5 && !strncmp(trim, "false", trim_len)) { - RETVAL_BOOL(0); - } - - if ((type = is_numeric_string_ex(trim, trim_len, &p, &d, 0, &overflow_info)) != 0) { - if (type == IS_LONG) { - RETVAL_LONG(p); - } else if (type == IS_DOUBLE) { - if (options & PHP_JSON_BIGINT_AS_STRING && overflow_info) { - /* Within an object or array, a numeric literal is assumed - * to be an integer if and only if it's entirely made up of - * digits (exponent notation will result in the number - * being treated as a double). We'll match that behaviour - * here. */ - int i; - zend_bool is_float = 0; - - for (i = (trim[0] == '-' ? 1 : 0); i < trim_len; i++) { - /* Not using isdigit() because it's locale specific, - * but we expect JSON input to always be UTF-8. */ - if (trim[i] < '0' || trim[i] > '9') { - is_float = 1; - break; - } - } - - if (is_float) { - RETVAL_DOUBLE(d); - } else { - RETVAL_STRINGL(trim, trim_len); - } - } else { - RETVAL_DOUBLE(d); - } - } - } - - if (Z_TYPE_P(return_value) != IS_NULL) { - jp->error_code = PHP_JSON_ERROR_NONE; - } + if (php_json_yyparse(&parser)) { + JSON_G(error_code) = php_json_parser_error_code(&parser); + RETURN_NULL(); } - efree(utf16); - JSON_G(error_code) = jp->error_code; - free_JSON_parser(jp); } /* }}} */ @@@ -193,9 -778,9 +198,9 @@@ static PHP_FUNCTION(json_encode zval *parameter; smart_str buf = {0}; zend_long options = 0; - zend_long depth = JSON_PARSER_DEFAULT_DEPTH; + zend_long depth = PHP_JSON_PARSER_DEFAULT_DEPTH; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|ll", ¶meter, &options, &depth) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "z|ll", ¶meter, &options, &depth) == FAILURE) { return; } @@@ -222,10 -807,10 +227,10 @@@ static PHP_FUNCTION(json_decode char *str; size_t str_len; zend_bool assoc = 0; /* return JS objects as PHP objects by default */ - zend_long depth = JSON_PARSER_DEFAULT_DEPTH; + zend_long depth = PHP_JSON_PARSER_DEFAULT_DEPTH; zend_long options = 0; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|bll", &str, &str_len, &assoc, &depth, &options) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|bll", &str, &str_len, &assoc, &depth, &options) == FAILURE) { return; } diff --cc ext/json/json_encoder.c index 6976d90,0000000..b9fb355 mode 100644,000000..100644 --- a/ext/json/json_encoder.c +++ b/ext/json/json_encoder.c @@@ -1,541 -1,0 +1,541 @@@ +/* + +----------------------------------------------------------------------+ + | PHP Version 7 | + +----------------------------------------------------------------------+ + | Copyright (c) 1997-2014 The PHP Group | + +----------------------------------------------------------------------+ + | This source file is subject to version 3.01 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available through the world-wide-web at the following url: | + | http://www.php.net/license/3_01.txt | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Author: Omar Kilani <omar@php.net> | + +----------------------------------------------------------------------+ +*/ + +/* $Id$ */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "php.h" +#include "php_ini.h" +#include "ext/standard/info.h" +#include "ext/standard/html.h" +#include "zend_smart_str.h" +#include "php_json.h" +#include <zend_exceptions.h> + +ZEND_DECLARE_MODULE_GLOBALS(json) + +static const char digits[] = "0123456789abcdef"; + - static void json_escape_string(smart_str *buf, char *s, size_t len, int options TSRMLS_DC); ++static void json_escape_string(smart_str *buf, char *s, size_t len, int options); + - static int json_determine_array_type(zval *val TSRMLS_DC) /* {{{ */ ++static int json_determine_array_type(zval *val) /* {{{ */ +{ + int i; + HashTable *myht = HASH_OF(val); + + i = myht ? zend_hash_num_elements(myht) : 0; + if (i > 0) { + zend_string *key; + zend_ulong index, idx; + + idx = 0; + ZEND_HASH_FOREACH_KEY(myht, index, key) { + if (key) { + return PHP_JSON_OUTPUT_OBJECT; + } else { + if (index != idx) { + return PHP_JSON_OUTPUT_OBJECT; + } + } + idx++; + } ZEND_HASH_FOREACH_END(); + } + + return PHP_JSON_OUTPUT_ARRAY; +} +/* }}} */ + +/* {{{ Pretty printing support functions */ + - static inline void json_pretty_print_char(smart_str *buf, int options, char c TSRMLS_DC) /* {{{ */ ++static inline void json_pretty_print_char(smart_str *buf, int options, char c) /* {{{ */ +{ + if (options & PHP_JSON_PRETTY_PRINT) { + smart_str_appendc(buf, c); + } +} +/* }}} */ + - static inline void json_pretty_print_indent(smart_str *buf, int options TSRMLS_DC) /* {{{ */ ++static inline void json_pretty_print_indent(smart_str *buf, int options) /* {{{ */ +{ + int i; + + if (options & PHP_JSON_PRETTY_PRINT) { + for (i = 0; i < JSON_G(encoder_depth); ++i) { + smart_str_appendl(buf, " ", 4); + } + } +} +/* }}} */ + +/* }}} */ + - static void json_encode_array(smart_str *buf, zval *val, int options TSRMLS_DC) /* {{{ */ ++static void json_encode_array(smart_str *buf, zval *val, int options) /* {{{ */ +{ + int i, r, need_comma = 0; + HashTable *myht; + + if (Z_TYPE_P(val) == IS_ARRAY) { + myht = HASH_OF(val); - r = (options & PHP_JSON_FORCE_OBJECT) ? PHP_JSON_OUTPUT_OBJECT : json_determine_array_type(val TSRMLS_CC); ++ r = (options & PHP_JSON_FORCE_OBJECT) ? PHP_JSON_OUTPUT_OBJECT : json_determine_array_type(val); + } else { + myht = Z_OBJPROP_P(val); + r = PHP_JSON_OUTPUT_OBJECT; + } + + if (myht && ZEND_HASH_GET_APPLY_COUNT(myht) > 1) { + JSON_G(error_code) = PHP_JSON_ERROR_RECURSION; + smart_str_appendl(buf, "null", 4); + return; + } + + if (r == PHP_JSON_OUTPUT_ARRAY) { + smart_str_appendc(buf, '['); + } else { + smart_str_appendc(buf, '{'); + } + + ++JSON_G(encoder_depth); + + i = myht ? zend_hash_num_elements(myht) : 0; + + if (i > 0) { + zend_string *key; + zval *data; + zend_ulong index; + HashTable *tmp_ht; + + ZEND_HASH_FOREACH_KEY_VAL_IND(myht, index, key, data) { + ZVAL_DEREF(data); + tmp_ht = HASH_OF(data); + if (tmp_ht && ZEND_HASH_APPLY_PROTECTION(tmp_ht)) { + ZEND_HASH_INC_APPLY_COUNT(tmp_ht); + } + + if (r == PHP_JSON_OUTPUT_ARRAY) { + if (need_comma) { + smart_str_appendc(buf, ','); + } else { + need_comma = 1; + } + - json_pretty_print_char(buf, options, '\n' TSRMLS_CC); - json_pretty_print_indent(buf, options TSRMLS_CC); - php_json_encode(buf, data, options TSRMLS_CC); ++ json_pretty_print_char(buf, options, '\n'); ++ json_pretty_print_indent(buf, options); ++ php_json_encode(buf, data, options); + } else if (r == PHP_JSON_OUTPUT_OBJECT) { + if (key) { + if (key->val[0] == '\0' && Z_TYPE_P(val) == IS_OBJECT) { + /* Skip protected and private members. */ + if (tmp_ht && ZEND_HASH_APPLY_PROTECTION(tmp_ht)) { + ZEND_HASH_DEC_APPLY_COUNT(tmp_ht); + } + continue; + } + + if (need_comma) { + smart_str_appendc(buf, ','); + } else { + need_comma = 1; + } + - json_pretty_print_char(buf, options, '\n' TSRMLS_CC); - json_pretty_print_indent(buf, options TSRMLS_CC); ++ json_pretty_print_char(buf, options, '\n'); ++ json_pretty_print_indent(buf, options); + - json_escape_string(buf, key->val, key->len, options & ~PHP_JSON_NUMERIC_CHECK TSRMLS_CC); ++ json_escape_string(buf, key->val, key->len, options & ~PHP_JSON_NUMERIC_CHECK); + smart_str_appendc(buf, ':'); + - json_pretty_print_char(buf, options, ' ' TSRMLS_CC); ++ json_pretty_print_char(buf, options, ' '); + - php_json_encode(buf, data, options TSRMLS_CC); ++ php_json_encode(buf, data, options); + } else { + if (need_comma) { + smart_str_appendc(buf, ','); + } else { + need_comma = 1; + } + - json_pretty_print_char(buf, options, '\n' TSRMLS_CC); - json_pretty_print_indent(buf, options TSRMLS_CC); ++ json_pretty_print_char(buf, options, '\n'); ++ json_pretty_print_indent(buf, options); + + smart_str_appendc(buf, '"'); + smart_str_append_long(buf, (zend_long) index); + smart_str_appendc(buf, '"'); + smart_str_appendc(buf, ':'); + - json_pretty_print_char(buf, options, ' ' TSRMLS_CC); ++ json_pretty_print_char(buf, options, ' '); + - php_json_encode(buf, data, options TSRMLS_CC); ++ php_json_encode(buf, data, options); + } + } + + if (tmp_ht && ZEND_HASH_APPLY_PROTECTION(tmp_ht)) { + ZEND_HASH_DEC_APPLY_COUNT(tmp_ht); + } + } ZEND_HASH_FOREACH_END(); + } + + if (JSON_G(encoder_depth) > JSON_G(encode_max_depth)) { + JSON_G(error_code) = PHP_JSON_ERROR_DEPTH; + } + --JSON_G(encoder_depth); + + /* Only keep closing bracket on same line for empty arrays/objects */ + if (need_comma) { - json_pretty_print_char(buf, options, '\n' TSRMLS_CC); - json_pretty_print_indent(buf, options TSRMLS_CC); ++ json_pretty_print_char(buf, options, '\n'); ++ json_pretty_print_indent(buf, options); + } + + if (r == PHP_JSON_OUTPUT_ARRAY) { + smart_str_appendc(buf, ']'); + } else { + smart_str_appendc(buf, '}'); + } +} +/* }}} */ + +static int json_utf8_to_utf16(unsigned short *utf16, char utf8[], int len) /* {{{ */ +{ + size_t pos = 0, us; + int j, status; + + if (utf16) { + /* really convert the utf8 string */ + for (j=0 ; pos < len ; j++) { + us = php_next_utf8_char((const unsigned char *)utf8, len, &pos, &status); + if (status != SUCCESS) { + return -1; + } + /* From http://en.wikipedia.org/wiki/UTF16 */ + if (us >= 0x10000) { + us -= 0x10000; + utf16[j++] = (unsigned short)((us >> 10) | 0xd800); + utf16[j] = (unsigned short)((us & 0x3ff) | 0xdc00); + } else { + utf16[j] = (unsigned short)us; + } + } + } else { + /* Only check if utf8 string is valid, and compute utf16 length */ + for (j=0 ; pos < len ; j++) { + us = php_next_utf8_char((const unsigned char *)utf8, len, &pos, &status); + if (status != SUCCESS) { + return -1; + } + if (us >= 0x10000) { + j++; + } + } + } + return j; +} +/* }}} */ + - static void json_escape_string(smart_str *buf, char *s, size_t len, int options TSRMLS_DC) /* {{{ */ ++static void json_escape_string(smart_str *buf, char *s, size_t len, int options) /* {{{ */ +{ + int status; + unsigned int us, next_us = 0; + size_t pos, checkpoint; + + if (len == 0) { + smart_str_appendl(buf, "\"\"", 2); + return; + } + + if (options & PHP_JSON_NUMERIC_CHECK) { + double d; + int type; + zend_long p; + + if ((type = is_numeric_string(s, len, &p, &d, 0)) != 0) { + if (type == IS_LONG) { + smart_str_append_long(buf, p); + } else if (type == IS_DOUBLE) { + if (!zend_isinf(d) && !zend_isnan(d)) { + char *tmp; + int l = spprintf(&tmp, 0, "%.*k", (int) EG(precision), d); + smart_str_appendl(buf, tmp, l); + efree(tmp); + } else { + JSON_G(error_code) = PHP_JSON_ERROR_INF_OR_NAN; + smart_str_appendc(buf, '0'); + } + } + return; + } + + } + + if (options & PHP_JSON_UNESCAPED_UNICODE) { + /* validate UTF-8 string first */ + if (json_utf8_to_utf16(NULL, s, len) < 0) { + JSON_G(error_code) = PHP_JSON_ERROR_UTF8; + smart_str_appendl(buf, "null", 4); - return; ++ return; + } + } + + pos = 0; + checkpoint = buf->s ? buf->s->len : 0; + + /* pre-allocate for string length plus 2 quotes */ + smart_str_alloc(buf, len+2, 0); + smart_str_appendc(buf, '"'); + + do { + if (UNEXPECTED(next_us)) { + us = next_us; + next_us = 0; + } else { + us = (unsigned char)s[pos]; + if (!(options & PHP_JSON_UNESCAPED_UNICODE) && us >= 0x80) { + /* UTF-8 character */ + us = php_next_utf8_char((const unsigned char *)s, len, &pos, &status); + if (status != SUCCESS) { + if (buf->s) { + buf->s->len = checkpoint; + } + JSON_G(error_code) = PHP_JSON_ERROR_UTF8; + smart_str_appendl(buf, "null", 4); + return; + } + /* From http://en.wikipedia.org/wiki/UTF16 */ + if (us >= 0x10000) { + us -= 0x10000; + next_us = (unsigned short)((us & 0x3ff) | 0xdc00); + us = (unsigned short)((us >> 10) | 0xd800); + } + } else { + pos++; + } + } + + switch (us) { + case '"': + if (options & PHP_JSON_HEX_QUOT) { + smart_str_appendl(buf, "\\u0022", 6); + } else { + smart_str_appendl(buf, "\\\"", 2); + } + break; + + case '\\': + smart_str_appendl(buf, "\\\\", 2); + break; + + case '/': + if (options & PHP_JSON_UNESCAPED_SLASHES) { + smart_str_appendc(buf, '/'); + } else { + smart_str_appendl(buf, "\\/", 2); + } + break; + + case '\b': + smart_str_appendl(buf, "\\b", 2); + break; + + case '\f': + smart_str_appendl(buf, "\\f", 2); + break; + + case '\n': + smart_str_appendl(buf, "\\n", 2); + break; + + case '\r': + smart_str_appendl(buf, "\\r", 2); + break; + + case '\t': + smart_str_appendl(buf, "\\t", 2); + break; + + case '<': + if (options & PHP_JSON_HEX_TAG) { + smart_str_appendl(buf, "\\u003C", 6); + } else { + smart_str_appendc(buf, '<'); + } + break; + + case '>': + if (options & PHP_JSON_HEX_TAG) { + smart_str_appendl(buf, "\\u003E", 6); + } else { + smart_str_appendc(buf, '>'); + } + break; + + case '&': + if (options & PHP_JSON_HEX_AMP) { + smart_str_appendl(buf, "\\u0026", 6); + } else { + smart_str_appendc(buf, '&'); + } + break; + + case '\'': + if (options & PHP_JSON_HEX_APOS) { + smart_str_appendl(buf, "\\u0027", 6); + } else { + smart_str_appendc(buf, '\''); + } + break; + + default: + if (us >= ' ' && ((options & PHP_JSON_UNESCAPED_UNICODE) || (us & 127) == us)) { + smart_str_appendc(buf, (unsigned char) us); + } else { + smart_str_appendl(buf, "\\u", 2); + smart_str_appendc(buf, digits[(us & 0xf000) >> 12]); + smart_str_appendc(buf, digits[(us & 0xf00) >> 8]); + smart_str_appendc(buf, digits[(us & 0xf0) >> 4]); + smart_str_appendc(buf, digits[(us & 0xf)]); + } + break; + } + } while (pos < len || next_us); - ++ + smart_str_appendc(buf, '"'); +} +/* }}} */ + - static void json_encode_serializable_object(smart_str *buf, zval *val, int options TSRMLS_DC) /* {{{ */ ++static void json_encode_serializable_object(smart_str *buf, zval *val, int options) /* {{{ */ +{ + zend_class_entry *ce = Z_OBJCE_P(val); + zval retval, fname; + HashTable* myht; + + if (Z_TYPE_P(val) == IS_ARRAY) { + myht = HASH_OF(val); + } else { + myht = Z_OBJPROP_P(val); + } + + if (myht && ZEND_HASH_GET_APPLY_COUNT(myht) > 1) { + JSON_G(error_code) = PHP_JSON_ERROR_RECURSION; + smart_str_appendl(buf, "null", 4); + return; + } + + ZVAL_STRING(&fname, "jsonSerialize"); + - if (FAILURE == call_user_function_ex(EG(function_table), val, &fname, &retval, 0, NULL, 1, NULL TSRMLS_CC) || Z_TYPE(retval) == IS_UNDEF) { - zend_throw_exception_ex(NULL, 0 TSRMLS_CC, "Failed calling %s::jsonSerialize()", ce->name->val); ++ if (FAILURE == call_user_function_ex(EG(function_table), val, &fname, &retval, 0, NULL, 1, NULL) || Z_TYPE(retval) == IS_UNDEF) { ++ zend_throw_exception_ex(NULL, 0, "Failed calling %s::jsonSerialize()", ce->name->val); + smart_str_appendl(buf, "null", sizeof("null") - 1); + zval_ptr_dtor(&fname); + return; - } ++ } + + if (EG(exception)) { + /* Error already raised */ + zval_ptr_dtor(&retval); + zval_ptr_dtor(&fname); + smart_str_appendl(buf, "null", sizeof("null") - 1); + return; + } + + if ((Z_TYPE(retval) == IS_OBJECT) && + (Z_OBJ_HANDLE(retval) == Z_OBJ_HANDLE_P(val))) { + /* Handle the case where jsonSerialize does: return $this; by going straight to encode array */ - json_encode_array(buf, &retval, options TSRMLS_CC); ++ json_encode_array(buf, &retval, options); + } else { + /* All other types, encode as normal */ - php_json_encode(buf, &retval, options TSRMLS_CC); ++ php_json_encode(buf, &retval, options); + } + + zval_ptr_dtor(&retval); + zval_ptr_dtor(&fname); +} +/* }}} */ + - void php_json_encode_zval(smart_str *buf, zval *val, int options TSRMLS_DC) /* {{{ */ ++void php_json_encode_zval(smart_str *buf, zval *val, int options) /* {{{ */ +{ +again: + switch (Z_TYPE_P(val)) + { + case IS_NULL: + smart_str_appendl(buf, "null", 4); + break; + + case IS_TRUE: + smart_str_appendl(buf, "true", 4); + break; + case IS_FALSE: + smart_str_appendl(buf, "false", 5); + break; + + case IS_LONG: + smart_str_append_long(buf, Z_LVAL_P(val)); + break; + + case IS_DOUBLE: + { + char *d = NULL; + int len; + double dbl = Z_DVAL_P(val); + + if (!zend_isinf(dbl) && !zend_isnan(dbl)) { + len = spprintf(&d, 0, "%.*k", (int) EG(precision), dbl); + smart_str_appendl(buf, d, len); + efree(d); + } else { + JSON_G(error_code) = PHP_JSON_ERROR_INF_OR_NAN; + smart_str_appendc(buf, '0'); + } + } + break; + + case IS_STRING: - json_escape_string(buf, Z_STRVAL_P(val), Z_STRLEN_P(val), options TSRMLS_CC); ++ json_escape_string(buf, Z_STRVAL_P(val), Z_STRLEN_P(val), options); + break; + + case IS_OBJECT: - if (instanceof_function(Z_OBJCE_P(val), php_json_serializable_ce TSRMLS_CC)) { - json_encode_serializable_object(buf, val, options TSRMLS_CC); ++ if (instanceof_function(Z_OBJCE_P(val), php_json_serializable_ce)) { ++ json_encode_serializable_object(buf, val, options); + break; + } + /* fallthrough -- Non-serializable object */ + case IS_ARRAY: - json_encode_array(buf, val, options TSRMLS_CC); ++ json_encode_array(buf, val, options); + break; + + case IS_REFERENCE: + val = Z_REFVAL_P(val); + goto again; + + default: + JSON_G(error_code) = PHP_JSON_ERROR_UNSUPPORTED_TYPE; + smart_str_appendl(buf, "null", 4); + break; + } + + return; +} +/* }}} */ + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + * vim600: noet sw=4 ts=4 fdm=marker + * vim<600: noet sw=4 ts=4 + */ diff --cc ext/json/json_parser.y index dbacc3d,0000000..4af46ec mode 100644,000000..100644 --- a/ext/json/json_parser.y +++ b/ext/json/json_parser.y @@@ -1,231 -1,0 +1,227 @@@ +%code top { +/* + +----------------------------------------------------------------------+ + | PHP Version 5 | + +----------------------------------------------------------------------+ + | Copyright (c) 1997-2014 The PHP Group | + +----------------------------------------------------------------------+ + | This source file is subject to version 3.01 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available through the world-wide-web at the following url: | + | http://www.php.net/license/3_01.txt | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Author: Jakub Zelenka <bukka@php.net> | + +----------------------------------------------------------------------+ +*/ + +#include "php.h" +#include "php_json.h" +#include "php_json_parser.h" + +#define YYDEBUG 0 + +#if YYDEBUG +int json_yydebug = 1; +#endif + +#define PHP_JSON_USE(uv) ((void) (uv)) +#define PHP_JSON_USE_1(uvr, uv1) PHP_JSON_USE(uvr); PHP_JSON_USE(uv1) +#define PHP_JSON_USE_2(uvr, uv1, uv2) PHP_JSON_USE(uvr); PHP_JSON_USE(uv1); PHP_JSON_USE(uv2) + +} + +%pure-parser +%name-prefix "php_json_yy" +%lex-param { php_json_parser *parser } +%parse-param { php_json_parser *parser } + +%union { + zval value; + struct { + zval key; + zval val; + } pair; +} + + +%token <value> PHP_JSON_T_NUL +%token <value> PHP_JSON_T_TRUE +%token <value> PHP_JSON_T_FALSE +%token <value> PHP_JSON_T_INT +%token <value> PHP_JSON_T_DOUBLE +%token <value> PHP_JSON_T_STRING +%token <value> PHP_JSON_T_ESTRING +%token <value> PHP_JSON_T_EOI +%token <value> PHP_JSON_T_ERROR + +%type <value> start object key value array errlex +%type <value> members member elements element +%type <pair> pair + +%destructor { zval_dtor(&$$); } <value> +%destructor { zend_hash_destroy($$); FREE_HASHTABLE($$); } <ht> +%destructor { zval_dtor(&$$.key); zval_dtor(&$$.val); } <pair> + +%code { +int php_json_yylex(union YYSTYPE *value, php_json_parser *parser); +void php_json_yyerror(php_json_parser *parser, char const *msg); +void php_json_parser_object_init(php_json_parser *parser, zval *object); +void php_json_parser_object_update(php_json_parser *parser, zval *object, zval *zkey, zval *zvalue); +void php_json_parser_array_init(zval *object); +void php_json_parser_array_append(zval *array, zval *zvalue); + +#define PHP_JSON_DEPTH_DEC --parser->depth +#define PHP_JSON_DEPTH_INC \ + if (parser->max_depth && parser->depth >= parser->max_depth) { \ + parser->scanner.errcode = PHP_JSON_ERROR_DEPTH; \ + YYERROR; \ + } \ + ++parser->depth +} + +%% /* Rules */ + +start: + value PHP_JSON_T_EOI { $$ = $1; *parser->return_value = $1; PHP_JSON_USE($2); YYACCEPT; } + | value errlex { PHP_JSON_USE_2($$, $1, $2); } +; + +object: + '{' { PHP_JSON_DEPTH_INC; } members object_end { PHP_JSON_DEPTH_DEC; $$ = $3; } +; + +object_end: + '}' + | ']' { parser->scanner.errcode = PHP_JSON_ERROR_STATE_MISMATCH; YYERROR; } +; + +members: + /* empty */ { php_json_parser_object_init(parser, &$$); } + | member +; + +member: + pair { php_json_parser_object_init(parser, &$$); php_json_parser_object_update(parser, &$$, &$1.key, &$1.val); } + | member ',' pair { php_json_parser_object_update(parser, &$1, &$3.key, &$3.val); $$ = $1; } + | member errlex { PHP_JSON_USE_2($$, $1, $2); } +; + +pair: + key ':' value { $$.key = $1; $$.val = $3; } + | key errlex { PHP_JSON_USE_2($$, $1, $2); } +; + +array: + '[' { PHP_JSON_DEPTH_INC; } elements array_end { PHP_JSON_DEPTH_DEC; $$ = $3; } +; + +array_end: + ']' + | '}' { parser->scanner.errcode = PHP_JSON_ERROR_STATE_MISMATCH; YYERROR; } +; + +elements: + /* empty */ { php_json_parser_array_init(&$$); } + | element +; + +element: + value { php_json_parser_array_init(&$$); php_json_parser_array_append(&$$, &$1); } + | element ',' value { php_json_parser_array_append(&$1, &$3); $$ = $1; } + | element errlex { PHP_JSON_USE_2($$, $1, $2); } +; + +key: + PHP_JSON_T_STRING + | PHP_JSON_T_ESTRING +; + +value: + object + | array + | PHP_JSON_T_STRING + | PHP_JSON_T_ESTRING + | PHP_JSON_T_INT + | PHP_JSON_T_DOUBLE + | PHP_JSON_T_NUL + | PHP_JSON_T_TRUE + | PHP_JSON_T_FALSE + | errlex +; + +errlex: + PHP_JSON_T_ERROR { PHP_JSON_USE_1($$, $1); YYERROR; } +; + +%% /* Functions */ + - void php_json_parser_init(php_json_parser *parser, zval *return_value, char *str, int str_len, long options, long max_depth TSRMLS_DC) ++void php_json_parser_init(php_json_parser *parser, zval *return_value, char *str, int str_len, long options, long max_depth) +{ + memset(parser, 0, sizeof(php_json_parser)); + php_json_scanner_init(&parser->scanner, str, str_len, options); + parser->depth = 1; + parser->max_depth = max_depth; + parser->return_value = return_value; - TSRMLS_SET_CTX(parser->zts_ctx); +} + +php_json_error_code php_json_parser_error_code(php_json_parser *parser) +{ + return parser->scanner.errcode; +} + +void php_json_parser_object_init(php_json_parser *parser, zval *object) +{ - TSRMLS_FETCH_FROM_CTX(parser->zts_ctx); + if (parser->scanner.options & PHP_JSON_OBJECT_AS_ARRAY) { + array_init(object); + } else { + object_init(object); + } +} + +void php_json_parser_object_update(php_json_parser *parser, zval *object, zval *zkey, zval *zvalue) +{ + char *key = Z_STRVAL_P(zkey); + int key_len = Z_STRLEN_P(zkey); - TSRMLS_FETCH_FROM_CTX(parser->zts_ctx); + + if (parser->scanner.options & PHP_JSON_OBJECT_AS_ARRAY) { + add_assoc_zval_ex(object, key, key_len, zvalue); + } else { + if (key_len == 0) { + key = "_empty_"; + key_len = sizeof("_empty_") - 1; + } + add_property_zval_ex(object, key, key_len, zvalue TSRMLS_CC); + + if (Z_REFCOUNTED_P(zvalue)) { + Z_DELREF_P(zvalue); + } + } + zval_dtor(zkey); +} + +void php_json_parser_array_init(zval *array) +{ + array_init(array); +} + +void php_json_parser_array_append(zval *array, zval *zvalue) +{ + add_next_index_zval(array, zvalue); +} + +int php_json_yylex(union YYSTYPE *value, php_json_parser *parser) +{ - TSRMLS_FETCH_FROM_CTX(parser->zts_ctx); - int token = php_json_scan(&parser->scanner TSRMLS_CC); ++ int token = php_json_scan(&parser->scanner); + value->value = parser->scanner.value; + return token; +} + +void php_json_yyerror(php_json_parser *parser, char const *msg) +{ + if (!parser->scanner.errcode) { + parser->scanner.errcode = PHP_JSON_ERROR_SYNTAX; + } +} diff --cc ext/json/json_scanner.re index 79df124,0000000..0592da8 mode 100644,000000..100644 --- a/ext/json/json_scanner.re +++ b/ext/json/json_scanner.re @@@ -1,347 -1,0 +1,347 @@@ +/* + +----------------------------------------------------------------------+ + | PHP Version 5 | + +----------------------------------------------------------------------+ + | Copyright (c) 1997-2014 The PHP Group | + +----------------------------------------------------------------------+ + | This source file is subject to version 3.01 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available through the world-wide-web at the following url: | + | http://www.php.net/license/3_01.txt | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Author: Jakub Zelenka <bukka@php.net> | + +----------------------------------------------------------------------+ +*/ + +#include "php.h" +#include "php_json_scanner.h" +#include "php_json_scanner_defs.h" +#include "php_json_parser.h" +#include "json_parser.tab.h" + +#define YYCTYPE php_json_ctype +#define YYCURSOR s->cursor +#define YYLIMIT s->limit +#define YYMARKER s->marker +#define YYCTXMARKER s->ctxmarker + +#define YYGETCONDITION() s->state +#define YYSETCONDITION(yystate) s->state = yystate + +#define YYFILL(n) + +#define PHP_JSON_CONDITION_SET(condition) YYSETCONDITION(yyc##condition) +#define PHP_JSON_CONDITION_GOTO(condition) goto yyc_##condition + +#define PHP_JSON_SCANNER_COPY_ESC() php_json_scanner_copy_string(s, 0) +#define PHP_JSON_SCANNER_COPY_UTF() php_json_scanner_copy_string(s, 5) +#define PHP_JSON_SCANNER_COPY_UTF_SP() php_json_scanner_copy_string(s, 11) + +#define PHP_JSON_INT_MAX_LENGTH (MAX_LENGTH_OF_LONG - 1) + + +static void php_json_scanner_copy_string(php_json_scanner *s, int esc_size) +{ + size_t len = s->cursor - s->str_start - esc_size - 1; + if (len) { + memcpy(s->pstr, s->str_start, len); + s->pstr += len; + } +} + +static int php_json_hex_to_int(char code) +{ + if (code >= '0' && code <= '9') { + return code - '0'; + } else if (code >= 'A' && code <= 'F') { + return code - ('A' - 10); + } else if (code >= 'a' && code <= 'f') { + return code - ('a' - 10); + } else { + /* this should never happened (just to suppress compiler warning) */ + return -1; + } +} + +static int php_json_ucs2_to_int_ex(php_json_scanner *s, int size, int start) +{ + int i, code = 0; + php_json_ctype *pc = s->cursor - start; + for (i = 0; i < size; i++) { + code |= php_json_hex_to_int(*(pc--)) << (i * 4); + } + return code; +} + +static int php_json_ucs2_to_int(php_json_scanner *s, int size) +{ + return php_json_ucs2_to_int_ex(s, size, 1); +} + +void php_json_scanner_init(php_json_scanner *s, char *str, int str_len, long options) +{ + s->cursor = (php_json_ctype *) str; + s->limit = (php_json_ctype *) str + str_len; + s->options = options; + PHP_JSON_CONDITION_SET(JS); +} + - int php_json_scan(php_json_scanner *s TSRMLS_DC) ++int php_json_scan(php_json_scanner *s) +{ + ZVAL_NULL(&s->value); + +std: + s->token = s->cursor; + +/*!re2c + re2c:indent:top = 1; + re2c:yyfill:enable = 0; + + DIGIT = [0-9] ; + DIGITNZ = [1-9] ; + UINT = "0" | ( DIGITNZ DIGIT* ) ; + INT = "-"? UINT ; + HEX = DIGIT | [a-fA-F] ; + HEXNZ = DIGITNZ | [a-fA-F] ; + HEX7 = [0-7] ; + HEXC = DIGIT | [a-cA-C] ; + FLOAT = INT "." DIGIT+ ; + EXP = ( INT | FLOAT ) [eE] [+-]? DIGIT+ ; + NL = "\r"? "\n" ; + WS = [ \t\r]+ ; + EOI = "\000"; + CTRL = [\x00-\x1F] ; + UTF8T = [\x80-\xBF] ; + UTF8_1 = [\x00-\x7F] ; + UTF8_2 = [\xC2-\xDF] UTF8T ; + UTF8_3A = "\xE0" [\xA0-\xBF] UTF8T ; + UTF8_3B = [\xE1-\xEC] UTF8T{2} ; + UTF8_3C = "\xED" [\x80-\x9F] UTF8T ; + UTF8_3D = [\xEE-\xEF] UTF8T{2} ; + UTF8_3 = UTF8_3A | UTF8_3B | UTF8_3C | UTF8_3D ; + UTF8_4A = "\xF0"[\x90-\xBF] UTF8T{2} ; + UTF8_4B = [\xF1-\xF3] UTF8T{3} ; + UTF8_4C = "\xF4" [\x80-\x8F] UTF8T{2} ; + UTF8_4 = UTF8_4A | UTF8_4B | UTF8_4C ; + UTF8 = UTF8_1 | UTF8_2 | UTF8_3 | UTF8_4 ; + ANY = [^] ; + ESCPREF = "\\" ; + ESCSYM = ( "\"" | "\\" | "/" | [bfnrt] ) ; + ESC = ESCPREF ESCSYM ; + UTFSYM = "u" ; + UTFPREF = ESCPREF UTFSYM ; + UCS2 = UTFPREF HEX{4} ; + UTF16_1 = UTFPREF "00" HEX7 HEX ; + UTF16_2 = UTFPREF "0" HEX7 HEX{2} ; + UTF16_3 = UTFPREF ( ( ( HEXC | [efEF] ) HEX ) | ( [dD] HEX7 ) ) HEX{2} ; + UTF16_4 = UTFPREF [dD] [89abAB] HEX{2} UTFPREF [dD] [c-fC-F] HEX{2} ; + + <JS>"{" { return '{'; } + <JS>"}" { return '}'; } + <JS>"[" { return '['; } + <JS>"]" { return ']'; } + <JS>":" { return ':'; } + <JS>"," { return ','; } + <JS>"null" { + ZVAL_NULL(&s->value); + return PHP_JSON_T_NUL; + } + <JS>"true" { + ZVAL_TRUE(&s->value); + return PHP_JSON_T_TRUE; + } + <JS>"false" { + ZVAL_FALSE(&s->value); + return PHP_JSON_T_FALSE; + } + <JS>INT { + zend_bool bigint = 0, negative = s->token[0] == '-'; + size_t digits = (size_t) (s->cursor - s->token - negative); + if (digits >= PHP_JSON_INT_MAX_LENGTH) { + if (digits == PHP_JSON_INT_MAX_LENGTH) { + int cmp = strncmp((char *) (s->token + negative), LONG_MIN_DIGITS, PHP_JSON_INT_MAX_LENGTH); + if (!(cmp < 0 || (cmp == 0 && negative))) { + bigint = 1; + } + } else { + bigint = 1; + } + } + if (!bigint) { + ZVAL_LONG(&s->value, strtol((char *) s->token, NULL, 10)); + return PHP_JSON_T_INT; + } else if (s->options & PHP_JSON_BIGINT_AS_STRING) { + ZVAL_STRINGL(&s->value, (char *) s->token, s->cursor - s->token); + return PHP_JSON_T_STRING; + } else { + ZVAL_DOUBLE(&s->value, zend_strtod((char *) s->token, NULL)); + return PHP_JSON_T_DOUBLE; + } + } + <JS>FLOAT|EXP { + ZVAL_DOUBLE(&s->value, zend_strtod((char *) s->token, NULL)); + return PHP_JSON_T_DOUBLE; + } + <JS>NL|WS { goto std; } + <JS>EOI { + if (s->limit < s->cursor) { + return PHP_JSON_T_EOI; + } else { + s->errcode = PHP_JSON_ERROR_SYNTAX; + return PHP_JSON_T_ERROR; + } + } + <JS>["] { + s->str_start = s->cursor; + s->str_esc = 0; + PHP_JSON_CONDITION_SET(STR_P1); + PHP_JSON_CONDITION_GOTO(STR_P1); + } + + <STR_P1>CTRL { + s->errcode = PHP_JSON_ERROR_CTRL_CHAR; + return PHP_JSON_T_ERROR; + } + <STR_P1>UTF16_1 { + s->str_esc += 5; + PHP_JSON_CONDITION_GOTO(STR_P1); + } + <STR_P1>UTF16_2 { + s->str_esc += 4; + PHP_JSON_CONDITION_GOTO(STR_P1); + } + <STR_P1>UTF16_3 { + s->str_esc += 3; + PHP_JSON_CONDITION_GOTO(STR_P1); + } + <STR_P1>UTF16_4 { + s->str_esc += 8; + PHP_JSON_CONDITION_GOTO(STR_P1); + } + <STR_P1>UCS2 { + s->errcode = PHP_JSON_ERROR_UTF16; + return PHP_JSON_T_ERROR; + } + <STR_P1>ESC { + s->str_esc++; + PHP_JSON_CONDITION_GOTO(STR_P1); + } + <STR_P1>ESCPREF { + s->errcode = PHP_JSON_ERROR_SYNTAX; + return PHP_JSON_T_ERROR; + } + <STR_P1>["] { + zend_string *str; + size_t len = s->cursor - s->str_start - s->str_esc - 1; + if (len == 0) { + PHP_JSON_CONDITION_SET(JS); + ZVAL_EMPTY_STRING(&s->value); + return PHP_JSON_T_ESTRING; + } + str = zend_string_alloc(len, 0); + str->val[len] = '\0'; + ZVAL_STR(&s->value, str); + if (s->str_esc) { + s->pstr = (php_json_ctype *) Z_STRVAL(s->value); + s->cursor = s->str_start; + PHP_JSON_CONDITION_SET(STR_P2); + PHP_JSON_CONDITION_GOTO(STR_P2); + } else { + memcpy(Z_STRVAL(s->value), s->str_start, len); + PHP_JSON_CONDITION_SET(JS); + return PHP_JSON_T_STRING; + } + } + <STR_P1>UTF8 { PHP_JSON_CONDITION_GOTO(STR_P1); } + <STR_P1>ANY { + s->errcode = PHP_JSON_ERROR_UTF8; + return PHP_JSON_T_ERROR; + } + + <STR_P2>UTF16_1 { + int utf16 = php_json_ucs2_to_int(s, 2); + PHP_JSON_SCANNER_COPY_UTF(); + *(s->pstr++) = (char) utf16; + s->str_start = s->cursor; + PHP_JSON_CONDITION_GOTO(STR_P2); + } + <STR_P2>UTF16_2 { + int utf16 = php_json_ucs2_to_int(s, 3); + PHP_JSON_SCANNER_COPY_UTF(); + *(s->pstr++) = (char) (0xc0 | (utf16 >> 6)); + *(s->pstr++) = (char) (0x80 | (utf16 & 0x3f)); + s->str_start = s->cursor; + PHP_JSON_CONDITION_GOTO(STR_P2); + } + <STR_P2>UTF16_3 { + int utf16 = php_json_ucs2_to_int(s, 4); + PHP_JSON_SCANNER_COPY_UTF(); + *(s->pstr++) = (char) (0xe0 | (utf16 >> 12)); + *(s->pstr++) = (char) (0x80 | ((utf16 >> 6) & 0x3f)); + *(s->pstr++) = (char) (0x80 | (utf16 & 0x3f)); + s->str_start = s->cursor; + PHP_JSON_CONDITION_GOTO(STR_P2); + } + <STR_P2>UTF16_4 { + int utf32, utf16_hi, utf16_lo; + utf16_hi = php_json_ucs2_to_int(s, 4); + utf16_lo = php_json_ucs2_to_int_ex(s, 4, 7); + utf32 = ((utf16_lo & 0x3FF) << 10) + (utf16_hi & 0x3FF) + 0x10000; + PHP_JSON_SCANNER_COPY_UTF_SP(); + *(s->pstr++) = (char) (0xf0 | (utf32 >> 18)); + *(s->pstr++) = (char) (0x80 | ((utf32 >> 12) & 0x3f)); + *(s->pstr++) = (char) (0x80 | ((utf32 >> 6) & 0x3f)); + *(s->pstr++) = (char) (0x80 | (utf32 & 0x3f)); + s->str_start = s->cursor; + PHP_JSON_CONDITION_GOTO(STR_P2); + } + <STR_P2>ESCPREF { + char esc; + PHP_JSON_SCANNER_COPY_ESC(); + switch (*s->cursor) { + case 'b': + esc = '\b'; + break; + case 'f': + esc = '\f'; + break; + case 'n': + esc = '\n'; + break; + case 'r': + esc = '\r'; + break; + case 't': + esc = '\t'; + break; + case '\\': + case '/': + case '"': + esc = *s->cursor; + break; + default: + s->errcode = PHP_JSON_ERROR_SYNTAX; + return PHP_JSON_T_ERROR; + } + *(s->pstr++) = esc; + ++YYCURSOR; + s->str_start = s->cursor; + PHP_JSON_CONDITION_GOTO(STR_P2); + } + <STR_P2>["] => JS { + PHP_JSON_SCANNER_COPY_ESC(); + return PHP_JSON_T_STRING; + } + <STR_P2>ANY { PHP_JSON_CONDITION_GOTO(STR_P2); } + + <*>ANY { + s->errcode = PHP_JSON_ERROR_SYNTAX; + return PHP_JSON_T_ERROR; + } +*/ + +} + diff --cc ext/json/php_json.h index 2e0bc1b,62eca73..25c799b --- a/ext/json/php_json.h +++ b/ext/json/php_json.h @@@ -69,31 -73,13 +69,35 @@@ typedef enum #define PHP_JSON_OUTPUT_ARRAY 0 #define PHP_JSON_OUTPUT_OBJECT 1 +/* default depth */ +#define PHP_JSON_PARSER_DEFAULT_DEPTH 512 + +ZEND_BEGIN_MODULE_GLOBALS(json) + int encoder_depth; + int encode_max_depth; + php_json_error_code error_code; +ZEND_END_MODULE_GLOBALS(json) + ++ +#ifdef ZTS - # define JSON_G(v) TSRMG(json_globals_id, zend_json_globals *, v) ++# define JSON_G(v) ZEND_TSRMG(json_globals_id, zend_json_globals *, v) ++# ifdef COMPILE_DL_JSON ++ZEND_TSRMLS_CACHE_EXTERN; ++# endif +#else +# define JSON_G(v) (json_globals.v) +#endif + /* json_decode() options */ #define PHP_JSON_OBJECT_AS_ARRAY (1<<0) #define PHP_JSON_BIGINT_AS_STRING (1<<1) - PHP_JSON_API void php_json_encode(smart_str *buf, zval *val, int options TSRMLS_DC); - PHP_JSON_API void php_json_decode_ex(zval *return_value, char *str, size_t str_len, zend_long options, zend_long depth TSRMLS_DC); ++PHP_JSON_API void php_json_encode(smart_str *buf, zval *val, int options); ++PHP_JSON_API void php_json_decode_ex(zval *return_value, char *str, size_t str_len, zend_long options, zend_long depth); + - static inline void php_json_decode(zval *return_value, char *str, int str_len, zend_bool assoc, zend_long depth TSRMLS_DC) + static inline void php_json_decode(zval *return_value, char *str, int str_len, zend_bool assoc, zend_long depth) { - php_json_decode_ex(return_value, str, str_len, assoc ? PHP_JSON_OBJECT_AS_ARRAY : 0, depth TSRMLS_CC); + php_json_decode_ex(return_value, str, str_len, assoc ? PHP_JSON_OBJECT_AS_ARRAY : 0, depth); } diff --cc ext/json/php_json_encoder.h index 4f2b9cc,0000000..db81262 mode 100644,000000..100644 --- a/ext/json/php_json_encoder.h +++ b/ext/json/php_json_encoder.h @@@ -1,27 -1,0 +1,27 @@@ +/* + +----------------------------------------------------------------------+ + | PHP Version 5 | + +----------------------------------------------------------------------+ + | Copyright (c) 1997-2014 The PHP Group | + +----------------------------------------------------------------------+ + | This source file is subject to version 3.01 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available through the world-wide-web at the following url: | + | http://www.php.net/license/3_01.txt | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Author: Jakub Zelenka <bukka@php.net> | + +----------------------------------------------------------------------+ +*/ + +#ifndef PHP_JSON_ENCODER_H +#define PHP_JSON_ENCODER_H + +#include "php.h" +#include "zend_smart_str.h" + - void php_json_encode_zval(smart_str *buf, zval *val, int options TSRMLS_DC); ++void php_json_encode_zval(smart_str *buf, zval *val, int options); + +#endif /* PHP_JSON_ENCODER_H */ diff --cc ext/json/php_json_parser.h index 7912b79,0000000..08c3ee1 mode 100644,000000..100644 --- a/ext/json/php_json_parser.h +++ b/ext/json/php_json_parser.h @@@ -1,42 -1,0 +1,39 @@@ +/* + +----------------------------------------------------------------------+ + | PHP Version 5 | + +----------------------------------------------------------------------+ + | Copyright (c) 1997-2014 The PHP Group | + +----------------------------------------------------------------------+ + | This source file is subject to version 3.01 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available through the world-wide-web at the following url: | + | http://www.php.net/license/3_01.txt | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Author: Jakub Zelenka <bukka@php.net> | + +----------------------------------------------------------------------+ +*/ + +#ifndef PHP_JSON_PARSER_H +#define PHP_JSON_PARSER_H + +#include "php.h" +#include "php_json_scanner.h" + +typedef struct _php_json_parser { + php_json_scanner scanner; + zval *return_value; + long depth; + long max_depth; - #if ZTS - void *zts_ctx; - #endif +} php_json_parser; + - void php_json_parser_init(php_json_parser *parser, zval *return_value, char *str, int str_len, long options, long max_depth TSRMLS_DC); ++void php_json_parser_init(php_json_parser *parser, zval *return_value, char *str, int str_len, long options, long max_depth); + +php_json_error_code php_json_parser_error_code(php_json_parser *parser); + +int php_json_yyparse(php_json_parser *parser); + +#endif /* PHP_JSON_PARSER_H */ + diff --cc ext/json/php_json_scanner.h index 2034654,0000000..fbf65ea mode 100644,000000..100644 --- a/ext/json/php_json_scanner.h +++ b/ext/json/php_json_scanner.h @@@ -1,47 -1,0 +1,47 @@@ +/* + +----------------------------------------------------------------------+ + | PHP Version 5 | + +----------------------------------------------------------------------+ + | Copyright (c) 1997-2014 The PHP Group | + +----------------------------------------------------------------------+ + | This source file is subject to version 3.01 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available through the world-wide-web at the following url: | + | http://www.php.net/license/3_01.txt | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Author: Jakub Zelenka <bukka@php.net> | + +----------------------------------------------------------------------+ +*/ + +#ifndef PHP_JSON_SCANNER_H +#define PHP_JSON_SCANNER_H + +#include "php.h" +#include "php_json.h" + +typedef unsigned char php_json_ctype; + +typedef struct _php_json_scanner { + php_json_ctype *cursor; /* cursor position */ + php_json_ctype *token; /* token position */ + php_json_ctype *limit; /* the last read character + 1 position */ + php_json_ctype *marker; /* marker position for backtracking */ + php_json_ctype *ctxmarker; /* marker position for context backtracking */ + php_json_ctype *str_start; /* start position of the string */ + php_json_ctype *pstr; /* string pointer for escapes conversion */ + int str_esc; /* number of extra characters for escaping */ + int state; /* condition state */ + zval value; /* value */ + long options; /* options */ + php_json_error_code errcode; /* error type if there is an error */ +} php_json_scanner; + + +void php_json_scanner_init(php_json_scanner *scanner, char *str, int str_len, long options); - int php_json_scan(php_json_scanner *s TSRMLS_DC); ++int php_json_scan(php_json_scanner *s); + +#endif /* PHP_JSON_SCANNER_H */ +