utf8.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. var Match = require ('../match');
  2. /**
  3. * Charset recognizer for UTF-8
  4. */
  5. module.exports = function() {
  6. this.name = function() {
  7. return 'UTF-8';
  8. };
  9. this.match = function(det) {
  10. var hasBOM = false,
  11. numValid = 0,
  12. numInvalid = 0,
  13. input = det.fRawInput,
  14. trailBytes = 0,
  15. confidence;
  16. if (det.fRawLength >= 3 &&
  17. (input[0] & 0xff) == 0xef && (input[1] & 0xff) == 0xbb && (input[2] & 0xff) == 0xbf) {
  18. hasBOM = true;
  19. }
  20. // Scan for multi-byte sequences
  21. for (var i = 0; i < det.fRawLength; i++) {
  22. var b = input[i];
  23. if ((b & 0x80) == 0)
  24. continue; // ASCII
  25. // Hi bit on char found. Figure out how long the sequence should be
  26. if ((b & 0x0e0) == 0x0c0) {
  27. trailBytes = 1;
  28. } else if ((b & 0x0f0) == 0x0e0) {
  29. trailBytes = 2;
  30. } else if ((b & 0x0f8) == 0xf0) {
  31. trailBytes = 3;
  32. } else {
  33. numInvalid++;
  34. if (numInvalid > 5)
  35. break;
  36. trailBytes = 0;
  37. }
  38. // Verify that we've got the right number of trail bytes in the sequence
  39. for (;;) {
  40. i++;
  41. if (i >= det.fRawLength)
  42. break;
  43. if ((input[i] & 0xc0) != 0x080) {
  44. numInvalid++;
  45. break;
  46. }
  47. if (--trailBytes == 0) {
  48. numValid++;
  49. break;
  50. }
  51. }
  52. }
  53. // Cook up some sort of confidence score, based on presense of a BOM
  54. // and the existence of valid and/or invalid multi-byte sequences.
  55. confidence = 0;
  56. if (hasBOM && numInvalid == 0)
  57. confidence = 100;
  58. else if (hasBOM && numValid > numInvalid * 10)
  59. confidence = 80;
  60. else if (numValid > 3 && numInvalid == 0)
  61. confidence = 100;
  62. else if (numValid > 0 && numInvalid == 0)
  63. confidence = 80;
  64. else if (numValid == 0 && numInvalid == 0)
  65. // Plain ASCII.
  66. confidence = 10;
  67. else if (numValid > numInvalid * 10)
  68. // Probably corruput utf-8 data. Valid sequences aren't likely by chance.
  69. confidence = 25;
  70. else
  71. return null
  72. return new Match(det, this, confidence);
  73. };
  74. };