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.

187 lines
5.2 KiB

4 years ago
  1. // String encode/decode helpers
  2. 'use strict';
  3. var utils = require('./common');
  4. // Quick check if we can use fast array to bin string conversion
  5. //
  6. // - apply(Array) can fail on Android 2.2
  7. // - apply(Uint8Array) can fail on iOS 5.1 Safari
  8. //
  9. var STR_APPLY_OK = true;
  10. var STR_APPLY_UIA_OK = true;
  11. try { String.fromCharCode.apply(null, [ 0 ]); } catch (__) { STR_APPLY_OK = false; }
  12. try { String.fromCharCode.apply(null, new Uint8Array(1)); } catch (__) { STR_APPLY_UIA_OK = false; }
  13. // Table with utf8 lengths (calculated by first byte of sequence)
  14. // Note, that 5 & 6-byte values and some 4-byte values can not be represented in JS,
  15. // because max possible codepoint is 0x10ffff
  16. var _utf8len = new utils.Buf8(256);
  17. for (var q = 0; q < 256; q++) {
  18. _utf8len[q] = (q >= 252 ? 6 : q >= 248 ? 5 : q >= 240 ? 4 : q >= 224 ? 3 : q >= 192 ? 2 : 1);
  19. }
  20. _utf8len[254] = _utf8len[254] = 1; // Invalid sequence start
  21. // convert string to array (typed, when possible)
  22. exports.string2buf = function (str) {
  23. var buf, c, c2, m_pos, i, str_len = str.length, buf_len = 0;
  24. // count binary size
  25. for (m_pos = 0; m_pos < str_len; m_pos++) {
  26. c = str.charCodeAt(m_pos);
  27. if ((c & 0xfc00) === 0xd800 && (m_pos + 1 < str_len)) {
  28. c2 = str.charCodeAt(m_pos + 1);
  29. if ((c2 & 0xfc00) === 0xdc00) {
  30. c = 0x10000 + ((c - 0xd800) << 10) + (c2 - 0xdc00);
  31. m_pos++;
  32. }
  33. }
  34. buf_len += c < 0x80 ? 1 : c < 0x800 ? 2 : c < 0x10000 ? 3 : 4;
  35. }
  36. // allocate buffer
  37. buf = new utils.Buf8(buf_len);
  38. // convert
  39. for (i = 0, m_pos = 0; i < buf_len; m_pos++) {
  40. c = str.charCodeAt(m_pos);
  41. if ((c & 0xfc00) === 0xd800 && (m_pos + 1 < str_len)) {
  42. c2 = str.charCodeAt(m_pos + 1);
  43. if ((c2 & 0xfc00) === 0xdc00) {
  44. c = 0x10000 + ((c - 0xd800) << 10) + (c2 - 0xdc00);
  45. m_pos++;
  46. }
  47. }
  48. if (c < 0x80) {
  49. /* one byte */
  50. buf[i++] = c;
  51. } else if (c < 0x800) {
  52. /* two bytes */
  53. buf[i++] = 0xC0 | (c >>> 6);
  54. buf[i++] = 0x80 | (c & 0x3f);
  55. } else if (c < 0x10000) {
  56. /* three bytes */
  57. buf[i++] = 0xE0 | (c >>> 12);
  58. buf[i++] = 0x80 | (c >>> 6 & 0x3f);
  59. buf[i++] = 0x80 | (c & 0x3f);
  60. } else {
  61. /* four bytes */
  62. buf[i++] = 0xf0 | (c >>> 18);
  63. buf[i++] = 0x80 | (c >>> 12 & 0x3f);
  64. buf[i++] = 0x80 | (c >>> 6 & 0x3f);
  65. buf[i++] = 0x80 | (c & 0x3f);
  66. }
  67. }
  68. return buf;
  69. };
  70. // Helper (used in 2 places)
  71. function buf2binstring(buf, len) {
  72. // On Chrome, the arguments in a function call that are allowed is `65534`.
  73. // If the length of the buffer is smaller than that, we can use this optimization,
  74. // otherwise we will take a slower path.
  75. if (len < 65534) {
  76. if ((buf.subarray && STR_APPLY_UIA_OK) || (!buf.subarray && STR_APPLY_OK)) {
  77. return String.fromCharCode.apply(null, utils.shrinkBuf(buf, len));
  78. }
  79. }
  80. var result = '';
  81. for (var i = 0; i < len; i++) {
  82. result += String.fromCharCode(buf[i]);
  83. }
  84. return result;
  85. }
  86. // Convert byte array to binary string
  87. exports.buf2binstring = function (buf) {
  88. return buf2binstring(buf, buf.length);
  89. };
  90. // Convert binary string (typed, when possible)
  91. exports.binstring2buf = function (str) {
  92. var buf = new utils.Buf8(str.length);
  93. for (var i = 0, len = buf.length; i < len; i++) {
  94. buf[i] = str.charCodeAt(i);
  95. }
  96. return buf;
  97. };
  98. // convert array to string
  99. exports.buf2string = function (buf, max) {
  100. var i, out, c, c_len;
  101. var len = max || buf.length;
  102. // Reserve max possible length (2 words per char)
  103. // NB: by unknown reasons, Array is significantly faster for
  104. // String.fromCharCode.apply than Uint16Array.
  105. var utf16buf = new Array(len * 2);
  106. for (out = 0, i = 0; i < len;) {
  107. c = buf[i++];
  108. // quick process ascii
  109. if (c < 0x80) { utf16buf[out++] = c; continue; }
  110. c_len = _utf8len[c];
  111. // skip 5 & 6 byte codes
  112. if (c_len > 4) { utf16buf[out++] = 0xfffd; i += c_len - 1; continue; }
  113. // apply mask on first byte
  114. c &= c_len === 2 ? 0x1f : c_len === 3 ? 0x0f : 0x07;
  115. // join the rest
  116. while (c_len > 1 && i < len) {
  117. c = (c << 6) | (buf[i++] & 0x3f);
  118. c_len--;
  119. }
  120. // terminated by end of string?
  121. if (c_len > 1) { utf16buf[out++] = 0xfffd; continue; }
  122. if (c < 0x10000) {
  123. utf16buf[out++] = c;
  124. } else {
  125. c -= 0x10000;
  126. utf16buf[out++] = 0xd800 | ((c >> 10) & 0x3ff);
  127. utf16buf[out++] = 0xdc00 | (c & 0x3ff);
  128. }
  129. }
  130. return buf2binstring(utf16buf, out);
  131. };
  132. // Calculate max possible position in utf8 buffer,
  133. // that will not break sequence. If that's not possible
  134. // - (very small limits) return max size as is.
  135. //
  136. // buf[] - utf8 bytes array
  137. // max - length limit (mandatory);
  138. exports.utf8border = function (buf, max) {
  139. var pos;
  140. max = max || buf.length;
  141. if (max > buf.length) { max = buf.length; }
  142. // go back from last position, until start of sequence found
  143. pos = max - 1;
  144. while (pos >= 0 && (buf[pos] & 0xC0) === 0x80) { pos--; }
  145. // Very small and broken sequence,
  146. // return max, because we should return something anyway.
  147. if (pos < 0) { return max; }
  148. // If we came to start of buffer - that means buffer is too small,
  149. // return max too.
  150. if (pos === 0) { return max; }
  151. return (pos + _utf8len[buf[pos]] > max) ? pos : max;
  152. };