2013-03-03 20:08:00 +00:00
|
|
|
var util = require('util'),
|
|
|
|
Match = require ('../match');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class matches UTF-16 and UTF-32, both big- and little-endian. The
|
|
|
|
* BOM will be used if it is present.
|
|
|
|
*/
|
|
|
|
module.exports.UTF_16BE = function() {
|
2013-03-04 19:47:01 +00:00
|
|
|
this.name = function() {
|
2013-08-06 01:35:58 +00:00
|
|
|
return 'UTF-16BE';
|
2013-03-04 19:47:01 +00:00
|
|
|
};
|
2013-03-03 20:08:00 +00:00
|
|
|
this.match = function(det) {
|
|
|
|
var input = det.fRawInput;
|
|
|
|
|
2013-08-15 02:31:32 +00:00
|
|
|
if (input.length >= 2 && ((input[0] & 0xFF) == 0xFE && (input[1] & 0xFF) == 0xFF))
|
|
|
|
return new Match(det, this, confidence = 100);
|
2013-03-03 20:08:00 +00:00
|
|
|
|
|
|
|
// TODO: Do some statistics to check for unsigned UTF-16BE
|
|
|
|
return null;
|
2013-08-15 02:31:32 +00:00
|
|
|
};
|
2013-03-03 20:08:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports.UTF_16LE = function() {
|
2013-03-04 19:47:01 +00:00
|
|
|
this.name = function() {
|
2013-08-06 01:35:58 +00:00
|
|
|
return 'UTF-16LE';
|
2013-03-04 19:47:01 +00:00
|
|
|
};
|
2013-03-03 20:08:00 +00:00
|
|
|
this.match = function(det) {
|
|
|
|
var input = det.fRawInput;
|
|
|
|
|
|
|
|
if (input.length >= 2 && ((input[0] & 0xFF) == 0xFF && (input[1] & 0xFF) == 0xFE)) {
|
|
|
|
// An LE BOM is present.
|
2013-08-15 02:31:32 +00:00
|
|
|
if (input.length >= 4 && input[2] == 0x00 && input[3] == 0x00)
|
2013-03-03 20:08:00 +00:00
|
|
|
// It is probably UTF-32 LE, not UTF-16
|
|
|
|
return null;
|
2013-08-15 02:31:32 +00:00
|
|
|
|
|
|
|
return new Match(det, this, confidence = 100);
|
2013-03-03 20:08:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Do some statistics to check for unsigned UTF-16LE
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
function UTF_32() {};
|
|
|
|
UTF_32.prototype.match = function(det) {
|
|
|
|
var input = det.fRawInput,
|
|
|
|
limit = (det.fRawLength / 4) * 4,
|
|
|
|
numValid = 0,
|
|
|
|
numInvalid = 0,
|
|
|
|
hasBOM = false,
|
|
|
|
confidence = 0;
|
|
|
|
|
2013-08-15 02:31:32 +00:00
|
|
|
if (limit == 0)
|
2013-03-03 20:08:00 +00:00
|
|
|
return null;
|
|
|
|
|
2013-08-15 02:31:32 +00:00
|
|
|
if (this.getChar(input, 0) == 0x0000FEFF)
|
2013-03-03 20:08:00 +00:00
|
|
|
hasBOM = true;
|
|
|
|
|
2013-08-15 02:31:32 +00:00
|
|
|
for (var i = 0; i < limit; i += 4) {
|
2013-03-03 20:08:00 +00:00
|
|
|
var ch = this.getChar(input, i);
|
|
|
|
|
2013-08-15 02:31:32 +00:00
|
|
|
if (ch < 0 || ch >= 0x10FFFF || (ch >= 0xD800 && ch <= 0xDFFF))
|
2013-03-03 20:08:00 +00:00
|
|
|
numInvalid += 1;
|
2013-08-15 02:31:32 +00:00
|
|
|
else
|
2013-03-03 20:08:00 +00:00
|
|
|
numValid += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cook up some sort of confidence score, based on presence of a BOM
|
|
|
|
// and the existence of valid and/or invalid multi-byte sequences.
|
2013-08-06 01:35:58 +00:00
|
|
|
if (hasBOM && numInvalid == 0) {
|
2013-03-03 20:08:00 +00:00
|
|
|
confidence = 100;
|
2013-08-06 01:35:58 +00:00
|
|
|
} else if (hasBOM && numValid > numInvalid * 10) {
|
2013-03-03 20:08:00 +00:00
|
|
|
confidence = 80;
|
|
|
|
} else if (numValid > 3 && numInvalid == 0) {
|
|
|
|
confidence = 100;
|
|
|
|
} else if (numValid > 0 && numInvalid == 0) {
|
|
|
|
confidence = 80;
|
2013-08-15 02:31:32 +00:00
|
|
|
} else if (numValid > numInvalid * 10) {
|
2013-03-03 20:08:00 +00:00
|
|
|
// Probably corrupt UTF-32BE data. Valid sequences aren't likely by chance.
|
|
|
|
confidence = 25;
|
|
|
|
}
|
|
|
|
|
|
|
|
// return confidence == 0 ? null : new CharsetMatch(det, this, confidence);
|
|
|
|
return confidence == 0 ? null : new Match(det, this, confidence);
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports.UTF_32BE = function() {
|
2013-03-04 19:47:01 +00:00
|
|
|
this.name = function() {
|
2013-08-06 01:35:58 +00:00
|
|
|
return 'UTF-32BE';
|
2013-03-04 19:47:01 +00:00
|
|
|
};
|
2013-03-03 20:08:00 +00:00
|
|
|
this.getChar = function(input, index) {
|
|
|
|
return (input[index + 0] & 0xFF) << 24 | (input[index + 1] & 0xFF) << 16 |
|
|
|
|
(input[index + 2] & 0xFF) << 8 | (input[index + 3] & 0xFF);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
util.inherits(module.exports.UTF_32BE, UTF_32);
|
|
|
|
|
|
|
|
module.exports.UTF_32LE = function() {
|
2013-03-04 19:47:01 +00:00
|
|
|
this.name = function() {
|
2013-08-06 01:35:58 +00:00
|
|
|
return 'UTF-32LE';
|
2013-03-04 19:47:01 +00:00
|
|
|
};
|
2013-03-03 20:08:00 +00:00
|
|
|
this.getChar = function(input, index) {
|
|
|
|
return (input[index + 3] & 0xFF) << 24 | (input[index + 2] & 0xFF) << 16 |
|
|
|
|
(input[index + 1] & 0xFF) << 8 | (input[index + 0] & 0xFF);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
util.inherits(module.exports.UTF_32LE, UTF_32);
|