You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

utf16.js 5.4KB

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