forked from chobie/php-protocolbuffers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
core_inl.h
503 lines (425 loc) · 12.6 KB
/
core_inl.h
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
#ifndef PHP_PROTOCOLBUFFERS_HELPER_INL_H
#define PHP_PROTOCOLBUFFERS_HELPER_INL_H
static inline int is_utf8(const char *s, int len)
{
int i;
const unsigned char *bytes = (const unsigned char *)s;
if (len < 1) {
/* NOTE: always return 1 when passed string is null */
return 1;
}
for (i = 0; i < len; i++) {
/* ASCII */
if (bytes[0] == 0x09 ||
bytes[0] == 0x0A ||
bytes[0] == 0x0D ||
(bytes[0] >= 0x20 && bytes[0] <= 0x7E)
) {
bytes += 1;
continue;
}
/* non-overlong 2-byte */
if (((i+1) <= len) &&
(bytes[0] >= 0xC2 && bytes[0] <= 0xDF) &&
(bytes[1] >= 0x80 && bytes[1] <= 0xBF)) {
bytes += 2;
i+=1;
continue;
}
/* excluding overlongs */
if (((i+2) <= len) &&
bytes[0] == 0xE0 &&
(bytes[1] >= 0xA0 && bytes[1] <= 0xBF) &&
(bytes[2] >= 0x80 && bytes[2] <= 0xBF)
) {
bytes += 3;
i+=2;
continue;
}
/* straight 3-byte */
if (((i+2) <= len) &&
((bytes[0] >= 0xE1 && bytes[0] <= 0xEC) ||
bytes[0] == 0xEE ||
bytes[0] == 0xEF) &&
(bytes[1] >= 0x80 && bytes[1] <= 0xBF) &&
(bytes[2] >= 0x80 && bytes[2] <= 0xBF)
) {
bytes += 3;
i+=2;
continue;
}
/* excluding surrogates */
if (((i+2) <= len) &&
bytes[0] == 0xED &&
(bytes[1] >= 0x80 && bytes[1] <= 0x9F) &&
(bytes[2] >= 0x80 && bytes[2] <= 0xBF)
) {
bytes += 3;
i+=2;
continue;
}
/* planes 1-3 */
if (((i+3) <= len) &&
bytes[0] == 0xF0 &&
(bytes[1] >= 0x90 && bytes[1] <= 0xBF) &&
(bytes[2] >= 0x80 && bytes[2] <= 0xBF) &&
(bytes[3] >= 0x80 && bytes[3] <= 0xBF)
) {
bytes += 4;
i+=3;
continue;
}
/* planes 4-15 */
if (((i+3) <= len) &&
bytes[0] >= 0xF1 && bytes[0] <= 0xF3 &&
bytes[1] >= 0x80 && bytes[1] <= 0xBF &&
bytes[2] >= 0x80 && bytes[2] <= 0xBF &&
bytes[3] >= 0x80 && bytes[3] <= 0xBF
) {
bytes += 4;
i+=3;
continue;
}
/* plane 16 */
if (((i+3) <= len) &&
bytes[0] == 0xF4 &&
(bytes[1] >= 0x80 && bytes[1] <= 0x8F) &&
(bytes[2] >= 0x80 && bytes[2] <= 0xBF) &&
(bytes[3] >= 0x80 && bytes[3] <= 0xBF)
) {
bytes += 4;
i+=3;
continue;
}
return 0;
}
return 1;
}
static inline uint32_t zigzag_encode32(int32_t n) {
// Note: the right-shift must be arithmetic
return (n << 1) ^ (n >> 31);
}
static inline int32_t zigzag_decode32(uint32_t n) {
return (n >> 1) ^ - (int32_t)(n & 1);
}
static inline uint64_t zigzag_encode64(int64_t n) {
// Note: the right-shift must be arithmetic
return (n << 1) ^ (n >> 63);
}
static inline int64_t zigzag_decode64(uint64_t n) {
return (n >> 1) ^ - (int64_t)(n & 1);
}
static inline uint64_t encode_double(double value) {
union {
double d;
uint64_t v;
} u;
u.d = value;
return u.v;
}
static inline double decode_double(uint64_t value) {
union {
double d;
uint64_t v;
} u;
u.v = value;
return u.d;
}
static inline uint32_t encode_float(float value) {
union {
float f;
uint32_t v;
} u;
u.f = value;
return u.v;
}
static inline float decode_float(int32_t value) {
union {
float f;
uint32_t v;
} u;
u.v = value;
return u.f;
}
static inline int php_protocolbuffers_get_lval_from_hash_by_tag(HashTable *proto, ulong tag, const char *name, size_t name_len TSRMLS_DC)
{
zval **d, **dd;
if (zend_hash_index_find(proto, tag, (void **)&d) != SUCCESS) {
return 0;
}
if (Z_TYPE_PP(d) != IS_ARRAY) {
return 0;
}
if (zend_hash_find(Z_ARRVAL_PP(d), (char*)name, name_len, (void **)&dd) == SUCCESS) {
return Z_LVAL_PP(dd);
}
return 0;
}
static inline int php_protocolbuffers_get_zval_from_hash_by_tag(HashTable *proto, ulong tag, const char *name, size_t name_len, zval **result TSRMLS_DC)
{
zval **d, **dd;
if (zend_hash_index_find(proto, tag, (void **)&d) != SUCCESS) {
return 0;
}
if (Z_TYPE_PP(d) != IS_ARRAY) {
return 0;
}
if (zend_hash_find(Z_ARRVAL_PP(d), (char*)name, name_len, (void **)&dd) == SUCCESS) {
*result = *dd;
return 1;
}
return 0;
}
static inline const char* ReadVarint32FromArray(const char* buffer, uint* value, const char* buffer_end) {
/**
* Fast path: We have enough bytes left in the buffer to guarantee that
* this read won't cross the end, so we can skip the checks.
*/
const char* ptr = buffer;
int b;
int result;
int i;
if (GOOGLE_PREDICT_TRUE(buffer < buffer_end) && (uint)*ptr < 0x80) {
*value = (uint)*buffer;
ptr++;
return ptr;
}
b = *(ptr++); result = (b & 0x7F) ; if (!(b & 0x80)) goto done;
b = *(ptr++); result |= (b & 0x7F) << 7; if (!(b & 0x80)) goto done;
b = *(ptr++); result |= (b & 0x7F) << 14; if (!(b & 0x80)) goto done;
b = *(ptr++); result |= (b & 0x7F) << 21; if (!(b & 0x80)) goto done;
b = *(ptr++); result |= b << 28; if (!(b & 0x80)) goto done;
/**
* If the input is larger than 32 bits, we still need to read it all
* and discard the high-order bits.
*/
//for (i = 0; i < kMaxVarintBytes - kMaxVarint32Bytes; i++) {
for (i = 0; i < 10 - 5; i++) {
b = *(ptr++); if (!(b & 0x80)) goto done;
}
/**
* We have overrun the maximum size of a varint (10 bytes). Assume
* the data is corrupt.
*/
return NULL;
done:
*value = result;
return ptr;
}
static inline const char* ReadVarint64FromArray(const char* buffer, uint64_t* value, const char* buffer_end)
{
const char* ptr = buffer;
uint32_t b;
/**
* Splitting into 32-bit pieces gives better performance on 32-bit
* processors.
*/
uint32_t part0 = 0, part1 = 0, part2 = 0;
b = *(ptr++); part0 = (b & 0x7F) ; if (!(b & 0x80)) goto done;
b = *(ptr++); part0 |= (b & 0x7F) << 7; if (!(b & 0x80)) goto done;
b = *(ptr++); part0 |= (b & 0x7F) << 14; if (!(b & 0x80)) goto done;
b = *(ptr++); part0 |= (b & 0x7F) << 21; if (!(b & 0x80)) goto done;
b = *(ptr++); part1 = (b & 0x7F) ; if (!(b & 0x80)) goto done;
b = *(ptr++); part1 |= (b & 0x7F) << 7; if (!(b & 0x80)) goto done;
b = *(ptr++); part1 |= (b & 0x7F) << 14; if (!(b & 0x80)) goto done;
b = *(ptr++); part1 |= (b & 0x7F) << 21; if (!(b & 0x80)) goto done;
b = *(ptr++); part2 = (b & 0x7F) ; if (!(b & 0x80)) goto done;
b = *(ptr++); part2 |= (b & 0x7F) << 7; if (!(b & 0x80)) goto done;
/**
* We have overrun the maximum size of a varint (10 bytes). The data
* must be corrupt.
*/
return "";
done:
*value = (uint64_t)(part0) |
((uint64_t)(part1) << 28) |
((uint64_t)(part2) << 56);
return ptr;
}
static inline php_protocolbuffers_scheme *php_protocolbuffers_search_scheme_by_tag(php_protocolbuffers_scheme* scheme, uint scheme_size, uint tag)
{
int i = 0;
for (i = 0; i < scheme_size; i++) {
if (scheme[i].tag == tag) {
return &scheme[i];
}
}
return NULL;
}
static inline void php_protocolbuffers_decode_add_value_and_consider_repeated(php_protocolbuffers_scheme_container *container, php_protocolbuffers_scheme *s, HashTable *hresult, zval *dz TSRMLS_DC)
{
char *name;
int name_len;
ulong hash;
if (container->use_single_property < 1) {
name = s->mangled_name;
name_len = s->mangled_name_len;
hash = s->mangled_name_h;
} else {
name = s->name;
name_len = s->name_len;
hash = s->name_h;
}
if (s->repeated) {
if (!zend_hash_quick_exists(hresult, name, name_len, hash)) {
zval *arr;
MAKE_STD_ZVAL(arr);
array_init(arr);
zend_hash_next_index_insert(Z_ARRVAL_P(arr), (void *)&dz, sizeof(dz), NULL);
Z_ADDREF_P(dz);
zend_hash_quick_update(hresult, name, name_len, hash, (void **)&arr, sizeof(arr), NULL);
Z_ADDREF_P(arr);
zval_ptr_dtor(&arr);
} else {
zval **arr2;
if (zend_hash_quick_find(hresult, name, name_len, hash, (void **)&arr2) == SUCCESS) {
if (Z_TYPE_PP(arr2) == IS_NULL) {
array_init(*arr2);
}
zend_hash_next_index_insert(Z_ARRVAL_PP(arr2), (void *)&dz, sizeof(dz), NULL);
//Z_ADDREF_P(dz);
}
}
} else {
//Z_ADDREF_P(dz);
zend_hash_quick_update(hresult, name, name_len, hash, (void **)&dz, sizeof(dz), NULL);
}
}
static inline int php_protocolbuffers_process_varint(INTERNAL_FUNCTION_PARAMETERS, int wiretype, int tag, php_protocolbuffers_scheme_container *container, php_protocolbuffers_scheme *scheme, uint64_t value, HashTable *hresult)
{
pbf __payload = {0};
zval *dz = NULL;
if (scheme == NULL) {
if (container->process_unknown_fields > 0) {
MAKE_STD_ZVAL(dz);
php_protocolbuffers_process_unknown_field(INTERNAL_FUNCTION_PARAM_PASSTHRU, container, hresult, dz, tag, wiretype, value);
} else {
/* NOTE: skip unknown field. nothing to do. */
}
} else {
MAKE_STD_ZVAL(dz);
switch (scheme->type) {
case TYPE_BOOL:
ZVAL_BOOL(dz, value);
break;
case TYPE_INT32:
case TYPE_ENUM:
__payload.type = TYPE_INT32;__payload.value.int32 = (int32_t)value;
break;
case TYPE_UINT32:
__payload.type = TYPE_UINT32;__payload.value.uint32 = (uint32_t)value;
break;
case TYPE_SINT32:
__payload.type = TYPE_INT32;__payload.value.int32 = (int32_t)zigzag_decode32(value);
break;
case TYPE_INT64:
__payload.type = TYPE_INT64;__payload.value.int64 = (int64_t)value;
break;
case TYPE_SINT64:
__payload.type = TYPE_INT64;__payload.value.int64 = (int64_t)zigzag_decode64(value);
break;
case TYPE_UINT64:
__payload.type = TYPE_UINT64;__payload.value.uint64 = (uint64_t)value;
break;
default:
zval_ptr_dtor(&dz);
return 0;
}
if (scheme->type != TYPE_BOOL) {
php_protocolbuffers_format_string(dz, &__payload TSRMLS_CC);
}
php_protocolbuffers_decode_add_value_and_consider_repeated(container, scheme, hresult, dz TSRMLS_CC);
}
return 1;
}
static inline int php_protocolbuffers_process_fixed64(INTERNAL_FUNCTION_PARAMETERS, int wiretype, int tag, php_protocolbuffers_scheme_container *container, php_protocolbuffers_scheme *scheme, const char *data, HashTable *hresult)
{
pbf __payload = {0};
zval *dz = NULL;
if (scheme == NULL) {
if (container->process_unknown_fields > 0) {
php_protocolbuffers_process_unknown_field_bytes(INTERNAL_FUNCTION_PARAM_PASSTHRU, container, hresult, tag, wiretype, (uint8_t *)data, 8);
} else {
/* skip unknown field */
}
} else {
MAKE_STD_ZVAL(dz);
switch (scheme->type) {
case TYPE_DOUBLE:
{
uint64_t v;
double d;
memcpy(&v, data, 8);
d = decode_double(v);
__payload.type = TYPE_DOUBLE;__payload.value.d = d;
php_protocolbuffers_format_string(dz, &__payload TSRMLS_CC);
}
break;
case TYPE_FIXED64:
{
uint64_t v;
memcpy(&v, data, 8);
__payload.type = TYPE_UINT64;__payload.value.uint64 = v;
php_protocolbuffers_format_string(dz, &__payload TSRMLS_CC);
}
break;
case TYPE_SFIXED64:
{
int64_t l;
memcpy(&l, data, 8);
__payload.type = TYPE_INT64;__payload.value.int64 = l;
php_protocolbuffers_format_string(dz, &__payload TSRMLS_CC);
}
break;
default:
zval_ptr_dtor(&dz);
return 0;
}
php_protocolbuffers_decode_add_value_and_consider_repeated(container, scheme, hresult, dz TSRMLS_CC);
}
return 1;
}
static inline int php_protocolbuffers_process_fixed32(INTERNAL_FUNCTION_PARAMETERS, int wiretype, int tag, php_protocolbuffers_scheme_container *container, php_protocolbuffers_scheme *scheme, const char *data, HashTable *hresult)
{
pbf __payload = {0};
zval *dz = NULL;
if (scheme == NULL) {
if (container->process_unknown_fields > 0) {
php_protocolbuffers_process_unknown_field_bytes(INTERNAL_FUNCTION_PARAM_PASSTHRU, container, hresult, tag, wiretype, (uint8_t*)data, 4);
} else {
/* skip unknown field */
}
} else {
MAKE_STD_ZVAL(dz);
switch(scheme->type) {
case TYPE_FLOAT:
{
float a = 0;
memcpy(&a, data, 4);
__payload.type = TYPE_FLOAT;__payload.value.f = a;
}
break;
case TYPE_SFIXED32:
{
int32_t l = 0;
memcpy(&l, data, 4);
__payload.type = TYPE_SINT32;__payload.value.int32 = l;
}
break;
case TYPE_FIXED32:
{
uint32_t l = 0;
memcpy(&l, data, 4);
__payload.type = TYPE_UINT32;__payload.value.uint32 = l;
}
break;
default:
zval_ptr_dtor(&dz);
return 0;
}
php_protocolbuffers_format_string(dz, &__payload TSRMLS_CC);
php_protocolbuffers_decode_add_value_and_consider_repeated(container, scheme, hresult, dz TSRMLS_CC);
}
return 1;
}
#endif