utf16.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. "use strict"
  2. // == UTF16-BE codec. ==========================================================
  3. exports.utf16be = Utf16BECodec;
  4. function Utf16BECodec() {
  5. }
  6. Utf16BECodec.prototype.encoder = Utf16BEEncoder;
  7. Utf16BECodec.prototype.decoder = Utf16BEDecoder;
  8. Utf16BECodec.prototype.bomAware = true;
  9. // -- Encoding
  10. function Utf16BEEncoder() {
  11. }
  12. Utf16BEEncoder.prototype.write = function(str) {
  13. var buf = new Buffer(str, 'ucs2');
  14. for (var i = 0; i < buf.length; i += 2) {
  15. var tmp = buf[i]; buf[i] = buf[i+1]; buf[i+1] = tmp;
  16. }
  17. return buf;
  18. }
  19. Utf16BEEncoder.prototype.end = function() {
  20. }
  21. // -- Decoding
  22. function Utf16BEDecoder() {
  23. this.overflowByte = -1;
  24. }
  25. Utf16BEDecoder.prototype.write = function(buf) {
  26. if (buf.length == 0)
  27. return '';
  28. var buf2 = new Buffer(buf.length + 1),
  29. i = 0, j = 0;
  30. if (this.overflowByte !== -1) {
  31. buf2[0] = buf[0];
  32. buf2[1] = this.overflowByte;
  33. i = 1; j = 2;
  34. }
  35. for (; i < buf.length-1; i += 2, j+= 2) {
  36. buf2[j] = buf[i+1];
  37. buf2[j+1] = buf[i];
  38. }
  39. this.overflowByte = (i == buf.length-1) ? buf[buf.length-1] : -1;
  40. return buf2.slice(0, j).toString('ucs2');
  41. }
  42. Utf16BEDecoder.prototype.end = function() {
  43. }
  44. // == UTF-16 codec =============================================================
  45. // Decoder chooses automatically from UTF-16LE and UTF-16BE using BOM and space-based heuristic.
  46. // Defaults to UTF-16LE, as it's prevalent and default in Node.
  47. // http://en.wikipedia.org/wiki/UTF-16 and http://encoding.spec.whatwg.org/#utf-16le
  48. // Decoder default can be changed: iconv.decode(buf, 'utf16', {defaultEncoding: 'utf-16be'});
  49. // Encoder uses UTF-16LE and prepends BOM (which can be overridden with addBOM: false).
  50. exports.utf16 = Utf16Codec;
  51. function Utf16Codec(codecOptions, iconv) {
  52. this.iconv = iconv;
  53. }
  54. Utf16Codec.prototype.encoder = Utf16Encoder;
  55. Utf16Codec.prototype.decoder = Utf16Decoder;
  56. // -- Encoding (pass-through)
  57. function Utf16Encoder(options, codec) {
  58. options = options || {};
  59. if (options.addBOM === undefined)
  60. options.addBOM = true;
  61. this.encoder = codec.iconv.getEncoder('utf-16le', options);
  62. }
  63. Utf16Encoder.prototype.write = function(str) {
  64. return this.encoder.write(str);
  65. }
  66. Utf16Encoder.prototype.end = function() {
  67. return this.encoder.end();
  68. }
  69. // -- Decoding
  70. function Utf16Decoder(options, codec) {
  71. this.decoder = null;
  72. this.initialBytes = [];
  73. this.initialBytesLen = 0;
  74. this.options = options || {};
  75. this.iconv = codec.iconv;
  76. }
  77. Utf16Decoder.prototype.write = function(buf) {
  78. if (!this.decoder) {
  79. // Codec is not chosen yet. Accumulate initial bytes.
  80. this.initialBytes.push(buf);
  81. this.initialBytesLen += buf.length;
  82. if (this.initialBytesLen < 16) // We need more bytes to use space heuristic (see below)
  83. return '';
  84. // We have enough bytes -> detect endianness.
  85. var buf = Buffer.concat(this.initialBytes),
  86. encoding = detectEncoding(buf, this.options.defaultEncoding);
  87. this.decoder = this.iconv.getDecoder(encoding, this.options);
  88. this.initialBytes.length = this.initialBytesLen = 0;
  89. }
  90. return this.decoder.write(buf);
  91. }
  92. Utf16Decoder.prototype.end = function() {
  93. if (!this.decoder) {
  94. var buf = Buffer.concat(this.initialBytes),
  95. encoding = detectEncoding(buf, this.options.defaultEncoding);
  96. this.decoder = this.iconv.getDecoder(encoding, this.options);
  97. var res = this.decoder.write(buf),
  98. trail = this.decoder.end();
  99. return trail ? (res + trail) : res;
  100. }
  101. return this.decoder.end();
  102. }
  103. function detectEncoding(buf, defaultEncoding) {
  104. var enc = defaultEncoding || 'utf-16le';
  105. if (buf.length >= 2) {
  106. // Check BOM.
  107. if (buf[0] == 0xFE && buf[1] == 0xFF) // UTF-16BE BOM
  108. enc = 'utf-16be';
  109. else if (buf[0] == 0xFF && buf[1] == 0xFE) // UTF-16LE BOM
  110. enc = 'utf-16le';
  111. else {
  112. // No BOM found. Try to deduce encoding from initial content.
  113. // Most of the time, the content has ASCII chars (U+00**), but the opposite (U+**00) is uncommon.
  114. // So, we count ASCII as if it was LE or BE, and decide from that.
  115. var asciiCharsLE = 0, asciiCharsBE = 0, // Counts of chars in both positions
  116. _len = Math.min(buf.length - (buf.length % 2), 64); // Len is always even.
  117. for (var i = 0; i < _len; i += 2) {
  118. if (buf[i] === 0 && buf[i+1] !== 0) asciiCharsBE++;
  119. if (buf[i] !== 0 && buf[i+1] === 0) asciiCharsLE++;
  120. }
  121. if (asciiCharsBE > asciiCharsLE)
  122. enc = 'utf-16be';
  123. else if (asciiCharsBE < asciiCharsLE)
  124. enc = 'utf-16le';
  125. }
  126. }
  127. return enc;
  128. }