2
0

parser.js 31 KB

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