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 <unicode/rbbi.h>
18 
19 extern "C" {
20 #define USE_BREAKITERATOR_POINTER 1
21 #include "breakiterator_class.h"
22 #include <zend_exceptions.h>
23 #include <limits.h>
24 }
25 
26 #include "../intl_convertcpp.h"
27 #include "../intl_common.h"
28 
29 using icu::RuleBasedBreakIterator;
30 using icu::Locale;
31 
fetch_rbbi(BreakIterator_object * bio)32 static inline RuleBasedBreakIterator *fetch_rbbi(BreakIterator_object *bio) {
33 	return (RuleBasedBreakIterator*)bio->biter;
34 }
35 
_php_intlrbbi_constructor_body(INTERNAL_FUNCTION_PARAMETERS)36 static void _php_intlrbbi_constructor_body(INTERNAL_FUNCTION_PARAMETERS)
37 {
38 	char		*rules;
39 	size_t		rules_len;
40 	zend_bool	compiled	= 0;
41 	UErrorCode	status		= U_ZERO_ERROR;
42 	intl_error_reset(NULL);
43 
44 	if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "s|b",
45 			&rules, &rules_len, &compiled) == FAILURE) {
46 		intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
47 			"rbbi_create_instance: bad arguments", 0);
48 		return;
49 	}
50 
51 	// instantiation of ICU object
52 	RuleBasedBreakIterator *rbbi;
53 
54 	if (!compiled) {
55 		UnicodeString	rulesStr;
56 		UParseError		parseError = UParseError();
57 		if (intl_stringFromChar(rulesStr, rules, rules_len, &status)
58 				== FAILURE) {
59 			intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
60 				"rbbi_create_instance: rules were not a valid UTF-8 string",
61 				0);
62 			RETURN_NULL();
63 		}
64 
65 		rbbi = new RuleBasedBreakIterator(rulesStr, parseError, status);
66 		intl_error_set_code(NULL, status);
67 		if (U_FAILURE(status)) {
68 			char *msg;
69 			smart_str parse_error_str;
70 			parse_error_str = intl_parse_error_to_string(&parseError);
71 			spprintf(&msg, 0, "rbbi_create_instance: unable to create "
72 				"RuleBasedBreakIterator from rules (%s)", parse_error_str.s? ZSTR_VAL(parse_error_str.s) : "");
73 			smart_str_free(&parse_error_str);
74 			intl_error_set_custom_msg(NULL, msg, 1);
75 			efree(msg);
76 			delete rbbi;
77 			return;
78 		}
79 	} else { // compiled
80 #if U_ICU_VERSION_MAJOR_NUM * 10 + U_ICU_VERSION_MINOR_NUM >= 48
81 		rbbi = new RuleBasedBreakIterator((uint8_t*)rules, rules_len, status);
82 		if (U_FAILURE(status)) {
83 			intl_error_set(NULL, status, "rbbi_create_instance: unable to "
84 				"create instance from compiled rules", 0);
85 			delete rbbi;
86 			return;
87 		}
88 #else
89 		intl_error_set(NULL, U_UNSUPPORTED_ERROR, "rbbi_create_instance: "
90 			"compiled rules require ICU >= 4.8", 0);
91 		return;
92 #endif
93 	}
94 
95 	breakiterator_object_create(return_value, rbbi, 0);
96 }
97 
PHP_METHOD(IntlRuleBasedBreakIterator,__construct)98 U_CFUNC PHP_METHOD(IntlRuleBasedBreakIterator, __construct)
99 {
100 	zend_error_handling error_handling;
101 
102 	zend_replace_error_handling(EH_THROW, IntlException_ce_ptr, &error_handling);
103 	return_value = getThis();
104 	_php_intlrbbi_constructor_body(INTERNAL_FUNCTION_PARAM_PASSTHRU);
105 	zend_restore_error_handling(&error_handling);
106 }
107 
PHP_FUNCTION(rbbi_get_rules)108 U_CFUNC PHP_FUNCTION(rbbi_get_rules)
109 {
110 	BREAKITER_METHOD_INIT_VARS;
111 	object = getThis();
112 
113 	if (zend_parse_parameters_none() == FAILURE) {
114 		intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
115 			"rbbi_get_rules: bad arguments", 0);
116 		RETURN_FALSE;
117 	}
118 
119 	BREAKITER_METHOD_FETCH_OBJECT;
120 
121 	zend_string *u8str;
122 	const UnicodeString rules = fetch_rbbi(bio)->getRules();
123 
124 	u8str = intl_charFromString(rules, BREAKITER_ERROR_CODE_P(bio));
125 	if (!u8str)
126 	{
127 		intl_errors_set(BREAKITER_ERROR_P(bio), BREAKITER_ERROR_CODE(bio),
128 				"rbbi_hash_code: Error converting result to UTF-8 string",
129 				0);
130 		RETURN_FALSE;
131 	}
132 	RETVAL_STR(u8str);
133 }
134 
PHP_FUNCTION(rbbi_get_rule_status)135 U_CFUNC PHP_FUNCTION(rbbi_get_rule_status)
136 {
137 	BREAKITER_METHOD_INIT_VARS;
138 	object = getThis();
139 
140 	if (zend_parse_parameters_none() == FAILURE) {
141 		intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
142 			"rbbi_get_rule_status: bad arguments", 0);
143 		RETURN_FALSE;
144 	}
145 
146 	BREAKITER_METHOD_FETCH_OBJECT;
147 
148 	RETURN_LONG(fetch_rbbi(bio)->getRuleStatus());
149 }
150 
PHP_FUNCTION(rbbi_get_rule_status_vec)151 U_CFUNC PHP_FUNCTION(rbbi_get_rule_status_vec)
152 {
153 	BREAKITER_METHOD_INIT_VARS;
154 	object = getThis();
155 
156 	if (zend_parse_parameters_none() == FAILURE) {
157 		intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
158 			"rbbi_get_rule_status_vec: bad arguments", 0);
159 		RETURN_FALSE;
160 	}
161 
162 	BREAKITER_METHOD_FETCH_OBJECT;
163 
164 	int32_t num_rules = fetch_rbbi(bio)->getRuleStatusVec(NULL, 0,
165 			BREAKITER_ERROR_CODE(bio));
166 	if (BREAKITER_ERROR_CODE(bio) == U_BUFFER_OVERFLOW_ERROR) {
167 		BREAKITER_ERROR_CODE(bio) = U_ZERO_ERROR;
168 	} else {
169 		// should not happen
170 		INTL_METHOD_CHECK_STATUS(bio, "rbbi_get_rule_status_vec: failed "
171 				" determining the number of status values");
172 	}
173 	int32_t *rules = new int32_t[num_rules];
174 	num_rules = fetch_rbbi(bio)->getRuleStatusVec(rules, num_rules,
175 			BREAKITER_ERROR_CODE(bio));
176 	if (U_FAILURE(BREAKITER_ERROR_CODE(bio))) {
177 		delete[] rules;
178 		intl_errors_set(BREAKITER_ERROR_P(bio), BREAKITER_ERROR_CODE(bio),
179 				"rbbi_get_rule_status_vec: failed obtaining the status values",
180 				0);
181 		RETURN_FALSE;
182 	}
183 
184 	array_init_size(return_value, num_rules);
185 	for (int32_t i = 0; i < num_rules; i++) {
186 		add_next_index_long(return_value, rules[i]);
187 	}
188 	delete[] rules;
189 }
190 
191 #if U_ICU_VERSION_MAJOR_NUM * 10 + U_ICU_VERSION_MINOR_NUM >= 48
PHP_FUNCTION(rbbi_get_binary_rules)192 U_CFUNC PHP_FUNCTION(rbbi_get_binary_rules)
193 {
194 	BREAKITER_METHOD_INIT_VARS;
195 	object = getThis();
196 
197 	if (zend_parse_parameters_none() == FAILURE) {
198 		intl_error_set(NULL, U_ILLEGAL_ARGUMENT_ERROR,
199 			"rbbi_get_binary_rules: bad arguments", 0);
200 		RETURN_FALSE;
201 	}
202 
203 	BREAKITER_METHOD_FETCH_OBJECT;
204 
205 	uint32_t		rules_len;
206 	const uint8_t	*rules = fetch_rbbi(bio)->getBinaryRules(rules_len);
207 
208 	if (rules_len > INT_MAX - 1) {
209 		intl_errors_set(BREAKITER_ERROR_P(bio), BREAKITER_ERROR_CODE(bio),
210 				"rbbi_get_binary_rules: the rules are too large",
211 				0);
212 		RETURN_FALSE;
213 	}
214 
215 	zend_string *ret_rules = zend_string_alloc(rules_len, 0);
216 	memcpy(ZSTR_VAL(ret_rules), rules, rules_len);
217 	ZSTR_VAL(ret_rules)[rules_len] = '\0';
218 
219 	RETURN_STR(ret_rules);
220 }
221 #endif
222