-
Notifications
You must be signed in to change notification settings - Fork 2
/
php_trace_zval.c
156 lines (129 loc) · 5.53 KB
/
php_trace_zval.c
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
/*
+----------------------------------------------------------------------+
| PHP Version 7 |
+----------------------------------------------------------------------+
| Copyright (c) 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: |
| https://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 |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: krakjoe |
+----------------------------------------------------------------------+
*/
#ifndef HAVE_PHP_TRACE_ZVAL
#define HAVE_PHP_TRACE_ZVAL
#include <php.h>
#include <php_trace.h>
void php_trace_zval_dup(php_trace_context_t *context, zval *argv, uint32_t argc) {
zval *it = argv,
*end = argv + argc;
while (it < end) {
if (!Z_COUNTED_P(it)) {
it++;
continue;
}
switch (Z_TYPE_P(it)) {
case IS_STRING: {
zend_string *str = php_trace_get_string(context, Z_STR_P(it), context->strData);
if (str) {
ZVAL_STR(it, str);
} else {
ZVAL_NULL(it);
}
} break;
case IS_ARRAY: {
HashTable *table = calloc(1, sizeof(HashTable));
if (!table || php_trace_get_symbol(context, Z_ARRVAL_P(it), table, sizeof(HashTable)) != SUCCESS) {
if (table) {
free(table);
}
ZVAL_NULL(it);
} else if (context->arData){
void *arData = calloc(table->nNumOfElements, sizeof(Bucket));
if (!arData || php_trace_get_symbol(context, table->arData, arData, sizeof(Bucket) * table->nNumOfElements) != SUCCESS) {
if (arData) {
free(arData);
}
free(table);
ZVAL_NULL(it);
} else {
Bucket *bit = arData,
*bend = bit + table->nNumOfElements;
while (bit < bend) {
if (Z_ISUNDEF(bit->val)) {
bit++;
continue;
}
if (bit->key) {
bit->key = php_trace_get_string(context, bit->key, 1);
}
if (Z_COUNTED(bit->val)) {
php_trace_zval_dup(context, &bit->val, 1);
}
bit++;
}
table->arData = arData;
Z_ARRVAL_P(it) = table;
}
} else {
Z_ARRVAL_P(it) = table;
}
} break;
case IS_OBJECT: {
zend_object *object = php_trace_get_object(context, it, Z_OBJ_P(it));
if (object){
ZVAL_OBJ(it, object);
} else {
ZVAL_NULL(it);
}
} break;
}
it++;
}
}
void php_trace_zval_dtor(php_trace_context_t *context, zval *argv, uint32_t argc) {
zval *it = argv,
*end = argv + argc;
while (it < end) {
if (!Z_COUNTED_P(it)) {
it++;
continue;
}
switch (Z_TYPE_P(it)) {
case IS_STRING:
free(Z_STR_P(it));
break;
case IS_ARRAY: {
HashTable *table = Z_ARRVAL_P(it);
if (table) {
if (context->arData) {
Bucket *bit = table->arData,
*bend = bit + table->nNumOfElements;
while (bit < bend) {
if (Z_ISUNDEF(bit->val)) {
bit++;
continue;
}
if (bit->key) {
free(bit->key);
}
if (Z_COUNTED(bit->val)) {
php_trace_zval_dtor(context, &bit->val, 1);
}
bit++;
}
free(table->arData);
}
free(table);
}
} break;
}
it++;
}
}
#endif