From df3abf1db725f951ef68be3b0f8fe94d8f8f0bb6 Mon Sep 17 00:00:00 2001 From: "Zane U. Ji" Date: Thu, 2 Aug 2012 19:21:47 +0800 Subject: [PATCH] Bug #2146675 cannot open UTF-16 little endian coded file --- src/xmlcopyeditor.cpp | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/src/xmlcopyeditor.cpp b/src/xmlcopyeditor.cpp index 32b91dd..883071a 100755 --- a/src/xmlcopyeditor.cpp +++ b/src/xmlcopyeditor.cpp @@ -3054,6 +3054,7 @@ bool MyFrame::openFile ( wxString& fileName, bool largeFile ) } // convert buffer if not UTF-8 + int nBOM = 0; if ( isUtf8 ) { finalBuffer = docBuffer; @@ -3069,37 +3070,32 @@ bool MyFrame::openFile ( wxString& fileName, bool largeFile ) ( unsigned char ) docBuffer[2] == 0xFE && ( unsigned char ) docBuffer[3] == 0xFF ) { - docBuffer += 4; - docBufferLen -= 4; + nBOM = 4; } - if ( docBuffer && // UTF-32 LE + else if ( docBuffer && // UTF-32 LE ( unsigned char ) docBuffer[0] == 0xFF && ( unsigned char ) docBuffer[1] == 0xFE && ( unsigned char ) docBuffer[2] == 0x00 && ( unsigned char ) docBuffer[3] == 0x00 ) { - docBuffer += 4; - docBufferLen -= 4; + nBOM = 4; } - - if ( docBuffer && //UTF-16 BE + else if ( docBuffer && //UTF-16 BE ( unsigned char ) docBuffer[0] == 0xFE && ( unsigned char ) docBuffer[1] == 0xFF ) { - docBuffer += 2; - docBufferLen -= 2; + nBOM = 2; } - if ( docBuffer && //UTF-16 LE + else if ( docBuffer && //UTF-16 LE ( unsigned char ) docBuffer[0] == 0xFF && ( unsigned char ) docBuffer[1] == 0xFE ) { - docBuffer += 2; - docBufferLen -= 2; + nBOM = 2; } if ( !encoding.size() ) // Expat couldn't parse file (e.g. UTF-32) { - encoding = getApproximateEncoding ( docBuffer, docBufferLen ); + encoding = getApproximateEncoding ( docBuffer + nBOM, docBufferLen - nBOM ); } wxString wideEncoding = wxString ( @@ -3134,7 +3130,7 @@ bool MyFrame::openFile ( wxString& fileName, bool largeFile ) } size_t iconvBufferLeft, docBufferLeft; - iconvBufferLen = iconvBufferLeft = docBufferLen * iconvLenMultiplier + 1; + iconvBufferLen = iconvBufferLeft = (docBufferLen - nBOM) * iconvLenMultiplier + 1; docBufferLeft = docBufferLen; iconvBuffer = new char[iconvBufferLen]; finalBuffer = iconvBuffer; // iconvBuffer will be incremented by iconv