diff --git a/lib/framework/utf.c b/lib/framework/utf.c index 4ce3b1b1a..280fb7ec3 100644 --- a/lib/framework/utf.c +++ b/lib/framework/utf.c @@ -318,7 +318,7 @@ static size_t utf16_utf8_buffer_length(const utf_16_char* unicode_string) return length; } -char *UTF16toUTF8(const utf_16_char* unicode_string) +char *UTF16toUTF8(const utf_16_char *unicode_string, size_t *nbytes) { const utf_16_char* curChar; @@ -343,6 +343,12 @@ char *UTF16toUTF8(const utf_16_char* unicode_string) // Terminate the string with a nul character utf8_string[utf8_length] = '\0'; + // Set the number of bytes allocated + if (nbytes) + { + *nbytes = utf8_length + 1; + } + return utf8_string; } @@ -376,7 +382,7 @@ static size_t utf8_as_utf16_buf_size(const char* utf8_string) return length; } -utf_16_char *UTF8toUTF16(const char* utf8_string) +utf_16_char *UTF8toUTF16(const char* utf8_string, size_t *nbytes) { const char* curChar = utf8_string; const size_t unicode_length = utf8_as_utf16_buf_size(utf8_string); @@ -399,6 +405,12 @@ utf_16_char *UTF8toUTF16(const char* utf8_string) // Terminate the string with a nul unicode_string[unicode_length] = '\0'; + // Set the number of bytes allocated + if (nbytes) + { + *nbytes = sizeof(utf_16_char) * (unicode_length + 1); + } + return unicode_string; } diff --git a/lib/framework/utf.h b/lib/framework/utf.h index eb7e5ebbb..74325c376 100644 --- a/lib/framework/utf.h +++ b/lib/framework/utf.h @@ -68,15 +68,17 @@ size_t UTF16CharacterCount(const uint16_t *utf16); /** Encodes a UTF-16 encoded unicode string to a UTF-8 encoded string * \param unicode_string the UTF-16 encoded unicode string to encode into UTF-8 + * \param[out] nbytes the number of bytes allocated, may be NULL * \return a UTF-8 encoded unicode nul terminated string (use free() to deallocate it) */ -char *UTF16toUTF8(const utf_16_char *unicode_string); +char *UTF16toUTF8(const utf_16_char *unicode_string, size_t *nbytes); /** Decodes a UTF-8 encode string to a UTF-16 encoded string (native endianess) * \param utf8_string a UTF-8 encoded nul terminated string + * \param[out] nbytes the number of bytes allocated, may be NULL * \return a UTF-16 encoded unicode nul terminated string (use free() to deallocate it) */ -utf_16_char *UTF8toUTF16(const char *utf8_string); +utf_16_char *UTF8toUTF16(const char *utf8_string, size_t *nbytes); char *UTF8CharacterAtOffset(const char *utf8_string, size_t index); utf_16_char *UTF16CharacterAtOffset(const utf_16_char *utf16_string, size_t index);