X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/4cb0e8d05cadea6be3a7bd93f1fea9a9e0df95f0..c81394808bf7efd84e4294e44a9a9a7b7f6dd8a9:/src/common/convauto.cpp?ds=sidebyside diff --git a/src/common/convauto.cpp b/src/common/convauto.cpp index c9ff7df9f6..8620d4e02e 100644 --- a/src/common/convauto.cpp +++ b/src/common/convauto.cpp @@ -23,12 +23,6 @@ #pragma hdrstop #endif -#if wxUSE_WCHAR_T - -#ifndef WX_PRECOMP - #include "wx/wx.h" -#endif //WX_PRECOMP - #include "wx/convauto.h" // we use latin1 by default as it seems the least bad choice: the files we need @@ -107,11 +101,13 @@ wxConvAuto::BOMType wxConvAuto::DetectBOM(const char *src, size_t srcLen) if ( src[0] == '\x00' && src[1] == '\x00' ) { - // this could only be UTF-32BE - if ( srcLen == 3 && src[2] == '\xFE' ) - return BOM_Unknown; - } + // this could only be UTF-32BE, check that the data we have so + // far allows for it + if ( srcLen == 3 && src[2] != '\xFE' ) + return BOM_None; + return BOM_Unknown; + } break; default: @@ -227,14 +223,13 @@ void wxConvAuto::SkipBOM(const char **src, size_t *len) const *len -= ofs; } -bool wxConvAuto::InitFromInput(const char **src, size_t *len) +bool wxConvAuto::InitFromInput(const char *src, size_t len) { - m_bomType = DetectBOM(*src, *len); + m_bomType = DetectBOM(src, len == wxNO_LEN ? strlen(src) : len); if ( m_bomType == BOM_Unknown ) return false; InitFromBOM(m_bomType); - SkipBOM(src, len); return true; } @@ -253,7 +248,7 @@ wxConvAuto::ToWChar(wchar_t *dst, size_t dstLen, if ( !m_conv ) { - if ( !self->InitFromInput(&src, &srcLen) ) + if ( !self->InitFromInput(src, srcLen) ) { // there is not enough data to determine whether we have a BOM or // not, so fail for now -- the caller is supposed to call us again @@ -261,9 +256,21 @@ wxConvAuto::ToWChar(wchar_t *dst, size_t dstLen, return wxCONV_FAILED; } } - else if ( !m_consumedBOM && dst ) + + if ( !m_consumedBOM ) { SkipBOM(&src, &srcLen); + if ( srcLen == 0 ) + { + // there is nothing left except the BOM so we'd return 0 below but + // this is unexpected: decoding a non-empty string must either fail + // or return something non-empty, in particular this would break + // the code in wxTextInputStream::NextChar() + // + // so still return an error as we need some more data to be able to + // decode it + return wxCONV_FAILED; + } } // try to convert using the auto-detected encoding @@ -286,8 +293,10 @@ wxConvAuto::ToWChar(wchar_t *dst, size_t dstLen, } } - if (rc != wxCONV_FAILED && dst && !m_consumedBOM) + // don't skip the BOM again the next time if we really consumed it + if ( rc != wxCONV_FAILED && dst && !m_consumedBOM ) self->m_consumedBOM = true; + return rc; } @@ -303,5 +312,3 @@ wxConvAuto::FromWChar(char *dst, size_t dstLen, return m_conv->FromWChar(dst, dstLen, src, srcLen); } - -#endif // wxUSE_WCHAR_T