2
0

parser.js 31 KB

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