decodeAscii.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.decodeAsciiMax15 = exports.decodeAscii = void 0;
  4. const fromCharCode = String.fromCharCode;
  5. /** This code was borrowed form cbor-x under the MIT license. */
  6. // MIT License
  7. // Copyright (c) 2020 Kris Zyp
  8. // Permission is hereby granted, free of charge, to any person obtaining a copy
  9. // of this software and associated documentation files (the "Software"), to deal
  10. // in the Software without restriction, including without limitation the rights
  11. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. // copies of the Software, and to permit persons to whom the Software is
  13. // furnished to do so, subject to the following conditions:
  14. // The above copyright notice and this permission notice shall be included in all
  15. // copies or substantial portions of the Software.
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. // SOFTWARE.
  23. const decodeAscii = (src, position, length) => {
  24. const bytes = [];
  25. for (let i = 0; i < length; i++) {
  26. const byte = src[position++];
  27. if (byte & 0x80)
  28. return;
  29. bytes.push(byte);
  30. }
  31. return fromCharCode.apply(String, bytes);
  32. };
  33. exports.decodeAscii = decodeAscii;
  34. const decodeAsciiMax15 = (src, position, length) => {
  35. if (length < 4) {
  36. if (length < 2) {
  37. if (length === 0)
  38. return '';
  39. else {
  40. const a = src[position++];
  41. if ((a & 0x80) > 1) {
  42. position -= 1;
  43. return;
  44. }
  45. return fromCharCode(a);
  46. }
  47. }
  48. else {
  49. const a = src[position++];
  50. const b = src[position++];
  51. if ((a & 0x80) > 0 || (b & 0x80) > 0) {
  52. position -= 2;
  53. return;
  54. }
  55. if (length < 3)
  56. return fromCharCode(a, b);
  57. const c = src[position++];
  58. if ((c & 0x80) > 0) {
  59. position -= 3;
  60. return;
  61. }
  62. return fromCharCode(a, b, c);
  63. }
  64. }
  65. else {
  66. const a = src[position++];
  67. const b = src[position++];
  68. const c = src[position++];
  69. const d = src[position++];
  70. if ((a & 0x80) > 0 || (b & 0x80) > 0 || (c & 0x80) > 0 || (d & 0x80) > 0) {
  71. position -= 4;
  72. return;
  73. }
  74. if (length < 6) {
  75. if (length === 4)
  76. return fromCharCode(a, b, c, d);
  77. else {
  78. const e = src[position++];
  79. if ((e & 0x80) > 0) {
  80. position -= 5;
  81. return;
  82. }
  83. return fromCharCode(a, b, c, d, e);
  84. }
  85. }
  86. else if (length < 8) {
  87. const e = src[position++];
  88. const f = src[position++];
  89. if ((e & 0x80) > 0 || (f & 0x80) > 0) {
  90. position -= 6;
  91. return;
  92. }
  93. if (length < 7)
  94. return fromCharCode(a, b, c, d, e, f);
  95. const g = src[position++];
  96. if ((g & 0x80) > 0) {
  97. position -= 7;
  98. return;
  99. }
  100. return fromCharCode(a, b, c, d, e, f, g);
  101. }
  102. else {
  103. const e = src[position++];
  104. const f = src[position++];
  105. const g = src[position++];
  106. const h = src[position++];
  107. if ((e & 0x80) > 0 || (f & 0x80) > 0 || (g & 0x80) > 0 || (h & 0x80) > 0) {
  108. position -= 8;
  109. return;
  110. }
  111. if (length < 10) {
  112. if (length === 8)
  113. return fromCharCode(a, b, c, d, e, f, g, h);
  114. else {
  115. const i = src[position++];
  116. if ((i & 0x80) > 0) {
  117. position -= 9;
  118. return;
  119. }
  120. return fromCharCode(a, b, c, d, e, f, g, h, i);
  121. }
  122. }
  123. else if (length < 12) {
  124. const i = src[position++];
  125. const j = src[position++];
  126. if ((i & 0x80) > 0 || (j & 0x80) > 0) {
  127. position -= 10;
  128. return;
  129. }
  130. if (length < 11)
  131. return fromCharCode(a, b, c, d, e, f, g, h, i, j);
  132. const k = src[position++];
  133. if ((k & 0x80) > 0) {
  134. position -= 11;
  135. return;
  136. }
  137. return fromCharCode(a, b, c, d, e, f, g, h, i, j, k);
  138. }
  139. else {
  140. const i = src[position++];
  141. const j = src[position++];
  142. const k = src[position++];
  143. const l = src[position++];
  144. if ((i & 0x80) > 0 || (j & 0x80) > 0 || (k & 0x80) > 0 || (l & 0x80) > 0) {
  145. position -= 12;
  146. return;
  147. }
  148. if (length < 14) {
  149. if (length === 12)
  150. return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l);
  151. else {
  152. const m = src[position++];
  153. if ((m & 0x80) > 0) {
  154. position -= 13;
  155. return;
  156. }
  157. return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m);
  158. }
  159. }
  160. else {
  161. const m = src[position++];
  162. const n = src[position++];
  163. if ((m & 0x80) > 0 || (n & 0x80) > 0) {
  164. position -= 14;
  165. return;
  166. }
  167. if (length < 15)
  168. return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n);
  169. const o = src[position++];
  170. if ((o & 0x80) > 0) {
  171. position -= 15;
  172. return;
  173. }
  174. return fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o);
  175. }
  176. }
  177. }
  178. }
  179. };
  180. exports.decodeAsciiMax15 = decodeAsciiMax15;
  181. //# sourceMappingURL=decodeAscii.js.map