From 86b7cabf4464ba683bce60ed41356d1fff4ebbb2 Mon Sep 17 00:00:00 2001 From: Tsuda Kageyu Date: Sun, 17 Mar 2013 20:00:05 +0900 Subject: [PATCH] Fix UTF-16 decoding where wchar_t is not 16-bit --- taglib/toolkit/tstring.cpp | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/taglib/toolkit/tstring.cpp b/taglib/toolkit/tstring.cpp index e7caa40d..b8f50f3d 100644 --- a/taglib/toolkit/tstring.cpp +++ b/taglib/toolkit/tstring.cpp @@ -764,9 +764,28 @@ void String::copyFromUTF16(const char *s, size_t length, Type t) copyFromUTF16(reinterpret_cast(s), length / 2, t); else { - std::vector sourceBuffer(length / 2); + bool swap; + if(t == UTF16) { + if(length >= 2) { + if(*reinterpret_cast(s) == 0xfeff) + swap = false; // Same as CPU endian. No need to swap bytes. + else if(*reinterpret_cast(s) == 0xfffe) + swap = true; // Not same as CPU endian. Need to swap bytes. + else { + debug("String::prepare() - Invalid UTF16 string."); + return; + } + + s += 2; + length -= 2; + } + } + else + swap = (t != WCharByteOrder); + + d->data.resize(length / 2); for(size_t i = 0; i < length / 2; ++i) { - sourceBuffer[i] = combine(*s, *(s + 1)); + d->data[i] = swap ? combine(*s, *(s + 1)) : combine(*(s + 1), *s); s += 2; } }