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: Gustavo Lopes <cataphract@php.net>                          |
12    +----------------------------------------------------------------------+
13 */
14 
15 #ifdef HAVE_CONFIG_H
16 #include "config.h"
17 #endif
18 
19 #include <unicode/brkiter.h>
20 #include <unicode/rbbi.h>
21 #include "codepointiterator_internal.h"
22 
23 #include "breakiterator_iterators.h"
24 
25 #include <typeinfo>
26 
27 extern "C" {
28 #define USE_BREAKITERATOR_POINTER 1
29 #include "breakiterator_class.h"
30 #include "breakiterator_arginfo.h"
31 #include <zend_exceptions.h>
32 #include <zend_interfaces.h>
33 #include <assert.h>
34 }
35 
36 using PHP::CodePointBreakIterator;
37 using icu::RuleBasedBreakIterator;
38 
39 /* {{{ Global variables */
40 zend_class_entry *BreakIterator_ce_ptr;
41 zend_class_entry *RuleBasedBreakIterator_ce_ptr;
42 zend_class_entry *CodePointBreakIterator_ce_ptr;
43 zend_object_handlers BreakIterator_handlers;
44 /* }}} */
45 
breakiterator_object_create(zval * object,BreakIterator * biter,int brand_new)46 U_CFUNC	void breakiterator_object_create(zval *object,
47 										 BreakIterator *biter, int brand_new)
48 {
49 	UClassID classId = biter->getDynamicClassID();
50 	zend_class_entry *ce;
51 
52 	if (classId == RuleBasedBreakIterator::getStaticClassID()) {
53 		ce = RuleBasedBreakIterator_ce_ptr;
54 	} else if (classId == CodePointBreakIterator::getStaticClassID()) {
55 		ce = CodePointBreakIterator_ce_ptr;
56 	} else {
57 		ce = BreakIterator_ce_ptr;
58 	}
59 
60 	if (brand_new) {
61 		object_init_ex(object, ce);
62 	}
63 	breakiterator_object_construct(object, biter);
64 }
65 
breakiterator_object_construct(zval * object,BreakIterator * biter)66 U_CFUNC void breakiterator_object_construct(zval *object,
67 											BreakIterator *biter)
68 {
69 	BreakIterator_object *bio;
70 
71 	BREAKITER_METHOD_FETCH_OBJECT_NO_CHECK; //populate to from object
72 	ZEND_ASSERT(bio->biter == NULL);
73 	bio->biter = biter;
74 }
75 
76 /* {{{ compare handler for BreakIterator */
BreakIterator_compare_objects(zval * object1,zval * object2)77 static int BreakIterator_compare_objects(zval *object1,
78 										 zval *object2)
79 {
80 	BreakIterator_object	*bio1,
81 							*bio2;
82 
83 	ZEND_COMPARE_OBJECTS_FALLBACK(object1, object2);
84 
85 	bio1 = Z_INTL_BREAKITERATOR_P(object1);
86 	bio2 = Z_INTL_BREAKITERATOR_P(object2);
87 
88 	if (bio1->biter == NULL || bio2->biter == NULL) {
89 		return bio1->biter == bio2->biter ? 0 : 1;
90 	}
91 
92 	return *bio1->biter == *bio2->biter ? 0 : 1;
93 }
94 /* }}} */
95 
96 /* {{{ clone handler for BreakIterator */
BreakIterator_clone_obj(zend_object * object)97 static zend_object *BreakIterator_clone_obj(zend_object *object)
98 {
99 	BreakIterator_object *bio_orig = php_intl_breakiterator_fetch_object(object);
100 	zend_object *ret_val           = BreakIterator_ce_ptr->create_object(object->ce);
101 	BreakIterator_object *bio_new  = php_intl_breakiterator_fetch_object(ret_val);
102 
103 	zend_objects_clone_members(&bio_new->zo, &bio_orig->zo);
104 
105 	if (bio_orig->biter != NULL) {
106 		BreakIterator *new_biter = bio_orig->biter->clone();
107 		if (!new_biter) {
108 			zend_throw_error(NULL, "Failed to clone BreakIterator");
109 		} else {
110 			bio_new->biter = new_biter;
111 			ZVAL_COPY(&bio_new->text, &bio_orig->text);
112 		}
113 	} else {
114 		zend_throw_error(NULL, "Cannot clone uninitialized BreakIterator");
115 	}
116 
117 	return ret_val;
118 }
119 /* }}} */
120 
121 /* {{{ get_debug_info handler for BreakIterator */
BreakIterator_get_debug_info(zend_object * object,int * is_temp)122 static HashTable *BreakIterator_get_debug_info(zend_object *object, int *is_temp)
123 {
124 	zval val;
125 	HashTable *debug_info;
126 	BreakIterator_object	*bio;
127 	const BreakIterator		*biter;
128 
129 	*is_temp = 1;
130 
131 	debug_info = zend_new_array(8);
132 
133 	bio  = php_intl_breakiterator_fetch_object(object);
134 	biter = bio->biter;
135 
136 	if (biter == NULL) {
137 		ZVAL_FALSE(&val);
138 		zend_hash_str_update(debug_info, "valid", sizeof("valid") - 1, &val);
139 		return debug_info;
140 	}
141 	ZVAL_TRUE(&val);
142 	zend_hash_str_update(debug_info, "valid", sizeof("valid") - 1, &val);
143 
144 	if (Z_ISUNDEF(bio->text)) {
145 		ZVAL_NULL(&val);
146 		zend_hash_str_update(debug_info, "text", sizeof("text") - 1, &val);
147 	} else {
148 		Z_TRY_ADDREF(bio->text);
149 		zend_hash_str_update(debug_info, "text", sizeof("text") - 1, &bio->text);
150 	}
151 
152 	ZVAL_STRING(&val, const_cast<char*>(typeid(*biter).name()));
153 	zend_hash_str_update(debug_info, "type", sizeof("type") - 1, &val);
154 
155 	return debug_info;
156 }
157 /* }}} */
158 
159 /* {{{ void breakiterator_object_init(BreakIterator_object* to)
160  * Initialize internals of BreakIterator_object not specific to zend standard objects.
161  */
breakiterator_object_init(BreakIterator_object * bio)162 static void breakiterator_object_init(BreakIterator_object *bio)
163 {
164 	intl_error_init(BREAKITER_ERROR_P(bio));
165 	bio->biter = NULL;
166 	ZVAL_UNDEF(&bio->text);
167 }
168 /* }}} */
169 
170 /* {{{ BreakIterator_objects_free */
BreakIterator_objects_free(zend_object * object)171 static void BreakIterator_objects_free(zend_object *object)
172 {
173 	BreakIterator_object* bio = php_intl_breakiterator_fetch_object(object);
174 
175 	zval_ptr_dtor(&bio->text);
176 	if (bio->biter) {
177 		delete bio->biter;
178 		bio->biter = NULL;
179 	}
180 	intl_error_reset(BREAKITER_ERROR_P(bio));
181 
182 	zend_object_std_dtor(&bio->zo);
183 }
184 /* }}} */
185 
186 /* {{{ BreakIterator_object_create */
BreakIterator_object_create(zend_class_entry * ce)187 static zend_object *BreakIterator_object_create(zend_class_entry *ce)
188 {
189 	BreakIterator_object*	intern;
190 
191 	intern = (BreakIterator_object*) zend_object_alloc(sizeof(BreakIterator_object), ce);
192 
193 	zend_object_std_init(&intern->zo, ce);
194 	object_properties_init(&intern->zo, ce);
195 	breakiterator_object_init(intern);
196 
197 	return &intern->zo;
198 }
199 /* }}} */
200 
201 /* {{{ breakiterator_register_BreakIterator_class
202  * Initialize 'BreakIterator' class
203  */
breakiterator_register_BreakIterator_class(void)204 U_CFUNC void breakiterator_register_BreakIterator_class(void)
205 {
206 	/* Create and register 'BreakIterator' class. */
207 
208 	BreakIterator_ce_ptr = register_class_IntlBreakIterator(zend_ce_aggregate);
209 	BreakIterator_ce_ptr->create_object = BreakIterator_object_create;
210 	BreakIterator_ce_ptr->default_object_handlers = &BreakIterator_handlers;
211 	BreakIterator_ce_ptr->get_iterator = _breakiterator_get_iterator;
212 
213 	memcpy(&BreakIterator_handlers, &std_object_handlers,
214 		sizeof BreakIterator_handlers);
215 	BreakIterator_handlers.offset = XtOffsetOf(BreakIterator_object, zo);
216 	BreakIterator_handlers.compare = BreakIterator_compare_objects;
217 	BreakIterator_handlers.clone_obj = BreakIterator_clone_obj;
218 	BreakIterator_handlers.get_debug_info = BreakIterator_get_debug_info;
219 	BreakIterator_handlers.free_obj = BreakIterator_objects_free;
220 
221 	/* Create and register 'RuleBasedBreakIterator' class. */
222 	RuleBasedBreakIterator_ce_ptr = register_class_IntlRuleBasedBreakIterator(BreakIterator_ce_ptr);
223 
224 	/* Create and register 'CodePointBreakIterator' class. */
225 	CodePointBreakIterator_ce_ptr = register_class_IntlCodePointBreakIterator(BreakIterator_ce_ptr);
226 }
227 /* }}} */
228