| 1 |
// validateunicode.cpp |
| 2 |
// |
| 3 |
// This file is a part of Unicode NSIS. |
| 4 |
// |
| 5 |
// Copyright (C) 2009-2026 Jim Park |
| 6 |
// |
| 7 |
// Licensed under the zlib/libpng license (the "License"); |
| 8 |
// you may not use this file except in compliance with the License. |
| 9 |
// |
| 10 |
// This software is provided 'as-is', without any expressed or implied |
| 11 |
// warranty. |
| 12 |
// |
| 13 |
// This class can be used to check a buffer to see if it has the expected |
| 14 |
// Unicode encoding and look for byte order marks. |
| 15 |
|
| 16 |
#ifdef _UNICODE |
| 17 |
|
| 18 |
#include "validateunicode.h" |
| 19 |
#include <vector> |
| 20 |
|
| 21 |
int CValidateUnicode::ValidateUTF8(unsigned char* buf, size_t characters) |
| 22 |
{ |
| 23 |
bool hasNonAscii = false; |
| 24 |
int bytesToFollow = 0; |
| 25 |
|
| 26 |
for ( ; characters != 0 ; --characters) |
| 27 |
{ |
| 28 |
unsigned char ch = *buf++; |
| 29 |
if (bytesToFollow != 0) // in the middle of a multi-byte sequence? |
| 30 |
{ |
| 31 |
if ((ch & 0xC0) != 0x80) |
| 32 |
return 0; // we expected a continuation byte |
| 33 |
hasNonAscii = true; |
| 34 |
--bytesToFollow; |
| 35 |
} |
| 36 |
else if (ch & 0x80) |
| 37 |
{ |
| 38 |
if ((ch & 0xC0) == 0x80) |
| 39 |
return 0; // continuation byte outside multi-byte sequence |
| 40 |
else if ((ch & 0xE0) == 0xC0) |
| 41 |
bytesToFollow = 1; |
| 42 |
else if ((ch & 0xF0) == 0xE0) |
| 43 |
bytesToFollow = 2; |
| 44 |
else if ((ch & 0xF8) == 0xF0) |
| 45 |
bytesToFollow = 3; |
| 46 |
else |
| 47 |
return 0; // byte is invalid UTF-8 (outside RFC 3629) |
| 48 |
|
| 49 |
} |
| 50 |
else if (ch == 0 && characters != 1) |
| 51 |
return 0; // NUL character in the middle of the buffer |
| 52 |
} |
| 53 |
if (bytesToFollow != 0) |
| 54 |
return 0; // end of buffer in the middle of a multi-byte sequence |
| 55 |
return hasNonAscii ? 2 : 1; |
| 56 |
} |
| 57 |
|
| 58 |
bool CValidateUnicode::ValidateUTF16LE(unsigned char* buf, size_t bytes) |
| 59 |
{ |
| 60 |
// We need to make sure the endianness matches the processor. |
| 61 |
// Intel x86 is little endian. |
| 62 |
return ValidateUTF16((unsigned short*)(buf), bytes/2); |
| 63 |
} |
| 64 |
|
| 65 |
bool CValidateUnicode::ValidateUTF16BE(unsigned char* buf, size_t bytes) |
| 66 |
{ |
| 67 |
std::vector<unsigned short> correctedBuf(bytes/2); |
| 68 |
|
| 69 |
for (size_t i = 0; i < bytes; i += 2) |
| 70 |
{ |
| 71 |
correctedBuf[i/2] = buf[i] << 8 | buf[i+1]; |
| 72 |
} |
| 73 |
|
| 74 |
return ValidateUTF16(&correctedBuf[0], correctedBuf.size()); |
| 75 |
} |
| 76 |
|
| 77 |
bool CValidateUnicode::ValidateUTF16(unsigned short* buf, size_t characters) |
| 78 |
{ |
| 79 |
unsigned short ch; |
| 80 |
bool valid = true; |
| 81 |
|
| 82 |
while (valid && characters > 0) |
| 83 |
{ |
| 84 |
// Last character may be 0. |
| 85 |
if ((ch = *buf) == 0 && characters != 1) |
| 86 |
{ |
| 87 |
valid = false; |
| 88 |
} |
| 89 |
else if (ch >= 0xd800 && ch <= 0xdbff) |
| 90 |
{ |
| 91 |
unsigned short trailing = *(++buf); |
| 92 |
--characters; |
| 93 |
// Unpaired leading surrogate found? |
| 94 |
if (trailing < 0xdc00 || trailing > 0xdfff) |
| 95 |
{ |
| 96 |
valid = false; |
| 97 |
} |
| 98 |
// Invalid surrogate pairs found? |
| 99 |
else if ((ch == 0xd83f || |
| 100 |
ch == 0xd87f || |
| 101 |
ch == 0xd8bf || |
| 102 |
ch == 0xd8ff || |
| 103 |
ch == 0xd93f || |
| 104 |
ch == 0xd97f || |
| 105 |
ch == 0xd9bf || |
| 106 |
ch == 0xd9ff || |
| 107 |
ch == 0xda3f || |
| 108 |
ch == 0xdA7f || |
| 109 |
ch == 0xdabf || |
| 110 |
ch == 0xdaff || |
| 111 |
ch == 0xdb3f || |
| 112 |
ch == 0xdb7f || |
| 113 |
ch == 0xdbbf || |
| 114 |
ch == 0xdbff) |
| 115 |
&& |
| 116 |
(trailing == 0xdffe || trailing == 0xdfff)) |
| 117 |
{ |
| 118 |
valid = false; |
| 119 |
} |
| 120 |
} |
| 121 |
// Unpaired trailing surrogate! |
| 122 |
else if (ch >= 0xdc00 && ch <= 0xdfff) |
| 123 |
{ |
| 124 |
valid = false; |
| 125 |
} |
| 126 |
// Invalid values |
| 127 |
else if (ch == 0xfffe || ch == 0xffff || |
| 128 |
(ch >= 0xfdd0 && ch <= 0xfdef)) |
| 129 |
{ |
| 130 |
valid = false; |
| 131 |
} |
| 132 |
|
| 133 |
++buf; |
| 134 |
--characters; |
| 135 |
} |
| 136 |
|
| 137 |
return valid; |
| 138 |
} |
| 139 |
|
| 140 |
CValidateUnicode::FILE_TYPE CValidateUnicode::CheckBOM( |
| 141 |
unsigned char* buf, |
| 142 |
size_t bytes) |
| 143 |
{ |
| 144 |
FILE_TYPE result = UNKNOWN; |
| 145 |
|
| 146 |
if (bytes >= 2) |
| 147 |
{ |
| 148 |
if (buf[0] == 0xff && buf[1] == 0xfe) |
| 149 |
{ |
| 150 |
result = UTF_16LE; |
| 151 |
} |
| 152 |
else if (buf[0] == 0xfe && buf[1] == 0xff) |
| 153 |
{ |
| 154 |
result = UTF_16BE; |
| 155 |
} |
| 156 |
else if (bytes >= 3 && |
| 157 |
buf[0] == 0xef && |
| 158 |
buf[1] == 0xbb && |
| 159 |
buf[2] == 0xbf) |
| 160 |
{ |
| 161 |
result = UTF_8; |
| 162 |
} |
| 163 |
else if (bytes >= 4) |
| 164 |
{ |
| 165 |
if (buf[0] == 0 && |
| 166 |
buf[1] == 0 && |
| 167 |
buf[2] == 0xfe && |
| 168 |
buf[3] == 0xff) |
| 169 |
{ |
| 170 |
result = UTF_32BE; |
| 171 |
} |
| 172 |
else if (buf[0] == 0xff && |
| 173 |
buf[1] == 0xfe && |
| 174 |
buf[2] == 0 && |
| 175 |
buf[3] == 0) |
| 176 |
{ |
| 177 |
result = UTF_32LE; |
| 178 |
} |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
return result; |
| 183 |
} |
| 184 |
|
| 185 |
const TCHAR* CValidateUnicode::TypeToName(CValidateUnicode::FILE_TYPE ftype) |
| 186 |
{ |
| 187 |
static const TCHAR* names[] = |
| 188 |
{ |
| 189 |
_T("UTF-8"), |
| 190 |
_T("UTF-16LE"), |
| 191 |
_T("UTF-16BE"), |
| 192 |
_T("UTF-32LE"), |
| 193 |
_T("UTF-32BE"), |
| 194 |
_T("UNKNOWN") |
| 195 |
}; |
| 196 |
|
| 197 |
return names[ftype]; |
| 198 |
} |
| 199 |
|
| 200 |
#endif |