forked from swoole/swoole-src
/
swoole_phpng.c
186 lines (163 loc) · 5.06 KB
/
swoole_phpng.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
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
/*
+----------------------------------------------------------------------+
| Swoole |
+----------------------------------------------------------------------+
| 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 |
| If you did not receive a copy of the Apache2.0 license and are unable|
| to obtain it through the world-wide-web, please send a note to |
| license@swoole.com so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Tianfeng Han <mikan.tenny@gmail.com> |
+----------------------------------------------------------------------+
*/
#include "php_swoole.h"
#if PHP_MAJOR_VERSION >= 7
inline int Z_BVAL_P(zval *v)
{
if (Z_TYPE_P(v) == IS_TRUE)
{
return 1;
}
else
{
return 0;
}
}
inline int sw_add_assoc_stringl_ex(zval *arg, const char *key, size_t key_len, char *str, size_t length, int duplicate)
{
return add_assoc_stringl_ex(arg, key, key_len, str, length);
}
inline char * sw_php_format_date(char *format, size_t format_len, time_t ts, int localtime) {
zend_string *time = php_format_date(format, format_len, ts, localtime);
char *return_str = (char*) emalloc(time->len);
memcpy(return_str, time->val, time->len);
zend_string_release(time);
return return_str;
}
inline char * sw_php_url_encode(char *value, size_t value_len, int* exten) {
zend_string *str = php_url_encode(value, value_len);
*exten = str->len;
char *return_str = (char*) emalloc(str->len);
memcpy(return_str, str->val, str->len);
zend_string_release(str);
return return_str;
}
inline zval * sw_zend_read_property(zend_class_entry *class_ptr, zval *obj, char *s, int len, int what) {
zval rv;
return zend_read_property(class_ptr, obj, s, len, 0, &rv TSRMLS_CC);
}
inline int sw_zend_is_callable(zval *cb, int a, char **name) {
zend_string *key;
int ret = zend_is_callable(cb, a, &key);
char * tmp = (char *)emalloc(key->len);
memcpy(tmp, key->val, key->len);
*name = tmp;
return ret;
}
inline int sw_zend_hash_del(HashTable *ht, char *k, int len) {
zval key;
ZVAL_STRING(&key, k);
return zend_hash_del(ht, Z_STR(key));
}
inline int sw_zend_hash_add(HashTable *ht, char *k, int len, void *pData, int datasize, void **pDest) {
zval key;
ZVAL_STRING(&key, k);
zval **real_p = pData;
return zend_hash_add(ht, Z_STR(key), *real_p) ? SUCCESS : FAILURE;
}
inline int sw_zend_hash_index_update(HashTable *ht, int key, void *pData, int datasize, void **pDest) {
zval **real_p = pData;
return zend_hash_index_update(ht, key, *real_p) ? SUCCESS : FAILURE;
}
inline int sw_zend_hash_update(HashTable *ht, char *k, int len, void * val, int size, void *ptr) {
zval key;
ZVAL_STRING(&key, k);
return zend_hash_update(ht, Z_STR(key), val) ? SUCCESS : FAILURE;
}
inline int sw_zend_hash_get_current_key(HashTable *ht, char **key, uint32_t *keylen, ulong *num)
{
zval str_key;
int type = zend_hash_get_current_key(ht, &Z_STR(str_key), (zend_ulong* ) num);
*key = Z_STRVAL(str_key);
*keylen = Z_STRLEN(str_key);
return type;
}
inline int sw_zend_hash_exists(HashTable *ht, char *k, int len)
{
zval key;
ZVAL_STRING(&key, k);
zval *value = zend_hash_find(ht, Z_STR(key));
if (value == NULL)
{
return FAILURE;
}
else
{
return SUCCESS;
}
}
#else
inline int SW_Z_TYPE_P(zval *z)
{
if(Z_TYPE_P(z)==IS_BOOL)
{
if((uint8_t) Z_BVAL_P(z)==1)
{
return IS_TRUE;
}
else
{
return 0;
}
}
else
{
return Z_TYPE_P(z);
}
}
#endif
inline int sw_zend_hash_find(HashTable *ht, char *k, int len, void **v)
{
// char _key[128];
// zend_string *key;
//
// if (sizeof (zend_string) + len > sizeof (_key)) {
// key = emalloc(sizeof (zend_string) + len);
// } else {
// key = _key;
// }
//
// key->len = len;
// memcpy(key->val, k, len);
// key->val[len] = 0;
#if PHP_MAJOR_VERSION < 7
zval **tmp = NULL;
if(zend_hash_find(ht, k,len, (void **) &tmp) == SUCCESS)
{
*v = *tmp;
return SUCCESS;
}
else
{
*v = NULL;
return FAILURE;
}
#else
zval key;
ZVAL_STRINGL(&key, k, len - 1);
zval *value = zend_hash_find(ht, Z_STR(key));
if (value == NULL)
{
return FAILURE;
}
else
{
*v = (void *) value;
v = (void *) value;
return SUCCESS;
}
#endif
}