filt_basic.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  1. #include "fitz-internal.h"
  2. /* Pretend we have a filter that just copies data forever */
  3. fz_stream *
  4. fz_open_copy(fz_stream *chain)
  5. {
  6. return fz_keep_stream(chain);
  7. }
  8. /* Null filter copies a specified amount of data */
  9. struct null_filter
  10. {
  11. fz_stream *chain;
  12. int remain;
  13. int pos;
  14. };
  15. static int
  16. read_null(fz_stream *stm, unsigned char *buf, int len)
  17. {
  18. struct null_filter *state = stm->state;
  19. int amount = fz_mini(len, state->remain);
  20. int n;
  21. fz_seek(state->chain, state->pos, 0);
  22. n = fz_read(state->chain, buf, amount);
  23. state->remain -= n;
  24. state->pos += n;
  25. return n;
  26. }
  27. static void
  28. close_null(fz_context *ctx, void *state_)
  29. {
  30. struct null_filter *state = (struct null_filter *)state_;
  31. fz_stream *chain = state->chain;
  32. fz_free(ctx, state);
  33. fz_close(chain);
  34. }
  35. fz_stream *
  36. fz_open_null(fz_stream *chain, int len, int offset)
  37. {
  38. struct null_filter *state;
  39. fz_context *ctx = chain->ctx;
  40. if (len < 0)
  41. len = 0;
  42. fz_try(ctx)
  43. {
  44. state = fz_malloc_struct(ctx, struct null_filter);
  45. state->chain = chain;
  46. state->remain = len;
  47. state->pos = offset;
  48. }
  49. fz_catch(ctx)
  50. {
  51. fz_close(chain);
  52. fz_rethrow(ctx);
  53. }
  54. return fz_new_stream(ctx, state, read_null, close_null);
  55. }
  56. /* Concat filter concatenates several streams into one */
  57. struct concat_filter
  58. {
  59. int max;
  60. int count;
  61. int current;
  62. int pad; /* 1 if we should add whitespace padding between streams */
  63. int ws; /* 1 if we should send a whitespace padding byte next */
  64. fz_stream *chain[1];
  65. };
  66. static int
  67. read_concat(fz_stream *stm, unsigned char *buf, int len)
  68. {
  69. struct concat_filter *state = (struct concat_filter *)stm->state;
  70. int n;
  71. int read = 0;
  72. if (len <= 0)
  73. return 0;
  74. while (state->current != state->count && len > 0)
  75. {
  76. /* If we need to send a whitespace char, do that */
  77. if (state->ws)
  78. {
  79. *buf++ = 32;
  80. read++;
  81. len--;
  82. state->ws = 0;
  83. continue;
  84. }
  85. /* Otherwise, read as much data as will fit in the buffer */
  86. n = fz_read(state->chain[state->current], buf, len);
  87. read += n;
  88. buf += n;
  89. len -= n;
  90. /* If we didn't read any, then we must have hit the end of
  91. * our buffer space. Move to the next stream, and remember to
  92. * pad. */
  93. if (n == 0)
  94. {
  95. fz_close(state->chain[state->current]);
  96. state->current++;
  97. state->ws = state->pad;
  98. }
  99. }
  100. return read;
  101. }
  102. static void
  103. close_concat(fz_context *ctx, void *state_)
  104. {
  105. struct concat_filter *state = (struct concat_filter *)state_;
  106. int i;
  107. for (i = state->current; i < state->count; i++)
  108. {
  109. fz_close(state->chain[i]);
  110. }
  111. fz_free(ctx, state);
  112. }
  113. fz_stream *
  114. fz_open_concat(fz_context *ctx, int len, int pad)
  115. {
  116. struct concat_filter *state;
  117. fz_try(ctx)
  118. {
  119. state = fz_calloc(ctx, 1, sizeof(struct concat_filter) + (len-1)*sizeof(fz_stream *));
  120. state->max = len;
  121. state->count = 0;
  122. state->current = 0;
  123. state->pad = pad;
  124. state->ws = 0; /* We never send padding byte at the start */
  125. }
  126. fz_catch(ctx)
  127. {
  128. fz_rethrow(ctx);
  129. }
  130. return fz_new_stream(ctx, state, read_concat, close_concat);
  131. }
  132. void
  133. fz_concat_push(fz_stream *concat, fz_stream *chain)
  134. {
  135. struct concat_filter *state = (struct concat_filter *)concat->state;
  136. if (state->count == state->max)
  137. fz_throw(concat->ctx, "Concat filter size exceeded");
  138. state->chain[state->count++] = chain;
  139. }
  140. /* ASCII Hex Decode */
  141. typedef struct fz_ahxd_s fz_ahxd;
  142. struct fz_ahxd_s
  143. {
  144. fz_stream *chain;
  145. int eod;
  146. };
  147. static inline int iswhite(int a)
  148. {
  149. switch (a) {
  150. case '\n': case '\r': case '\t': case ' ':
  151. case '\0': case '\f': case '\b': case 0177:
  152. return 1;
  153. }
  154. return 0;
  155. }
  156. static inline int ishex(int a)
  157. {
  158. return (a >= 'A' && a <= 'F') ||
  159. (a >= 'a' && a <= 'f') ||
  160. (a >= '0' && a <= '9');
  161. }
  162. static inline int unhex(int a)
  163. {
  164. if (a >= 'A' && a <= 'F') return a - 'A' + 0xA;
  165. if (a >= 'a' && a <= 'f') return a - 'a' + 0xA;
  166. if (a >= '0' && a <= '9') return a - '0';
  167. return 0;
  168. }
  169. static int
  170. read_ahxd(fz_stream *stm, unsigned char *buf, int len)
  171. {
  172. fz_ahxd *state = stm->state;
  173. unsigned char *p = buf;
  174. unsigned char *ep = buf + len;
  175. int a, b, c, odd;
  176. odd = 0;
  177. while (p < ep)
  178. {
  179. if (state->eod)
  180. return p - buf;
  181. c = fz_read_byte(state->chain);
  182. if (c < 0)
  183. return p - buf;
  184. if (ishex(c))
  185. {
  186. if (!odd)
  187. {
  188. a = unhex(c);
  189. odd = 1;
  190. }
  191. else
  192. {
  193. b = unhex(c);
  194. *p++ = (a << 4) | b;
  195. odd = 0;
  196. }
  197. }
  198. else if (c == '>')
  199. {
  200. if (odd)
  201. *p++ = (a << 4);
  202. state->eod = 1;
  203. }
  204. else if (!iswhite(c))
  205. {
  206. fz_throw(stm->ctx, "bad data in ahxd: '%c'", c);
  207. }
  208. }
  209. return p - buf;
  210. }
  211. static void
  212. close_ahxd(fz_context *ctx, void *state_)
  213. {
  214. fz_ahxd *state = (fz_ahxd *)state_;
  215. fz_stream *chain = state->chain;
  216. fz_free(ctx, state);
  217. fz_close(chain);
  218. }
  219. fz_stream *
  220. fz_open_ahxd(fz_stream *chain)
  221. {
  222. fz_ahxd *state;
  223. fz_context *ctx = chain->ctx;
  224. fz_try(ctx)
  225. {
  226. state = fz_malloc_struct(ctx, fz_ahxd);
  227. state->chain = chain;
  228. state->eod = 0;
  229. }
  230. fz_catch(ctx)
  231. {
  232. fz_close(chain);
  233. fz_rethrow(ctx);
  234. }
  235. return fz_new_stream(ctx, state, read_ahxd, close_ahxd);
  236. }
  237. /* ASCII 85 Decode */
  238. typedef struct fz_a85d_s fz_a85d;
  239. struct fz_a85d_s
  240. {
  241. fz_stream *chain;
  242. unsigned char bp[4];
  243. unsigned char *rp, *wp;
  244. int eod;
  245. };
  246. static int
  247. read_a85d(fz_stream *stm, unsigned char *buf, int len)
  248. {
  249. fz_a85d *state = stm->state;
  250. unsigned char *p = buf;
  251. unsigned char *ep = buf + len;
  252. int count = 0;
  253. int word = 0;
  254. int c;
  255. while (state->rp < state->wp && p < ep)
  256. *p++ = *state->rp++;
  257. while (p < ep)
  258. {
  259. if (state->eod)
  260. return p - buf;
  261. c = fz_read_byte(state->chain);
  262. if (c < 0)
  263. return p - buf;
  264. if (c >= '!' && c <= 'u')
  265. {
  266. if (count == 4)
  267. {
  268. word = word * 85 + (c - '!');
  269. state->bp[0] = (word >> 24) & 0xff;
  270. state->bp[1] = (word >> 16) & 0xff;
  271. state->bp[2] = (word >> 8) & 0xff;
  272. state->bp[3] = (word) & 0xff;
  273. state->rp = state->bp;
  274. state->wp = state->bp + 4;
  275. word = 0;
  276. count = 0;
  277. }
  278. else
  279. {
  280. word = word * 85 + (c - '!');
  281. count ++;
  282. }
  283. }
  284. else if (c == 'z' && count == 0)
  285. {
  286. state->bp[0] = 0;
  287. state->bp[1] = 0;
  288. state->bp[2] = 0;
  289. state->bp[3] = 0;
  290. state->rp = state->bp;
  291. state->wp = state->bp + 4;
  292. }
  293. else if (c == '~')
  294. {
  295. c = fz_read_byte(state->chain);
  296. if (c != '>')
  297. fz_warn(stm->ctx, "bad eod marker in a85d");
  298. switch (count) {
  299. case 0:
  300. break;
  301. case 1:
  302. /* Specifically illegal in the spec, but adobe
  303. * and gs both cope. See normal_87.pdf for a
  304. * case where this matters. */
  305. fz_warn(stm->ctx, "partial final byte in a85d");
  306. break;
  307. case 2:
  308. word = word * (85 * 85 * 85) + 0xffffff;
  309. state->bp[0] = word >> 24;
  310. state->rp = state->bp;
  311. state->wp = state->bp + 1;
  312. break;
  313. case 3:
  314. word = word * (85 * 85) + 0xffff;
  315. state->bp[0] = word >> 24;
  316. state->bp[1] = word >> 16;
  317. state->rp = state->bp;
  318. state->wp = state->bp + 2;
  319. break;
  320. case 4:
  321. word = word * 85 + 0xff;
  322. state->bp[0] = word >> 24;
  323. state->bp[1] = word >> 16;
  324. state->bp[2] = word >> 8;
  325. state->rp = state->bp;
  326. state->wp = state->bp + 3;
  327. break;
  328. }
  329. state->eod = 1;
  330. }
  331. else if (!iswhite(c))
  332. {
  333. fz_throw(stm->ctx, "bad data in a85d: '%c'", c);
  334. }
  335. while (state->rp < state->wp && p < ep)
  336. *p++ = *state->rp++;
  337. }
  338. return p - buf;
  339. }
  340. static void
  341. close_a85d(fz_context *ctx, void *state_)
  342. {
  343. fz_a85d *state = (fz_a85d *)state_;
  344. fz_stream *chain = state->chain;
  345. fz_free(ctx, state);
  346. fz_close(chain);
  347. }
  348. fz_stream *
  349. fz_open_a85d(fz_stream *chain)
  350. {
  351. fz_a85d *state;
  352. fz_context *ctx = chain->ctx;
  353. fz_try(ctx)
  354. {
  355. state = fz_malloc_struct(ctx, fz_a85d);
  356. state->chain = chain;
  357. state->rp = state->bp;
  358. state->wp = state->bp;
  359. state->eod = 0;
  360. }
  361. fz_catch(ctx)
  362. {
  363. fz_close(chain);
  364. fz_rethrow(ctx);
  365. }
  366. return fz_new_stream(ctx, state, read_a85d, close_a85d);
  367. }
  368. /* Run Length Decode */
  369. typedef struct fz_rld_s fz_rld;
  370. struct fz_rld_s
  371. {
  372. fz_stream *chain;
  373. int run, n, c;
  374. };
  375. static int
  376. read_rld(fz_stream *stm, unsigned char *buf, int len)
  377. {
  378. fz_rld *state = stm->state;
  379. unsigned char *p = buf;
  380. unsigned char *ep = buf + len;
  381. while (p < ep)
  382. {
  383. if (state->run == 128)
  384. return p - buf;
  385. if (state->n == 0)
  386. {
  387. state->run = fz_read_byte(state->chain);
  388. if (state->run < 0)
  389. state->run = 128;
  390. if (state->run < 128)
  391. state->n = state->run + 1;
  392. if (state->run > 128)
  393. {
  394. state->n = 257 - state->run;
  395. state->c = fz_read_byte(state->chain);
  396. if (state->c < 0)
  397. fz_throw(stm->ctx, "premature end of data in run length decode");
  398. }
  399. }
  400. if (state->run < 128)
  401. {
  402. while (p < ep && state->n)
  403. {
  404. int c = fz_read_byte(state->chain);
  405. if (c < 0)
  406. fz_throw(stm->ctx, "premature end of data in run length decode");
  407. *p++ = c;
  408. state->n--;
  409. }
  410. }
  411. if (state->run > 128)
  412. {
  413. while (p < ep && state->n)
  414. {
  415. *p++ = state->c;
  416. state->n--;
  417. }
  418. }
  419. }
  420. return p - buf;
  421. }
  422. static void
  423. close_rld(fz_context *ctx, void *state_)
  424. {
  425. fz_rld *state = (fz_rld *)state_;
  426. fz_stream *chain = state->chain;
  427. fz_free(ctx, state);
  428. fz_close(chain);
  429. }
  430. fz_stream *
  431. fz_open_rld(fz_stream *chain)
  432. {
  433. fz_rld *state;
  434. fz_context *ctx = chain->ctx;
  435. fz_try(ctx)
  436. {
  437. state = fz_malloc_struct(ctx, fz_rld);
  438. state->chain = chain;
  439. state->run = 0;
  440. state->n = 0;
  441. state->c = 0;
  442. }
  443. fz_catch(ctx)
  444. {
  445. fz_close(chain);
  446. fz_rethrow(ctx);
  447. }
  448. return fz_new_stream(ctx, state, read_rld, close_rld);
  449. }
  450. /* RC4 Filter */
  451. typedef struct fz_arc4c_s fz_arc4c;
  452. struct fz_arc4c_s
  453. {
  454. fz_stream *chain;
  455. fz_arc4 arc4;
  456. };
  457. static int
  458. read_arc4(fz_stream *stm, unsigned char *buf, int len)
  459. {
  460. fz_arc4c *state = stm->state;
  461. int n = fz_read(state->chain, buf, len);
  462. fz_arc4_encrypt(&state->arc4, buf, buf, n);
  463. return n;
  464. }
  465. static void
  466. close_arc4(fz_context *ctx, void *state_)
  467. {
  468. fz_arc4c *state = (fz_arc4c *)state_;
  469. fz_stream *chain = state->chain;
  470. fz_free(ctx, state);
  471. fz_close(chain);
  472. }
  473. fz_stream *
  474. fz_open_arc4(fz_stream *chain, unsigned char *key, unsigned keylen)
  475. {
  476. fz_arc4c *state;
  477. fz_context *ctx = chain->ctx;
  478. fz_try(ctx)
  479. {
  480. state = fz_malloc_struct(ctx, fz_arc4c);
  481. state->chain = chain;
  482. fz_arc4_init(&state->arc4, key, keylen);
  483. }
  484. fz_catch(ctx)
  485. {
  486. fz_close(chain);
  487. fz_rethrow(ctx);
  488. }
  489. return fz_new_stream(ctx, state, read_arc4, close_arc4);
  490. }
  491. /* AES Filter */
  492. typedef struct fz_aesd_s fz_aesd;
  493. struct fz_aesd_s
  494. {
  495. fz_stream *chain;
  496. fz_aes aes;
  497. unsigned char iv[16];
  498. int ivcount;
  499. unsigned char bp[16];
  500. unsigned char *rp, *wp;
  501. };
  502. static int
  503. read_aesd(fz_stream *stm, unsigned char *buf, int len)
  504. {
  505. fz_aesd *state = stm->state;
  506. unsigned char *p = buf;
  507. unsigned char *ep = buf + len;
  508. while (state->ivcount < 16)
  509. {
  510. int c = fz_read_byte(state->chain);
  511. if (c < 0)
  512. fz_throw(stm->ctx, "premature end in aes filter");
  513. state->iv[state->ivcount++] = c;
  514. }
  515. while (state->rp < state->wp && p < ep)
  516. *p++ = *state->rp++;
  517. while (p < ep)
  518. {
  519. int n = fz_read(state->chain, state->bp, 16);
  520. if (n == 0)
  521. return p - buf;
  522. else if (n < 16)
  523. fz_throw(stm->ctx, "partial block in aes filter");
  524. aes_crypt_cbc(&state->aes, AES_DECRYPT, 16, state->iv, state->bp, state->bp);
  525. state->rp = state->bp;
  526. state->wp = state->bp + 16;
  527. /* strip padding at end of file */
  528. if (fz_is_eof(state->chain))
  529. {
  530. int pad = state->bp[15];
  531. if (pad < 1 || pad > 16)
  532. fz_throw(stm->ctx, "aes padding out of range: %d", pad);
  533. state->wp -= pad;
  534. }
  535. while (state->rp < state->wp && p < ep)
  536. *p++ = *state->rp++;
  537. }
  538. return p - buf;
  539. }
  540. static void
  541. close_aesd(fz_context *ctx, void *state_)
  542. {
  543. fz_aesd *state = (fz_aesd *)state_;
  544. fz_stream *chain = state->chain;
  545. fz_free(ctx, state);
  546. fz_close(chain);
  547. }
  548. fz_stream *
  549. fz_open_aesd(fz_stream *chain, unsigned char *key, unsigned keylen)
  550. {
  551. fz_aesd *state;
  552. fz_context *ctx = chain->ctx;
  553. fz_try(ctx)
  554. {
  555. state = fz_malloc_struct(ctx, fz_aesd);
  556. state->chain = chain;
  557. if (aes_setkey_dec(&state->aes, key, keylen * 8))
  558. fz_throw(ctx, "AES key init failed (keylen=%d)", keylen * 8);
  559. state->ivcount = 0;
  560. state->rp = state->bp;
  561. state->wp = state->bp;
  562. }
  563. fz_catch(ctx)
  564. {
  565. fz_close(chain);
  566. fz_rethrow(ctx);
  567. }
  568. return fz_new_stream(ctx, state, read_aesd, close_aesd);
  569. }