]> granicus.if.org Git - php/commitdiff
MFH
authorRui Hirokawa <hirokawa@php.net>
Tue, 21 Mar 2006 07:56:28 +0000 (07:56 +0000)
committerRui Hirokawa <hirokawa@php.net>
Tue, 21 Mar 2006 07:56:28 +0000 (07:56 +0000)
ext/mbstring/libmbfl/mbfl/mbfilter.c
ext/mbstring/mbstring.c
ext/mbstring/mbstring.h

index 9f163318742069731918679c592777f1370e3a50..a8e3b68d33909068f52a2386fb1b32e8e1bb011d 100644 (file)
@@ -335,6 +335,10 @@ int mbfl_buffer_illegalchars(mbfl_buffer_converter *convd)
 {
        int num_illegalchars = 0;
 
+       if (convd == NULL) {
+               return 0;
+       }
+
        if (convd->filter1 != NULL) {
                num_illegalchars += convd->filter1->num_illegalchar;
        }
index b1c467574632d284674cb265af8950f5b87d7741..b521c551e31aa64a9f6dc49bcb578a5534becb70 100644 (file)
@@ -220,6 +220,7 @@ zend_function_entry mbstring_functions[] = {
        PHP_FE(mb_decode_numericentity, NULL)
        PHP_FE(mb_send_mail,                    NULL)
        PHP_FE(mb_get_info,                             NULL)
+       PHP_FE(mb_check_encoding,               NULL)
 #if HAVE_MBREGEX
        PHP_MBREGEX_FUNCTION_ENTRIES
 #endif
@@ -3437,6 +3438,67 @@ PHP_FUNCTION(mb_get_info)
 }
 /* }}} */
 
+/* {{{ proto bool mb_check_encoding([string var[, string encoding]])
+   Check if the string is valid for the specified encoding */
+PHP_FUNCTION(mb_check_encoding)
+{
+       char *var = NULL;
+       int var_len;
+       char *enc = NULL;
+       int enc_len;
+       char *name;
+       mbfl_buffer_converter *convd;
+       enum mbfl_no_encoding no_encoding = MBSTRG(current_internal_encoding);
+       zval *row;
+       mbfl_string string, result, *ret = NULL;
+       long illegalchars = 0;
+
+       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|ss", &var, &var_len, &enc, &enc_len) == FAILURE) {
+               RETURN_FALSE;
+       }
+
+       if (var == NULL) {
+               RETURN_BOOL(MBSTRG(illegalchars) == 0);
+       }
+
+       if (enc != NULL) {
+               no_encoding = mbfl_name2no_encoding(enc);
+               if (no_encoding == mbfl_no_encoding_invalid || no_encoding == mbfl_no_encoding_pass) {
+                       php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid encoding \"%s\"", enc);
+                       RETURN_FALSE;
+               }
+       }
+       
+       convd = mbfl_buffer_converter_new(no_encoding, no_encoding, 0);
+       if (convd == NULL) {
+               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to create converter");
+               RETURN_FALSE;
+       }       
+       mbfl_buffer_converter_illegal_mode(convd, MBSTRG(current_filter_illegal_mode));
+       mbfl_buffer_converter_illegal_substchar(convd, MBSTRG(current_filter_illegal_substchar));       
+       
+       /* initialize string */
+       mbfl_string_init(&string);
+       mbfl_string_init(&result);
+       string.no_encoding = no_encoding;
+       string.no_language = MBSTRG(current_language);
+
+       string.val = (unsigned char *)var;
+       string.len = var_len;
+       ret = mbfl_buffer_converter_feed_result(convd, &string, &result);
+       illegalchars = mbfl_buffer_illegalchars(convd);
+       mbfl_buffer_converter_delete(convd);
+
+       if (ret != NULL) {
+               MBSTRG(illegalchars) += illegalchars;
+               efree(ret->val);
+               RETURN_BOOL(illegalchars == 0);
+       } else {
+               RETURN_FALSE;
+       }
+}
+/* }}} */
+
 /* {{{ MBSTRING_API int php_mb_encoding_translation() */
 MBSTRING_API int php_mb_encoding_translation(TSRMLS_D) 
 {
index 4eecb32c8a6d4558590fdc431bbc61efb8af7570..f433bbf631163e8050d05988ee5926599fcd398a 100644 (file)
@@ -120,6 +120,7 @@ PHP_FUNCTION(mb_encode_numericentity);
 PHP_FUNCTION(mb_decode_numericentity);
 PHP_FUNCTION(mb_send_mail);
 PHP_FUNCTION(mb_get_info);
+PHP_FUNCTION(mb_check_encoding);
 
 MBSTRING_API int php_mb_encoding_translation(TSRMLS_D);