-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtext.c
463 lines (400 loc) · 12.6 KB
/
text.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/*
* PROJECT: ReactOS Notepad
* LICENSE: LGPL-2.1-or-later (https://spdx.org/licenses/LGPL-2.1-or-later)
* PURPOSE: Providing a Windows-compatible simple text editor for ReactOS
* COPYRIGHT: Copyright 1998,99 Marcel Baur <[email protected]>
* Copyright 2002 Sylvain Petreolle <[email protected]>
* Copyright 2002 Andriy Palamarchuk
* Copyright 2019-2023 Katayama Hirofumi MZ <[email protected]>
*/
#include "notepad.h"
#include <assert.h>
static BOOL IsTextNonZeroASCII(LPCVOID pText, DWORD dwSize)
{
const signed char *pch = pText;
while (dwSize-- > 0)
{
if (*pch <= 0)
return FALSE;
++pch;
}
return TRUE;
}
static ENCODING AnalyzeEncoding(const BYTE *pBytes, DWORD dwSize)
{
INT flags = IS_TEXT_UNICODE_STATISTICS | IS_TEXT_UNICODE_REVERSE_STATISTICS;
if (IsTextNonZeroASCII(pBytes, dwSize))
return ENCODING_DEFAULT;
if (IsTextUnicode(pBytes, dwSize, &flags))
return ENCODING_UTF16LE;
if (((flags & IS_TEXT_UNICODE_REVERSE_MASK) == IS_TEXT_UNICODE_REVERSE_STATISTICS))
return ENCODING_UTF16BE;
/* is it UTF-8? */
if (MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, (LPCSTR)pBytes, dwSize, NULL, 0))
return ENCODING_UTF8;
return ENCODING_ANSI;
}
static VOID
ReplaceNewLines(LPWSTR pszNew, SIZE_T cchNew, LPCWSTR pszOld, SIZE_T cchOld)
{
BOOL bPrevCR = FALSE;
SIZE_T ichNew, ichOld;
for (ichOld = ichNew = 0; ichOld < cchOld; ++ichOld)
{
WCHAR ch = pszOld[ichOld];
if (ch == L'\n')
{
if (!bPrevCR)
{
pszNew[ichNew++] = L'\r';
pszNew[ichNew++] = L'\n';
}
}
else if (ch == '\r')
{
pszNew[ichNew++] = L'\r';
pszNew[ichNew++] = L'\n';
}
else
{
pszNew[ichNew++] = ch;
}
bPrevCR = (ch == L'\r');
}
pszNew[ichNew] = UNICODE_NULL;
assert(ichNew == cchNew);
}
static BOOL
ProcessNewLinesAndNulls(HLOCAL *phLocal, LPWSTR *ppszText, SIZE_T *pcchText, EOLN *piEoln)
{
SIZE_T ich, cchText = *pcchText, adwEolnCount[3] = { 0, 0, 0 }, cNonCRLFs;
LPWSTR pszText = *ppszText;
EOLN iEoln;
BOOL bPrevCR = FALSE;
/* Replace '\0' with SPACE. Count newlines. */
for (ich = 0; ich < cchText; ++ich)
{
WCHAR ch = pszText[ich];
if (ch == UNICODE_NULL)
pszText[ich] = L' ';
if (ch == L'\n')
{
if (bPrevCR)
{
adwEolnCount[EOLN_CR]--;
adwEolnCount[EOLN_CRLF]++;
}
else
{
adwEolnCount[EOLN_LF]++;
}
}
else if (ch == '\r')
{
adwEolnCount[EOLN_CR]++;
}
bPrevCR = (ch == L'\r');
}
/* Choose the newline code */
if (adwEolnCount[EOLN_CR] > adwEolnCount[EOLN_CRLF])
iEoln = EOLN_CR;
else if (adwEolnCount[EOLN_LF] > adwEolnCount[EOLN_CRLF])
iEoln = EOLN_LF;
else
iEoln = EOLN_CRLF;
cNonCRLFs = adwEolnCount[EOLN_CR] + adwEolnCount[EOLN_LF];
if (cNonCRLFs != 0)
{
/* Allocate a buffer for EM_SETHANDLE */
SIZE_T cchNew = cchText + cNonCRLFs;
HLOCAL hLocal = LocalAlloc(LMEM_MOVEABLE, (cchNew + 1) * sizeof(WCHAR));
LPWSTR pszNew = LocalLock(hLocal);
if (!pszNew)
{
LocalFree(hLocal);
return FALSE; /* Failure */
}
ReplaceNewLines(pszNew, cchNew, pszText, cchText);
/* Replace with new data */
LocalUnlock(*phLocal);
LocalFree(*phLocal);
*phLocal = hLocal;
*ppszText = pszNew;
*pcchText = cchNew;
}
*piEoln = iEoln;
return TRUE;
}
BOOL
ReadText(HANDLE hFile, HLOCAL *phLocal, ENCODING *pencFile, EOLN *piEoln)
{
LPBYTE pBytes = NULL;
LPWSTR pszText, pszNewText = NULL;
DWORD dwSize, dwPos;
SIZE_T i, cchText, cbContent;
BOOL bSuccess = FALSE;
ENCODING encFile;
UINT iCodePage;
HANDLE hMapping = INVALID_HANDLE_VALUE;
HLOCAL hNewLocal;
dwSize = GetFileSize(hFile, NULL);
if (dwSize == INVALID_FILE_SIZE)
goto done;
if (dwSize == 0) // If file is empty
{
hNewLocal = LocalReAlloc(*phLocal, sizeof(UNICODE_NULL), LMEM_MOVEABLE);
pszNewText = LocalLock(hNewLocal);
if (hNewLocal == NULL || pszNewText == NULL)
goto done;
*pszNewText = UNICODE_NULL;
LocalUnlock(hNewLocal);
*phLocal = hNewLocal;
*piEoln = EOLN_CRLF;
*pencFile = ENCODING_DEFAULT;
return TRUE;
}
hMapping = CreateFileMappingW(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
if (hMapping == NULL)
goto done;
pBytes = MapViewOfFile(hMapping, FILE_MAP_READ, 0, 0, dwSize);
if (!pBytes)
goto done;
/* Look for Byte Order Marks */
dwPos = 0;
if ((dwSize >= 2) && (pBytes[0] == 0xFF) && (pBytes[1] == 0xFE))
{
encFile = ENCODING_UTF16LE;
dwPos += 2;
}
else if ((dwSize >= 2) && (pBytes[0] == 0xFE) && (pBytes[1] == 0xFF))
{
encFile = ENCODING_UTF16BE;
dwPos += 2;
}
else if ((dwSize >= 3) && (pBytes[0] == 0xEF) && (pBytes[1] == 0xBB) && (pBytes[2] == 0xBF))
{
encFile = ENCODING_UTF8BOM;
dwPos += 3;
}
else
{
encFile = AnalyzeEncoding(pBytes, dwSize);
}
switch(encFile)
{
case ENCODING_UTF16BE:
case ENCODING_UTF16LE:
{
/* Re-allocate the buffer for EM_SETHANDLE */
pszText = (LPWSTR) &pBytes[dwPos];
cchText = (dwSize - dwPos) / sizeof(WCHAR);
hNewLocal = LocalReAlloc(*phLocal, (cchText + 1) * sizeof(WCHAR), LMEM_MOVEABLE);
pszNewText = LocalLock(hNewLocal);
if (pszNewText == NULL)
goto done;
*phLocal = hNewLocal;
CopyMemory(pszNewText, pszText, cchText * sizeof(WCHAR));
if (encFile == ENCODING_UTF16BE) /* big endian; Swap bytes */
{
BYTE tmp, *pb = (LPBYTE)pszNewText;
for (i = 0; i < cchText * 2; i += 2)
{
tmp = pb[i];
pb[i] = pb[i + 1];
pb[i + 1] = tmp;
}
}
break;
}
case ENCODING_ANSI:
case ENCODING_UTF8:
case ENCODING_UTF8BOM:
{
iCodePage = ((encFile == ENCODING_UTF8 || encFile == ENCODING_UTF8BOM) ? CP_UTF8 : CP_ACP);
/* Get ready for ANSI-to-Wide conversion */
cbContent = dwSize - dwPos;
cchText = 0;
if (cbContent > 0)
{
cchText = MultiByteToWideChar(iCodePage, 0, (LPCSTR)&pBytes[dwPos], (INT)cbContent, NULL, 0);
if (cchText == 0)
goto done;
}
/* Re-allocate the buffer for EM_SETHANDLE */
hNewLocal = LocalReAlloc(*phLocal, (cchText + 1) * sizeof(WCHAR), LMEM_MOVEABLE);
pszNewText = LocalLock(hNewLocal);
if (!pszNewText)
goto done;
*phLocal = hNewLocal;
/* Do ANSI-to-Wide conversion */
if (cbContent > 0)
{
if (!MultiByteToWideChar(iCodePage, 0, (LPCSTR)&pBytes[dwPos], (INT)cbContent,
pszNewText, (INT)cchText))
{
goto done;
}
}
break;
}
DEFAULT_UNREACHABLE;
}
pszNewText[cchText] = UNICODE_NULL;
if (!ProcessNewLinesAndNulls(phLocal, &pszNewText, &cchText, piEoln))
goto done;
*pencFile = encFile;
bSuccess = TRUE;
done:
if (pBytes)
UnmapViewOfFile(pBytes);
if (hMapping != INVALID_HANDLE_VALUE)
CloseHandle(hMapping);
if (pszNewText)
LocalUnlock(*phLocal);
return bSuccess;
}
static BOOL WriteEncodedText(HANDLE hFile, LPCWSTR pszText, DWORD dwTextLen, ENCODING encFile)
{
LPBYTE pBytes = NULL;
LPBYTE pAllocBuffer = NULL;
DWORD dwPos = 0;
DWORD dwByteCount;
BYTE buffer[1024];
UINT iCodePage = 0;
DWORD dwDummy, i;
BOOL bSuccess = FALSE;
int iBufferSize, iRequiredBytes;
BYTE b;
while(dwPos < dwTextLen)
{
switch(encFile)
{
case ENCODING_UTF16LE:
pBytes = (LPBYTE) &pszText[dwPos];
dwByteCount = (dwTextLen - dwPos) * sizeof(WCHAR);
dwPos = dwTextLen;
break;
case ENCODING_UTF16BE:
dwByteCount = (dwTextLen - dwPos) * sizeof(WCHAR);
if (dwByteCount > sizeof(buffer))
dwByteCount = sizeof(buffer);
memcpy(buffer, &pszText[dwPos], dwByteCount);
for (i = 0; i < dwByteCount; i += 2)
{
b = buffer[i+0];
buffer[i+0] = buffer[i+1];
buffer[i+1] = b;
}
pBytes = (LPBYTE) &buffer[dwPos];
dwPos += dwByteCount / sizeof(WCHAR);
break;
case ENCODING_ANSI:
case ENCODING_UTF8:
case ENCODING_UTF8BOM:
if (encFile == ENCODING_UTF8 || encFile == ENCODING_UTF8BOM)
iCodePage = CP_UTF8;
else
iCodePage = CP_ACP;
iRequiredBytes = WideCharToMultiByte(iCodePage, 0, &pszText[dwPos], dwTextLen - dwPos, NULL, 0, NULL, NULL);
if (iRequiredBytes <= 0)
{
goto done;
}
else if (iRequiredBytes < sizeof(buffer))
{
pBytes = buffer;
iBufferSize = sizeof(buffer);
}
else
{
pAllocBuffer = (LPBYTE) HeapAlloc(GetProcessHeap(), 0, iRequiredBytes);
if (!pAllocBuffer)
return FALSE;
pBytes = pAllocBuffer;
iBufferSize = iRequiredBytes;
}
dwByteCount = WideCharToMultiByte(iCodePage, 0, &pszText[dwPos], dwTextLen - dwPos, (LPSTR) pBytes, iBufferSize, NULL, NULL);
if (!dwByteCount)
goto done;
dwPos = dwTextLen;
break;
default:
goto done;
}
if (!WriteFile(hFile, pBytes, dwByteCount, &dwDummy, NULL))
goto done;
/* free the buffer, if we have allocated one */
if (pAllocBuffer)
{
HeapFree(GetProcessHeap(), 0, pAllocBuffer);
pAllocBuffer = NULL;
}
}
bSuccess = TRUE;
done:
if (pAllocBuffer)
HeapFree(GetProcessHeap(), 0, pAllocBuffer);
return bSuccess;
}
BOOL WriteText(HANDLE hFile, LPCWSTR pszText, DWORD dwTextLen, ENCODING encFile, EOLN iEoln)
{
WCHAR wcBom;
LPCWSTR pszLF = L"\n";
DWORD dwPos, dwNext;
/* Write the proper byte order marks if not ANSI or UTF-8 without BOM */
if (encFile != ENCODING_ANSI && encFile != ENCODING_UTF8)
{
wcBom = 0xFEFF;
if (!WriteEncodedText(hFile, &wcBom, 1, encFile))
return FALSE;
}
dwPos = 0;
/* pszText eoln are always \r\n */
do
{
/* Find the next eoln */
dwNext = dwPos;
while(dwNext < dwTextLen)
{
if (pszText[dwNext] == '\r' && pszText[dwNext + 1] == '\n')
break;
dwNext++;
}
if (dwNext != dwTextLen)
{
switch (iEoln)
{
case EOLN_LF:
/* Write text (without eoln) */
if (!WriteEncodedText(hFile, &pszText[dwPos], dwNext - dwPos, encFile))
return FALSE;
/* Write eoln */
if (!WriteEncodedText(hFile, pszLF, 1, encFile))
return FALSE;
break;
case EOLN_CR:
/* Write text (including \r as eoln) */
if (!WriteEncodedText(hFile, &pszText[dwPos], dwNext - dwPos + 1, encFile))
return FALSE;
break;
case EOLN_CRLF:
/* Write text (including \r\n as eoln) */
if (!WriteEncodedText(hFile, &pszText[dwPos], dwNext - dwPos + 2, encFile))
return FALSE;
break;
default:
return FALSE;
}
}
else
{
/* Write text (without eoln, since this is the end of the file) */
if (!WriteEncodedText(hFile, &pszText[dwPos], dwNext - dwPos, encFile))
return FALSE;
}
/* Skip \r\n */
dwPos = dwNext + 2;
}
while (dwPos < dwTextLen);
return TRUE;
}