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: Scott MacVicar <scottmac@php.net> |
12 +----------------------------------------------------------------------+
13 */
14
15 #include "spoofchecker_class.h"
16 #include "spoofchecker_arginfo.h"
17 #include "php_intl.h"
18 #include "intl_error.h"
19
20 #include <unicode/uspoof.h>
21
22 zend_class_entry *Spoofchecker_ce_ptr = NULL;
23 static zend_object_handlers Spoofchecker_handlers;
24
25 /*
26 * Auxiliary functions needed by objects of 'Spoofchecker' class
27 */
28
29 /* {{{ Spoofchecker_objects_free */
Spoofchecker_objects_free(zend_object * object)30 void Spoofchecker_objects_free(zend_object *object)
31 {
32 Spoofchecker_object* co = php_intl_spoofchecker_fetch_object(object);
33
34 zend_object_std_dtor(&co->zo);
35
36 spoofchecker_object_destroy(co);
37 }
38 /* }}} */
39
40 /* {{{ Spoofchecker_object_create */
Spoofchecker_object_create(zend_class_entry * ce)41 zend_object *Spoofchecker_object_create(zend_class_entry *ce)
42 {
43 Spoofchecker_object* intern;
44
45 intern = zend_object_alloc(sizeof(Spoofchecker_object), ce);
46 intl_error_init(SPOOFCHECKER_ERROR_P(intern));
47 zend_object_std_init(&intern->zo, ce);
48 object_properties_init(&intern->zo, ce);
49
50 return &intern->zo;
51 }
52 /* }}} */
53
54 /*
55 * 'Spoofchecker' class registration structures & functions
56 */
57
58 /* {{{ Spoofchecker_class_functions
59 * Every 'Spoofchecker' class method has an entry in this table
60 */
61
spoofchecker_clone_obj(zend_object * object)62 static zend_object *spoofchecker_clone_obj(zend_object *object) /* {{{ */
63 {
64 Spoofchecker_object *spoofchecker_orig = php_intl_spoofchecker_fetch_object(object);
65 zend_object *new_obj_val = Spoofchecker_ce_ptr->create_object(object->ce);
66 Spoofchecker_object *spoofchecker_new = php_intl_spoofchecker_fetch_object(new_obj_val);
67
68 zend_objects_clone_members(&spoofchecker_new->zo, &spoofchecker_orig->zo);
69
70 if (spoofchecker_orig->uspoof != NULL) {
71 /* guaranteed to return NULL if it fails */
72 UErrorCode error = U_ZERO_ERROR;
73 spoofchecker_new->uspoof = uspoof_clone(spoofchecker_orig->uspoof, &error);
74 if (U_FAILURE(error)) {
75 /* free new object */
76 Spoofchecker_objects_free(&spoofchecker_new->zo);
77 zend_throw_error(NULL, "Failed to clone SpoofChecker");
78 }
79 } else {
80 zend_throw_error(NULL, "Cannot clone uninitialized SpoofChecker");
81 }
82
83 return new_obj_val;
84 }
85 /* }}} */
86
87 /* {{{ spoofchecker_register_Spoofchecker_class
88 * Initialize 'Spoofchecker' class
89 */
spoofchecker_register_Spoofchecker_class(void)90 void spoofchecker_register_Spoofchecker_class(void)
91 {
92 /* Create and register 'Spoofchecker' class. */
93 Spoofchecker_ce_ptr = register_class_Spoofchecker();
94 Spoofchecker_ce_ptr->create_object = Spoofchecker_object_create;
95 Spoofchecker_ce_ptr->default_object_handlers = &Spoofchecker_handlers;
96
97 memcpy(&Spoofchecker_handlers, &std_object_handlers,
98 sizeof Spoofchecker_handlers);
99 Spoofchecker_handlers.offset = XtOffsetOf(Spoofchecker_object, zo);
100 Spoofchecker_handlers.clone_obj = spoofchecker_clone_obj;
101 Spoofchecker_handlers.free_obj = Spoofchecker_objects_free;
102 }
103 /* }}} */
104
105 /* {{{ void spoofchecker_object_init( Spoofchecker_object* co )
106 * Initialize internals of Spoofchecker_object.
107 * Must be called before any other call to 'spoofchecker_object_...' functions.
108 */
spoofchecker_object_init(Spoofchecker_object * co)109 void spoofchecker_object_init(Spoofchecker_object* co)
110 {
111 if (!co) {
112 return;
113 }
114
115 intl_error_init(SPOOFCHECKER_ERROR_P(co));
116 }
117 /* }}} */
118
119 /* {{{ void spoofchecker_object_destroy( Spoofchecker_object* co )
120 * Clean up mem allocted by internals of Spoofchecker_object
121 */
spoofchecker_object_destroy(Spoofchecker_object * co)122 void spoofchecker_object_destroy(Spoofchecker_object* co)
123 {
124 if (!co) {
125 return;
126 }
127
128 if (co->uspoof) {
129 uspoof_close(co->uspoof);
130 co->uspoof = NULL;
131 }
132
133 #if U_ICU_VERSION_MAJOR_NUM >= 58
134 if (co->uspoofres) {
135 uspoof_closeCheckResult(co->uspoofres);
136 co->uspoofres = NULL;
137 }
138 #endif
139
140 intl_error_reset(SPOOFCHECKER_ERROR_P(co));
141 }
142 /* }}} */
143