2
0

parser.js 36 KB

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