Back to index

php5  5.3.10
idn.c
Go to the documentation of this file.
00001 /*
00002    +----------------------------------------------------------------------+
00003    | PHP Version 5                                                        |
00004    +----------------------------------------------------------------------+
00005    | Copyright (c) 2009 The PHP Group                                     |
00006    +----------------------------------------------------------------------+
00007    | This source file is subject to version 3.01 of the PHP license,      |
00008    | that is bundled with this package in the file LICENSE, and is        |
00009    | available through the world-wide-web at the following url:           |
00010    | http://www.php.net/license/3_01.txt                                  |
00011    | If you did not receive a copy of the PHP license and are unable to   |
00012    | obtain it through the world-wide-web, please send a note to          |
00013    | license@php.net so we can mail you a copy immediately.               |
00014    +----------------------------------------------------------------------+
00015    | Author: Pierre A. Joye <pierre@php.net>                              |
00016    +----------------------------------------------------------------------+
00017  */
00018 /* $Id: idn.c 314218 2011-08-04 00:59:43Z felipe $ */
00019 
00020 /* {{{ includes */
00021 #ifdef HAVE_CONFIG_H
00022 #include "config.h"
00023 #endif
00024 
00025 #include <php.h>
00026 
00027 #include <unicode/uidna.h>
00028 #include <unicode/ustring.h>
00029 #include "ext/standard/php_string.h"
00030 
00031 #include "intl_error.h"
00032  #include "intl_convert.h"
00033 /* }}} */
00034 
00035 /* {{{ grapheme_register_constants
00036  * Register API constants
00037  */
00038 void idn_register_constants( INIT_FUNC_ARGS )
00039 {
00040        /* Option to prohibit processing of unassigned codepoints in the input and
00041           do not check if the input conforms to STD-3 ASCII rules. */
00042        REGISTER_LONG_CONSTANT("IDNA_DEFAULT", UIDNA_DEFAULT, CONST_CS | CONST_PERSISTENT);
00043 
00044        /* Option to allow processing of unassigned codepoints in the input */
00045        REGISTER_LONG_CONSTANT("IDNA_ALLOW_UNASSIGNED", UIDNA_ALLOW_UNASSIGNED, CONST_CS | CONST_PERSISTENT);
00046 
00047        /* Option to check if input conforms to STD-3 ASCII rules */
00048        REGISTER_LONG_CONSTANT("IDNA_USE_STD3_RULES", UIDNA_USE_STD3_RULES, CONST_CS | CONST_PERSISTENT);
00049 }
00050 /* }}} */
00051 
00052 enum {
00053        INTL_IDN_TO_ASCII = 0,
00054        INTL_IDN_TO_UTF8
00055 };
00056 
00057 static void php_intl_idn_to(INTERNAL_FUNCTION_PARAMETERS, int mode)
00058 {
00059        unsigned char* domain;
00060        int domain_len;
00061        long option = 0;
00062        UChar* ustring = NULL;
00063        int ustring_len = 0;
00064        UErrorCode status;
00065        char     *converted_utf8;
00066        int32_t   converted_utf8_len;
00067        UChar     converted[MAXPATHLEN];
00068        int32_t   converted_ret_len;
00069 
00070        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", (char **)&domain, &domain_len, &option) == FAILURE) {
00071               return;
00072        }
00073 
00074        if (domain_len < 1) {
00075               intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "idn_to_ascii: empty domain name", 0 TSRMLS_CC );
00076               RETURN_FALSE;
00077        }
00078 
00079        /* convert the string to UTF-16. */
00080        status = U_ZERO_ERROR;
00081        intl_convert_utf8_to_utf16(&ustring, &ustring_len, (char*) domain, domain_len, &status );
00082 
00083        if (U_FAILURE(status)) {
00084               intl_error_set_code(NULL, status TSRMLS_CC);
00085 
00086               /* Set error messages. */
00087               intl_error_set_custom_msg( NULL, "Error converting input string to UTF-16", 0 TSRMLS_CC );
00088               if (ustring) {
00089                      efree(ustring);
00090               }
00091               RETURN_FALSE;
00092        } else {
00093               UParseError parse_error;
00094 
00095               status = U_ZERO_ERROR;
00096               if (mode == INTL_IDN_TO_ASCII) {
00097                      converted_ret_len = uidna_IDNToASCII(ustring, ustring_len, converted, MAXPATHLEN, (int32_t)option, &parse_error, &status);
00098               } else {
00099                      converted_ret_len = uidna_IDNToUnicode(ustring, ustring_len, converted, MAXPATHLEN, (int32_t)option, &parse_error, &status);
00100               }
00101               efree(ustring);
00102 
00103               if (U_FAILURE(status)) {
00104                      intl_error_set( NULL, status, "idn_to_ascii: cannot convert to ASCII", 0 TSRMLS_CC );
00105                      RETURN_FALSE;
00106               }
00107 
00108               status = U_ZERO_ERROR;
00109               intl_convert_utf16_to_utf8(&converted_utf8, &converted_utf8_len, converted, converted_ret_len, &status);
00110 
00111               if (U_FAILURE(status)) {
00112                      /* Set global error code. */
00113                      intl_error_set_code(NULL, status TSRMLS_CC);
00114 
00115                      /* Set error messages. */
00116                      intl_error_set_custom_msg( NULL, "Error converting output string to UTF-8", 0 TSRMLS_CC );
00117                      efree(converted_utf8);
00118                      RETURN_FALSE;
00119               }
00120        }
00121 
00122        /* return the allocated string, not a duplicate */
00123        RETURN_STRINGL(((char *)converted_utf8), converted_utf8_len, 0);
00124 }
00125 
00126 /* {{{ proto int idn_to_ascii(string domain[, int options])
00127    Converts an Unicode domain to ASCII representation, as defined in the IDNA RFC */
00128 PHP_FUNCTION(idn_to_ascii)
00129 {
00130        php_intl_idn_to(INTERNAL_FUNCTION_PARAM_PASSTHRU, INTL_IDN_TO_ASCII);
00131 }
00132 /* }}} */
00133 
00134 
00135 /* {{{ proto int idn_to_utf8(string domain[, int options])
00136    Converts an ASCII representation of the domain to Unicode (UTF-8), as defined in the IDNA RFC */
00137 PHP_FUNCTION(idn_to_utf8)
00138 {
00139        php_intl_idn_to(INTERNAL_FUNCTION_PARAM_PASSTHRU, INTL_IDN_TO_UTF8);
00140 }
00141 /* }}} */
00142 
00143 
00144 /*
00145  * Local variables:
00146  * tab-width: 4
00147  * c-basic-offset: 4
00148  * End:
00149  * vim600: fdm=marker
00150  * vim: noet sw=4 ts=4
00151  */