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.

174 lines
4.9 KiB

1 year ago
  1. // String encode/decode helpers
  2. 'use strict';
  3. // Quick check if we can use fast array to bin string conversion
  4. //
  5. // - apply(Array) can fail on Android 2.2
  6. // - apply(Uint8Array) can fail on iOS 5.1 Safari
  7. //
  8. let STR_APPLY_UIA_OK = true;
  9. try { String.fromCharCode.apply(null, new Uint8Array(1)); } catch (__) { STR_APPLY_UIA_OK = false; }
  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. const _utf8len = new Uint8Array(256);
  14. for (let q = 0; q < 256; q++) {
  15. _utf8len[q] = (q >= 252 ? 6 : q >= 248 ? 5 : q >= 240 ? 4 : q >= 224 ? 3 : q >= 192 ? 2 : 1);
  16. }
  17. _utf8len[254] = _utf8len[254] = 1; // Invalid sequence start
  18. // convert string to array (typed, when possible)
  19. module.exports.string2buf = (str) => {
  20. if (typeof TextEncoder === 'function' && TextEncoder.prototype.encode) {
  21. return new TextEncoder().encode(str);
  22. }
  23. let 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 Uint8Array(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
  71. const 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) {
  77. return String.fromCharCode.apply(null, buf.length === len ? buf : buf.subarray(0, len));
  78. }
  79. }
  80. let result = '';
  81. for (let i = 0; i < len; i++) {
  82. result += String.fromCharCode(buf[i]);
  83. }
  84. return result;
  85. };
  86. // convert array to string
  87. module.exports.buf2string = (buf, max) => {
  88. const len = max || buf.length;
  89. if (typeof TextDecoder === 'function' && TextDecoder.prototype.decode) {
  90. return new TextDecoder().decode(buf.subarray(0, max));
  91. }
  92. let i, out;
  93. // Reserve max possible length (2 words per char)
  94. // NB: by unknown reasons, Array is significantly faster for
  95. // String.fromCharCode.apply than Uint16Array.
  96. const utf16buf = new Array(len * 2);
  97. for (out = 0, i = 0; i < len;) {
  98. let c = buf[i++];
  99. // quick process ascii
  100. if (c < 0x80) { utf16buf[out++] = c; continue; }
  101. let c_len = _utf8len[c];
  102. // skip 5 & 6 byte codes
  103. if (c_len > 4) { utf16buf[out++] = 0xfffd; i += c_len - 1; continue; }
  104. // apply mask on first byte
  105. c &= c_len === 2 ? 0x1f : c_len === 3 ? 0x0f : 0x07;
  106. // join the rest
  107. while (c_len > 1 && i < len) {
  108. c = (c << 6) | (buf[i++] & 0x3f);
  109. c_len--;
  110. }
  111. // terminated by end of string?
  112. if (c_len > 1) { utf16buf[out++] = 0xfffd; continue; }
  113. if (c < 0x10000) {
  114. utf16buf[out++] = c;
  115. } else {
  116. c -= 0x10000;
  117. utf16buf[out++] = 0xd800 | ((c >> 10) & 0x3ff);
  118. utf16buf[out++] = 0xdc00 | (c & 0x3ff);
  119. }
  120. }
  121. return buf2binstring(utf16buf, out);
  122. };
  123. // Calculate max possible position in utf8 buffer,
  124. // that will not break sequence. If that's not possible
  125. // - (very small limits) return max size as is.
  126. //
  127. // buf[] - utf8 bytes array
  128. // max - length limit (mandatory);
  129. module.exports.utf8border = (buf, max) => {
  130. max = max || buf.length;
  131. if (max > buf.length) { max = buf.length; }
  132. // go back from last position, until start of sequence found
  133. let pos = max - 1;
  134. while (pos >= 0 && (buf[pos] & 0xC0) === 0x80) { pos--; }
  135. // Very small and broken sequence,
  136. // return max, because we should return something anyway.
  137. if (pos < 0) { return max; }
  138. // If we came to start of buffer - that means buffer is too small,
  139. // return max too.
  140. if (pos === 0) { return max; }
  141. return (pos + _utf8len[buf[pos]] > max) ? pos : max;
  142. };