1818#include < config.h>
1919#endif
2020
21- #include " ../php_intl.h"
22- #include < unicode/uidna.h>
21+ #include " ../intl_cppshims.h"
22+
23+ #include < unicode/idna.h>
24+ #include < unicode/bytestream.h>
25+ #include < unicode/stringpiece.h>
2326#include < unicode/ustring.h>
2427
28+ #include < memory>
29+
30+ #include " ../php_intl.h"
31+
2532#include " idn.h"
2633
2734#include " intl_error.h"
2835/* }}} */
2936
37+ using icu::CheckedArrayByteSink;
38+ using icu::IDNA ;
39+ using icu::IDNAInfo;
40+ using icu::StringPiece;
41+
3042enum {
3143 INTL_IDN_TO_ASCII = 0 ,
3244 INTL_IDN_TO_UTF8
@@ -48,30 +60,28 @@ static void php_intl_idn_to_46(INTERNAL_FUNCTION_PARAMETERS,
4860 const zend_string *domain, uint32_t option, int mode, zval *idna_info)
4961{
5062 UErrorCode status = U_ZERO_ERROR ;
51- UIDNA *uts46;
5263 int32_t len;
5364 int32_t buffer_capac;
5465 zend_string *buffer;
55- UIDNAInfo info = UIDNA_INFO_INITIALIZER ;
66+ IDNAInfo info;
5667
57- uts46 = uidna_openUTS46 ( option, & status);
68+ std::unique_ptr< IDNA > uts46 ( IDNA::createUTS46Instance ( option, status) );
5869 if (UNEXPECTED (php_intl_idn_check_status (status, " failed to open UIDNA instance" ) == FAILURE )) {
5970 RETURN_FALSE ;
6071 }
6172
73+ StringPiece name (ZSTR_VAL (domain), static_cast <int32_t >(ZSTR_LEN (domain)));
74+
75+ buffer_capac = mode == INTL_IDN_TO_ASCII ? 255 : 252 *4 ;
76+ buffer = zend_string_alloc (buffer_capac, 0 );
77+ CheckedArrayByteSink sink (ZSTR_VAL (buffer), buffer_capac);
6278 if (mode == INTL_IDN_TO_ASCII ) {
63- buffer_capac = 255 ;
64- buffer = zend_string_alloc (buffer_capac, 0 );
65- len = uidna_nameToASCII_UTF8 (uts46, ZSTR_VAL (domain), ZSTR_LEN (domain),
66- ZSTR_VAL (buffer), buffer_capac, &info, &status);
79+ uts46->nameToASCII_UTF8 (name, sink, info, status);
6780 } else {
68- buffer_capac = 252 *4 ;
69- buffer = zend_string_alloc (buffer_capac, 0 );
70- len = uidna_nameToUnicodeUTF8 (uts46, ZSTR_VAL (domain), ZSTR_LEN (domain),
71- ZSTR_VAL (buffer), buffer_capac, &info, &status);
81+ uts46->nameToUnicodeUTF8 (name, sink, info, status);
7282 }
83+ len = sink.NumberOfBytesAppended ();
7384 if (UNEXPECTED (len >= buffer_capac || php_intl_idn_check_status (status, " failed to convert name" ) == FAILURE )) {
74- uidna_close (uts46);
7585 zend_string_efree (buffer);
7686 RETURN_FALSE ;
7787 }
@@ -82,18 +92,16 @@ static void php_intl_idn_to_46(INTERNAL_FUNCTION_PARAMETERS,
8292 if (idna_info) {
8393 add_assoc_str_ex (idna_info, " result" , sizeof (" result" )-1 , zend_string_copy (buffer));
8494 add_assoc_bool_ex (idna_info, " isTransitionalDifferent" ,
85- sizeof (" isTransitionalDifferent" )-1 , info.isTransitionalDifferent );
86- add_assoc_long_ex (idna_info, " errors" , sizeof (" errors" )-1 , (zend_long)info.errors );
95+ sizeof (" isTransitionalDifferent" )-1 , info.isTransitionalDifferent () );
96+ add_assoc_long_ex (idna_info, " errors" , sizeof (" errors" )-1 , (zend_long)info.getErrors () );
8797 }
8898
89- if (info.errors == 0 ) {
99+ if (info.getErrors () == 0 ) {
90100 RETVAL_STR (buffer);
91101 } else {
92102 zend_string_release_ex (buffer, false );
93103 RETVAL_FALSE ;
94104 }
95-
96- uidna_close (uts46);
97105}
98106
99107static void php_intl_idn_handoff (INTERNAL_FUNCTION_PARAMETERS , int mode)
0 commit comments