No Description

utf8.js 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. 'use strict';
  2. var utils = require('./utils');
  3. var support = require('./support');
  4. var nodejsUtils = require('./nodejsUtils');
  5. var GenericWorker = require('./stream/GenericWorker');
  6. /**
  7. * The following functions come from pako, from pako/lib/utils/strings
  8. * released under the MIT license, see pako https://github.com/nodeca/pako/
  9. */
  10. // Table with utf8 lengths (calculated by first byte of sequence)
  11. // Note, that 5 & 6-byte values and some 4-byte values can not be represented in JS,
  12. // because max possible codepoint is 0x10ffff
  13. var _utf8len = new Array(256);
  14. for (var i=0; i<256; i++) {
  15. _utf8len[i] = (i >= 252 ? 6 : i >= 248 ? 5 : i >= 240 ? 4 : i >= 224 ? 3 : i >= 192 ? 2 : 1);
  16. }
  17. _utf8len[254]=_utf8len[254]=1; // Invalid sequence start
  18. // convert string to array (typed, when possible)
  19. var string2buf = function (str) {
  20. var buf, c, c2, m_pos, i, str_len = str.length, buf_len = 0;
  21. // count binary size
  22. for (m_pos = 0; m_pos < str_len; m_pos++) {
  23. c = str.charCodeAt(m_pos);
  24. if ((c & 0xfc00) === 0xd800 && (m_pos+1 < str_len)) {
  25. c2 = str.charCodeAt(m_pos+1);
  26. if ((c2 & 0xfc00) === 0xdc00) {
  27. c = 0x10000 + ((c - 0xd800) << 10) + (c2 - 0xdc00);
  28. m_pos++;
  29. }
  30. }
  31. buf_len += c < 0x80 ? 1 : c < 0x800 ? 2 : c < 0x10000 ? 3 : 4;
  32. }
  33. // allocate buffer
  34. if (support.uint8array) {
  35. buf = new Uint8Array(buf_len);
  36. } else {
  37. buf = new Array(buf_len);
  38. }
  39. // convert
  40. for (i=0, m_pos = 0; i < buf_len; m_pos++) {
  41. c = str.charCodeAt(m_pos);
  42. if ((c & 0xfc00) === 0xd800 && (m_pos+1 < str_len)) {
  43. c2 = str.charCodeAt(m_pos+1);
  44. if ((c2 & 0xfc00) === 0xdc00) {
  45. c = 0x10000 + ((c - 0xd800) << 10) + (c2 - 0xdc00);
  46. m_pos++;
  47. }
  48. }
  49. if (c < 0x80) {
  50. /* one byte */
  51. buf[i++] = c;
  52. } else if (c < 0x800) {
  53. /* two bytes */
  54. buf[i++] = 0xC0 | (c >>> 6);
  55. buf[i++] = 0x80 | (c & 0x3f);
  56. } else if (c < 0x10000) {
  57. /* three bytes */
  58. buf[i++] = 0xE0 | (c >>> 12);
  59. buf[i++] = 0x80 | (c >>> 6 & 0x3f);
  60. buf[i++] = 0x80 | (c & 0x3f);
  61. } else {
  62. /* four bytes */
  63. buf[i++] = 0xf0 | (c >>> 18);
  64. buf[i++] = 0x80 | (c >>> 12 & 0x3f);
  65. buf[i++] = 0x80 | (c >>> 6 & 0x3f);
  66. buf[i++] = 0x80 | (c & 0x3f);
  67. }
  68. }
  69. return buf;
  70. };
  71. // Calculate max possible position in utf8 buffer,
  72. // that will not break sequence. If that's not possible
  73. // - (very small limits) return max size as is.
  74. //
  75. // buf[] - utf8 bytes array
  76. // max - length limit (mandatory);
  77. var utf8border = function(buf, max) {
  78. var pos;
  79. max = max || buf.length;
  80. if (max > buf.length) { max = buf.length; }
  81. // go back from last position, until start of sequence found
  82. pos = max-1;
  83. while (pos >= 0 && (buf[pos] & 0xC0) === 0x80) { pos--; }
  84. // Fuckup - very small and broken sequence,
  85. // return max, because we should return something anyway.
  86. if (pos < 0) { return max; }
  87. // If we came to start of buffer - that means vuffer is too small,
  88. // return max too.
  89. if (pos === 0) { return max; }
  90. return (pos + _utf8len[buf[pos]] > max) ? pos : max;
  91. };
  92. // convert array to string
  93. var buf2string = function (buf) {
  94. var str, i, out, c, c_len;
  95. var len = buf.length;
  96. // Reserve max possible length (2 words per char)
  97. // NB: by unknown reasons, Array is significantly faster for
  98. // String.fromCharCode.apply than Uint16Array.
  99. var utf16buf = new Array(len*2);
  100. for (out=0, i=0; i<len;) {
  101. c = buf[i++];
  102. // quick process ascii
  103. if (c < 0x80) { utf16buf[out++] = c; continue; }
  104. c_len = _utf8len[c];
  105. // skip 5 & 6 byte codes
  106. if (c_len > 4) { utf16buf[out++] = 0xfffd; i += c_len-1; continue; }
  107. // apply mask on first byte
  108. c &= c_len === 2 ? 0x1f : c_len === 3 ? 0x0f : 0x07;
  109. // join the rest
  110. while (c_len > 1 && i < len) {
  111. c = (c << 6) | (buf[i++] & 0x3f);
  112. c_len--;
  113. }
  114. // terminated by end of string?
  115. if (c_len > 1) { utf16buf[out++] = 0xfffd; continue; }
  116. if (c < 0x10000) {
  117. utf16buf[out++] = c;
  118. } else {
  119. c -= 0x10000;
  120. utf16buf[out++] = 0xd800 | ((c >> 10) & 0x3ff);
  121. utf16buf[out++] = 0xdc00 | (c & 0x3ff);
  122. }
  123. }
  124. // shrinkBuf(utf16buf, out)
  125. if (utf16buf.length !== out) {
  126. if(utf16buf.subarray) {
  127. utf16buf = utf16buf.subarray(0, out);
  128. } else {
  129. utf16buf.length = out;
  130. }
  131. }
  132. // return String.fromCharCode.apply(null, utf16buf);
  133. return utils.applyFromCharCode(utf16buf);
  134. };
  135. // That's all for the pako functions.
  136. /**
  137. * Transform a javascript string into an array (typed if possible) of bytes,
  138. * UTF-8 encoded.
  139. * @param {String} str the string to encode
  140. * @return {Array|Uint8Array|Buffer} the UTF-8 encoded string.
  141. */
  142. exports.utf8encode = function utf8encode(str) {
  143. if (support.nodebuffer) {
  144. return nodejsUtils.newBufferFrom(str, "utf-8");
  145. }
  146. return string2buf(str);
  147. };
  148. /**
  149. * Transform a bytes array (or a representation) representing an UTF-8 encoded
  150. * string into a javascript string.
  151. * @param {Array|Uint8Array|Buffer} buf the data de decode
  152. * @return {String} the decoded string.
  153. */
  154. exports.utf8decode = function utf8decode(buf) {
  155. if (support.nodebuffer) {
  156. return utils.transformTo("nodebuffer", buf).toString("utf-8");
  157. }
  158. buf = utils.transformTo(support.uint8array ? "uint8array" : "array", buf);
  159. return buf2string(buf);
  160. };
  161. /**
  162. * A worker to decode utf8 encoded binary chunks into string chunks.
  163. * @constructor
  164. */
  165. function Utf8DecodeWorker() {
  166. GenericWorker.call(this, "utf-8 decode");
  167. // the last bytes if a chunk didn't end with a complete codepoint.
  168. this.leftOver = null;
  169. }
  170. utils.inherits(Utf8DecodeWorker, GenericWorker);
  171. /**
  172. * @see GenericWorker.processChunk
  173. */
  174. Utf8DecodeWorker.prototype.processChunk = function (chunk) {
  175. var data = utils.transformTo(support.uint8array ? "uint8array" : "array", chunk.data);
  176. // 1st step, re-use what's left of the previous chunk
  177. if (this.leftOver && this.leftOver.length) {
  178. if(support.uint8array) {
  179. var previousData = data;
  180. data = new Uint8Array(previousData.length + this.leftOver.length);
  181. data.set(this.leftOver, 0);
  182. data.set(previousData, this.leftOver.length);
  183. } else {
  184. data = this.leftOver.concat(data);
  185. }
  186. this.leftOver = null;
  187. }
  188. var nextBoundary = utf8border(data);
  189. var usableData = data;
  190. if (nextBoundary !== data.length) {
  191. if (support.uint8array) {
  192. usableData = data.subarray(0, nextBoundary);
  193. this.leftOver = data.subarray(nextBoundary, data.length);
  194. } else {
  195. usableData = data.slice(0, nextBoundary);
  196. this.leftOver = data.slice(nextBoundary, data.length);
  197. }
  198. }
  199. this.push({
  200. data : exports.utf8decode(usableData),
  201. meta : chunk.meta
  202. });
  203. };
  204. /**
  205. * @see GenericWorker.flush
  206. */
  207. Utf8DecodeWorker.prototype.flush = function () {
  208. if(this.leftOver && this.leftOver.length) {
  209. this.push({
  210. data : exports.utf8decode(this.leftOver),
  211. meta : {}
  212. });
  213. this.leftOver = null;
  214. }
  215. };
  216. exports.Utf8DecodeWorker = Utf8DecodeWorker;
  217. /**
  218. * A worker to endcode string chunks into utf8 encoded binary chunks.
  219. * @constructor
  220. */
  221. function Utf8EncodeWorker() {
  222. GenericWorker.call(this, "utf-8 encode");
  223. }
  224. utils.inherits(Utf8EncodeWorker, GenericWorker);
  225. /**
  226. * @see GenericWorker.processChunk
  227. */
  228. Utf8EncodeWorker.prototype.processChunk = function (chunk) {
  229. this.push({
  230. data : exports.utf8encode(chunk.data),
  231. meta : chunk.meta
  232. });
  233. };
  234. exports.Utf8EncodeWorker = Utf8EncodeWorker;