1 /*
2    +----------------------------------------------------------------------+
3    | This source file is subject to version 3.01 of the PHP license,      |
4    | that is bundled with this package in the file LICENSE, and is        |
5    | available through the world-wide-web at the following url:           |
6    | https://www.php.net/license/3_01.txt                                 |
7    | If you did not receive a copy of the PHP license and are unable to   |
8    | obtain it through the world-wide-web, please send a note to          |
9    | license@php.net so we can mail you a copy immediately.               |
10    +----------------------------------------------------------------------+
11    | Authors: Vadim Savchuk <vsavchuk@productengine.com>                  |
12    |          Dmitry Lakhtyuk <dlakhtyuk@productengine.com>               |
13    +----------------------------------------------------------------------+
14  */
15 
16 #ifdef HAVE_CONFIG_H
17 #include "config.h"
18 #endif
19 
20 #include "php_intl.h"
21 #include "collator_class.h"
22 #include "collator_is_numeric.h"
23 #include "collator_convert.h"
24 #include "intl_convert.h"
25 
26 #include <unicode/ustring.h>
27 #include <php.h>
28 
29 #define COLLATOR_CONVERT_RETURN_FAILED(retval) { \
30 			Z_TRY_ADDREF_P(retval);              \
31 			return retval;                       \
32 	}
33 
34 /* {{{ collator_convert_hash_item_from_utf8_to_utf16 */
collator_convert_hash_item_from_utf8_to_utf16(HashTable * hash,zval * hashData,zend_string * hashKey,zend_ulong hashIndex,UErrorCode * status)35 static void collator_convert_hash_item_from_utf8_to_utf16(
36 	HashTable* hash, zval *hashData, zend_string *hashKey, zend_ulong hashIndex,
37 	UErrorCode* status )
38 {
39 	const char* old_val;
40 	size_t      old_val_len;
41 	UChar*      new_val      = NULL;
42 	int32_t     new_val_len  = 0;
43 	zval        znew_val;
44 
45 	/* Process string values only. */
46 	if( Z_TYPE_P( hashData ) != IS_STRING )
47 		return;
48 
49 	old_val     = Z_STRVAL_P( hashData );
50 	old_val_len = Z_STRLEN_P( hashData );
51 
52 	/* Convert it from UTF-8 to UTF-16LE and save the result to new_val[_len]. */
53 	intl_convert_utf8_to_utf16( &new_val, &new_val_len, old_val, old_val_len, status );
54 	if( U_FAILURE( *status ) )
55 		return;
56 
57 	/* Update current hash item with the converted value. */
58 	ZVAL_STRINGL( &znew_val, (char*)new_val, UBYTES(new_val_len + 1) );
59 	//???
60 	efree(new_val);
61 	/* hack to fix use of initialized value */
62 	Z_STRLEN(znew_val) = Z_STRLEN(znew_val) - UBYTES(1);
63 
64 	if( hashKey)
65 	{
66 		zend_hash_update( hash, hashKey, &znew_val);
67 	}
68 	else /* hashKeyType == HASH_KEY_IS_LONG */
69 	{
70 		zend_hash_index_update( hash, hashIndex, &znew_val);
71 	}
72 }
73 /* }}} */
74 
75 /* {{{ collator_convert_hash_item_from_utf16_to_utf8 */
collator_convert_hash_item_from_utf16_to_utf8(HashTable * hash,zval * hashData,zend_string * hashKey,zend_ulong hashIndex,UErrorCode * status)76 static void collator_convert_hash_item_from_utf16_to_utf8(
77 	HashTable* hash, zval * hashData, zend_string* hashKey, zend_ulong hashIndex,
78 	UErrorCode* status )
79 {
80 	const char* old_val;
81 	size_t      old_val_len;
82 	zend_string* u8str;
83 	zval        znew_val;
84 
85 	/* Process string values only. */
86 	if( Z_TYPE_P( hashData ) != IS_STRING )
87 		return;
88 
89 	old_val     = Z_STRVAL_P( hashData );
90 	old_val_len = Z_STRLEN_P( hashData );
91 
92 	/* Convert it from UTF-16LE to UTF-8 and save the result to new_val[_len]. */
93 	u8str = intl_convert_utf16_to_utf8(
94 		(UChar*)old_val, UCHARS(old_val_len), status );
95 	if( !u8str )
96 		return;
97 
98 	/* Update current hash item with the converted value. */
99 	ZVAL_NEW_STR( &znew_val, u8str);
100 
101 	if( hashKey )
102 	{
103 		zend_hash_update( hash, hashKey, &znew_val);
104 	}
105 	else /* hashKeyType == HASH_KEY_IS_LONG */
106 	{
107 		zend_hash_index_update( hash, hashIndex, &znew_val);
108 	}
109 }
110 /* }}} */
111 
112 /* {{{ collator_convert_hash_from_utf8_to_utf16
113  *  Convert values of the given hash from UTF-8 encoding to UTF-16LE.
114  */
collator_convert_hash_from_utf8_to_utf16(HashTable * hash,UErrorCode * status)115 void collator_convert_hash_from_utf8_to_utf16( HashTable* hash, UErrorCode* status )
116 {
117 	zend_ulong    hashIndex;
118 	zval *hashData;
119 	zend_string *hashKey;
120 
121 	ZEND_HASH_FOREACH_KEY_VAL(hash, hashIndex, hashKey, hashData) {
122 		/* Convert current hash item from UTF-8 to UTF-16LE. */
123 		collator_convert_hash_item_from_utf8_to_utf16(
124 			hash, hashData, hashKey, hashIndex, status );
125 		if( U_FAILURE( *status ) )
126 			return;
127 	} ZEND_HASH_FOREACH_END();
128 }
129 /* }}} */
130 
131 /* {{{ collator_convert_hash_from_utf16_to_utf8
132  * Convert values of the given hash from UTF-16LE encoding to UTF-8.
133  */
collator_convert_hash_from_utf16_to_utf8(HashTable * hash,UErrorCode * status)134 void collator_convert_hash_from_utf16_to_utf8( HashTable* hash, UErrorCode* status )
135 {
136 	zend_ulong hashIndex;
137 	zend_string *hashKey;
138 	zval *hashData;
139 
140 	ZEND_HASH_FOREACH_KEY_VAL(hash, hashIndex, hashKey, hashData) {
141 		/* Convert current hash item from UTF-16LE to UTF-8. */
142 		collator_convert_hash_item_from_utf16_to_utf8(
143 			hash, hashData, hashKey, hashIndex, status );
144 		if( U_FAILURE( *status ) ) {
145 			return;
146 		}
147 	} ZEND_HASH_FOREACH_END();
148 }
149 /* }}} */
150 
151 /* {{{ collator_convert_zstr_utf16_to_utf8
152  *
153  * Convert string from utf16 to utf8.
154  *
155  * @param  zval* utf16_zval String to convert.
156  *
157  * @return zval* Converted string.
158  */
collator_convert_zstr_utf16_to_utf8(zval * utf16_zval,zval * rv)159 zval* collator_convert_zstr_utf16_to_utf8( zval* utf16_zval, zval *rv )
160 {
161 	zend_string* u8str;
162 	UErrorCode status = U_ZERO_ERROR;
163 
164 	/* Convert to utf8 then. */
165 	u8str = intl_convert_utf16_to_utf8(
166 		(UChar*) Z_STRVAL_P(utf16_zval), UCHARS( Z_STRLEN_P(utf16_zval) ), &status );
167 	if( !u8str ) {
168 		php_error( E_WARNING, "Error converting utf16 to utf8 in collator_convert_zval_utf16_to_utf8()" );
169 		ZVAL_EMPTY_STRING( rv );
170 	} else {
171 		ZVAL_NEW_STR( rv, u8str );
172 	}
173 	return rv;
174 }
175 /* }}} */
176 
collator_convert_zstr_utf8_to_utf16(zend_string * utf8_str)177 zend_string *collator_convert_zstr_utf8_to_utf16(zend_string *utf8_str)
178 {
179 	UChar *ustr = NULL;
180 	int32_t ustr_len = 0;
181 	UErrorCode status = U_ZERO_ERROR;
182 
183 	/* Convert the string to UTF-16. */
184 	intl_convert_utf8_to_utf16(
185 			&ustr, &ustr_len,
186 			ZSTR_VAL(utf8_str), ZSTR_LEN(utf8_str),
187 			&status);
188 	// FIXME Or throw error or use intl internal error handler
189 	if (U_FAILURE(status)) {
190 		php_error(E_WARNING,
191 			"Error casting object to string in collator_convert_zstr_utf8_to_utf16()");
192 	}
193 
194 	zend_string *zstr = zend_string_init((char *) ustr, UBYTES(ustr_len), 0);
195 	efree((char *)ustr);
196 	return zstr;
197 }
198 
199 /* {{{ collator_convert_object_to_string
200  * Convert object to UTF16-encoded string.
201  */
collator_convert_object_to_string(zval * obj,zval * rv)202 zval* collator_convert_object_to_string( zval* obj, zval *rv )
203 {
204 	zval* zstr        = NULL;
205 	UErrorCode status = U_ZERO_ERROR;
206 	UChar* ustr       = NULL;
207 	int32_t ustr_len  = 0;
208 
209 	/* Bail out if it's not an object. */
210 	if( Z_TYPE_P( obj ) != IS_OBJECT )
211 	{
212 		COLLATOR_CONVERT_RETURN_FAILED( obj );
213 	}
214 
215 	/* Try object's handlers. */
216 	zstr = rv;
217 
218 	if( Z_OBJ_HT_P(obj)->cast_object( Z_OBJ_P(obj), zstr, IS_STRING ) == FAILURE )
219 	{
220 		/* cast_object failed => bail out. */
221 		zval_ptr_dtor( zstr );
222 		COLLATOR_CONVERT_RETURN_FAILED( obj );
223 	}
224 
225 	/* Object wasn't successfully converted => bail out. */
226 	if( zstr == NULL )
227 	{
228 		COLLATOR_CONVERT_RETURN_FAILED( obj );
229 	}
230 
231 	/* Convert the string to UTF-16. */
232 	intl_convert_utf8_to_utf16(
233 			&ustr, &ustr_len,
234 			Z_STRVAL_P( zstr ), Z_STRLEN_P( zstr ),
235 			&status );
236 	// FIXME Or throw error or use intl internal error handler
237 	if( U_FAILURE( status ) )
238 		php_error( E_WARNING, "Error casting object to string in collator_convert_object_to_string()" );
239 
240 	/* Cleanup zstr to hold utf16 string. */
241 	zval_ptr_dtor_str( zstr );
242 
243 	/* Set string. */
244 	ZVAL_STRINGL( zstr, (char*)ustr, UBYTES(ustr_len));
245 	//???
246 	efree((char *)ustr);
247 
248 	/* Don't free ustr cause it's set in zstr without copy.
249 	 * efree( ustr );
250 	 */
251 
252 	return zstr;
253 }
254 /* }}} */
255 
256 /* {{{ collator_convert_string_to_number
257  *
258  * Convert string to number.
259  *
260  * @param  zval* str String to convert.
261  *
262  * @return zval* Number. If str is not numeric string return number zero.
263  */
collator_convert_string_to_number(zval * str,zval * rv)264 zval* collator_convert_string_to_number( zval* str, zval *rv )
265 {
266 	zval* num = collator_convert_string_to_number_if_possible( str, rv );
267 	if( num == str )
268 	{
269 		/* String wasn't converted => return zero. */
270 		zval_ptr_dtor( num );
271 
272 		num = rv;
273 		ZVAL_LONG( num, 0 );
274 	}
275 
276 	return num;
277 }
278 /* }}} */
279 
280 /* {{{ collator_convert_string_to_double
281  *
282  * Convert string to double.
283  *
284  * @param  zval* str String to convert.
285  *
286  * @return zval* Number. If str is not numeric string return number zero.
287  */
collator_convert_string_to_double(zval * str,zval * rv)288 zval* collator_convert_string_to_double( zval* str, zval *rv )
289 {
290 	zval* num = collator_convert_string_to_number( str, rv );
291 	if( Z_TYPE_P(num) == IS_LONG )
292 	{
293 		ZVAL_DOUBLE( num, Z_LVAL_P( num ) );
294 	}
295 
296 	return num;
297 }
298 /* }}} */
299 
300 /* {{{ collator_convert_string_to_number_if_possible
301  *
302  * Convert string to numer.
303  *
304  * @param  zval* str String to convert.
305  *
306  * @return zval* Number if str is numeric string. Otherwise
307  *               original str param.
308  */
collator_convert_string_to_number_if_possible(zval * str,zval * rv)309 zval* collator_convert_string_to_number_if_possible( zval* str, zval *rv )
310 {
311 	uint8_t is_numeric = 0;
312 	zend_long lval      = 0;
313 	double dval    = 0;
314 
315 	if( Z_TYPE_P( str ) != IS_STRING )
316 	{
317 		COLLATOR_CONVERT_RETURN_FAILED( str );
318 	}
319 
320 	if ( ( is_numeric = collator_is_numeric( (UChar*) Z_STRVAL_P(str), UCHARS( Z_STRLEN_P(str) ), &lval, &dval, /* allow_errors */ 1 ) ) )
321 	{
322 		if( is_numeric == IS_LONG ) {
323 			ZVAL_LONG(rv, lval);
324 		}
325 		if( is_numeric == IS_DOUBLE )
326 			ZVAL_DOUBLE(rv, dval);
327 	}
328 	else
329 	{
330 		COLLATOR_CONVERT_RETURN_FAILED( str );
331 	}
332 
333 	return rv;
334 }
335 /* }}} */
336 
337 /* Returns string from input zval.
338  *
339  * @param  zval* arg zval to get string from
340  *
341  * @return zend_string* UTF16 string.
342  */
collator_zval_to_string(zval * arg)343 zend_string *collator_zval_to_string(zval *arg)
344 {
345 	// TODO: This is extremely weird in that it leaves pre-existing strings alone and does not
346 	// perform a UTF-8 to UTF-16 conversion for them. The assumption is that values that are
347 	// already strings have already been converted beforehand. It would be good to clean this up.
348 	if (Z_TYPE_P(arg) == IS_STRING) {
349 		return zend_string_copy(Z_STR_P(arg));
350 	}
351 
352 	zend_string *utf8_str = zval_get_string(arg);
353 	zend_string *utf16_str = collator_convert_zstr_utf8_to_utf16(utf8_str);
354 	zend_string_release(utf8_str);
355 	return utf16_str;
356 }
357 
358 /* {{{ collator_normalize_sort_argument
359  *
360  * Normalize argument to use in sort's compare function.
361  *
362  * @param  zval* arg Sort's argument to normalize.
363  *
364  * @return zval* Normalized copy of arg or unmodified arg
365  *               if normalization is not needed.
366  */
collator_normalize_sort_argument(zval * arg,zval * rv)367 zval* collator_normalize_sort_argument( zval* arg, zval *rv )
368 {
369 	zval* n_arg = NULL;
370 
371 	if( Z_TYPE_P( arg ) != IS_STRING )
372 	{
373 		/* If it's not a string then nothing to do.
374 		 * Return original arg.
375 		 */
376 		COLLATOR_CONVERT_RETURN_FAILED( arg );
377 	}
378 
379 	/* Try convert to number. */
380 	n_arg = collator_convert_string_to_number_if_possible( arg, rv );
381 
382 	if( n_arg == arg )
383 	{
384 		/* Conversion to number failed. */
385 		zval_ptr_dtor( n_arg );
386 
387 		/* Convert string to utf8. */
388 		n_arg = collator_convert_zstr_utf16_to_utf8( arg, rv );
389 	}
390 
391 	return n_arg;
392 }
393 /* }}} */
394