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 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif
20 
21 #include <unicode/brkiter.h>
22 #include <unicode/rbbi.h>
23 #include "codepointiterator_internal.h"
24 
25 #include "breakiterator_iterators.h"
26 
27 #include <typeinfo>
28 
29 extern "C" {
30 #define USE_BREAKITERATOR_POINTER 1
31 #include "breakiterator_class.h"
32 #include "breakiterator_methods.h"
33 #include "rulebasedbreakiterator_methods.h"
34 #include "codepointiterator_methods.h"
35 #include <zend_exceptions.h>
36 #include <zend_interfaces.h>
37 #include <assert.h>
38 }
39 
40 using PHP::CodePointBreakIterator;
41 using icu::RuleBasedBreakIterator;
42 
43 /* {{{ Global variables */
44 zend_class_entry *BreakIterator_ce_ptr;
45 zend_class_entry *RuleBasedBreakIterator_ce_ptr;
46 zend_class_entry *CodePointBreakIterator_ce_ptr;
47 zend_object_handlers BreakIterator_handlers;
48 /* }}} */
49 
breakiterator_object_create(zval * object,BreakIterator * biter,int brand_new)50 U_CFUNC	void breakiterator_object_create(zval *object,
51 										 BreakIterator *biter, int brand_new)
52 {
53 	UClassID classId = biter->getDynamicClassID();
54 	zend_class_entry *ce;
55 
56 	if (classId == RuleBasedBreakIterator::getStaticClassID()) {
57 		ce = RuleBasedBreakIterator_ce_ptr;
58 	} else if (classId == CodePointBreakIterator::getStaticClassID()) {
59 		ce = CodePointBreakIterator_ce_ptr;
60 	} else {
61 		ce = BreakIterator_ce_ptr;
62 	}
63 
64 	if (brand_new) {
65 		object_init_ex(object, ce);
66 	}
67 	breakiterator_object_construct(object, biter);
68 }
69 
breakiterator_object_construct(zval * object,BreakIterator * biter)70 U_CFUNC void breakiterator_object_construct(zval *object,
71 											BreakIterator *biter)
72 {
73 	BreakIterator_object *bio;
74 
75 	BREAKITER_METHOD_FETCH_OBJECT_NO_CHECK; //populate to from object
76 	assert(bio->biter == NULL);
77 	bio->biter = biter;
78 }
79 
80 /* {{{ compare handler for BreakIterator */
BreakIterator_compare_objects(zval * object1,zval * object2)81 static int BreakIterator_compare_objects(zval *object1,
82 										 zval *object2)
83 {
84 	BreakIterator_object	*bio1,
85 							*bio2;
86 
87 	bio1 = Z_INTL_BREAKITERATOR_P(object1);
88 	bio2 = Z_INTL_BREAKITERATOR_P(object2);
89 
90 	if (bio1->biter == NULL || bio2->biter == NULL) {
91 		return bio1->biter == bio2->biter ? 0 : 1;
92 	}
93 
94 	return *bio1->biter == *bio2->biter ? 0 : 1;
95 }
96 /* }}} */
97 
98 /* {{{ clone handler for BreakIterator */
BreakIterator_clone_obj(zval * object)99 static zend_object *BreakIterator_clone_obj(zval *object)
100 {
101 	BreakIterator_object	*bio_orig,
102 							*bio_new;
103 	zend_object				*ret_val;
104 
105 	bio_orig = Z_INTL_BREAKITERATOR_P(object);
106 	intl_errors_reset(INTL_DATA_ERROR_P(bio_orig));
107 
108 	ret_val = BreakIterator_ce_ptr->create_object(Z_OBJCE_P(object));
109 	bio_new  = php_intl_breakiterator_fetch_object(ret_val);
110 
111 	zend_objects_clone_members(&bio_new->zo, &bio_orig->zo);
112 
113 	if (bio_orig->biter != NULL) {
114 		BreakIterator *new_biter;
115 
116 		new_biter = bio_orig->biter->clone();
117 		if (!new_biter) {
118 			zend_string *err_msg;
119 			intl_errors_set_code(BREAKITER_ERROR_P(bio_orig),
120 				U_MEMORY_ALLOCATION_ERROR);
121 			intl_errors_set_custom_msg(BREAKITER_ERROR_P(bio_orig),
122 				"Could not clone BreakIterator", 0);
123 			err_msg = intl_error_get_message(BREAKITER_ERROR_P(bio_orig));
124 			zend_throw_exception(NULL, ZSTR_VAL(err_msg), 0);
125 			zend_string_free(err_msg);
126 		} else {
127 			bio_new->biter = new_biter;
128 			ZVAL_COPY(&bio_new->text, &bio_orig->text);
129 		}
130 	} else {
131 		zend_throw_exception(NULL, "Cannot clone unconstructed BreakIterator", 0);
132 	}
133 
134 	return ret_val;
135 }
136 /* }}} */
137 
138 /* {{{ get_debug_info handler for BreakIterator */
BreakIterator_get_debug_info(zval * object,int * is_temp)139 static HashTable *BreakIterator_get_debug_info(zval *object, int *is_temp)
140 {
141 	zval val;
142 	HashTable *debug_info;
143 	BreakIterator_object	*bio;
144 	const BreakIterator		*biter;
145 
146 	*is_temp = 1;
147 
148 	debug_info = zend_new_array(8);
149 
150 	bio  = Z_INTL_BREAKITERATOR_P(object);
151 	biter = bio->biter;
152 
153 	if (biter == NULL) {
154 		ZVAL_FALSE(&val);
155 		zend_hash_str_update(debug_info, "valid", sizeof("valid") - 1, &val);
156 		return debug_info;
157 	}
158 	ZVAL_TRUE(&val);
159 	zend_hash_str_update(debug_info, "valid", sizeof("valid") - 1, &val);
160 
161 	if (Z_ISUNDEF(bio->text)) {
162 		ZVAL_NULL(&val);
163 		zend_hash_str_update(debug_info, "text", sizeof("text") - 1, &val);
164 	} else {
165 		Z_TRY_ADDREF(bio->text);
166 		zend_hash_str_update(debug_info, "text", sizeof("text") - 1, &bio->text);
167 	}
168 
169 	ZVAL_STRING(&val, const_cast<char*>(typeid(*biter).name()));
170 	zend_hash_str_update(debug_info, "type", sizeof("type") - 1, &val);
171 
172 	return debug_info;
173 }
174 /* }}} */
175 
176 /* {{{ void breakiterator_object_init(BreakIterator_object* to)
177  * Initialize internals of BreakIterator_object not specific to zend standard objects.
178  */
breakiterator_object_init(BreakIterator_object * bio)179 static void breakiterator_object_init(BreakIterator_object *bio)
180 {
181 	intl_error_init(BREAKITER_ERROR_P(bio));
182 	bio->biter = NULL;
183 	ZVAL_UNDEF(&bio->text);
184 }
185 /* }}} */
186 
187 /* {{{ BreakIterator_objects_free */
BreakIterator_objects_free(zend_object * object)188 static void BreakIterator_objects_free(zend_object *object)
189 {
190 	BreakIterator_object* bio = php_intl_breakiterator_fetch_object(object);
191 
192 	zval_ptr_dtor(&bio->text);
193 	if (bio->biter) {
194 		delete bio->biter;
195 		bio->biter = NULL;
196 	}
197 	intl_error_reset(BREAKITER_ERROR_P(bio));
198 
199 	zend_object_std_dtor(&bio->zo);
200 }
201 /* }}} */
202 
203 /* {{{ BreakIterator_object_create */
BreakIterator_object_create(zend_class_entry * ce)204 static zend_object *BreakIterator_object_create(zend_class_entry *ce)
205 {
206 	BreakIterator_object*	intern;
207 
208 	intern = (BreakIterator_object*)ecalloc(1, sizeof(BreakIterator_object) + sizeof(zval) * (ce->default_properties_count - 1));
209 
210 	zend_object_std_init(&intern->zo, ce);
211     object_properties_init((zend_object*) intern, ce);
212 	breakiterator_object_init(intern);
213 
214 	intern->zo.handlers = &BreakIterator_handlers;
215 
216 	return &intern->zo;
217 }
218 /* }}} */
219 
220 /* {{{ BreakIterator/RuleBasedBreakIterator methods arguments info */
221 
222 ZEND_BEGIN_ARG_INFO_EX(ainfo_biter_void, 0, 0, 0)
223 ZEND_END_ARG_INFO()
224 
225 ZEND_BEGIN_ARG_INFO_EX(ainfo_biter_locale, 0, 0, 0)
226 	ZEND_ARG_INFO(0, locale)
227 ZEND_END_ARG_INFO()
228 
229 ZEND_BEGIN_ARG_INFO_EX(ainfo_biter_setText, 0, 0, 1)
230 	ZEND_ARG_INFO(0, text)
231 ZEND_END_ARG_INFO()
232 
233 ZEND_BEGIN_ARG_INFO_EX(ainfo_biter_next, 0, 0, 0)
234 	ZEND_ARG_INFO(0, offset)
235 ZEND_END_ARG_INFO()
236 
237 ZEND_BEGIN_ARG_INFO_EX(ainfo_biter_offset, 0, 0, 1)
238 	ZEND_ARG_INFO(0, offset)
239 ZEND_END_ARG_INFO()
240 
241 ZEND_BEGIN_ARG_INFO_EX(ainfo_biter_get_locale, 0, 0, 1)
242 	ZEND_ARG_INFO(0, locale_type)
243 ZEND_END_ARG_INFO()
244 
245 ZEND_BEGIN_ARG_INFO_EX(ainfo_biter_getPartsIterator, 0, 0, 0)
246 	ZEND_ARG_INFO(0, key_type)
247 ZEND_END_ARG_INFO()
248 
249 ZEND_BEGIN_ARG_INFO_EX(ainfo_rbbi___construct, 0, 0, 1)
250 	ZEND_ARG_INFO(0, rules)
251 	ZEND_ARG_INFO(0, areCompiled)
252 ZEND_END_ARG_INFO()
253 
254 /* }}} */
255 
256 /* {{{ BreakIterator_class_functions
257  * Every 'BreakIterator' class method has an entry in this table
258  */
259 static const zend_function_entry BreakIterator_class_functions[] = {
260 	PHP_ME(BreakIterator,					__construct,							ainfo_biter_void,					ZEND_ACC_PRIVATE)
261 	PHP_ME_MAPPING(createWordInstance,		breakiter_create_word_instance,			ainfo_biter_locale,					ZEND_ACC_STATIC | ZEND_ACC_PUBLIC)
262 	PHP_ME_MAPPING(createLineInstance,		breakiter_create_line_instance,			ainfo_biter_locale,					ZEND_ACC_STATIC | ZEND_ACC_PUBLIC)
263 	PHP_ME_MAPPING(createCharacterInstance,	breakiter_create_character_instance,	ainfo_biter_locale,					ZEND_ACC_STATIC | ZEND_ACC_PUBLIC)
264 	PHP_ME_MAPPING(createSentenceInstance,	breakiter_create_sentence_instance,		ainfo_biter_locale,					ZEND_ACC_STATIC | ZEND_ACC_PUBLIC)
265 	PHP_ME_MAPPING(createTitleInstance,		breakiter_create_title_instance,		ainfo_biter_locale,					ZEND_ACC_STATIC | ZEND_ACC_PUBLIC)
266 	PHP_ME_MAPPING(createCodePointInstance,	breakiter_create_code_point_instance,	ainfo_biter_void,					ZEND_ACC_STATIC | ZEND_ACC_PUBLIC)
267 	PHP_ME_MAPPING(getText,					breakiter_get_text,						ainfo_biter_void,					ZEND_ACC_PUBLIC)
268 	PHP_ME_MAPPING(setText,					breakiter_set_text,						ainfo_biter_setText,				ZEND_ACC_PUBLIC)
269 	PHP_ME_MAPPING(first,					breakiter_first,						ainfo_biter_void,					ZEND_ACC_PUBLIC)
270 	PHP_ME_MAPPING(last,					breakiter_last,							ainfo_biter_void,					ZEND_ACC_PUBLIC)
271 	PHP_ME_MAPPING(previous,				breakiter_previous,						ainfo_biter_void,					ZEND_ACC_PUBLIC)
272 	PHP_ME_MAPPING(next,					breakiter_next,							ainfo_biter_next,					ZEND_ACC_PUBLIC)
273 	PHP_ME_MAPPING(current,					breakiter_current,						ainfo_biter_void,					ZEND_ACC_PUBLIC)
274 	PHP_ME_MAPPING(following,				breakiter_following,					ainfo_biter_offset,					ZEND_ACC_PUBLIC)
275 	PHP_ME_MAPPING(preceding,				breakiter_preceding,					ainfo_biter_offset,					ZEND_ACC_PUBLIC)
276 	PHP_ME_MAPPING(isBoundary,				breakiter_is_boundary,					ainfo_biter_offset,					ZEND_ACC_PUBLIC)
277 	PHP_ME_MAPPING(getLocale,				breakiter_get_locale,					ainfo_biter_get_locale,				ZEND_ACC_PUBLIC)
278 	PHP_ME_MAPPING(getPartsIterator,		breakiter_get_parts_iterator,			ainfo_biter_getPartsIterator,		ZEND_ACC_PUBLIC)
279 
280 	PHP_ME_MAPPING(getErrorCode,			breakiter_get_error_code,				ainfo_biter_void,					ZEND_ACC_PUBLIC)
281 	PHP_ME_MAPPING(getErrorMessage,			breakiter_get_error_message,			ainfo_biter_void,					ZEND_ACC_PUBLIC)
282 	PHP_FE_END
283 };
284 /* }}} */
285 
286 /* {{{ RuleBasedBreakIterator_class_functions
287  */
288 static const zend_function_entry RuleBasedBreakIterator_class_functions[] = {
289 	PHP_ME(IntlRuleBasedBreakIterator,		__construct,							ainfo_rbbi___construct,				ZEND_ACC_PUBLIC)
290 	PHP_ME_MAPPING(getRules,				rbbi_get_rules,							ainfo_biter_void,					ZEND_ACC_PUBLIC)
291 	PHP_ME_MAPPING(getRuleStatus,			rbbi_get_rule_status,					ainfo_biter_void,					ZEND_ACC_PUBLIC)
292 	PHP_ME_MAPPING(getRuleStatusVec,		rbbi_get_rule_status_vec,				ainfo_biter_void,					ZEND_ACC_PUBLIC)
293 	PHP_ME_MAPPING(getBinaryRules,			rbbi_get_binary_rules,					ainfo_biter_void,					ZEND_ACC_PUBLIC)
294 	PHP_FE_END
295 };
296 /* }}} */
297 
298 /* {{{ CodePointBreakIterator_class_functions
299  */
300 static const zend_function_entry CodePointBreakIterator_class_functions[] = {
301 	PHP_ME_MAPPING(getLastCodePoint,		cpbi_get_last_code_point,				ainfo_biter_void,					ZEND_ACC_PUBLIC)
302 	PHP_FE_END
303 };
304 /* }}} */
305 
306 
307 /* {{{ breakiterator_register_BreakIterator_class
308  * Initialize 'BreakIterator' class
309  */
breakiterator_register_BreakIterator_class(void)310 U_CFUNC void breakiterator_register_BreakIterator_class(void)
311 {
312 	zend_class_entry ce;
313 
314 	/* Create and register 'BreakIterator' class. */
315 	INIT_CLASS_ENTRY(ce, "IntlBreakIterator", BreakIterator_class_functions);
316 	ce.create_object = BreakIterator_object_create;
317 	ce.get_iterator = _breakiterator_get_iterator;
318 	BreakIterator_ce_ptr = zend_register_internal_class(&ce);
319 
320 	memcpy(&BreakIterator_handlers, &std_object_handlers,
321 		sizeof BreakIterator_handlers);
322 	BreakIterator_handlers.offset = XtOffsetOf(BreakIterator_object, zo);
323 	BreakIterator_handlers.compare_objects = BreakIterator_compare_objects;
324 	BreakIterator_handlers.clone_obj = BreakIterator_clone_obj;
325 	BreakIterator_handlers.get_debug_info = BreakIterator_get_debug_info;
326 	BreakIterator_handlers.free_obj = BreakIterator_objects_free;
327 
328 	zend_class_implements(BreakIterator_ce_ptr, 1,
329 			zend_ce_traversable);
330 
331 	zend_declare_class_constant_long(BreakIterator_ce_ptr,
332 		"DONE", sizeof("DONE") - 1, BreakIterator::DONE );
333 
334 	/* Declare constants that are defined in the C header */
335 #define BREAKITER_DECL_LONG_CONST(name) \
336 	zend_declare_class_constant_long(BreakIterator_ce_ptr, #name, \
337 		sizeof(#name) - 1, UBRK_ ## name)
338 
339 	BREAKITER_DECL_LONG_CONST(WORD_NONE);
340 	BREAKITER_DECL_LONG_CONST(WORD_NONE_LIMIT);
341 	BREAKITER_DECL_LONG_CONST(WORD_NUMBER);
342 	BREAKITER_DECL_LONG_CONST(WORD_NUMBER_LIMIT);
343 	BREAKITER_DECL_LONG_CONST(WORD_LETTER);
344 	BREAKITER_DECL_LONG_CONST(WORD_LETTER_LIMIT);
345 	BREAKITER_DECL_LONG_CONST(WORD_KANA);
346 	BREAKITER_DECL_LONG_CONST(WORD_KANA_LIMIT);
347 	BREAKITER_DECL_LONG_CONST(WORD_IDEO);
348 	BREAKITER_DECL_LONG_CONST(WORD_IDEO_LIMIT);
349 
350 	BREAKITER_DECL_LONG_CONST(LINE_SOFT);
351 	BREAKITER_DECL_LONG_CONST(LINE_SOFT_LIMIT);
352 	BREAKITER_DECL_LONG_CONST(LINE_HARD);
353 	BREAKITER_DECL_LONG_CONST(LINE_HARD_LIMIT);
354 
355 	BREAKITER_DECL_LONG_CONST(SENTENCE_TERM);
356 	BREAKITER_DECL_LONG_CONST(SENTENCE_TERM_LIMIT);
357 	BREAKITER_DECL_LONG_CONST(SENTENCE_SEP);
358 	BREAKITER_DECL_LONG_CONST(SENTENCE_SEP_LIMIT);
359 
360 #undef BREAKITER_DECL_LONG_CONST
361 
362 
363 	/* Create and register 'RuleBasedBreakIterator' class. */
364 	INIT_CLASS_ENTRY(ce, "IntlRuleBasedBreakIterator",
365 			RuleBasedBreakIterator_class_functions);
366 	RuleBasedBreakIterator_ce_ptr = zend_register_internal_class_ex(&ce,
367 			BreakIterator_ce_ptr);
368 
369 	/* Create and register 'CodePointBreakIterator' class. */
370 	INIT_CLASS_ENTRY(ce, "IntlCodePointBreakIterator",
371 			CodePointBreakIterator_class_functions);
372 	CodePointBreakIterator_ce_ptr = zend_register_internal_class_ex(&ce,
373 			BreakIterator_ce_ptr);
374 }
375 /* }}} */
376