Moriyoshi Koizumi
moriy****@users*****
2002年 10月 7日 (月) 06:59:59 JST
moriyoshi 02/10/07 06:59:59 Modified: ext/mbstring php_mbregex.c Log: merged the recent cvs.php.net changes Revision Changes Path 1.4 +57 -41 php4/ext/mbstring/php_mbregex.c Index: php_mbregex.c =================================================================== RCS file: /cvsroot/php-i18n/php4/ext/mbstring/php_mbregex.c,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- php_mbregex.c 26 May 2002 09:33:54 -0000 1.3 +++ php_mbregex.c 6 Oct 2002 21:59:59 -0000 1.4 @@ -16,7 +16,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: php_mbregex.c,v 1.4 2002/04/26 10:13:45 sas Exp $ */ +/* $Id: php_mbregex.c,v 1.9 2002/10/06 16:37:33 rasmus Exp $ */ #ifdef HAVE_CONFIG_H @@ -31,6 +31,7 @@ #if HAVE_MBREGEX +ZEND_EXTERN_MODULE_GLOBALS(mbstring) /* * string buffer @@ -90,22 +91,23 @@ mbctype = -1; if (pname != NULL) { - if (strcasecmp("EUC-JP", pname) == 0) { + if (strcasecmp("EUC-JP", pname) == 0 + || strcasecmp("X-EUC-JP", pname) == 0 + || strcasecmp("UJIS", pname) == 0 + || strcasecmp("EUCJP", pname) == 0 + || strcasecmp("EUC_JP", pname) == 0) { mbctype = MBCTYPE_EUC; - } else if (strcasecmp("UTF-8", pname) == 0) { + } else if (strcasecmp("UTF-8", pname) == 0 + || strcasecmp("UTF8", pname) == 0) { mbctype = MBCTYPE_UTF8; - } else if (strcasecmp("SJIS", pname) == 0) { + } else if (strcasecmp("SJIS", pname) == 0 + || strcasecmp("CP932", pname) == 0 + || strcasecmp("MS932", pname) == 0 + || strcasecmp("SHIFT_JIS", pname) == 0 ) { mbctype = MBCTYPE_SJIS; - } else if (strcasecmp("ascii", pname) == 0) { + } else if (strcasecmp("ASCII", pname) == 0) { mbctype = MBCTYPE_ASCII; - } else if (strcasecmp("euc", pname) == 0) { mbctype = MBCTYPE_EUC; - } else if (strcasecmp("eucJP", pname) == 0) { - mbctype = MBCTYPE_EUC; - } else if (strcasecmp("EUC_JP", pname) == 0) { - mbctype = MBCTYPE_EUC; - } else if (strcasecmp("Shift_JIS", pname) == 0) { - mbctype = MBCTYPE_SJIS; } } @@ -115,7 +117,7 @@ static const char* php_mbregex_mbctype2name(int mbctype) { - const char *p; + const char *p = NULL; if (mbctype == MBCTYPE_EUC) { p = "EUC-JP"; @@ -125,8 +127,6 @@ p = "SJIS"; } else if(mbctype == MBCTYPE_ASCII) { p = "ascii"; - } else { - p = "unknown"; } return p; @@ -156,11 +156,11 @@ } else { efree(pre->fastmap); pre->fastmap = (char*)0; - php_error(E_WARNING, "mbregex compile err: %s", err_str); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "mbregex compile err: %s", err_str); res = 1; } } else { - php_error(E_WARNING, "Unable to allocate memory in mbregex_compile_pattern"); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to allocate memory in mbregex_compile_pattern"); res = 1; } } else { @@ -224,12 +224,18 @@ int mbctype; if (ZEND_NUM_ARGS() == 0) { - RETVAL_STRING((char*)php_mbregex_mbctype2name(MBSTRG(regex_mbctype_r)), 1); - } else if (ZEND_NUM_ARGS() == 1 && zend_get_parameters_ex(1, &arg1) != FAILURE) { + const char *retval = php_mbregex_mbctype2name(MBSTRG(regex_mbctype_r)); + if ( retval != NULL ) { + RETVAL_STRING((char *)retval, 1); + } else { + RETVAL_FALSE; + } + } else if (ZEND_NUM_ARGS() == 1 && + zend_get_parameters_ex(1, &arg1) != FAILURE) { convert_to_string_ex(arg1); mbctype = php_mbregex_name2mbctype(Z_STRVAL_PP(arg1)); if (mbctype < 0) { - php_error(E_WARNING, "unknown encoding \"%s\"", Z_STRVAL_PP(arg1)); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown encoding \"%s\"", Z_STRVAL_PP(arg1)); RETVAL_FALSE; } else { MBSTRG(regex_mbctype_r) = mbctype; @@ -307,7 +313,9 @@ mbre_free_registers(®s); RETURN_FALSE; } - + if (regs.beg[0] == regs.end[0]) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Empty regular expression"); + } match_len = 1; str = Z_STRVAL_PP(arg_string); if (array) { @@ -423,8 +431,12 @@ pos = 0; while (err >= 0) { err = mbre_search(&re, string, string_len, pos, string_len - pos, ®s); + if ( regs.beg[0] == regs.end[0] ) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Empty regular expression"); + break; + } if (err <= -2) { - php_error(E_WARNING, "mbregex search failure in php_mbereg_replace_exec()"); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "mbregex search failure in php_mbereg_replace_exec()"); break; } if (err >= 0) { @@ -565,20 +577,19 @@ /* churn through str, generating array entries as we go */ while ((count != 0) && (err = mbre_search(&re, string, string_len, pos, string_len - pos, ®s)) >= 0) { + if ( regs.beg[0] == regs.end[0] ) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Empty regular expression"); + break; + } + n = regs.beg[0]; - if (n == pos) { - /* match is at start of string, return empty string */ - add_next_index_stringl(return_value, empty_string, 0, 1); + /* add it to the array */ + if (n < string_len && n <= pos) { + n -= pos; + add_next_index_stringl(return_value, &string[pos], n, 1); } else { - /* On a real match */ - /* add it to the array */ - if (n < string_len) { - n -= pos; - add_next_index_stringl(return_value, &string[pos], n, 1); - } else { - err = -2; - break; - } + err = -2; + break; } /* point at our new starting point */ n = regs.end[0]; @@ -598,7 +609,7 @@ /* see if we encountered an error */ if (err <= -2) { - php_error(E_WARNING, "mbregex search failure in mbsplit()"); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "mbregex search failure in mbsplit()"); zval_dtor(return_value); RETURN_FALSE; } @@ -721,11 +732,11 @@ } if (!MBSTRG(search_re)) { - php_error(E_WARNING, "no regex for search"); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "No regex given"); RETURN_FALSE; } if (!str) { - php_error(E_WARNING, "no string for search"); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "No string given"); RETURN_FALSE; } if (MBSTRG(search_regs)) { @@ -736,14 +747,16 @@ } err = mbre_search(MBSTRG(search_re), str, len, pos, len - pos, MBSTRG(search_regs)); - if (err <= -2) { - php_error(E_WARNING, "mbregex search failure in mbregex_search()"); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "mbregex search failure in mbregex_search()"); RETVAL_FALSE; } else if (err < 0) { MBSTRG(search_pos) = len; RETVAL_FALSE; } else { + if (MBSTRG(search_regs)->beg[0] == MBSTRG(search_regs)->end[0]) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Empty regular expression"); + } switch (mode) { case 1: if (array_init(return_value) != FAILURE) { @@ -955,8 +968,11 @@ } convert_to_long_ex(arg_pos); n = Z_LVAL_PP(arg_pos); - if (n < 0) { - php_error(E_WARNING, "position is minus value"); + if (n < 0 + || ( MBSTRG(search_str) != NULL && *MBSTRG(search_str) != NULL && + Z_TYPE_PP(MBSTRG(search_str)) == IS_STRING && + n >= Z_STRLEN_PP(MBSTRG(search_str)) ) ) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Position is out of range"); MBSTRG(search_pos) = 0; RETVAL_FALSE; } else {