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 	intern->zo.handlers = &Spoofchecker_handlers;
51 
52 	return &intern->zo;
53 }
54 /* }}} */
55 
56 /*
57  * 'Spoofchecker' class registration structures & functions
58  */
59 
60 /* {{{ Spoofchecker_class_functions
61  * Every 'Spoofchecker' class method has an entry in this table
62  */
63 
spoofchecker_clone_obj(zend_object * object)64 static zend_object *spoofchecker_clone_obj(zend_object *object) /* {{{ */
65 {
66 	zend_object *new_obj_val;
67 	Spoofchecker_object *sfo, *new_sfo;
68 
69 	sfo = php_intl_spoofchecker_fetch_object(object);
70 	intl_error_reset(SPOOFCHECKER_ERROR_P(sfo));
71 
72 	new_obj_val = Spoofchecker_ce_ptr->create_object(object->ce);
73 	new_sfo = php_intl_spoofchecker_fetch_object(new_obj_val);
74 	/* clone standard parts */
75 	zend_objects_clone_members(&new_sfo->zo, &sfo->zo);
76 	/* clone internal object */
77 	new_sfo->uspoof = uspoof_clone(sfo->uspoof, SPOOFCHECKER_ERROR_CODE_P(new_sfo));
78 	if(U_FAILURE(SPOOFCHECKER_ERROR_CODE(new_sfo))) {
79 		/* set up error in case error handler is interested */
80 		intl_error_set( NULL, SPOOFCHECKER_ERROR_CODE(new_sfo), "Failed to clone SpoofChecker object", 0 );
81 		Spoofchecker_objects_free(&new_sfo->zo); /* free new object */
82 		zend_error(E_ERROR, "Failed to clone SpoofChecker object");
83 	}
84 	return new_obj_val;
85 }
86 /* }}} */
87 
88 /* {{{ spoofchecker_register_Spoofchecker_class
89  * Initialize 'Spoofchecker' class
90  */
spoofchecker_register_Spoofchecker_class(void)91 void spoofchecker_register_Spoofchecker_class(void)
92 {
93 	/* Create and register 'Spoofchecker' class. */
94 	Spoofchecker_ce_ptr = register_class_Spoofchecker();
95 	Spoofchecker_ce_ptr->create_object = Spoofchecker_object_create;
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 	intl_error_reset(SPOOFCHECKER_ERROR_P(co));
134 }
135 /* }}} */
136