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.

utf32.js 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. 'use strict';
  2. var Buffer = require('safer-buffer').Buffer;
  3. // == UTF32-LE/BE codec. ==========================================================
  4. exports._utf32 = Utf32Codec;
  5. function Utf32Codec(codecOptions, iconv) {
  6. this.iconv = iconv;
  7. this.bomAware = true;
  8. this.isLE = codecOptions.isLE;
  9. }
  10. exports.utf32le = { type: '_utf32', isLE: true };
  11. exports.utf32be = { type: '_utf32', isLE: false };
  12. // Aliases
  13. exports.ucs4le = 'utf32le';
  14. exports.ucs4be = 'utf32be';
  15. Utf32Codec.prototype.encoder = Utf32Encoder;
  16. Utf32Codec.prototype.decoder = Utf32Decoder;
  17. // -- Encoding
  18. function Utf32Encoder(options, codec) {
  19. this.isLE = codec.isLE;
  20. this.highSurrogate = 0;
  21. }
  22. Utf32Encoder.prototype.write = function(str) {
  23. var src = Buffer.from(str, 'ucs2');
  24. var dst = Buffer.alloc(src.length * 2);
  25. var write32 = this.isLE ? dst.writeUInt32LE : dst.writeUInt32BE;
  26. var offset = 0;
  27. for (var i = 0; i < src.length; i += 2) {
  28. var code = src.readUInt16LE(i);
  29. var isHighSurrogate = (0xD800 <= code && code < 0xDC00);
  30. var isLowSurrogate = (0xDC00 <= code && code < 0xE000);
  31. if (this.highSurrogate) {
  32. if (isHighSurrogate || !isLowSurrogate) {
  33. // There shouldn't be two high surrogates in a row, nor a high surrogate which isn't followed by a low
  34. // surrogate. If this happens, keep the pending high surrogate as a stand-alone semi-invalid character
  35. // (technically wrong, but expected by some applications, like Windows file names).
  36. write32.call(dst, this.highSurrogate, offset);
  37. offset += 4;
  38. }
  39. else {
  40. // Create 32-bit value from high and low surrogates;
  41. var codepoint = (((this.highSurrogate - 0xD800) << 10) | (code - 0xDC00)) + 0x10000;
  42. write32.call(dst, codepoint, offset);
  43. offset += 4;
  44. this.highSurrogate = 0;
  45. continue;
  46. }
  47. }
  48. if (isHighSurrogate)
  49. this.highSurrogate = code;
  50. else {
  51. // Even if the current character is a low surrogate, with no previous high surrogate, we'll
  52. // encode it as a semi-invalid stand-alone character for the same reasons expressed above for
  53. // unpaired high surrogates.
  54. write32.call(dst, code, offset);
  55. offset += 4;
  56. this.highSurrogate = 0;
  57. }
  58. }
  59. if (offset < dst.length)
  60. dst = dst.slice(0, offset);
  61. return dst;
  62. };
  63. Utf32Encoder.prototype.end = function() {
  64. // Treat any leftover high surrogate as a semi-valid independent character.
  65. if (!this.highSurrogate)
  66. return;
  67. var buf = Buffer.alloc(4);
  68. if (this.isLE)
  69. buf.writeUInt32LE(this.highSurrogate, 0);
  70. else
  71. buf.writeUInt32BE(this.highSurrogate, 0);
  72. this.highSurrogate = 0;
  73. return buf;
  74. };
  75. // -- Decoding
  76. function Utf32Decoder(options, codec) {
  77. this.isLE = codec.isLE;
  78. this.badChar = codec.iconv.defaultCharUnicode.charCodeAt(0);
  79. this.overflow = [];
  80. }
  81. Utf32Decoder.prototype.write = function(src) {
  82. if (src.length === 0)
  83. return '';
  84. var i = 0;
  85. var codepoint = 0;
  86. var dst = Buffer.alloc(src.length + 4);
  87. var offset = 0;
  88. var isLE = this.isLE;
  89. var overflow = this.overflow;
  90. var badChar = this.badChar;
  91. if (overflow.length > 0) {
  92. for (; i < src.length && overflow.length < 4; i++)
  93. overflow.push(src[i]);
  94. if (overflow.length === 4) {
  95. // NOTE: codepoint is a signed int32 and can be negative.
  96. // NOTE: We copied this block from below to help V8 optimize it (it works with array, not buffer).
  97. if (isLE) {
  98. codepoint = overflow[i] | (overflow[i+1] << 8) | (overflow[i+2] << 16) | (overflow[i+3] << 24);
  99. } else {
  100. codepoint = overflow[i+3] | (overflow[i+2] << 8) | (overflow[i+1] << 16) | (overflow[i] << 24);
  101. }
  102. overflow.length = 0;
  103. offset = _writeCodepoint(dst, offset, codepoint, badChar);
  104. }
  105. }
  106. // Main loop. Should be as optimized as possible.
  107. for (; i < src.length - 3; i += 4) {
  108. // NOTE: codepoint is a signed int32 and can be negative.
  109. if (isLE) {
  110. codepoint = src[i] | (src[i+1] << 8) | (src[i+2] << 16) | (src[i+3] << 24);
  111. } else {
  112. codepoint = src[i+3] | (src[i+2] << 8) | (src[i+1] << 16) | (src[i] << 24);
  113. }
  114. offset = _writeCodepoint(dst, offset, codepoint, badChar);
  115. }
  116. // Keep overflowing bytes.
  117. for (; i < src.length; i++) {
  118. overflow.push(src[i]);
  119. }
  120. return dst.slice(0, offset).toString('ucs2');
  121. };
  122. function _writeCodepoint(dst, offset, codepoint, badChar) {
  123. // NOTE: codepoint is signed int32 and can be negative. We keep it that way to help V8 with optimizations.
  124. if (codepoint < 0 || codepoint > 0x10FFFF) {
  125. // Not a valid Unicode codepoint
  126. codepoint = badChar;
  127. }
  128. // Ephemeral Planes: Write high surrogate.
  129. if (codepoint >= 0x10000) {
  130. codepoint -= 0x10000;
  131. var high = 0xD800 | (codepoint >> 10);
  132. dst[offset++] = high & 0xff;
  133. dst[offset++] = high >> 8;
  134. // Low surrogate is written below.
  135. var codepoint = 0xDC00 | (codepoint & 0x3FF);
  136. }
  137. // Write BMP char or low surrogate.
  138. dst[offset++] = codepoint & 0xff;
  139. dst[offset++] = codepoint >> 8;
  140. return offset;
  141. };
  142. Utf32Decoder.prototype.end = function() {
  143. this.overflow.length = 0;
  144. };
  145. // == UTF-32 Auto codec =============================================================
  146. // Decoder chooses automatically from UTF-32LE and UTF-32BE using BOM and space-based heuristic.
  147. // Defaults to UTF-32LE. http://en.wikipedia.org/wiki/UTF-32
  148. // Encoder/decoder default can be changed: iconv.decode(buf, 'utf32', {defaultEncoding: 'utf-32be'});
  149. // Encoder prepends BOM (which can be overridden with (addBOM: false}).
  150. exports.utf32 = Utf32AutoCodec;
  151. exports.ucs4 = 'utf32';
  152. function Utf32AutoCodec(options, iconv) {
  153. this.iconv = iconv;
  154. }
  155. Utf32AutoCodec.prototype.encoder = Utf32AutoEncoder;
  156. Utf32AutoCodec.prototype.decoder = Utf32AutoDecoder;
  157. // -- Encoding
  158. function Utf32AutoEncoder(options, codec) {
  159. options = options || {};
  160. if (options.addBOM === undefined)
  161. options.addBOM = true;
  162. this.encoder = codec.iconv.getEncoder(options.defaultEncoding || 'utf-32le', options);
  163. }
  164. Utf32AutoEncoder.prototype.write = function(str) {
  165. return this.encoder.write(str);
  166. };
  167. Utf32AutoEncoder.prototype.end = function() {
  168. return this.encoder.end();
  169. };
  170. // -- Decoding
  171. function Utf32AutoDecoder(options, codec) {
  172. this.decoder = null;
  173. this.initialBufs = [];
  174. this.initialBufsLen = 0;
  175. this.options = options || {};
  176. this.iconv = codec.iconv;
  177. }
  178. Utf32AutoDecoder.prototype.write = function(buf) {
  179. if (!this.decoder) {
  180. // Codec is not chosen yet. Accumulate initial bytes.
  181. this.initialBufs.push(buf);
  182. this.initialBufsLen += buf.length;
  183. if (this.initialBufsLen < 32) // We need more bytes to use space heuristic (see below)
  184. return '';
  185. // We have enough bytes -> detect endianness.
  186. var encoding = detectEncoding(this.initialBufs, this.options.defaultEncoding);
  187. this.decoder = this.iconv.getDecoder(encoding, this.options);
  188. var resStr = '';
  189. for (var i = 0; i < this.initialBufs.length; i++)
  190. resStr += this.decoder.write(this.initialBufs[i]);
  191. this.initialBufs.length = this.initialBufsLen = 0;
  192. return resStr;
  193. }
  194. return this.decoder.write(buf);
  195. };
  196. Utf32AutoDecoder.prototype.end = function() {
  197. if (!this.decoder) {
  198. var encoding = detectEncoding(this.initialBufs, this.options.defaultEncoding);
  199. this.decoder = this.iconv.getDecoder(encoding, this.options);
  200. var resStr = '';
  201. for (var i = 0; i < this.initialBufs.length; i++)
  202. resStr += this.decoder.write(this.initialBufs[i]);
  203. var trail = this.decoder.end();
  204. if (trail)
  205. resStr += trail;
  206. this.initialBufs.length = this.initialBufsLen = 0;
  207. return resStr;
  208. }
  209. return this.decoder.end();
  210. };
  211. function detectEncoding(bufs, defaultEncoding) {
  212. var b = [];
  213. var charsProcessed = 0;
  214. var invalidLE = 0, invalidBE = 0; // Number of invalid chars when decoded as LE or BE.
  215. var bmpCharsLE = 0, bmpCharsBE = 0; // Number of BMP chars when decoded as LE or BE.
  216. outer_loop:
  217. for (var i = 0; i < bufs.length; i++) {
  218. var buf = bufs[i];
  219. for (var j = 0; j < buf.length; j++) {
  220. b.push(buf[j]);
  221. if (b.length === 4) {
  222. if (charsProcessed === 0) {
  223. // Check BOM first.
  224. if (b[0] === 0xFF && b[1] === 0xFE && b[2] === 0 && b[3] === 0) {
  225. return 'utf-32le';
  226. }
  227. if (b[0] === 0 && b[1] === 0 && b[2] === 0xFE && b[3] === 0xFF) {
  228. return 'utf-32be';
  229. }
  230. }
  231. if (b[0] !== 0 || b[1] > 0x10) invalidBE++;
  232. if (b[3] !== 0 || b[2] > 0x10) invalidLE++;
  233. if (b[0] === 0 && b[1] === 0 && (b[2] !== 0 || b[3] !== 0)) bmpCharsBE++;
  234. if ((b[0] !== 0 || b[1] !== 0) && b[2] === 0 && b[3] === 0) bmpCharsLE++;
  235. b.length = 0;
  236. charsProcessed++;
  237. if (charsProcessed >= 100) {
  238. break outer_loop;
  239. }
  240. }
  241. }
  242. }
  243. // Make decisions.
  244. if (bmpCharsBE - invalidBE > bmpCharsLE - invalidLE) return 'utf-32be';
  245. if (bmpCharsBE - invalidBE < bmpCharsLE - invalidLE) return 'utf-32le';
  246. // Couldn't decide (likely all zeros or not enough data).
  247. return defaultEncoding || 'utf-32le';
  248. }