unicode.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. 'use strict';
  2. var util = require('util'),
  3. Match = require ('../match');
  4. /**
  5. * This class matches UTF-16 and UTF-32, both big- and little-endian. The
  6. * BOM will be used if it is present.
  7. */
  8. module.exports.UTF_16BE = function() {
  9. this.name = function() {
  10. return 'UTF-16BE';
  11. };
  12. this.match = function(det) {
  13. var input = det.fRawInput;
  14. if (input.length >= 2 && ((input[0] & 0xff) == 0xfe && (input[1] & 0xff) == 0xff)) {
  15. return new Match(det, this, 100); // confidence = 100
  16. }
  17. // TODO: Do some statistics to check for unsigned UTF-16BE
  18. return null;
  19. };
  20. };
  21. module.exports.UTF_16LE = function() {
  22. this.name = function() {
  23. return 'UTF-16LE';
  24. };
  25. this.match = function(det) {
  26. var input = det.fRawInput;
  27. if (input.length >= 2 && ((input[0] & 0xff) == 0xff && (input[1] & 0xff) == 0xfe)) {
  28. // LE BOM is present.
  29. if (input.length >= 4 && input[2] == 0x00 && input[3] == 0x00) {
  30. // It is probably UTF-32 LE, not UTF-16
  31. return null;
  32. }
  33. return new Match(det, this, 100); // confidence = 100
  34. }
  35. // TODO: Do some statistics to check for unsigned UTF-16LE
  36. return null;
  37. }
  38. };
  39. function UTF_32() {};
  40. UTF_32.prototype.match = function(det) {
  41. var input = det.fRawInput,
  42. limit = (det.fRawLength / 4) * 4,
  43. numValid = 0,
  44. numInvalid = 0,
  45. hasBOM = false,
  46. confidence = 0;
  47. if (limit == 0) {
  48. return null;
  49. }
  50. if (this.getChar(input, 0) == 0x0000FEFF) {
  51. hasBOM = true;
  52. }
  53. for (var i = 0; i < limit; i += 4) {
  54. var ch = this.getChar(input, i);
  55. if (ch < 0 || ch >= 0x10FFFF || (ch >= 0xD800 && ch <= 0xDFFF)) {
  56. numInvalid += 1;
  57. } else {
  58. numValid += 1;
  59. }
  60. }
  61. // Cook up some sort of confidence score, based on presence of a BOM
  62. // and the existence of valid and/or invalid multi-byte sequences.
  63. if (hasBOM && numInvalid == 0) {
  64. confidence = 100;
  65. } else if (hasBOM && numValid > numInvalid * 10) {
  66. confidence = 80;
  67. } else if (numValid > 3 && numInvalid == 0) {
  68. confidence = 100;
  69. } else if (numValid > 0 && numInvalid == 0) {
  70. confidence = 80;
  71. } else if (numValid > numInvalid * 10) {
  72. // Probably corrupt UTF-32BE data. Valid sequences aren't likely by chance.
  73. confidence = 25;
  74. }
  75. // return confidence == 0 ? null : new CharsetMatch(det, this, confidence);
  76. return confidence == 0 ? null : new Match(det, this, confidence);
  77. };
  78. module.exports.UTF_32BE = function() {
  79. this.name = function() {
  80. return 'UTF-32BE';
  81. };
  82. this.getChar = function(input, index) {
  83. return (input[index + 0] & 0xff) << 24 | (input[index + 1] & 0xff) << 16 |
  84. (input[index + 2] & 0xff) << 8 | (input[index + 3] & 0xff);
  85. };
  86. };
  87. util.inherits(module.exports.UTF_32BE, UTF_32);
  88. module.exports.UTF_32LE = function() {
  89. this.name = function() {
  90. return 'UTF-32LE';
  91. };
  92. this.getChar = function(input, index) {
  93. return (input[index + 3] & 0xff) << 24 | (input[index + 2] & 0xff) << 16 |
  94. (input[index + 1] & 0xff) << 8 | (input[index + 0] & 0xff);
  95. };
  96. };
  97. util.inherits(module.exports.UTF_32LE, UTF_32);