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: Vadim Savchuk <vsavchuk@productengine.com>                  |
14    |          Dmitry Lakhtyuk <dlakhtyuk@productengine.com>               |
15    +----------------------------------------------------------------------+
16  */
17 
18 #ifdef HAVE_CONFIG_H
19 #include "config.h"
20 #endif
21 
22 #include "php_intl.h"
23 #include "collator_class.h"
24 #include "collator_compare.h"
25 #include "intl_convert.h"
26 
27 /* {{{ proto int Collator::compare( string $str1, string $str2 )
28  * Compare two strings. }}} */
29 /* {{{ proto int collator_compare( Collator $coll, string $str1, string $str2 )
30  * Compare two strings.
31  */
PHP_FUNCTION(collator_compare)32 PHP_FUNCTION( collator_compare )
33 {
34 	char*            str1      = NULL;
35 	char*            str2      = NULL;
36 	size_t              str1_len  = 0;
37 	size_t              str2_len  = 0;
38 
39 	UChar*           ustr1     = NULL;
40 	UChar*           ustr2     = NULL;
41 	int              ustr1_len = 0;
42 	int              ustr2_len = 0;
43 
44 	UCollationResult result;
45 
46 	COLLATOR_METHOD_INIT_VARS
47 
48 	/* Parse parameters. */
49 	if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "Oss",
50 		&object, Collator_ce_ptr, &str1, &str1_len, &str2, &str2_len ) == FAILURE )
51 	{
52 		intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
53 			 "collator_compare: unable to parse input params", 0 );
54 
55 		RETURN_FALSE;
56 	}
57 
58 	/* Fetch the object. */
59 	COLLATOR_METHOD_FETCH_OBJECT;
60 
61 	if (!co || !co->ucoll) {
62 		intl_error_set_code( NULL, COLLATOR_ERROR_CODE( co ) );
63 		intl_errors_set_custom_msg( COLLATOR_ERROR_P( co ),
64 			"Object not initialized", 0 );
65 		zend_throw_error(NULL, "Object not initialized");
66 
67 		RETURN_FALSE;
68 	}
69 
70 	/*
71 	 * Compare given strings (converting them to UTF-16 first).
72 	 */
73 
74 	/* First convert the strings to UTF-16. */
75 	intl_convert_utf8_to_utf16(
76 		&ustr1, &ustr1_len, str1, str1_len, COLLATOR_ERROR_CODE_P( co ) );
77 	if( U_FAILURE( COLLATOR_ERROR_CODE( co ) ) )
78 	{
79 		/* Set global error code. */
80 		intl_error_set_code( NULL, COLLATOR_ERROR_CODE( co ) );
81 
82 		/* Set error messages. */
83 		intl_errors_set_custom_msg( COLLATOR_ERROR_P( co ),
84 			"Error converting first argument to UTF-16", 0 );
85 		if (ustr1) {
86 			efree( ustr1 );
87 		}
88 		RETURN_FALSE;
89 	}
90 
91 	intl_convert_utf8_to_utf16(
92 		&ustr2, &ustr2_len, str2, str2_len, COLLATOR_ERROR_CODE_P( co ) );
93 	if( U_FAILURE( COLLATOR_ERROR_CODE( co ) ) )
94 	{
95 		/* Set global error code. */
96 		intl_error_set_code( NULL, COLLATOR_ERROR_CODE( co ) );
97 
98 		/* Set error messages. */
99 		intl_errors_set_custom_msg( COLLATOR_ERROR_P( co ),
100 			"Error converting second argument to UTF-16", 0 );
101 		if (ustr1) {
102 			efree( ustr1 );
103 		}
104 		if (ustr2) {
105 			efree( ustr2 );
106 		}
107 		RETURN_FALSE;
108 	}
109 
110 	/* Then compare them. */
111 	result = ucol_strcoll(
112 		co->ucoll,
113 		ustr1, ustr1_len,
114 		ustr2, ustr2_len );
115 
116 	if( ustr1 )
117 		efree( ustr1 );
118 	if( ustr2 )
119 		efree( ustr2 );
120 
121 	/* Return result of the comparison. */
122 	RETURN_LONG( result );
123 }
124 /* }}} */
125