xpdf-3.02pl2.patch 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  1. diff -c -r xpdf-3.02pl1.orig/xpdf/Stream.cc xpdf-3.02/xpdf/Stream.cc
  2. *** xpdf-3.02pl1.orig/xpdf/Stream.cc Thu Oct 25 15:47:38 2007
  3. --- xpdf-3.02/xpdf/Stream.cc Thu Oct 25 15:48:19 2007
  4. ***************
  5. *** 1243,1265 ****
  6. columns = columnsA;
  7. if (columns < 1) {
  8. columns = 1;
  9. ! }
  10. ! if (columns + 4 <= 0) {
  11. ! columns = INT_MAX - 4;
  12. }
  13. rows = rowsA;
  14. endOfBlock = endOfBlockA;
  15. black = blackA;
  16. ! refLine = (short *)gmallocn(columns + 3, sizeof(short));
  17. ! codingLine = (short *)gmallocn(columns + 2, sizeof(short));
  18. eof = gFalse;
  19. row = 0;
  20. nextLine2D = encoding < 0;
  21. inputBits = 0;
  22. ! codingLine[0] = 0;
  23. ! codingLine[1] = refLine[2] = columns;
  24. ! a0 = 1;
  25. buf = EOF;
  26. }
  27. --- 1243,1268 ----
  28. columns = columnsA;
  29. if (columns < 1) {
  30. columns = 1;
  31. ! } else if (columns > INT_MAX - 2) {
  32. ! columns = INT_MAX - 2;
  33. }
  34. rows = rowsA;
  35. endOfBlock = endOfBlockA;
  36. black = blackA;
  37. ! // 0 <= codingLine[0] < codingLine[1] < ... < codingLine[n] = columns
  38. ! // ---> max codingLine size = columns + 1
  39. ! // refLine has one extra guard entry at the end
  40. ! // ---> max refLine size = columns + 2
  41. ! codingLine = (int *)gmallocn(columns + 1, sizeof(int));
  42. ! refLine = (int *)gmallocn(columns + 2, sizeof(int));
  43. eof = gFalse;
  44. row = 0;
  45. nextLine2D = encoding < 0;
  46. inputBits = 0;
  47. ! codingLine[0] = columns;
  48. ! a0i = 0;
  49. ! outputBits = 0;
  50. buf = EOF;
  51. }
  52. ***************
  53. *** 1278,1286 ****
  54. row = 0;
  55. nextLine2D = encoding < 0;
  56. inputBits = 0;
  57. ! codingLine[0] = 0;
  58. ! codingLine[1] = columns;
  59. ! a0 = 1;
  60. buf = EOF;
  61. // skip any initial zero bits and end-of-line marker, and get the 2D
  62. --- 1281,1289 ----
  63. row = 0;
  64. nextLine2D = encoding < 0;
  65. inputBits = 0;
  66. ! codingLine[0] = columns;
  67. ! a0i = 0;
  68. ! outputBits = 0;
  69. buf = EOF;
  70. // skip any initial zero bits and end-of-line marker, and get the 2D
  71. ***************
  72. *** 1297,1507 ****
  73. }
  74. }
  75. int CCITTFaxStream::lookChar() {
  76. short code1, code2, code3;
  77. ! int a0New;
  78. ! GBool err, gotEOL;
  79. ! int ret;
  80. ! int bits, i;
  81. ! // if at eof just return EOF
  82. ! if (eof && codingLine[a0] >= columns) {
  83. ! return EOF;
  84. }
  85. // read the next row
  86. ! err = gFalse;
  87. ! if (codingLine[a0] >= columns) {
  88. // 2-D encoding
  89. if (nextLine2D) {
  90. - // state:
  91. - // a0New = current position in coding line (0 <= a0New <= columns)
  92. - // codingLine[a0] = last change in coding line
  93. - // (black-to-white if a0 is even,
  94. - // white-to-black if a0 is odd)
  95. - // refLine[b1] = next change in reference line of opposite color
  96. - // to a0
  97. - // invariants:
  98. - // 0 <= codingLine[a0] <= a0New
  99. - // <= refLine[b1] <= refLine[b1+1] <= columns
  100. - // 0 <= a0 <= columns+1
  101. - // refLine[0] = 0
  102. - // refLine[n] = refLine[n+1] = columns
  103. - // -- for some 1 <= n <= columns+1
  104. - // end condition:
  105. - // 0 = codingLine[0] <= codingLine[1] < codingLine[2] < ...
  106. - // < codingLine[n-1] < codingLine[n] = columns
  107. - // -- where 1 <= n <= columns+1
  108. for (i = 0; codingLine[i] < columns; ++i) {
  109. refLine[i] = codingLine[i];
  110. }
  111. ! refLine[i] = refLine[i + 1] = columns;
  112. ! b1 = 1;
  113. ! a0New = codingLine[a0 = 0] = 0;
  114. ! do {
  115. code1 = getTwoDimCode();
  116. switch (code1) {
  117. case twoDimPass:
  118. ! if (refLine[b1] < columns) {
  119. ! a0New = refLine[b1 + 1];
  120. ! b1 += 2;
  121. }
  122. break;
  123. case twoDimHoriz:
  124. ! if ((a0 & 1) == 0) {
  125. ! code1 = code2 = 0;
  126. do {
  127. ! code1 += code3 = getWhiteCode();
  128. } while (code3 >= 64);
  129. do {
  130. ! code2 += code3 = getBlackCode();
  131. } while (code3 >= 64);
  132. } else {
  133. - code1 = code2 = 0;
  134. do {
  135. ! code1 += code3 = getBlackCode();
  136. } while (code3 >= 64);
  137. do {
  138. ! code2 += code3 = getWhiteCode();
  139. } while (code3 >= 64);
  140. }
  141. ! if (code1 > 0 || code2 > 0) {
  142. ! if (a0New + code1 <= columns) {
  143. ! codingLine[a0 + 1] = a0New + code1;
  144. ! } else {
  145. ! codingLine[a0 + 1] = columns;
  146. ! }
  147. ! ++a0;
  148. ! if (codingLine[a0] + code2 <= columns) {
  149. ! codingLine[a0 + 1] = codingLine[a0] + code2;
  150. ! } else {
  151. ! codingLine[a0 + 1] = columns;
  152. ! }
  153. ! ++a0;
  154. ! a0New = codingLine[a0];
  155. ! while (refLine[b1] <= a0New && refLine[b1] < columns) {
  156. ! b1 += 2;
  157. }
  158. }
  159. break;
  160. ! case twoDimVert0:
  161. ! if (refLine[b1] < columns) {
  162. ! a0New = codingLine[++a0] = refLine[b1];
  163. ! ++b1;
  164. ! while (refLine[b1] <= a0New && refLine[b1] < columns) {
  165. ! b1 += 2;
  166. }
  167. - } else {
  168. - a0New = codingLine[++a0] = columns;
  169. }
  170. break;
  171. case twoDimVertR1:
  172. ! if (refLine[b1] + 1 < columns) {
  173. ! a0New = codingLine[++a0] = refLine[b1] + 1;
  174. ! ++b1;
  175. ! while (refLine[b1] <= a0New && refLine[b1] < columns) {
  176. ! b1 += 2;
  177. }
  178. - } else {
  179. - a0New = codingLine[++a0] = columns;
  180. }
  181. break;
  182. ! case twoDimVertL1:
  183. ! if (refLine[b1] - 1 > a0New || (a0 == 0 && refLine[b1] == 1)) {
  184. ! a0New = codingLine[++a0] = refLine[b1] - 1;
  185. ! --b1;
  186. ! while (refLine[b1] <= a0New && refLine[b1] < columns) {
  187. ! b1 += 2;
  188. }
  189. }
  190. break;
  191. ! case twoDimVertR2:
  192. ! if (refLine[b1] + 2 < columns) {
  193. ! a0New = codingLine[++a0] = refLine[b1] + 2;
  194. ! ++b1;
  195. ! while (refLine[b1] <= a0New && refLine[b1] < columns) {
  196. ! b1 += 2;
  197. }
  198. - } else {
  199. - a0New = codingLine[++a0] = columns;
  200. }
  201. break;
  202. case twoDimVertL2:
  203. ! if (refLine[b1] - 2 > a0New || (a0 == 0 && refLine[b1] == 2)) {
  204. ! a0New = codingLine[++a0] = refLine[b1] - 2;
  205. ! --b1;
  206. ! while (refLine[b1] <= a0New && refLine[b1] < columns) {
  207. ! b1 += 2;
  208. }
  209. ! }
  210. ! break;
  211. ! case twoDimVertR3:
  212. ! if (refLine[b1] + 3 < columns) {
  213. ! a0New = codingLine[++a0] = refLine[b1] + 3;
  214. ! ++b1;
  215. ! while (refLine[b1] <= a0New && refLine[b1] < columns) {
  216. ! b1 += 2;
  217. }
  218. - } else {
  219. - a0New = codingLine[++a0] = columns;
  220. }
  221. break;
  222. ! case twoDimVertL3:
  223. ! if (refLine[b1] - 3 > a0New || (a0 == 0 && refLine[b1] == 3)) {
  224. ! a0New = codingLine[++a0] = refLine[b1] - 3;
  225. ! --b1;
  226. ! while (refLine[b1] <= a0New && refLine[b1] < columns) {
  227. ! b1 += 2;
  228. }
  229. }
  230. break;
  231. case EOF:
  232. eof = gTrue;
  233. ! codingLine[a0 = 0] = columns;
  234. ! return EOF;
  235. default:
  236. error(getPos(), "Bad 2D code %04x in CCITTFax stream", code1);
  237. err = gTrue;
  238. break;
  239. }
  240. ! } while (codingLine[a0] < columns);
  241. // 1-D encoding
  242. } else {
  243. ! codingLine[a0 = 0] = 0;
  244. ! while (1) {
  245. code1 = 0;
  246. ! do {
  247. ! code1 += code3 = getWhiteCode();
  248. ! } while (code3 >= 64);
  249. ! codingLine[a0+1] = codingLine[a0] + code1;
  250. ! ++a0;
  251. ! if (codingLine[a0] >= columns) {
  252. ! break;
  253. ! }
  254. ! code2 = 0;
  255. ! do {
  256. ! code2 += code3 = getBlackCode();
  257. ! } while (code3 >= 64);
  258. ! codingLine[a0+1] = codingLine[a0] + code2;
  259. ! ++a0;
  260. ! if (codingLine[a0] >= columns) {
  261. ! break;
  262. }
  263. }
  264. }
  265. - if (codingLine[a0] != columns) {
  266. - error(getPos(), "CCITTFax row is wrong length (%d)", codingLine[a0]);
  267. - // force the row to be the correct length
  268. - while (codingLine[a0] > columns) {
  269. - --a0;
  270. - }
  271. - codingLine[++a0] = columns;
  272. - err = gTrue;
  273. - }
  274. -
  275. // byte-align the row
  276. if (byteAlign) {
  277. inputBits &= ~7;
  278. --- 1300,1529 ----
  279. }
  280. }
  281. + inline void CCITTFaxStream::addPixels(int a1, int blackPixels) {
  282. + if (a1 > codingLine[a0i]) {
  283. + if (a1 > columns) {
  284. + error(getPos(), "CCITTFax row is wrong length (%d)", a1);
  285. + err = gTrue;
  286. + a1 = columns;
  287. + }
  288. + if ((a0i & 1) ^ blackPixels) {
  289. + ++a0i;
  290. + }
  291. + codingLine[a0i] = a1;
  292. + }
  293. + }
  294. +
  295. + inline void CCITTFaxStream::addPixelsNeg(int a1, int blackPixels) {
  296. + if (a1 > codingLine[a0i]) {
  297. + if (a1 > columns) {
  298. + error(getPos(), "CCITTFax row is wrong length (%d)", a1);
  299. + err = gTrue;
  300. + a1 = columns;
  301. + }
  302. + if ((a0i & 1) ^ blackPixels) {
  303. + ++a0i;
  304. + }
  305. + codingLine[a0i] = a1;
  306. + } else if (a1 < codingLine[a0i]) {
  307. + if (a1 < 0) {
  308. + error(getPos(), "Invalid CCITTFax code");
  309. + err = gTrue;
  310. + a1 = 0;
  311. + }
  312. + while (a0i > 0 && a1 <= codingLine[a0i - 1]) {
  313. + --a0i;
  314. + }
  315. + codingLine[a0i] = a1;
  316. + }
  317. + }
  318. +
  319. int CCITTFaxStream::lookChar() {
  320. short code1, code2, code3;
  321. ! int b1i, blackPixels, i, bits;
  322. ! GBool gotEOL;
  323. ! if (buf != EOF) {
  324. ! return buf;
  325. }
  326. // read the next row
  327. ! if (outputBits == 0) {
  328. !
  329. ! // if at eof just return EOF
  330. ! if (eof) {
  331. ! return EOF;
  332. ! }
  333. !
  334. ! err = gFalse;
  335. // 2-D encoding
  336. if (nextLine2D) {
  337. for (i = 0; codingLine[i] < columns; ++i) {
  338. refLine[i] = codingLine[i];
  339. }
  340. ! refLine[i++] = columns;
  341. ! refLine[i] = columns;
  342. ! codingLine[0] = 0;
  343. ! a0i = 0;
  344. ! b1i = 0;
  345. ! blackPixels = 0;
  346. ! // invariant:
  347. ! // refLine[b1i-1] <= codingLine[a0i] < refLine[b1i] < refLine[b1i+1]
  348. ! // <= columns
  349. ! // exception at left edge:
  350. ! // codingLine[a0i = 0] = refLine[b1i = 0] = 0 is possible
  351. ! // exception at right edge:
  352. ! // refLine[b1i] = refLine[b1i+1] = columns is possible
  353. ! while (codingLine[a0i] < columns) {
  354. code1 = getTwoDimCode();
  355. switch (code1) {
  356. case twoDimPass:
  357. ! addPixels(refLine[b1i + 1], blackPixels);
  358. ! if (refLine[b1i + 1] < columns) {
  359. ! b1i += 2;
  360. }
  361. break;
  362. case twoDimHoriz:
  363. ! code1 = code2 = 0;
  364. ! if (blackPixels) {
  365. do {
  366. ! code1 += code3 = getBlackCode();
  367. } while (code3 >= 64);
  368. do {
  369. ! code2 += code3 = getWhiteCode();
  370. } while (code3 >= 64);
  371. } else {
  372. do {
  373. ! code1 += code3 = getWhiteCode();
  374. } while (code3 >= 64);
  375. do {
  376. ! code2 += code3 = getBlackCode();
  377. } while (code3 >= 64);
  378. }
  379. ! addPixels(codingLine[a0i] + code1, blackPixels);
  380. ! if (codingLine[a0i] < columns) {
  381. ! addPixels(codingLine[a0i] + code2, blackPixels ^ 1);
  382. ! }
  383. ! while (refLine[b1i] <= codingLine[a0i] && refLine[b1i] < columns) {
  384. ! b1i += 2;
  385. ! }
  386. ! break;
  387. ! case twoDimVertR3:
  388. ! addPixels(refLine[b1i] + 3, blackPixels);
  389. ! blackPixels ^= 1;
  390. ! if (codingLine[a0i] < columns) {
  391. ! ++b1i;
  392. ! while (refLine[b1i] <= codingLine[a0i] && refLine[b1i] < columns) {
  393. ! b1i += 2;
  394. }
  395. }
  396. break;
  397. ! case twoDimVertR2:
  398. ! addPixels(refLine[b1i] + 2, blackPixels);
  399. ! blackPixels ^= 1;
  400. ! if (codingLine[a0i] < columns) {
  401. ! ++b1i;
  402. ! while (refLine[b1i] <= codingLine[a0i] && refLine[b1i] < columns) {
  403. ! b1i += 2;
  404. }
  405. }
  406. break;
  407. case twoDimVertR1:
  408. ! addPixels(refLine[b1i] + 1, blackPixels);
  409. ! blackPixels ^= 1;
  410. ! if (codingLine[a0i] < columns) {
  411. ! ++b1i;
  412. ! while (refLine[b1i] <= codingLine[a0i] && refLine[b1i] < columns) {
  413. ! b1i += 2;
  414. }
  415. }
  416. break;
  417. ! case twoDimVert0:
  418. ! addPixels(refLine[b1i], blackPixels);
  419. ! blackPixels ^= 1;
  420. ! if (codingLine[a0i] < columns) {
  421. ! ++b1i;
  422. ! while (refLine[b1i] <= codingLine[a0i] && refLine[b1i] < columns) {
  423. ! b1i += 2;
  424. }
  425. }
  426. break;
  427. ! case twoDimVertL3:
  428. ! addPixelsNeg(refLine[b1i] - 3, blackPixels);
  429. ! blackPixels ^= 1;
  430. ! if (codingLine[a0i] < columns) {
  431. ! if (b1i > 0) {
  432. ! --b1i;
  433. ! } else {
  434. ! ++b1i;
  435. ! }
  436. ! while (refLine[b1i] <= codingLine[a0i] && refLine[b1i] < columns) {
  437. ! b1i += 2;
  438. }
  439. }
  440. break;
  441. case twoDimVertL2:
  442. ! addPixelsNeg(refLine[b1i] - 2, blackPixels);
  443. ! blackPixels ^= 1;
  444. ! if (codingLine[a0i] < columns) {
  445. ! if (b1i > 0) {
  446. ! --b1i;
  447. ! } else {
  448. ! ++b1i;
  449. }
  450. ! while (refLine[b1i] <= codingLine[a0i] && refLine[b1i] < columns) {
  451. ! b1i += 2;
  452. }
  453. }
  454. break;
  455. ! case twoDimVertL1:
  456. ! addPixelsNeg(refLine[b1i] - 1, blackPixels);
  457. ! blackPixels ^= 1;
  458. ! if (codingLine[a0i] < columns) {
  459. ! if (b1i > 0) {
  460. ! --b1i;
  461. ! } else {
  462. ! ++b1i;
  463. ! }
  464. ! while (refLine[b1i] <= codingLine[a0i] && refLine[b1i] < columns) {
  465. ! b1i += 2;
  466. }
  467. }
  468. break;
  469. case EOF:
  470. + addPixels(columns, 0);
  471. eof = gTrue;
  472. ! break;
  473. default:
  474. error(getPos(), "Bad 2D code %04x in CCITTFax stream", code1);
  475. + addPixels(columns, 0);
  476. err = gTrue;
  477. break;
  478. }
  479. ! }
  480. // 1-D encoding
  481. } else {
  482. ! codingLine[0] = 0;
  483. ! a0i = 0;
  484. ! blackPixels = 0;
  485. ! while (codingLine[a0i] < columns) {
  486. code1 = 0;
  487. ! if (blackPixels) {
  488. ! do {
  489. ! code1 += code3 = getBlackCode();
  490. ! } while (code3 >= 64);
  491. ! } else {
  492. ! do {
  493. ! code1 += code3 = getWhiteCode();
  494. ! } while (code3 >= 64);
  495. }
  496. + addPixels(codingLine[a0i] + code1, blackPixels);
  497. + blackPixels ^= 1;
  498. }
  499. }
  500. // byte-align the row
  501. if (byteAlign) {
  502. inputBits &= ~7;
  503. ***************
  504. *** 1560,1573 ****
  505. // this if we know the stream contains end-of-line markers because
  506. // the "just plow on" technique tends to work better otherwise
  507. } else if (err && endOfLine) {
  508. ! do {
  509. if (code1 == EOF) {
  510. eof = gTrue;
  511. return EOF;
  512. }
  513. eatBits(1);
  514. ! code1 = lookBits(13);
  515. ! } while ((code1 >> 1) != 0x001);
  516. eatBits(12);
  517. if (encoding > 0) {
  518. eatBits(1);
  519. --- 1582,1598 ----
  520. // this if we know the stream contains end-of-line markers because
  521. // the "just plow on" technique tends to work better otherwise
  522. } else if (err && endOfLine) {
  523. ! while (1) {
  524. ! code1 = lookBits(13);
  525. if (code1 == EOF) {
  526. eof = gTrue;
  527. return EOF;
  528. }
  529. + if ((code1 >> 1) == 0x001) {
  530. + break;
  531. + }
  532. eatBits(1);
  533. ! }
  534. eatBits(12);
  535. if (encoding > 0) {
  536. eatBits(1);
  537. ***************
  538. *** 1575,1585 ****
  539. }
  540. }
  541. ! a0 = 0;
  542. ! outputBits = codingLine[1] - codingLine[0];
  543. ! if (outputBits == 0) {
  544. ! a0 = 1;
  545. ! outputBits = codingLine[2] - codingLine[1];
  546. }
  547. ++row;
  548. --- 1600,1610 ----
  549. }
  550. }
  551. ! // set up for output
  552. ! if (codingLine[0] > 0) {
  553. ! outputBits = codingLine[a0i = 0];
  554. ! } else {
  555. ! outputBits = codingLine[a0i = 1];
  556. }
  557. ++row;
  558. ***************
  559. *** 1587,1625 ****
  560. // get a byte
  561. if (outputBits >= 8) {
  562. ! ret = ((a0 & 1) == 0) ? 0xff : 0x00;
  563. ! if ((outputBits -= 8) == 0) {
  564. ! ++a0;
  565. ! if (codingLine[a0] < columns) {
  566. ! outputBits = codingLine[a0 + 1] - codingLine[a0];
  567. ! }
  568. }
  569. } else {
  570. bits = 8;
  571. ! ret = 0;
  572. do {
  573. if (outputBits > bits) {
  574. ! i = bits;
  575. ! bits = 0;
  576. ! if ((a0 & 1) == 0) {
  577. ! ret |= 0xff >> (8 - i);
  578. }
  579. ! outputBits -= i;
  580. } else {
  581. ! i = outputBits;
  582. ! bits -= outputBits;
  583. ! if ((a0 & 1) == 0) {
  584. ! ret |= (0xff >> (8 - i)) << bits;
  585. }
  586. outputBits = 0;
  587. ! ++a0;
  588. ! if (codingLine[a0] < columns) {
  589. ! outputBits = codingLine[a0 + 1] - codingLine[a0];
  590. }
  591. }
  592. ! } while (bits > 0 && codingLine[a0] < columns);
  593. }
  594. - buf = black ? (ret ^ 0xff) : ret;
  595. return buf;
  596. }
  597. --- 1612,1654 ----
  598. // get a byte
  599. if (outputBits >= 8) {
  600. ! buf = (a0i & 1) ? 0x00 : 0xff;
  601. ! outputBits -= 8;
  602. ! if (outputBits == 0 && codingLine[a0i] < columns) {
  603. ! ++a0i;
  604. ! outputBits = codingLine[a0i] - codingLine[a0i - 1];
  605. }
  606. } else {
  607. bits = 8;
  608. ! buf = 0;
  609. do {
  610. if (outputBits > bits) {
  611. ! buf <<= bits;
  612. ! if (!(a0i & 1)) {
  613. ! buf |= 0xff >> (8 - bits);
  614. }
  615. ! outputBits -= bits;
  616. ! bits = 0;
  617. } else {
  618. ! buf <<= outputBits;
  619. ! if (!(a0i & 1)) {
  620. ! buf |= 0xff >> (8 - outputBits);
  621. }
  622. + bits -= outputBits;
  623. outputBits = 0;
  624. ! if (codingLine[a0i] < columns) {
  625. ! ++a0i;
  626. ! outputBits = codingLine[a0i] - codingLine[a0i - 1];
  627. ! } else if (bits > 0) {
  628. ! buf <<= bits;
  629. ! bits = 0;
  630. }
  631. }
  632. ! } while (bits);
  633. ! }
  634. ! if (black) {
  635. ! buf ^= 0xff;
  636. }
  637. return buf;
  638. }
  639. ***************
  640. *** 1661,1666 ****
  641. --- 1690,1698 ----
  642. code = 0; // make gcc happy
  643. if (endOfBlock) {
  644. code = lookBits(12);
  645. + if (code == EOF) {
  646. + return 1;
  647. + }
  648. if ((code >> 5) == 0) {
  649. p = &whiteTab1[code];
  650. } else {
  651. ***************
  652. *** 1673,1678 ****
  653. --- 1705,1713 ----
  654. } else {
  655. for (n = 1; n <= 9; ++n) {
  656. code = lookBits(n);
  657. + if (code == EOF) {
  658. + return 1;
  659. + }
  660. if (n < 9) {
  661. code <<= 9 - n;
  662. }
  663. ***************
  664. *** 1684,1689 ****
  665. --- 1719,1727 ----
  666. }
  667. for (n = 11; n <= 12; ++n) {
  668. code = lookBits(n);
  669. + if (code == EOF) {
  670. + return 1;
  671. + }
  672. if (n < 12) {
  673. code <<= 12 - n;
  674. }
  675. ***************
  676. *** 1709,1717 ****
  677. code = 0; // make gcc happy
  678. if (endOfBlock) {
  679. code = lookBits(13);
  680. if ((code >> 7) == 0) {
  681. p = &blackTab1[code];
  682. ! } else if ((code >> 9) == 0) {
  683. p = &blackTab2[(code >> 1) - 64];
  684. } else {
  685. p = &blackTab3[code >> 7];
  686. --- 1747,1758 ----
  687. code = 0; // make gcc happy
  688. if (endOfBlock) {
  689. code = lookBits(13);
  690. + if (code == EOF) {
  691. + return 1;
  692. + }
  693. if ((code >> 7) == 0) {
  694. p = &blackTab1[code];
  695. ! } else if ((code >> 9) == 0 && (code >> 7) != 0) {
  696. p = &blackTab2[(code >> 1) - 64];
  697. } else {
  698. p = &blackTab3[code >> 7];
  699. ***************
  700. *** 1723,1728 ****
  701. --- 1764,1772 ----
  702. } else {
  703. for (n = 2; n <= 6; ++n) {
  704. code = lookBits(n);
  705. + if (code == EOF) {
  706. + return 1;
  707. + }
  708. if (n < 6) {
  709. code <<= 6 - n;
  710. }
  711. ***************
  712. *** 1734,1739 ****
  713. --- 1778,1786 ----
  714. }
  715. for (n = 7; n <= 12; ++n) {
  716. code = lookBits(n);
  717. + if (code == EOF) {
  718. + return 1;
  719. + }
  720. if (n < 12) {
  721. code <<= 12 - n;
  722. }
  723. ***************
  724. *** 1747,1752 ****
  725. --- 1794,1802 ----
  726. }
  727. for (n = 10; n <= 13; ++n) {
  728. code = lookBits(n);
  729. + if (code == EOF) {
  730. + return 1;
  731. + }
  732. if (n < 13) {
  733. code <<= 13 - n;
  734. }
  735. ***************
  736. *** 1961,1966 ****
  737. --- 2011,2022 ----
  738. // allocate a buffer for the whole image
  739. bufWidth = ((width + mcuWidth - 1) / mcuWidth) * mcuWidth;
  740. bufHeight = ((height + mcuHeight - 1) / mcuHeight) * mcuHeight;
  741. + if (bufWidth <= 0 || bufHeight <= 0 ||
  742. + bufWidth > INT_MAX / bufWidth / (int)sizeof(int)) {
  743. + error(getPos(), "Invalid image size in DCT stream");
  744. + y = height;
  745. + return;
  746. + }
  747. for (i = 0; i < numComps; ++i) {
  748. frameBuf[i] = (int *)gmallocn(bufWidth * bufHeight, sizeof(int));
  749. memset(frameBuf[i], 0, bufWidth * bufHeight * sizeof(int));
  750. ***************
  751. *** 3036,3041 ****
  752. --- 3092,3102 ----
  753. }
  754. scanInfo.firstCoeff = str->getChar();
  755. scanInfo.lastCoeff = str->getChar();
  756. + if (scanInfo.firstCoeff < 0 || scanInfo.lastCoeff > 63 ||
  757. + scanInfo.firstCoeff > scanInfo.lastCoeff) {
  758. + error(getPos(), "Bad DCT coefficient numbers in scan info block");
  759. + return gFalse;
  760. + }
  761. c = str->getChar();
  762. scanInfo.ah = (c >> 4) & 0x0f;
  763. scanInfo.al = c & 0x0f;
  764. diff -c -r xpdf-3.02pl1.orig/xpdf/Stream.h xpdf-3.02/xpdf/Stream.h
  765. *** xpdf-3.02pl1.orig/xpdf/Stream.h Tue Feb 27 14:05:52 2007
  766. --- xpdf-3.02/xpdf/Stream.h Thu Oct 25 15:48:15 2007
  767. ***************
  768. *** 528,540 ****
  769. int row; // current row
  770. int inputBuf; // input buffer
  771. int inputBits; // number of bits in input buffer
  772. ! short *refLine; // reference line changing elements
  773. ! int b1; // index into refLine
  774. ! short *codingLine; // coding line changing elements
  775. ! int a0; // index into codingLine
  776. int outputBits; // remaining ouput bits
  777. int buf; // character buffer
  778. short getTwoDimCode();
  779. short getWhiteCode();
  780. short getBlackCode();
  781. --- 528,542 ----
  782. int row; // current row
  783. int inputBuf; // input buffer
  784. int inputBits; // number of bits in input buffer
  785. ! int *codingLine; // coding line changing elements
  786. ! int *refLine; // reference line changing elements
  787. ! int a0i; // index into codingLine
  788. ! GBool err; // error on current line
  789. int outputBits; // remaining ouput bits
  790. int buf; // character buffer
  791. + void addPixels(int a1, int black);
  792. + void addPixelsNeg(int a1, int black);
  793. short getTwoDimCode();
  794. short getWhiteCode();
  795. short getBlackCode();