Tsukada
ttsuk****@users*****
2002年 5月 26日 (日) 01:14:42 JST
ttsukada 02/05/26 01:14:42 Modified: ext/mbstring mbfunction.c Log: improve error hanling Revision Changes Path 1.5 +99 -86 php4/ext/mbstring/mbfunction.c Index: mbfunction.c =================================================================== RCS file: /cvsroot/php-i18n/php4/ext/mbstring/mbfunction.c,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- mbfunction.c 22 May 2002 20:13:45 -0000 1.4 +++ mbfunction.c 25 May 2002 16:14:42 -0000 1.5 @@ -28,12 +28,12 @@ case PHP_MB_ERR_BUFFER_OVER_FLOW: result = "internal error: Buffer over flow"; break; - case PHP_MB_ERR_STRING_OVER_FLOW: - result = "internal error: String over flow"; - break; case PHP_MB_ERR_UNSUPPORTED_ENCODING: result = "Unsupported encoding"; break; + case PHP_MB_ERR_UNSUPPORTED_LANGUAGE: + result = "Unsupported language"; + break; case PHP_MB_ERR_UNSUPPORTED_CONVERT: result = "Unsupported convert"; break; @@ -54,6 +54,27 @@ return result; } +PHPAPI void +_php_mb_error(int last_errno, const char *extra_msg TSRMLS_DC) +{ +#if PHP_DEBUG + if (extra_msg == NULL) { + php_error(E_WARNING, "%s() %s (%s: %d)", + get_active_function_name(TSRMLS_C), php_mb_error_getstring(last_errno), + MBSTRG(last_err_source_file), MBSTRG(last_err_source_line)); + } else { + php_error(E_WARNING, "%s() %s. \"%s\" (%s: %d)", + get_active_function_name(TSRMLS_C), php_mb_error_getstring(last_errno), extra_msg, + MBSTRG(last_err_source_file), MBSTRG(last_err_source_line)); + } +#else + if (extra_msg == NULL) { + php_error(E_WARNING, "%s() %s", get_active_function_name(TSRMLS_C), php_mb_error_getstring(last_errno)); + } else { + php_error(E_WARNING, "%s() %s. \"%s\"", get_active_function_name(TSRMLS_C), php_mb_error_getstring(last_errno), extra_msg); + } +#endif +} /* Return 0 if input contains any illegal encoding, otherwise 1. * Even if any illegal encoding is detected the result may contain a list @@ -61,8 +82,7 @@ */ PHPAPI int php_mb_parse_encoding_list( - const char *string_val, - int string_len, + const char *cstring, php_mb_enc ***return_list, int *return_size, int persistent TSRMLS_DC) @@ -78,24 +98,20 @@ if (return_size) { *return_size = 0; } - if (string_val == NULL) { - MBSTRG(last_errno) = PHP_MB_ERR_NULL_POINTER; - return result; - } - if (string_len <= 0) { - MBSTRG(last_errno) = PHP_MB_ERR_ILLEGAL_ARGUMENT; + if (cstring == NULL) { + PHP_MB_ERRSET(PHP_MB_ERR_NULL_POINTER); return result; } /* copy the string for work */ - tmpstr = (char *)estrndup(string_val, string_len); + tmpstr = (char *)estrdup(cstring); if (tmpstr == NULL) { - MBSTRG(last_errno) = PHP_MB_ERR_NO_MEMORY; + PHP_MB_ERRSET(PHP_MB_ERR_NO_MEMORY); return result; } /* count the number of listed encoding names */ - endp = tmpstr + string_len; + endp = tmpstr + strlen(tmpstr); n = 1; p1 = tmpstr; while ((p2 = php_memnstr(p1, ",", 1, endp)) != NULL) { @@ -107,7 +123,7 @@ /* make list */ list = (php_mb_enc **)pecalloc(size + 1, sizeof(php_mb_enc *), persistent); if (list == NULL) { - MBSTRG(last_errno) = PHP_MB_ERR_NO_MEMORY; + PHP_MB_ERRSET(PHP_MB_ERR_NO_MEMORY); efree(tmpstr); return result; } @@ -176,7 +192,7 @@ /* {{{ php_mb_check_encoding_list */ PHPAPI int php_mb_check_encoding_list(const char *encoding_list TSRMLS_DC) { - return php_mb_parse_encoding_list(encoding_list, strlen(encoding_list), NULL, NULL, 0 TSRMLS_CC); + return php_mb_parse_encoding_list(encoding_list, NULL, NULL, 0 TSRMLS_CC); } /* }}} */ @@ -201,15 +217,15 @@ result = NULL; if (from == NULL || to == NULL) { - MBSTRG(last_errno) = PHP_MB_ERR_UNSUPPORTED_ENCODING; + PHP_MB_ERRSET(PHP_MB_ERR_UNSUPPORTED_ENCODING); return result; } if (string_val == NULL) { - MBSTRG(last_errno) = PHP_MB_ERR_NULL_POINTER; + PHP_MB_ERRSET(PHP_MB_ERR_NULL_POINTER); return result; } if (string_len < 0) { - MBSTRG(last_errno) = PHP_MB_ERR_ILLEGAL_ARGUMENT; + PHP_MB_ERRSET(PHP_MB_ERR_ILLEGAL_ARGUMENT); return result; } @@ -235,7 +251,7 @@ php_mb_conv_filt_free(filter2); } php_mb_str_free(tmpstr); - MBSTRG(last_errno) = PHP_MB_ERR_UNSUPPORTED_CONVERT; + PHP_MB_ERRSET(PHP_MB_ERR_UNSUPPORTED_CONVERT); return result; } PHP_MB_FILT_TSRMLS_SET(filter1); @@ -286,15 +302,15 @@ if (output_len != NULL) { *output_len = 0; } - if ( !input || !length) { + if (input == NULL) { + PHP_MB_ERRSET(PHP_MB_ERR_NULL_POINTER); return NULL; } + /* new encoding */ if (_to_encoding && strlen(_to_encoding)) { to_encoding = php_mb_enc_resolve(_to_encoding); if (to_encoding == NULL) { - php_error(E_WARNING, "%s() unknown encoding \"%s\"", - get_active_function_name(TSRMLS_C), _to_encoding); return NULL; } } else { @@ -305,7 +321,7 @@ if (_from_encodings != NULL) { list = NULL; size = 0; - php_mb_parse_encoding_list(_from_encodings, strlen(_from_encodings), &list, &size, 0 TSRMLS_CC); + php_mb_parse_encoding_list(_from_encodings, &list, &size, 0 TSRMLS_CC); if (size == 1) { from_encoding = list[0]; } else if (size > 1) { @@ -324,13 +340,13 @@ if (list != NULL) { efree(list); } + } else { + from_encoding = MBSTRG(internal_encoding_r); } /* initialize converter */ convd = php_mb_bufconv_create(to_encoding, from_encoding, length TSRMLS_CC); if (convd == NULL) { - php_error(E_WARNING, "%s() unable to create character encoding converter", - get_active_function_name(TSRMLS_C)); return NULL; } @@ -354,11 +370,11 @@ mbfl_convert_filter *filter; if (string_val == NULL || from == NULL) { - MBSTRG(last_errno) = PHP_MB_ERR_NULL_POINTER; + PHP_MB_ERRSET(PHP_MB_ERR_NULL_POINTER); return NULL; } if (string_len < 0) { - MBSTRG(last_errno) = PHP_MB_ERR_ILLEGAL_ARGUMENT; + PHP_MB_ERRSET(PHP_MB_ERR_ILLEGAL_ARGUMENT); return NULL; } @@ -407,7 +423,7 @@ result = NULL; if (buffer == NULL || to == NULL) { - MBSTRG(last_errno) = PHP_MB_ERR_NULL_POINTER; + PHP_MB_ERRSET(PHP_MB_ERR_NULL_POINTER); return result; } @@ -460,18 +476,18 @@ php_mb_bufconv *convd; if (from == NULL || to == NULL) { - MBSTRG(last_errno) = PHP_MB_ERR_NULL_POINTER; + PHP_MB_ERRSET(PHP_MB_ERR_NULL_POINTER); return NULL; } if (initsz < 0) { - MBSTRG(last_errno) = PHP_MB_ERR_ILLEGAL_ARGUMENT; + PHP_MB_ERRSET(PHP_MB_ERR_ILLEGAL_ARGUMENT); return NULL; } /* allocate */ convd = (php_mb_bufconv*)emalloc(sizeof(php_mb_bufconv)); if (convd == NULL) { - MBSTRG(last_errno) = PHP_MB_ERR_NO_MEMORY; + PHP_MB_ERRSET(PHP_MB_ERR_NO_MEMORY); return NULL; } convd->string = php_mb_str_create(); @@ -496,10 +512,10 @@ php_mb_conv_filt_free(convd->filter2); php_mb_str_free(convd->string); efree(convd); - MBSTRG(last_errno) = PHP_MB_ERR_UNSUPPORTED_CONVERT; + PHP_MB_ERRSET(PHP_MB_ERR_UNSUPPORTED_CONVERT); return NULL; } - MBSTRG(last_errno) = PHP_MB_SUCCESS; + PHP_MB_ERRSET(PHP_MB_SUCCESS); convd->from = from; convd->to = to; @@ -528,7 +544,7 @@ php_mb_bufconv_reset(php_mb_bufconv *convd, php_mb_enc *to, php_mb_enc *from TSRMLS_DC) { if (convd == NULL || from == NULL || to == NULL) { - MBSTRG(last_errno) = PHP_MB_ERR_NULL_POINTER; + PHP_MB_ERRSET(PHP_MB_ERR_NULL_POINTER); return FAILURE; } @@ -572,7 +588,7 @@ int (*filter_function)(int c, mbfl_convert_filter *filter); if (convd == NULL || value == NULL) { - MBSTRG(last_errno) = PHP_MB_ERR_NULL_POINTER; + PHP_MB_ERRSET(PHP_MB_ERR_NULL_POINTER); return FAILURE; } @@ -634,18 +650,18 @@ php_mb_enc *result; if (string_val == NULL || list == NULL) { - MBSTRG(last_errno) = PHP_MB_ERR_NULL_POINTER; + PHP_MB_ERRSET(PHP_MB_ERR_NULL_POINTER); return NULL; } if (string_len < 0 || listsize < 0) { - MBSTRG(last_errno) = PHP_MB_ERR_ILLEGAL_ARGUMENT; + PHP_MB_ERRSET(PHP_MB_ERR_ILLEGAL_ARGUMENT); return NULL; } /* initialize */ flist = (mbfl_identify_filter **)emalloc(listsize*sizeof(mbfl_identify_filter*)); if (flist == NULL) { - MBSTRG(last_errno) = PHP_MB_ERR_NO_MEMORY; + PHP_MB_ERRSET(PHP_MB_ERR_NO_MEMORY); return NULL; } i = 0; @@ -715,24 +731,24 @@ mbfl_identify_filter *filter; if (list == NULL) { - MBSTRG(last_errno) = PHP_MB_ERR_NULL_POINTER; + PHP_MB_ERRSET(PHP_MB_ERR_NULL_POINTER); return NULL; } if (listsize <= 0) { - MBSTRG(last_errno) = PHP_MB_ERR_ILLEGAL_ARGUMENT; + PHP_MB_ERRSET(PHP_MB_ERR_ILLEGAL_ARGUMENT); return NULL; } /* allocate */ identd = (php_mb_detector*)emalloc(sizeof(php_mb_detector)); if (identd == NULL) { - MBSTRG(last_errno) = PHP_MB_ERR_NO_MEMORY; + PHP_MB_ERRSET(PHP_MB_ERR_NO_MEMORY); return NULL; } identd->filter_list = (mbfl_identify_filter **)emalloc(listsize*sizeof(mbfl_identify_filter *)); if (identd->filter_list == NULL) { efree(identd); - MBSTRG(last_errno) = PHP_MB_ERR_NO_MEMORY; + PHP_MB_ERRSET(PHP_MB_ERR_NO_MEMORY); return NULL; } @@ -800,7 +816,7 @@ res = 0; if (identd == NULL || value == NULL) { - MBSTRG(last_errno) = PHP_MB_ERR_NULL_POINTER; + PHP_MB_ERRSET(PHP_MB_ERR_NULL_POINTER); return res; } @@ -902,7 +918,7 @@ len = -1; if (string_val == NULL || encoding == NULL) { - MBSTRG(last_errno) = PHP_MB_ERR_NULL_POINTER; + PHP_MB_ERRSET(PHP_MB_ERR_NULL_POINTER); return len; } @@ -1027,15 +1043,15 @@ result = -1; if (haystack == NULL || needle == NULL || encoding == NULL) { - MBSTRG(last_errno) = PHP_MB_ERR_NULL_POINTER; + PHP_MB_ERRSET(PHP_MB_ERR_NULL_POINTER); return result; } if (haystack_len < 0 || needle_len < 0) { - MBSTRG(last_errno) = PHP_MB_ERR_ILLEGAL_ARGUMENT; + PHP_MB_ERRSET(PHP_MB_ERR_ILLEGAL_ARGUMENT); return result; } if (offset < 0) { - MBSTRG(last_errno) = PHP_MB_ERR_INDEX_OUT_OF_BOUNDS; + PHP_MB_ERRSET(PHP_MB_ERR_INDEX_OUT_OF_BOUNDS); return result; } @@ -1046,7 +1062,7 @@ } if (cd.needle->length <= 0 || cd.needle->value == NULL) { php_mb_buf_free(cd.needle); - MBSTRG(last_errno) = PHP_MB_ERR_ILLEGAL_ARGUMENT; + PHP_MB_ERRSET(PHP_MB_ERR_ILLEGAL_ARGUMENT); return result; } @@ -1153,7 +1169,7 @@ struct collector_replace_data pc; if (search == NULL || replace == NULL || subject == NULL) { - MBSTRG(last_errno) = PHP_MB_ERR_NULL_POINTER; + PHP_MB_ERRSET(PHP_MB_ERR_NULL_POINTER); return NULL; } @@ -1269,15 +1285,15 @@ result = NULL; if (string_val == NULL || encoding == NULL) { - MBSTRG(last_errno) = PHP_MB_ERR_NULL_POINTER; + PHP_MB_ERRSET(PHP_MB_ERR_NULL_POINTER); return result; } if (string_len < 0 || count < 0) { - MBSTRG(last_errno) = PHP_MB_ERR_ILLEGAL_ARGUMENT; + PHP_MB_ERRSET(PHP_MB_ERR_ILLEGAL_ARGUMENT); return result; } if (start < 0) { - MBSTRG(last_errno) = PHP_MB_ERR_INDEX_OUT_OF_BOUNDS; + PHP_MB_ERRSET(PHP_MB_ERR_INDEX_OUT_OF_BOUNDS); return result; } @@ -1328,7 +1344,7 @@ } if (start_index < 0 || start_index >= string_len || start_index > end_index) { - MBSTRG(last_errno) = PHP_MB_ERR_INDEX_OUT_OF_BOUNDS; + PHP_MB_ERRSET(PHP_MB_ERR_INDEX_OUT_OF_BOUNDS); return result; } if (end_index > string_len) { @@ -1388,7 +1404,7 @@ php_mb_conv_filt_free(encoder); php_mb_conv_filt_free(cd.decoder); if (start >= cd.output) { - MBSTRG(last_errno) = PHP_MB_ERR_INDEX_OUT_OF_BOUNDS; + PHP_MB_ERRSET(PHP_MB_ERR_INDEX_OUT_OF_BOUNDS); } else { php_mb_str_nullpad(tmpstr); result = tmpstr->value; @@ -1420,15 +1436,15 @@ result = NULL; if (string_val == NULL || encoding == NULL) { - MBSTRG(last_errno) = PHP_MB_ERR_NULL_POINTER; + PHP_MB_ERRSET(PHP_MB_ERR_NULL_POINTER); return result; } if (string_len < 0 || size < 0) { - MBSTRG(last_errno) = PHP_MB_ERR_ILLEGAL_ARGUMENT; + PHP_MB_ERRSET(PHP_MB_ERR_ILLEGAL_ARGUMENT); return result; } if (start < 0 || start >= string_len) { - MBSTRG(last_errno) = PHP_MB_ERR_INDEX_OUT_OF_BOUNDS; + PHP_MB_ERRSET(PHP_MB_ERR_INDEX_OUT_OF_BOUNDS); return result; } @@ -1483,7 +1499,7 @@ } if (start_index < 0 || start_index >= string_len || start_index > end_index) { - MBSTRG(last_errno) = PHP_MB_ERR_INDEX_OUT_OF_BOUNDS; + PHP_MB_ERRSET(PHP_MB_ERR_INDEX_OUT_OF_BOUNDS); return result; } if (end_index > string_len) { @@ -1609,11 +1625,11 @@ len = -1; if (string_val == NULL || encoding == NULL) { - MBSTRG(last_errno) = PHP_MB_ERR_NULL_POINTER; + PHP_MB_ERRSET(PHP_MB_ERR_NULL_POINTER); return len; } if (string_len < 0) { - MBSTRG(last_errno) = PHP_MB_ERR_ILLEGAL_ARGUMENT; + PHP_MB_ERRSET(PHP_MB_ERR_ILLEGAL_ARGUMENT); return len; } @@ -1712,15 +1728,15 @@ result = NULL; if (string_val == NULL || encoding == NULL) { - MBSTRG(last_errno) = PHP_MB_ERR_NULL_POINTER; + PHP_MB_ERRSET(PHP_MB_ERR_NULL_POINTER); return result; } if (string_len < 0 || marker_len < 0 || width < 0) { - MBSTRG(last_errno) = PHP_MB_ERR_ILLEGAL_ARGUMENT; + PHP_MB_ERRSET(PHP_MB_ERR_ILLEGAL_ARGUMENT); return result; } if (start < 0) { - MBSTRG(last_errno) = PHP_MB_ERR_INDEX_OUT_OF_BOUNDS; + PHP_MB_ERRSET(PHP_MB_ERR_INDEX_OUT_OF_BOUNDS); return result; } @@ -1801,7 +1817,7 @@ php_mb_conv_filt_free(cd.decoder); php_mb_conv_filt_free(encoder); if (start >= cd.outchar) { - MBSTRG(last_errno) = PHP_MB_ERR_INDEX_OUT_OF_BOUNDS; + PHP_MB_ERRSET(PHP_MB_ERR_INDEX_OUT_OF_BOUNDS); } else { php_mb_str_nullpad(tmpstr); result = tmpstr->value; @@ -1925,20 +1941,20 @@ php_mb_mime_header_encoder *pe; if (input == NULL || charset == NULL || transfer == NULL) { - MBSTRG(last_errno) = PHP_MB_ERR_NULL_POINTER; + PHP_MB_ERRSET(PHP_MB_ERR_NULL_POINTER); return NULL; } /* check MIME charset name */ if (charset->mime_name == NULL || transfer->mime_name == NULL) { - MBSTRG(last_errno) = PHP_MB_ERR_UNSUPPORTED_ENCODING; + PHP_MB_ERRSET(PHP_MB_ERR_UNSUPPORTED_ENCODING); return NULL; } /* allocate */ pe = (php_mb_mime_header_encoder*)emalloc(sizeof(php_mb_mime_header_encoder)); if (pe == NULL) { - MBSTRG(last_errno) = PHP_MB_ERR_NO_MEMORY; + PHP_MB_ERRSET(PHP_MB_ERR_NO_MEMORY); return NULL; } @@ -1984,10 +2000,8 @@ php_mb_str_append_cstr(pe->strenc, "\x3f"); if (transfer->id == php_mb_encid_qprint) { php_mb_str_append_cstr(pe->strenc, "\x51"); - pe->encod_filter->status |= MBFL_QPRINT_STS_MIME_HEADER; } else if (transfer->id == php_mb_encid_base64) { php_mb_str_append_cstr(pe->strenc, "\x42"); - pe->encod_filter->status |= MBFL_BASE64_STS_MIME_HEADER; } else { php_mb_str_append_cstr(pe->strenc, transfer->name); } @@ -1996,6 +2010,9 @@ /* make LWSP */ php_mb_str_append_cstr(pe->strlwsp, "\x0d\x0a\x20"); + /* set filter mode */ + pe->encod_filter->mode |= MBFL_CONVFILTER_MIME_MODE_HEADER; + return pe; } @@ -2077,11 +2094,11 @@ php_mb_mime_header_encoder *pe; if (string_val == NULL) { - MBSTRG(last_errno) = PHP_MB_ERR_NULL_POINTER; + PHP_MB_ERRSET(PHP_MB_ERR_NULL_POINTER); return NULL; } if (string_len < 0 || indent < 0) { - MBSTRG(last_errno) = PHP_MB_ERR_ILLEGAL_ARGUMENT; + PHP_MB_ERRSET(PHP_MB_ERR_ILLEGAL_ARGUMENT); return NULL; } @@ -2192,11 +2209,7 @@ php_mb_conv_filt_reset(pd->conv1_filter, pd->encoding_charset, MBSTRG(wchar_encoding) ); /* decode filter */ php_mb_conv_filt_reset(pd->deco_filter, pd->encoding_transfer, php_mb_enc_get(php_mb_encid_8bit) ); - if (pd->encoding_transfer->id == php_mb_encid_qprint) { - pd->deco_filter->status |= MBFL_QPRINT_STS_MIME_HEADER; - } else if (pd->encoding_transfer->id == php_mb_encid_base64) { - pd->deco_filter->status |= MBFL_BASE64_STS_MIME_HEADER; - } + pd->deco_filter->mode |= MBFL_CONVFILTER_MIME_MODE_HEADER; } else { pd->status = 0; php_mb_conv_filt_feed_string(pd->conv1_filter, pd->strtmp); @@ -2291,14 +2304,14 @@ php_mb_mime_header_decoder *decoder; if (output_enc == NULL) { - MBSTRG(last_errno) = PHP_MB_ERR_NULL_POINTER; + PHP_MB_ERRSET(PHP_MB_ERR_NULL_POINTER); return NULL; } /* allocate */ decoder = (php_mb_mime_header_decoder*)emalloc(sizeof(php_mb_mime_header_decoder)); if (decoder == NULL) { - MBSTRG(last_errno) = PHP_MB_ERR_NO_MEMORY; + PHP_MB_ERRSET(PHP_MB_ERR_NO_MEMORY); return NULL; } @@ -2414,7 +2427,7 @@ php_mb_mime_header_decoder *decoder; if (string_val == NULL) { - MBSTRG(last_errno) = PHP_MB_ERR_NULL_POINTER; + PHP_MB_ERRSET(PHP_MB_ERR_NULL_POINTER); return NULL; } @@ -2668,11 +2681,11 @@ result = NULL; if (string_val == NULL || encoding == NULL || convmap == NULL) { - MBSTRG(last_errno) = PHP_MB_ERR_NULL_POINTER; + PHP_MB_ERRSET(PHP_MB_ERR_NULL_POINTER); return result; } if (string_len < 0 || mapsize <= 0) { - MBSTRG(last_errno) = PHP_MB_ERR_ILLEGAL_ARGUMENT; + PHP_MB_ERRSET(PHP_MB_ERR_ILLEGAL_ARGUMENT); return result; } @@ -3036,11 +3049,11 @@ result = NULL; if (string_val == NULL || encoding == NULL) { - MBSTRG(last_errno) = PHP_MB_ERR_NULL_POINTER; + PHP_MB_ERRSET(PHP_MB_ERR_NULL_POINTER); return result; } if (string_len < 0) { - MBSTRG(last_errno) = PHP_MB_ERR_ILLEGAL_ARGUMENT; + PHP_MB_ERRSET(PHP_MB_ERR_ILLEGAL_ARGUMENT); return result; } @@ -3143,13 +3156,13 @@ /* make encoding list */ list = NULL; size = 0; - php_mb_parse_encoding_list(arg_list, strlen(arg_list), &elist, &size, 0 TSRMLS_CC); + php_mb_parse_encoding_list(arg_list, &elist, &size, 0 TSRMLS_CC); if (size <= 0) { return NULL; } if (elist == NULL) { - php_mb_parse_encoding_list(MBSTRG(detect_order), strlen(MBSTRG(detect_order)), &elist, &size, 0 TSRMLS_CC); + php_mb_parse_encoding_list(MBSTRG(detect_order), &elist, &size, 0 TSRMLS_CC); } encoding = php_mb_detect_encoding(arg_string, arg_length, elist, size TSRMLS_CC);