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 	zend_object *new_obj_val;
65 	Spoofchecker_object *sfo, *new_sfo;
66 
67 	sfo = php_intl_spoofchecker_fetch_object(object);
68 	intl_error_reset(SPOOFCHECKER_ERROR_P(sfo));
69 
70 	new_obj_val = Spoofchecker_ce_ptr->create_object(object->ce);
71 	new_sfo = php_intl_spoofchecker_fetch_object(new_obj_val);
72 	/* clone standard parts */
73 	zend_objects_clone_members(&new_sfo->zo, &sfo->zo);
74 	/* clone internal object */
75 	new_sfo->uspoof = uspoof_clone(sfo->uspoof, SPOOFCHECKER_ERROR_CODE_P(new_sfo));
76 	if(U_FAILURE(SPOOFCHECKER_ERROR_CODE(new_sfo))) {
77 		/* set up error in case error handler is interested */
78 		intl_error_set( NULL, SPOOFCHECKER_ERROR_CODE(new_sfo), "Failed to clone SpoofChecker object", 0 );
79 		Spoofchecker_objects_free(&new_sfo->zo); /* free new object */
80 		zend_error(E_ERROR, "Failed to clone SpoofChecker object");
81 	}
82 	return new_obj_val;
83 }
84 /* }}} */
85 
86 /* {{{ spoofchecker_register_Spoofchecker_class
87  * Initialize 'Spoofchecker' class
88  */
spoofchecker_register_Spoofchecker_class(void)89 void spoofchecker_register_Spoofchecker_class(void)
90 {
91 	/* Create and register 'Spoofchecker' class. */
92 	Spoofchecker_ce_ptr = register_class_Spoofchecker();
93 	Spoofchecker_ce_ptr->create_object = Spoofchecker_object_create;
94 	Spoofchecker_ce_ptr->default_object_handlers = &Spoofchecker_handlers;
95 
96 	memcpy(&Spoofchecker_handlers, &std_object_handlers,
97 		sizeof Spoofchecker_handlers);
98 	Spoofchecker_handlers.offset = XtOffsetOf(Spoofchecker_object, zo);
99 	Spoofchecker_handlers.clone_obj = spoofchecker_clone_obj;
100 	Spoofchecker_handlers.free_obj = Spoofchecker_objects_free;
101 }
102 /* }}} */
103 
104 /* {{{ void spoofchecker_object_init( Spoofchecker_object* co )
105  * Initialize internals of Spoofchecker_object.
106  * Must be called before any other call to 'spoofchecker_object_...' functions.
107  */
spoofchecker_object_init(Spoofchecker_object * co)108 void spoofchecker_object_init(Spoofchecker_object* co)
109 {
110 	if (!co) {
111 		return;
112 	}
113 
114 	intl_error_init(SPOOFCHECKER_ERROR_P(co));
115 }
116 /* }}} */
117 
118 /* {{{ void spoofchecker_object_destroy( Spoofchecker_object* co )
119  * Clean up mem allocted by internals of Spoofchecker_object
120  */
spoofchecker_object_destroy(Spoofchecker_object * co)121 void spoofchecker_object_destroy(Spoofchecker_object* co)
122 {
123 	if (!co) {
124 		return;
125 	}
126 
127 	if (co->uspoof) {
128 		uspoof_close(co->uspoof);
129 		co->uspoof = NULL;
130 	}
131 
132 #if U_ICU_VERSION_MAJOR_NUM >= 58
133 	if (co->uspoofres) {
134 		uspoof_closeCheckResult(co->uspoofres);
135 		co->uspoofres = NULL;
136 	}
137 #endif
138 
139 	intl_error_reset(SPOOFCHECKER_ERROR_P(co));
140 }
141 /* }}} */
142