-
Notifications
You must be signed in to change notification settings - Fork 17
/
php_convert.c
193 lines (174 loc) · 6.2 KB
/
php_convert.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
187
188
189
190
191
192
193
/*
+----------------------------------------------------------------------+
| PHP Version 7 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2016 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: |
| http://www.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: Santiago Lizardo <[email protected]> |
+----------------------------------------------------------------------+
*/
#ifdef PHP_WIN32
#include <windows.h>
#endif
#include "php.h"
#include "php_convert.h"
void c_array_to_php_array(void *c_array, int num, zval *php_array, int type) {
zval val;
int i;
for (i = 0; i < num; i++) {
switch (type) {
case C_INT_TO_PHP_LONG:
ZVAL_LONG(&val, (int) ((int*) c_array)[i]);
break;
case C_UINT_TO_PHP_LONG:
ZVAL_LONG(&val, (unsigned int) ((unsigned int*) c_array)[i]);
break;
case C_LONG_TO_PHP_LONG:
ZVAL_LONG(&val, ((long*) c_array)[i]);
break;
case C_FLOAT_TO_PHP_DOUBLE:
ZVAL_DOUBLE(&val, (float) ((float*) c_array)[i]);
break;
case C_DOUBLE_TO_PHP_DOUBLE:
ZVAL_DOUBLE(&val, (double) ((double*) c_array)[i]);
break;
case C_BOOLEAN_TO_PHP_BOOLEAN:
ZVAL_BOOL(&val, (unsigned char) ((unsigned char*) c_array)[i]);
break;
case C_CHAR_TO_PHP_LONG:
ZVAL_LONG(&val, (char) ((char*) c_array)[i]);
break;
case C_USHORT_TO_PHP_LONG:
ZVAL_LONG(&val, (unsigned short) ((unsigned short*) c_array)[i]);
break;
}
zend_hash_next_index_insert(Z_ARRVAL_P(php_array), &val);
}
}
void *php_array_to_c_array(zval *param, int type, int size, int *array_size) {
zend_array *param_ht = HASH_OF(param);
zval *cur = NULL;
void *params;
int i, tmp_size = zend_hash_num_elements(param_ht);
params = (void *) emalloc(size * tmp_size);
i = 0;
ZEND_HASH_FOREACH_VAL(param_ht, cur) {
if (Z_TYPE_P(cur) == IS_ARRAY) {
int new_array_size;
void *array = php_array_to_c_array(cur, type, size, &new_array_size);
params = erealloc(params, (tmp_size + new_array_size) * size);
memcpy(&((char*) params)[i * size], array, new_array_size * size);
i += (new_array_size - 1);
efree(array);
} else {
switch (type) {
case TO_C_FLOAT:
convert_to_double(cur);
((float*) params)[i] = (float) Z_DVAL_P(cur);
break;
case TO_C_DOUBLE:
convert_to_double(cur);
((double*) params)[i] = Z_DVAL_P(cur);
break;
case TO_C_INT:
convert_to_long(cur);
((int*) params)[i] = (int) Z_LVAL_P(cur);
break;
case TO_C_LONG:
convert_to_long(cur);
((long*) params)[i] = Z_LVAL_P(cur);
break;
case TO_C_UCHAR:
convert_to_long(cur);
((unsigned char*) params)[i] = (unsigned char) Z_LVAL_P(cur);
break;
case TO_C_SCHAR:
convert_to_long(cur);
((signed char*) params)[i] = (signed char) Z_LVAL_P(cur);
break;
case TO_C_USHORT:
convert_to_long(cur);
((unsigned short*) params)[i] = (unsigned short) Z_LVAL_P(cur);
break;
case TO_C_SHORT:
convert_to_long(cur);
((short*) params)[i] = (short) Z_LVAL_P(cur);
break;
case TO_C_UINT:
convert_to_long(cur);
((unsigned int*) params)[i] = (unsigned int) Z_LVAL_P(cur);
break;
case TO_C_STRING:
convert_to_string(cur);
((char **) params)[i] = estrdup(Z_STRVAL_P(cur));
}
}
i++;
}
ZEND_HASH_FOREACH_END();
if (array_size != NULL)
*array_size = i;
return params;
}
int gl_pixel_size(GLenum format) {
switch (format) {
// case GL_COLOR_INDEX:
case GL_RED:
case GL_GREEN:
case GL_BLUE:
case GL_ALPHA:
case GL_STENCIL_INDEX:
case GL_DEPTH_COMPONENT:
// case GL_LUMINANCE:
return 1;
// case GL_LUMINANCE_ALPHA:
//: return 2;
case GL_RGB:
#ifdef GL_BGR_EXT
case GL_BGR_EXT:
#endif
return 3;
case GL_RGBA:
#ifdef GL_BGRA_EXT
case GL_BGRA_EXT:
#endif
#ifdef GL_ABGR_EXT
case GL_ABGR_EXT:
#endif
return 4;
case 1:
case 2:
case 3:
case 4:
return format;
default:
return -1;
}
}
int gl_type_size(GLenum type) {
switch (type) {
case GL_BYTE:
case GL_UNSIGNED_BYTE:
return sizeof (GLbyte);
case GL_SHORT:
case GL_UNSIGNED_SHORT:
return sizeof (GLshort);
case GL_INT:
case GL_UNSIGNED_INT:
return sizeof (GLint);
case GL_FLOAT:
return sizeof (GLfloat);
// case GL_BITMAP:
// return sizeof (GLbitfield);
default:
return 1;
}
}