[PHP-CVS] cvs: php-src /ext/json JSON_parser.c JSON_parser.h json.c
scottmacThu May 14 00:13:57 2009 UTC Modified files: /php-src/ext/json JSON_parser.c JSON_parser.h json.c Log: Allow the json_decode() depth to be any size, but keep the static one around for now. It might make sense to allow an unbound depth. http://cvs.php.net/viewvc.cgi/php-src/ext/json/JSON_parser.c?r1=1.25&r2=1.26&diff_format=u Index: php-src/ext/json/JSON_parser.c diff -u php-src/ext/json/JSON_parser.c:1.25 php-src/ext/json/JSON_parser.c:1.26 --- php-src/ext/json/JSON_parser.c:1.25 Thu Mar 19 19:25:42 2009 +++ php-src/ext/json/JSON_parser.c Thu May 14 00:13:57 2009 @@ -247,6 +247,11 @@ jp->top = -1; jp->error_code = PHP_JSON_ERROR_NONE; jp->stack = (int*)ecalloc(depth, sizeof(int)); +if (depth > JSON_PARSER_DEFAULT_DEPTH) { +jp->the_zstack = (zval **)safe_emalloc(depth, sizeof(zval), 0); +} else { +jp->the_zstack = &jp->the_static_zstack[0]; +} push(jp, MODE_DONE); return jp; } @@ -258,6 +263,9 @@ free_JSON_parser(JSON_parser jp) { efree((void*)jp->stack); +if (jp->the_zstack != &jp->the_static_zstack[0]) { +efree(jp->the_zstack); +} efree((void*)jp); return false; } http://cvs.php.net/viewvc.cgi/php-src/ext/json/JSON_parser.h?r1=1.4&r2=1.5&diff_format=u Index: php-src/ext/json/JSON_parser.h diff -u php-src/ext/json/JSON_parser.h:1.4 php-src/ext/json/JSON_parser.h:1.5 --- php-src/ext/json/JSON_parser.h:1.4 Fri Dec 19 02:00:59 2008 +++ php-src/ext/json/JSON_parser.h Thu May 14 00:13:57 2009 @@ -6,7 +6,7 @@ #include "php.h" #include "ext/standard/php_smart_str.h" -#define JSON_PARSER_MAX_DEPTH 512 +#define JSON_PARSER_DEFAULT_DEPTH 512 typedef struct JSON_parser_struct { int state; @@ -14,8 +14,8 @@ int top; int error_code; int* stack; -zval *the_zstack[JSON_PARSER_MAX_DEPTH]; - +zval **the_zstack; +zval *the_static_zstack[JSON_PARSER_DEFAULT_DEPTH]; } * JSON_parser; enum error_codes { http://cvs.php.net/viewvc.cgi/php-src/ext/json/json.c?r1=1.50&r2=1.51&diff_format=u Index: php-src/ext/json/json.c diff -u php-src/ext/json/json.c:1.50 php-src/ext/json/json.c:1.51 --- php-src/ext/json/json.c:1.50Tue Mar 17 23:25:39 2009 +++ php-src/ext/json/json.c Thu May 14 00:13:57 2009 @@ -16,7 +16,7 @@ +--+ */ -/* $Id: json.c,v 1.50 2009/03/17 23:25:39 scottmac Exp $ */ +/* $Id: json.c,v 1.51 2009/05/14 00:13:57 scottmac Exp $ */ #ifdef HAVE_CONFIG_H #include "config.h" @@ -507,7 +507,7 @@ int str_len, utf16_len; zend_uchar str_type; zend_bool assoc = 0; /* return JS objects as PHP objects by default */ - long depth = JSON_PARSER_MAX_DEPTH; + long depth = JSON_PARSER_DEFAULT_DEPTH; zval *z; unsigned short *utf16; JSON_parser jp; @@ -535,9 +535,8 @@ } } - /* can be removed once we remove the max depth limit */ - if (depth <= 0 || depth > JSON_PARSER_MAX_DEPTH) { - depth = JSON_PARSER_MAX_DEPTH; + if (depth <= 0) { + depth = JSON_PARSER_DEFAULT_DEPTH; } ALLOC_INIT_ZVAL(z); -- PHP CVS Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP-CVS] cvs: php-src /ext/json JSON_parser.c JSON_parser.h json.c php_json.h /ext/json/tests 007.phpt
scottmacFri Dec 19 02:01:00 2008 UTC Modified files: /php-src/ext/json JSON_parser.c JSON_parser.h json.c php_json.h /php-src/ext/json/tests 007.phpt Log: Add json_last_error() for getting a bit of information about what failed during a decode, also fixes a segfault when we have [1} [DOC] http://cvs.php.net/viewvc.cgi/php-src/ext/json/JSON_parser.c?r1=1.21&r2=1.22&diff_format=u Index: php-src/ext/json/JSON_parser.c diff -u php-src/ext/json/JSON_parser.c:1.21 php-src/ext/json/JSON_parser.c:1.22 --- php-src/ext/json/JSON_parser.c:1.21 Wed Dec 17 15:46:00 2008 +++ php-src/ext/json/JSON_parser.c Fri Dec 19 02:00:59 2008 @@ -194,13 +194,6 @@ MODE_OBJECT, }; -enum error_codes { -ERROR_DEPTH, -ERROR_MISMATCH, -ERROR_CTRL_CHAR, -ERROR_SYNTAX, -}; - /* Push a mode onto the stack. Return false if there is overflow. @@ -210,7 +203,7 @@ { jp->top += 1; if (jp->top >= jp->depth) { - jp->error = ERROR_DEPTH; + jp->error_code = PHP_JSON_ERROR_DEPTH; return false; } jp->stack[jp->top] = mode; @@ -226,7 +219,7 @@ pop(JSON_parser jp, int mode) { if (jp->top < 0 || jp->stack[jp->top] != mode) { - jp->error = ERROR_MISMATCH; + jp->error_code = PHP_JSON_ERROR_STATE_MISMATCH; return false; } jp->top -= 1; @@ -252,6 +245,7 @@ jp->state = GO; jp->depth = depth; jp->top = -1; + jp->error_code = PHP_JSON_ERROR_NONE; jp->stack = (int*)ecalloc(depth, sizeof(int)); push(jp, MODE_DONE); return jp; @@ -428,7 +422,7 @@ } else { next_class = ascii_class[next_char]; if (next_class <= __) { - jp->error = ERROR_CTRL_CHAR; + jp->error_code = PHP_JSON_ERROR_CTRL_CHAR; FREE_BUFFERS(); return false; } @@ -511,9 +505,7 @@ break; /* } */ case -8: -if (type != -1 && -(jp->stack[jp->top] == MODE_OBJECT || - jp->stack[jp->top] == MODE_ARRAY)) +if (type != -1 && jp->stack[jp->top] == MODE_OBJECT) { zval *mval; smart_str_0(&buf); @@ -541,9 +533,7 @@ /* ] */ case -7: { -if (type != -1 && -(jp->stack[jp->top] == MODE_OBJECT || - jp->stack[jp->top] == MODE_ARRAY)) +if (type != -1 && jp->stack[jp->top] == MODE_ARRAY) { zval *mval; smart_str_0(&buf); @@ -702,7 +692,7 @@ */ default: { - jp->error = ERROR_SYNTAX; + jp->error_code = PHP_JSON_ERROR_SYNTAX; FREE_BUFFERS(); return false; } @@ -711,11 +701,14 @@ } FREE_BUFFERS(); + if (jp->state == OK && pop(jp, MODE_DONE)) { + return true; + } -return jp->state == OK && pop(jp, MODE_DONE); + jp->error_code = PHP_JSON_ERROR_SYNTAX; + return false; } - /* * Local variables: * tab-width: 4 http://cvs.php.net/viewvc.cgi/php-src/ext/json/JSON_parser.h?r1=1.3&r2=1.4&diff_format=u Index: php-src/ext/json/JSON_parser.h diff -u php-src/ext/json/JSON_parser.h:1.3 php-src/ext/json/JSON_parser.h:1.4 --- php-src/ext/json/JSON_parser.h:1.3 Wed Dec 17 14:39:56 2008 +++ php-src/ext/json/JSON_parser.h Fri Dec 19 02:00:59 2008 @@ -12,12 +12,20 @@ int state; int depth; int top; -int error; +int error_code; int* stack; zval *the_zstack[JSON_PARSER_MAX_DEPTH]; } * JSON_parser; +enum error_codes { + PHP_JSON_ERROR_NONE = 0, +PHP_JSON_ERROR_DEPTH, +PHP_JSON_ERROR_STATE_MISMATCH, +PHP_JSON_ERROR_CTRL_CHAR, +PHP_JSON_ERROR_SYNTAX, +}; + extern JSON_parser new_JSON_parser(int depth); extern int parse_JSON(JSON_parser jp, zval *z, unsigned short utf16_json[], int length, int assoc TSRMLS_DC); extern int free_JSON_parser(JSON_parser jp); http://cvs.php.net/viewvc.cgi/php-src/ext/json/json.c?r1=1.44&r2=1.45&diff_format=u Index: php-src/ext/json/json.c diff -u php-src/ext/json/json.c:1.44 php-src/ext/json/json.c:1.45 --- php-src/ext/json/json.c:1.44Wed Dec 17 14:39:56 2008 +++ php-src/ext/json/json.c Fri Dec 19 02:00:59 2008 @@ -16,7 +16,7 @@ +--+ */ -/* $Id: json.c,v 1.44 2008/12/17 14:39:56 scottmac Exp $ */ +/* $Id: json.c,v 1.45 2008/12/19 02:00:59 scottmac Exp $ */ #ifdef HAVE_CONFIG_H #include "config.h" @@ -33,6 +33,7 @@ static PHP_MINFO_FUNCTION(json); static PHP_FUNCTION(
[PHP-CVS] cvs: php-src /ext/json JSON_parser.c JSON_parser.h json.c
scottmacWed Dec 17 14:39:56 2008 UTC Modified files: /php-src/ext/json JSON_parser.c JSON_parser.h json.c Log: Update the JSON parser with that on json.org, biggest change here is code readability. Less magic numbers in the state table. Add missing reflection information to json_encode() Fixes bug #45791 with 0e0 not being supported as a value Error values are stored when encountered during parsing http://cvs.php.net/viewvc.cgi/php-src/ext/json/JSON_parser.c?r1=1.19&r2=1.20&diff_format=u Index: php-src/ext/json/JSON_parser.c diff -u php-src/ext/json/JSON_parser.c:1.19 php-src/ext/json/JSON_parser.c:1.20 --- php-src/ext/json/JSON_parser.c:1.19 Tue Dec 2 14:50:01 2008 +++ php-src/ext/json/JSON_parser.c Wed Dec 17 14:39:56 2008 @@ -26,331 +26,363 @@ SOFTWARE. */ - -#include "JSON_parser.h" #include +#include "JSON_parser.h" #define true 1 #define false 0 +#define __ -1 /* the universal error code */ /* -Characters are mapped into these 32 symbol classes. This allows for -significant reductions in the size of the state transition table. +Characters are mapped into these 31 character classes. This allows for +a significant reduction in the size of the state transition table. */ -/* {{{ constants */ - -/* error */ -#define S_ERR -1 - -/* space */ -#define S_SPA 0 - -/* other whitespace */ -#define S_WSP 1 - -/* { */ -#define S_LBE 2 - -/* } */ -#define S_RBE 3 - -/* [ */ -#define S_LBT 4 - -/* ] */ -#define S_RBT 5 - -/* : */ -#define S_COL 6 - -/* , */ -#define S_COM 7 - -/* " */ -#define S_QUO 8 - -/* \ */ -#define S_BAC 9 - -/* / */ -#define S_SLA 10 - -/* + */ -#define S_PLU 11 - -/* - */ -#define S_MIN 12 - -/* . */ -#define S_DOT 13 - -/* 0 */ -#define S_ZER 14 - -/* 123456789 */ -#define S_DIG 15 - -/* a */ -#define S__A_ 16 - -/* b */ -#define S__B_ 17 - -/* c */ -#define S__C_ 18 - -/* d */ -#define S__D_ 19 - -/* e */ -#define S__E_ 20 - -/* f */ -#define S__F_ 21 - -/* l */ -#define S__L_ 22 - -/* n */ -#define S__N_ 23 - -/* r */ -#define S__R_ 24 - -/* s */ -#define S__S_ 25 - -/* t */ -#define S__T_ 26 - -/* u */ -#define S__U_ 27 - -/* ABCDF */ -#define S_A_F 28 - -/* E */ -#define S_E 29 +enum classes { +C_SPACE, /* space */ +C_WHITE, /* other whitespace */ +C_LCURB, /* { */ +C_RCURB, /* } */ +C_LSQRB, /* [ */ +C_RSQRB, /* ] */ +C_COLON, /* : */ +C_COMMA, /* , */ +C_QUOTE, /* " */ +C_BACKS, /* \ */ +C_SLASH, /* / */ +C_PLUS, /* + */ +C_MINUS, /* - */ +C_POINT, /* . */ +C_ZERO , /* 0 */ +C_DIGIT, /* 123456789 */ +C_LOW_A, /* a */ +C_LOW_B, /* b */ +C_LOW_C, /* c */ +C_LOW_D, /* d */ +C_LOW_E, /* e */ +C_LOW_F, /* f */ +C_LOW_L, /* l */ +C_LOW_N, /* n */ +C_LOW_R, /* r */ +C_LOW_S, /* s */ +C_LOW_T, /* t */ +C_LOW_U, /* u */ +C_ABCDF, /* ABCDF */ +C_E, /* E */ +C_ETC,/* everything else */ +NR_CLASSES +}; -/* everything else */ -#define S_ETC 30 +static const int ascii_class[128] = { +/* +This array maps the 128 ASCII characters into character classes. +The remaining Unicode characters should be mapped to C_ETC. +Non-whitespace control characters are errors. +*/ +__, __, __, __, __, __, __, __, +__, C_WHITE, C_WHITE, __, __, C_WHITE, __, __, +__, __, __, __, __, __, __, __, +__, __, __, __, __, __, __, __, + +C_SPACE, C_ETC, C_QUOTE, C_ETC, C_ETC, C_ETC, C_ETC, C_ETC, +C_ETC, C_ETC, C_ETC, C_PLUS, C_COMMA, C_MINUS, C_POINT, C_SLASH, +C_ZERO, C_DIGIT, C_DIGIT, C_DIGIT, C_DIGIT, C_DIGIT, C_DIGIT, C_DIGIT, +C_DIGIT, C_DIGIT, C_COLON, C_ETC, C_ETC, C_ETC, C_ETC, C_ETC, + +C_ETC, C_ABCDF, C_ABCDF, C_ABCDF, C_ABCDF, C_E, C_ABCDF, C_ETC, +C_ETC, C_ETC, C_ETC, C_ETC, C_ETC, C_ETC, C_ETC, C_ETC, +C_ETC, C_ETC, C_ETC, C_ETC, C_ETC, C_ETC, C_ETC, C_ETC, +C_ETC, C_ETC, C_ETC, C_LSQRB, C_BACKS, C_RSQRB, C_ETC, C_ETC, + +C_ETC, C_LOW_A, C_LOW_B, C_LOW_C, C_LOW_D, C_LOW_E, C_LOW_F, C_ETC, +C_ETC, C_ETC, C_ETC, C_ETC, C_LOW_L, C_ETC, C_LOW_N, C_ETC, +C_ETC, C_ETC, C_LOW_R, C_LOW_S, C_LOW_T, C_LOW_U, C_ETC, C_ETC, +C_ETC, C_ETC, C_ETC, C_LCURB, C_ETC, C_RCURB, C_ETC, C_ETC +}; -/* }}} */ -/* {{{ tables */ /* -This table maps the 128 ASCII characters into the 32 character classes. -The remaining Unicode characters should be mapped to S_ETC. +The state codes. */ -static const int ascii_class[128] = { -S_ERR, S_ERR, S_ERR, S_ERR, S_ERR, S_ERR, S_ERR, S_ERR, -S_ERR, S_WSP, S_WSP, S_ERR, S_ERR, S_WSP, S_ERR, S_ERR, -S_ERR, S_ERR, S_ERR, S_ERR, S_ERR, S_ERR, S_ERR, S_ERR, -S_ERR, S_ERR, S_ERR, S_ERR, S_ERR, S_ERR, S_ERR, S_ER