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. (0, _util.error)('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. (0, _util.error)('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. (0, _util.error)('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 === 'ASCII85Decide' || 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. if (this.imageCache.adler32 === adler32) {
  350. this.buf2 = _primitives.Cmd.get('EI');
  351. this.shift();
  352. this.imageCache[adler32].reset();
  353. return this.imageCache[adler32];
  354. }
  355. }
  356. if (cipherTransform) {
  357. imageStream = cipherTransform.createStream(imageStream, length);
  358. }
  359. imageStream = this.filter(imageStream, dict, length);
  360. imageStream.dict = dict;
  361. if (adler32 !== undefined) {
  362. imageStream.cacheKey = 'inline_' + length + '_' + adler32;
  363. this.imageCache[adler32] = imageStream;
  364. }
  365. this.buf2 = _primitives.Cmd.get('EI');
  366. this.shift();
  367. return imageStream;
  368. },
  369. makeStream: function Parser_makeStream(dict, cipherTransform) {
  370. var lexer = this.lexer;
  371. var stream = lexer.stream;
  372. lexer.skipToNextLine();
  373. var pos = stream.pos - 1;
  374. var length = dict.get('Length');
  375. if (!(0, _util.isInt)(length)) {
  376. (0, _util.info)('Bad ' + length + ' attribute in stream');
  377. length = 0;
  378. }
  379. stream.pos = pos + length;
  380. lexer.nextChar();
  381. if (this.tryShift() && (0, _primitives.isCmd)(this.buf2, 'endstream')) {
  382. this.shift();
  383. } else {
  384. stream.pos = pos;
  385. var SCAN_BLOCK_SIZE = 2048;
  386. var ENDSTREAM_SIGNATURE_LENGTH = 9;
  387. var ENDSTREAM_SIGNATURE = [0x65, 0x6E, 0x64, 0x73, 0x74, 0x72, 0x65, 0x61, 0x6D];
  388. var skipped = 0,
  389. found = false,
  390. i,
  391. j;
  392. while (stream.pos < stream.end) {
  393. var scanBytes = stream.peekBytes(SCAN_BLOCK_SIZE);
  394. var scanLength = scanBytes.length - ENDSTREAM_SIGNATURE_LENGTH;
  395. if (scanLength <= 0) {
  396. break;
  397. }
  398. found = false;
  399. i = 0;
  400. while (i < scanLength) {
  401. j = 0;
  402. while (j < ENDSTREAM_SIGNATURE_LENGTH && scanBytes[i + j] === ENDSTREAM_SIGNATURE[j]) {
  403. j++;
  404. }
  405. if (j >= ENDSTREAM_SIGNATURE_LENGTH) {
  406. found = true;
  407. break;
  408. }
  409. i++;
  410. }
  411. if (found) {
  412. skipped += i;
  413. stream.pos += i;
  414. break;
  415. }
  416. skipped += scanLength;
  417. stream.pos += scanLength;
  418. }
  419. if (!found) {
  420. (0, _util.error)('Missing endstream');
  421. }
  422. length = skipped;
  423. lexer.nextChar();
  424. this.shift();
  425. this.shift();
  426. }
  427. this.shift();
  428. stream = stream.makeSubStream(pos, length, dict);
  429. if (cipherTransform) {
  430. stream = cipherTransform.createStream(stream, length);
  431. }
  432. stream = this.filter(stream, dict, length);
  433. stream.dict = dict;
  434. return stream;
  435. },
  436. filter: function Parser_filter(stream, dict, length) {
  437. var filter = dict.get('Filter', 'F');
  438. var params = dict.get('DecodeParms', 'DP');
  439. if ((0, _primitives.isName)(filter)) {
  440. if ((0, _util.isArray)(params)) {
  441. params = this.xref.fetchIfRef(params[0]);
  442. }
  443. return this.makeFilter(stream, filter.name, length, params);
  444. }
  445. var maybeLength = length;
  446. if ((0, _util.isArray)(filter)) {
  447. var filterArray = filter;
  448. var paramsArray = params;
  449. for (var i = 0, ii = filterArray.length; i < ii; ++i) {
  450. filter = this.xref.fetchIfRef(filterArray[i]);
  451. if (!(0, _primitives.isName)(filter)) {
  452. (0, _util.error)('Bad filter name: ' + filter);
  453. }
  454. params = null;
  455. if ((0, _util.isArray)(paramsArray) && i in paramsArray) {
  456. params = this.xref.fetchIfRef(paramsArray[i]);
  457. }
  458. stream = this.makeFilter(stream, filter.name, maybeLength, params);
  459. maybeLength = null;
  460. }
  461. }
  462. return stream;
  463. },
  464. makeFilter: function Parser_makeFilter(stream, name, maybeLength, params) {
  465. if (maybeLength === 0) {
  466. (0, _util.warn)('Empty "' + name + '" stream.');
  467. return new _stream.NullStream(stream);
  468. }
  469. try {
  470. var xrefStreamStats = this.xref.stats.streamTypes;
  471. if (name === 'FlateDecode' || name === 'Fl') {
  472. xrefStreamStats[_util.StreamType.FLATE] = true;
  473. if (params) {
  474. return new _stream.PredictorStream(new _stream.FlateStream(stream, maybeLength), maybeLength, params);
  475. }
  476. return new _stream.FlateStream(stream, maybeLength);
  477. }
  478. if (name === 'LZWDecode' || name === 'LZW') {
  479. xrefStreamStats[_util.StreamType.LZW] = true;
  480. var earlyChange = 1;
  481. if (params) {
  482. if (params.has('EarlyChange')) {
  483. earlyChange = params.get('EarlyChange');
  484. }
  485. return new _stream.PredictorStream(new _stream.LZWStream(stream, maybeLength, earlyChange), maybeLength, params);
  486. }
  487. return new _stream.LZWStream(stream, maybeLength, earlyChange);
  488. }
  489. if (name === 'DCTDecode' || name === 'DCT') {
  490. xrefStreamStats[_util.StreamType.DCT] = true;
  491. return new _stream.JpegStream(stream, maybeLength, stream.dict, params);
  492. }
  493. if (name === 'JPXDecode' || name === 'JPX') {
  494. xrefStreamStats[_util.StreamType.JPX] = true;
  495. return new _stream.JpxStream(stream, maybeLength, stream.dict, params);
  496. }
  497. if (name === 'ASCII85Decode' || name === 'A85') {
  498. xrefStreamStats[_util.StreamType.A85] = true;
  499. return new _stream.Ascii85Stream(stream, maybeLength);
  500. }
  501. if (name === 'ASCIIHexDecode' || name === 'AHx') {
  502. xrefStreamStats[_util.StreamType.AHX] = true;
  503. return new _stream.AsciiHexStream(stream, maybeLength);
  504. }
  505. if (name === 'CCITTFaxDecode' || name === 'CCF') {
  506. xrefStreamStats[_util.StreamType.CCF] = true;
  507. return new _stream.CCITTFaxStream(stream, maybeLength, params);
  508. }
  509. if (name === 'RunLengthDecode' || name === 'RL') {
  510. xrefStreamStats[_util.StreamType.RL] = true;
  511. return new _stream.RunLengthStream(stream, maybeLength);
  512. }
  513. if (name === 'JBIG2Decode') {
  514. xrefStreamStats[_util.StreamType.JBIG] = true;
  515. return new _stream.Jbig2Stream(stream, maybeLength, stream.dict, params);
  516. }
  517. (0, _util.warn)('filter "' + name + '" not supported yet');
  518. return stream;
  519. } catch (ex) {
  520. if (ex instanceof _util.MissingDataException) {
  521. throw ex;
  522. }
  523. (0, _util.warn)('Invalid stream: \"' + ex + '\"');
  524. return new _stream.NullStream(stream);
  525. }
  526. }
  527. };
  528. return Parser;
  529. }();
  530. var Lexer = function LexerClosure() {
  531. function Lexer(stream, knownCommands) {
  532. this.stream = stream;
  533. this.nextChar();
  534. this.strBuf = [];
  535. this.knownCommands = knownCommands;
  536. }
  537. 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];
  538. function toHexDigit(ch) {
  539. if (ch >= 0x30 && ch <= 0x39) {
  540. return ch & 0x0F;
  541. }
  542. if (ch >= 0x41 && ch <= 0x46 || ch >= 0x61 && ch <= 0x66) {
  543. return (ch & 0x0F) + 9;
  544. }
  545. return -1;
  546. }
  547. Lexer.prototype = {
  548. nextChar: function Lexer_nextChar() {
  549. return this.currentChar = this.stream.getByte();
  550. },
  551. peekChar: function Lexer_peekChar() {
  552. return this.stream.peekByte();
  553. },
  554. getNumber: function Lexer_getNumber() {
  555. var ch = this.currentChar;
  556. var eNotation = false;
  557. var divideBy = 0;
  558. var sign = 1;
  559. if (ch === 0x2D) {
  560. sign = -1;
  561. ch = this.nextChar();
  562. if (ch === 0x2D) {
  563. ch = this.nextChar();
  564. }
  565. } else if (ch === 0x2B) {
  566. ch = this.nextChar();
  567. }
  568. if (ch === 0x2E) {
  569. divideBy = 10;
  570. ch = this.nextChar();
  571. }
  572. if (ch === 0x0A || ch === 0x0D) {
  573. do {
  574. ch = this.nextChar();
  575. } while (ch === 0x0A || ch === 0x0D);
  576. }
  577. if (ch < 0x30 || ch > 0x39) {
  578. (0, _util.error)('Invalid number: ' + String.fromCharCode(ch) + ' (charCode ' + ch + ')');
  579. }
  580. var baseValue = ch - 0x30;
  581. var powerValue = 0;
  582. var powerValueSign = 1;
  583. while ((ch = this.nextChar()) >= 0) {
  584. if (0x30 <= ch && ch <= 0x39) {
  585. var currentDigit = ch - 0x30;
  586. if (eNotation) {
  587. powerValue = powerValue * 10 + currentDigit;
  588. } else {
  589. if (divideBy !== 0) {
  590. divideBy *= 10;
  591. }
  592. baseValue = baseValue * 10 + currentDigit;
  593. }
  594. } else if (ch === 0x2E) {
  595. if (divideBy === 0) {
  596. divideBy = 1;
  597. } else {
  598. break;
  599. }
  600. } else if (ch === 0x2D) {
  601. (0, _util.warn)('Badly formatted number');
  602. } else if (ch === 0x45 || ch === 0x65) {
  603. ch = this.peekChar();
  604. if (ch === 0x2B || ch === 0x2D) {
  605. powerValueSign = ch === 0x2D ? -1 : 1;
  606. this.nextChar();
  607. } else if (ch < 0x30 || ch > 0x39) {
  608. break;
  609. }
  610. eNotation = true;
  611. } else {
  612. break;
  613. }
  614. }
  615. if (divideBy !== 0) {
  616. baseValue /= divideBy;
  617. }
  618. if (eNotation) {
  619. baseValue *= Math.pow(10, powerValueSign * powerValue);
  620. }
  621. return sign * baseValue;
  622. },
  623. getString: function Lexer_getString() {
  624. var numParen = 1;
  625. var done = false;
  626. var strBuf = this.strBuf;
  627. strBuf.length = 0;
  628. var ch = this.nextChar();
  629. while (true) {
  630. var charBuffered = false;
  631. switch (ch | 0) {
  632. case -1:
  633. (0, _util.warn)('Unterminated string');
  634. done = true;
  635. break;
  636. case 0x28:
  637. ++numParen;
  638. strBuf.push('(');
  639. break;
  640. case 0x29:
  641. if (--numParen === 0) {
  642. this.nextChar();
  643. done = true;
  644. } else {
  645. strBuf.push(')');
  646. }
  647. break;
  648. case 0x5C:
  649. ch = this.nextChar();
  650. switch (ch) {
  651. case -1:
  652. (0, _util.warn)('Unterminated string');
  653. done = true;
  654. break;
  655. case 0x6E:
  656. strBuf.push('\n');
  657. break;
  658. case 0x72:
  659. strBuf.push('\r');
  660. break;
  661. case 0x74:
  662. strBuf.push('\t');
  663. break;
  664. case 0x62:
  665. strBuf.push('\b');
  666. break;
  667. case 0x66:
  668. strBuf.push('\f');
  669. break;
  670. case 0x5C:
  671. case 0x28:
  672. case 0x29:
  673. strBuf.push(String.fromCharCode(ch));
  674. break;
  675. case 0x30:
  676. case 0x31:
  677. case 0x32:
  678. case 0x33:
  679. case 0x34:
  680. case 0x35:
  681. case 0x36:
  682. case 0x37:
  683. var x = ch & 0x0F;
  684. ch = this.nextChar();
  685. charBuffered = true;
  686. if (ch >= 0x30 && ch <= 0x37) {
  687. x = (x << 3) + (ch & 0x0F);
  688. ch = this.nextChar();
  689. if (ch >= 0x30 && ch <= 0x37) {
  690. charBuffered = false;
  691. x = (x << 3) + (ch & 0x0F);
  692. }
  693. }
  694. strBuf.push(String.fromCharCode(x));
  695. break;
  696. case 0x0D:
  697. if (this.peekChar() === 0x0A) {
  698. this.nextChar();
  699. }
  700. break;
  701. case 0x0A:
  702. break;
  703. default:
  704. strBuf.push(String.fromCharCode(ch));
  705. break;
  706. }
  707. break;
  708. default:
  709. strBuf.push(String.fromCharCode(ch));
  710. break;
  711. }
  712. if (done) {
  713. break;
  714. }
  715. if (!charBuffered) {
  716. ch = this.nextChar();
  717. }
  718. }
  719. return strBuf.join('');
  720. },
  721. getName: function Lexer_getName() {
  722. var ch, previousCh;
  723. var strBuf = this.strBuf;
  724. strBuf.length = 0;
  725. while ((ch = this.nextChar()) >= 0 && !specialChars[ch]) {
  726. if (ch === 0x23) {
  727. ch = this.nextChar();
  728. if (specialChars[ch]) {
  729. (0, _util.warn)('Lexer_getName: ' + 'NUMBER SIGN (#) should be followed by a hexadecimal number.');
  730. strBuf.push('#');
  731. break;
  732. }
  733. var x = toHexDigit(ch);
  734. if (x !== -1) {
  735. previousCh = ch;
  736. ch = this.nextChar();
  737. var x2 = toHexDigit(ch);
  738. if (x2 === -1) {
  739. (0, _util.warn)('Lexer_getName: Illegal digit (' + String.fromCharCode(ch) + ') in hexadecimal number.');
  740. strBuf.push('#', String.fromCharCode(previousCh));
  741. if (specialChars[ch]) {
  742. break;
  743. }
  744. strBuf.push(String.fromCharCode(ch));
  745. continue;
  746. }
  747. strBuf.push(String.fromCharCode(x << 4 | x2));
  748. } else {
  749. strBuf.push('#', String.fromCharCode(ch));
  750. }
  751. } else {
  752. strBuf.push(String.fromCharCode(ch));
  753. }
  754. }
  755. if (strBuf.length > 127) {
  756. (0, _util.warn)('name token is longer than allowed by the spec: ' + strBuf.length);
  757. }
  758. return _primitives.Name.get(strBuf.join(''));
  759. },
  760. getHexString: function Lexer_getHexString() {
  761. var strBuf = this.strBuf;
  762. strBuf.length = 0;
  763. var ch = this.currentChar;
  764. var isFirstHex = true;
  765. var firstDigit;
  766. var secondDigit;
  767. while (true) {
  768. if (ch < 0) {
  769. (0, _util.warn)('Unterminated hex string');
  770. break;
  771. } else if (ch === 0x3E) {
  772. this.nextChar();
  773. break;
  774. } else if (specialChars[ch] === 1) {
  775. ch = this.nextChar();
  776. continue;
  777. } else {
  778. if (isFirstHex) {
  779. firstDigit = toHexDigit(ch);
  780. if (firstDigit === -1) {
  781. (0, _util.warn)('Ignoring invalid character "' + ch + '" in hex string');
  782. ch = this.nextChar();
  783. continue;
  784. }
  785. } else {
  786. secondDigit = toHexDigit(ch);
  787. if (secondDigit === -1) {
  788. (0, _util.warn)('Ignoring invalid character "' + ch + '" in hex string');
  789. ch = this.nextChar();
  790. continue;
  791. }
  792. strBuf.push(String.fromCharCode(firstDigit << 4 | secondDigit));
  793. }
  794. isFirstHex = !isFirstHex;
  795. ch = this.nextChar();
  796. }
  797. }
  798. return strBuf.join('');
  799. },
  800. getObj: function Lexer_getObj() {
  801. var comment = false;
  802. var ch = this.currentChar;
  803. while (true) {
  804. if (ch < 0) {
  805. return _primitives.EOF;
  806. }
  807. if (comment) {
  808. if (ch === 0x0A || ch === 0x0D) {
  809. comment = false;
  810. }
  811. } else if (ch === 0x25) {
  812. comment = true;
  813. } else if (specialChars[ch] !== 1) {
  814. break;
  815. }
  816. ch = this.nextChar();
  817. }
  818. switch (ch | 0) {
  819. case 0x30:
  820. case 0x31:
  821. case 0x32:
  822. case 0x33:
  823. case 0x34:
  824. case 0x35:
  825. case 0x36:
  826. case 0x37:
  827. case 0x38:
  828. case 0x39:
  829. case 0x2B:
  830. case 0x2D:
  831. case 0x2E:
  832. return this.getNumber();
  833. case 0x28:
  834. return this.getString();
  835. case 0x2F:
  836. return this.getName();
  837. case 0x5B:
  838. this.nextChar();
  839. return _primitives.Cmd.get('[');
  840. case 0x5D:
  841. this.nextChar();
  842. return _primitives.Cmd.get(']');
  843. case 0x3C:
  844. ch = this.nextChar();
  845. if (ch === 0x3C) {
  846. this.nextChar();
  847. return _primitives.Cmd.get('<<');
  848. }
  849. return this.getHexString();
  850. case 0x3E:
  851. ch = this.nextChar();
  852. if (ch === 0x3E) {
  853. this.nextChar();
  854. return _primitives.Cmd.get('>>');
  855. }
  856. return _primitives.Cmd.get('>');
  857. case 0x7B:
  858. this.nextChar();
  859. return _primitives.Cmd.get('{');
  860. case 0x7D:
  861. this.nextChar();
  862. return _primitives.Cmd.get('}');
  863. case 0x29:
  864. this.nextChar();
  865. (0, _util.error)('Illegal character: ' + ch);
  866. break;
  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. (0, _util.error)('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;