From 160a7614dd1888924a9ae44039e1639ab8bced6c Mon Sep 17 00:00:00 2001 From: Pierre Schweitzer Date: Fri, 1 Aug 2008 19:43:18 +0000 Subject: [PATCH] =?UTF-8?q?Changes=20by=20Herv=C3=A9=20Poussineau:=20-=20S?= =?UTF-8?q?implified=20code=20for=20WriteText=20which=20avoids=20infinite?= =?UTF-8?q?=20loops=20and=20fixes=20EOLN=20conversion.=20This=20fixes=20bu?= =?UTF-8?q?g=203551=20Changes=20by=20Pierre=20Schweitzer=20-=20Removed=20s?= =?UTF-8?q?ome=20non-understandable=20unicode=20code=20in=20WriteText=20-?= =?UTF-8?q?=20Added=20a=20check=20to=20prevent=20crash=20when=20trying=20t?= =?UTF-8?q?o=20create=20an=20empty=20file=20-=20Fixed=20formatting=20for?= =?UTF-8?q?=20WriteText=20See=20issue=20#3551=20for=20more=20details.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit svn path=/trunk/; revision=35018 --- reactos/base/applications/notepad/dialog.c | 11 +- reactos/base/applications/notepad/text.c | 126 ++++++++++----------- 2 files changed, 67 insertions(+), 70 deletions(-) diff --git a/reactos/base/applications/notepad/dialog.c b/reactos/base/applications/notepad/dialog.c index 4385f99b8c9..93be73edd65 100644 --- a/reactos/base/applications/notepad/dialog.c +++ b/reactos/base/applications/notepad/dialog.c @@ -235,10 +235,13 @@ static VOID DoSaveFile(VOID) } #endif - if (!WriteText(hFile, (LPWSTR)pTemp, size, Globals.iEncoding, Globals.iEoln)) - ShowLastError(); - else - SendMessage(Globals.hEdit, EM_SETMODIFY, FALSE, 0); + if (size) + { + if (!WriteText(hFile, (LPWSTR)pTemp, size, Globals.iEncoding, Globals.iEoln)) + ShowLastError(); + else + SendMessage(Globals.hEdit, EM_SETMODIFY, FALSE, 0); + } CloseHandle(hFile); HeapFree(GetProcessHeap(), 0, pTemp); diff --git a/reactos/base/applications/notepad/text.c b/reactos/base/applications/notepad/text.c index f99420f91ba..3b6fe23e3fa 100644 --- a/reactos/base/applications/notepad/text.c +++ b/reactos/base/applications/notepad/text.c @@ -316,79 +316,73 @@ done: BOOL WriteText(HANDLE hFile, LPCWSTR pszText, DWORD dwTextLen, int iEncoding, int iEoln) { - WCHAR wcBom; - WCHAR wcEoln; - BYTE bEoln; - LPBYTE pbEoln = NULL; - DWORD dwDummy, dwPos, dwNext, dwEolnSize = 0; + WCHAR wcBom; + BYTE bEoln[1]; + LPBYTE pbEoln = NULL; + DWORD dwDummy, dwPos, dwNext, dwEolnSize = 0; - /* Write the proper byte order marks if not ANSI */ - if (iEncoding != ENCODING_ANSI) - { - wcBom = 0xFEFF; - if (!WriteEncodedText(hFile, &wcBom, 1, iEncoding)) - return FALSE; - } + /* Write the proper byte order marks if not ANSI */ + if (iEncoding != ENCODING_ANSI) + { + wcBom = 0xFEFF; + if (!WriteEncodedText(hFile, &wcBom, 1, iEncoding)) + return FALSE; + } - /* Identify the proper eoln to use */ - switch(iEoln) - { - case EOLN_LF: - bEoln = '\n'; - pbEoln = &bEoln; - dwEolnSize = sizeof(bEoln); - break; - case EOLN_CR: - bEoln = '\r'; - pbEoln = &bEoln; - dwEolnSize = sizeof(bEoln); - break; - } + /* Identify the proper eoln to use */ + switch(iEoln) + { + case EOLN_LF: + bEoln[0] = '\n'; + pbEoln = (LPBYTE) &bEoln; + dwEolnSize = 1; + break; + case EOLN_CR: + bEoln[0] = '\r'; + pbEoln = (LPBYTE) &bEoln; + dwEolnSize = 1; + break; + case EOLN_CRLF: + bEoln[0] = '\r'; + bEoln[1] = '\n'; + pbEoln = (LPBYTE) &bEoln; + dwEolnSize = 2; + break; + default: + return FALSE; + } - /* If we have an eoln, make sure it is of the proper encoding */ - if (pbEoln && ((iEncoding == ENCODING_UNICODE) || (iEncoding == ENCODING_UNICODE_BE))) - { - wcEoln = bEoln; - pbEoln = (LPBYTE) &wcEoln; - dwEolnSize = sizeof(wcEoln); - } + dwPos = 0; - dwPos = 0; + /* pszText eoln are always \r\n */ - while(dwPos < dwTextLen) - { - if (pbEoln) - { - /* Find the next eoln */ - dwNext = dwPos; - while(dwNext < dwTextLen-1) - { - if ((pszText[dwNext] == '\r') && (pszText[dwNext+1] == '\n')) - break; - dwNext++; - } - } - else - { - /* No eoln conversion is necessary */ - dwNext = dwTextLen; - } + do + { + /* Find the next eoln */ + dwNext = dwPos; + while(dwNext < dwTextLen) + { + if (pszText[dwNext] == '\r' && pszText[dwNext + 1] == '\n') + break; + dwNext++; + } - if (!WriteEncodedText(hFile, &pszText[dwPos], dwNext - dwPos, iEncoding)) - return FALSE; - dwPos = dwNext; + /* Write text (without eoln) */ + if (!WriteEncodedText(hFile, &pszText[dwPos], dwNext - dwPos, iEncoding)) + return FALSE; - /* are we at an eoln? */ - while ((dwPos < dwTextLen-1) && - ((pszText[dwPos] == '\r') && (pszText[dwPos+1] == '\n'))) - { - if (!WriteFile(hFile, pbEoln, dwEolnSize, &dwDummy, NULL)) - return FALSE; - dwPos += 2; - } - } - while(dwPos < dwTextLen); + /* Write eoln */ + if (dwNext != dwTextLen) + { + if (!WriteFile(hFile, pbEoln, dwEolnSize, &dwDummy, NULL)) + return FALSE; + } - return TRUE; + /* Skip \r\n */ + dwPos = dwNext + 2; + } + while (dwPos < dwTextLen); + + return TRUE; }