@@ -1035,7 +1035,7 @@ static constexpr bool IsC0ControlChar(uint8_t ch) noexcept {
1035
1035
#endif
1036
1036
}
1037
1037
1038
- bool MaybeBinaryFile (const uint8_t *ptr, DWORD length) noexcept {
1038
+ bool MaybeBinaryFile (const uint8_t *ptr, DWORD length, int *encodingFlag ) noexcept {
1039
1039
/* Test C0 Control Character
1040
1040
These characters are not reused in most text encodings, and do not appear in normal text files.
1041
1041
Most binary files have reserved fields (mostly zeros) or small values in the header.
@@ -1046,17 +1046,25 @@ bool MaybeBinaryFile(const uint8_t *ptr, DWORD length) noexcept {
1046
1046
const uint8_t * const end = ptr + length;
1047
1047
UINT count = 0 ;
1048
1048
UINT mask = 0 ; // find two different C0 control characters
1049
+ int result = 0 ;
1049
1050
while (ptr < end) {
1050
1051
const uint8_t ch = *ptr++;
1051
1052
if (IsC0ControlChar (ch)) {
1052
1053
++count;
1053
1054
mask |= 1U << ch;
1054
- if (((mask & (mask - 1 )) != 0 ) && ((count >= 8 ) || IsC0ControlChar (*ptr))) {
1055
- return true ;
1055
+ if ((mask & (mask - 1 )) != 0 ) {
1056
+ result |= 2 ;
1057
+ }
1058
+ if ((count >= 8 ) || IsC0ControlChar (*ptr)) {
1059
+ result |= 1 ;
1060
+ }
1061
+ if (result == 3 ) {
1062
+ *encodingFlag = EncodingFlag_Binary;
1063
+ break ;
1056
1064
}
1057
1065
}
1058
1066
}
1059
- return false ;
1067
+ return result & true ;
1060
1068
}
1061
1069
1062
1070
static inline BOOL IsValidMultiByte (UINT codePage, const char *lpData, DWORD cbData) noexcept {
@@ -2439,9 +2447,8 @@ int EditDetermineEncoding(LPCWSTR pszFile, char *lpData, DWORD cbData, int *enco
2439
2447
*encodingFlag = EncodingFlag_Invalid;
2440
2448
}
2441
2449
// detect binary file
2442
- if (MaybeBinaryFile (reinterpret_cast <const uint8_t *>(lpData), cbData)) {
2450
+ if (MaybeBinaryFile (reinterpret_cast <const uint8_t *>(lpData), cbData, encodingFlag )) {
2443
2451
tryUnicode = true ;
2444
- *encodingFlag = EncodingFlag_Binary;
2445
2452
}
2446
2453
// check UTF-16 without BOM
2447
2454
if (tryUnicode && (cbData & 1 ) == 0 && fvCurFile.mask == 0 ) {
0 commit comments