xref: /PHP-8.0/ext/json/json.c (revision aa0d886c)
1 /*
2   +----------------------------------------------------------------------+
3   | Copyright (c) The PHP Group                                          |
4   +----------------------------------------------------------------------+
5   | This source file is subject to version 3.01 of the PHP license,      |
6   | that is bundled with this package in the file LICENSE, and is        |
7   | available through the world-wide-web at the following url:           |
8   | http://www.php.net/license/3_01.txt                                  |
9   | If you did not receive a copy of the PHP license and are unable to   |
10   | obtain it through the world-wide-web, please send a note to          |
11   | license@php.net so we can mail you a copy immediately.               |
12   +----------------------------------------------------------------------+
13   | Author: Omar Kilani <omar@php.net>                                   |
14   |         Jakub Zelenka <bukka@php.net>                                |
15   +----------------------------------------------------------------------+
16 */
17 
18 #ifdef HAVE_CONFIG_H
19 #include "config.h"
20 #endif
21 
22 #include "php.h"
23 #include "php_ini.h"
24 #include "ext/standard/info.h"
25 #include "ext/standard/html.h"
26 #include "zend_smart_str.h"
27 #include "php_json.h"
28 #include "php_json_encoder.h"
29 #include "php_json_parser.h"
30 #include "json_arginfo.h"
31 #include <zend_exceptions.h>
32 
33 static PHP_MINFO_FUNCTION(json);
34 
35 PHP_JSON_API zend_class_entry *php_json_serializable_ce;
36 PHP_JSON_API zend_class_entry *php_json_exception_ce;
37 
ZEND_DECLARE_MODULE_GLOBALS(json)38 PHP_JSON_API ZEND_DECLARE_MODULE_GLOBALS(json)
39 
40 /* Register constant for options and errors */
41 #define PHP_JSON_REGISTER_CONSTANT(_name, _value) \
42 	REGISTER_LONG_CONSTANT(_name,  _value, CONST_CS | CONST_PERSISTENT);
43 
44 /* {{{ MINIT */
45 static PHP_MINIT_FUNCTION(json)
46 {
47 	zend_class_entry ce;
48 
49 	INIT_CLASS_ENTRY(ce, "JsonSerializable", class_JsonSerializable_methods);
50 	php_json_serializable_ce = zend_register_internal_interface(&ce);
51 
52 	INIT_CLASS_ENTRY(ce, "JsonException", NULL);
53 	php_json_exception_ce = zend_register_internal_class_ex(&ce, zend_ce_exception);
54 
55 	/* options for json_encode */
56 	PHP_JSON_REGISTER_CONSTANT("JSON_HEX_TAG",  PHP_JSON_HEX_TAG);
57 	PHP_JSON_REGISTER_CONSTANT("JSON_HEX_AMP",  PHP_JSON_HEX_AMP);
58 	PHP_JSON_REGISTER_CONSTANT("JSON_HEX_APOS", PHP_JSON_HEX_APOS);
59 	PHP_JSON_REGISTER_CONSTANT("JSON_HEX_QUOT", PHP_JSON_HEX_QUOT);
60 	PHP_JSON_REGISTER_CONSTANT("JSON_FORCE_OBJECT", PHP_JSON_FORCE_OBJECT);
61 	PHP_JSON_REGISTER_CONSTANT("JSON_NUMERIC_CHECK", PHP_JSON_NUMERIC_CHECK);
62 	PHP_JSON_REGISTER_CONSTANT("JSON_UNESCAPED_SLASHES", PHP_JSON_UNESCAPED_SLASHES);
63 	PHP_JSON_REGISTER_CONSTANT("JSON_PRETTY_PRINT", PHP_JSON_PRETTY_PRINT);
64 	PHP_JSON_REGISTER_CONSTANT("JSON_UNESCAPED_UNICODE", PHP_JSON_UNESCAPED_UNICODE);
65 	PHP_JSON_REGISTER_CONSTANT("JSON_PARTIAL_OUTPUT_ON_ERROR", PHP_JSON_PARTIAL_OUTPUT_ON_ERROR);
66 	PHP_JSON_REGISTER_CONSTANT("JSON_PRESERVE_ZERO_FRACTION", PHP_JSON_PRESERVE_ZERO_FRACTION);
67 	PHP_JSON_REGISTER_CONSTANT("JSON_UNESCAPED_LINE_TERMINATORS", PHP_JSON_UNESCAPED_LINE_TERMINATORS);
68 
69 	/* options for json_decode */
70 	PHP_JSON_REGISTER_CONSTANT("JSON_OBJECT_AS_ARRAY", PHP_JSON_OBJECT_AS_ARRAY);
71 	PHP_JSON_REGISTER_CONSTANT("JSON_BIGINT_AS_STRING", PHP_JSON_BIGINT_AS_STRING);
72 
73 	/* common options for json_decode and json_encode */
74 	PHP_JSON_REGISTER_CONSTANT("JSON_INVALID_UTF8_IGNORE", PHP_JSON_INVALID_UTF8_IGNORE);
75 	PHP_JSON_REGISTER_CONSTANT("JSON_INVALID_UTF8_SUBSTITUTE", PHP_JSON_INVALID_UTF8_SUBSTITUTE);
76 	PHP_JSON_REGISTER_CONSTANT("JSON_THROW_ON_ERROR", PHP_JSON_THROW_ON_ERROR);
77 
78 	/* json error constants */
79 	PHP_JSON_REGISTER_CONSTANT("JSON_ERROR_NONE", PHP_JSON_ERROR_NONE);
80 	PHP_JSON_REGISTER_CONSTANT("JSON_ERROR_DEPTH", PHP_JSON_ERROR_DEPTH);
81 	PHP_JSON_REGISTER_CONSTANT("JSON_ERROR_STATE_MISMATCH", PHP_JSON_ERROR_STATE_MISMATCH);
82 	PHP_JSON_REGISTER_CONSTANT("JSON_ERROR_CTRL_CHAR", PHP_JSON_ERROR_CTRL_CHAR);
83 	PHP_JSON_REGISTER_CONSTANT("JSON_ERROR_SYNTAX", PHP_JSON_ERROR_SYNTAX);
84 	PHP_JSON_REGISTER_CONSTANT("JSON_ERROR_UTF8", PHP_JSON_ERROR_UTF8);
85 	PHP_JSON_REGISTER_CONSTANT("JSON_ERROR_RECURSION", PHP_JSON_ERROR_RECURSION);
86 	PHP_JSON_REGISTER_CONSTANT("JSON_ERROR_INF_OR_NAN", PHP_JSON_ERROR_INF_OR_NAN);
87 	PHP_JSON_REGISTER_CONSTANT("JSON_ERROR_UNSUPPORTED_TYPE", PHP_JSON_ERROR_UNSUPPORTED_TYPE);
88 	PHP_JSON_REGISTER_CONSTANT("JSON_ERROR_INVALID_PROPERTY_NAME", PHP_JSON_ERROR_INVALID_PROPERTY_NAME);
89 	PHP_JSON_REGISTER_CONSTANT("JSON_ERROR_UTF16", PHP_JSON_ERROR_UTF16);
90 
91 	return SUCCESS;
92 }
93 /* }}} */
94 
95 /* {{{ PHP_GINIT_FUNCTION */
PHP_GINIT_FUNCTION(json)96 static PHP_GINIT_FUNCTION(json)
97 {
98 #if defined(COMPILE_DL_JSON) && defined(ZTS)
99 	ZEND_TSRMLS_CACHE_UPDATE();
100 #endif
101 	json_globals->encoder_depth = 0;
102 	json_globals->error_code = 0;
103 	json_globals->encode_max_depth = PHP_JSON_PARSER_DEFAULT_DEPTH;
104 }
105 /* }}} */
106 
PHP_RINIT_FUNCTION(json)107 static PHP_RINIT_FUNCTION(json)
108 {
109 	JSON_G(error_code) = 0;
110 	return SUCCESS;
111 }
112 
113 /* {{{ json_module_entry */
114 zend_module_entry json_module_entry = {
115 	STANDARD_MODULE_HEADER,
116 	"json",
117 	ext_functions,
118 	PHP_MINIT(json),
119 	NULL,
120 	PHP_RINIT(json),
121 	NULL,
122 	PHP_MINFO(json),
123 	PHP_JSON_VERSION,
124 	PHP_MODULE_GLOBALS(json),
125 	PHP_GINIT(json),
126 	NULL,
127 	NULL,
128 	STANDARD_MODULE_PROPERTIES_EX
129 };
130 /* }}} */
131 
132 #ifdef COMPILE_DL_JSON
133 #ifdef ZTS
134 ZEND_TSRMLS_CACHE_DEFINE()
135 #endif
ZEND_GET_MODULE(json)136 ZEND_GET_MODULE(json)
137 #endif
138 
139 /* {{{ PHP_MINFO_FUNCTION */
140 static PHP_MINFO_FUNCTION(json)
141 {
142 	php_info_print_table_start();
143 	php_info_print_table_row(2, "json support", "enabled");
144 	php_info_print_table_end();
145 }
146 /* }}} */
147 
php_json_encode_ex(smart_str * buf,zval * val,int options,zend_long depth)148 PHP_JSON_API int php_json_encode_ex(smart_str *buf, zval *val, int options, zend_long depth) /* {{{ */
149 {
150 	php_json_encoder encoder;
151 	int return_code;
152 
153 	php_json_encode_init(&encoder);
154 	encoder.max_depth = depth;
155 
156 	return_code = php_json_encode_zval(buf, val, options, &encoder);
157 	JSON_G(error_code) = encoder.error_code;
158 
159 	return return_code;
160 }
161 /* }}} */
162 
php_json_encode(smart_str * buf,zval * val,int options)163 PHP_JSON_API int php_json_encode(smart_str *buf, zval *val, int options) /* {{{ */
164 {
165 	return php_json_encode_ex(buf, val, options, JSON_G(encode_max_depth));
166 }
167 /* }}} */
168 
php_json_get_error_msg(php_json_error_code error_code)169 static const char *php_json_get_error_msg(php_json_error_code error_code) /* {{{ */
170 {
171 	switch(error_code) {
172 		case PHP_JSON_ERROR_NONE:
173 			return "No error";
174 		case PHP_JSON_ERROR_DEPTH:
175 			return "Maximum stack depth exceeded";
176 		case PHP_JSON_ERROR_STATE_MISMATCH:
177 			return "State mismatch (invalid or malformed JSON)";
178 		case PHP_JSON_ERROR_CTRL_CHAR:
179 			return "Control character error, possibly incorrectly encoded";
180 		case PHP_JSON_ERROR_SYNTAX:
181 			return "Syntax error";
182 		case PHP_JSON_ERROR_UTF8:
183 			return "Malformed UTF-8 characters, possibly incorrectly encoded";
184 		case PHP_JSON_ERROR_RECURSION:
185 			return "Recursion detected";
186 		case PHP_JSON_ERROR_INF_OR_NAN:
187 			return "Inf and NaN cannot be JSON encoded";
188 		case PHP_JSON_ERROR_UNSUPPORTED_TYPE:
189 			return "Type is not supported";
190 		case PHP_JSON_ERROR_INVALID_PROPERTY_NAME:
191 			return "The decoded property name is invalid";
192 		case PHP_JSON_ERROR_UTF16:
193 			return "Single unpaired UTF-16 surrogate in unicode escape";
194 		default:
195 			return "Unknown error";
196 	}
197 }
198 /* }}} */
199 
php_json_decode_ex(zval * return_value,const char * str,size_t str_len,zend_long options,zend_long depth)200 PHP_JSON_API int php_json_decode_ex(zval *return_value, const char *str, size_t str_len, zend_long options, zend_long depth) /* {{{ */
201 {
202 	php_json_parser parser;
203 
204 	php_json_parser_init(&parser, return_value, str, str_len, (int)options, (int)depth);
205 
206 	if (php_json_yyparse(&parser)) {
207 		php_json_error_code error_code = php_json_parser_error_code(&parser);
208 		if (!(options & PHP_JSON_THROW_ON_ERROR)) {
209 			JSON_G(error_code) = error_code;
210 		} else {
211 			zend_throw_exception(php_json_exception_ce, php_json_get_error_msg(error_code), error_code);
212 		}
213 		RETVAL_NULL();
214 		return FAILURE;
215 	}
216 
217 	return SUCCESS;
218 }
219 /* }}} */
220 
221 /* {{{ Returns the JSON representation of a value */
PHP_FUNCTION(json_encode)222 PHP_FUNCTION(json_encode)
223 {
224 	zval *parameter;
225 	php_json_encoder encoder;
226 	smart_str buf = {0};
227 	zend_long options = 0;
228 	zend_long depth = PHP_JSON_PARSER_DEFAULT_DEPTH;
229 
230 	ZEND_PARSE_PARAMETERS_START(1, 3)
231 		Z_PARAM_ZVAL(parameter)
232 		Z_PARAM_OPTIONAL
233 		Z_PARAM_LONG(options)
234 		Z_PARAM_LONG(depth)
235 	ZEND_PARSE_PARAMETERS_END();
236 
237 	php_json_encode_init(&encoder);
238 	encoder.max_depth = (int)depth;
239 	php_json_encode_zval(&buf, parameter, (int)options, &encoder);
240 
241 	if (!(options & PHP_JSON_THROW_ON_ERROR) || (options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR)) {
242 		JSON_G(error_code) = encoder.error_code;
243 		if (encoder.error_code != PHP_JSON_ERROR_NONE && !(options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR)) {
244 			smart_str_free(&buf);
245 			RETURN_FALSE;
246 		}
247 	} else {
248 		if (encoder.error_code != PHP_JSON_ERROR_NONE) {
249 			smart_str_free(&buf);
250 			zend_throw_exception(php_json_exception_ce, php_json_get_error_msg(encoder.error_code), encoder.error_code);
251 			RETURN_THROWS();
252 		}
253 	}
254 
255 	smart_str_0(&buf); /* copy? */
256 	if (buf.s) {
257 		RETURN_NEW_STR(buf.s);
258 	}
259 	RETURN_EMPTY_STRING();
260 }
261 /* }}} */
262 
263 /* {{{ Decodes the JSON representation into a PHP value */
PHP_FUNCTION(json_decode)264 PHP_FUNCTION(json_decode)
265 {
266 	char *str;
267 	size_t str_len;
268 	zend_bool assoc = 0; /* return JS objects as PHP objects by default */
269 	zend_bool assoc_null = 1;
270 	zend_long depth = PHP_JSON_PARSER_DEFAULT_DEPTH;
271 	zend_long options = 0;
272 
273 	ZEND_PARSE_PARAMETERS_START(1, 4)
274 		Z_PARAM_STRING(str, str_len)
275 		Z_PARAM_OPTIONAL
276 		Z_PARAM_BOOL_EX(assoc, assoc_null, 1, 0)
277 		Z_PARAM_LONG(depth)
278 		Z_PARAM_LONG(options)
279 	ZEND_PARSE_PARAMETERS_END();
280 
281 	if (!(options & PHP_JSON_THROW_ON_ERROR)) {
282 		JSON_G(error_code) = PHP_JSON_ERROR_NONE;
283 	}
284 
285 	if (!str_len) {
286 		if (!(options & PHP_JSON_THROW_ON_ERROR)) {
287 			JSON_G(error_code) = PHP_JSON_ERROR_SYNTAX;
288 		} else {
289 			zend_throw_exception(php_json_exception_ce, php_json_get_error_msg(PHP_JSON_ERROR_SYNTAX), PHP_JSON_ERROR_SYNTAX);
290 		}
291 		RETURN_NULL();
292 	}
293 
294 	if (depth <= 0) {
295 		zend_argument_value_error(3, "must be greater than 0");
296 		RETURN_THROWS();
297 	}
298 
299 	if (depth > INT_MAX) {
300 		zend_argument_value_error(3, "must be less than %d", INT_MAX);
301 		RETURN_THROWS();
302 	}
303 
304 	/* For BC reasons, the bool $assoc overrides the long $options bit for PHP_JSON_OBJECT_AS_ARRAY */
305 	if (!assoc_null) {
306 		if (assoc) {
307 			options |=  PHP_JSON_OBJECT_AS_ARRAY;
308 		} else {
309 			options &= ~PHP_JSON_OBJECT_AS_ARRAY;
310 		}
311 	}
312 
313 	php_json_decode_ex(return_value, str, str_len, options, depth);
314 }
315 /* }}} */
316 
317 /* {{{ Returns the error code of the last json_encode() or json_decode() call. */
PHP_FUNCTION(json_last_error)318 PHP_FUNCTION(json_last_error)
319 {
320 	ZEND_PARSE_PARAMETERS_NONE();
321 
322 	RETURN_LONG(JSON_G(error_code));
323 }
324 /* }}} */
325 
326 /* {{{ Returns the error string of the last json_encode() or json_decode() call. */
PHP_FUNCTION(json_last_error_msg)327 PHP_FUNCTION(json_last_error_msg)
328 {
329 	ZEND_PARSE_PARAMETERS_NONE();
330 
331 	RETURN_STRING(php_json_get_error_msg(JSON_G(error_code)));
332 }
333 /* }}} */
334