| 1 |
/* |
| 2 |
* utf.cpp |
| 3 |
* |
| 4 |
* This file is a part of NSIS. |
| 5 |
* |
| 6 |
* Copyright (C) 2011-2026 Anders Kjersem |
| 7 |
* |
| 8 |
* Licensed under the zlib/libpng license (the "License"); |
| 9 |
* you may not use this file except in compliance with the License. |
| 10 |
* |
| 11 |
* Licence details can be found in the file COPYING. |
| 12 |
* |
| 13 |
* This software is provided 'as-is', without any express or implied |
| 14 |
* warranty. |
| 15 |
* |
| 16 |
*/ |
| 17 |
|
| 18 |
#include "utf.h" |
| 19 |
#include "util.h" |
| 20 |
|
| 21 |
#define FIX_ENDIAN_INT16LETOHOST_INPLACE FIX_ENDIAN_INT16_INPLACE |
| 22 |
|
| 23 |
UINT StrLenUTF16(const void*str) |
| 24 |
{ |
| 25 |
return sizeof(wchar_t) == 2 ? (UINT)wcslen((wchar_t*)str) : InlineStrLenUTF16(str); |
| 26 |
} |
| 27 |
|
| 28 |
bool StrSetUTF16LE(tstring&dest, const void*src) |
| 29 |
{ |
| 30 |
#ifndef _WIN32 |
| 31 |
CharEncConv cec; |
| 32 |
if (!cec.Initialize(-1,NStreamEncoding::UTF16LE)) return false; |
| 33 |
src = (const void*) cec.Convert(src); |
| 34 |
if (!src) return false; |
| 35 |
#endif |
| 36 |
#ifdef C_ASSERT |
| 37 |
C_ASSERT(sizeof(tstring::value_type) >= sizeof(wchar_t)); |
| 38 |
#endif |
| 39 |
try { dest = (wchar_t*) src; } catch(...) { return false; } |
| 40 |
return true; |
| 41 |
} |
| 42 |
|
| 43 |
void UTF16InplaceEndianSwap(void*Buffer, UINT cch) |
| 44 |
{ |
| 45 |
unsigned short *p = (unsigned short *) Buffer; |
| 46 |
while(cch--) p[cch] = SWAP_ENDIAN_INT16(p[cch]); |
| 47 |
} |
| 48 |
|
| 49 |
inline UINT UTF8ToWC_Convert(LPCSTR StrU8,UINT cbU8,wchar_t*Buffer,UINT cchBuf) |
| 50 |
{ |
| 51 |
#ifndef MB_ERR_INVALID_CHARS |
| 52 |
const UINT MB_ERR_INVALID_CHARS = 8; // MSDN says this flag is OK for CP_UTF8 |
| 53 |
#endif |
| 54 |
return (UINT) MultiByteToWideChar(CP_UTF8,MB_ERR_INVALID_CHARS,StrU8,cbU8,Buffer,cchBuf); |
| 55 |
} |
| 56 |
inline UINT UTF8ToWC_Prepare(LPCSTR StrU8,UINT cbU8) |
| 57 |
{ |
| 58 |
return UTF8ToWC_Convert(StrU8,cbU8,0,0); |
| 59 |
} |
| 60 |
|
| 61 |
UINT WCFromCodePoint(wchar_t*Dest,UINT cchDest,UINT32 CodPt) |
| 62 |
{ |
| 63 |
// Don't allow half surrogate pairs |
| 64 |
if (CodPt >= 0xd800 && CodPt <= 0xdfff) CodPt = UNICODE_REPLACEMENT_CHARACTER; |
| 65 |
#ifdef _WIN32 |
| 66 |
if (CodPt <= 0xffff && cchDest) |
| 67 |
{ |
| 68 |
*Dest = (wchar_t) CodPt; |
| 69 |
return 1; |
| 70 |
} |
| 71 |
else if (cchDest >= 2) |
| 72 |
{ |
| 73 |
const UINT32 lead_offset = 0xd800 - (0x10000 >> 10); |
| 74 |
UINT16 lead = lead_offset + (CodPt >> 10), trail = 0xdc00 + (CodPt & 0x3ff); |
| 75 |
Dest[0] = lead, Dest[1] = trail; |
| 76 |
return 2; |
| 77 |
} |
| 78 |
return 0; |
| 79 |
#else |
| 80 |
iconvdescriptor iconvd; |
| 81 |
if (!iconvd.Open("wchar_t",iconvdescriptor::GetHostEndianUCS4Code())) return 0; |
| 82 |
size_t inleft = 4; |
| 83 |
UINT cchW = iconvd.Convert(&CodPt,&inleft,Dest,cchDest*sizeof(wchar_t)) / sizeof(wchar_t); |
| 84 |
return !inleft ? cchW : 0; |
| 85 |
#endif |
| 86 |
} |
| 87 |
|
| 88 |
wchar_t* DupWCFromBytes(void*Buffer,UINT cbBuffer,UINT32 SrcCP) |
| 89 |
{ |
| 90 |
/*\ |
| 91 |
Converts a buffer encoded with SrcCP to a \0 terminated wchar_t malloc'ed buffer. |
| 92 |
Returns 0 on failure. |
| 93 |
\*/ |
| 94 |
CharEncConv cec; |
| 95 |
cec.SetAllowOptimizedReturn(!!(SrcCP&DWCFBF_ALLOWOPTIMIZEDRETURN)); |
| 96 |
if (!cec.Initialize(-1, SrcCP&=~DWCFBF_ALLOWOPTIMIZEDRETURN)) return 0; |
| 97 |
wchar_t *pWC = (wchar_t*) cec.Convert(Buffer, cbBuffer); |
| 98 |
return pWC ? (wchar_t*) cec.Detach() : 0; |
| 99 |
} |
| 100 |
|
| 101 |
BOOL CharEncConv::IsValidCodePage(UINT cp) |
| 102 |
{ |
| 103 |
#ifdef _WIN32 |
| 104 |
if (cp <= 1 || NStreamEncoding::IsUnicodeCodepage(cp)) return true; // Allow ACP/OEM/UTF* |
| 105 |
#endif |
| 106 |
return cp < NStreamEncoding::CPCOUNT && ::IsValidCodePage(cp); |
| 107 |
} |
| 108 |
bool CharEncConv::Initialize(UINT32 ToEnc, UINT32 FromEnc) |
| 109 |
{ |
| 110 |
/* |
| 111 |
** Initialize() with a Windows codepage or -1 for wchar_t |
| 112 |
*/ |
| 113 |
const WORD UTF32LE = NStreamEncoding::UTF32LE; |
| 114 |
#ifdef _WIN32 |
| 115 |
if (NStreamEncoding::UTF16LE == ToEnc) ToEnc = -1; |
| 116 |
if (NStreamEncoding::UTF16LE == FromEnc) FromEnc = -1; |
| 117 |
#endif |
| 118 |
m_TE = (WORD) ToEnc, m_FE = (WORD) FromEnc; |
| 119 |
if ((UTF32LE|1) == (m_FE|1) || (UTF32LE|1) == (m_TE|1)) return false; // UTF-32 is a pain to deal with on Windows |
| 120 |
#ifdef _WIN32 |
| 121 |
return (IsWE(m_FE) || IsValidCodePage(FromEnc)) && (IsWE(m_TE) || IsValidCodePage(ToEnc)); |
| 122 |
#else |
| 123 |
char f[50], t[COUNTOF(f)]; |
| 124 |
if (IsWE(m_FE)) strcpy(f, "wchar_t"); else create_code_page_string(f, COUNTOF(f), m_FE); |
| 125 |
if (IsWE(m_TE)) strcpy(t, "wchar_t"); else create_code_page_string(t, COUNTOF(t), m_TE); |
| 126 |
return m_TE == m_FE || m_iconvd.Open(t, f); |
| 127 |
#endif |
| 128 |
} |
| 129 |
size_t CharEncConv::GuessOutputSize(size_t cbConverted) |
| 130 |
{ |
| 131 |
UINT cus = IsWE(m_TE) ? sizeof(wchar_t) : NStreamEncoding::GetCodeUnitSize(m_TE); |
| 132 |
size_t zt = 1, cch = cbConverted / cus; |
| 133 |
if (!cch) return 0; |
| 134 |
switch(cus) |
| 135 |
{ |
| 136 |
case 1: zt = !((char*)m_Result)[--cch]; break; |
| 137 |
case 2: zt = !((WORD*)m_Result)[--cch]; break; |
| 138 |
case 4: zt = !((UINT32*)m_Result)[--cch]; break; |
| 139 |
} |
| 140 |
return (cch + (zt ? 0 : 1)) * cus; |
| 141 |
} |
| 142 |
void* CharEncConv::Convert(const void*Src, size_t cbSrc, size_t*cbOut) |
| 143 |
{ |
| 144 |
/* |
| 145 |
** Convert() mallocs a buffer and converts Src (as m_FE) to m_TE. |
| 146 |
** If cbSrc is -1 the size is calculated. cbOut can be NULL. |
| 147 |
** Returns a pointer to the buffer on success or 0 on error. |
| 148 |
** The buffer is valid until you call Close() or Convert(). |
| 149 |
*/ |
| 150 |
#ifdef _WIN32 |
| 151 |
m_OptimizedReturn = false; |
| 152 |
#endif |
| 153 |
if ((size_t)-1 == cbSrc) |
| 154 |
{ |
| 155 |
UINT cus = IsWE(m_FE) ? sizeof(wchar_t) : NStreamEncoding::GetCodeUnitSize(m_FE); |
| 156 |
switch(cus) |
| 157 |
{ |
| 158 |
case 1: cbSrc = strlen((char*)Src); break; |
| 159 |
case 2: cbSrc = StrLenUTF16(Src); break; |
| 160 |
//case 4: // No UTF-32 support... |
| 161 |
default: |
| 162 |
if (sizeof(wchar_t) > 2 && sizeof(wchar_t) == cus) |
| 163 |
{ |
| 164 |
cbSrc = wcslen((wchar_t*)Src); |
| 165 |
break; |
| 166 |
} |
| 167 |
assert(0); |
| 168 |
return 0; |
| 169 |
} |
| 170 |
cbSrc = (cbSrc + 1) * cus; |
| 171 |
} |
| 172 |
if (m_FE == m_TE) |
| 173 |
{ |
| 174 |
#ifdef _WIN32 |
| 175 |
if (m_AllowOptimizedReturn && IsWE(m_FE)) |
| 176 |
{ |
| 177 |
if (cbOut) |
| 178 |
{ |
| 179 |
cbSrc /= sizeof(wchar_t); |
| 180 |
if (cbSrc && ((WORD*)Src)[--cbSrc]) ++cbSrc; |
| 181 |
*cbOut = cbSrc * sizeof(wchar_t); |
| 182 |
} |
| 183 |
m_OptimizedReturn = true; |
| 184 |
return (void*) (m_Result = (char*) Src); |
| 185 |
} |
| 186 |
#endif |
| 187 |
char *p = (char*) realloc(m_Result, cbSrc + sizeof(UINT32)); |
| 188 |
if (p) m_Result = p; else return 0; |
| 189 |
memcpy(p, Src, cbSrc); |
| 190 |
*((UINT32*)(p+cbSrc)) = 0; |
| 191 |
if (cbOut) *cbOut = GuessOutputSize(cbSrc); |
| 192 |
return m_Result; |
| 193 |
} |
| 194 |
#ifdef _WIN32 |
| 195 |
if (!IsWE(m_FE) && !IsWE(m_TE) && NStreamEncoding::UTF16BE != m_TE) |
| 196 |
{ |
| 197 |
// We need a middle step: Src -> wchar_t -> Target |
| 198 |
CharEncConv cec; |
| 199 |
if (!cec.Initialize(-1, m_FE)) return 0; |
| 200 |
size_t cbConv; |
| 201 |
char *pWC = (char*) cec.Convert(Src, cbSrc, &cbConv); |
| 202 |
if (!pWC) return 0; |
| 203 |
this->m_FE = -1; |
| 204 |
return this->Convert(pWC, cbConv, cbOut); |
| 205 |
} |
| 206 |
if (IsWE(m_FE)) |
| 207 |
{ |
| 208 |
if (NStreamEncoding::UTF16BE == m_TE) goto l_swapUTF16; |
| 209 |
cbSrc /= sizeof(wchar_t); |
| 210 |
UINT cbDest = WideCharToMultiByte(m_TE, 0, (wchar_t*)Src, (int)cbSrc, 0, 0, 0, 0); |
| 211 |
char *p = (char*) realloc(m_Result, (cbDest + 1) * sizeof(char)); |
| 212 |
if (p) m_Result = p; else return 0; |
| 213 |
if (!(cbDest = WideCharToMultiByte(m_TE, 0, (wchar_t*)Src, (int)cbSrc, p, (int)cbDest, 0, 0))) return 0; |
| 214 |
if (p[--cbDest]) p[++cbDest] = '\0'; // Always \0 terminate |
| 215 |
if (cbOut) *cbOut = cbDest; // cbOut never includes the \0 terminator |
| 216 |
} |
| 217 |
else |
| 218 |
{ |
| 219 |
UINT cchDest; |
| 220 |
if (NStreamEncoding::UTF16BE == m_FE) // UTF16BE -> UTF16LE/wchar_t |
| 221 |
{ |
| 222 |
l_swapUTF16: |
| 223 |
char *p = (char*) realloc(m_Result, cbSrc + sizeof(wchar_t)); |
| 224 |
if (p) m_Result = p; else return 0; |
| 225 |
memcpy(p, Src, cbSrc); |
| 226 |
cchDest = (UINT) (cbSrc / sizeof(wchar_t)); |
| 227 |
UTF16InplaceEndianSwap(p, cchDest); |
| 228 |
if (!cchDest) *((WORD*)p) = 0, ++cchDest; // For "--cchDest" during \0 termination |
| 229 |
} |
| 230 |
else |
| 231 |
{ |
| 232 |
cchDest = MultiByteToWideChar(m_FE, 0, (char*)Src, (int)cbSrc, 0, 0); |
| 233 |
char *p = (char*) realloc(m_Result, (cchDest + 1) * sizeof(wchar_t)); |
| 234 |
if (p) m_Result = p; else return 0; |
| 235 |
if (!(cchDest = MultiByteToWideChar(m_FE, 0, (char*)Src, (int)cbSrc, (LPWSTR)p, (int)cchDest))) return 0; |
| 236 |
if (NStreamEncoding::UTF16BE == m_TE) UTF16InplaceEndianSwap(p, cchDest); |
| 237 |
} |
| 238 |
if (((WORD*)m_Result)[--cchDest]) ((WORD*)m_Result)[++cchDest] = '\0'; |
| 239 |
if (cbOut) *cbOut = cchDest * sizeof(wchar_t); |
| 240 |
} |
| 241 |
#else |
| 242 |
char *in = (char*) Src; |
| 243 |
size_t cbConv; |
| 244 |
if (!nsis_iconv_reallociconv(m_iconvd, &in, &cbSrc, &m_Result, cbConv)) return 0; |
| 245 |
if (cbOut) *cbOut = GuessOutputSize(cbConv); |
| 246 |
#endif |
| 247 |
return m_Result; |
| 248 |
} |
| 249 |
|
| 250 |
#if !defined(_WIN32) || !defined(_UNICODE) |
| 251 |
bool WCToUTF16LEHlpr::Create(const TCHAR*in, unsigned int codepage) |
| 252 |
{ |
| 253 |
CharEncConv cec; |
| 254 |
if (!cec.Initialize(NStreamEncoding::UTF16LE, -1)) return false; |
| 255 |
if (!cec.Convert(in)) return false; |
| 256 |
m_s = (unsigned short*) cec.Detach(); |
| 257 |
return true; |
| 258 |
} |
| 259 |
#endif |
| 260 |
|
| 261 |
UINT DetectUTFBOM(void*Buffer, UINT cb) |
| 262 |
{ |
| 263 |
unsigned char *b = (unsigned char*) Buffer; |
| 264 |
if (cb >= 3 && 0xef == b[0] && 0xbb == b[1] && 0xbf == b[2]) |
| 265 |
return NStreamEncoding::UTF8; |
| 266 |
if (cb >= 2) |
| 267 |
{ |
| 268 |
if (cb >= 4 && !b[0] && !b[1] && 0xfe == b[2] && 0xff == b[3]) |
| 269 |
return NStreamEncoding::UTF32BE; |
| 270 |
if (0xff == b[0] && 0xfe == b[1]) |
| 271 |
return (cb >= 4 && !b[2] && !b[3]) ? NStreamEncoding::UTF32LE : NStreamEncoding::UTF16LE; |
| 272 |
if (0xfe == b[0] && 0xff == b[1]) |
| 273 |
return NStreamEncoding::UTF16BE; |
| 274 |
} |
| 275 |
return 0; |
| 276 |
} |
| 277 |
|
| 278 |
UINT DetectUTFBOM(FILE*strm) |
| 279 |
{ |
| 280 |
/*\ |
| 281 |
Tries to detect a BOM at the current position in a stream. |
| 282 |
If a BOM is found it is eaten. |
| 283 |
NOTE: ungetc is only guaranteed to support 1 pushback, |
| 284 |
lets hope no MBCS file starts with parts of a BOM. |
| 285 |
\*/ |
| 286 |
const int b1 = fgetc(strm); |
| 287 |
if (EOF == b1) return 0; |
| 288 |
if (0xef == b1) |
| 289 |
{ |
| 290 |
const int b2 = fgetc(strm); |
| 291 |
if (0xbb == b2) |
| 292 |
{ |
| 293 |
const int b3 = fgetc(strm); |
| 294 |
if (0xbf == b3) return NStreamEncoding::UTF8; |
| 295 |
ungetc(b3,strm); |
| 296 |
} |
| 297 |
ungetc(b2,strm); |
| 298 |
} |
| 299 |
if (0xfe == b1 || 0xff == b1 || 0x00 == b1) |
| 300 |
{ |
| 301 |
const int b2 = fgetc(strm), b3 = fgetc(strm); |
| 302 |
if (b1 && (b1^b2) == (0xfe^0xff)) |
| 303 |
{ |
| 304 |
if (0xff == b1 && 0 == b3) |
| 305 |
{ |
| 306 |
const int b4 = fgetc(strm); |
| 307 |
if (0 == b4) return NStreamEncoding::UTF32LE; |
| 308 |
ungetc(b4,strm); |
| 309 |
} |
| 310 |
ungetc(b3,strm); |
| 311 |
return 0xff == b1 ? NStreamEncoding::UTF16LE : NStreamEncoding::UTF16BE; |
| 312 |
} |
| 313 |
if (0 == b1 && 0 == b2) |
| 314 |
{ |
| 315 |
if (0xfe == b3) |
| 316 |
{ |
| 317 |
const int b4 = fgetc(strm); |
| 318 |
if (0xff == b4) return NStreamEncoding::UTF32BE; |
| 319 |
ungetc(b4,strm); |
| 320 |
} |
| 321 |
} |
| 322 |
ungetc(b3,strm); |
| 323 |
ungetc(b2,strm); |
| 324 |
} |
| 325 |
ungetc(b1,strm); |
| 326 |
return 0; |
| 327 |
} |
| 328 |
|
| 329 |
static bool IsUTFEncodingString(const TCHAR*s, const TCHAR*suff) |
| 330 |
{ |
| 331 |
if (S7ChLwr(s[0]) == 'u' && S7ChLwr(s[1]) == 't' && S7ChLwr(s[2]) == 'f') |
| 332 |
{ |
| 333 |
s += 3 + (s[3] == '-' || s[3] == '_'); |
| 334 |
return !_tcsicmp(s, suff); |
| 335 |
} |
| 336 |
return false; |
| 337 |
} |
| 338 |
|
| 339 |
WORD GetEncodingFromString(const TCHAR*s, bool&BOM) |
| 340 |
{ |
| 341 |
BOM = false; |
| 342 |
if (!_tcsicmp(s,_T("ACP"))) |
| 343 |
return NStreamEncoding::ACP; |
| 344 |
if (!_tcsicmp(s,_T("OEM"))) |
| 345 |
return NStreamEncoding::OEMCP; |
| 346 |
if (IsUTFEncodingString(s, _T("8"))) |
| 347 |
return NStreamEncoding::UTF8; |
| 348 |
if (IsUTFEncodingString(s, _T("8SIG")) || IsUTFEncodingString(s, _T("8BOM"))) |
| 349 |
return (BOM = true, NStreamEncoding::UTF8); |
| 350 |
if (IsUTFEncodingString(s, _T("16LE")) || (IsUTFEncodingString(s, _T("16LEBOM")) && (BOM = true))) |
| 351 |
return NStreamEncoding::UTF16LE; |
| 352 |
if (IsUTFEncodingString(s, _T("16BE")) || (IsUTFEncodingString(s, _T("16BEBOM")) && (BOM = true))) |
| 353 |
return NStreamEncoding::UTF16LE; |
| 354 |
if (S7IsChEqualI('C', *s++) && S7IsChEqualI('P', *s++)) |
| 355 |
{ |
| 356 |
int cp = _tstoi(s); |
| 357 |
if (cp > 0 && cp < NStreamEncoding::CPCOUNT) return (WORD) cp; |
| 358 |
} |
| 359 |
return NStreamEncoding::UNKNOWN; |
| 360 |
} |
| 361 |
|
| 362 |
WORD GetEncodingFromString(const TCHAR*s) |
| 363 |
{ |
| 364 |
bool bom; |
| 365 |
return GetEncodingFromString(s, bom); |
| 366 |
} |
| 367 |
|
| 368 |
void NStreamEncoding::GetCPDisplayName(WORD CP, TCHAR*Buf) |
| 369 |
{ |
| 370 |
TCHAR mybuf[10]; |
| 371 |
const TCHAR *p = mybuf; |
| 372 |
switch(CP) |
| 373 |
{ |
| 374 |
case ACP: p = _T("ACP"); break; |
| 375 |
case OEMCP: p = _T("OEM"); break; |
| 376 |
case UTF16LE: p = _T("UTF16LE"); break; |
| 377 |
case UTF16BE: p = _T("UTF16BE"); break; |
| 378 |
case UTF32LE: p = _T("UTF32LE"); break; |
| 379 |
case UTF32BE: p = _T("UTF32BE"); break; |
| 380 |
case UTF8: p = _T("UTF8"); break; |
| 381 |
case BINARY: p = _T("BIN"); break; |
| 382 |
default: |
| 383 |
_stprintf(mybuf,_T("CP%u"), CP); |
| 384 |
if (CP >= NStreamEncoding::CPCOUNT) p = _T("?"); |
| 385 |
} |
| 386 |
_tcscpy(Buf, p); |
| 387 |
} |
| 388 |
|
| 389 |
bool NBaseStream::Attach(FILE*hFile, WORD enc, bool Seek /*= true*/) |
| 390 |
{ |
| 391 |
Close(); |
| 392 |
m_hFile = hFile; |
| 393 |
if (!m_hFile) return false; |
| 394 |
if (!NStream::SetBinaryMode(m_hFile) && m_hFile != stdin) return false; |
| 395 |
WORD cp = 0; |
| 396 |
if (enc != NStreamEncoding::BINARY) |
| 397 |
{ |
| 398 |
fpos_t pos; |
| 399 |
if (Seek && !fgetpos(m_hFile, &pos)) rewind(m_hFile); else Seek = false; |
| 400 |
cp = DetectUTFBOM(m_hFile); |
| 401 |
if (Seek) |
| 402 |
{ |
| 403 |
fsetpos(m_hFile, &pos); |
| 404 |
if (cp) |
| 405 |
{ |
| 406 |
DetectUTFBOM(m_hFile); // parseScript() etc does not like the BOM, make sure we skip past it |
| 407 |
fseek(m_hFile, 0, SEEK_CUR); // Force intervening call to a file positioning function so writes works for files with BOM opened with "a+" |
| 408 |
} |
| 409 |
} |
| 410 |
} |
| 411 |
if (!cp) cp = enc; |
| 412 |
m_Enc.SafeSetCodepage(cp); |
| 413 |
return true; |
| 414 |
} |
| 415 |
|
| 416 |
bool NOStream::WriteString(const wchar_t*Str, size_t cch /*= -1*/) |
| 417 |
{ |
| 418 |
CharEncConv cec; |
| 419 |
if (!cec.Initialize(m_Enc.GetCodepage(), -1)) return false; |
| 420 |
cec.SetAllowOptimizedReturn(true); |
| 421 |
if ((size_t)-1 != cch) cch *= sizeof(wchar_t); // cec.Convert wants byte count |
| 422 |
size_t cbConv; |
| 423 |
char *p = (char*) cec.Convert(Str, cch, &cbConv); |
| 424 |
return p && WriteOctets(p, cbConv); |
| 425 |
} |
| 426 |
bool NOStream::WritePlatformNLString(const wchar_t*Str, size_t cch /*= -1*/) |
| 427 |
{ |
| 428 |
#ifdef _WIN32 |
| 429 |
size_t cch2 = 0, nlcount = 0; |
| 430 |
for(; cch2 < cch && Str[cch2]; ++cch2) if (L'\n' == Str[cch2]) ++nlcount; |
| 431 |
if (nlcount) |
| 432 |
{ |
| 433 |
cch = cch2 + nlcount; |
| 434 |
wchar_t chPrev = 0, *buf = (wchar_t*) malloc(cch * sizeof(wchar_t)); |
| 435 |
if (!buf) return false; |
| 436 |
for(size_t s = 0, d = 0; d < cch; ++s, ++d) |
| 437 |
{ |
| 438 |
if (L'\n' == Str[s]) |
| 439 |
{ |
| 440 |
if (L'\r' != chPrev) buf[d++] = L'\r'; else --cch; |
| 441 |
} |
| 442 |
buf[d] = chPrev = Str[s]; |
| 443 |
} |
| 444 |
bool retval = WriteString(buf, cch); |
| 445 |
free(buf); |
| 446 |
return retval; |
| 447 |
} |
| 448 |
#endif |
| 449 |
return WriteString(Str, cch); |
| 450 |
} |
| 451 |
|
| 452 |
tstring NStreamLineReader::GetErrorMessage(UINT Error, const TCHAR*Filename, UINT Line) |
| 453 |
{ |
| 454 |
tstring msg; |
| 455 |
TCHAR buf[40]; |
| 456 |
switch(Error) |
| 457 |
{ |
| 458 |
case NStream::ERR_BUFFEROVERFLOW: |
| 459 |
msg = _T("Line too long"); |
| 460 |
break; |
| 461 |
case NStream::ERR_IOERROR: |
| 462 |
msg = _T("I/O error"), Filename = 0; |
| 463 |
break; |
| 464 |
case NStream::ERR_UNSUPPORTEDENCODING: |
| 465 |
StreamEncoding().GetCPDisplayName(buf); |
| 466 |
msg = tstring(buf) + _T(" is not supported"), Filename = 0; |
| 467 |
break; |
| 468 |
default: |
| 469 |
msg = _T("Bad text encoding"); |
| 470 |
break; |
| 471 |
} |
| 472 |
if (Filename) |
| 473 |
{ |
| 474 |
const TCHAR *filelinesep = *Filename ? _T(":") : _T(""); |
| 475 |
_stprintf(buf,_T("%") NPRIs _T("%u"),filelinesep,Line); |
| 476 |
msg = msg + _T(": ") + Filename + buf; |
| 477 |
} |
| 478 |
return msg + _T("\n"); |
| 479 |
} |
| 480 |
|
| 481 |
UINT NStreamLineReader::ReadLine(wchar_t*Buffer, UINT cchBuf) |
| 482 |
{ |
| 483 |
/*\ |
| 484 |
Reads from the associated stream until it finds a new-line or |
| 485 |
the read fails (I/O error or EOF). It fails with ERR_BUFFEROVERFLOW if |
| 486 |
cchBuf-1 wchar_t's are read without finding the end of the line. |
| 487 |
Buffer MUST be a valid pointer, it will be \0 terminated as long as cchBuf > 0. |
| 488 |
\*/ |
| 489 |
if (!cchBuf) return NStream::ERR_BUFFEROVERFLOW; |
| 490 |
#ifndef MB_ERR_INVALID_CHARS |
| 491 |
const UINT MB_ERR_INVALID_CHARS = 8; |
| 492 |
#endif |
| 493 |
const UINT cchFullBuf = cchBuf; |
| 494 |
NIStream&strm = GetStream(); |
| 495 |
#ifndef _WIN32 |
| 496 |
iconvdescriptor iconvd; |
| 497 |
#endif |
| 498 |
|
| 499 |
l_restart: |
| 500 |
if (StreamEncoding().IsUTF8()) |
| 501 |
{ |
| 502 |
for(;;) |
| 503 |
{ |
| 504 |
BYTE cb = 0; // bytes in chU8 -1 |
| 505 |
BYTE chU8[6]; |
| 506 |
if (!strm.ReadOctet(&chU8[0])) goto l_ioerror; |
| 507 |
UINT cchWC; |
| 508 |
#if defined(_WIN32) || defined(__CYGWIN__) // wchar_t==UTF16LE on Cygwin: www.mail-archive.com/[email protected]/msg21543.html |
| 509 |
// Fast path if wchar_t == UTF16 and in ASCII range |
| 510 |
if (chU8[0] <= 127 && sizeof(wchar_t) == 2) |
| 511 |
{ |
| 512 |
cchWC = ++cb; |
| 513 |
if (cchBuf <= cchWC) goto l_lineoverflow; |
| 514 |
*Buffer = (wchar_t) chU8[0]; |
| 515 |
} |
| 516 |
else |
| 517 |
#endif |
| 518 |
{ |
| 519 |
if (!UTF8_GetTrailCount(chU8[0], cb)) goto l_badutf; |
| 520 |
for(BYTE moreU8 = 0; moreU8 < cb;) |
| 521 |
{ |
| 522 |
BYTE b; |
| 523 |
if (!strm.ReadOctet(&b)) goto l_ioerror; |
| 524 |
if (0x80 != (0xC0 & b)) goto l_badutf; // chU8[1..n] must be 0b10xxxxxx |
| 525 |
chU8[++moreU8] = b; |
| 526 |
} |
| 527 |
++cb; |
| 528 |
cchWC = UTF8ToWC_Prepare((LPCSTR)chU8,cb); |
| 529 |
if (!cchWC) goto l_badutf; |
| 530 |
if (cchBuf <= cchWC) goto l_lineoverflow; |
| 531 |
cchWC = UTF8ToWC_Convert((LPCSTR)chU8,cb,Buffer,cchWC); |
| 532 |
} |
| 533 |
if (CompleteLine(Buffer,cchWC,cchBuf,true)) goto l_success; |
| 534 |
} |
| 535 |
} |
| 536 |
else if (StreamEncoding().IsUTF16()) |
| 537 |
{ |
| 538 |
#ifndef _WIN32 |
| 539 |
if (!iconvd.Open("wchar_t", iconvdescriptor::GetHostEndianUCS4Code())) goto l_unsupportedencoding; |
| 540 |
#endif |
| 541 |
const bool utf16be = StreamEncoding().IsUTF16BE(); |
| 542 |
unsigned short lead, trail, cchWC; |
| 543 |
for(;;) |
| 544 |
{ |
| 545 |
if (!strm.ReadInt16(&lead)) goto l_ioerror; |
| 546 |
FIX_ENDIAN_INT16LETOHOST_INPLACE(lead); |
| 547 |
if (utf16be) lead = SWAP_ENDIAN_INT16(lead); |
| 548 |
if (IsTrailSurrogateUTF16(lead)) goto l_badutf; |
| 549 |
UINT32 codpt = lead; |
| 550 |
if (cchBuf <= 1) goto l_lineoverflow; |
| 551 |
Buffer[0] = lead, cchWC = 1; |
| 552 |
if (IsLeadSurrogateUTF16(lead)) |
| 553 |
{ |
| 554 |
if (!strm.ReadInt16(&trail)) goto l_ioerror; |
| 555 |
FIX_ENDIAN_INT16LETOHOST_INPLACE(trail); |
| 556 |
if (utf16be) trail = SWAP_ENDIAN_INT16(trail); |
| 557 |
if (!IsTrailSurrogateUTF16(trail)) goto l_badutf; |
| 558 |
codpt = CodePointFromUTF16SurrogatePair(lead,trail); |
| 559 |
#ifdef _WIN32 |
| 560 |
if (cchBuf <= 2) goto l_lineoverflow; |
| 561 |
Buffer[1] = trail, ++cchWC; |
| 562 |
#endif |
| 563 |
} |
| 564 |
if (!IsValidUnicodeCodePoint(codpt)) goto l_badutf; |
| 565 |
#ifndef _WIN32 |
| 566 |
char tmpdest[8]; // Should be plenty of space to store one UCS4 character as wchar_t(s) |
| 567 |
size_t inleft = 4; |
| 568 |
cchWC = iconvd.Convert(&codpt,&inleft,tmpdest,sizeof(tmpdest)) / sizeof(wchar_t); |
| 569 |
if (!cchWC) goto l_badutf; |
| 570 |
if (cchBuf <= cchWC) goto l_lineoverflow; |
| 571 |
for (UINT i = cchWC; i;) --i, Buffer[i] = ((wchar_t*)tmpdest)[i]; |
| 572 |
#endif |
| 573 |
if (CompleteLine(Buffer,cchWC,cchBuf,true)) goto l_success; |
| 574 |
} |
| 575 |
} |
| 576 |
else if (StreamEncoding().IsUnicode()) |
| 577 |
{ |
| 578 |
goto l_unsupportedencoding; |
| 579 |
} |
| 580 |
else |
| 581 |
{ |
| 582 |
const UINT cp = StreamEncoding().GetCodepage(); |
| 583 |
UINT mbtowcflags = (cp < 50220 && cp != 42) ? MB_ERR_INVALID_CHARS : 0; |
| 584 |
for(;;) |
| 585 |
{ |
| 586 |
BYTE bufMB[2]; |
| 587 |
BYTE mb = 0; |
| 588 |
if (!strm.ReadOctet(&bufMB[0])) goto l_ioerror; |
| 589 |
if (IsDBCSLeadByteEx(cp,bufMB[0])) |
| 590 |
{ |
| 591 |
if (!strm.ReadOctet(&bufMB[++mb])) goto l_ioerror; |
| 592 |
} |
| 593 |
++mb; |
| 594 |
UINT cchWC = MultiByteToWideChar(cp,mbtowcflags,(LPCSTR)bufMB,mb,0,0); |
| 595 |
if (!cchWC) goto l_badencoding; |
| 596 |
if (cchBuf <= cchWC) goto l_lineoverflow; |
| 597 |
cchWC = MultiByteToWideChar(cp,mbtowcflags,(LPCSTR)bufMB,mb,Buffer,cchWC); |
| 598 |
if (CompleteLine(Buffer,cchWC,cchBuf,false)) goto l_success; |
| 599 |
} |
| 600 |
} |
| 601 |
l_ioerror: |
| 602 |
*Buffer = 0; |
| 603 |
return NStream::ERR_IOERROR; |
| 604 |
l_lineoverflow: |
| 605 |
*Buffer = 0; |
| 606 |
return NStream::ERR_BUFFEROVERFLOW; |
| 607 |
l_badutf: |
| 608 |
l_badencoding: |
| 609 |
*Buffer = 0; |
| 610 |
return NStream::ERR_INVALIDENCODING; |
| 611 |
l_unsupportedencoding: |
| 612 |
*Buffer = 0; |
| 613 |
return NStream::ERR_UNSUPPORTEDENCODING; |
| 614 |
l_success: |
| 615 |
*Buffer = 0; |
| 616 |
// "Foo\r\nBar" is 2 and not 3 lines |
| 617 |
const wchar_t chThisNL = *--Buffer, chPrevNL = m_PrevNL; |
| 618 |
const bool onlyNL = ++cchBuf == cchFullBuf; |
| 619 |
m_PrevNL = chThisNL; |
| 620 |
if (onlyNL && (chPrevNL^chThisNL) == ('\r'^'\n')) |
| 621 |
{ |
| 622 |
m_PrevNL = 0; |
| 623 |
goto l_restart; // Previous line was "Foo\r". This line was "\n", ignore it. |
| 624 |
} |
| 625 |
return NStream::OK; |
| 626 |
} |