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