2013-03-04 19:47:01 +00:00
|
|
|
|
2013-04-29 14:31:48 +00:00
|
|
|
var fs = require('fs');
|
2013-03-04 19:47:01 +00:00
|
|
|
|
2013-05-01 14:57:54 +00:00
|
|
|
var utf8 = require('./encoding/utf8'),
|
|
|
|
unicode = require('./encoding/unicode'),
|
|
|
|
mbcs = require('./encoding/mbcs'),
|
|
|
|
sbcs = require('./encoding/sbcs'),
|
|
|
|
iso2022 = require('./encoding/iso2022');
|
2013-03-04 19:47:01 +00:00
|
|
|
|
2013-04-29 14:31:48 +00:00
|
|
|
var self = this;
|
|
|
|
|
2013-03-04 19:47:01 +00:00
|
|
|
var recognisers = [
|
2013-05-01 14:57:54 +00:00
|
|
|
new utf8,
|
2013-03-04 19:47:01 +00:00
|
|
|
new unicode.UTF_16BE,
|
|
|
|
new unicode.UTF_16LE,
|
|
|
|
new unicode.UTF_32BE,
|
|
|
|
new unicode.UTF_32LE,
|
|
|
|
new mbcs.sjis,
|
|
|
|
new mbcs.big5,
|
|
|
|
new mbcs.euc_jp,
|
|
|
|
new mbcs.euc_kr,
|
|
|
|
new mbcs.gb_18030,
|
|
|
|
new iso2022.ISO_2022_JP,
|
|
|
|
new iso2022.ISO_2022_KR,
|
2013-05-01 14:57:54 +00:00
|
|
|
new iso2022.ISO_2022_CN,
|
|
|
|
new sbcs.ISO_8859_1,
|
|
|
|
new sbcs.ISO_8859_2,
|
|
|
|
new sbcs.ISO_8859_5,
|
|
|
|
new sbcs.ISO_8859_6,
|
|
|
|
new sbcs.ISO_8859_7,
|
2013-05-02 08:31:57 +00:00
|
|
|
new sbcs.ISO_8859_8,
|
2013-05-04 09:27:28 +00:00
|
|
|
new sbcs.ISO_8859_9,
|
|
|
|
new sbcs.windows_1251,
|
|
|
|
new sbcs.windows_1256,
|
|
|
|
new sbcs.KOI8_R
|
2013-03-04 19:47:01 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
module.exports.detect = function(buffer) {
|
|
|
|
|
2013-05-04 09:27:28 +00:00
|
|
|
// Tally up the byte occurence statistics.
|
|
|
|
var fByteStats = [];
|
|
|
|
for (var i = 0; i < 256; i++)
|
|
|
|
fByteStats[i] = 0;
|
|
|
|
|
|
|
|
for (var i = buffer.length - 1; i >= 0; i--)
|
|
|
|
fByteStats[buffer[i] & 0x00ff]++;
|
|
|
|
|
|
|
|
var fC1Bytes = false;
|
|
|
|
for (var i = 0x80; i <= 0x9F; i += 1) {
|
|
|
|
if (fByteStats[i] != 0) {
|
|
|
|
fC1Bytes = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-29 14:31:48 +00:00
|
|
|
var context = {
|
2013-05-04 09:27:28 +00:00
|
|
|
fByteStats: fByteStats,
|
|
|
|
fC1Bytes: fC1Bytes,
|
2013-03-04 19:47:01 +00:00
|
|
|
fRawInput: buffer,
|
|
|
|
fRawLength: buffer.length,
|
|
|
|
fInputBytes: buffer,
|
|
|
|
fInputLen: buffer.length
|
|
|
|
};
|
|
|
|
|
2013-08-06 01:35:58 +00:00
|
|
|
var match = recognisers.map(function(rec) {
|
2013-04-29 14:31:48 +00:00
|
|
|
return rec.match(context);
|
|
|
|
}).filter(function(match) {
|
|
|
|
return !!match;
|
2013-08-06 01:35:58 +00:00
|
|
|
}).sort(function(a, b) {
|
2013-03-04 19:47:01 +00:00
|
|
|
return a.confidence - b.confidence;
|
2013-08-06 01:35:58 +00:00
|
|
|
}).pop();
|
2013-03-04 19:47:01 +00:00
|
|
|
|
2013-08-06 01:35:58 +00:00
|
|
|
return match ? match.name : null;
|
2013-04-29 14:31:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports.detectFile = function(filepath, fn) {
|
|
|
|
fs.readFile(filepath, function(err, res) {
|
2013-08-06 01:35:58 +00:00
|
|
|
if (err) return fn(err, null);
|
2013-04-29 14:31:48 +00:00
|
|
|
fn(null, self.detect(res));
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports.detectFileSync = function(filepath) {
|
|
|
|
return self.detect(fs.readFileSync(filepath));
|
2013-08-06 01:35:58 +00:00
|
|
|
};
|