php-internal-docs 8.4.8
Unofficial docs for php/php-src
Loading...
Searching...
No Matches
idn.c
Go to the documentation of this file.
1/*
2 +----------------------------------------------------------------------+
3 | Copyright (c) The PHP Group |
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 | https://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 | Author: Pierre A. Joye <pierre@php.net> |
14 | Gustavo Lopes <cataphract@php.net> |
15 +----------------------------------------------------------------------+
16 */
17
18/* {{{ includes */
19#ifdef HAVE_CONFIG_H
20#include <config.h>
21#endif
22
23#include <php.h>
24
25#include <unicode/uidna.h>
26#include <unicode/ustring.h>
27
28#include "idn.h"
29#include "intl_error.h"
30/* }}} */
31
32enum {
35};
36
37/* like INTL_CHECK_STATUS, but as a function and varying the name of the func */
38static zend_result php_intl_idn_check_status(UErrorCode err, const char *msg)
39{
41 if (U_FAILURE(err)) {
42 char *buff;
43 spprintf(&buff, 0, "%s: %s",
45 msg);
47 efree(buff);
48 return FAILURE;
49 }
50
51 return SUCCESS;
52}
53
54static void php_intl_idn_to_46(INTERNAL_FUNCTION_PARAMETERS,
55 const zend_string *domain, uint32_t option, int mode, zval *idna_info)
56{
57 UErrorCode status = U_ZERO_ERROR;
58 UIDNA *uts46;
59 int32_t len;
61 UIDNAInfo info = UIDNA_INFO_INITIALIZER;
62
63 uts46 = uidna_openUTS46(option, &status);
64 if (php_intl_idn_check_status(status, "failed to open UIDNA instance") == FAILURE) {
66 }
67
68 if (mode == INTL_IDN_TO_ASCII) {
69 const int32_t buffer_capac = 255;
70 buffer = zend_string_alloc(buffer_capac, 0);
71 len = uidna_nameToASCII_UTF8(uts46, ZSTR_VAL(domain), ZSTR_LEN(domain),
72 ZSTR_VAL(buffer), buffer_capac, &info, &status);
73 if (len >= buffer_capac || php_intl_idn_check_status(status, "failed to convert name") == FAILURE) {
74 uidna_close(uts46);
75 zend_string_efree(buffer);
77 }
78 } else {
79 const int32_t buffer_capac = 252*4;
80 buffer = zend_string_alloc(buffer_capac, 0);
81 len = uidna_nameToUnicodeUTF8(uts46, ZSTR_VAL(domain), ZSTR_LEN(domain),
82 ZSTR_VAL(buffer), buffer_capac, &info, &status);
83 if (len >= buffer_capac || php_intl_idn_check_status(status, "failed to convert name") == FAILURE) {
84 uidna_close(uts46);
85 zend_string_efree(buffer);
87 }
88 }
89
90 ZSTR_VAL(buffer)[len] = '\0';
92
93 if (info.errors == 0) {
95 } else {
97 }
98
99 if (idna_info) {
100 add_assoc_str_ex(idna_info, "result", sizeof("result")-1, zend_string_copy(buffer));
101 add_assoc_bool_ex(idna_info, "isTransitionalDifferent",
102 sizeof("isTransitionalDifferent")-1, info.isTransitionalDifferent);
103 add_assoc_long_ex(idna_info, "errors", sizeof("errors")-1, (zend_long)info.errors);
104 }
105
106 zend_string_release(buffer);
107 uidna_close(uts46);
108}
109
110static void php_intl_idn_handoff(INTERNAL_FUNCTION_PARAMETERS, int mode)
111{
112 zend_string *domain;
113 zend_long option = UIDNA_DEFAULT,
115 zval *idna_info = NULL;
116
118
120 Z_PARAM_STR(domain)
122 Z_PARAM_LONG(option)
124 Z_PARAM_ZVAL(idna_info)
126
127 if (ZSTR_LEN(domain) == 0) {
130 }
131 if (ZSTR_LEN(domain) > INT32_MAX - 1) {
132 zend_argument_value_error(1, "must be less than " PRId32 " bytes", INT32_MAX);
134 }
136 zend_argument_value_error(2, "must be INTL_IDNA_VARIANT_UTS46");
138 }
139 /* don't check options; it wasn't checked before */
140
141 if (idna_info != NULL) {
142 idna_info = zend_try_array_init(idna_info);
143 if (!idna_info) {
145 }
146 }
147
148 php_intl_idn_to_46(INTERNAL_FUNCTION_PARAM_PASSTHRU, domain, (uint32_t)option, mode, idna_info);
149}
150
151/* {{{ Converts an Unicode domain to ASCII representation, as defined in the IDNA RFC */
156/* }}} */
157
158
159/* {{{ Converts an ASCII representation of the domain to Unicode (UTF-8), as defined in the IDNA RFC */
164/* }}} */
size_t len
Definition apprentice.c:174
const U_ZERO_ERROR
DNS_STATUS status
Definition dns_win32.c:49
char * err
Definition ffi.c:3029
char * mode
#define NULL
Definition gdcache.h:45
#define SUCCESS
Definition hash_sha3.c:261
foreach($dp as $el) foreach( $dp as $el) if( $pass2< 2) echo ""
@ INTL_IDN_TO_ASCII
Definition idn.c:33
@ INTL_IDN_TO_UTF8
Definition idn.c:34
@ INTL_IDN_VARIANT_UTS46
Definition idn.h:23
void intl_error_reset(intl_error *err)
Definition intl_error.c:78
void intl_error_set_code(intl_error *err, UErrorCode err_code)
Definition intl_error.c:141
void intl_error_set_custom_msg(intl_error *err, const char *msg, int copyMsg)
Definition intl_error.c:90
#define PHP_FUNCTION
Definition php.h:364
idn_to_ascii(string $domain, int $flags=IDNA_DEFAULT, int $variant=INTL_IDNA_VARIANT_UTS46, &$idna_info=null)
idn_to_utf8(string $domain, int $flags=IDNA_DEFAULT, int $variant=INTL_IDNA_VARIANT_UTS46, &$idna_info=null)
char * msg
Definition phpdbg.h:289
#define spprintf
Definition spprintf.h:29
Definition file.h:177
#define INTERNAL_FUNCTION_PARAMETERS
Definition zend.h:49
#define INTERNAL_FUNCTION_PARAM_PASSTHRU
Definition zend.h:50
ZEND_API void add_assoc_bool_ex(zval *arg, const char *key, size_t key_len, bool b)
Definition zend_API.c:1946
ZEND_API ZEND_COLD void zend_argument_must_not_be_empty_error(uint32_t arg_num)
Definition zend_API.c:443
ZEND_API void add_assoc_long_ex(zval *arg, const char *key, size_t key_len, zend_long n)
Definition zend_API.c:1928
ZEND_API void add_assoc_str_ex(zval *arg, const char *key, size_t key_len, zend_string *str)
Definition zend_API.c:1973
ZEND_API ZEND_COLD void zend_argument_value_error(uint32_t arg_num, const char *format,...)
Definition zend_API.c:433
#define ZEND_PARSE_PARAMETERS_END()
Definition zend_API.h:1641
#define RETURN_FALSE
Definition zend_API.h:1058
#define RETVAL_STR_COPY(s)
Definition zend_API.h:1016
#define Z_PARAM_OPTIONAL
Definition zend_API.h:1667
#define Z_PARAM_STR(dest)
Definition zend_API.h:2086
#define ZEND_PARSE_PARAMETERS_START(min_num_args, max_num_args)
Definition zend_API.h:1620
#define Z_PARAM_LONG(dest)
Definition zend_API.h:1896
#define RETURN_THROWS()
Definition zend_API.h:1060
#define Z_PARAM_ZVAL(dest)
Definition zend_API.h:2100
#define RETVAL_FALSE
Definition zend_API.h:1032
#define efree(ptr)
Definition zend_alloc.h:155
struct _zval_struct zval
ZEND_API const char * get_active_function_name(void)
int32_t zend_long
Definition zend_long.h:42
struct _zend_string zend_string
#define ZSTR_VAL(zstr)
Definition zend_string.h:68
#define ZSTR_LEN(zstr)
Definition zend_string.h:69
@ FAILURE
Definition zend_types.h:61
ZEND_RESULT_CODE zend_result
Definition zend_types.h:64