public static bool IsFileUnicode(string file) { byte[] Bytes = File.ReadAllBytes(file); IsTextUnicodeFlags Result = IsTextUnicodeFlags.IS_TEXT_UNICODE_NOT_UNICODE_MASK; if (IsTextUnicode(Bytes, Bytes.Length, ref Result)) { return(false); } return(true); }
public static extern bool IsTextUnicode( byte[] buf, int len, ref IsTextUnicodeFlags opt );
//LEA 지원은 일단 버림. private void AddList(ulong dwStr2, string Address, string ASM) { if (MenuItem자동감지.Checked) { byte[] bString = PE.ANSIBinaryCopy(dwStr2); byte[] bString2 = PE.UnicodeBinaryCopy(dwStr2); string szString = null; if (CodePage.IsAscii(bString)) { szString = Encoding.Default.GetString(bString); } else if (CodePage.isCP949_asc(bString)) { szString = Encoding.GetEncoding(949).GetString(bString); } else if (CodePage.isCP936_asc(bString)) { szString = Encoding.GetEncoding(936).GetString(bString); } IsTextUnicodeFlags tmpU = IsTextUnicodeFlags.IS_TEXT_UNICODE_STATISTICS | IsTextUnicodeFlags.IS_TEXT_UNICODE_ASCII16 | IsTextUnicodeFlags.IS_TEXT_UNICODE_CONTROLS; //높은 확률로 (바이트 배열로 10바이트 정도만 넘어가면 거의 100%) 유니코드면 여기 걸리나 //안걸리수도 있다. if (SafeNativeMethods.IsTextUnicode(bString2, bString2.Length, ref tmpU)) { LAddr.Add(Address); LDis.Add(ASM); LStrA.Add(Encoding.Default.GetString(bString)); LStrU.Add(Encoding.Unicode.GetString(bString2)); } else //유니코드가 아닐 가능성이 매우 높을때? { //if (bString.Length <= 1) return; if (szString == null && bString.Length > 9) { return; } if (CodePage.IsPrintAbleStr(bString) == false) { return; } LAddr.Add(Address); LDis.Add(ASM); LStrA.Add(szString); LStrU.Add(""); } } else if (MenuItem영어.Checked) { byte[] bString = PE.ANSIBinaryCopy(dwStr2); string szString = Encoding.Default.GetString(bString); if (CodePage.IsAscii(bString) == false) { return; } if (bString.Length == 1) { return; } LAddr.Add(Address); LDis.Add(ASM); LStrA.Add(szString); LStrU.Add(""); } else if (MenuItem유니코드.Checked) { byte[] bString2 = PE.UnicodeBinaryCopy(dwStr2); LAddr.Add(Address); LDis.Add(ASM); LStrA.Add(""); LStrU.Add(Encoding.Unicode.GetString(bString2)); } else { byte[] bString = PE.ANSIBinaryCopy(dwStr2); string szString = Encoding.GetEncoding(LngNum).GetString(bString); if (bString.Length == 1) { return; } if (CodePage.IsPrintAbleStr(bString) == false) { return; } LAddr.Add(Address); LDis.Add(ASM); LStrA.Add(szString); LStrU.Add(""); } }
public static extern bool IsTextUnicode( [MarshalAs(UnmanagedType.LPArray)] byte[] buffer, int cb, ref IsTextUnicodeFlags flags);
/// <summary> /// Detects the encoding of the input /// </summary> private void DetectEncoding() { Encoding encoding = null; // try to find the encoding from byte order marks if (_bytesLength - _bytesPointer >= 2) { if (_bytes[_bytesPointer + 0] == 0xff && _bytes[_bytesPointer + 1] == 0xfe) { encoding = Encoding.Unicode; //UTF-16LE } if (_bytes[_bytesPointer + 0] == 0xfe && _bytes[_bytesPointer + 1] == 0xff) { encoding = Encoding.BigEndianUnicode; //UTF-16BE } if (encoding != null) { _bytesPointer += 2; } } if (_bytesLength - _bytesPointer >= 3) { if (_bytes[_bytesPointer + 0] == 0x2b && _bytes[_bytesPointer + 1] == 0x2f && _bytes[_bytesPointer + 2] == 0x76) { encoding = Encoding.UTF7; } if (_bytes[_bytesPointer + 0] == 0xef && _bytes[_bytesPointer + 1] == 0xbb && _bytes[_bytesPointer + 2] == 0xbf) { encoding = Encoding.UTF8; } if (encoding != null) { _bytesPointer += 3; } } if (_bytesLength - _bytesPointer >= 4) { if (_bytes[_bytesPointer + 0] == 0x00 && _bytes[_bytesPointer + 1] == 0x00 && _bytes[_bytesPointer + 2] == 0xfe && _bytes[_bytesPointer + 3] == 0xff) { encoding = Encoding.UTF32; } if (encoding != null) { _bytesPointer += 4; } } // no definitive encoding given, try some heuristics on the data in the buffer if (encoding == null) { if (_bytesPointer > 0) { Array.Copy(_bytes, _bytesPointer, _bytes, 0, _bytesLength - _bytesPointer); _bytesLength -= _bytesPointer; _bytesPointer = 0; } IsTextUnicodeFlags output = IsTextUnicodeFlags.IS_TEXT_UNICODE_UNICODE_MASK; if (IsTextUnicode(_bytes, _bytesLength, ref output)) { Trace.WriteLine($"AutoDetected text: {output}"); } } // nope, just use the default encoding if (encoding == null) { encoding = Encoding.Default; } _encoding = encoding; _decoder = encoding.GetDecoder(); }
private static bool IsTextUnicode(IntPtr buffer, int byteCount, ref IsTextUnicodeFlags flags) { flags = (IsTextUnicodeFlags)0; return false; }
private static extern bool IsTextUnicode(IntPtr buffer, int byteCount, ref IsTextUnicodeFlags flags);