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.

string_decoder.js 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. // Copyright Joyent, Inc. and other Node contributors.
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a
  4. // copy of this software and associated documentation files (the
  5. // "Software"), to deal in the Software without restriction, including
  6. // without limitation the rights to use, copy, modify, merge, publish,
  7. // distribute, sublicense, and/or sell copies of the Software, and to permit
  8. // persons to whom the Software is furnished to do so, subject to the
  9. // following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included
  12. // in all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  15. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  17. // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  18. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  19. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  20. // USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. 'use strict';
  22. /*<replacement>*/
  23. var Buffer = require('safe-buffer').Buffer;
  24. /*</replacement>*/
  25. var isEncoding = Buffer.isEncoding || function (encoding) {
  26. encoding = '' + encoding;
  27. switch (encoding && encoding.toLowerCase()) {
  28. case 'hex':case 'utf8':case 'utf-8':case 'ascii':case 'binary':case 'base64':case 'ucs2':case 'ucs-2':case 'utf16le':case 'utf-16le':case 'raw':
  29. return true;
  30. default:
  31. return false;
  32. }
  33. };
  34. function _normalizeEncoding(enc) {
  35. if (!enc) return 'utf8';
  36. var retried;
  37. while (true) {
  38. switch (enc) {
  39. case 'utf8':
  40. case 'utf-8':
  41. return 'utf8';
  42. case 'ucs2':
  43. case 'ucs-2':
  44. case 'utf16le':
  45. case 'utf-16le':
  46. return 'utf16le';
  47. case 'latin1':
  48. case 'binary':
  49. return 'latin1';
  50. case 'base64':
  51. case 'ascii':
  52. case 'hex':
  53. return enc;
  54. default:
  55. if (retried) return; // undefined
  56. enc = ('' + enc).toLowerCase();
  57. retried = true;
  58. }
  59. }
  60. };
  61. // Do not cache `Buffer.isEncoding` when checking encoding names as some
  62. // modules monkey-patch it to support additional encodings
  63. function normalizeEncoding(enc) {
  64. var nenc = _normalizeEncoding(enc);
  65. if (typeof nenc !== 'string' && (Buffer.isEncoding === isEncoding || !isEncoding(enc))) throw new Error('Unknown encoding: ' + enc);
  66. return nenc || enc;
  67. }
  68. // StringDecoder provides an interface for efficiently splitting a series of
  69. // buffers into a series of JS strings without breaking apart multi-byte
  70. // characters.
  71. exports.StringDecoder = StringDecoder;
  72. function StringDecoder(encoding) {
  73. this.encoding = normalizeEncoding(encoding);
  74. var nb;
  75. switch (this.encoding) {
  76. case 'utf16le':
  77. this.text = utf16Text;
  78. this.end = utf16End;
  79. nb = 4;
  80. break;
  81. case 'utf8':
  82. this.fillLast = utf8FillLast;
  83. nb = 4;
  84. break;
  85. case 'base64':
  86. this.text = base64Text;
  87. this.end = base64End;
  88. nb = 3;
  89. break;
  90. default:
  91. this.write = simpleWrite;
  92. this.end = simpleEnd;
  93. return;
  94. }
  95. this.lastNeed = 0;
  96. this.lastTotal = 0;
  97. this.lastChar = Buffer.allocUnsafe(nb);
  98. }
  99. StringDecoder.prototype.write = function (buf) {
  100. if (buf.length === 0) return '';
  101. var r;
  102. var i;
  103. if (this.lastNeed) {
  104. r = this.fillLast(buf);
  105. if (r === undefined) return '';
  106. i = this.lastNeed;
  107. this.lastNeed = 0;
  108. } else {
  109. i = 0;
  110. }
  111. if (i < buf.length) return r ? r + this.text(buf, i) : this.text(buf, i);
  112. return r || '';
  113. };
  114. StringDecoder.prototype.end = utf8End;
  115. // Returns only complete characters in a Buffer
  116. StringDecoder.prototype.text = utf8Text;
  117. // Attempts to complete a partial non-UTF-8 character using bytes from a Buffer
  118. StringDecoder.prototype.fillLast = function (buf) {
  119. if (this.lastNeed <= buf.length) {
  120. buf.copy(this.lastChar, this.lastTotal - this.lastNeed, 0, this.lastNeed);
  121. return this.lastChar.toString(this.encoding, 0, this.lastTotal);
  122. }
  123. buf.copy(this.lastChar, this.lastTotal - this.lastNeed, 0, buf.length);
  124. this.lastNeed -= buf.length;
  125. };
  126. // Checks the type of a UTF-8 byte, whether it's ASCII, a leading byte, or a
  127. // continuation byte. If an invalid byte is detected, -2 is returned.
  128. function utf8CheckByte(byte) {
  129. if (byte <= 0x7F) return 0;else if (byte >> 5 === 0x06) return 2;else if (byte >> 4 === 0x0E) return 3;else if (byte >> 3 === 0x1E) return 4;
  130. return byte >> 6 === 0x02 ? -1 : -2;
  131. }
  132. // Checks at most 3 bytes at the end of a Buffer in order to detect an
  133. // incomplete multi-byte UTF-8 character. The total number of bytes (2, 3, or 4)
  134. // needed to complete the UTF-8 character (if applicable) are returned.
  135. function utf8CheckIncomplete(self, buf, i) {
  136. var j = buf.length - 1;
  137. if (j < i) return 0;
  138. var nb = utf8CheckByte(buf[j]);
  139. if (nb >= 0) {
  140. if (nb > 0) self.lastNeed = nb - 1;
  141. return nb;
  142. }
  143. if (--j < i || nb === -2) return 0;
  144. nb = utf8CheckByte(buf[j]);
  145. if (nb >= 0) {
  146. if (nb > 0) self.lastNeed = nb - 2;
  147. return nb;
  148. }
  149. if (--j < i || nb === -2) return 0;
  150. nb = utf8CheckByte(buf[j]);
  151. if (nb >= 0) {
  152. if (nb > 0) {
  153. if (nb === 2) nb = 0;else self.lastNeed = nb - 3;
  154. }
  155. return nb;
  156. }
  157. return 0;
  158. }
  159. // Validates as many continuation bytes for a multi-byte UTF-8 character as
  160. // needed or are available. If we see a non-continuation byte where we expect
  161. // one, we "replace" the validated continuation bytes we've seen so far with
  162. // a single UTF-8 replacement character ('\ufffd'), to match v8's UTF-8 decoding
  163. // behavior. The continuation byte check is included three times in the case
  164. // where all of the continuation bytes for a character exist in the same buffer.
  165. // It is also done this way as a slight performance increase instead of using a
  166. // loop.
  167. function utf8CheckExtraBytes(self, buf, p) {
  168. if ((buf[0] & 0xC0) !== 0x80) {
  169. self.lastNeed = 0;
  170. return '\ufffd';
  171. }
  172. if (self.lastNeed > 1 && buf.length > 1) {
  173. if ((buf[1] & 0xC0) !== 0x80) {
  174. self.lastNeed = 1;
  175. return '\ufffd';
  176. }
  177. if (self.lastNeed > 2 && buf.length > 2) {
  178. if ((buf[2] & 0xC0) !== 0x80) {
  179. self.lastNeed = 2;
  180. return '\ufffd';
  181. }
  182. }
  183. }
  184. }
  185. // Attempts to complete a multi-byte UTF-8 character using bytes from a Buffer.
  186. function utf8FillLast(buf) {
  187. var p = this.lastTotal - this.lastNeed;
  188. var r = utf8CheckExtraBytes(this, buf, p);
  189. if (r !== undefined) return r;
  190. if (this.lastNeed <= buf.length) {
  191. buf.copy(this.lastChar, p, 0, this.lastNeed);
  192. return this.lastChar.toString(this.encoding, 0, this.lastTotal);
  193. }
  194. buf.copy(this.lastChar, p, 0, buf.length);
  195. this.lastNeed -= buf.length;
  196. }
  197. // Returns all complete UTF-8 characters in a Buffer. If the Buffer ended on a
  198. // partial character, the character's bytes are buffered until the required
  199. // number of bytes are available.
  200. function utf8Text(buf, i) {
  201. var total = utf8CheckIncomplete(this, buf, i);
  202. if (!this.lastNeed) return buf.toString('utf8', i);
  203. this.lastTotal = total;
  204. var end = buf.length - (total - this.lastNeed);
  205. buf.copy(this.lastChar, 0, end);
  206. return buf.toString('utf8', i, end);
  207. }
  208. // For UTF-8, a replacement character is added when ending on a partial
  209. // character.
  210. function utf8End(buf) {
  211. var r = buf && buf.length ? this.write(buf) : '';
  212. if (this.lastNeed) return r + '\ufffd';
  213. return r;
  214. }
  215. // UTF-16LE typically needs two bytes per character, but even if we have an even
  216. // number of bytes available, we need to check if we end on a leading/high
  217. // surrogate. In that case, we need to wait for the next two bytes in order to
  218. // decode the last character properly.
  219. function utf16Text(buf, i) {
  220. if ((buf.length - i) % 2 === 0) {
  221. var r = buf.toString('utf16le', i);
  222. if (r) {
  223. var c = r.charCodeAt(r.length - 1);
  224. if (c >= 0xD800 && c <= 0xDBFF) {
  225. this.lastNeed = 2;
  226. this.lastTotal = 4;
  227. this.lastChar[0] = buf[buf.length - 2];
  228. this.lastChar[1] = buf[buf.length - 1];
  229. return r.slice(0, -1);
  230. }
  231. }
  232. return r;
  233. }
  234. this.lastNeed = 1;
  235. this.lastTotal = 2;
  236. this.lastChar[0] = buf[buf.length - 1];
  237. return buf.toString('utf16le', i, buf.length - 1);
  238. }
  239. // For UTF-16LE we do not explicitly append special replacement characters if we
  240. // end on a partial character, we simply let v8 handle that.
  241. function utf16End(buf) {
  242. var r = buf && buf.length ? this.write(buf) : '';
  243. if (this.lastNeed) {
  244. var end = this.lastTotal - this.lastNeed;
  245. return r + this.lastChar.toString('utf16le', 0, end);
  246. }
  247. return r;
  248. }
  249. function base64Text(buf, i) {
  250. var n = (buf.length - i) % 3;
  251. if (n === 0) return buf.toString('base64', i);
  252. this.lastNeed = 3 - n;
  253. this.lastTotal = 3;
  254. if (n === 1) {
  255. this.lastChar[0] = buf[buf.length - 1];
  256. } else {
  257. this.lastChar[0] = buf[buf.length - 2];
  258. this.lastChar[1] = buf[buf.length - 1];
  259. }
  260. return buf.toString('base64', i, buf.length - n);
  261. }
  262. function base64End(buf) {
  263. var r = buf && buf.length ? this.write(buf) : '';
  264. if (this.lastNeed) return r + this.lastChar.toString('base64', 0, 3 - this.lastNeed);
  265. return r;
  266. }
  267. // Pass bytes on through for single-byte encodings (e.g. ascii, latin1, hex)
  268. function simpleWrite(buf) {
  269. return buf.toString(this.encoding);
  270. }
  271. function simpleEnd(buf) {
  272. return buf && buf.length ? this.write(buf) : '';
  273. }