1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | This source file is subject to version 3.01 of the PHP license,      |
6    | that is bundled with this package in the file LICENSE, and is        |
7    | available through the world-wide-web at the following url:           |
8    | http://www.php.net/license/3_01.txt                                  |
9    | If you did not receive a copy of the PHP license and are unable to   |
10    | obtain it through the world-wide-web, please send a note to          |
11    | license@php.net so we can mail you a copy immediately.               |
12    +----------------------------------------------------------------------+
13    | Authors: Gustavo Lopes <cataphract@php.net>                          |
14    +----------------------------------------------------------------------+
15  */
16 
17 #include "transliterator_class.h"
18 #include "php_intl.h"
19 #include "transliterator_methods.h"
20 #include "intl_error.h"
21 #include "intl_convert.h"
22 #include "intl_data.h"
23 
24 #include <unicode/utrans.h>
25 
26 zend_class_entry *Transliterator_ce_ptr = NULL;
27 
28 zend_object_handlers Transliterator_handlers;
29 
30 /* {{{ int transliterator_object_construct( zval *object, UTransliterator *utrans, UErrorCode *status )
31  * Initialize internals of Transliterator_object.
32  */
transliterator_object_construct(zval * object,UTransliterator * utrans,UErrorCode * status)33 int transliterator_object_construct( zval *object,
34 									 UTransliterator *utrans,
35 									 UErrorCode *status )
36 {
37 	const UChar           *ustr_id;
38 	int32_t               ustr_id_len;
39 	zend_string           *u8str;
40 	zval                  tmp;
41 	Transliterator_object *to;
42 
43 	TRANSLITERATOR_METHOD_FETCH_OBJECT_NO_CHECK;
44 
45 	assert( to->utrans == NULL );
46 	/* this assignment must happen before any return with failure because the
47 	 * caller relies on it always being made (so it can just destroy the object
48 	 * to close the transliterator) */
49 	to->utrans = utrans;
50 
51 	ustr_id = utrans_getUnicodeID( utrans, &ustr_id_len );
52 	u8str = intl_convert_utf16_to_utf8(ustr_id, (int ) ustr_id_len, status );
53 	if( !u8str )
54 	{
55 		return FAILURE;
56 	}
57 
58 	ZVAL_NEW_STR(&tmp, u8str);
59 	zend_update_property(Transliterator_ce_ptr, object,
60 		"id", sizeof( "id" ) - 1, &tmp );
61 	GC_DELREF(u8str);
62 	return SUCCESS;
63 }
64 /* }}} */
65 
66 /*
67  * Auxiliary functions needed by objects of 'Transliterator' class
68  */
69 
70 /* {{{ void transliterator_object_init( Transliterator_object* to )
71  * Initialize internals of Transliterator_object.
72  */
transliterator_object_init(Transliterator_object * to)73 static void transliterator_object_init( Transliterator_object* to )
74 {
75 	if( !to )
76 		return;
77 
78 	intl_error_init( TRANSLITERATOR_ERROR_P( to ) );
79 }
80 /* }}} */
81 
82 /* {{{ void transliterator_object_destroy( Transliterator_object* to )
83  * Clean up mem allocted by internals of Transliterator_object
84  */
transliterator_object_destroy(Transliterator_object * to)85 static void transliterator_object_destroy( Transliterator_object* to )
86 {
87 	if( !to )
88 		return;
89 
90 	if( to->utrans )
91 	{
92 		utrans_close( to->utrans );
93 		to->utrans = NULL;
94 	}
95 
96 	intl_error_reset( TRANSLITERATOR_ERROR_P( to ) );
97 }
98 /* }}} */
99 
100 /* {{{ Transliterator_objects_free */
Transliterator_objects_free(zend_object * object)101 static void Transliterator_objects_free( zend_object *object )
102 {
103 	Transliterator_object* to = php_intl_transliterator_fetch_object(object);
104 
105 	zend_object_std_dtor( &to->zo );
106 
107 	transliterator_object_destroy( to );
108 }
109 /* }}} */
110 
111 /* {{{ Transliterator_object_create */
Transliterator_object_create(zend_class_entry * ce)112 static zend_object *Transliterator_object_create( zend_class_entry *ce )
113 {
114 	Transliterator_object* intern;
115 
116 	intern = zend_object_alloc(sizeof(Transliterator_object), ce);
117 
118 	zend_object_std_init( &intern->zo, ce );
119     object_properties_init( &intern->zo, ce );
120 	transliterator_object_init( intern );
121 
122 	intern->zo.handlers = &Transliterator_handlers;
123 
124 	return &intern->zo;
125 }
126 /* }}} */
127 
128 /*
129  * Object handlers for Transliterator class (and subclasses)
130  */
131 
132 /* {{{ clone handler for Transliterator */
Transliterator_clone_obj(zval * object)133 static zend_object *Transliterator_clone_obj( zval *object )
134 {
135 	Transliterator_object *to_orig,
136 	                      *to_new;
137 	zend_object 		  *ret_val;
138 	intl_error_reset( NULL );
139 
140 	to_orig = Z_INTL_TRANSLITERATOR_P( object );
141 	intl_error_reset( INTL_DATA_ERROR_P( to_orig ) );
142 	ret_val = Transliterator_ce_ptr->create_object( Z_OBJCE_P( object ) );
143 	to_new  = php_intl_transliterator_fetch_object( ret_val );
144 
145 	zend_objects_clone_members( &to_new->zo, &to_orig->zo );
146 
147 	if( to_orig->utrans != NULL )
148 	{
149 		zval			tempz; /* dummy zval to pass to transliterator_object_construct */
150 
151 		/* guaranteed to return NULL if it fails */
152 		UTransliterator *utrans = utrans_clone( to_orig->utrans, TRANSLITERATOR_ERROR_CODE_P( to_orig ) );
153 
154 		if( U_FAILURE( TRANSLITERATOR_ERROR_CODE( to_orig ) ) )
155 			goto err;
156 
157 		ZVAL_OBJ(&tempz, ret_val);
158 		transliterator_object_construct( &tempz, utrans,
159 			TRANSLITERATOR_ERROR_CODE_P( to_orig ) );
160 
161 		if( U_FAILURE( TRANSLITERATOR_ERROR_CODE( to_orig ) ) )
162 		{
163 			zend_string *err_msg;
164 err:
165 
166 			if( utrans != NULL )
167 				transliterator_object_destroy( to_new );
168 
169 			/* set the error anyway, in case in the future we decide not to
170 			 * throw an error. It also helps build the error message */
171 			intl_error_set_code( NULL, INTL_DATA_ERROR_CODE( to_orig ) );
172 			intl_errors_set_custom_msg( TRANSLITERATOR_ERROR_P( to_orig ),
173 				"Could not clone transliterator", 0 );
174 
175 			err_msg = intl_error_get_message( TRANSLITERATOR_ERROR_P( to_orig ) );
176 			zend_throw_error( NULL, "%s", ZSTR_VAL(err_msg) );
177 			zend_string_free( err_msg ); /* if it's changed into a warning */
178 			/* do not destroy tempz; we need to return something */
179 		}
180 	}
181 	else
182 	{
183 		/* We shouldn't have unconstructed objects in the first place */
184 		php_error_docref( NULL, E_WARNING,
185 			"Cloning unconstructed transliterator." );
186 	}
187 
188 	return ret_val;
189 }
190 /* }}} */
191 
192 #define TRANSLITERATOR_PROPERTY_HANDLER_PROLOG(return_fail) \
193 	zval tmp_member;							\
194 	if( Z_TYPE_P( member ) != IS_STRING )		\
195 	{											\
196 		zend_string *_str =						\
197 			zval_try_get_string_func(member);	\
198 		if (UNEXPECTED(!_str)) { return_fail; }	\
199 		ZVAL_STR(&tmp_member, _str);			\
200 		member = &tmp_member;					\
201 		cache_slot = NULL;						\
202     }
203 
204 #define TRANSLITERATOR_PROPERTY_HANDLER_EPILOG	\
205 	if( member == &tmp_member )					\
206 	{											\
207 		zval_ptr_dtor_str( &tmp_member );		\
208 	}
209 
210 /* {{{ get_property_ptr_ptr handler */
Transliterator_get_property_ptr_ptr(zval * object,zval * member,int type,void ** cache_slot)211 static zval *Transliterator_get_property_ptr_ptr( zval *object, zval *member, int type, void **cache_slot )
212 {
213 	zval *retval;
214 
215 	TRANSLITERATOR_PROPERTY_HANDLER_PROLOG(return NULL);
216 
217 	if(zend_binary_strcmp( "id", sizeof( "id" ) - 1,
218 		Z_STRVAL_P( member ), Z_STRLEN_P( member ) ) == 0 )
219 	{
220 		retval = NULL; /* fallback to read_property */
221 	}
222 	else
223 	{
224 		retval = zend_std_get_property_ptr_ptr( object, member, type, cache_slot );
225 	}
226 
227 	TRANSLITERATOR_PROPERTY_HANDLER_EPILOG;
228 
229 	return retval;
230 }
231 /* }}} */
232 
233 /* {{{ read_property handler */
Transliterator_read_property(zval * object,zval * member,int type,void ** cache_slot,zval * rv)234 static zval *Transliterator_read_property( zval *object, zval *member, int type, void **cache_slot, zval *rv )
235 {
236 	zval *retval;
237 
238 	TRANSLITERATOR_PROPERTY_HANDLER_PROLOG(return &EG(uninitialized_zval));
239 
240 	if( ( type != BP_VAR_R && type != BP_VAR_IS ) &&
241 		( zend_binary_strcmp( "id", sizeof( "id" ) - 1,
242 		Z_STRVAL_P( member ), Z_STRLEN_P( member ) ) == 0 ) )
243 	{
244 		php_error_docref(NULL, E_WARNING, "The property \"id\" is read-only" );
245 		retval = &EG( uninitialized_zval );
246 	}
247 	else
248 	{
249 		retval = zend_std_read_property( object, member, type, cache_slot, rv );
250 	}
251 
252 	TRANSLITERATOR_PROPERTY_HANDLER_EPILOG;
253 
254 	return retval;
255 }
256 
257 /* }}} */
258 
259 /* {{{ write_property handler */
Transliterator_write_property(zval * object,zval * member,zval * value,void ** cache_slot)260 static zval *Transliterator_write_property( zval *object, zval *member, zval *value, void **cache_slot )
261 {
262 	zend_class_entry *scope;
263 	TRANSLITERATOR_PROPERTY_HANDLER_PROLOG(return value);
264 
265 	if (EG(fake_scope)) {
266 		scope = EG(fake_scope);
267 	} else {
268 		scope = zend_get_executed_scope();
269 	}
270 	if( ( scope != Transliterator_ce_ptr ) &&
271 		( zend_binary_strcmp( "id", sizeof( "id" ) - 1,
272 		Z_STRVAL_P( member ), Z_STRLEN_P( member ) ) == 0 ) )
273 	{
274 		php_error_docref(NULL, E_WARNING, "The property \"id\" is read-only" );
275 	}
276 	else
277 	{
278 		value = zend_std_write_property( object, member, value, cache_slot );
279 	}
280 
281 	TRANSLITERATOR_PROPERTY_HANDLER_EPILOG;
282 
283 	return value;
284 }
285 /* }}} */
286 
287 /*
288  * 'Transliterator' class registration structures & functions
289  */
290 
291 /* {{{ Transliterator methods arguments info */
292 
293 ZEND_BEGIN_ARG_INFO_EX( ainfo_trans_void, 0, 0, 0 )
294 ZEND_END_ARG_INFO()
295 
296 ZEND_BEGIN_ARG_INFO_EX( ainfo_trans_create, 0, 0, 1 )
297 	ZEND_ARG_INFO( 0, id )
298 	ZEND_ARG_INFO( 0, direction )
299 ZEND_END_ARG_INFO()
300 
301 ZEND_BEGIN_ARG_INFO_EX( ainfo_trans_create_from_rules, 0, 0, 1 )
302 	ZEND_ARG_INFO( 0, rules )
303 	ZEND_ARG_INFO( 0, direction )
304 ZEND_END_ARG_INFO()
305 
306 ZEND_BEGIN_ARG_INFO_EX( ainfo_trans_me_transliterate, 0, 0, 1 )
307 	ZEND_ARG_INFO( 0, subject )
308 	ZEND_ARG_INFO( 0, start )
309 	ZEND_ARG_INFO( 0, end )
310 ZEND_END_ARG_INFO()
311 /* }}} */
312 
313 /* {{{ Transliterator_class_functions
314  * Every 'Transliterator' class method has an entry in this table
315  */
316 static const zend_function_entry Transliterator_class_functions[] = {
317 	PHP_ME( Transliterator,			__construct,						ainfo_trans_void,				ZEND_ACC_PRIVATE | ZEND_ACC_FINAL )
318 	PHP_ME_MAPPING( create,			transliterator_create,				ainfo_trans_create,				ZEND_ACC_STATIC |ZEND_ACC_PUBLIC )
319 	PHP_ME_MAPPING( createFromRules,transliterator_create_from_rules,	ainfo_trans_create_from_rules,	ZEND_ACC_STATIC | ZEND_ACC_PUBLIC )
320 	PHP_ME_MAPPING( createInverse,	transliterator_create_inverse,		ainfo_trans_void,				ZEND_ACC_PUBLIC )
321 	PHP_ME_MAPPING( listIDs,		transliterator_list_ids,			ainfo_trans_void,				ZEND_ACC_STATIC | ZEND_ACC_PUBLIC )
322 	PHP_ME_MAPPING( transliterate,	transliterator_transliterate,		ainfo_trans_me_transliterate,	ZEND_ACC_PUBLIC )
323 	PHP_ME_MAPPING( getErrorCode,	transliterator_get_error_code,		ainfo_trans_void,				ZEND_ACC_PUBLIC )
324 	PHP_ME_MAPPING( getErrorMessage,transliterator_get_error_message,	ainfo_trans_void,				ZEND_ACC_PUBLIC )
325 	PHP_FE_END
326 };
327 /* }}} */
328 
329 /* {{{ transliterator_register_Transliterator_class
330  * Initialize 'Transliterator' class
331  */
transliterator_register_Transliterator_class(void)332 void transliterator_register_Transliterator_class( void )
333 {
334 	zend_class_entry ce;
335 
336 	/* Create and register 'Transliterator' class. */
337 	INIT_CLASS_ENTRY( ce, "Transliterator", Transliterator_class_functions );
338 	ce.create_object = Transliterator_object_create;
339 	Transliterator_ce_ptr = zend_register_internal_class( &ce );
340 	memcpy( &Transliterator_handlers, &std_object_handlers,
341 		sizeof Transliterator_handlers );
342 	Transliterator_handlers.offset = XtOffsetOf(Transliterator_object, zo);
343 	Transliterator_handlers.free_obj = Transliterator_objects_free;
344 	Transliterator_handlers.clone_obj = Transliterator_clone_obj;
345 	Transliterator_handlers.get_property_ptr_ptr = Transliterator_get_property_ptr_ptr;
346 	Transliterator_handlers.read_property = Transliterator_read_property;
347 	Transliterator_handlers.write_property = Transliterator_write_property;
348 
349 	/* Declare 'Transliterator' class properties */
350 	if( !Transliterator_ce_ptr )
351 	{
352 		zend_error( E_ERROR,
353 			"Transliterator: attempt to create properties "
354 			"on a non-registered class." );
355 		return;
356 	}
357 	zend_declare_property_null( Transliterator_ce_ptr,
358 		"id", sizeof( "id" ) - 1, ZEND_ACC_PUBLIC );
359 
360 	/* constants are declared in transliterator_register_constants, called from MINIT */
361 
362 }
363 /* }}} */
364