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 #ifndef SPOOFCHECKER_CLASS_H
16 #define SPOOFCHECKER_CLASS_H
17 
18 #include <php.h>
19 
20 #include "intl_common.h"
21 #include "intl_error.h"
22 #include "intl_data.h"
23 
24 #include <unicode/uspoof.h>
25 
26 typedef struct {
27 	// error handling
28 	intl_error  err;
29 
30 	// ICU Spoofchecker
31 	USpoofChecker*     uspoof;
32 #if U_ICU_VERSION_MAJOR_NUM >= 58
33 	USpoofCheckResult* uspoofres;
34 #endif
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 		zend_throw_error(NULL, "Found unconstructed Spoofchecker");	\
69 		RETURN_THROWS();											\
70 	}
71 
72 // Macro to check return value of a ucol_* function call.
73 #define SPOOFCHECKER_CHECK_STATUS(co, msg)                                        \
74     intl_error_set_code(NULL, SPOOFCHECKER_ERROR_CODE(co));           \
75     if (U_FAILURE(SPOOFCHECKER_ERROR_CODE(co))) {                                  \
76         intl_errors_set_custom_msg(SPOOFCHECKER_ERROR_P(co), msg, 0); \
77         RETURN_FALSE;                                                           \
78     }                                                                           \
79 
80 #if U_ICU_VERSION_MAJOR_NUM >= 58
81 #define SPOOFCHECKER_DEFAULT_RESTRICTION_LEVEL USPOOF_HIGHLY_RESTRICTIVE
82 #endif
83 
84 #endif // #ifndef SPOOFCHECKER_CLASS_H
85