1 /*
2  * Copyright 2014-2017 MongoDB, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <php.h>
18 #include <Zend/zend_interfaces.h>
19 #include <ext/standard/php_var.h>
20 #include <zend_smart_str.h>
21 
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25 
26 #include "phongo_compat.h"
27 #include "php_phongo.h"
28 #include "php_bson.h"
29 
30 zend_class_entry* php_phongo_symbol_ce;
31 
32 /* Initialize the object and return whether it was successful. An exception will
33  * be thrown on error. */
php_phongo_symbol_init(php_phongo_symbol_t * intern,const char * symbol,size_t symbol_len)34 static bool php_phongo_symbol_init(php_phongo_symbol_t* intern, const char* symbol, size_t symbol_len) /* {{{ */
35 {
36 	if (strlen(symbol) != (size_t) symbol_len) {
37 		phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Symbol cannot contain null bytes");
38 		return false;
39 	}
40 
41 	intern->symbol     = estrndup(symbol, symbol_len);
42 	intern->symbol_len = symbol_len;
43 
44 	return true;
45 } /* }}} */
46 
47 /* Initialize the object from a HashTable and return whether it was successful.
48  * An exception will be thrown on error. */
php_phongo_symbol_init_from_hash(php_phongo_symbol_t * intern,HashTable * props)49 static bool php_phongo_symbol_init_from_hash(php_phongo_symbol_t* intern, HashTable* props) /* {{{ */
50 {
51 	zval* symbol;
52 
53 	if ((symbol = zend_hash_str_find(props, "symbol", sizeof("symbol") - 1)) && Z_TYPE_P(symbol) == IS_STRING) {
54 		return php_phongo_symbol_init(intern, Z_STRVAL_P(symbol), Z_STRLEN_P(symbol));
55 	}
56 
57 	phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "%s initialization requires \"symbol\" string field", ZSTR_VAL(php_phongo_symbol_ce->name));
58 	return false;
59 } /* }}} */
60 
61 /* {{{ proto string MongoDB\BSON\Symbol::__toString()
62    Return the Symbol's symbol string. */
PHP_METHOD(Symbol,__toString)63 static PHP_METHOD(Symbol, __toString)
64 {
65 	zend_error_handling  error_handling;
66 	php_phongo_symbol_t* intern;
67 
68 	zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling);
69 	if (zend_parse_parameters_none() == FAILURE) {
70 		zend_restore_error_handling(&error_handling);
71 		return;
72 	}
73 	zend_restore_error_handling(&error_handling);
74 
75 	intern = Z_SYMBOL_OBJ_P(getThis());
76 
77 	RETURN_STRINGL(intern->symbol, intern->symbol_len);
78 } /* }}} */
79 
80 /* {{{ proto array MongoDB\BSON\Symbol::jsonSerialize()
81 */
PHP_METHOD(Symbol,jsonSerialize)82 static PHP_METHOD(Symbol, jsonSerialize)
83 {
84 	zend_error_handling  error_handling;
85 	php_phongo_symbol_t* intern;
86 
87 	zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling);
88 	if (zend_parse_parameters_none() == FAILURE) {
89 		zend_restore_error_handling(&error_handling);
90 		return;
91 	}
92 	zend_restore_error_handling(&error_handling);
93 
94 	intern = Z_SYMBOL_OBJ_P(getThis());
95 
96 	array_init_size(return_value, 1);
97 	ADD_ASSOC_STRINGL(return_value, "$symbol", intern->symbol, intern->symbol_len);
98 } /* }}} */
99 
100 /* {{{ proto string MongoDB\BSON\Symbol::serialize()
101 */
PHP_METHOD(Symbol,serialize)102 static PHP_METHOD(Symbol, serialize)
103 {
104 	zend_error_handling  error_handling;
105 	php_phongo_symbol_t* intern;
106 	zval                 retval;
107 	php_serialize_data_t var_hash;
108 	smart_str            buf = { 0 };
109 
110 	intern = Z_SYMBOL_OBJ_P(getThis());
111 
112 	zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling);
113 	if (zend_parse_parameters_none() == FAILURE) {
114 		zend_restore_error_handling(&error_handling);
115 		return;
116 	}
117 	zend_restore_error_handling(&error_handling);
118 
119 	array_init_size(&retval, 1);
120 	ADD_ASSOC_STRINGL(&retval, "symbol", intern->symbol, intern->symbol_len);
121 
122 	PHP_VAR_SERIALIZE_INIT(var_hash);
123 	php_var_serialize(&buf, &retval, &var_hash);
124 	smart_str_0(&buf);
125 	PHP_VAR_SERIALIZE_DESTROY(var_hash);
126 
127 	PHONGO_RETVAL_SMART_STR(buf);
128 
129 	smart_str_free(&buf);
130 	zval_ptr_dtor(&retval);
131 } /* }}} */
132 
133 /* {{{ proto void MongoDB\BSON\Symbol::unserialize(string $serialized)
134 */
PHP_METHOD(Symbol,unserialize)135 static PHP_METHOD(Symbol, unserialize)
136 {
137 	zend_error_handling    error_handling;
138 	php_phongo_symbol_t*   intern;
139 	char*                  serialized;
140 	size_t                 serialized_len;
141 	zval                   props;
142 	php_unserialize_data_t var_hash;
143 
144 	intern = Z_SYMBOL_OBJ_P(getThis());
145 
146 	zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling);
147 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &serialized, &serialized_len) == FAILURE) {
148 		zend_restore_error_handling(&error_handling);
149 		return;
150 	}
151 	zend_restore_error_handling(&error_handling);
152 
153 	PHP_VAR_UNSERIALIZE_INIT(var_hash);
154 	if (!php_var_unserialize(&props, (const unsigned char**) &serialized, (unsigned char*) serialized + serialized_len, &var_hash)) {
155 		zval_ptr_dtor(&props);
156 		phongo_throw_exception(PHONGO_ERROR_UNEXPECTED_VALUE, "%s unserialization failed", ZSTR_VAL(php_phongo_symbol_ce->name));
157 
158 		PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
159 		return;
160 	}
161 	PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
162 
163 	php_phongo_symbol_init_from_hash(intern, HASH_OF(&props));
164 	zval_ptr_dtor(&props);
165 } /* }}} */
166 
167 /* {{{ MongoDB\BSON\Symbol function entries */
168 ZEND_BEGIN_ARG_INFO_EX(ai_Symbol_unserialize, 0, 0, 1)
169 	ZEND_ARG_INFO(0, serialized)
170 ZEND_END_ARG_INFO()
171 
172 ZEND_BEGIN_ARG_INFO_EX(ai_Symbol_void, 0, 0, 0)
173 ZEND_END_ARG_INFO()
174 
175 static zend_function_entry php_phongo_symbol_me[] = {
176 	/* clang-format off */
177 	/* __set_state intentionally missing */
178 	PHP_ME(Symbol, __toString, ai_Symbol_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
179 	PHP_ME(Symbol, jsonSerialize, ai_Symbol_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
180 	PHP_ME(Symbol, serialize, ai_Symbol_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
181 	PHP_ME(Symbol, unserialize, ai_Symbol_unserialize, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
182 	ZEND_NAMED_ME(__construct, PHP_FN(MongoDB_disabled___construct), ai_Symbol_void, ZEND_ACC_PRIVATE | ZEND_ACC_FINAL)
183 	PHP_FE_END
184 	/* clang-format on */
185 };
186 /* }}} */
187 
188 /* {{{ MongoDB\BSON\Symbol object handlers */
189 static zend_object_handlers php_phongo_handler_symbol;
190 
php_phongo_symbol_free_object(zend_object * object)191 static void php_phongo_symbol_free_object(zend_object* object) /* {{{ */
192 {
193 	php_phongo_symbol_t* intern = Z_OBJ_SYMBOL(object);
194 
195 	zend_object_std_dtor(&intern->std);
196 
197 	if (intern->symbol) {
198 		efree(intern->symbol);
199 	}
200 
201 	if (intern->properties) {
202 		zend_hash_destroy(intern->properties);
203 		FREE_HASHTABLE(intern->properties);
204 	}
205 } /* }}} */
206 
php_phongo_symbol_create_object(zend_class_entry * class_type)207 zend_object* php_phongo_symbol_create_object(zend_class_entry* class_type) /* {{{ */
208 {
209 	php_phongo_symbol_t* intern = NULL;
210 
211 	intern = PHONGO_ALLOC_OBJECT_T(php_phongo_symbol_t, class_type);
212 	zend_object_std_init(&intern->std, class_type);
213 	object_properties_init(&intern->std, class_type);
214 
215 	intern->std.handlers = &php_phongo_handler_symbol;
216 
217 	return &intern->std;
218 } /* }}} */
219 
php_phongo_symbol_clone_object(phongo_compat_object_handler_type * object)220 static zend_object* php_phongo_symbol_clone_object(phongo_compat_object_handler_type* object) /* {{{ */
221 {
222 	php_phongo_symbol_t* intern;
223 	php_phongo_symbol_t* new_intern;
224 	zend_object*         new_object;
225 
226 	intern     = Z_OBJ_SYMBOL(PHONGO_COMPAT_GET_OBJ(object));
227 	new_object = php_phongo_symbol_create_object(PHONGO_COMPAT_GET_OBJ(object)->ce);
228 
229 	new_intern = Z_OBJ_SYMBOL(new_object);
230 	zend_objects_clone_members(&new_intern->std, &intern->std);
231 
232 	php_phongo_symbol_init(new_intern, intern->symbol, intern->symbol_len);
233 
234 	return new_object;
235 } /* }}} */
236 
php_phongo_symbol_compare_objects(zval * o1,zval * o2)237 static int php_phongo_symbol_compare_objects(zval* o1, zval* o2) /* {{{ */
238 {
239 	php_phongo_symbol_t *intern1, *intern2;
240 
241 	ZEND_COMPARE_OBJECTS_FALLBACK(o1, o2);
242 
243 	intern1 = Z_SYMBOL_OBJ_P(o1);
244 	intern2 = Z_SYMBOL_OBJ_P(o2);
245 
246 	return strcmp(intern1->symbol, intern2->symbol);
247 } /* }}} */
248 
php_phongo_symbol_get_properties_hash(phongo_compat_object_handler_type * object,bool is_debug)249 HashTable* php_phongo_symbol_get_properties_hash(phongo_compat_object_handler_type* object, bool is_debug) /* {{{ */
250 {
251 	php_phongo_symbol_t* intern;
252 	HashTable*           props;
253 
254 	intern = Z_OBJ_SYMBOL(PHONGO_COMPAT_GET_OBJ(object));
255 
256 	PHONGO_GET_PROPERTY_HASH_INIT_PROPS(is_debug, intern, props, 2);
257 
258 	if (!intern->symbol) {
259 		return props;
260 	}
261 
262 	{
263 		zval symbol;
264 
265 		ZVAL_STRING(&symbol, intern->symbol);
266 		zend_hash_str_update(props, "symbol", sizeof("symbol") - 1, &symbol);
267 	}
268 
269 	return props;
270 } /* }}} */
271 
php_phongo_symbol_get_debug_info(phongo_compat_object_handler_type * object,int * is_temp)272 static HashTable* php_phongo_symbol_get_debug_info(phongo_compat_object_handler_type* object, int* is_temp) /* {{{ */
273 {
274 	*is_temp = 1;
275 	return php_phongo_symbol_get_properties_hash(object, true);
276 } /* }}} */
277 
php_phongo_symbol_get_properties(phongo_compat_object_handler_type * object)278 static HashTable* php_phongo_symbol_get_properties(phongo_compat_object_handler_type* object) /* {{{ */
279 {
280 	return php_phongo_symbol_get_properties_hash(object, false);
281 } /* }}} */
282 /* }}} */
283 
php_phongo_symbol_init_ce(INIT_FUNC_ARGS)284 void php_phongo_symbol_init_ce(INIT_FUNC_ARGS) /* {{{ */
285 {
286 	zend_class_entry ce;
287 
288 	INIT_NS_CLASS_ENTRY(ce, "MongoDB\\BSON", "Symbol", php_phongo_symbol_me);
289 	php_phongo_symbol_ce                = zend_register_internal_class(&ce);
290 	php_phongo_symbol_ce->create_object = php_phongo_symbol_create_object;
291 	PHONGO_CE_FINAL(php_phongo_symbol_ce);
292 
293 	zend_class_implements(php_phongo_symbol_ce, 1, php_phongo_json_serializable_ce);
294 	zend_class_implements(php_phongo_symbol_ce, 1, php_phongo_type_ce);
295 	zend_class_implements(php_phongo_symbol_ce, 1, zend_ce_serializable);
296 
297 	memcpy(&php_phongo_handler_symbol, phongo_get_std_object_handlers(), sizeof(zend_object_handlers));
298 	PHONGO_COMPAT_SET_COMPARE_OBJECTS_HANDLER(symbol);
299 	php_phongo_handler_symbol.clone_obj      = php_phongo_symbol_clone_object;
300 	php_phongo_handler_symbol.get_debug_info = php_phongo_symbol_get_debug_info;
301 	php_phongo_handler_symbol.get_properties = php_phongo_symbol_get_properties;
302 	php_phongo_handler_symbol.free_obj       = php_phongo_symbol_free_object;
303 	php_phongo_handler_symbol.offset         = XtOffsetOf(php_phongo_symbol_t, std);
304 } /* }}} */
305 
306 /*
307  * Local variables:
308  * tab-width: 4
309  * c-basic-offset: 4
310  * End:
311  * vim600: noet sw=4 ts=4 fdm=marker
312  * vim<600: noet sw=4 ts=4
313  */
314