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: Vadim Savchuk <vsavchuk@productengine.com> |
12 | Dmitry Lakhtyuk <dlakhtyuk@productengine.com> |
13 | Stanislav Malyshev <stas@zend.com> |
14 +----------------------------------------------------------------------+
15 */
16
17 #ifdef HAVE_CONFIG_H
18 #include <config.h>
19 #endif
20
21 #include <php.h>
22 #include <zend_exceptions.h>
23
24 #include "php_intl.h"
25 #include "intl_error.h"
26 #include "intl_convert.h"
27
ZEND_EXTERN_MODULE_GLOBALS(intl)28 ZEND_EXTERN_MODULE_GLOBALS( intl )
29
30 zend_class_entry *IntlException_ce_ptr;
31
32 /* {{{ Return global error structure. */
33 static intl_error* intl_g_error_get( void )
34 {
35 return &INTL_G( g_error );
36 }
37 /* }}} */
38
39 /* {{{ Free mem. */
intl_free_custom_error_msg(intl_error * err)40 static void intl_free_custom_error_msg( intl_error* err )
41 {
42 if( !err && !( err = intl_g_error_get( ) ) )
43 return;
44
45 if(err->free_custom_error_message ) {
46 efree( err->custom_error_message );
47 }
48
49 err->custom_error_message = NULL;
50 err->free_custom_error_message = 0;
51 }
52 /* }}} */
53
54 /* {{{ Create and initialize internals of 'intl_error'. */
intl_error_create(void)55 intl_error* intl_error_create( void )
56 {
57 intl_error* err = ecalloc( 1, sizeof( intl_error ) );
58
59 intl_error_init( err );
60
61 return err;
62 }
63 /* }}} */
64
65 /* {{{ Initialize internals of 'intl_error'. */
intl_error_init(intl_error * err)66 void intl_error_init( intl_error* err )
67 {
68 if( !err && !( err = intl_g_error_get( ) ) )
69 return;
70
71 err->code = U_ZERO_ERROR;
72 err->custom_error_message = NULL;
73 err->free_custom_error_message = 0;
74 }
75 /* }}} */
76
77 /* {{{ Set last error code to 0 and unset last error message */
intl_error_reset(intl_error * err)78 void intl_error_reset( intl_error* err )
79 {
80 if( !err && !( err = intl_g_error_get( ) ) )
81 return;
82
83 err->code = U_ZERO_ERROR;
84
85 intl_free_custom_error_msg( err );
86 }
87 /* }}} */
88
89 /* {{{ Set last error message to msg copying it if needed. */
intl_error_set_custom_msg(intl_error * err,const char * msg,int copyMsg)90 void intl_error_set_custom_msg( intl_error* err, const char* msg, int copyMsg )
91 {
92 if( !msg )
93 return;
94
95 if( !err ) {
96 if( INTL_G( error_level ) )
97 php_error_docref( NULL, INTL_G( error_level ), "%s", msg );
98 if( INTL_G( use_exceptions ) )
99 zend_throw_exception_ex( IntlException_ce_ptr, 0, "%s", msg );
100 }
101 if( !err && !( err = intl_g_error_get( ) ) )
102 return;
103
104 /* Free previous message if any */
105 intl_free_custom_error_msg( err );
106
107 /* Mark message copied if any */
108 err->free_custom_error_message = copyMsg;
109
110 /* Set user's error text message */
111 err->custom_error_message = copyMsg ? estrdup( msg ) : (char *) msg;
112 }
113 /* }}} */
114
115 /* {{{ Create output message in format "<intl_error_text>: <extra_user_error_text>". */
intl_error_get_message(intl_error * err)116 zend_string * intl_error_get_message( intl_error* err )
117 {
118 const char *uErrorName = NULL;
119 zend_string *errMessage = 0;
120
121 if( !err && !( err = intl_g_error_get( ) ) )
122 return ZSTR_EMPTY_ALLOC();
123
124 uErrorName = u_errorName( err->code );
125
126 /* Format output string */
127 if( err->custom_error_message )
128 {
129 errMessage = strpprintf(0, "%s: %s", err->custom_error_message, uErrorName );
130 }
131 else
132 {
133 errMessage = strpprintf(0, "%s", uErrorName );
134 }
135
136 return errMessage;
137 }
138 /* }}} */
139
140 /* {{{ Set last error code. */
intl_error_set_code(intl_error * err,UErrorCode err_code)141 void intl_error_set_code( intl_error* err, UErrorCode err_code )
142 {
143 if( !err && !( err = intl_g_error_get( ) ) )
144 return;
145
146 err->code = err_code;
147 }
148 /* }}} */
149
150 /* {{{ Return last error code. */
intl_error_get_code(intl_error * err)151 UErrorCode intl_error_get_code( intl_error* err )
152 {
153 if( !err && !( err = intl_g_error_get( ) ) )
154 return U_ZERO_ERROR;
155
156 return err->code;
157 }
158 /* }}} */
159
160 /* {{{ Set error code and message. */
intl_error_set(intl_error * err,UErrorCode code,const char * msg,int copyMsg)161 void intl_error_set( intl_error* err, UErrorCode code, const char* msg, int copyMsg )
162 {
163 intl_error_set_code( err, code );
164 intl_error_set_custom_msg( err, msg, copyMsg );
165 }
166 /* }}} */
167
168 /* {{{ Set error code and message. */
intl_errors_set(intl_error * err,UErrorCode code,const char * msg,int copyMsg)169 void intl_errors_set( intl_error* err, UErrorCode code, const char* msg, int copyMsg )
170 {
171 intl_errors_set_code( err, code );
172 intl_errors_set_custom_msg( err, msg, copyMsg );
173 }
174 /* }}} */
175
176 /* {{{ */
intl_errors_reset(intl_error * err)177 void intl_errors_reset( intl_error* err )
178 {
179 if(err) {
180 intl_error_reset( err );
181 }
182 intl_error_reset( NULL );
183 }
184 /* }}} */
185
186 /* {{{ */
intl_errors_set_custom_msg(intl_error * err,const char * msg,int copyMsg)187 void intl_errors_set_custom_msg( intl_error* err, const char* msg, int copyMsg )
188 {
189 if(err) {
190 intl_error_set_custom_msg( err, msg, copyMsg );
191 }
192 intl_error_set_custom_msg( NULL, msg, copyMsg );
193 }
194 /* }}} */
195
196 /* {{{ */
intl_errors_set_code(intl_error * err,UErrorCode err_code)197 void intl_errors_set_code( intl_error* err, UErrorCode err_code )
198 {
199 if(err) {
200 intl_error_set_code( err, err_code );
201 }
202 intl_error_set_code( NULL, err_code );
203 }
204 /* }}} */
205
intl_parse_error_to_string(UParseError * pe)206 smart_str intl_parse_error_to_string( UParseError* pe )
207 {
208 smart_str ret = {0};
209 zend_string *u8str;
210 UErrorCode status;
211 int any = 0;
212
213 assert( pe != NULL );
214
215 smart_str_appends( &ret, "parse error " );
216 if( pe->line > 0 )
217 {
218 smart_str_appends( &ret, "on line " );
219 smart_str_append_long( &ret, (zend_long ) pe->line );
220 any = 1;
221 }
222 if( pe->offset >= 0 ) {
223 if( any )
224 smart_str_appends( &ret, ", " );
225 else
226 smart_str_appends( &ret, "at " );
227
228 smart_str_appends( &ret, "offset " );
229 smart_str_append_long( &ret, (zend_long ) pe->offset );
230 any = 1;
231 }
232
233 if (pe->preContext[0] != 0 ) {
234 if( any )
235 smart_str_appends( &ret, ", " );
236
237 smart_str_appends( &ret, "after \"" );
238 u8str = intl_convert_utf16_to_utf8(pe->preContext, -1, &status );
239 if( !u8str )
240 {
241 smart_str_appends( &ret, "(could not convert parser error pre-context to UTF-8)" );
242 }
243 else {
244 smart_str_append( &ret, u8str );
245 zend_string_release_ex( u8str, 0 );
246 }
247 smart_str_appends( &ret, "\"" );
248 any = 1;
249 }
250
251 if( pe->postContext[0] != 0 )
252 {
253 if( any )
254 smart_str_appends( &ret, ", " );
255
256 smart_str_appends( &ret, "before or at \"" );
257 u8str = intl_convert_utf16_to_utf8(pe->postContext, -1, &status );
258 if( !u8str )
259 {
260 smart_str_appends( &ret, "(could not convert parser error post-context to UTF-8)" );
261 }
262 else
263 {
264 smart_str_append( &ret, u8str );
265 zend_string_release_ex( u8str, 0 );
266 }
267 smart_str_appends( &ret, "\"" );
268 any = 1;
269 }
270
271 if( !any )
272 {
273 smart_str_free( &ret );
274 smart_str_appends( &ret, "no parse error" );
275 }
276
277 smart_str_0( &ret );
278 return ret;
279 }
280