major style changes
This commit is contained in:
parent
c429ac9026
commit
e5ba182d7f
|
@ -1,5 +1,5 @@
|
||||||
var util = require('util'),
|
var util = require('util'),
|
||||||
Match = require ('../match');
|
Match = require ('../match');
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -12,130 +12,130 @@ function ISO_2022() {}
|
||||||
|
|
||||||
ISO_2022.prototype.match = function(det) {
|
ISO_2022.prototype.match = function(det) {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Matching function shared among the 2022 detectors JP, CN and KR
|
* Matching function shared among the 2022 detectors JP, CN and KR
|
||||||
* Counts up the number of legal an unrecognized escape sequences in
|
* Counts up the number of legal an unrecognized escape sequences in
|
||||||
* the sample of text, and computes a score based on the total number &
|
* the sample of text, and computes a score based on the total number &
|
||||||
* the proportion that fit the encoding.
|
* the proportion that fit the encoding.
|
||||||
*
|
*
|
||||||
*
|
*
|
||||||
* @param text the byte buffer containing text to analyse
|
* @param text the byte buffer containing text to analyse
|
||||||
* @param textLen the size of the text in the byte.
|
* @param textLen the size of the text in the byte.
|
||||||
* @param escapeSequences the byte escape sequences to test for.
|
* @param escapeSequences the byte escape sequences to test for.
|
||||||
* @return match quality, in the range of 0-100.
|
* @return match quality, in the range of 0-100.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
var i, j;
|
var i, j;
|
||||||
var escN;
|
var escN;
|
||||||
var hits = 0;
|
var hits = 0;
|
||||||
var misses = 0;
|
var misses = 0;
|
||||||
var shifts = 0;
|
var shifts = 0;
|
||||||
var quality;
|
var quality;
|
||||||
|
|
||||||
// TODO: refactor me
|
// TODO: refactor me
|
||||||
var text = det.fInputBytes;
|
var text = det.fInputBytes;
|
||||||
var textLen = det.fInputLen;
|
var textLen = det.fInputLen;
|
||||||
|
|
||||||
scanInput:
|
scanInput:
|
||||||
for (i = 0; i < textLen; i++) {
|
for (i = 0; i < textLen; i++) {
|
||||||
if (text[i] == 0x1b) {
|
if (text[i] == 0x1b) {
|
||||||
checkEscapes:
|
checkEscapes:
|
||||||
for (escN = 0; escN < this.escapeSequences.length; escN++) {
|
for (escN = 0; escN < this.escapeSequences.length; escN++) {
|
||||||
var seq = this.escapeSequences[escN];
|
var seq = this.escapeSequences[escN];
|
||||||
|
|
||||||
if ((textLen - i) < seq.length)
|
if ((textLen - i) < seq.length)
|
||||||
continue checkEscapes;
|
continue checkEscapes;
|
||||||
|
|
||||||
for (j = 1; j < seq.length; j++)
|
for (j = 1; j < seq.length; j++)
|
||||||
if (seq[j] != text[i + j])
|
if (seq[j] != text[i + j])
|
||||||
continue checkEscapes;
|
continue checkEscapes;
|
||||||
|
|
||||||
|
|
||||||
hits++;
|
hits++;
|
||||||
i += seq.length - 1;
|
i += seq.length - 1;
|
||||||
continue scanInput;
|
continue scanInput;
|
||||||
}
|
}
|
||||||
|
|
||||||
misses++;
|
misses++;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Shift in/out
|
// Shift in/out
|
||||||
if (text[i] == 0x0e || text[i] == 0x0f)
|
if (text[i] == 0x0e || text[i] == 0x0f)
|
||||||
shifts++;
|
shifts++;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (hits == 0)
|
if (hits == 0)
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
//
|
//
|
||||||
// Initial quality is based on relative proportion of recongized vs.
|
// Initial quality is based on relative proportion of recongized vs.
|
||||||
// unrecognized escape sequences.
|
// unrecognized escape sequences.
|
||||||
// All good: quality = 100;
|
// All good: quality = 100;
|
||||||
// half or less good: quality = 0;
|
// half or less good: quality = 0;
|
||||||
// linear inbetween.
|
// linear inbetween.
|
||||||
quality = (100 * hits - 100 * misses) / (hits + misses);
|
quality = (100 * hits - 100 * misses) / (hits + misses);
|
||||||
|
|
||||||
// Back off quality if there were too few escape sequences seen.
|
// Back off quality if there were too few escape sequences seen.
|
||||||
// Include shifts in this computation, so that KR does not get penalized
|
// Include shifts in this computation, so that KR does not get penalized
|
||||||
// for having only a single Escape sequence, but many shifts.
|
// for having only a single Escape sequence, but many shifts.
|
||||||
if (hits + shifts < 5)
|
if (hits + shifts < 5)
|
||||||
quality -= (5 - (hits + shifts)) * 10;
|
quality -= (5 - (hits + shifts)) * 10;
|
||||||
|
|
||||||
return quality <= 0 ? null : new Match(det, this, quality);
|
return quality <= 0 ? null : new Match(det, this, quality);
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports.ISO_2022_JP = function() {
|
module.exports.ISO_2022_JP = function() {
|
||||||
this.name = function() {
|
this.name = function() {
|
||||||
return 'ISO-2022-JP';
|
return 'ISO-2022-JP';
|
||||||
};
|
};
|
||||||
this.escapeSequences = [
|
this.escapeSequences = [
|
||||||
[ 0x1b, 0x24, 0x28, 0x43 ], // KS X 1001:1992
|
[ 0x1b, 0x24, 0x28, 0x43 ], // KS X 1001:1992
|
||||||
[ 0x1b, 0x24, 0x28, 0x44 ], // JIS X 212-1990
|
[ 0x1b, 0x24, 0x28, 0x44 ], // JIS X 212-1990
|
||||||
[ 0x1b, 0x24, 0x40 ], // JIS C 6226-1978
|
[ 0x1b, 0x24, 0x40 ], // JIS C 6226-1978
|
||||||
[ 0x1b, 0x24, 0x41 ], // GB 2312-80
|
[ 0x1b, 0x24, 0x41 ], // GB 2312-80
|
||||||
[ 0x1b, 0x24, 0x42 ], // JIS X 208-1983
|
[ 0x1b, 0x24, 0x42 ], // JIS X 208-1983
|
||||||
[ 0x1b, 0x26, 0x40 ], // JIS X 208 1990, 1997
|
[ 0x1b, 0x26, 0x40 ], // JIS X 208 1990, 1997
|
||||||
[ 0x1b, 0x28, 0x42 ], // ASCII
|
[ 0x1b, 0x28, 0x42 ], // ASCII
|
||||||
[ 0x1b, 0x28, 0x48 ], // JIS-Roman
|
[ 0x1b, 0x28, 0x48 ], // JIS-Roman
|
||||||
[ 0x1b, 0x28, 0x49 ], // Half-width katakana
|
[ 0x1b, 0x28, 0x49 ], // Half-width katakana
|
||||||
[ 0x1b, 0x28, 0x4a ], // JIS-Roman
|
[ 0x1b, 0x28, 0x4a ], // JIS-Roman
|
||||||
[ 0x1b, 0x2e, 0x41 ], // ISO 8859-1
|
[ 0x1b, 0x2e, 0x41 ], // ISO 8859-1
|
||||||
[ 0x1b, 0x2e, 0x46 ] // ISO 8859-7
|
[ 0x1b, 0x2e, 0x46 ] // ISO 8859-7
|
||||||
];
|
];
|
||||||
};
|
};
|
||||||
util.inherits(module.exports.ISO_2022_JP, ISO_2022);
|
util.inherits(module.exports.ISO_2022_JP, ISO_2022);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
module.exports.ISO_2022_KR = function() {
|
module.exports.ISO_2022_KR = function() {
|
||||||
this.name = function() {
|
this.name = function() {
|
||||||
return 'ISO-2022-KR';
|
return 'ISO-2022-KR';
|
||||||
};
|
};
|
||||||
this.escapeSequences = [
|
this.escapeSequences = [
|
||||||
[ 0x1b, 0x24, 0x29, 0x43 ]
|
[ 0x1b, 0x24, 0x29, 0x43 ]
|
||||||
];
|
];
|
||||||
};
|
};
|
||||||
util.inherits(module.exports.ISO_2022_KR, ISO_2022);
|
util.inherits(module.exports.ISO_2022_KR, ISO_2022);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
module.exports.ISO_2022_CN = function() {
|
module.exports.ISO_2022_CN = function() {
|
||||||
this.name = function() {
|
this.name = function() {
|
||||||
return 'ISO-2022-CN';
|
return 'ISO-2022-CN';
|
||||||
};
|
};
|
||||||
this.escapeSequences = [
|
this.escapeSequences = [
|
||||||
[ 0x1b, 0x24, 0x29, 0x41 ], // GB 2312-80
|
[ 0x1b, 0x24, 0x29, 0x41 ], // GB 2312-80
|
||||||
[ 0x1b, 0x24, 0x29, 0x47 ], // CNS 11643-1992 Plane 1
|
[ 0x1b, 0x24, 0x29, 0x47 ], // CNS 11643-1992 Plane 1
|
||||||
[ 0x1b, 0x24, 0x2A, 0x48 ], // CNS 11643-1992 Plane 2
|
[ 0x1b, 0x24, 0x2A, 0x48 ], // CNS 11643-1992 Plane 2
|
||||||
[ 0x1b, 0x24, 0x29, 0x45 ], // ISO-IR-165
|
[ 0x1b, 0x24, 0x29, 0x45 ], // ISO-IR-165
|
||||||
[ 0x1b, 0x24, 0x2B, 0x49 ], // CNS 11643-1992 Plane 3
|
[ 0x1b, 0x24, 0x2B, 0x49 ], // CNS 11643-1992 Plane 3
|
||||||
[ 0x1b, 0x24, 0x2B, 0x4A ], // CNS 11643-1992 Plane 4
|
[ 0x1b, 0x24, 0x2B, 0x4A ], // CNS 11643-1992 Plane 4
|
||||||
[ 0x1b, 0x24, 0x2B, 0x4B ], // CNS 11643-1992 Plane 5
|
[ 0x1b, 0x24, 0x2B, 0x4B ], // CNS 11643-1992 Plane 5
|
||||||
[ 0x1b, 0x24, 0x2B, 0x4C ], // CNS 11643-1992 Plane 6
|
[ 0x1b, 0x24, 0x2B, 0x4C ], // CNS 11643-1992 Plane 6
|
||||||
[ 0x1b, 0x24, 0x2B, 0x4D ], // CNS 11643-1992 Plane 7
|
[ 0x1b, 0x24, 0x2B, 0x4D ], // CNS 11643-1992 Plane 7
|
||||||
[ 0x1b, 0x4e ], // SS2
|
[ 0x1b, 0x4e ], // SS2
|
||||||
[ 0x1b, 0x4f ] // SS3
|
[ 0x1b, 0x4f ] // SS3
|
||||||
];
|
];
|
||||||
};
|
};
|
||||||
util.inherits(module.exports.ISO_2022_CN, ISO_2022);
|
util.inherits(module.exports.ISO_2022_CN, ISO_2022);
|
||||||
|
|
686
encoding/mbcs.js
686
encoding/mbcs.js
|
@ -1,30 +1,30 @@
|
||||||
var util = require('util'),
|
var util = require('util'),
|
||||||
Match = require ('../match');
|
Match = require ('../match');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Binary search implementation (recursive)
|
* Binary search implementation (recursive)
|
||||||
*/
|
*/
|
||||||
function binarySearch(arr, searchValue) {
|
function binarySearch(arr, searchValue) {
|
||||||
function find(arr, searchValue, left, right) {
|
function find(arr, searchValue, left, right) {
|
||||||
if (right < left)
|
if (right < left)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
int mid = mid = (left + right) / 2;
|
int mid = mid = (left + right) / 2;
|
||||||
There is a bug in the above line;
|
There is a bug in the above line;
|
||||||
Joshua Bloch suggests the following replacement:
|
Joshua Bloch suggests the following replacement:
|
||||||
*/
|
*/
|
||||||
var mid = Math.floor((left + right) >>> 1);
|
var mid = Math.floor((left + right) >>> 1);
|
||||||
if (searchValue > arr[mid])
|
if (searchValue > arr[mid])
|
||||||
return find(arr, searchValue, mid + 1, right);
|
return find(arr, searchValue, mid + 1, right);
|
||||||
|
|
||||||
if (searchValue < arr[mid])
|
if (searchValue < arr[mid])
|
||||||
return find(arr, searchValue, left, mid - 1);
|
return find(arr, searchValue, left, mid - 1);
|
||||||
|
|
||||||
return mid;
|
return mid;
|
||||||
};
|
};
|
||||||
|
|
||||||
return find(arr, searchValue, 0, arr.length - 1);
|
return find(arr, searchValue, 0, arr.length - 1);
|
||||||
};
|
};
|
||||||
|
|
||||||
// 'Character' iterated character class.
|
// 'Character' iterated character class.
|
||||||
|
@ -40,28 +40,28 @@ function binarySearch(arr, searchValue) {
|
||||||
//
|
//
|
||||||
function IteratedChar() {
|
function IteratedChar() {
|
||||||
|
|
||||||
this.charValue = 0; // 1-4 bytes from the raw input data
|
this.charValue = 0; // 1-4 bytes from the raw input data
|
||||||
this.index = 0;
|
this.index = 0;
|
||||||
|
this.nextIndex = 0;
|
||||||
|
this.error = false;
|
||||||
|
this.done = false;
|
||||||
|
|
||||||
|
this.reset = function() {
|
||||||
|
this.charValue = 0;
|
||||||
|
this.index = -1;
|
||||||
this.nextIndex = 0;
|
this.nextIndex = 0;
|
||||||
this.error = false;
|
this.error = false;
|
||||||
this.done = false;
|
this.done = false;
|
||||||
|
};
|
||||||
|
|
||||||
this.reset = function() {
|
this.nextByte = function(det) {
|
||||||
this.charValue = 0;
|
if (this.nextIndex >= det.fRawLength) {
|
||||||
this.index = -1;
|
this.done = true;
|
||||||
this.nextIndex = 0;
|
return -1;
|
||||||
this.error = false;
|
}
|
||||||
this.done = false;
|
var byteValue = det.fRawInput[this.nextIndex++] & 0x00ff;
|
||||||
};
|
return byteValue;
|
||||||
|
};
|
||||||
this.nextByte = function(det) {
|
|
||||||
if (this.nextIndex >= det.fRawLength) {
|
|
||||||
this.done = true;
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
var byteValue = det.fRawInput[this.nextIndex++] & 0x00ff;
|
|
||||||
return byteValue;
|
|
||||||
};
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
@ -87,87 +87,87 @@ function mbcs() {};
|
||||||
*/
|
*/
|
||||||
mbcs.prototype.match = function(det) {
|
mbcs.prototype.match = function(det) {
|
||||||
|
|
||||||
var singleByteCharCount = 0, //TODO Do we really need this?
|
var singleByteCharCount = 0, //TODO Do we really need this?
|
||||||
doubleByteCharCount = 0,
|
doubleByteCharCount = 0,
|
||||||
commonCharCount = 0,
|
commonCharCount = 0,
|
||||||
badCharCount = 0,
|
badCharCount = 0,
|
||||||
totalCharCount = 0,
|
totalCharCount = 0,
|
||||||
confidence = 0;
|
confidence = 0;
|
||||||
|
|
||||||
var iter = new IteratedChar();
|
var iter = new IteratedChar();
|
||||||
|
|
||||||
detectBlock: {
|
detectBlock: {
|
||||||
for (iter.reset(); this.nextChar(iter, det);) {
|
for (iter.reset(); this.nextChar(iter, det);) {
|
||||||
totalCharCount++;
|
totalCharCount++;
|
||||||
if (iter.error) {
|
if (iter.error) {
|
||||||
badCharCount++;
|
badCharCount++;
|
||||||
} else {
|
} else {
|
||||||
var cv = iter.charValue & 0xFFFFFFFF;
|
var cv = iter.charValue & 0xFFFFFFFF;
|
||||||
|
|
||||||
if (cv <= 0xff) {
|
if (cv <= 0xff) {
|
||||||
singleByteCharCount++;
|
singleByteCharCount++;
|
||||||
} else {
|
|
||||||
doubleByteCharCount++;
|
|
||||||
if (this.commonChars != null) {
|
|
||||||
// NOTE: This assumes that there are no 4-byte common chars.
|
|
||||||
if (binarySearch(this.commonChars, cv) >= 0) {
|
|
||||||
commonCharCount++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (badCharCount >= 2 && badCharCount * 5 >= doubleByteCharCount) {
|
|
||||||
// console.log('its here!')
|
|
||||||
// Bail out early if the byte data is not matching the encoding scheme.
|
|
||||||
break detectBlock;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (doubleByteCharCount <= 10 && badCharCount== 0) {
|
|
||||||
// Not many multi-byte chars.
|
|
||||||
if (doubleByteCharCount == 0 && totalCharCount < 10) {
|
|
||||||
// There weren't any multibyte sequences, and there was a low density of non-ASCII single bytes.
|
|
||||||
// We don't have enough data to have any confidence.
|
|
||||||
// Statistical analysis of single byte non-ASCII charcters would probably help here.
|
|
||||||
confidence = 0;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
// ASCII or ISO file? It's probably not our encoding,
|
|
||||||
// but is not incompatible with our encoding, so don't give it a zero.
|
|
||||||
confidence = 10;
|
|
||||||
}
|
|
||||||
break detectBlock;
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
// No match if there are too many characters that don't fit the encoding scheme.
|
|
||||||
// (should we have zero tolerance for these?)
|
|
||||||
//
|
|
||||||
if (doubleByteCharCount < 20 * badCharCount) {
|
|
||||||
confidence = 0;
|
|
||||||
break detectBlock;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (this.commonChars == null) {
|
|
||||||
// We have no statistics on frequently occuring characters.
|
|
||||||
// Assess confidence purely on having a reasonable number of
|
|
||||||
// multi-byte characters (the more the better
|
|
||||||
confidence = 30 + doubleByteCharCount - 20 * badCharCount;
|
|
||||||
if (confidence > 100) {
|
|
||||||
confidence = 100;
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
//
|
doubleByteCharCount++;
|
||||||
// Frequency of occurence statistics exist.
|
if (this.commonChars != null) {
|
||||||
//
|
// NOTE: This assumes that there are no 4-byte common chars.
|
||||||
var maxVal = Math.log(parseFloat(doubleByteCharCount) / 4);
|
if (binarySearch(this.commonChars, cv) >= 0) {
|
||||||
var scaleFactor = 90.0 / maxVal;
|
commonCharCount++;
|
||||||
confidence = Math.floor(Math.log(commonCharCount + 1) * scaleFactor + 10);
|
}
|
||||||
confidence = Math.min(confidence, 100);
|
}
|
||||||
}
|
}
|
||||||
} // end of detectBlock:
|
}
|
||||||
|
if (badCharCount >= 2 && badCharCount * 5 >= doubleByteCharCount) {
|
||||||
|
// console.log('its here!')
|
||||||
|
// Bail out early if the byte data is not matching the encoding scheme.
|
||||||
|
break detectBlock;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return confidence == 0 ? null : new Match(det, this, confidence);
|
if (doubleByteCharCount <= 10 && badCharCount== 0) {
|
||||||
|
// Not many multi-byte chars.
|
||||||
|
if (doubleByteCharCount == 0 && totalCharCount < 10) {
|
||||||
|
// There weren't any multibyte sequences, and there was a low density of non-ASCII single bytes.
|
||||||
|
// We don't have enough data to have any confidence.
|
||||||
|
// Statistical analysis of single byte non-ASCII charcters would probably help here.
|
||||||
|
confidence = 0;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// ASCII or ISO file? It's probably not our encoding,
|
||||||
|
// but is not incompatible with our encoding, so don't give it a zero.
|
||||||
|
confidence = 10;
|
||||||
|
}
|
||||||
|
break detectBlock;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// No match if there are too many characters that don't fit the encoding scheme.
|
||||||
|
// (should we have zero tolerance for these?)
|
||||||
|
//
|
||||||
|
if (doubleByteCharCount < 20 * badCharCount) {
|
||||||
|
confidence = 0;
|
||||||
|
break detectBlock;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.commonChars == null) {
|
||||||
|
// We have no statistics on frequently occuring characters.
|
||||||
|
// Assess confidence purely on having a reasonable number of
|
||||||
|
// multi-byte characters (the more the better
|
||||||
|
confidence = 30 + doubleByteCharCount - 20 * badCharCount;
|
||||||
|
if (confidence > 100) {
|
||||||
|
confidence = 100;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
//
|
||||||
|
// Frequency of occurence statistics exist.
|
||||||
|
//
|
||||||
|
var maxVal = Math.log(parseFloat(doubleByteCharCount) / 4);
|
||||||
|
var scaleFactor = 90.0 / maxVal;
|
||||||
|
confidence = Math.floor(Math.log(commonCharCount + 1) * scaleFactor + 10);
|
||||||
|
confidence = Math.min(confidence, 100);
|
||||||
|
}
|
||||||
|
} // end of detectBlock:
|
||||||
|
|
||||||
|
return confidence == 0 ? null : new Match(det, this, confidence);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -191,48 +191,48 @@ mbcs.prototype.nextChar = function(iter, det) {};
|
||||||
* Shift-JIS charset recognizer.
|
* Shift-JIS charset recognizer.
|
||||||
*/
|
*/
|
||||||
module.exports.sjis = function() {
|
module.exports.sjis = function() {
|
||||||
this.name = function() {
|
this.name = function() {
|
||||||
return 'Shift-JIS';
|
return 'Shift-JIS';
|
||||||
};
|
};
|
||||||
this.language = function() {
|
this.language = function() {
|
||||||
return 'ja';
|
return 'ja';
|
||||||
};
|
};
|
||||||
|
|
||||||
// TODO: This set of data comes from the character frequency-
|
// TODO: This set of data comes from the character frequency-
|
||||||
// of-occurence analysis tool. The data needs to be moved
|
// of-occurence analysis tool. The data needs to be moved
|
||||||
// into a resource and loaded from there.
|
// into a resource and loaded from there.
|
||||||
this.commonChars = [
|
this.commonChars = [
|
||||||
0x8140, 0x8141, 0x8142, 0x8145, 0x815b, 0x8169, 0x816a, 0x8175, 0x8176, 0x82a0,
|
0x8140, 0x8141, 0x8142, 0x8145, 0x815b, 0x8169, 0x816a, 0x8175, 0x8176, 0x82a0,
|
||||||
0x82a2, 0x82a4, 0x82a9, 0x82aa, 0x82ab, 0x82ad, 0x82af, 0x82b1, 0x82b3, 0x82b5,
|
0x82a2, 0x82a4, 0x82a9, 0x82aa, 0x82ab, 0x82ad, 0x82af, 0x82b1, 0x82b3, 0x82b5,
|
||||||
0x82b7, 0x82bd, 0x82be, 0x82c1, 0x82c4, 0x82c5, 0x82c6, 0x82c8, 0x82c9, 0x82cc,
|
0x82b7, 0x82bd, 0x82be, 0x82c1, 0x82c4, 0x82c5, 0x82c6, 0x82c8, 0x82c9, 0x82cc,
|
||||||
0x82cd, 0x82dc, 0x82e0, 0x82e7, 0x82e8, 0x82e9, 0x82ea, 0x82f0, 0x82f1, 0x8341,
|
0x82cd, 0x82dc, 0x82e0, 0x82e7, 0x82e8, 0x82e9, 0x82ea, 0x82f0, 0x82f1, 0x8341,
|
||||||
0x8343, 0x834e, 0x834f, 0x8358, 0x835e, 0x8362, 0x8367, 0x8375, 0x8376, 0x8389,
|
0x8343, 0x834e, 0x834f, 0x8358, 0x835e, 0x8362, 0x8367, 0x8375, 0x8376, 0x8389,
|
||||||
0x838a, 0x838b, 0x838d, 0x8393, 0x8e96, 0x93fa, 0x95aa
|
0x838a, 0x838b, 0x838d, 0x8393, 0x8e96, 0x93fa, 0x95aa
|
||||||
];
|
];
|
||||||
|
|
||||||
this.nextChar = function(iter, det) {
|
this.nextChar = function(iter, det) {
|
||||||
iter.index = iter.nextIndex;
|
iter.index = iter.nextIndex;
|
||||||
iter.error = false;
|
iter.error = false;
|
||||||
|
|
||||||
var firstByte;
|
var firstByte;
|
||||||
firstByte = iter.charValue = iter.nextByte(det);
|
firstByte = iter.charValue = iter.nextByte(det);
|
||||||
if (firstByte < 0)
|
if (firstByte < 0)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if (firstByte <= 0x7f || (firstByte > 0xa0 && firstByte <= 0xdf))
|
if (firstByte <= 0x7f || (firstByte > 0xa0 && firstByte <= 0xdf))
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
var secondByte = iter.nextByte(det);
|
var secondByte = iter.nextByte(det);
|
||||||
if (secondByte < 0)
|
if (secondByte < 0)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
iter.charValue = (firstByte << 8) | secondByte;
|
iter.charValue = (firstByte << 8) | secondByte;
|
||||||
if (! ((secondByte >= 0x40 && secondByte <= 0x7f) || (secondByte >= 0x80 && secondByte <= 0xff))) {
|
if (! ((secondByte >= 0x40 && secondByte <= 0x7f) || (secondByte >= 0x80 && secondByte <= 0xff))) {
|
||||||
// Illegal second byte value.
|
// Illegal second byte value.
|
||||||
iter.error = true;
|
iter.error = true;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
util.inherits(module.exports.sjis, mbcs);
|
util.inherits(module.exports.sjis, mbcs);
|
||||||
|
|
||||||
|
@ -242,52 +242,52 @@ util.inherits(module.exports.sjis, mbcs);
|
||||||
* Big5 charset recognizer.
|
* Big5 charset recognizer.
|
||||||
*/
|
*/
|
||||||
module.exports.big5 = function() {
|
module.exports.big5 = function() {
|
||||||
this.name = function() {
|
this.name = function() {
|
||||||
return 'Big5';
|
return 'Big5';
|
||||||
};
|
};
|
||||||
this.language = function() {
|
this.language = function() {
|
||||||
return 'zh';
|
return 'zh';
|
||||||
};
|
};
|
||||||
// TODO: This set of data comes from the character frequency-
|
// TODO: This set of data comes from the character frequency-
|
||||||
// of-occurence analysis tool. The data needs to be moved
|
// of-occurence analysis tool. The data needs to be moved
|
||||||
// into a resource and loaded from there.
|
// into a resource and loaded from there.
|
||||||
this.commonChars = [
|
this.commonChars = [
|
||||||
0xa140, 0xa141, 0xa142, 0xa143, 0xa147, 0xa149, 0xa175, 0xa176, 0xa440, 0xa446,
|
0xa140, 0xa141, 0xa142, 0xa143, 0xa147, 0xa149, 0xa175, 0xa176, 0xa440, 0xa446,
|
||||||
0xa447, 0xa448, 0xa451, 0xa454, 0xa457, 0xa464, 0xa46a, 0xa46c, 0xa477, 0xa4a3,
|
0xa447, 0xa448, 0xa451, 0xa454, 0xa457, 0xa464, 0xa46a, 0xa46c, 0xa477, 0xa4a3,
|
||||||
0xa4a4, 0xa4a7, 0xa4c1, 0xa4ce, 0xa4d1, 0xa4df, 0xa4e8, 0xa4fd, 0xa540, 0xa548,
|
0xa4a4, 0xa4a7, 0xa4c1, 0xa4ce, 0xa4d1, 0xa4df, 0xa4e8, 0xa4fd, 0xa540, 0xa548,
|
||||||
0xa558, 0xa569, 0xa5cd, 0xa5e7, 0xa657, 0xa661, 0xa662, 0xa668, 0xa670, 0xa6a8,
|
0xa558, 0xa569, 0xa5cd, 0xa5e7, 0xa657, 0xa661, 0xa662, 0xa668, 0xa670, 0xa6a8,
|
||||||
0xa6b3, 0xa6b9, 0xa6d3, 0xa6db, 0xa6e6, 0xa6f2, 0xa740, 0xa751, 0xa759, 0xa7da,
|
0xa6b3, 0xa6b9, 0xa6d3, 0xa6db, 0xa6e6, 0xa6f2, 0xa740, 0xa751, 0xa759, 0xa7da,
|
||||||
0xa8a3, 0xa8a5, 0xa8ad, 0xa8d1, 0xa8d3, 0xa8e4, 0xa8fc, 0xa9c0, 0xa9d2, 0xa9f3,
|
0xa8a3, 0xa8a5, 0xa8ad, 0xa8d1, 0xa8d3, 0xa8e4, 0xa8fc, 0xa9c0, 0xa9d2, 0xa9f3,
|
||||||
0xaa6b, 0xaaba, 0xaabe, 0xaacc, 0xaafc, 0xac47, 0xac4f, 0xacb0, 0xacd2, 0xad59,
|
0xaa6b, 0xaaba, 0xaabe, 0xaacc, 0xaafc, 0xac47, 0xac4f, 0xacb0, 0xacd2, 0xad59,
|
||||||
0xaec9, 0xafe0, 0xb0ea, 0xb16f, 0xb2b3, 0xb2c4, 0xb36f, 0xb44c, 0xb44e, 0xb54c,
|
0xaec9, 0xafe0, 0xb0ea, 0xb16f, 0xb2b3, 0xb2c4, 0xb36f, 0xb44c, 0xb44e, 0xb54c,
|
||||||
0xb5a5, 0xb5bd, 0xb5d0, 0xb5d8, 0xb671, 0xb7ed, 0xb867, 0xb944, 0xbad8, 0xbb44,
|
0xb5a5, 0xb5bd, 0xb5d0, 0xb5d8, 0xb671, 0xb7ed, 0xb867, 0xb944, 0xbad8, 0xbb44,
|
||||||
0xbba1, 0xbdd1, 0xc2c4, 0xc3b9, 0xc440, 0xc45f
|
0xbba1, 0xbdd1, 0xc2c4, 0xc3b9, 0xc440, 0xc45f
|
||||||
];
|
];
|
||||||
this.nextChar = function(iter, det) {
|
this.nextChar = function(iter, det) {
|
||||||
iter.index = iter.nextIndex;
|
iter.index = iter.nextIndex;
|
||||||
iter.error = false;
|
iter.error = false;
|
||||||
|
|
||||||
var firstByte = iter.charValue = iter.nextByte(det);
|
var firstByte = iter.charValue = iter.nextByte(det);
|
||||||
|
|
||||||
if (firstByte < 0)
|
if (firstByte < 0)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
// single byte character.
|
// single byte character.
|
||||||
if (firstByte <= 0x7f || firstByte == 0xff)
|
if (firstByte <= 0x7f || firstByte == 0xff)
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
var secondByte = iter.nextByte(det);
|
var secondByte = iter.nextByte(det);
|
||||||
|
|
||||||
if (secondByte < 0)
|
if (secondByte < 0)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
iter.charValue = (iter.charValue << 8) | secondByte;
|
iter.charValue = (iter.charValue << 8) | secondByte;
|
||||||
|
|
||||||
if (secondByte < 0x40 || secondByte == 0x7f || secondByte == 0xff)
|
if (secondByte < 0x40 || secondByte == 0x7f || secondByte == 0xff)
|
||||||
iter.error = true;
|
iter.error = true;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
util.inherits(module.exports.big5, mbcs);
|
util.inherits(module.exports.big5, mbcs);
|
||||||
|
|
||||||
|
@ -303,55 +303,55 @@ util.inherits(module.exports.big5, mbcs);
|
||||||
* packed into an int.
|
* packed into an int.
|
||||||
*/
|
*/
|
||||||
function eucNextChar(iter, det) {
|
function eucNextChar(iter, det) {
|
||||||
iter.index = iter.nextIndex;
|
iter.index = iter.nextIndex;
|
||||||
iter.error = false;
|
iter.error = false;
|
||||||
var firstByte = 0;
|
var firstByte = 0;
|
||||||
var secondByte = 0;
|
var secondByte = 0;
|
||||||
var thirdByte = 0;
|
var thirdByte = 0;
|
||||||
//int fourthByte = 0;
|
//int fourthByte = 0;
|
||||||
buildChar: {
|
buildChar: {
|
||||||
firstByte = iter.charValue = iter.nextByte(det);
|
firstByte = iter.charValue = iter.nextByte(det);
|
||||||
if (firstByte < 0) {
|
if (firstByte < 0) {
|
||||||
// Ran off the end of the input data
|
// Ran off the end of the input data
|
||||||
iter.done = true;
|
iter.done = true;
|
||||||
break buildChar;
|
break buildChar;
|
||||||
}
|
|
||||||
if (firstByte <= 0x8d) {
|
|
||||||
// single byte char
|
|
||||||
break buildChar;
|
|
||||||
}
|
|
||||||
secondByte = iter.nextByte(det);
|
|
||||||
iter.charValue = (iter.charValue << 8) | secondByte;
|
|
||||||
if (firstByte >= 0xA1 && firstByte <= 0xfe) {
|
|
||||||
// Two byte Char
|
|
||||||
if (secondByte < 0xa1) {
|
|
||||||
iter.error = true;
|
|
||||||
}
|
|
||||||
break buildChar;
|
|
||||||
}
|
|
||||||
if (firstByte == 0x8e) {
|
|
||||||
// Code Set 2.
|
|
||||||
// In EUC-JP, total char size is 2 bytes, only one byte of actual char value.
|
|
||||||
// In EUC-TW, total char size is 4 bytes, three bytes contribute to char value.
|
|
||||||
// We don't know which we've got.
|
|
||||||
// Treat it like EUC-JP. If the data really was EUC-TW, the following two
|
|
||||||
// bytes will look like a well formed 2 byte char.
|
|
||||||
if (secondByte < 0xa1) {
|
|
||||||
iter.error = true;
|
|
||||||
}
|
|
||||||
break buildChar;
|
|
||||||
}
|
|
||||||
if (firstByte == 0x8f) {
|
|
||||||
// Code set 3.
|
|
||||||
// Three byte total char size, two bytes of actual char value.
|
|
||||||
thirdByte = iter.nextByte(det);
|
|
||||||
iter.charValue = (iter.charValue << 8) | thirdByte;
|
|
||||||
if (thirdByte < 0xa1) {
|
|
||||||
iter.error = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return iter.done == false;
|
if (firstByte <= 0x8d) {
|
||||||
|
// single byte char
|
||||||
|
break buildChar;
|
||||||
|
}
|
||||||
|
secondByte = iter.nextByte(det);
|
||||||
|
iter.charValue = (iter.charValue << 8) | secondByte;
|
||||||
|
if (firstByte >= 0xA1 && firstByte <= 0xfe) {
|
||||||
|
// Two byte Char
|
||||||
|
if (secondByte < 0xa1) {
|
||||||
|
iter.error = true;
|
||||||
|
}
|
||||||
|
break buildChar;
|
||||||
|
}
|
||||||
|
if (firstByte == 0x8e) {
|
||||||
|
// Code Set 2.
|
||||||
|
// In EUC-JP, total char size is 2 bytes, only one byte of actual char value.
|
||||||
|
// In EUC-TW, total char size is 4 bytes, three bytes contribute to char value.
|
||||||
|
// We don't know which we've got.
|
||||||
|
// Treat it like EUC-JP. If the data really was EUC-TW, the following two
|
||||||
|
// bytes will look like a well formed 2 byte char.
|
||||||
|
if (secondByte < 0xa1) {
|
||||||
|
iter.error = true;
|
||||||
|
}
|
||||||
|
break buildChar;
|
||||||
|
}
|
||||||
|
if (firstByte == 0x8f) {
|
||||||
|
// Code set 3.
|
||||||
|
// Three byte total char size, two bytes of actual char value.
|
||||||
|
thirdByte = iter.nextByte(det);
|
||||||
|
iter.charValue = (iter.charValue << 8) | thirdByte;
|
||||||
|
if (thirdByte < 0xa1) {
|
||||||
|
iter.error = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return iter.done == false;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
@ -361,30 +361,30 @@ function eucNextChar(iter, det) {
|
||||||
* is created and kept by the public CharsetDetector class
|
* is created and kept by the public CharsetDetector class
|
||||||
*/
|
*/
|
||||||
module.exports.euc_jp = function() {
|
module.exports.euc_jp = function() {
|
||||||
this.name = function() {
|
this.name = function() {
|
||||||
return 'EUC-JP';
|
return 'EUC-JP';
|
||||||
};
|
};
|
||||||
this.language = function() {
|
this.language = function() {
|
||||||
return 'ja';
|
return 'ja';
|
||||||
};
|
};
|
||||||
|
|
||||||
// TODO: This set of data comes from the character frequency-
|
// TODO: This set of data comes from the character frequency-
|
||||||
// of-occurence analysis tool. The data needs to be moved
|
// of-occurence analysis tool. The data needs to be moved
|
||||||
// into a resource and loaded from there.
|
// into a resource and loaded from there.
|
||||||
this.commonChars = [
|
this.commonChars = [
|
||||||
0xa1a1, 0xa1a2, 0xa1a3, 0xa1a6, 0xa1bc, 0xa1ca, 0xa1cb, 0xa1d6, 0xa1d7, 0xa4a2,
|
0xa1a1, 0xa1a2, 0xa1a3, 0xa1a6, 0xa1bc, 0xa1ca, 0xa1cb, 0xa1d6, 0xa1d7, 0xa4a2,
|
||||||
0xa4a4, 0xa4a6, 0xa4a8, 0xa4aa, 0xa4ab, 0xa4ac, 0xa4ad, 0xa4af, 0xa4b1, 0xa4b3,
|
0xa4a4, 0xa4a6, 0xa4a8, 0xa4aa, 0xa4ab, 0xa4ac, 0xa4ad, 0xa4af, 0xa4b1, 0xa4b3,
|
||||||
0xa4b5, 0xa4b7, 0xa4b9, 0xa4bb, 0xa4bd, 0xa4bf, 0xa4c0, 0xa4c1, 0xa4c3, 0xa4c4,
|
0xa4b5, 0xa4b7, 0xa4b9, 0xa4bb, 0xa4bd, 0xa4bf, 0xa4c0, 0xa4c1, 0xa4c3, 0xa4c4,
|
||||||
0xa4c6, 0xa4c7, 0xa4c8, 0xa4c9, 0xa4ca, 0xa4cb, 0xa4ce, 0xa4cf, 0xa4d0, 0xa4de,
|
0xa4c6, 0xa4c7, 0xa4c8, 0xa4c9, 0xa4ca, 0xa4cb, 0xa4ce, 0xa4cf, 0xa4d0, 0xa4de,
|
||||||
0xa4df, 0xa4e1, 0xa4e2, 0xa4e4, 0xa4e8, 0xa4e9, 0xa4ea, 0xa4eb, 0xa4ec, 0xa4ef,
|
0xa4df, 0xa4e1, 0xa4e2, 0xa4e4, 0xa4e8, 0xa4e9, 0xa4ea, 0xa4eb, 0xa4ec, 0xa4ef,
|
||||||
0xa4f2, 0xa4f3, 0xa5a2, 0xa5a3, 0xa5a4, 0xa5a6, 0xa5a7, 0xa5aa, 0xa5ad, 0xa5af,
|
0xa4f2, 0xa4f3, 0xa5a2, 0xa5a3, 0xa5a4, 0xa5a6, 0xa5a7, 0xa5aa, 0xa5ad, 0xa5af,
|
||||||
0xa5b0, 0xa5b3, 0xa5b5, 0xa5b7, 0xa5b8, 0xa5b9, 0xa5bf, 0xa5c3, 0xa5c6, 0xa5c7,
|
0xa5b0, 0xa5b3, 0xa5b5, 0xa5b7, 0xa5b8, 0xa5b9, 0xa5bf, 0xa5c3, 0xa5c6, 0xa5c7,
|
||||||
0xa5c8, 0xa5c9, 0xa5cb, 0xa5d0, 0xa5d5, 0xa5d6, 0xa5d7, 0xa5de, 0xa5e0, 0xa5e1,
|
0xa5c8, 0xa5c9, 0xa5cb, 0xa5d0, 0xa5d5, 0xa5d6, 0xa5d7, 0xa5de, 0xa5e0, 0xa5e1,
|
||||||
0xa5e5, 0xa5e9, 0xa5ea, 0xa5eb, 0xa5ec, 0xa5ed, 0xa5f3, 0xb8a9, 0xb9d4, 0xbaee,
|
0xa5e5, 0xa5e9, 0xa5ea, 0xa5eb, 0xa5ec, 0xa5ed, 0xa5f3, 0xb8a9, 0xb9d4, 0xbaee,
|
||||||
0xbbc8, 0xbef0, 0xbfb7, 0xc4ea, 0xc6fc, 0xc7bd, 0xcab8, 0xcaf3, 0xcbdc, 0xcdd1
|
0xbbc8, 0xbef0, 0xbfb7, 0xc4ea, 0xc6fc, 0xc7bd, 0xcab8, 0xcaf3, 0xcbdc, 0xcdd1
|
||||||
];
|
];
|
||||||
|
|
||||||
this.nextChar = eucNextChar;
|
this.nextChar = eucNextChar;
|
||||||
};
|
};
|
||||||
util.inherits(module.exports.euc_jp, mbcs);
|
util.inherits(module.exports.euc_jp, mbcs);
|
||||||
|
|
||||||
|
@ -395,30 +395,30 @@ util.inherits(module.exports.euc_jp, mbcs);
|
||||||
* is created and kept by the public CharsetDetector class
|
* is created and kept by the public CharsetDetector class
|
||||||
*/
|
*/
|
||||||
module.exports.euc_kr = function() {
|
module.exports.euc_kr = function() {
|
||||||
this.name = function() {
|
this.name = function() {
|
||||||
return 'EUC-KR';
|
return 'EUC-KR';
|
||||||
};
|
};
|
||||||
this.language = function() {
|
this.language = function() {
|
||||||
return 'ko';
|
return 'ko';
|
||||||
};
|
};
|
||||||
|
|
||||||
// TODO: This set of data comes from the character frequency-
|
// TODO: This set of data comes from the character frequency-
|
||||||
// of-occurence analysis tool. The data needs to be moved
|
// of-occurence analysis tool. The data needs to be moved
|
||||||
// into a resource and loaded from there.
|
// into a resource and loaded from there.
|
||||||
this.commonChars = [
|
this.commonChars = [
|
||||||
0xb0a1, 0xb0b3, 0xb0c5, 0xb0cd, 0xb0d4, 0xb0e6, 0xb0ed, 0xb0f8, 0xb0fa, 0xb0fc,
|
0xb0a1, 0xb0b3, 0xb0c5, 0xb0cd, 0xb0d4, 0xb0e6, 0xb0ed, 0xb0f8, 0xb0fa, 0xb0fc,
|
||||||
0xb1b8, 0xb1b9, 0xb1c7, 0xb1d7, 0xb1e2, 0xb3aa, 0xb3bb, 0xb4c2, 0xb4cf, 0xb4d9,
|
0xb1b8, 0xb1b9, 0xb1c7, 0xb1d7, 0xb1e2, 0xb3aa, 0xb3bb, 0xb4c2, 0xb4cf, 0xb4d9,
|
||||||
0xb4eb, 0xb5a5, 0xb5b5, 0xb5bf, 0xb5c7, 0xb5e9, 0xb6f3, 0xb7af, 0xb7c2, 0xb7ce,
|
0xb4eb, 0xb5a5, 0xb5b5, 0xb5bf, 0xb5c7, 0xb5e9, 0xb6f3, 0xb7af, 0xb7c2, 0xb7ce,
|
||||||
0xb8a6, 0xb8ae, 0xb8b6, 0xb8b8, 0xb8bb, 0xb8e9, 0xb9ab, 0xb9ae, 0xb9cc, 0xb9ce,
|
0xb8a6, 0xb8ae, 0xb8b6, 0xb8b8, 0xb8bb, 0xb8e9, 0xb9ab, 0xb9ae, 0xb9cc, 0xb9ce,
|
||||||
0xb9fd, 0xbab8, 0xbace, 0xbad0, 0xbaf1, 0xbbe7, 0xbbf3, 0xbbfd, 0xbcad, 0xbcba,
|
0xb9fd, 0xbab8, 0xbace, 0xbad0, 0xbaf1, 0xbbe7, 0xbbf3, 0xbbfd, 0xbcad, 0xbcba,
|
||||||
0xbcd2, 0xbcf6, 0xbdba, 0xbdc0, 0xbdc3, 0xbdc5, 0xbec6, 0xbec8, 0xbedf, 0xbeee,
|
0xbcd2, 0xbcf6, 0xbdba, 0xbdc0, 0xbdc3, 0xbdc5, 0xbec6, 0xbec8, 0xbedf, 0xbeee,
|
||||||
0xbef8, 0xbefa, 0xbfa1, 0xbfa9, 0xbfc0, 0xbfe4, 0xbfeb, 0xbfec, 0xbff8, 0xc0a7,
|
0xbef8, 0xbefa, 0xbfa1, 0xbfa9, 0xbfc0, 0xbfe4, 0xbfeb, 0xbfec, 0xbff8, 0xc0a7,
|
||||||
0xc0af, 0xc0b8, 0xc0ba, 0xc0bb, 0xc0bd, 0xc0c7, 0xc0cc, 0xc0ce, 0xc0cf, 0xc0d6,
|
0xc0af, 0xc0b8, 0xc0ba, 0xc0bb, 0xc0bd, 0xc0c7, 0xc0cc, 0xc0ce, 0xc0cf, 0xc0d6,
|
||||||
0xc0da, 0xc0e5, 0xc0fb, 0xc0fc, 0xc1a4, 0xc1a6, 0xc1b6, 0xc1d6, 0xc1df, 0xc1f6,
|
0xc0da, 0xc0e5, 0xc0fb, 0xc0fc, 0xc1a4, 0xc1a6, 0xc1b6, 0xc1d6, 0xc1df, 0xc1f6,
|
||||||
0xc1f8, 0xc4a1, 0xc5cd, 0xc6ae, 0xc7cf, 0xc7d1, 0xc7d2, 0xc7d8, 0xc7e5, 0xc8ad
|
0xc1f8, 0xc4a1, 0xc5cd, 0xc6ae, 0xc7cf, 0xc7d1, 0xc7d2, 0xc7d8, 0xc7e5, 0xc8ad
|
||||||
];
|
];
|
||||||
|
|
||||||
this.nextChar = eucNextChar;
|
this.nextChar = eucNextChar;
|
||||||
};
|
};
|
||||||
util.inherits(module.exports.euc_kr, mbcs);
|
util.inherits(module.exports.euc_kr, mbcs);
|
||||||
|
|
||||||
|
@ -428,75 +428,75 @@ util.inherits(module.exports.euc_kr, mbcs);
|
||||||
* GB-18030 recognizer. Uses simplified Chinese statistics.
|
* GB-18030 recognizer. Uses simplified Chinese statistics.
|
||||||
*/
|
*/
|
||||||
module.exports.gb_18030 = function() {
|
module.exports.gb_18030 = function() {
|
||||||
this.name = function() {
|
this.name = function() {
|
||||||
return 'GB18030';
|
return 'GB18030';
|
||||||
};
|
};
|
||||||
this.language = function() {
|
this.language = function() {
|
||||||
return 'zh';
|
return 'zh';
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Get the next character value for EUC based encodings.
|
* Get the next character value for EUC based encodings.
|
||||||
* Character 'value' is simply the raw bytes that make up the character
|
* Character 'value' is simply the raw bytes that make up the character
|
||||||
* packed into an int.
|
* packed into an int.
|
||||||
*/
|
*/
|
||||||
this.nextChar = function(iter, det) {
|
this.nextChar = function(iter, det) {
|
||||||
iter.index = iter.nextIndex;
|
iter.index = iter.nextIndex;
|
||||||
iter.error = false;
|
iter.error = false;
|
||||||
var firstByte = 0;
|
var firstByte = 0;
|
||||||
var secondByte = 0;
|
var secondByte = 0;
|
||||||
var thirdByte = 0;
|
var thirdByte = 0;
|
||||||
var fourthByte = 0;
|
var fourthByte = 0;
|
||||||
buildChar: {
|
buildChar: {
|
||||||
firstByte = iter.charValue = iter.nextByte(det);
|
firstByte = iter.charValue = iter.nextByte(det);
|
||||||
if (firstByte < 0) {
|
if (firstByte < 0) {
|
||||||
// Ran off the end of the input data
|
// Ran off the end of the input data
|
||||||
iter.done = true;
|
iter.done = true;
|
||||||
break buildChar;
|
break buildChar;
|
||||||
}
|
}
|
||||||
if (firstByte <= 0x80) {
|
if (firstByte <= 0x80) {
|
||||||
// single byte char
|
// single byte char
|
||||||
break buildChar;
|
break buildChar;
|
||||||
}
|
}
|
||||||
secondByte = iter.nextByte(det);
|
secondByte = iter.nextByte(det);
|
||||||
iter.charValue = (iter.charValue << 8) | secondByte;
|
iter.charValue = (iter.charValue << 8) | secondByte;
|
||||||
if (firstByte >= 0x81 && firstByte <= 0xFE) {
|
if (firstByte >= 0x81 && firstByte <= 0xFE) {
|
||||||
// Two byte Char
|
// Two byte Char
|
||||||
if ((secondByte >= 0x40 && secondByte <= 0x7E) || (secondByte >=80 && secondByte <= 0xFE)) {
|
if ((secondByte >= 0x40 && secondByte <= 0x7E) || (secondByte >=80 && secondByte <= 0xFE)) {
|
||||||
break buildChar;
|
break buildChar;
|
||||||
}
|
|
||||||
// Four byte char
|
|
||||||
if (secondByte >= 0x30 && secondByte <= 0x39) {
|
|
||||||
thirdByte = iter.nextByte(det);
|
|
||||||
if (thirdByte >= 0x81 && thirdByte <= 0xFE) {
|
|
||||||
fourthByte = iter.nextByte(det);
|
|
||||||
if (fourthByte >= 0x30 && fourthByte <= 0x39) {
|
|
||||||
iter.charValue = (iter.charValue << 16) | (thirdByte << 8) | fourthByte;
|
|
||||||
break buildChar;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
iter.error = true;
|
|
||||||
break buildChar;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return iter.done == false;
|
// Four byte char
|
||||||
};
|
if (secondByte >= 0x30 && secondByte <= 0x39) {
|
||||||
|
thirdByte = iter.nextByte(det);
|
||||||
|
if (thirdByte >= 0x81 && thirdByte <= 0xFE) {
|
||||||
|
fourthByte = iter.nextByte(det);
|
||||||
|
if (fourthByte >= 0x30 && fourthByte <= 0x39) {
|
||||||
|
iter.charValue = (iter.charValue << 16) | (thirdByte << 8) | fourthByte;
|
||||||
|
break buildChar;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
iter.error = true;
|
||||||
|
break buildChar;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return iter.done == false;
|
||||||
|
};
|
||||||
|
|
||||||
// TODO: This set of data comes from the character frequency-
|
// TODO: This set of data comes from the character frequency-
|
||||||
// of-occurence analysis tool. The data needs to be moved
|
// of-occurence analysis tool. The data needs to be moved
|
||||||
// into a resource and loaded from there.
|
// into a resource and loaded from there.
|
||||||
this.commonChars = [
|
this.commonChars = [
|
||||||
0xa1a1, 0xa1a2, 0xa1a3, 0xa1a4, 0xa1b0, 0xa1b1, 0xa1f1, 0xa1f3, 0xa3a1, 0xa3ac,
|
0xa1a1, 0xa1a2, 0xa1a3, 0xa1a4, 0xa1b0, 0xa1b1, 0xa1f1, 0xa1f3, 0xa3a1, 0xa3ac,
|
||||||
0xa3ba, 0xb1a8, 0xb1b8, 0xb1be, 0xb2bb, 0xb3c9, 0xb3f6, 0xb4f3, 0xb5bd, 0xb5c4,
|
0xa3ba, 0xb1a8, 0xb1b8, 0xb1be, 0xb2bb, 0xb3c9, 0xb3f6, 0xb4f3, 0xb5bd, 0xb5c4,
|
||||||
0xb5e3, 0xb6af, 0xb6d4, 0xb6e0, 0xb7a2, 0xb7a8, 0xb7bd, 0xb7d6, 0xb7dd, 0xb8b4,
|
0xb5e3, 0xb6af, 0xb6d4, 0xb6e0, 0xb7a2, 0xb7a8, 0xb7bd, 0xb7d6, 0xb7dd, 0xb8b4,
|
||||||
0xb8df, 0xb8f6, 0xb9ab, 0xb9c9, 0xb9d8, 0xb9fa, 0xb9fd, 0xbacd, 0xbba7, 0xbbd6,
|
0xb8df, 0xb8f6, 0xb9ab, 0xb9c9, 0xb9d8, 0xb9fa, 0xb9fd, 0xbacd, 0xbba7, 0xbbd6,
|
||||||
0xbbe1, 0xbbfa, 0xbcbc, 0xbcdb, 0xbcfe, 0xbdcc, 0xbecd, 0xbedd, 0xbfb4, 0xbfc6,
|
0xbbe1, 0xbbfa, 0xbcbc, 0xbcdb, 0xbcfe, 0xbdcc, 0xbecd, 0xbedd, 0xbfb4, 0xbfc6,
|
||||||
0xbfc9, 0xc0b4, 0xc0ed, 0xc1cb, 0xc2db, 0xc3c7, 0xc4dc, 0xc4ea, 0xc5cc, 0xc6f7,
|
0xbfc9, 0xc0b4, 0xc0ed, 0xc1cb, 0xc2db, 0xc3c7, 0xc4dc, 0xc4ea, 0xc5cc, 0xc6f7,
|
||||||
0xc7f8, 0xc8ab, 0xc8cb, 0xc8d5, 0xc8e7, 0xc9cf, 0xc9fa, 0xcab1, 0xcab5, 0xcac7,
|
0xc7f8, 0xc8ab, 0xc8cb, 0xc8d5, 0xc8e7, 0xc9cf, 0xc9fa, 0xcab1, 0xcab5, 0xcac7,
|
||||||
0xcad0, 0xcad6, 0xcaf5, 0xcafd, 0xccec, 0xcdf8, 0xceaa, 0xcec4, 0xced2, 0xcee5,
|
0xcad0, 0xcad6, 0xcaf5, 0xcafd, 0xccec, 0xcdf8, 0xceaa, 0xcec4, 0xced2, 0xcee5,
|
||||||
0xcfb5, 0xcfc2, 0xcfd6, 0xd0c2, 0xd0c5, 0xd0d0, 0xd0d4, 0xd1a7, 0xd2aa, 0xd2b2,
|
0xcfb5, 0xcfc2, 0xcfd6, 0xd0c2, 0xd0c5, 0xd0d0, 0xd0d4, 0xd1a7, 0xd2aa, 0xd2b2,
|
||||||
0xd2b5, 0xd2bb, 0xd2d4, 0xd3c3, 0xd3d0, 0xd3fd, 0xd4c2, 0xd4da, 0xd5e2, 0xd6d0
|
0xd2b5, 0xd2bb, 0xd2d4, 0xd3c3, 0xd3d0, 0xd3fd, 0xd4c2, 0xd4da, 0xd5e2, 0xd6d0
|
||||||
];
|
];
|
||||||
};
|
};
|
||||||
util.inherits(module.exports.gb_18030, mbcs);
|
util.inherits(module.exports.gb_18030, mbcs);
|
||||||
|
|
1542
encoding/sbcs.js
1542
encoding/sbcs.js
File diff suppressed because it is too large
Load Diff
|
@ -1,107 +1,107 @@
|
||||||
var util = require('util'),
|
var util = require('util'),
|
||||||
Match = require ('../match');
|
Match = require ('../match');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class matches UTF-16 and UTF-32, both big- and little-endian. The
|
* This class matches UTF-16 and UTF-32, both big- and little-endian. The
|
||||||
* BOM will be used if it is present.
|
* BOM will be used if it is present.
|
||||||
*/
|
*/
|
||||||
module.exports.UTF_16BE = function() {
|
module.exports.UTF_16BE = function() {
|
||||||
this.name = function() {
|
this.name = function() {
|
||||||
return 'UTF-16BE';
|
return 'UTF-16BE';
|
||||||
};
|
};
|
||||||
this.match = function(det) {
|
this.match = function(det) {
|
||||||
var input = det.fRawInput;
|
var input = det.fRawInput;
|
||||||
|
|
||||||
if (input.length >= 2 && ((input[0] & 0xff) == 0xfe && (input[1] & 0xff) == 0xff))
|
if (input.length >= 2 && ((input[0] & 0xff) == 0xfe && (input[1] & 0xff) == 0xff))
|
||||||
return new Match(det, this, confidence = 100);
|
return new Match(det, this, confidence = 100);
|
||||||
|
|
||||||
// TODO: Do some statistics to check for unsigned UTF-16BE
|
// TODO: Do some statistics to check for unsigned UTF-16BE
|
||||||
return null;
|
return null;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports.UTF_16LE = function() {
|
module.exports.UTF_16LE = function() {
|
||||||
this.name = function() {
|
this.name = function() {
|
||||||
return 'UTF-16LE';
|
return 'UTF-16LE';
|
||||||
};
|
};
|
||||||
this.match = function(det) {
|
this.match = function(det) {
|
||||||
var input = det.fRawInput;
|
var input = det.fRawInput;
|
||||||
|
|
||||||
if (input.length >= 2 && ((input[0] & 0xff) == 0xff && (input[1] & 0xff) == 0xfe)) {
|
if (input.length >= 2 && ((input[0] & 0xff) == 0xff && (input[1] & 0xff) == 0xfe)) {
|
||||||
// An LE BOM is present.
|
// An LE BOM is present.
|
||||||
if (input.length >= 4 && input[2] == 0x00 && input[3] == 0x00)
|
if (input.length >= 4 && input[2] == 0x00 && input[3] == 0x00)
|
||||||
// It is probably UTF-32 LE, not UTF-16
|
// It is probably UTF-32 LE, not UTF-16
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
return new Match(det, this, confidence = 100);
|
return new Match(det, this, confidence = 100);
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: Do some statistics to check for unsigned UTF-16LE
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: Do some statistics to check for unsigned UTF-16LE
|
||||||
|
return null;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
function UTF_32() {};
|
function UTF_32() {};
|
||||||
UTF_32.prototype.match = function(det) {
|
UTF_32.prototype.match = function(det) {
|
||||||
var input = det.fRawInput,
|
var input = det.fRawInput,
|
||||||
limit = (det.fRawLength / 4) * 4,
|
limit = (det.fRawLength / 4) * 4,
|
||||||
numValid = 0,
|
numValid = 0,
|
||||||
numInvalid = 0,
|
numInvalid = 0,
|
||||||
hasBOM = false,
|
hasBOM = false,
|
||||||
confidence = 0;
|
confidence = 0;
|
||||||
|
|
||||||
if (limit == 0)
|
if (limit == 0)
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
if (this.getChar(input, 0) == 0x0000FEFF)
|
if (this.getChar(input, 0) == 0x0000FEFF)
|
||||||
hasBOM = true;
|
hasBOM = true;
|
||||||
|
|
||||||
for (var i = 0; i < limit; i += 4) {
|
for (var i = 0; i < limit; i += 4) {
|
||||||
var ch = this.getChar(input, i);
|
var ch = this.getChar(input, i);
|
||||||
|
|
||||||
if (ch < 0 || ch >= 0x10FFFF || (ch >= 0xD800 && ch <= 0xDFFF))
|
if (ch < 0 || ch >= 0x10FFFF || (ch >= 0xD800 && ch <= 0xDFFF))
|
||||||
numInvalid += 1;
|
numInvalid += 1;
|
||||||
else
|
else
|
||||||
numValid += 1;
|
numValid += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cook up some sort of confidence score, based on presence of a BOM
|
// Cook up some sort of confidence score, based on presence of a BOM
|
||||||
// and the existence of valid and/or invalid multi-byte sequences.
|
// and the existence of valid and/or invalid multi-byte sequences.
|
||||||
if (hasBOM && numInvalid == 0) {
|
if (hasBOM && numInvalid == 0) {
|
||||||
confidence = 100;
|
confidence = 100;
|
||||||
} else if (hasBOM && numValid > numInvalid * 10) {
|
} else if (hasBOM && numValid > numInvalid * 10) {
|
||||||
confidence = 80;
|
confidence = 80;
|
||||||
} else if (numValid > 3 && numInvalid == 0) {
|
} else if (numValid > 3 && numInvalid == 0) {
|
||||||
confidence = 100;
|
confidence = 100;
|
||||||
} else if (numValid > 0 && numInvalid == 0) {
|
} else if (numValid > 0 && numInvalid == 0) {
|
||||||
confidence = 80;
|
confidence = 80;
|
||||||
} else if (numValid > numInvalid * 10) {
|
} else if (numValid > numInvalid * 10) {
|
||||||
// Probably corrupt UTF-32BE data. Valid sequences aren't likely by chance.
|
// Probably corrupt UTF-32BE data. Valid sequences aren't likely by chance.
|
||||||
confidence = 25;
|
confidence = 25;
|
||||||
}
|
}
|
||||||
|
|
||||||
// return confidence == 0 ? null : new CharsetMatch(det, this, confidence);
|
// return confidence == 0 ? null : new CharsetMatch(det, this, confidence);
|
||||||
return confidence == 0 ? null : new Match(det, this, confidence);
|
return confidence == 0 ? null : new Match(det, this, confidence);
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports.UTF_32BE = function() {
|
module.exports.UTF_32BE = function() {
|
||||||
this.name = function() {
|
this.name = function() {
|
||||||
return 'UTF-32BE';
|
return 'UTF-32BE';
|
||||||
};
|
};
|
||||||
this.getChar = function(input, index) {
|
this.getChar = function(input, index) {
|
||||||
return (input[index + 0] & 0xff) << 24 | (input[index + 1] & 0xff) << 16 |
|
return (input[index + 0] & 0xff) << 24 | (input[index + 1] & 0xff) << 16 |
|
||||||
(input[index + 2] & 0xff) << 8 | (input[index + 3] & 0xff);
|
(input[index + 2] & 0xff) << 8 | (input[index + 3] & 0xff);
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
util.inherits(module.exports.UTF_32BE, UTF_32);
|
util.inherits(module.exports.UTF_32BE, UTF_32);
|
||||||
|
|
||||||
module.exports.UTF_32LE = function() {
|
module.exports.UTF_32LE = function() {
|
||||||
this.name = function() {
|
this.name = function() {
|
||||||
return 'UTF-32LE';
|
return 'UTF-32LE';
|
||||||
};
|
};
|
||||||
this.getChar = function(input, index) {
|
this.getChar = function(input, index) {
|
||||||
return (input[index + 3] & 0xff) << 24 | (input[index + 2] & 0xff) << 16 |
|
return (input[index + 3] & 0xff) << 24 | (input[index + 2] & 0xff) << 16 |
|
||||||
(input[index + 1] & 0xff) << 8 | (input[index + 0] & 0xff);
|
(input[index + 1] & 0xff) << 8 | (input[index + 0] & 0xff);
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
util.inherits(module.exports.UTF_32LE, UTF_32);
|
util.inherits(module.exports.UTF_32LE, UTF_32);
|
||||||
|
|
140
encoding/utf8.js
140
encoding/utf8.js
|
@ -5,80 +5,80 @@ var Match = require ('../match');
|
||||||
* Charset recognizer for UTF-8
|
* Charset recognizer for UTF-8
|
||||||
*/
|
*/
|
||||||
module.exports = function() {
|
module.exports = function() {
|
||||||
this.name = function() {
|
this.name = function() {
|
||||||
return 'UTF-8';
|
return 'UTF-8';
|
||||||
};
|
};
|
||||||
this.match = function(det) {
|
this.match = function(det) {
|
||||||
|
|
||||||
var hasBOM = false,
|
var hasBOM = false,
|
||||||
numValid = 0,
|
numValid = 0,
|
||||||
numInvalid = 0,
|
numInvalid = 0,
|
||||||
input = det.fRawInput,
|
input = det.fRawInput,
|
||||||
trailBytes = 0,
|
trailBytes = 0,
|
||||||
confidence;
|
confidence;
|
||||||
|
|
||||||
if (det.fRawLength >= 3 &&
|
if (det.fRawLength >= 3 &&
|
||||||
(input[0] & 0xff) == 0xef && (input[1] & 0xff) == 0xbb && (input[2] & 0xff) == 0xbf) {
|
(input[0] & 0xff) == 0xef && (input[1] & 0xff) == 0xbb && (input[2] & 0xff) == 0xbf) {
|
||||||
hasBOM = true;
|
hasBOM = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Scan for multi-byte sequences
|
||||||
|
for (var i = 0; i < det.fRawLength; i++) {
|
||||||
|
var b = input[i];
|
||||||
|
if ((b & 0x80) == 0)
|
||||||
|
continue; // ASCII
|
||||||
|
|
||||||
|
// Hi bit on char found. Figure out how long the sequence should be
|
||||||
|
if ((b & 0x0e0) == 0x0c0) {
|
||||||
|
trailBytes = 1;
|
||||||
|
} else if ((b & 0x0f0) == 0x0e0) {
|
||||||
|
trailBytes = 2;
|
||||||
|
} else if ((b & 0x0f8) == 0xf0) {
|
||||||
|
trailBytes = 3;
|
||||||
|
} else {
|
||||||
|
numInvalid++;
|
||||||
|
if (numInvalid > 5)
|
||||||
|
break;
|
||||||
|
trailBytes = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify that we've got the right number of trail bytes in the sequence
|
||||||
|
for (;;) {
|
||||||
|
i++;
|
||||||
|
if (i >= det.fRawLength)
|
||||||
|
break;
|
||||||
|
|
||||||
|
if ((input[i] & 0xc0) != 0x080) {
|
||||||
|
numInvalid++;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
if (--trailBytes == 0) {
|
||||||
// Scan for multi-byte sequences
|
numValid++;
|
||||||
for (var i = 0; i < det.fRawLength; i++) {
|
break;
|
||||||
var b = input[i];
|
|
||||||
if ((b & 0x80) == 0)
|
|
||||||
continue; // ASCII
|
|
||||||
|
|
||||||
// Hi bit on char found. Figure out how long the sequence should be
|
|
||||||
if ((b & 0x0e0) == 0x0c0) {
|
|
||||||
trailBytes = 1;
|
|
||||||
} else if ((b & 0x0f0) == 0x0e0) {
|
|
||||||
trailBytes = 2;
|
|
||||||
} else if ((b & 0x0f8) == 0xf0) {
|
|
||||||
trailBytes = 3;
|
|
||||||
} else {
|
|
||||||
numInvalid++;
|
|
||||||
if (numInvalid > 5)
|
|
||||||
break;
|
|
||||||
trailBytes = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Verify that we've got the right number of trail bytes in the sequence
|
|
||||||
for (;;) {
|
|
||||||
i++;
|
|
||||||
if (i >= det.fRawLength)
|
|
||||||
break;
|
|
||||||
|
|
||||||
if ((input[i] & 0xc0) != 0x080) {
|
|
||||||
numInvalid++;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (--trailBytes == 0) {
|
|
||||||
numValid++;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Cook up some sort of confidence score, based on presense of a BOM
|
// Cook up some sort of confidence score, based on presense of a BOM
|
||||||
// and the existence of valid and/or invalid multi-byte sequences.
|
// and the existence of valid and/or invalid multi-byte sequences.
|
||||||
confidence = 0;
|
confidence = 0;
|
||||||
if (hasBOM && numInvalid == 0)
|
if (hasBOM && numInvalid == 0)
|
||||||
confidence = 100;
|
confidence = 100;
|
||||||
else if (hasBOM && numValid > numInvalid * 10)
|
else if (hasBOM && numValid > numInvalid * 10)
|
||||||
confidence = 80;
|
confidence = 80;
|
||||||
else if (numValid > 3 && numInvalid == 0)
|
else if (numValid > 3 && numInvalid == 0)
|
||||||
confidence = 100;
|
confidence = 100;
|
||||||
else if (numValid > 0 && numInvalid == 0)
|
else if (numValid > 0 && numInvalid == 0)
|
||||||
confidence = 80;
|
confidence = 80;
|
||||||
else if (numValid == 0 && numInvalid == 0)
|
else if (numValid == 0 && numInvalid == 0)
|
||||||
// Plain ASCII.
|
// Plain ASCII.
|
||||||
confidence = 10;
|
confidence = 10;
|
||||||
else if (numValid > numInvalid * 10)
|
else if (numValid > numInvalid * 10)
|
||||||
// Probably corruput utf-8 data. Valid sequences aren't likely by chance.
|
// Probably corruput utf-8 data. Valid sequences aren't likely by chance.
|
||||||
confidence = 25;
|
confidence = 25;
|
||||||
else
|
else
|
||||||
return null
|
return null
|
||||||
|
|
||||||
return new Match(det, this, confidence);
|
return new Match(det, this, confidence);
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue