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 
42 /* {{{ Global variables */
43 zend_class_entry *BreakIterator_ce_ptr;
44 zend_class_entry *RuleBasedBreakIterator_ce_ptr;
45 zend_class_entry *CodePointBreakIterator_ce_ptr;
46 zend_object_handlers BreakIterator_handlers;
47 /* }}} */
48 
breakiterator_object_create(zval * object,BreakIterator * biter,int brand_new)49 U_CFUNC	void breakiterator_object_create(zval *object,
50 										 BreakIterator *biter, int brand_new)
51 {
52 	UClassID classId = biter->getDynamicClassID();
53 	zend_class_entry *ce;
54 
55 	if (classId == RuleBasedBreakIterator::getStaticClassID()) {
56 		ce = RuleBasedBreakIterator_ce_ptr;
57 	} else if (classId == CodePointBreakIterator::getStaticClassID()) {
58 		ce = CodePointBreakIterator_ce_ptr;
59 	} else {
60 		ce = BreakIterator_ce_ptr;
61 	}
62 
63 	if (brand_new) {
64 		object_init_ex(object, ce);
65 	}
66 	breakiterator_object_construct(object, biter);
67 }
68 
breakiterator_object_construct(zval * object,BreakIterator * biter)69 U_CFUNC void breakiterator_object_construct(zval *object,
70 											BreakIterator *biter)
71 {
72 	BreakIterator_object *bio;
73 
74 	BREAKITER_METHOD_FETCH_OBJECT_NO_CHECK; //populate to from object
75 	assert(bio->biter == NULL);
76 	bio->biter = biter;
77 }
78 
79 /* {{{ compare handler for BreakIterator */
BreakIterator_compare_objects(zval * object1,zval * object2)80 static int BreakIterator_compare_objects(zval *object1,
81 										 zval *object2)
82 {
83 	BreakIterator_object	*bio1,
84 							*bio2;
85 
86 	bio1 = Z_INTL_BREAKITERATOR_P(object1);
87 	bio2 = Z_INTL_BREAKITERATOR_P(object2);
88 
89 	if (bio1->biter == NULL || bio2->biter == NULL) {
90 		return bio1->biter == bio2->biter ? 0 : 1;
91 	}
92 
93 	return *bio1->biter == *bio2->biter ? 0 : 1;
94 }
95 /* }}} */
96 
97 /* {{{ clone handler for BreakIterator */
BreakIterator_clone_obj(zval * object)98 static zend_object *BreakIterator_clone_obj(zval *object)
99 {
100 	BreakIterator_object	*bio_orig,
101 							*bio_new;
102 	zend_object				*ret_val;
103 
104 	bio_orig = Z_INTL_BREAKITERATOR_P(object);
105 	intl_errors_reset(INTL_DATA_ERROR_P(bio_orig));
106 
107 	ret_val = BreakIterator_ce_ptr->create_object(Z_OBJCE_P(object));
108 	bio_new  = php_intl_breakiterator_fetch_object(ret_val);
109 
110 	zend_objects_clone_members(&bio_new->zo, &bio_orig->zo);
111 
112 	if (bio_orig->biter != NULL) {
113 		BreakIterator *new_biter;
114 
115 		new_biter = bio_orig->biter->clone();
116 		if (!new_biter) {
117 			zend_string *err_msg;
118 			intl_errors_set_code(BREAKITER_ERROR_P(bio_orig),
119 				U_MEMORY_ALLOCATION_ERROR);
120 			intl_errors_set_custom_msg(BREAKITER_ERROR_P(bio_orig),
121 				"Could not clone BreakIterator", 0);
122 			err_msg = intl_error_get_message(BREAKITER_ERROR_P(bio_orig));
123 			zend_throw_exception(NULL, ZSTR_VAL(err_msg), 0);
124 			zend_string_free(err_msg);
125 		} else {
126 			bio_new->biter = new_biter;
127 			ZVAL_COPY(&bio_new->text, &bio_orig->text);
128 		}
129 	} else {
130 		zend_throw_exception(NULL, "Cannot clone unconstructed BreakIterator", 0);
131 	}
132 
133 	return ret_val;
134 }
135 /* }}} */
136 
137 /* {{{ get_debug_info handler for BreakIterator */
BreakIterator_get_debug_info(zval * object,int * is_temp)138 static HashTable *BreakIterator_get_debug_info(zval *object, int *is_temp)
139 {
140 	zval val;
141 	HashTable *debug_info;
142 	BreakIterator_object	*bio;
143 	const BreakIterator		*biter;
144 
145 	*is_temp = 1;
146 
147 	ALLOC_HASHTABLE(debug_info);
148 	zend_hash_init(debug_info, 8, NULL, ZVAL_PTR_DTOR, 0);
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 #if U_ICU_VERSION_MAJOR_NUM * 10 + U_ICU_VERSION_MINOR_NUM >= 48
294 	PHP_ME_MAPPING(getBinaryRules,			rbbi_get_binary_rules,					ainfo_biter_void,					ZEND_ACC_PUBLIC)
295 #endif
296 	PHP_FE_END
297 };
298 /* }}} */
299 
300 /* {{{ CodePointBreakIterator_class_functions
301  */
302 static const zend_function_entry CodePointBreakIterator_class_functions[] = {
303 	PHP_ME_MAPPING(getLastCodePoint,		cpbi_get_last_code_point,				ainfo_biter_void,					ZEND_ACC_PUBLIC)
304 	PHP_FE_END
305 };
306 /* }}} */
307 
308 
309 /* {{{ breakiterator_register_BreakIterator_class
310  * Initialize 'BreakIterator' class
311  */
breakiterator_register_BreakIterator_class(void)312 U_CFUNC void breakiterator_register_BreakIterator_class(void)
313 {
314 	zend_class_entry ce;
315 
316 	/* Create and register 'BreakIterator' class. */
317 	INIT_CLASS_ENTRY(ce, "IntlBreakIterator", BreakIterator_class_functions);
318 	ce.create_object = BreakIterator_object_create;
319 	ce.get_iterator = _breakiterator_get_iterator;
320 	BreakIterator_ce_ptr = zend_register_internal_class(&ce);
321 
322 	memcpy(&BreakIterator_handlers, zend_get_std_object_handlers(),
323 		sizeof BreakIterator_handlers);
324 	BreakIterator_handlers.offset = XtOffsetOf(BreakIterator_object, zo);
325 	BreakIterator_handlers.compare_objects = BreakIterator_compare_objects;
326 	BreakIterator_handlers.clone_obj = BreakIterator_clone_obj;
327 	BreakIterator_handlers.get_debug_info = BreakIterator_get_debug_info;
328 	BreakIterator_handlers.free_obj = BreakIterator_objects_free;
329 
330 	zend_class_implements(BreakIterator_ce_ptr, 1,
331 			zend_ce_traversable);
332 
333 	zend_declare_class_constant_long(BreakIterator_ce_ptr,
334 		"DONE", sizeof("DONE") - 1, BreakIterator::DONE );
335 
336 	/* Declare constants that are defined in the C header */
337 #define BREAKITER_DECL_LONG_CONST(name) \
338 	zend_declare_class_constant_long(BreakIterator_ce_ptr, #name, \
339 		sizeof(#name) - 1, UBRK_ ## name)
340 
341 	BREAKITER_DECL_LONG_CONST(WORD_NONE);
342 	BREAKITER_DECL_LONG_CONST(WORD_NONE_LIMIT);
343 	BREAKITER_DECL_LONG_CONST(WORD_NUMBER);
344 	BREAKITER_DECL_LONG_CONST(WORD_NUMBER_LIMIT);
345 	BREAKITER_DECL_LONG_CONST(WORD_LETTER);
346 	BREAKITER_DECL_LONG_CONST(WORD_LETTER_LIMIT);
347 	BREAKITER_DECL_LONG_CONST(WORD_KANA);
348 	BREAKITER_DECL_LONG_CONST(WORD_KANA_LIMIT);
349 	BREAKITER_DECL_LONG_CONST(WORD_IDEO);
350 	BREAKITER_DECL_LONG_CONST(WORD_IDEO_LIMIT);
351 
352 	BREAKITER_DECL_LONG_CONST(LINE_SOFT);
353 	BREAKITER_DECL_LONG_CONST(LINE_SOFT_LIMIT);
354 	BREAKITER_DECL_LONG_CONST(LINE_HARD);
355 	BREAKITER_DECL_LONG_CONST(LINE_HARD_LIMIT);
356 
357 	BREAKITER_DECL_LONG_CONST(SENTENCE_TERM);
358 	BREAKITER_DECL_LONG_CONST(SENTENCE_TERM_LIMIT);
359 	BREAKITER_DECL_LONG_CONST(SENTENCE_SEP);
360 	BREAKITER_DECL_LONG_CONST(SENTENCE_SEP_LIMIT);
361 
362 #undef BREAKITER_DECL_LONG_CONST
363 
364 
365 	/* Create and register 'RuleBasedBreakIterator' class. */
366 	INIT_CLASS_ENTRY(ce, "IntlRuleBasedBreakIterator",
367 			RuleBasedBreakIterator_class_functions);
368 	RuleBasedBreakIterator_ce_ptr = zend_register_internal_class_ex(&ce,
369 			BreakIterator_ce_ptr);
370 
371 	/* Create and register 'CodePointBreakIterator' class. */
372 	INIT_CLASS_ENTRY(ce, "IntlCodePointBreakIterator",
373 			CodePointBreakIterator_class_functions);
374 	CodePointBreakIterator_ce_ptr = zend_register_internal_class_ex(&ce,
375 			BreakIterator_ce_ptr);
376 }
377 /* }}} */
378