diff options
author | Jaromir Wysoglad | 2019-07-31 21:45:47 +0200 |
---|---|---|
committer | Filippos Karapetis | 2019-08-24 18:12:45 +0300 |
commit | 24d35df4760678f7592a42c2b78453bdfd6e0050 (patch) | |
tree | cfd02c7525ce33ac00f4d2713666d19a51c4f622 /common | |
parent | 9fa09eeefedcb0b9dbf1c2883a26f37e76315151 (diff) | |
download | scummvm-rg350-24d35df4760678f7592a42c2b78453bdfd6e0050.tar.gz scummvm-rg350-24d35df4760678f7592a42c2b78453bdfd6e0050.tar.bz2 scummvm-rg350-24d35df4760678f7592a42c2b78453bdfd6e0050.zip |
COMMON: Refactor convertIconv
Diffstat (limited to 'common')
-rw-r--r-- | common/encoding.cpp | 11 |
1 files changed, 7 insertions, 4 deletions
diff --git a/common/encoding.cpp b/common/encoding.cpp index 30d8dabd7f..00870e71ab 100644 --- a/common/encoding.cpp +++ b/common/encoding.cpp @@ -173,7 +173,7 @@ char *Encoding::convertIconv(iconv_t iconvHandle, const char *string, size_t len size_t inSize = length; size_t outSize = inSize; - size_t stringSize = inSize > 4 ? inSize : outSize; + size_t stringSize = inSize > 4 ? inSize : 4; #ifdef ICONV_USES_CONST @@ -184,12 +184,11 @@ char *Encoding::convertIconv(iconv_t iconvHandle, const char *string, size_t len memcpy(src, string, length); #endif // ICONV_USES_CONST - char *buffer = (char *) malloc(sizeof(char) * stringSize); + char *buffer = (char *) calloc(sizeof(char), stringSize); if (!buffer) { warning ("Cannot allocate memory for converting string"); return nullptr; } - memset(buffer, 0, stringSize); char *dst = buffer; bool error = false; @@ -215,6 +214,7 @@ char *Encoding::convertIconv(iconv_t iconvHandle, const char *string, size_t len } } } + iconv(iconvHandle, NULL, NULL, &dst, &outSize); // Add a zero character to the end. Hopefuly UTF32 uses the most bytes from // all possible encodings, so add 4 zero bytes. buffer = (char *) realloc(buffer, stringSize + 4); @@ -224,8 +224,11 @@ char *Encoding::convertIconv(iconv_t iconvHandle, const char *string, size_t len delete[] originalSrc; #endif // ICONV_USES_CONST - if (error) + if (error) { + if (buffer) + free(buffer); return nullptr; + } debug("Size: %d", stringSize); return buffer; |