forked from JakubOnderka/simdjson_php
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathphp_simdjson.cpp
586 lines (496 loc) · 17.3 KB
/
php_simdjson.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
/*
+----------------------------------------------------------------------+
| simdjson_php |
+----------------------------------------------------------------------+
| This source file is subject to version 2.0 of the Apache 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.apache.org/licenses/LICENSE-2.0.html |
+----------------------------------------------------------------------+
| Author: Jinxi Wang <[email protected]> |
+----------------------------------------------------------------------+
*/
extern "C" {
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "zend_exceptions.h"
#include "zend_smart_str.h"
#include "main/SAPI.h"
#include "ext/standard/info.h"
#include "ext/spl/spl_exceptions.h"
#include "ext/json/php_json.h" /* For php_json_serializable_ce */
#include "php_simdjson.h"
/**
* Both the declaration and the definition of PHP_SIMDJSON_API variables, functions must be within an 'extern "C"' block for Windows
*/
PHP_SIMDJSON_API zend_class_entry *simdjson_exception_ce;
PHP_SIMDJSON_API zend_class_entry *simdjson_base64_encode_ce;
} /* end extern "C" */
/* C++ header file for simdjson_php helper methods/classes */
#include "src/simdjson_compatibility.h"
#include "src/simdjson_smart_str.h"
#include "src/simdjson_decoder_defs.h"
#include "src/simdjson_encoder.h"
/* Single header file from fork of simdjson C project (to imitate php's handling of infinity/overflowing integers in json_decode) */
#include "src/simdjson.h"
#include "src/simdutf.h"
#include "simdjson_arginfo.h"
static zend_string *simdjson_json_empty_array;
ZEND_DECLARE_MODULE_GLOBALS(simdjson);
#define SIMDJSON_G(v) ZEND_MODULE_GLOBALS_ACCESSOR(simdjson, v)
static simdjson_php_parser *simdjson_get_parser() {
simdjson_php_parser *parser = SIMDJSON_G(parser);
if (parser == NULL) {
parser = php_simdjson_create_parser();
SIMDJSON_G(parser) = parser;
ZEND_ASSERT(parser != NULL);
}
return parser;
}
PHP_SIMDJSON_API struct simdjson_php_parser *php_simdjson_get_default_singleton_parser(void) {
return simdjson_get_parser();
}
// The simdjson parser accepts strings with at most 32-bit lengths, for now.
#define SIMDJSON_MAX_DEPTH ((zend_long)((SIZE_MAX / 8) < (UINT32_MAX / 2) ? (SIZE_MAX / 8) : (UINT32_MAX / 2)))
static zend_always_inline bool simdjson_validate_depth(zend_long depth, const int arg_num) {
if (UNEXPECTED(depth <= 0)) {
zend_argument_value_error(arg_num, "must be greater than zero");
return false;
} else if (UNEXPECTED(depth > SIMDJSON_MAX_DEPTH)) {
zend_argument_value_error(arg_num, "exceeds maximum allowed value of " ZEND_LONG_FMT, SIMDJSON_MAX_DEPTH);
return false;
}
return true;
}
// Free memory allocated by parser if capacity is bigger than 100 MB
static void simdjson_parser_cleanup() {
simdjson_php_parser *parser = SIMDJSON_G(parser);
if (parser->parser.capacity() > 100 * 1024 * 1024) {
php_simdjson_free_parser(parser);
SIMDJSON_G(parser) = NULL;
}
}
PHP_FUNCTION(simdjson_validate) {
zend_string *json = NULL;
zend_long depth = SIMDJSON_PARSE_DEFAULT_DEPTH;
ZEND_PARSE_PARAMETERS_START(1, 2)
Z_PARAM_STR(json)
Z_PARAM_OPTIONAL
Z_PARAM_LONG(depth)
ZEND_PARSE_PARAMETERS_END();
if (!simdjson_validate_depth(depth, 2)) {
RETURN_THROWS();
}
simdjson_php_error_code error = php_simdjson_validate(simdjson_get_parser(), json, depth);
simdjson_parser_cleanup();
ZVAL_BOOL(return_value, !error);
}
// Decode simple and common JSON values without allocating and using simdjson parser
static zend_always_inline bool simdjson_simple_decode(zend_string *json, zval *return_value, bool associative) {
// Empty object
if (ZSTR_LEN(json) == 2 && ZSTR_VAL(json)[0] == '{' && ZSTR_VAL(json)[1] == '}') {
if (associative) {
RETVAL_EMPTY_ARRAY();
} else {
object_init(return_value);
}
return true;
}
// Empty array
if (ZSTR_LEN(json) == 2 && ZSTR_VAL(json)[0] == '[' && ZSTR_VAL(json)[1] == ']') {
RETVAL_EMPTY_ARRAY();
return true;
}
if (zend_string_equals_cstr(json, "true", 4)) {
RETVAL_TRUE;
return true;
} else if (zend_string_equals_cstr(json, "false", 5)) {
RETVAL_FALSE;
return true;
}
return false;
}
PHP_FUNCTION(simdjson_decode) {
zend_bool associative = 0;
zend_long depth = SIMDJSON_PARSE_DEFAULT_DEPTH;
zend_string *json = NULL;
ZEND_PARSE_PARAMETERS_START(1, 3)
Z_PARAM_STR(json)
Z_PARAM_OPTIONAL
Z_PARAM_BOOL(associative)
Z_PARAM_LONG(depth)
ZEND_PARSE_PARAMETERS_END();
if (!simdjson_validate_depth(depth, 3)) {
RETURN_THROWS();
}
if (simdjson_simple_decode(json, return_value, associative)) {
return;
}
simdjson_php_error_code error = php_simdjson_parse(simdjson_get_parser(), json, return_value, associative, depth);
simdjson_parser_cleanup();
if (UNEXPECTED(error)) {
php_simdjson_throw_jsonexception(error);
RETURN_THROWS();
}
}
PHP_FUNCTION(simdjson_key_value) {
zend_string *json = NULL;
zend_string *key = NULL;
zend_bool associative = 0;
zend_long depth = SIMDJSON_PARSE_DEFAULT_DEPTH;
ZEND_PARSE_PARAMETERS_START(2, 4)
Z_PARAM_STR(json)
Z_PARAM_STR(key)
Z_PARAM_OPTIONAL
Z_PARAM_BOOL(associative)
Z_PARAM_LONG(depth)
ZEND_PARSE_PARAMETERS_END();
if (!simdjson_validate_depth(depth, 4)) {
RETURN_THROWS();
}
simdjson_php_error_code error = php_simdjson_key_value(simdjson_get_parser(), json, ZSTR_VAL(key), return_value, associative, depth);
simdjson_parser_cleanup();
if (error) {
php_simdjson_throw_jsonexception(error);
RETURN_THROWS();
}
}
PHP_FUNCTION(simdjson_key_count) {
zend_string *json = NULL;
zend_string *key = NULL;
zend_long depth = SIMDJSON_PARSE_DEFAULT_DEPTH;
bool throw_if_uncountable = false;
ZEND_PARSE_PARAMETERS_START(2, 4)
Z_PARAM_STR(json)
Z_PARAM_STR(key)
Z_PARAM_OPTIONAL
Z_PARAM_LONG(depth)
Z_PARAM_BOOL(throw_if_uncountable)
ZEND_PARSE_PARAMETERS_END();
if (!simdjson_validate_depth(depth, 3)) {
RETURN_THROWS();
}
simdjson_php_error_code error = php_simdjson_key_count(simdjson_get_parser(), json, ZSTR_VAL(key), return_value, depth, throw_if_uncountable);
simdjson_parser_cleanup();
if (error) {
if (error == SIMDJSON_PHP_ERR_KEY_COUNT_NOT_COUNTABLE && !throw_if_uncountable) {
RETURN_LONG(0);
}
php_simdjson_throw_jsonexception(error);
RETURN_THROWS();
}
}
PHP_FUNCTION(simdjson_key_exists) {
zend_string *json = NULL;
zend_string *key = NULL;
zend_long depth = SIMDJSON_PARSE_DEFAULT_DEPTH;
ZEND_PARSE_PARAMETERS_START(2, 3)
Z_PARAM_STR(json)
Z_PARAM_STR(key)
Z_PARAM_OPTIONAL
Z_PARAM_LONG(depth)
ZEND_PARSE_PARAMETERS_END();
if (!simdjson_validate_depth(depth, 3)) {
RETURN_THROWS();
}
simdjson_php_error_code error = php_simdjson_key_exists(simdjson_get_parser(), json, ZSTR_VAL(key), depth);
simdjson_parser_cleanup();
switch (error) {
case simdjson::SUCCESS:
RETURN_TRUE;
case simdjson::NO_SUCH_FIELD:
case simdjson::INDEX_OUT_OF_BOUNDS:
case simdjson::INCORRECT_TYPE:
RETURN_FALSE;
default:
php_simdjson_throw_jsonexception(error);
RETURN_THROWS();
}
}
PHP_FUNCTION(simdjson_cleanup) {
if (zend_parse_parameters_none() == FAILURE) {
RETURN_THROWS();
}
simdjson_php_parser *parser = SIMDJSON_G(parser);
if (EXPECTED(parser != NULL)) {
php_simdjson_free_parser(parser);
SIMDJSON_G(parser) = NULL;
}
RETURN_TRUE;
}
PHP_FUNCTION(simdjson_is_valid_utf8) {
zend_string *string = NULL;
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_STR(string)
ZEND_PARSE_PARAMETERS_END();
// If string was already successfully validated, just return true
if (ZSTR_IS_VALID_UTF8(string)) {
RETURN_TRUE;
}
bool is_ok = simdjson::validate_utf8(ZSTR_VAL(string), ZSTR_LEN(string));
if (EXPECTED(is_ok)) {
// String is UTF-8 valid, so we can also set proper flag
GC_ADD_FLAGS(string, IS_STR_VALID_UTF8);
}
RETURN_BOOL(is_ok);
}
static const char *simdjson_get_error_msg(simdjson_error_code error_code) {
switch(error_code) {
case SIMDJSON_ERROR_NONE:
return "No error";
case SIMDJSON_ERROR_DEPTH:
return "Maximum stack depth exceeded";
case SIMDJSON_ERROR_STATE_MISMATCH:
return "State mismatch (invalid or malformed JSON)";
case SIMDJSON_ERROR_CTRL_CHAR:
return "Control character error, possibly incorrectly encoded";
case SIMDJSON_ERROR_SYNTAX:
return "Syntax error";
case SIMDJSON_ERROR_UTF8:
return "Malformed UTF-8 characters, possibly incorrectly encoded";
case SIMDJSON_ERROR_RECURSION:
return "Recursion detected";
case SIMDJSON_ERROR_INF_OR_NAN:
return "Inf and NaN cannot be JSON encoded";
case SIMDJSON_ERROR_UNSUPPORTED_TYPE:
return "Type is not supported";
case SIMDJSON_ERROR_INVALID_PROPERTY_NAME:
return "The decoded property name is invalid";
case SIMDJSON_ERROR_UTF16:
return "Single unpaired UTF-16 surrogate in unicode escape";
case SIMDJSON_ERROR_NON_BACKED_ENUM:
return "Non-backed enums have no default serialization";
case SIMDJSON_ERROR_STREAM_WRITE:
return "Stream write error";
default:
return "Unknown error";
}
}
static zend_always_inline bool simdjson_validate_encode_depth(const zend_long depth, const int arg_num) {
if (UNEXPECTED(depth <= 0)) {
zend_argument_value_error(arg_num, "must be greater than 0");
return false;
}
if (UNEXPECTED(depth > INT_MAX)) {
zend_argument_value_error(arg_num, "must be less than %d", INT_MAX);
return false;
}
return true;
}
#if PHP_VERSION_ID >= 80200
/** For simple types we can just return direct interned string without allocating new strings */
static zend_always_inline bool simdjson_encode_simple(const zval *parameter, zval *return_value, zend_long options) {
switch (Z_TYPE_P(parameter)) {
case IS_NULL:
RETVAL_STR(ZSTR_KNOWN(ZEND_STR_NULL_LOWERCASE));
return true;
case IS_TRUE:
RETVAL_STR(ZSTR_KNOWN(ZEND_STR_TRUE));
return true;
case IS_FALSE:
RETVAL_STR(ZSTR_KNOWN(ZEND_STR_FALSE));
return true;
case IS_LONG:
if (Z_LVAL_P(parameter) >= 0 && Z_LVAL_P(parameter) < 10) {
RETVAL_STR(ZSTR_CHAR((unsigned char) '0' + Z_LVAL_P(parameter)));
return true;
}
break;
case IS_ARRAY:
if (zend_hash_num_elements(Z_ARRVAL_P(parameter)) == 0) {
RETVAL_STR(simdjson_json_empty_array);
return true;
}
break;
}
return false;
}
#endif // PHP_VERSION_ID >= 80200
PHP_FUNCTION(simdjson_encode) {
zval *parameter;
simdjson_encoder encoder = {0};
smart_str buf = {0};
zend_long options = 0;
zend_long depth = 512;
ZEND_PARSE_PARAMETERS_START(1, 3)
Z_PARAM_ZVAL(parameter)
Z_PARAM_OPTIONAL
Z_PARAM_LONG(options)
Z_PARAM_LONG(depth)
ZEND_PARSE_PARAMETERS_END();
if (!simdjson_validate_encode_depth(depth, 3)) {
RETURN_THROWS();
}
#if PHP_VERSION_ID >= 80200
if (!(options & SIMDJSON_APPEND_NEWLINE) && simdjson_encode_simple(parameter, return_value, options)) {
return;
}
#endif
encoder.max_depth = (int)depth;
encoder.options = (int)options;
// Allocate output buffer to smallest size, so we remove checks if buffer was allocated in simdjson_encode_zval method
smart_str_erealloc(&buf, 200);
simdjson_encode_zval(&buf, parameter, &encoder);
if (UNEXPECTED(encoder.error_code != SIMDJSON_ERROR_NONE)) {
efree(buf.s);
zend_throw_exception(simdjson_exception_ce, simdjson_get_error_msg(encoder.error_code), encoder.error_code);
RETURN_THROWS();
}
if (options & SIMDJSON_APPEND_NEWLINE) {
simdjson_smart_str_appendc(&buf, '\n');
}
RETURN_STR(simdjson_smart_str_extract(&buf));
}
PHP_FUNCTION(simdjson_encode_to_stream) {
zval *res;
zval *parameter;
simdjson_encoder encoder = {0};
smart_str buf = {0};
zend_long options = 0;
zend_long depth = 512;
php_stream *stream;
ZEND_PARSE_PARAMETERS_START(2, 4)
Z_PARAM_ZVAL(parameter)
Z_PARAM_RESOURCE(res)
Z_PARAM_OPTIONAL
Z_PARAM_LONG(options)
Z_PARAM_LONG(depth)
ZEND_PARSE_PARAMETERS_END();
ZEND_ASSERT(Z_TYPE_P(res) == IS_RESOURCE);
php_stream_from_res(stream, Z_RES_P(res));
if (!simdjson_validate_encode_depth(depth, 4)) {
RETURN_THROWS();
}
encoder.max_depth = (int)depth;
encoder.options = (int)options;
encoder.stream = stream;
// Allocate output buffer to smallest size, so we remove checks if buffer was allocated in simdjson_encode_zval method
smart_str_erealloc(&buf, 200);
if (simdjson_encode_zval(&buf, parameter, &encoder) == SUCCESS) {
if (options & SIMDJSON_APPEND_NEWLINE) {
simdjson_smart_str_appendc(&buf, '\n');
}
simdjson_encode_write_stream(&buf, &encoder); // write rest
}
efree(buf.s);
if (UNEXPECTED(encoder.error_code != SIMDJSON_ERROR_NONE)) {
zend_throw_exception(simdjson_exception_ce, simdjson_get_error_msg(encoder.error_code), encoder.error_code);
RETURN_THROWS();
}
RETURN_TRUE;
}
PHP_METHOD(SimdJsonBase64Encode, __construct) {
zend_string *string = NULL;
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_STR(string)
ZEND_PARSE_PARAMETERS_END();
return_value = ZEND_THIS;
zend_string_addref(string);
ZVAL_STR(OBJ_PROP_NUM(Z_OBJ_P(ZEND_THIS), 0), string);
}
PHP_METHOD(SimdJsonBase64Encode, jsonSerialize) {
ZEND_PARSE_PARAMETERS_NONE();
zend_string *input = Z_STR_P(OBJ_PROP_NUM(Z_OBJ_P(ZEND_THIS), 0));
size_t encoded_length = simdutf::base64_length_from_binary(ZSTR_LEN(input));
zend_string *result = zend_string_alloc(encoded_length, 0);
simdutf::binary_to_base64(ZSTR_VAL(input), ZSTR_LEN(input), ZSTR_VAL(result));
GC_ADD_FLAGS(result, IS_STR_VALID_UTF8); // base64 encoded string must be always valid UTF-8 string
RETURN_STR(result);
}
/** {{{ PHP_GINIT_FUNCTION
*/
PHP_GINIT_FUNCTION (simdjson) {
#if defined(COMPILE_DL_SIMDJSON) && defined(ZTS)
ZEND_TSRMLS_CACHE_UPDATE();
#endif
}
/* }}} */
/** {{{ PHP_MINIT_FUNCTION
*/
PHP_MINIT_FUNCTION (simdjson) {
#if PHP_VERSION_ID >= 80200
// Interned string for empty array
simdjson_json_empty_array = zend_new_interned_string(zend_string_init("[]", 2, 1));
GC_ADD_FLAGS(simdjson_json_empty_array, IS_STR_VALID_UTF8);
#endif
simdjson_exception_ce = register_class_SimdJsonException(spl_ce_RuntimeException);
simdjson_base64_encode_ce = register_class_SimdJsonBase64Encode(php_json_serializable_ce);
register_simdjson_symbols(0);
return SUCCESS;
}
/* }}} */
/** {{{ PHP_MSHUTDOWN_FUNCTION
*/
PHP_MSHUTDOWN_FUNCTION (simdjson) {
return SUCCESS;
}
/* }}} */
/** {{{ PHP_RINIT_FUNCTION
*/
PHP_RINIT_FUNCTION (simdjson) {
SIMDJSON_G(parser) = NULL;
return SUCCESS;
}
/* }}} */
/** {{{ PHP_RSHUTDOWN_FUNCTION
*/
PHP_RSHUTDOWN_FUNCTION (simdjson) {
simdjson_php_parser *parser = SIMDJSON_G(parser);
if (parser != NULL) {
php_simdjson_free_parser(parser);
SIMDJSON_G(parser) = NULL;
}
return SUCCESS;
}
/* }}} */
/** {{{ PHP_MINFO_FUNCTION
*/
PHP_MINFO_FUNCTION (simdjson) {
php_info_print_table_start();
php_info_print_table_row(2, "simdjson support", "enabled");
php_info_print_table_row(2, "Version", PHP_SIMDJSON_VERSION);
php_info_print_table_row(2, "Support", SIMDJSON_SUPPORT_URL);
php_info_print_table_row(2, "simdjson library version", SIMDJSON_VERSION);
php_info_print_table_row(2, "simdutf library version", SIMDUTF_VERSION);
php_info_print_table_row(2, "Decoder implementation", simdjson::get_active_implementation()->description().c_str());
php_info_print_table_row(2, "Encoder implementation", simdjson_encode_implementation());
php_info_print_table_end();
}
/* }}} */
/** {{{ module depends
*/
zend_module_dep simdjson_deps[] = {
{NULL, NULL, NULL}
};
/* }}} */
/** {{{ simdjson_module_entry
*/
zend_module_entry simdjson_module_entry = {
STANDARD_MODULE_HEADER_EX, NULL,
simdjson_deps,
"simdjson",
ext_functions,
PHP_MINIT(simdjson),
PHP_MSHUTDOWN(simdjson),
PHP_RINIT(simdjson),
PHP_RSHUTDOWN(simdjson),
PHP_MINFO(simdjson),
PHP_SIMDJSON_VERSION,
PHP_MODULE_GLOBALS(simdjson),
PHP_GINIT(simdjson),
NULL,
NULL,
STANDARD_MODULE_PROPERTIES_EX
};
/* }}} */
/** {{{ DL support
*/
#ifdef COMPILE_DL_SIMDJSON
#ifdef ZTS
ZEND_TSRMLS_CACHE_DEFINE();
#endif
ZEND_GET_MODULE(simdjson)
#endif
/* }}} */