123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- #ifdef HAVE_CONFIG_H
- #include "config.h"
- #endif
- #include <php.h>
- #include "intl_common.h"
- #include "intl_convert.h"
- void intl_convert_utf8_to_utf16(
- UChar** target, int32_t* target_len,
- const char* src, size_t src_len,
- UErrorCode* status )
- {
- UChar* dst_buf = NULL;
- int32_t dst_len = 0;
-
- *status = U_ZERO_ERROR;
- if(src_len > INT32_MAX) {
-
- *status = U_BUFFER_OVERFLOW_ERROR;
- return;
- }
- u_strFromUTF8( *target, *target_len, &dst_len, src, (int32_t)src_len, status );
- if( *status == U_ZERO_ERROR )
- {
-
- (*target)[dst_len] = 0;
- *target_len = dst_len;
- return;
- }
-
- if( *status != U_BUFFER_OVERFLOW_ERROR && *status != U_STRING_NOT_TERMINATED_WARNING )
- return;
-
- dst_buf = eumalloc( dst_len + 1 );
-
- *status = U_ZERO_ERROR;
- u_strFromUTF8( dst_buf, dst_len+1, NULL, src, src_len, status );
- if( U_FAILURE( *status ) )
- {
- efree( dst_buf );
- return;
- }
- dst_buf[dst_len] = 0;
- if( *target )
- efree( *target );
- *target = dst_buf;
- *target_len = dst_len;
- }
- zend_string* intl_convert_utf16_to_utf8(
- const UChar* src, int32_t src_len,
- UErrorCode* status )
- {
- zend_string* dst;
- int32_t dst_len;
-
- *status = U_ZERO_ERROR;
- u_strToUTF8( NULL, 0, &dst_len, src, src_len, status );
-
- if( *status != U_BUFFER_OVERFLOW_ERROR && *status != U_STRING_NOT_TERMINATED_WARNING )
- return NULL;
-
- dst = zend_string_alloc(dst_len, 0);
-
- *status = U_ZERO_ERROR;
- u_strToUTF8( ZSTR_VAL(dst), dst_len, NULL, src, src_len, status );
- if( U_FAILURE( *status ) )
- {
- zend_string_efree(dst);
- return NULL;
- }
-
- *status = U_ZERO_ERROR;
- ZSTR_VAL(dst)[dst_len] = 0;
- return dst;
- }
|