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: Scott MacVicar <scottmac@php.net>                           |
14    +----------------------------------------------------------------------+
15  */
16 
17 #ifndef SPOOFCHECKER_CLASS_H
18 #define SPOOFCHECKER_CLASS_H
19 
20 #include <php.h>
21 
22 #include "intl_common.h"
23 #include "spoofchecker_create.h"
24 #include "intl_error.h"
25 #include "intl_data.h"
26 
27 #include <unicode/uspoof.h>
28 
29 typedef struct {
30 	// error handling
31 	intl_error  err;
32 
33 	// ICU Spoofchecker
34 	USpoofChecker*      uspoof;
35 
36 	zend_object     zo;
37 } Spoofchecker_object;
38 
php_intl_spoofchecker_fetch_object(zend_object * obj)39 static inline Spoofchecker_object *php_intl_spoofchecker_fetch_object(zend_object *obj) {
40 	    return (Spoofchecker_object *)((char*)(obj) - XtOffsetOf(Spoofchecker_object, zo));
41 }
42 #define Z_INTL_SPOOFCHECKER_P(zv) php_intl_spoofchecker_fetch_object((Z_OBJ_P(zv)))
43 
44 #define SPOOFCHECKER_ERROR(co) (co)->err
45 #define SPOOFCHECKER_ERROR_P(co) &(SPOOFCHECKER_ERROR(co))
46 
47 #define SPOOFCHECKER_ERROR_CODE(co)   INTL_ERROR_CODE(SPOOFCHECKER_ERROR(co))
48 #define SPOOFCHECKER_ERROR_CODE_P(co) &(INTL_ERROR_CODE(SPOOFCHECKER_ERROR(co)))
49 
50 void spoofchecker_register_Spoofchecker_class(void);
51 
52 void spoofchecker_object_init(Spoofchecker_object* co);
53 void spoofchecker_object_destroy(Spoofchecker_object* co);
54 
55 extern zend_class_entry *Spoofchecker_ce_ptr;
56 
57 /* Auxiliary macros */
58 
59 #define SPOOFCHECKER_METHOD_INIT_VARS       \
60     zval*             object  = ZEND_THIS;  \
61     Spoofchecker_object*  co  = NULL;   \
62     intl_error_reset(NULL); \
63 
64 #define SPOOFCHECKER_METHOD_FETCH_OBJECT_NO_CHECK	INTL_METHOD_FETCH_OBJECT(INTL_SPOOFCHECKER, co)
65 #define SPOOFCHECKER_METHOD_FETCH_OBJECT							\
66 	SPOOFCHECKER_METHOD_FETCH_OBJECT_NO_CHECK;						\
67 	if (co->uspoof == NULL)	{										\
68 		intl_errors_set(&co->err, U_ILLEGAL_ARGUMENT_ERROR,			\
69 				"Found unconstructed Spoofchecker", 0);	\
70 		RETURN_FALSE;												\
71 	}
72 
73 // Macro to check return value of a ucol_* function call.
74 #define SPOOFCHECKER_CHECK_STATUS(co, msg)                                        \
75     intl_error_set_code(NULL, SPOOFCHECKER_ERROR_CODE(co));           \
76     if (U_FAILURE(SPOOFCHECKER_ERROR_CODE(co))) {                                  \
77         intl_errors_set_custom_msg(SPOOFCHECKER_ERROR_P(co), msg, 0); \
78         RETURN_FALSE;                                                           \
79     }                                                                           \
80 
81 #if U_ICU_VERSION_MAJOR_NUM >= 58
82 #define SPOOFCHECKER_DEFAULT_RESTRICTION_LEVEL USPOOF_HIGHLY_RESTRICTIVE
83 #endif
84 
85 #endif // #ifndef SPOOFCHECKER_CLASS_H
86