filter-basic.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891
  1. // Copyright (C) 2004-2022 Artifex Software, Inc.
  2. //
  3. // This file is part of MuPDF.
  4. //
  5. // MuPDF is free software: you can redistribute it and/or modify it under the
  6. // terms of the GNU Affero General Public License as published by the Free
  7. // Software Foundation, either version 3 of the License, or (at your option)
  8. // any later version.
  9. //
  10. // MuPDF is distributed in the hope that it will be useful, but WITHOUT ANY
  11. // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  12. // FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  13. // details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html>
  17. //
  18. // Alternative licensing terms are available from the licensor.
  19. // For commercial licensing, see <https://www.artifex.com/> or contact
  20. // Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
  21. // CA 94129, USA, for further information.
  22. #include "mupdf/fitz.h"
  23. #include <string.h>
  24. /* null filter */
  25. struct null_filter
  26. {
  27. fz_stream *chain;
  28. uint64_t remain;
  29. int64_t offset;
  30. unsigned char buffer[4096];
  31. };
  32. static int
  33. next_null(fz_context *ctx, fz_stream *stm, size_t max)
  34. {
  35. struct null_filter *state = stm->state;
  36. uint64_t n;
  37. if (state->remain == 0)
  38. return EOF;
  39. fz_seek(ctx, state->chain, state->offset, 0);
  40. n = fz_available(ctx, state->chain, max);
  41. if (n == 0)
  42. return EOF;
  43. if (n > state->remain)
  44. n = state->remain;
  45. if (n > sizeof(state->buffer))
  46. n = sizeof(state->buffer);
  47. memcpy(state->buffer, state->chain->rp, n);
  48. stm->rp = state->buffer;
  49. stm->wp = stm->rp + n;
  50. state->chain->rp += n;
  51. state->remain -= n;
  52. state->offset += n;
  53. stm->pos += n;
  54. return *stm->rp++;
  55. }
  56. static void
  57. close_null(fz_context *ctx, void *state_)
  58. {
  59. struct null_filter *state = (struct null_filter *)state_;
  60. fz_drop_stream(ctx, state->chain);
  61. fz_free(ctx, state);
  62. }
  63. fz_stream *
  64. fz_open_null_filter(fz_context *ctx, fz_stream *chain, uint64_t len, int64_t offset)
  65. {
  66. struct null_filter *state = fz_malloc_struct(ctx, struct null_filter);
  67. state->chain = fz_keep_stream(ctx, chain);
  68. state->remain = len;
  69. state->offset = offset;
  70. return fz_new_stream(ctx, state, next_null, close_null);
  71. }
  72. /* range filter */
  73. struct range_filter
  74. {
  75. fz_stream *chain;
  76. fz_range *ranges;
  77. int nranges;
  78. int next_range;
  79. size_t remain;
  80. int64_t offset;
  81. unsigned char buffer[4096];
  82. };
  83. static int
  84. next_range(fz_context *ctx, fz_stream *stm, size_t max)
  85. {
  86. struct range_filter *state = stm->state;
  87. size_t n;
  88. while (state->remain == 0 && state->next_range < state->nranges)
  89. {
  90. fz_range *range = &state->ranges[state->next_range++];
  91. state->remain = range->length;
  92. state->offset = range->offset;
  93. }
  94. if (state->remain == 0)
  95. return EOF;
  96. fz_seek(ctx, state->chain, state->offset, 0);
  97. n = fz_available(ctx, state->chain, max);
  98. if (n > state->remain)
  99. n = state->remain;
  100. if (n > sizeof(state->buffer))
  101. n = sizeof(state->buffer);
  102. memcpy(state->buffer, state->chain->rp, n);
  103. stm->rp = state->buffer;
  104. stm->wp = stm->rp + n;
  105. if (n == 0)
  106. return EOF;
  107. state->chain->rp += n;
  108. state->remain -= n;
  109. state->offset += n;
  110. stm->pos += n;
  111. return *stm->rp++;
  112. }
  113. static void
  114. close_range(fz_context *ctx, void *state_)
  115. {
  116. struct range_filter *state = (struct range_filter *)state_;
  117. fz_drop_stream(ctx, state->chain);
  118. fz_free(ctx, state->ranges);
  119. fz_free(ctx, state);
  120. }
  121. fz_stream *
  122. fz_open_range_filter(fz_context *ctx, fz_stream *chain, fz_range *ranges, int nranges)
  123. {
  124. struct range_filter *state = NULL;
  125. state = fz_malloc_struct(ctx, struct range_filter);
  126. fz_try(ctx)
  127. {
  128. if (nranges > 0)
  129. {
  130. state->ranges = fz_calloc(ctx, nranges, sizeof(*ranges));
  131. memcpy(state->ranges, ranges, nranges * sizeof(*ranges));
  132. state->nranges = nranges;
  133. state->next_range = 1;
  134. state->remain = ranges[0].length;
  135. state->offset = ranges[0].offset;
  136. }
  137. else
  138. {
  139. state->ranges = NULL;
  140. state->nranges = 0;
  141. state->next_range = 1;
  142. state->remain = 0;
  143. state->offset = 0;
  144. }
  145. state->chain = fz_keep_stream(ctx, chain);
  146. }
  147. fz_catch(ctx)
  148. {
  149. fz_free(ctx, state->ranges);
  150. fz_free(ctx, state);
  151. fz_rethrow(ctx);
  152. }
  153. return fz_new_stream(ctx, state, next_range, close_range);
  154. }
  155. /* endstream filter */
  156. #define END_CHECK_SIZE 32
  157. struct endstream_filter
  158. {
  159. fz_stream *chain;
  160. uint64_t remain;
  161. size_t extras, size;
  162. int64_t offset;
  163. int warned;
  164. unsigned char buffer[4096];
  165. };
  166. static int
  167. next_endstream(fz_context *ctx, fz_stream *stm, size_t max)
  168. {
  169. struct endstream_filter *state = stm->state;
  170. size_t n, nbytes_in_buffer, size;
  171. unsigned char *rp;
  172. if (state->remain == 0)
  173. goto look_for_endstream;
  174. fz_seek(ctx, state->chain, state->offset, 0);
  175. n = fz_available(ctx, state->chain, max);
  176. if (n == 0)
  177. return EOF;
  178. if (n > state->remain)
  179. n = state->remain;
  180. if (n > sizeof(state->buffer))
  181. n = sizeof(state->buffer);
  182. memcpy(state->buffer, state->chain->rp, n);
  183. stm->rp = state->buffer;
  184. stm->wp = stm->rp + n;
  185. state->chain->rp += n;
  186. state->remain -= n;
  187. state->offset += n;
  188. stm->pos += n;
  189. return *stm->rp++;
  190. look_for_endstream:
  191. /* We should distrust the stream length, and check for end
  192. * marker before terminating the stream - this is to cope
  193. * with files with duff "Length" values. */
  194. /* Move any data left over in our buffer down to the start.
  195. * Ordinarily, there won't be any, but this allows for the
  196. * case where we were part way through matching a stream end
  197. * marker when the buffer filled before. */
  198. nbytes_in_buffer = state->extras;
  199. if (nbytes_in_buffer)
  200. memmove(state->buffer, stm->rp, nbytes_in_buffer);
  201. stm->rp = state->buffer;
  202. stm->wp = stm->rp + nbytes_in_buffer;
  203. /* In most sane files, we'll get "\nendstream" instantly. We
  204. * should only need (say) 32 bytes to be sure. For crap files
  205. * where we overread regularly, don't harm performance by
  206. * working in small chunks. */
  207. size = state->size * 2;
  208. if (size > sizeof(state->buffer))
  209. size = sizeof(state->buffer);
  210. state->size = size;
  211. /* Read enough data into our buffer to start looking for the 'endstream' token. */
  212. fz_seek(ctx, state->chain, state->offset, 0);
  213. while (nbytes_in_buffer < size)
  214. {
  215. n = fz_available(ctx, state->chain, size - nbytes_in_buffer);
  216. if (n == 0)
  217. break;
  218. if (n > size - nbytes_in_buffer)
  219. n = size - nbytes_in_buffer;
  220. memcpy(stm->wp, state->chain->rp, n);
  221. stm->wp += n;
  222. state->chain->rp += n;
  223. nbytes_in_buffer += n;
  224. state->offset += n;
  225. }
  226. /* Look for the 'endstream' token. */
  227. rp = fz_memmem(state->buffer, nbytes_in_buffer, "endstream", 9);
  228. if (rp)
  229. {
  230. /* Include newline (CR|LF|CRLF) before 'endstream' token */
  231. if (rp > state->buffer && rp[-1] == '\n') --rp;
  232. if (rp > state->buffer && rp[-1] == '\r') --rp;
  233. n = rp - state->buffer;
  234. stm->eof = 1; /* We're done, don't call us again! */
  235. }
  236. else if (nbytes_in_buffer > 11) /* 11 covers enough data to detect "\r?\n?endstream" */
  237. n = nbytes_in_buffer - 11; /* no endstream, but there is more data */
  238. else
  239. n = nbytes_in_buffer; /* no endstream, but at the end of the file */
  240. /* We have at least n bytes before we hit an end marker */
  241. state->extras = nbytes_in_buffer - n;
  242. stm->wp = stm->rp + n;
  243. stm->pos += n;
  244. if (n == 0)
  245. return EOF;
  246. if (!state->warned)
  247. {
  248. state->warned = 1;
  249. fz_warn(ctx, "PDF stream Length incorrect");
  250. }
  251. return *stm->rp++;
  252. }
  253. static void
  254. close_endstream(fz_context *ctx, void *state_)
  255. {
  256. struct endstream_filter *state = (struct endstream_filter *)state_;
  257. fz_drop_stream(ctx, state->chain);
  258. fz_free(ctx, state);
  259. }
  260. fz_stream *
  261. fz_open_endstream_filter(fz_context *ctx, fz_stream *chain, uint64_t len, int64_t offset)
  262. {
  263. struct endstream_filter *state;
  264. state = fz_malloc_struct(ctx, struct endstream_filter);
  265. state->chain = fz_keep_stream(ctx, chain);
  266. state->remain = len;
  267. state->offset = offset;
  268. state->extras = 0;
  269. state->size = END_CHECK_SIZE >> 1; /* size is doubled first thing when used */
  270. return fz_new_stream(ctx, state, next_endstream, close_endstream);
  271. }
  272. /* concat filter */
  273. struct concat_filter
  274. {
  275. int max;
  276. int count;
  277. int current;
  278. int pad; /* 1 if we should add whitespace padding between streams */
  279. unsigned char ws_buf;
  280. fz_stream *chain[1];
  281. };
  282. static int
  283. next_concat(fz_context *ctx, fz_stream *stm, size_t max)
  284. {
  285. struct concat_filter *state = (struct concat_filter *)stm->state;
  286. size_t n;
  287. while (state->current < state->count)
  288. {
  289. /* Read the next block of underlying data. */
  290. if (stm->wp == state->chain[state->current]->wp)
  291. state->chain[state->current]->rp = stm->wp;
  292. n = fz_available(ctx, state->chain[state->current], max);
  293. if (n)
  294. {
  295. stm->rp = state->chain[state->current]->rp;
  296. stm->wp = state->chain[state->current]->wp;
  297. stm->pos += n;
  298. return *stm->rp++;
  299. }
  300. else
  301. {
  302. if (state->chain[state->current]->error)
  303. {
  304. stm->error = 1;
  305. break;
  306. }
  307. state->current++;
  308. fz_drop_stream(ctx, state->chain[state->current-1]);
  309. if (state->pad)
  310. {
  311. stm->wp = stm->rp = (&state->ws_buf)+1;
  312. stm->pos++;
  313. return 32;
  314. }
  315. }
  316. }
  317. stm->rp = stm->wp;
  318. return EOF;
  319. }
  320. static void
  321. close_concat(fz_context *ctx, void *state_)
  322. {
  323. struct concat_filter *state = (struct concat_filter *)state_;
  324. int i;
  325. for (i = state->current; i < state->count; i++)
  326. {
  327. fz_drop_stream(ctx, state->chain[i]);
  328. }
  329. fz_free(ctx, state);
  330. }
  331. fz_stream *
  332. fz_open_concat(fz_context *ctx, int len, int pad)
  333. {
  334. struct concat_filter *state;
  335. state = fz_calloc(ctx, 1, sizeof(struct concat_filter) + (len-1)*sizeof(fz_stream *));
  336. state->max = len;
  337. state->count = 0;
  338. state->current = 0;
  339. state->pad = pad;
  340. state->ws_buf = 32;
  341. return fz_new_stream(ctx, state, next_concat, close_concat);
  342. }
  343. void
  344. fz_concat_push_drop(fz_context *ctx, fz_stream *concat, fz_stream *chain)
  345. {
  346. struct concat_filter *state = (struct concat_filter *)concat->state;
  347. if (state->count == state->max)
  348. {
  349. fz_drop_stream(ctx, chain);
  350. fz_throw(ctx, FZ_ERROR_ARGUMENT, "concatenated more streams than promised");
  351. }
  352. state->chain[state->count++] = chain;
  353. }
  354. /* ASCII Hex Decode */
  355. typedef struct
  356. {
  357. fz_stream *chain;
  358. int eod;
  359. unsigned char buffer[256];
  360. } fz_ahxd;
  361. static inline int iswhite(int a)
  362. {
  363. switch (a) {
  364. case '\n': case '\r': case '\t': case ' ':
  365. case '\0': case '\f': case '\b': case 0177:
  366. return 1;
  367. }
  368. return 0;
  369. }
  370. static inline int ishex(int a)
  371. {
  372. return (a >= 'A' && a <= 'F') ||
  373. (a >= 'a' && a <= 'f') ||
  374. (a >= '0' && a <= '9');
  375. }
  376. static inline int unhex(int a)
  377. {
  378. if (a >= 'A' && a <= 'F') return a - 'A' + 0xA;
  379. if (a >= 'a' && a <= 'f') return a - 'a' + 0xA;
  380. if (a >= '0' && a <= '9') return a - '0';
  381. return 0;
  382. }
  383. static int
  384. next_ahxd(fz_context *ctx, fz_stream *stm, size_t max)
  385. {
  386. fz_ahxd *state = stm->state;
  387. unsigned char *p = state->buffer;
  388. unsigned char *ep;
  389. int a, b, c, odd;
  390. if (max > sizeof(state->buffer))
  391. max = sizeof(state->buffer);
  392. ep = p + max;
  393. odd = 0;
  394. while (p < ep)
  395. {
  396. if (state->eod)
  397. break;
  398. c = fz_read_byte(ctx, state->chain);
  399. if (c < 0)
  400. break;
  401. if (ishex(c))
  402. {
  403. if (!odd)
  404. {
  405. a = unhex(c);
  406. odd = 1;
  407. }
  408. else
  409. {
  410. b = unhex(c);
  411. *p++ = (a << 4) | b;
  412. odd = 0;
  413. }
  414. }
  415. else if (c == '>')
  416. {
  417. if (odd)
  418. *p++ = (a << 4);
  419. state->eod = 1;
  420. break;
  421. }
  422. else if (!iswhite(c))
  423. {
  424. fz_throw(ctx, FZ_ERROR_FORMAT, "bad data in ahxd: '%c'", c);
  425. }
  426. }
  427. stm->rp = state->buffer;
  428. stm->wp = p;
  429. stm->pos += p - state->buffer;
  430. if (stm->rp != p)
  431. return *stm->rp++;
  432. return EOF;
  433. }
  434. static void
  435. close_ahxd(fz_context *ctx, void *state_)
  436. {
  437. fz_ahxd *state = (fz_ahxd *)state_;
  438. fz_drop_stream(ctx, state->chain);
  439. fz_free(ctx, state);
  440. }
  441. fz_stream *
  442. fz_open_ahxd(fz_context *ctx, fz_stream *chain)
  443. {
  444. fz_ahxd *state = fz_malloc_struct(ctx, fz_ahxd);
  445. state->chain = fz_keep_stream(ctx, chain);
  446. state->eod = 0;
  447. return fz_new_stream(ctx, state, next_ahxd, close_ahxd);
  448. }
  449. /* ASCII 85 Decode */
  450. typedef struct
  451. {
  452. fz_stream *chain;
  453. unsigned char buffer[256];
  454. int eod;
  455. } fz_a85d;
  456. static int
  457. next_a85d(fz_context *ctx, fz_stream *stm, size_t max)
  458. {
  459. fz_a85d *state = stm->state;
  460. unsigned char *p = state->buffer;
  461. unsigned char *ep;
  462. int count = 0;
  463. int word = 0;
  464. int c;
  465. if (state->eod)
  466. return EOF;
  467. if (max > sizeof(state->buffer))
  468. max = sizeof(state->buffer);
  469. ep = p + max;
  470. while (p < ep)
  471. {
  472. c = fz_read_byte(ctx, state->chain);
  473. if (c < 0)
  474. break;
  475. if (c >= '!' && c <= 'u')
  476. {
  477. if (count == 4)
  478. {
  479. word = word * 85 + (c - '!');
  480. *p++ = (word >> 24) & 0xff;
  481. *p++ = (word >> 16) & 0xff;
  482. *p++ = (word >> 8) & 0xff;
  483. *p++ = (word) & 0xff;
  484. word = 0;
  485. count = 0;
  486. }
  487. else
  488. {
  489. word = word * 85 + (c - '!');
  490. count ++;
  491. }
  492. }
  493. else if (c == 'z' && count == 0)
  494. {
  495. *p++ = 0;
  496. *p++ = 0;
  497. *p++ = 0;
  498. *p++ = 0;
  499. }
  500. else if (c == '~')
  501. {
  502. c = fz_read_byte(ctx, state->chain);
  503. if (c != '>')
  504. fz_warn(ctx, "bad eod marker in a85d");
  505. switch (count) {
  506. case 0:
  507. break;
  508. case 1:
  509. /* Specifically illegal in the spec, but adobe
  510. * and gs both cope. See normal_87.pdf for a
  511. * case where this matters. */
  512. fz_warn(ctx, "partial final byte in a85d");
  513. break;
  514. case 2:
  515. word = word * (85 * 85 * 85) + 0xffffff;
  516. *p++ = word >> 24;
  517. break;
  518. case 3:
  519. word = word * (85 * 85) + 0xffff;
  520. *p++ = word >> 24;
  521. *p++ = word >> 16;
  522. break;
  523. case 4:
  524. word = word * 85 + 0xff;
  525. *p++ = word >> 24;
  526. *p++ = word >> 16;
  527. *p++ = word >> 8;
  528. break;
  529. }
  530. state->eod = 1;
  531. break;
  532. }
  533. else if (!iswhite(c))
  534. {
  535. fz_throw(ctx, FZ_ERROR_FORMAT, "bad data in a85d: '%c'", c);
  536. }
  537. }
  538. stm->rp = state->buffer;
  539. stm->wp = p;
  540. stm->pos += p - state->buffer;
  541. if (p == stm->rp)
  542. return EOF;
  543. return *stm->rp++;
  544. }
  545. static void
  546. close_a85d(fz_context *ctx, void *state_)
  547. {
  548. fz_a85d *state = (fz_a85d *)state_;
  549. fz_drop_stream(ctx, state->chain);
  550. fz_free(ctx, state);
  551. }
  552. fz_stream *
  553. fz_open_a85d(fz_context *ctx, fz_stream *chain)
  554. {
  555. fz_a85d *state = fz_malloc_struct(ctx, fz_a85d);
  556. state->chain = fz_keep_stream(ctx, chain);
  557. state->eod = 0;
  558. return fz_new_stream(ctx, state, next_a85d, close_a85d);
  559. }
  560. /* Run Length Decode */
  561. typedef struct
  562. {
  563. fz_stream *chain;
  564. int run, n, c;
  565. unsigned char buffer[256];
  566. } fz_rld;
  567. static int
  568. next_rld(fz_context *ctx, fz_stream *stm, size_t max)
  569. {
  570. fz_rld *state = stm->state;
  571. unsigned char *p = state->buffer;
  572. unsigned char *ep;
  573. if (state->run == 128)
  574. return EOF;
  575. if (max > sizeof(state->buffer))
  576. max = sizeof(state->buffer);
  577. ep = p + max;
  578. while (p < ep)
  579. {
  580. if (state->run == 128)
  581. break;
  582. if (state->n == 0)
  583. {
  584. state->run = fz_read_byte(ctx, state->chain);
  585. if (state->run < 0)
  586. {
  587. state->run = 128;
  588. break;
  589. }
  590. if (state->run < 128)
  591. state->n = state->run + 1;
  592. if (state->run > 128)
  593. {
  594. state->n = 257 - state->run;
  595. state->c = fz_read_byte(ctx, state->chain);
  596. if (state->c < 0)
  597. fz_throw(ctx, FZ_ERROR_FORMAT, "premature end of data in run length decode");
  598. }
  599. }
  600. if (state->run < 128)
  601. {
  602. while (p < ep && state->n)
  603. {
  604. int c = fz_read_byte(ctx, state->chain);
  605. if (c < 0)
  606. fz_throw(ctx, FZ_ERROR_FORMAT, "premature end of data in run length decode");
  607. *p++ = c;
  608. state->n--;
  609. }
  610. }
  611. if (state->run > 128)
  612. {
  613. while (p < ep && state->n)
  614. {
  615. *p++ = state->c;
  616. state->n--;
  617. }
  618. }
  619. }
  620. stm->rp = state->buffer;
  621. stm->wp = p;
  622. stm->pos += p - state->buffer;
  623. if (p == stm->rp)
  624. return EOF;
  625. return *stm->rp++;
  626. }
  627. static void
  628. close_rld(fz_context *ctx, void *state_)
  629. {
  630. fz_rld *state = (fz_rld *)state_;
  631. fz_drop_stream(ctx, state->chain);
  632. fz_free(ctx, state);
  633. }
  634. fz_stream *
  635. fz_open_rld(fz_context *ctx, fz_stream *chain)
  636. {
  637. fz_stream *stm;
  638. fz_rld *state = fz_malloc_struct(ctx, fz_rld);
  639. state->chain = fz_keep_stream(ctx, chain);
  640. state->run = 0;
  641. state->n = 0;
  642. state->c = 0;
  643. stm = fz_new_stream(ctx, state, next_rld, close_rld);
  644. /* Don't explode RLE compression bombs. */
  645. if (chain->next == next_rld)
  646. {
  647. fz_warn(ctx, "RLE bomb defused");
  648. stm->eof = 1;
  649. }
  650. return stm;
  651. }
  652. /* RC4 Filter */
  653. typedef struct
  654. {
  655. fz_stream *chain;
  656. fz_arc4 arc4;
  657. unsigned char buffer[256];
  658. } fz_arc4c;
  659. static int
  660. next_arc4(fz_context *ctx, fz_stream *stm, size_t max)
  661. {
  662. fz_arc4c *state = stm->state;
  663. size_t n = fz_available(ctx, state->chain, max);
  664. if (n == 0)
  665. return EOF;
  666. if (n > sizeof(state->buffer))
  667. n = sizeof(state->buffer);
  668. stm->rp = state->buffer;
  669. stm->wp = state->buffer + n;
  670. fz_arc4_encrypt(&state->arc4, stm->rp, state->chain->rp, n);
  671. state->chain->rp += n;
  672. stm->pos += n;
  673. return *stm->rp++;
  674. }
  675. static void
  676. close_arc4(fz_context *ctx, void *state_)
  677. {
  678. fz_arc4c *state = (fz_arc4c *)state_;
  679. fz_drop_stream(ctx, state->chain);
  680. fz_free(ctx, state);
  681. }
  682. fz_stream *
  683. fz_open_arc4(fz_context *ctx, fz_stream *chain, unsigned char *key, unsigned keylen)
  684. {
  685. fz_arc4c *state = fz_malloc_struct(ctx, fz_arc4c);
  686. state->chain = fz_keep_stream(ctx, chain);
  687. fz_arc4_init(&state->arc4, key, keylen);
  688. return fz_new_stream(ctx, state, next_arc4, close_arc4);
  689. }
  690. /* AES Filter */
  691. typedef struct
  692. {
  693. fz_stream *chain;
  694. fz_aes aes;
  695. unsigned char iv[16];
  696. int ivcount;
  697. unsigned char bp[16];
  698. unsigned char *rp, *wp;
  699. unsigned char buffer[256];
  700. } fz_aesd;
  701. static int
  702. next_aesd(fz_context *ctx, fz_stream *stm, size_t max)
  703. {
  704. fz_aesd *state = stm->state;
  705. unsigned char *p = state->buffer;
  706. unsigned char *ep;
  707. if (max > sizeof(state->buffer))
  708. max = sizeof(state->buffer);
  709. ep = p + max;
  710. while (state->ivcount < 16)
  711. {
  712. int c = fz_read_byte(ctx, state->chain);
  713. if (c < 0)
  714. fz_throw(ctx, FZ_ERROR_FORMAT, "premature end in aes filter");
  715. state->iv[state->ivcount++] = c;
  716. }
  717. while (state->rp < state->wp && p < ep)
  718. *p++ = *state->rp++;
  719. while (p < ep)
  720. {
  721. size_t n = fz_read(ctx, state->chain, state->bp, 16);
  722. if (n == 0)
  723. break;
  724. else if (n < 16)
  725. fz_throw(ctx, FZ_ERROR_FORMAT, "partial block in aes filter");
  726. fz_aes_crypt_cbc(&state->aes, FZ_AES_DECRYPT, 16, state->iv, state->bp, state->bp);
  727. state->rp = state->bp;
  728. state->wp = state->bp + 16;
  729. /* strip padding at end of file */
  730. if (fz_is_eof(ctx, state->chain))
  731. {
  732. int pad = state->bp[15];
  733. if (pad < 1 || pad > 16)
  734. fz_throw(ctx, FZ_ERROR_FORMAT, "aes padding out of range: %d", pad);
  735. state->wp -= pad;
  736. }
  737. while (state->rp < state->wp && p < ep)
  738. *p++ = *state->rp++;
  739. }
  740. stm->rp = state->buffer;
  741. stm->wp = p;
  742. stm->pos += p - state->buffer;
  743. if (p == stm->rp)
  744. return EOF;
  745. return *stm->rp++;
  746. }
  747. static void
  748. close_aesd(fz_context *ctx, void *state_)
  749. {
  750. fz_aesd *state = (fz_aesd *)state_;
  751. fz_drop_stream(ctx, state->chain);
  752. fz_free(ctx, state);
  753. }
  754. fz_stream *
  755. fz_open_aesd(fz_context *ctx, fz_stream *chain, unsigned char *key, unsigned keylen)
  756. {
  757. fz_aesd *state = fz_malloc_struct(ctx, fz_aesd);
  758. if (fz_aes_setkey_dec(&state->aes, key, keylen * 8))
  759. {
  760. fz_free(ctx, state);
  761. fz_throw(ctx, FZ_ERROR_ARGUMENT, "aes invalid key size (%d)", keylen * 8);
  762. }
  763. state->ivcount = 0;
  764. state->rp = state->bp;
  765. state->wp = state->bp;
  766. state->chain = fz_keep_stream(ctx, chain);
  767. return fz_new_stream(ctx, state, next_aesd, close_aesd);
  768. }