parser.js 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2019 Mozilla Foundation
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * @licend The above is the entire license notice for the
  20. * Javascript code in this page
  21. */
  22. "use strict";
  23. Object.defineProperty(exports, "__esModule", {
  24. value: true
  25. });
  26. exports.Parser = exports.Linearization = exports.Lexer = void 0;
  27. var _stream = require("./stream");
  28. var _util = require("../shared/util");
  29. var _primitives = require("./primitives");
  30. var _ccitt_stream = require("./ccitt_stream");
  31. var _jbig2_stream = require("./jbig2_stream");
  32. var _jpeg_stream = require("./jpeg_stream");
  33. var _jpx_stream = require("./jpx_stream");
  34. var _core_utils = require("./core_utils");
  35. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  36. function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
  37. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
  38. var MAX_LENGTH_TO_CACHE = 1000;
  39. var MAX_ADLER32_LENGTH = 5552;
  40. function computeAdler32(bytes) {
  41. var bytesLength = bytes.length;
  42. var a = 1,
  43. b = 0;
  44. for (var i = 0; i < bytesLength; ++i) {
  45. a += bytes[i] & 0xFF;
  46. b += a;
  47. }
  48. return b % 65521 << 16 | a % 65521;
  49. }
  50. var Parser =
  51. /*#__PURE__*/
  52. function () {
  53. function Parser(_ref) {
  54. var lexer = _ref.lexer,
  55. xref = _ref.xref,
  56. _ref$allowStreams = _ref.allowStreams,
  57. allowStreams = _ref$allowStreams === void 0 ? false : _ref$allowStreams,
  58. _ref$recoveryMode = _ref.recoveryMode,
  59. recoveryMode = _ref$recoveryMode === void 0 ? false : _ref$recoveryMode;
  60. _classCallCheck(this, Parser);
  61. this.lexer = lexer;
  62. this.xref = xref;
  63. this.allowStreams = allowStreams;
  64. this.recoveryMode = recoveryMode;
  65. this.imageCache = Object.create(null);
  66. this.refill();
  67. }
  68. _createClass(Parser, [{
  69. key: "refill",
  70. value: function refill() {
  71. this.buf1 = this.lexer.getObj();
  72. this.buf2 = this.lexer.getObj();
  73. }
  74. }, {
  75. key: "shift",
  76. value: function shift() {
  77. if ((0, _primitives.isCmd)(this.buf2, 'ID')) {
  78. this.buf1 = this.buf2;
  79. this.buf2 = null;
  80. } else {
  81. this.buf1 = this.buf2;
  82. this.buf2 = this.lexer.getObj();
  83. }
  84. }
  85. }, {
  86. key: "tryShift",
  87. value: function tryShift() {
  88. try {
  89. this.shift();
  90. return true;
  91. } catch (e) {
  92. if (e instanceof _core_utils.MissingDataException) {
  93. throw e;
  94. }
  95. return false;
  96. }
  97. }
  98. }, {
  99. key: "getObj",
  100. value: function getObj(cipherTransform) {
  101. var buf1 = this.buf1;
  102. this.shift();
  103. if (buf1 instanceof _primitives.Cmd) {
  104. switch (buf1.cmd) {
  105. case 'BI':
  106. return this.makeInlineImage(cipherTransform);
  107. case '[':
  108. var array = [];
  109. while (!(0, _primitives.isCmd)(this.buf1, ']') && !(0, _primitives.isEOF)(this.buf1)) {
  110. array.push(this.getObj(cipherTransform));
  111. }
  112. if ((0, _primitives.isEOF)(this.buf1)) {
  113. if (!this.recoveryMode) {
  114. throw new _util.FormatError('End of file inside array');
  115. }
  116. return array;
  117. }
  118. this.shift();
  119. return array;
  120. case '<<':
  121. var dict = new _primitives.Dict(this.xref);
  122. while (!(0, _primitives.isCmd)(this.buf1, '>>') && !(0, _primitives.isEOF)(this.buf1)) {
  123. if (!(0, _primitives.isName)(this.buf1)) {
  124. (0, _util.info)('Malformed dictionary: key must be a name object');
  125. this.shift();
  126. continue;
  127. }
  128. var key = this.buf1.name;
  129. this.shift();
  130. if ((0, _primitives.isEOF)(this.buf1)) {
  131. break;
  132. }
  133. dict.set(key, this.getObj(cipherTransform));
  134. }
  135. if ((0, _primitives.isEOF)(this.buf1)) {
  136. if (!this.recoveryMode) {
  137. throw new _util.FormatError('End of file inside dictionary');
  138. }
  139. return dict;
  140. }
  141. if ((0, _primitives.isCmd)(this.buf2, 'stream')) {
  142. return this.allowStreams ? this.makeStream(dict, cipherTransform) : dict;
  143. }
  144. this.shift();
  145. return dict;
  146. default:
  147. return buf1;
  148. }
  149. }
  150. if (Number.isInteger(buf1)) {
  151. var num = buf1;
  152. if (Number.isInteger(this.buf1) && (0, _primitives.isCmd)(this.buf2, 'R')) {
  153. var ref = _primitives.Ref.get(num, this.buf1);
  154. this.shift();
  155. this.shift();
  156. return ref;
  157. }
  158. return num;
  159. }
  160. if ((0, _util.isString)(buf1)) {
  161. var str = buf1;
  162. if (cipherTransform) {
  163. str = cipherTransform.decryptString(str);
  164. }
  165. return str;
  166. }
  167. return buf1;
  168. }
  169. }, {
  170. key: "findDefaultInlineStreamEnd",
  171. value: function findDefaultInlineStreamEnd(stream) {
  172. var E = 0x45,
  173. I = 0x49,
  174. SPACE = 0x20,
  175. LF = 0xA,
  176. CR = 0xD;
  177. var n = 10,
  178. NUL = 0x0;
  179. var startPos = stream.pos,
  180. state = 0,
  181. ch,
  182. maybeEIPos;
  183. while ((ch = stream.getByte()) !== -1) {
  184. if (state === 0) {
  185. state = ch === E ? 1 : 0;
  186. } else if (state === 1) {
  187. state = ch === I ? 2 : 0;
  188. } else {
  189. (0, _util.assert)(state === 2);
  190. if (ch === SPACE || ch === LF || ch === CR) {
  191. maybeEIPos = stream.pos;
  192. var followingBytes = stream.peekBytes(n);
  193. for (var i = 0, ii = followingBytes.length; i < ii; i++) {
  194. ch = followingBytes[i];
  195. if (ch === NUL && followingBytes[i + 1] !== NUL) {
  196. continue;
  197. }
  198. if (ch !== LF && ch !== CR && (ch < SPACE || ch > 0x7F)) {
  199. state = 0;
  200. break;
  201. }
  202. }
  203. if (state === 2) {
  204. break;
  205. }
  206. } else {
  207. state = 0;
  208. }
  209. }
  210. }
  211. if (ch === -1) {
  212. (0, _util.warn)('findDefaultInlineStreamEnd: ' + 'Reached the end of the stream without finding a valid EI marker');
  213. if (maybeEIPos) {
  214. (0, _util.warn)('... trying to recover by using the last "EI" occurrence.');
  215. stream.skip(-(stream.pos - maybeEIPos));
  216. }
  217. }
  218. var endOffset = 4;
  219. stream.skip(-endOffset);
  220. ch = stream.peekByte();
  221. stream.skip(endOffset);
  222. if (!(0, _util.isSpace)(ch)) {
  223. endOffset--;
  224. }
  225. return stream.pos - endOffset - startPos;
  226. }
  227. }, {
  228. key: "findDCTDecodeInlineStreamEnd",
  229. value: function findDCTDecodeInlineStreamEnd(stream) {
  230. var startPos = stream.pos,
  231. foundEOI = false,
  232. b,
  233. markerLength,
  234. length;
  235. while ((b = stream.getByte()) !== -1) {
  236. if (b !== 0xFF) {
  237. continue;
  238. }
  239. switch (stream.getByte()) {
  240. case 0x00:
  241. break;
  242. case 0xFF:
  243. stream.skip(-1);
  244. break;
  245. case 0xD9:
  246. foundEOI = true;
  247. break;
  248. case 0xC0:
  249. case 0xC1:
  250. case 0xC2:
  251. case 0xC3:
  252. case 0xC5:
  253. case 0xC6:
  254. case 0xC7:
  255. case 0xC9:
  256. case 0xCA:
  257. case 0xCB:
  258. case 0xCD:
  259. case 0xCE:
  260. case 0xCF:
  261. case 0xC4:
  262. case 0xCC:
  263. case 0xDA:
  264. case 0xDB:
  265. case 0xDC:
  266. case 0xDD:
  267. case 0xDE:
  268. case 0xDF:
  269. case 0xE0:
  270. case 0xE1:
  271. case 0xE2:
  272. case 0xE3:
  273. case 0xE4:
  274. case 0xE5:
  275. case 0xE6:
  276. case 0xE7:
  277. case 0xE8:
  278. case 0xE9:
  279. case 0xEA:
  280. case 0xEB:
  281. case 0xEC:
  282. case 0xED:
  283. case 0xEE:
  284. case 0xEF:
  285. case 0xFE:
  286. markerLength = stream.getUint16();
  287. if (markerLength > 2) {
  288. stream.skip(markerLength - 2);
  289. } else {
  290. stream.skip(-2);
  291. }
  292. break;
  293. }
  294. if (foundEOI) {
  295. break;
  296. }
  297. }
  298. length = stream.pos - startPos;
  299. if (b === -1) {
  300. (0, _util.warn)('Inline DCTDecode image stream: ' + 'EOI marker not found, searching for /EI/ instead.');
  301. stream.skip(-length);
  302. return this.findDefaultInlineStreamEnd(stream);
  303. }
  304. this.inlineStreamSkipEI(stream);
  305. return length;
  306. }
  307. }, {
  308. key: "findASCII85DecodeInlineStreamEnd",
  309. value: function findASCII85DecodeInlineStreamEnd(stream) {
  310. var TILDE = 0x7E,
  311. GT = 0x3E;
  312. var startPos = stream.pos,
  313. ch,
  314. length;
  315. while ((ch = stream.getByte()) !== -1) {
  316. if (ch === TILDE) {
  317. ch = stream.peekByte();
  318. while ((0, _util.isSpace)(ch)) {
  319. stream.skip();
  320. ch = stream.peekByte();
  321. }
  322. if (ch === GT) {
  323. stream.skip();
  324. break;
  325. }
  326. }
  327. }
  328. length = stream.pos - startPos;
  329. if (ch === -1) {
  330. (0, _util.warn)('Inline ASCII85Decode image stream: ' + 'EOD marker not found, searching for /EI/ instead.');
  331. stream.skip(-length);
  332. return this.findDefaultInlineStreamEnd(stream);
  333. }
  334. this.inlineStreamSkipEI(stream);
  335. return length;
  336. }
  337. }, {
  338. key: "findASCIIHexDecodeInlineStreamEnd",
  339. value: function findASCIIHexDecodeInlineStreamEnd(stream) {
  340. var GT = 0x3E;
  341. var startPos = stream.pos,
  342. ch,
  343. length;
  344. while ((ch = stream.getByte()) !== -1) {
  345. if (ch === GT) {
  346. break;
  347. }
  348. }
  349. length = stream.pos - startPos;
  350. if (ch === -1) {
  351. (0, _util.warn)('Inline ASCIIHexDecode image stream: ' + 'EOD marker not found, searching for /EI/ instead.');
  352. stream.skip(-length);
  353. return this.findDefaultInlineStreamEnd(stream);
  354. }
  355. this.inlineStreamSkipEI(stream);
  356. return length;
  357. }
  358. }, {
  359. key: "inlineStreamSkipEI",
  360. value: function inlineStreamSkipEI(stream) {
  361. var E = 0x45,
  362. I = 0x49;
  363. var state = 0,
  364. ch;
  365. while ((ch = stream.getByte()) !== -1) {
  366. if (state === 0) {
  367. state = ch === E ? 1 : 0;
  368. } else if (state === 1) {
  369. state = ch === I ? 2 : 0;
  370. } else if (state === 2) {
  371. break;
  372. }
  373. }
  374. }
  375. }, {
  376. key: "makeInlineImage",
  377. value: function makeInlineImage(cipherTransform) {
  378. var lexer = this.lexer;
  379. var stream = lexer.stream;
  380. var dict = new _primitives.Dict(this.xref);
  381. var dictLength;
  382. while (!(0, _primitives.isCmd)(this.buf1, 'ID') && !(0, _primitives.isEOF)(this.buf1)) {
  383. if (!(0, _primitives.isName)(this.buf1)) {
  384. throw new _util.FormatError('Dictionary key must be a name object');
  385. }
  386. var key = this.buf1.name;
  387. this.shift();
  388. if ((0, _primitives.isEOF)(this.buf1)) {
  389. break;
  390. }
  391. dict.set(key, this.getObj(cipherTransform));
  392. }
  393. if (lexer.beginInlineImagePos !== -1) {
  394. dictLength = stream.pos - lexer.beginInlineImagePos;
  395. }
  396. var filter = dict.get('Filter', 'F');
  397. var filterName;
  398. if ((0, _primitives.isName)(filter)) {
  399. filterName = filter.name;
  400. } else if (Array.isArray(filter)) {
  401. var filterZero = this.xref.fetchIfRef(filter[0]);
  402. if ((0, _primitives.isName)(filterZero)) {
  403. filterName = filterZero.name;
  404. }
  405. }
  406. var startPos = stream.pos;
  407. var length;
  408. if (filterName === 'DCTDecode' || filterName === 'DCT') {
  409. length = this.findDCTDecodeInlineStreamEnd(stream);
  410. } else if (filterName === 'ASCII85Decode' || filterName === 'A85') {
  411. length = this.findASCII85DecodeInlineStreamEnd(stream);
  412. } else if (filterName === 'ASCIIHexDecode' || filterName === 'AHx') {
  413. length = this.findASCIIHexDecodeInlineStreamEnd(stream);
  414. } else {
  415. length = this.findDefaultInlineStreamEnd(stream);
  416. }
  417. var imageStream = stream.makeSubStream(startPos, length, dict);
  418. var cacheKey;
  419. if (length < MAX_LENGTH_TO_CACHE && dictLength < MAX_ADLER32_LENGTH) {
  420. var imageBytes = imageStream.getBytes();
  421. imageStream.reset();
  422. var initialStreamPos = stream.pos;
  423. stream.pos = lexer.beginInlineImagePos;
  424. var dictBytes = stream.getBytes(dictLength);
  425. stream.pos = initialStreamPos;
  426. cacheKey = computeAdler32(imageBytes) + '_' + computeAdler32(dictBytes);
  427. var cacheEntry = this.imageCache[cacheKey];
  428. if (cacheEntry !== undefined) {
  429. this.buf2 = _primitives.Cmd.get('EI');
  430. this.shift();
  431. cacheEntry.reset();
  432. return cacheEntry;
  433. }
  434. }
  435. if (cipherTransform) {
  436. imageStream = cipherTransform.createStream(imageStream, length);
  437. }
  438. imageStream = this.filter(imageStream, dict, length);
  439. imageStream.dict = dict;
  440. if (cacheKey !== undefined) {
  441. imageStream.cacheKey = "inline_".concat(length, "_").concat(cacheKey);
  442. this.imageCache[cacheKey] = imageStream;
  443. }
  444. this.buf2 = _primitives.Cmd.get('EI');
  445. this.shift();
  446. return imageStream;
  447. }
  448. }, {
  449. key: "_findStreamLength",
  450. value: function _findStreamLength(startPos, signature) {
  451. var stream = this.lexer.stream;
  452. stream.pos = startPos;
  453. var SCAN_BLOCK_LENGTH = 2048;
  454. var signatureLength = signature.length;
  455. while (stream.pos < stream.end) {
  456. var scanBytes = stream.peekBytes(SCAN_BLOCK_LENGTH);
  457. var scanLength = scanBytes.length - signatureLength;
  458. if (scanLength <= 0) {
  459. break;
  460. }
  461. var pos = 0;
  462. while (pos < scanLength) {
  463. var j = 0;
  464. while (j < signatureLength && scanBytes[pos + j] === signature[j]) {
  465. j++;
  466. }
  467. if (j >= signatureLength) {
  468. stream.pos += pos;
  469. return stream.pos - startPos;
  470. }
  471. pos++;
  472. }
  473. stream.pos += scanLength;
  474. }
  475. return -1;
  476. }
  477. }, {
  478. key: "makeStream",
  479. value: function makeStream(dict, cipherTransform) {
  480. var lexer = this.lexer;
  481. var stream = lexer.stream;
  482. lexer.skipToNextLine();
  483. var startPos = stream.pos - 1;
  484. var length = dict.get('Length');
  485. if (!Number.isInteger(length)) {
  486. (0, _util.info)("Bad length \"".concat(length, "\" in stream"));
  487. length = 0;
  488. }
  489. stream.pos = startPos + length;
  490. lexer.nextChar();
  491. if (this.tryShift() && (0, _primitives.isCmd)(this.buf2, 'endstream')) {
  492. this.shift();
  493. } else {
  494. var ENDSTREAM_SIGNATURE = new Uint8Array([0x65, 0x6E, 0x64, 0x73, 0x74, 0x72, 0x65, 0x61, 0x6D]);
  495. var actualLength = this._findStreamLength(startPos, ENDSTREAM_SIGNATURE);
  496. if (actualLength < 0) {
  497. var MAX_TRUNCATION = 1;
  498. for (var i = 1; i <= MAX_TRUNCATION; i++) {
  499. var end = ENDSTREAM_SIGNATURE.length - i;
  500. var TRUNCATED_SIGNATURE = ENDSTREAM_SIGNATURE.slice(0, end);
  501. var maybeLength = this._findStreamLength(startPos, TRUNCATED_SIGNATURE);
  502. if (maybeLength >= 0) {
  503. var lastByte = stream.peekBytes(end + 1)[end];
  504. if (!(0, _util.isSpace)(lastByte)) {
  505. break;
  506. }
  507. (0, _util.info)("Found \"".concat((0, _util.bytesToString)(TRUNCATED_SIGNATURE), "\" when ") + 'searching for endstream command.');
  508. actualLength = maybeLength;
  509. break;
  510. }
  511. }
  512. if (actualLength < 0) {
  513. throw new _util.FormatError('Missing endstream command.');
  514. }
  515. }
  516. length = actualLength;
  517. lexer.nextChar();
  518. this.shift();
  519. this.shift();
  520. }
  521. this.shift();
  522. stream = stream.makeSubStream(startPos, length, dict);
  523. if (cipherTransform) {
  524. stream = cipherTransform.createStream(stream, length);
  525. }
  526. stream = this.filter(stream, dict, length);
  527. stream.dict = dict;
  528. return stream;
  529. }
  530. }, {
  531. key: "filter",
  532. value: function filter(stream, dict, length) {
  533. var filter = dict.get('Filter', 'F');
  534. var params = dict.get('DecodeParms', 'DP');
  535. if ((0, _primitives.isName)(filter)) {
  536. if (Array.isArray(params)) {
  537. (0, _util.warn)('/DecodeParms should not contain an Array, ' + 'when /Filter contains a Name.');
  538. }
  539. return this.makeFilter(stream, filter.name, length, params);
  540. }
  541. var maybeLength = length;
  542. if (Array.isArray(filter)) {
  543. var filterArray = filter;
  544. var paramsArray = params;
  545. for (var i = 0, ii = filterArray.length; i < ii; ++i) {
  546. filter = this.xref.fetchIfRef(filterArray[i]);
  547. if (!(0, _primitives.isName)(filter)) {
  548. throw new _util.FormatError("Bad filter name \"".concat(filter, "\""));
  549. }
  550. params = null;
  551. if (Array.isArray(paramsArray) && i in paramsArray) {
  552. params = this.xref.fetchIfRef(paramsArray[i]);
  553. }
  554. stream = this.makeFilter(stream, filter.name, maybeLength, params);
  555. maybeLength = null;
  556. }
  557. }
  558. return stream;
  559. }
  560. }, {
  561. key: "makeFilter",
  562. value: function makeFilter(stream, name, maybeLength, params) {
  563. if (maybeLength === 0) {
  564. (0, _util.warn)("Empty \"".concat(name, "\" stream."));
  565. return new _stream.NullStream();
  566. }
  567. try {
  568. var xrefStreamStats = this.xref.stats.streamTypes;
  569. if (name === 'FlateDecode' || name === 'Fl') {
  570. xrefStreamStats[_util.StreamType.FLATE] = true;
  571. if (params) {
  572. return new _stream.PredictorStream(new _stream.FlateStream(stream, maybeLength), maybeLength, params);
  573. }
  574. return new _stream.FlateStream(stream, maybeLength);
  575. }
  576. if (name === 'LZWDecode' || name === 'LZW') {
  577. xrefStreamStats[_util.StreamType.LZW] = true;
  578. var earlyChange = 1;
  579. if (params) {
  580. if (params.has('EarlyChange')) {
  581. earlyChange = params.get('EarlyChange');
  582. }
  583. return new _stream.PredictorStream(new _stream.LZWStream(stream, maybeLength, earlyChange), maybeLength, params);
  584. }
  585. return new _stream.LZWStream(stream, maybeLength, earlyChange);
  586. }
  587. if (name === 'DCTDecode' || name === 'DCT') {
  588. xrefStreamStats[_util.StreamType.DCT] = true;
  589. return new _jpeg_stream.JpegStream(stream, maybeLength, stream.dict, params);
  590. }
  591. if (name === 'JPXDecode' || name === 'JPX') {
  592. xrefStreamStats[_util.StreamType.JPX] = true;
  593. return new _jpx_stream.JpxStream(stream, maybeLength, stream.dict, params);
  594. }
  595. if (name === 'ASCII85Decode' || name === 'A85') {
  596. xrefStreamStats[_util.StreamType.A85] = true;
  597. return new _stream.Ascii85Stream(stream, maybeLength);
  598. }
  599. if (name === 'ASCIIHexDecode' || name === 'AHx') {
  600. xrefStreamStats[_util.StreamType.AHX] = true;
  601. return new _stream.AsciiHexStream(stream, maybeLength);
  602. }
  603. if (name === 'CCITTFaxDecode' || name === 'CCF') {
  604. xrefStreamStats[_util.StreamType.CCF] = true;
  605. return new _ccitt_stream.CCITTFaxStream(stream, maybeLength, params);
  606. }
  607. if (name === 'RunLengthDecode' || name === 'RL') {
  608. xrefStreamStats[_util.StreamType.RL] = true;
  609. return new _stream.RunLengthStream(stream, maybeLength);
  610. }
  611. if (name === 'JBIG2Decode') {
  612. xrefStreamStats[_util.StreamType.JBIG] = true;
  613. return new _jbig2_stream.Jbig2Stream(stream, maybeLength, stream.dict, params);
  614. }
  615. (0, _util.warn)("Filter \"".concat(name, "\" is not supported."));
  616. return stream;
  617. } catch (ex) {
  618. if (ex instanceof _core_utils.MissingDataException) {
  619. throw ex;
  620. }
  621. (0, _util.warn)("Invalid stream: \"".concat(ex, "\""));
  622. return new _stream.NullStream();
  623. }
  624. }
  625. }]);
  626. return Parser;
  627. }();
  628. exports.Parser = Parser;
  629. var specialChars = [1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 2, 0, 0, 2, 2, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
  630. function toHexDigit(ch) {
  631. if (ch >= 0x30 && ch <= 0x39) {
  632. return ch & 0x0F;
  633. }
  634. if (ch >= 0x41 && ch <= 0x46 || ch >= 0x61 && ch <= 0x66) {
  635. return (ch & 0x0F) + 9;
  636. }
  637. return -1;
  638. }
  639. var Lexer =
  640. /*#__PURE__*/
  641. function () {
  642. function Lexer(stream) {
  643. var knownCommands = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
  644. _classCallCheck(this, Lexer);
  645. this.stream = stream;
  646. this.nextChar();
  647. this.strBuf = [];
  648. this.knownCommands = knownCommands;
  649. this.beginInlineImagePos = -1;
  650. }
  651. _createClass(Lexer, [{
  652. key: "nextChar",
  653. value: function nextChar() {
  654. return this.currentChar = this.stream.getByte();
  655. }
  656. }, {
  657. key: "peekChar",
  658. value: function peekChar() {
  659. return this.stream.peekByte();
  660. }
  661. }, {
  662. key: "getNumber",
  663. value: function getNumber() {
  664. var ch = this.currentChar;
  665. var eNotation = false;
  666. var divideBy = 0;
  667. var sign = 0;
  668. if (ch === 0x2D) {
  669. sign = -1;
  670. ch = this.nextChar();
  671. if (ch === 0x2D) {
  672. ch = this.nextChar();
  673. }
  674. } else if (ch === 0x2B) {
  675. sign = 1;
  676. ch = this.nextChar();
  677. }
  678. if (ch === 0x0A || ch === 0x0D) {
  679. do {
  680. ch = this.nextChar();
  681. } while (ch === 0x0A || ch === 0x0D);
  682. }
  683. if (ch === 0x2E) {
  684. divideBy = 10;
  685. ch = this.nextChar();
  686. }
  687. if (ch < 0x30 || ch > 0x39) {
  688. if (divideBy === 10 && sign === 0 && ((0, _util.isSpace)(ch) || ch === -1)) {
  689. (0, _util.warn)('Lexer.getNumber - treating a single decimal point as zero.');
  690. return 0;
  691. }
  692. throw new _util.FormatError("Invalid number: ".concat(String.fromCharCode(ch), " (charCode ").concat(ch, ")"));
  693. }
  694. sign = sign || 1;
  695. var baseValue = ch - 0x30;
  696. var powerValue = 0;
  697. var powerValueSign = 1;
  698. while ((ch = this.nextChar()) >= 0) {
  699. if (0x30 <= ch && ch <= 0x39) {
  700. var currentDigit = ch - 0x30;
  701. if (eNotation) {
  702. powerValue = powerValue * 10 + currentDigit;
  703. } else {
  704. if (divideBy !== 0) {
  705. divideBy *= 10;
  706. }
  707. baseValue = baseValue * 10 + currentDigit;
  708. }
  709. } else if (ch === 0x2E) {
  710. if (divideBy === 0) {
  711. divideBy = 1;
  712. } else {
  713. break;
  714. }
  715. } else if (ch === 0x2D) {
  716. (0, _util.warn)('Badly formatted number: minus sign in the middle');
  717. } else if (ch === 0x45 || ch === 0x65) {
  718. ch = this.peekChar();
  719. if (ch === 0x2B || ch === 0x2D) {
  720. powerValueSign = ch === 0x2D ? -1 : 1;
  721. this.nextChar();
  722. } else if (ch < 0x30 || ch > 0x39) {
  723. break;
  724. }
  725. eNotation = true;
  726. } else {
  727. break;
  728. }
  729. }
  730. if (divideBy !== 0) {
  731. baseValue /= divideBy;
  732. }
  733. if (eNotation) {
  734. baseValue *= Math.pow(10, powerValueSign * powerValue);
  735. }
  736. return sign * baseValue;
  737. }
  738. }, {
  739. key: "getString",
  740. value: function getString() {
  741. var numParen = 1;
  742. var done = false;
  743. var strBuf = this.strBuf;
  744. strBuf.length = 0;
  745. var ch = this.nextChar();
  746. while (true) {
  747. var charBuffered = false;
  748. switch (ch | 0) {
  749. case -1:
  750. (0, _util.warn)('Unterminated string');
  751. done = true;
  752. break;
  753. case 0x28:
  754. ++numParen;
  755. strBuf.push('(');
  756. break;
  757. case 0x29:
  758. if (--numParen === 0) {
  759. this.nextChar();
  760. done = true;
  761. } else {
  762. strBuf.push(')');
  763. }
  764. break;
  765. case 0x5C:
  766. ch = this.nextChar();
  767. switch (ch) {
  768. case -1:
  769. (0, _util.warn)('Unterminated string');
  770. done = true;
  771. break;
  772. case 0x6E:
  773. strBuf.push('\n');
  774. break;
  775. case 0x72:
  776. strBuf.push('\r');
  777. break;
  778. case 0x74:
  779. strBuf.push('\t');
  780. break;
  781. case 0x62:
  782. strBuf.push('\b');
  783. break;
  784. case 0x66:
  785. strBuf.push('\f');
  786. break;
  787. case 0x5C:
  788. case 0x28:
  789. case 0x29:
  790. strBuf.push(String.fromCharCode(ch));
  791. break;
  792. case 0x30:
  793. case 0x31:
  794. case 0x32:
  795. case 0x33:
  796. case 0x34:
  797. case 0x35:
  798. case 0x36:
  799. case 0x37:
  800. var x = ch & 0x0F;
  801. ch = this.nextChar();
  802. charBuffered = true;
  803. if (ch >= 0x30 && ch <= 0x37) {
  804. x = (x << 3) + (ch & 0x0F);
  805. ch = this.nextChar();
  806. if (ch >= 0x30 && ch <= 0x37) {
  807. charBuffered = false;
  808. x = (x << 3) + (ch & 0x0F);
  809. }
  810. }
  811. strBuf.push(String.fromCharCode(x));
  812. break;
  813. case 0x0D:
  814. if (this.peekChar() === 0x0A) {
  815. this.nextChar();
  816. }
  817. break;
  818. case 0x0A:
  819. break;
  820. default:
  821. strBuf.push(String.fromCharCode(ch));
  822. break;
  823. }
  824. break;
  825. default:
  826. strBuf.push(String.fromCharCode(ch));
  827. break;
  828. }
  829. if (done) {
  830. break;
  831. }
  832. if (!charBuffered) {
  833. ch = this.nextChar();
  834. }
  835. }
  836. return strBuf.join('');
  837. }
  838. }, {
  839. key: "getName",
  840. value: function getName() {
  841. var ch, previousCh;
  842. var strBuf = this.strBuf;
  843. strBuf.length = 0;
  844. while ((ch = this.nextChar()) >= 0 && !specialChars[ch]) {
  845. if (ch === 0x23) {
  846. ch = this.nextChar();
  847. if (specialChars[ch]) {
  848. (0, _util.warn)('Lexer_getName: ' + 'NUMBER SIGN (#) should be followed by a hexadecimal number.');
  849. strBuf.push('#');
  850. break;
  851. }
  852. var x = toHexDigit(ch);
  853. if (x !== -1) {
  854. previousCh = ch;
  855. ch = this.nextChar();
  856. var x2 = toHexDigit(ch);
  857. if (x2 === -1) {
  858. (0, _util.warn)("Lexer_getName: Illegal digit (".concat(String.fromCharCode(ch), ") ") + 'in hexadecimal number.');
  859. strBuf.push('#', String.fromCharCode(previousCh));
  860. if (specialChars[ch]) {
  861. break;
  862. }
  863. strBuf.push(String.fromCharCode(ch));
  864. continue;
  865. }
  866. strBuf.push(String.fromCharCode(x << 4 | x2));
  867. } else {
  868. strBuf.push('#', String.fromCharCode(ch));
  869. }
  870. } else {
  871. strBuf.push(String.fromCharCode(ch));
  872. }
  873. }
  874. if (strBuf.length > 127) {
  875. (0, _util.warn)("Name token is longer than allowed by the spec: ".concat(strBuf.length));
  876. }
  877. return _primitives.Name.get(strBuf.join(''));
  878. }
  879. }, {
  880. key: "getHexString",
  881. value: function getHexString() {
  882. var strBuf = this.strBuf;
  883. strBuf.length = 0;
  884. var ch = this.currentChar;
  885. var isFirstHex = true;
  886. var firstDigit, secondDigit;
  887. while (true) {
  888. if (ch < 0) {
  889. (0, _util.warn)('Unterminated hex string');
  890. break;
  891. } else if (ch === 0x3E) {
  892. this.nextChar();
  893. break;
  894. } else if (specialChars[ch] === 1) {
  895. ch = this.nextChar();
  896. continue;
  897. } else {
  898. if (isFirstHex) {
  899. firstDigit = toHexDigit(ch);
  900. if (firstDigit === -1) {
  901. (0, _util.warn)("Ignoring invalid character \"".concat(ch, "\" in hex string"));
  902. ch = this.nextChar();
  903. continue;
  904. }
  905. } else {
  906. secondDigit = toHexDigit(ch);
  907. if (secondDigit === -1) {
  908. (0, _util.warn)("Ignoring invalid character \"".concat(ch, "\" in hex string"));
  909. ch = this.nextChar();
  910. continue;
  911. }
  912. strBuf.push(String.fromCharCode(firstDigit << 4 | secondDigit));
  913. }
  914. isFirstHex = !isFirstHex;
  915. ch = this.nextChar();
  916. }
  917. }
  918. return strBuf.join('');
  919. }
  920. }, {
  921. key: "getObj",
  922. value: function getObj() {
  923. var comment = false;
  924. var ch = this.currentChar;
  925. while (true) {
  926. if (ch < 0) {
  927. return _primitives.EOF;
  928. }
  929. if (comment) {
  930. if (ch === 0x0A || ch === 0x0D) {
  931. comment = false;
  932. }
  933. } else if (ch === 0x25) {
  934. comment = true;
  935. } else if (specialChars[ch] !== 1) {
  936. break;
  937. }
  938. ch = this.nextChar();
  939. }
  940. switch (ch | 0) {
  941. case 0x30:
  942. case 0x31:
  943. case 0x32:
  944. case 0x33:
  945. case 0x34:
  946. case 0x35:
  947. case 0x36:
  948. case 0x37:
  949. case 0x38:
  950. case 0x39:
  951. case 0x2B:
  952. case 0x2D:
  953. case 0x2E:
  954. return this.getNumber();
  955. case 0x28:
  956. return this.getString();
  957. case 0x2F:
  958. return this.getName();
  959. case 0x5B:
  960. this.nextChar();
  961. return _primitives.Cmd.get('[');
  962. case 0x5D:
  963. this.nextChar();
  964. return _primitives.Cmd.get(']');
  965. case 0x3C:
  966. ch = this.nextChar();
  967. if (ch === 0x3C) {
  968. this.nextChar();
  969. return _primitives.Cmd.get('<<');
  970. }
  971. return this.getHexString();
  972. case 0x3E:
  973. ch = this.nextChar();
  974. if (ch === 0x3E) {
  975. this.nextChar();
  976. return _primitives.Cmd.get('>>');
  977. }
  978. return _primitives.Cmd.get('>');
  979. case 0x7B:
  980. this.nextChar();
  981. return _primitives.Cmd.get('{');
  982. case 0x7D:
  983. this.nextChar();
  984. return _primitives.Cmd.get('}');
  985. case 0x29:
  986. this.nextChar();
  987. throw new _util.FormatError("Illegal character: ".concat(ch));
  988. }
  989. var str = String.fromCharCode(ch);
  990. var knownCommands = this.knownCommands;
  991. var knownCommandFound = knownCommands && knownCommands[str] !== undefined;
  992. while ((ch = this.nextChar()) >= 0 && !specialChars[ch]) {
  993. var possibleCommand = str + String.fromCharCode(ch);
  994. if (knownCommandFound && knownCommands[possibleCommand] === undefined) {
  995. break;
  996. }
  997. if (str.length === 128) {
  998. throw new _util.FormatError("Command token too long: ".concat(str.length));
  999. }
  1000. str = possibleCommand;
  1001. knownCommandFound = knownCommands && knownCommands[str] !== undefined;
  1002. }
  1003. if (str === 'true') {
  1004. return true;
  1005. }
  1006. if (str === 'false') {
  1007. return false;
  1008. }
  1009. if (str === 'null') {
  1010. return null;
  1011. }
  1012. if (str === 'BI') {
  1013. this.beginInlineImagePos = this.stream.pos;
  1014. }
  1015. return _primitives.Cmd.get(str);
  1016. }
  1017. }, {
  1018. key: "skipToNextLine",
  1019. value: function skipToNextLine() {
  1020. var ch = this.currentChar;
  1021. while (ch >= 0) {
  1022. if (ch === 0x0D) {
  1023. ch = this.nextChar();
  1024. if (ch === 0x0A) {
  1025. this.nextChar();
  1026. }
  1027. break;
  1028. } else if (ch === 0x0A) {
  1029. this.nextChar();
  1030. break;
  1031. }
  1032. ch = this.nextChar();
  1033. }
  1034. }
  1035. }]);
  1036. return Lexer;
  1037. }();
  1038. exports.Lexer = Lexer;
  1039. var Linearization =
  1040. /*#__PURE__*/
  1041. function () {
  1042. function Linearization() {
  1043. _classCallCheck(this, Linearization);
  1044. }
  1045. _createClass(Linearization, null, [{
  1046. key: "create",
  1047. value: function create(stream) {
  1048. function getInt(linDict, name) {
  1049. var allowZeroValue = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
  1050. var obj = linDict.get(name);
  1051. if (Number.isInteger(obj) && (allowZeroValue ? obj >= 0 : obj > 0)) {
  1052. return obj;
  1053. }
  1054. throw new Error("The \"".concat(name, "\" parameter in the linearization ") + 'dictionary is invalid.');
  1055. }
  1056. function getHints(linDict) {
  1057. var hints = linDict.get('H');
  1058. var hintsLength;
  1059. if (Array.isArray(hints) && ((hintsLength = hints.length) === 2 || hintsLength === 4)) {
  1060. for (var index = 0; index < hintsLength; index++) {
  1061. var hint = hints[index];
  1062. if (!(Number.isInteger(hint) && hint > 0)) {
  1063. throw new Error("Hint (".concat(index, ") in the linearization dictionary ") + 'is invalid.');
  1064. }
  1065. }
  1066. return hints;
  1067. }
  1068. throw new Error('Hint array in the linearization dictionary is invalid.');
  1069. }
  1070. var parser = new Parser({
  1071. lexer: new Lexer(stream),
  1072. xref: null
  1073. });
  1074. var obj1 = parser.getObj();
  1075. var obj2 = parser.getObj();
  1076. var obj3 = parser.getObj();
  1077. var linDict = parser.getObj();
  1078. var obj, length;
  1079. if (!(Number.isInteger(obj1) && Number.isInteger(obj2) && (0, _primitives.isCmd)(obj3, 'obj') && (0, _primitives.isDict)(linDict) && (0, _util.isNum)(obj = linDict.get('Linearized')) && obj > 0)) {
  1080. return null;
  1081. } else if ((length = getInt(linDict, 'L')) !== stream.length) {
  1082. throw new Error('The "L" parameter in the linearization dictionary ' + 'does not equal the stream length.');
  1083. }
  1084. return {
  1085. length: length,
  1086. hints: getHints(linDict),
  1087. objectNumberFirst: getInt(linDict, 'O'),
  1088. endFirst: getInt(linDict, 'E'),
  1089. numPages: getInt(linDict, 'N'),
  1090. mainXRefEntriesOffset: getInt(linDict, 'T'),
  1091. pageFirst: linDict.has('P') ? getInt(linDict, 'P', true) : 0
  1092. };
  1093. }
  1094. }]);
  1095. return Linearization;
  1096. }();
  1097. exports.Linearization = Linearization;