var GlobalObject = this;
var FSO = fso = new ActiveXObject("Scripting.FileSystemObject");
var WshShell = new ActiveXObject("WScript.Shell");
Function.prototype.GetResource = function(ResourceName) {
if (!this.Resources) {
var UnNamedResourceIndex = 0,
_this = this;
this.Resources = {};
function f(match, resType, Content) {
_this.Resources[(resType == "[[") ? UnNamedResourceIndex++:resType.slice(1, -1)] = Content;
}
this.toString().replace(/\/\*(\[(?:[^\[]+)?\[)((?:[\r\n]|.)*?)\]\]\*\//gi, f);
}
return this.Resources[ResourceName];
}
Array.prototype.forEach = function (action, that /*opt*/ ) {
for (var i = 0, n = this.length; i < n; i++)
//if (i in this)
action.call(that, this[i], i, this);
};
var VBSEngine = new ActiveXObject('ScriptControl');
VBSEngine.Language = 'VBScript';
VBSEngine.AddCode(Resourses.GetResource("vbs"));
function Resourses() {
/*[vbs[
Function [_MidB](v, s, l):[_MidB]=MidB(v, s+1, l):End Function
Function [_Asc](v):[_Asc]=Asc(v):End Function
Function [_Chr](v):[_Chr]=Chr(v):End Function
Function [_LenB](v):[_LenB]=LenB(v):End Function
Function [_VarType](v):[_VarType]=VarType(v):End Function
Function [_TypeName](v):[_TypeName]=TypeName(v):End Function
Function [_ChrB](v):[_ChrB]=ChrB(v):End Function
Function [_AscB](v):[_AscB]=AscB(v):End Function
Function [_LeftB](v, l):[_LeftB]=LeftB(v, l):End Function
Function [_RightB](v, l):[_RightB]=RightB(v, l):End Function
Function [_InstrB](start, stringtosearch, stringtofind, comparemode):[_InstrB]=InstrB(start, stringtosearch, stringtofind, comparemode):End Function
Sub ConvertBinaryToJSArray(arrByteArray, js_arr)
Dim i
For i = 1 To LenB(arrByteArray)
js_arr.push(AscB(MidB(arrByteArray, i, 1)))
Next
End Sub
Function MultiByteToBinary(MultiByte)
' 2000 Antonin Foller, http://www.motobit.com
' MultiByteToBinary converts multibyte string To real binary data (VT_UI1 | VT_ARRAY)
' Using recordset
Dim RS, LMultiByte, Binary
Const adLongVarBinary = 205
Set RS = CreateObject("ADODB.Recordset")
LMultiByte = LenB(MultiByte)
If LMultiByte>0 Then
RS.Fields.Append "mBinary", adLongVarBinary, LMultiByte
RS.Open
RS.AddNew
RS("mBinary").AppendChunk MultiByte & ChrB(0)
RS.Update
Binary = RS("mBinary").GetChunk(LMultiByte)
End If
MultiByteToBinary = Binary
End Function
Function RSBinaryToString(xBinary)
'1999 Antonin Foller, Motobit Software
'This function converts binary data (VT_UI1 | VT_ARRAY or MultiByte string)
'to string (BSTR) using ADO recordset
'The fastest way - requires ADODB.Recordset
'Use this function instead of MBBinaryToString if you have ADODB.Recordset installed
'to eliminate problem with PureASP performance
Dim Binary
'MultiByte data must be converted to VT_UI1 | VT_ARRAY first.
if vartype(xBinary) = 8 then Binary = MultiByteToBinary(xBinary) else Binary = xBinary
Dim RS, LBinary
Const adLongVarChar = 201
Set RS = CreateObject("ADODB.Recordset")
LBinary = LenB(Binary)
if LBinary>0 then
RS.Fields.Append "mBinary", adLongVarChar, LBinary
RS.Open
RS.AddNew
RS("mBinary").AppendChunk Binary
RS.Update
RSBinaryToString = RS("mBinary")
Else
RSBinaryToString = ""
End If
End Function
]]*/
}
WshShell.CurrentDirectory = GetScriptDir();;
function BinaryMid(s, l, c) {
return VBSEngine.CodeObject._MidB(s, l, c);
}
function BinaryLen(s) {
return VBSEngine.CodeObject._LenB(s);
}
function ArrayToBinary(ar) {
var ms = new ActiveXObject("SAPI.spMemoryStream");
for (var i = 0, l = ar.length; i < l; i++) {
ms.Write(ar[i]);
ms.Seek(i + 1);
}
s = ms.GetData();
var l = BinaryLen(s);
s = BinaryMid(s, 0, l - 3)
return MultiByteToBinary(s);
}
function Binary(s) {
if (!s) s = 0;
var value;
var type = TypeOf(s);
if (type == "array") value = ArrayToBinary(s);
if (type == "bytearray") value = s;
if (type == "function" || type == "object" || type == "empty") return null;
try {
var ms = new ActiveXObject("SAPI.spMemoryStream");
ms.SetData(s.valueOf());
value = ms.GetData();
} catch(e) {}
if (arguments.callee != this.constructor) return value;
this.value = value;
}
function MultiByteToBinary(x) {
return VBSEngine.CodeObject.MultiByteToBinary(x);
}
function BinaryToHex(binary) {
var xml = new ActiveXObject('Microsoft.XMLDOM').createElement('Binary');
xml.dataType = 'bin.hex';
xml.nodeTypedValue = binary;
return xml.text.toUpperCase();
}
function HexToBinary(text)
{
var xml = new ActiveXObject('Microsoft.XMLDOM').createElement('Binary');
xml.dataType = 'bin.hex';
xml.text = text;
return xml.nodeTypedValue;
}
function StringToBinary(str, dstCharset) { ! dstCharset ? dstCharset = "UTF-8" : 0;
var stream = new ActiveXObject("ADODB.Stream");
stream.type = 2;
stream.open();
stream.charset = dstCharset;
stream.writeText(str);
stream.position = 0;
stream.type = 1;
var result = stream.read();
stream.close();
return result;
}
//BinaryToString Converts a binary variant into a string.
function BinaryToString(binary, destCharset) {
!destCharset ? destCharset = "UTF-8" : 0;
var stream = new ActiveXObject("ADODB.Stream");
stream.type = 1;
stream.open();
stream.write(binary);
stream.position = 0;
stream.type = 2;
stream.charset = destCharset;
var result = stream.readText();
stream.close();
return result;
}
function Asc(str) {
return VBSEngine.CodeObject._Asc(str);
}
function TypeOf(o) {
var type = typeof o;
if (type != "object" && type != "unknown") return type;
if (o === null || o === undefined) return "empty";
if (Object.prototype.toString.call(o) === '[object Array]') return "array";
if (Object.prototype.toString.call(o) === '[object Number]') return "number";
if (Object.prototype.toString.call(o) === '[object String]') return "string";
if (VBSEngine.CodeObject._VarType(o) == (8192 + 17)) return "bytearray";
return type;
}
function Exit() {
if (this.WScript) WScript.Quit();
if (this.window) window.close();
}
function MsgBox(strText, strTitle, nType) {
return WshShell.Popup(strText, 0, strTitle, nType);
}
function GetScriptDir() {
return GetScriptFullPath().replace(/[^\\]+$/g, "");
}
function GetScriptFullPath() {
if (this.location && this.location.href) return location.href.replace(/^.+\/\/\//, "").replace(/\//g, "\\");
if (this.WScript) return WScript.ScriptFullName;
return "";
}
function FileGetEncoding(path) {
var f = FSO.OpenTextFile(WshShell.ExpandEnvironmentStrings(path), 1, 0, 0);
var n = FSO.GetFile(path).Size;
n = n <= 60 ? n : 60;
var val = MultiByteToBinary(f.Read(n));
f.Close();
return EncodingDetect(val);
}
function EncodingDetect(bin) {
var head = BinaryToHex(bin);
if (/^EFBBBF/.test(head)) return "UTF-8";
if (/^0000FEFF/.test(head)) return "UTF-32BE";
if (/^FFFE0000/.test(head)) return "UTF-32LE";
if (/^2B2F76(38|39|2B|2F)/.test(head)) return "UTF-7";
if (/^F7644C/.test(head)) return "ISO-10646-UTF-1";
if (/^DD736673/.test(head)) return "UTF-EBCDIC";
if (/^0EFEFF/.test(head)) return "SCSU";
if (/^FBEE28/.test(head)) return "BOCU-1";
if (/^84319533/.test(head)) return "GB-18030";
if (/^FEFF0000/.test(head)) return "ISO-10646-UCS-4";
if (/^0000FFFE/.test(head)) return "ISO-10646-UCS-4";
if (/^FEFF/.test(head)) return "UTF-16BE";
if (/^FFFE/.test(head)) return "UTF-16LE";
var o = {
"UTF-32": 0,
"UTF-16": 0,
"UTF-8": 0,
"ASCII": 0
},
re = /[-A-Za-z0-9А-Яа-я\?!=<>"\\\/\s\r\n]/,
max = 0,
maxP = "ASCII";
function _tmp(p) {
try {
BinaryToString(bin, p).split("").forEach(function(s) {
if (Asc(s) == 0) o[p] -= 0.1;
if (re.test(s)) o[p]++
});
if (max <= o[p]) {
maxP = p;
max = o[p]
}
} catch(e) {}
}
for (i in o) _tmp(i);
//MsgBox([o["UTF-32"], o["UTF-16"], o["UTF-8"], o["ASCII"]]);
return maxP;
}
//*
MsgBox(EncodingDetect(Binary("привет")));
MsgBox(EncodingDetect(StringToBinary("привет", "UTF-8")));
MsgBox(EncodingDetect(StringToBinary("привет", "ascii")));
MsgBox(EncodingDetect(Binary("hello")));
MsgBox(EncodingDetect(StringToBinary("hello", "UTF-8")));
MsgBox(EncodingDetect(StringToBinary("hello", "ascii")));
MsgBox(FileGetEncoding("12345.txt"));
Exit();