PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/source/fitz/filter-basic.c

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