text.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. const text = {};
  2. text.Decoder = class {
  3. static open(data, encoding) {
  4. if (typeof data === 'string') {
  5. return new text.Decoder.String(data);
  6. }
  7. const assert = (encoding, condition) => {
  8. if (encoding && encoding !== condition) {
  9. throw new text.Error(`Invalid encoding '${encoding}'.`);
  10. }
  11. };
  12. const buffer = data instanceof Uint8Array ? data : data.peek();
  13. const length = buffer.length;
  14. if (length >= 3 && buffer[0] === 0xef && buffer[1] === 0xbb && buffer[2] === 0xbf) {
  15. assert(encoding, 'utf-8');
  16. return new text.Decoder.Utf8(buffer, 3, true);
  17. }
  18. if (length >= 4 && buffer[0] === 0x00 && buffer[1] === 0x00 && buffer[2] === 0xfe && buffer[3] === 0xff) {
  19. assert(encoding, 'utf-32');
  20. return new text.Decoder.Utf32BE(buffer, 4);
  21. }
  22. if (length >= 4 && buffer[0] === 0xff && buffer[1] === 0xfe && buffer[2] === 0x00 && buffer[3] === 0x00) {
  23. assert(encoding, 'utf-32');
  24. return new text.Decoder.Utf32LE(buffer, 4);
  25. }
  26. if (length >= 2 && buffer[0] === 0xff && buffer[1] === 0xfe) {
  27. assert(encoding, 'utf-16');
  28. return new text.Decoder.Utf16LE(buffer, 2);
  29. }
  30. if (length >= 2 && buffer[0] === 0xfe && buffer[1] === 0xff) {
  31. assert(encoding, 'utf-16');
  32. return new text.Decoder.Utf16BE(buffer, 2);
  33. }
  34. if (length >= 5 && buffer[0] === 0x2B && buffer[1] === 0x2F && buffer[2] === 0x76 && buffer[3] === 0x38 && buffer[4] === 0x2D) {
  35. throw new text.Error("Unsupported UTF-7 encoding.");
  36. }
  37. if (length >= 4 && buffer[0] === 0x2B && buffer[1] === 0x2F && buffer[2] === 0x76 && (buffer[3] === 0x38 || buffer[3] === 0x39 || buffer[3] === 0x2B || buffer[3] === 0x2F)) {
  38. throw new text.Error("Unsupported UTF-7 encoding.");
  39. }
  40. if (length >= 4 && buffer[0] === 0x84 && buffer[1] === 0x31 && buffer[2] === 0x95 && buffer[3] === 0x33) {
  41. throw new text.Error("Unsupported GB-18030 encoding.");
  42. }
  43. if (length > 4 && (length % 2) === 0 && (buffer[0] === 0x00 || buffer[1] === 0x00 || buffer[2] === 0x00 || buffer[3] === 0x00)) {
  44. const lo = new Uint32Array(256);
  45. const hi = new Uint32Array(256);
  46. const size = Math.min(1024, length);
  47. for (let i = 0; i < size; i += 2) {
  48. lo[buffer[i]]++;
  49. hi[buffer[i + 1]]++;
  50. }
  51. if (lo[0x00] === 0 && (hi[0x00] / (size >> 1)) > 0.5) {
  52. assert(encoding, 'utf-16');
  53. return new text.Decoder.Utf16LE(buffer, 0);
  54. }
  55. if (hi[0x00] === 0 && (lo[0x00] / (size >> 1)) > 0.5) {
  56. assert(encoding, 'utf-16');
  57. return new text.Decoder.Utf16BE(buffer, 0);
  58. }
  59. }
  60. if (encoding && (encoding.startsWith('iso-8859-') || encoding.startsWith('latin-'))) {
  61. return new text.Decoder.Latin1(buffer, 0);
  62. }
  63. assert(encoding, 'utf-8');
  64. return new text.Decoder.Utf8(buffer, 0, encoding === 'utf-8');
  65. }
  66. };
  67. text.Decoder.String = class {
  68. constructor(buffer) {
  69. this.buffer = /[\u0020-\uD800]/.test(buffer) ? buffer : buffer.match(/[\uD800-\uDBFF][\uDC00-\uDFFF]|[^\uD800-\uDFFF]/g);
  70. this.position = 0;
  71. this.length = this.buffer.length;
  72. }
  73. get encoding() {
  74. return null;
  75. }
  76. decode() {
  77. if (this.position < this.length) {
  78. return this.buffer[this.position++];
  79. }
  80. return undefined;
  81. }
  82. };
  83. text.Decoder.Utf8 = class {
  84. constructor(buffer, position, fatal) {
  85. this.position = position;
  86. this.buffer = buffer;
  87. this.fatal = fatal;
  88. }
  89. get encoding() {
  90. return 'utf-8';
  91. }
  92. decode() {
  93. const c = this.buffer[this.position];
  94. if (c === undefined) {
  95. return c;
  96. }
  97. this.position++;
  98. if (c < 0x80) {
  99. return String.fromCodePoint(c);
  100. }
  101. if (c >= 0xC2 && c <= 0xDF) {
  102. if (this.buffer[this.position] !== undefined) {
  103. const c2 = this.buffer[this.position];
  104. if (c2 >= 0x80 && c2 <= 0xBF) {
  105. this.position++;
  106. return String.fromCharCode(((c & 0x1F) << 6) | (c2 & 0x3F));
  107. }
  108. }
  109. }
  110. if (c >= 0xE0 && c <= 0xEF) {
  111. if (this.buffer[this.position + 1] !== undefined) {
  112. const c2 = this.buffer[this.position];
  113. if (c2 >= 0x80 && c2 <= 0xBF &&
  114. (c !== 0xE0 || c2 >= 0xA0) &&
  115. (c !== 0xED || c2 <= 0x9f)) {
  116. const c3 = this.buffer[this.position + 1];
  117. if (c3 >= 0x80 && c3 <= 0xBF) {
  118. this.position += 2;
  119. return String.fromCharCode(((c & 0x0F) << 12) | ((c2 & 0x3F) << 6) | ((c3 & 0x3F) << 0));
  120. }
  121. }
  122. }
  123. }
  124. if (c >= 0xF0 && c <= 0xF4) {
  125. if (this.buffer[this.position + 2] !== undefined) {
  126. const c2 = this.buffer[this.position];
  127. if (c2 >= 0x80 && c2 <= 0xBF) {
  128. const c3 = this.buffer[this.position + 1];
  129. if (c3 >= 0x80 && c3 <= 0xBF) {
  130. const c4 = this.buffer[this.position + 2];
  131. if (c4 >= 0x80 && c4 <= 0xBF) {
  132. const codePoint = ((c & 0x07) << 18) | ((c2 & 0x3F) << 12) | ((c3 & 0x3F) << 6) | (c4 & 0x3F);
  133. if (codePoint <= 0x10FFFF) {
  134. this.position += 3;
  135. return String.fromCodePoint(codePoint);
  136. }
  137. }
  138. }
  139. }
  140. }
  141. }
  142. if (this.fatal) {
  143. throw new text.Error('Invalid utf-8 character.');
  144. }
  145. return String.fromCharCode(0xfffd);
  146. }
  147. };
  148. text.Decoder.Latin1 = class {
  149. constructor(buffer, position) {
  150. this.position = position;
  151. this.buffer = buffer;
  152. }
  153. get encoding() {
  154. return 'latin-1';
  155. }
  156. decode() {
  157. const c = this.buffer[this.position];
  158. if (c === undefined) {
  159. return c;
  160. }
  161. this.position++;
  162. return String.fromCodePoint(c);
  163. }
  164. };
  165. text.Decoder.Utf16LE = class {
  166. constructor(buffer, position = 0) {
  167. this.buffer = buffer;
  168. this.position = position;
  169. this.length = buffer.length;
  170. }
  171. get encoding() {
  172. return 'utf-16';
  173. }
  174. decode() {
  175. if (this.position + 1 < this.length) {
  176. const c = this.buffer[this.position++] | (this.buffer[this.position++] << 8);
  177. if (c < 0xD800 || c > 0xDFFF) {
  178. return String.fromCharCode(c);
  179. }
  180. if (c >= 0xD800 && c <= 0xDBFF) {
  181. if (this.position + 1 < this.length) {
  182. const c2 = this.buffer[this.position++] | (this.buffer[this.position++] << 8);
  183. if (c2 >= 0xDC00 && c2 <= 0xDFFF) {
  184. return String.fromCodePoint(0x10000 + ((c & 0x3ff) << 10) + (c2 & 0x3ff));
  185. }
  186. }
  187. }
  188. return String.fromCharCode(0xfffd);
  189. }
  190. return undefined;
  191. }
  192. };
  193. text.Decoder.Utf16BE = class {
  194. constructor(buffer, position = 0) {
  195. this.buffer = buffer;
  196. this.position = position;
  197. this.length = buffer.length;
  198. }
  199. get encoding() {
  200. return 'utf-16';
  201. }
  202. decode() {
  203. if (this.position + 1 < this.length) {
  204. const c = (this.buffer[this.position++] << 8) | this.buffer[this.position++];
  205. if (c < 0xD800 || c > 0xDFFF) {
  206. return String.fromCharCode(c);
  207. }
  208. if (c >= 0xD800 && c <= 0xDBFF) {
  209. if (this.position + 1 < this.length) {
  210. const c2 = (this.buffer[this.position++] << 8) | this.buffer[this.position++];
  211. if (c2 >= 0xDC00 && c2 <= 0xDFFF) {
  212. return String.fromCodePoint(0x10000 + ((c & 0x3ff) << 10) + (c2 & 0x3ff));
  213. }
  214. }
  215. }
  216. return String.fromCharCode(0xfffd);
  217. }
  218. return undefined;
  219. }
  220. };
  221. text.Decoder.Utf32LE = class {
  222. constructor(buffer, position = 0) {
  223. this.buffer = buffer;
  224. this.position = position;
  225. this.length = buffer.length;
  226. }
  227. get encoding() {
  228. return 'utf-32';
  229. }
  230. decode() {
  231. if (this.position + 3 < this.length) {
  232. const c = (
  233. (this.buffer[this.position++]) |
  234. (this.buffer[this.position++] << 8) |
  235. (this.buffer[this.position++] << 16) |
  236. (this.buffer[this.position++] << 24)
  237. ) >>> 0;
  238. if (c >= 0xD800 && c <= 0xDFFF) {
  239. return String.fromCharCode(0xfffd);
  240. }
  241. if (c <= 0x10FFFF) {
  242. return String.fromCodePoint(c);
  243. }
  244. return String.fromCharCode(0xfffd);
  245. }
  246. return undefined;
  247. }
  248. };
  249. text.Decoder.Utf32BE = class {
  250. constructor(buffer, position = 0) {
  251. this.buffer = buffer;
  252. this.position = position;
  253. this.length = buffer.length;
  254. }
  255. get encoding() {
  256. return 'utf-32';
  257. }
  258. decode() {
  259. if (this.position + 3 < this.length) {
  260. const c = (
  261. (this.buffer[this.position++] << 24) |
  262. (this.buffer[this.position++] << 16) |
  263. (this.buffer[this.position++] << 8) |
  264. (this.buffer[this.position++])
  265. ) >>> 0;
  266. if (c >= 0xD800 && c <= 0xDFFF) {
  267. return String.fromCharCode(0xfffd);
  268. }
  269. if (c <= 0x10FFFF) {
  270. return String.fromCodePoint(c);
  271. }
  272. return String.fromCharCode(0xfffd);
  273. }
  274. return undefined;
  275. }
  276. };
  277. text.Reader = class {
  278. constructor(data, length) {
  279. this.decoder = text.Decoder.open(data);
  280. this.position = 0;
  281. this.length = length === undefined ? Number.MAX_SAFE_INTEGER : length;
  282. }
  283. static open(data, length) {
  284. return new text.Reader(data, length);
  285. }
  286. read(terminal) {
  287. if (this.position >= this.length) {
  288. return undefined;
  289. }
  290. let line = '';
  291. let buffer = null;
  292. for (;;) {
  293. const c = this.decoder.decode();
  294. if (c === undefined) {
  295. this.length = this.position;
  296. break;
  297. }
  298. this.position++;
  299. if (c === terminal || this.position > this.length) {
  300. break;
  301. }
  302. line += c;
  303. if (line.length >= 64) {
  304. buffer = buffer || [];
  305. buffer.push(line);
  306. line = '';
  307. }
  308. }
  309. if (buffer) {
  310. buffer.push(line);
  311. return buffer.join('');
  312. }
  313. return line;
  314. }
  315. };
  316. text.Error = class extends Error {
  317. constructor(message) {
  318. super(message);
  319. this.name = 'Text Error';
  320. }
  321. };
  322. export const Decoder = text.Decoder;
  323. export const Reader = text.Reader;