utf16.js 5.0 KB

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