encode-basic.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. // Copyright (C) 2004-2025 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 "z-imp.h"
  24. #include <limits.h>
  25. struct ahx
  26. {
  27. fz_output *chain;
  28. int column;
  29. };
  30. static void ahx_write(fz_context *ctx, void *opaque, const void *data, size_t n)
  31. {
  32. static const char tohex[17] = "0123456789ABCDEF";
  33. struct ahx *state = opaque;
  34. const unsigned char *p = data;
  35. while (n-- > 0)
  36. {
  37. int c = *p++;
  38. fz_write_byte(ctx, state->chain, tohex[(c>>4) & 15]);
  39. fz_write_byte(ctx, state->chain, tohex[(c) & 15]);
  40. state->column += 2;
  41. if (state->column == 64)
  42. {
  43. fz_write_byte(ctx, state->chain, '\n');
  44. state->column = 0;
  45. }
  46. }
  47. }
  48. static void ahx_close(fz_context *ctx, void *opaque)
  49. {
  50. struct ahx *state = opaque;
  51. fz_write_byte(ctx, state->chain, '>');
  52. }
  53. static void ahx_reset(fz_context *ctx, void *opaque)
  54. {
  55. struct ahx *state = opaque;
  56. state->column = 0;
  57. fz_reset_output(ctx, state->chain);
  58. }
  59. static void ahx_drop(fz_context *ctx, void *opaque)
  60. {
  61. struct ahx *state = opaque;
  62. fz_free(ctx, state);
  63. }
  64. fz_output *
  65. fz_new_asciihex_output(fz_context *ctx, fz_output *chain)
  66. {
  67. fz_output *out;
  68. struct ahx *state = fz_malloc_struct(ctx, struct ahx);
  69. state->chain = chain;
  70. state->column = 0;
  71. out = fz_new_output(ctx, 512, state, ahx_write, ahx_close, ahx_drop);
  72. out->reset = ahx_reset;
  73. return out;
  74. }
  75. struct a85
  76. {
  77. fz_output *chain;
  78. int column;
  79. unsigned int word, n;
  80. };
  81. static void a85_flush(fz_context *ctx, struct a85 *state)
  82. {
  83. unsigned int v1, v2, v3, v4, v5;
  84. v5 = state->word;
  85. v4 = v5 / 85;
  86. v3 = v4 / 85;
  87. v2 = v3 / 85;
  88. v1 = v2 / 85;
  89. if (state->column >= 70)
  90. {
  91. fz_write_byte(ctx, state->chain, '\n');
  92. state->column = 0;
  93. }
  94. if (state->n == 4)
  95. {
  96. if (state->word == 0)
  97. {
  98. fz_write_byte(ctx, state->chain, 'z');
  99. state->column += 1;
  100. }
  101. else
  102. {
  103. fz_write_byte(ctx, state->chain, (v1 % 85) + '!');
  104. fz_write_byte(ctx, state->chain, (v2 % 85) + '!');
  105. fz_write_byte(ctx, state->chain, (v3 % 85) + '!');
  106. fz_write_byte(ctx, state->chain, (v4 % 85) + '!');
  107. fz_write_byte(ctx, state->chain, (v5 % 85) + '!');
  108. state->column += 5;
  109. }
  110. }
  111. else if (state->n == 3)
  112. {
  113. fz_write_byte(ctx, state->chain, (v2 % 85) + '!');
  114. fz_write_byte(ctx, state->chain, (v3 % 85) + '!');
  115. fz_write_byte(ctx, state->chain, (v4 % 85) + '!');
  116. fz_write_byte(ctx, state->chain, (v5 % 85) + '!');
  117. state->column += 4;
  118. }
  119. else if (state->n == 2)
  120. {
  121. fz_write_byte(ctx, state->chain, (v3 % 85) + '!');
  122. fz_write_byte(ctx, state->chain, (v4 % 85) + '!');
  123. fz_write_byte(ctx, state->chain, (v5 % 85) + '!');
  124. state->column += 3;
  125. }
  126. else if (state->n == 1)
  127. {
  128. fz_write_byte(ctx, state->chain, (v4 % 85) + '!');
  129. fz_write_byte(ctx, state->chain, (v5 % 85) + '!');
  130. state->column += 2;
  131. }
  132. state->word = 0;
  133. state->n = 0;
  134. }
  135. static void a85_write(fz_context *ctx, void *opaque, const void *data, size_t n)
  136. {
  137. struct a85 *state = opaque;
  138. const unsigned char *p = data;
  139. while (n-- > 0)
  140. {
  141. unsigned int c = *p++;
  142. if (state->n == 4)
  143. a85_flush(ctx, state);
  144. state->word = (state->word << 8) | c;
  145. state->n++;
  146. }
  147. }
  148. static void a85_close(fz_context *ctx, void *opaque)
  149. {
  150. struct a85 *state = opaque;
  151. a85_flush(ctx, state);
  152. fz_write_byte(ctx, state->chain, '~');
  153. fz_write_byte(ctx, state->chain, '>');
  154. }
  155. static void a85_reset(fz_context *ctx, void *opaque)
  156. {
  157. struct a85 *state = opaque;
  158. state->column = 0;
  159. state->word = 0;
  160. state->n = 0;
  161. fz_reset_output(ctx, state->chain);
  162. }
  163. static void a85_drop(fz_context *ctx, void *opaque)
  164. {
  165. struct a85 *state = opaque;
  166. fz_free(ctx, state);
  167. }
  168. fz_output *
  169. fz_new_ascii85_output(fz_context *ctx, fz_output *chain)
  170. {
  171. fz_output *out;
  172. struct a85 *state = fz_malloc_struct(ctx, struct a85);
  173. state->chain = chain;
  174. state->column = 0;
  175. state->word = 0;
  176. state->n = 0;
  177. out = fz_new_output(ctx, 512, state, a85_write, a85_close, a85_drop);
  178. out->reset = a85_reset;
  179. return out;
  180. }
  181. struct rle
  182. {
  183. fz_output *chain;
  184. int state;
  185. int run;
  186. unsigned char buf[128];
  187. };
  188. enum { ZERO, ONE, DIFF, SAME };
  189. static void rle_flush_same(fz_context *ctx, struct rle *enc)
  190. {
  191. fz_write_byte(ctx, enc->chain, 257 - enc->run);
  192. fz_write_byte(ctx, enc->chain, enc->buf[0]);
  193. }
  194. static void rle_flush_diff(fz_context *ctx, struct rle *enc)
  195. {
  196. fz_write_byte(ctx, enc->chain, enc->run - 1);
  197. fz_write_data(ctx, enc->chain, enc->buf, enc->run);
  198. }
  199. static void rle_write(fz_context *ctx, void *opaque, const void *data, size_t n)
  200. {
  201. struct rle *enc = opaque;
  202. const unsigned char *p = data;
  203. while (n-- > 0)
  204. {
  205. int c = *p++;
  206. switch (enc->state)
  207. {
  208. case ZERO:
  209. enc->state = ONE;
  210. enc->run = 1;
  211. enc->buf[0] = c;
  212. break;
  213. case ONE:
  214. enc->state = DIFF;
  215. enc->run = 2;
  216. enc->buf[1] = c;
  217. break;
  218. case DIFF:
  219. /* Max run length */
  220. if (enc->run == 128)
  221. {
  222. rle_flush_diff(ctx, enc);
  223. enc->state = ONE;
  224. enc->run = 1;
  225. enc->buf[0] = c;
  226. }
  227. /* Run of three same */
  228. else if ((enc->run >= 2) && (c == enc->buf[enc->run-1]) && (c == enc->buf[enc->run-2]))
  229. {
  230. if (enc->run >= 3) {
  231. enc->run -= 2; /* skip last two in previous run */
  232. rle_flush_diff(ctx, enc);
  233. }
  234. enc->state = SAME;
  235. enc->run = 3;
  236. enc->buf[0] = c;
  237. }
  238. else
  239. {
  240. enc->buf[enc->run] = c;
  241. enc->run++;
  242. }
  243. break;
  244. case SAME:
  245. if ((enc->run == 128) || (c != enc->buf[0]))
  246. {
  247. rle_flush_same(ctx, enc);
  248. enc->state = ONE;
  249. enc->run = 1;
  250. enc->buf[0] = c;
  251. }
  252. else
  253. {
  254. enc->run++;
  255. }
  256. }
  257. }
  258. }
  259. static void rle_close(fz_context *ctx, void *opaque)
  260. {
  261. struct rle *enc = opaque;
  262. switch (enc->state)
  263. {
  264. case ZERO: break;
  265. case ONE: rle_flush_diff(ctx, enc); break;
  266. case DIFF: rle_flush_diff(ctx, enc); break;
  267. case SAME: rle_flush_same(ctx, enc); break;
  268. }
  269. fz_write_byte(ctx, enc->chain, 128);
  270. }
  271. static void rle_reset(fz_context *ctx, void *opaque)
  272. {
  273. struct rle *enc = opaque;
  274. enc->state = ZERO;
  275. enc->run = 0;
  276. fz_reset_output(ctx, enc->chain);
  277. }
  278. static void rle_drop(fz_context *ctx, void *opaque)
  279. {
  280. struct rle *enc = opaque;
  281. fz_free(ctx, enc);
  282. }
  283. fz_output *
  284. fz_new_rle_output(fz_context *ctx, fz_output *chain)
  285. {
  286. fz_output *out;
  287. struct rle *enc = fz_malloc_struct(ctx, struct rle);
  288. enc->chain = chain;
  289. enc->state = ZERO;
  290. enc->run = 0;
  291. out = fz_new_output(ctx, 4096, enc, rle_write, rle_close, rle_drop);
  292. out->reset = rle_reset;
  293. return out;
  294. }
  295. struct arc4
  296. {
  297. fz_output *chain;
  298. fz_arc4 arc4;
  299. fz_arc4 arc4_orig;
  300. };
  301. static void arc4_write(fz_context *ctx, void *opaque, const void *data, size_t n)
  302. {
  303. struct arc4 *state = opaque;
  304. const unsigned char *p = data;
  305. unsigned char buffer[256];
  306. while (n > 0)
  307. {
  308. size_t x = (n > sizeof buffer) ? sizeof buffer : n;
  309. fz_arc4_encrypt(&state->arc4, buffer, p, x);
  310. fz_write_data(ctx, state->chain, buffer, x);
  311. p += x;
  312. n -= x;
  313. }
  314. }
  315. static void arc4_reset(fz_context *ctx, void *opaque)
  316. {
  317. struct arc4 *state = opaque;
  318. memcpy(&state->arc4, &state->arc4_orig, sizeof(state->arc4));
  319. fz_reset_output(ctx, state->chain);
  320. }
  321. static void arc4_drop(fz_context *ctx, void *opaque)
  322. {
  323. fz_free(ctx, opaque);
  324. }
  325. fz_output *
  326. fz_new_arc4_output(fz_context *ctx, fz_output *chain, unsigned char *key, size_t keylen)
  327. {
  328. fz_output *out;
  329. struct arc4 *state = fz_malloc_struct(ctx, struct arc4);
  330. state->chain = chain;
  331. fz_arc4_init(&state->arc4, key, keylen);
  332. memcpy(&state->arc4_orig, &state->arc4, sizeof(state->arc4));
  333. out = fz_new_output(ctx, 256, state, arc4_write, NULL, arc4_drop);
  334. out->reset = arc4_reset;
  335. return out;
  336. }
  337. struct deflate
  338. {
  339. fz_output *chain;
  340. z_stream z;
  341. uInt bufsize;
  342. unsigned char *buf;
  343. };
  344. static void deflate_write(fz_context *ctx, void *opaque, const void *data, size_t n)
  345. {
  346. struct deflate *state = opaque;
  347. const unsigned char *p = data;
  348. uLong newbufsizeLong;
  349. uInt newbufsize;
  350. int err;
  351. newbufsizeLong = n >= UINT_MAX ? UINT_MAX : deflateBound(&state->z, (uLong)n);
  352. newbufsize = (uInt)(newbufsizeLong >= UINT_MAX ? UINT_MAX : newbufsizeLong);
  353. if (state->buf == NULL)
  354. {
  355. state->buf = Memento_label(fz_malloc(ctx, newbufsize), "deflate_buffer");
  356. state->bufsize = newbufsize;
  357. }
  358. else if (newbufsize > state->bufsize)
  359. {
  360. state->buf = Memento_label(fz_realloc(ctx, state->buf, newbufsize), "deflate_buffer");
  361. state->bufsize = newbufsize;
  362. }
  363. while (n > 0)
  364. {
  365. state->z.avail_in = n <= UINT_MAX ? (uInt)n : UINT_MAX;
  366. state->z.next_in = (unsigned char *) p;
  367. n -= state->z.avail_in;
  368. p += state->z.avail_in;
  369. do
  370. {
  371. state->z.next_out = state->buf;
  372. state->z.avail_out = state->bufsize;
  373. err = deflate(&state->z, Z_NO_FLUSH);
  374. if (err != Z_OK)
  375. fz_throw(ctx, FZ_ERROR_LIBRARY, "zlib compression failed: %d", err);
  376. if (state->z.avail_out < state->bufsize)
  377. fz_write_data(ctx, state->chain, state->buf, state->bufsize - state->z.avail_out);
  378. } while (state->z.avail_in > 0);
  379. }
  380. }
  381. static void deflate_close(fz_context *ctx, void *opaque)
  382. {
  383. struct deflate *state = opaque;
  384. int err;
  385. state->z.next_in = NULL;
  386. state->z.avail_in = 0;
  387. do
  388. {
  389. state->z.next_out = state->buf;
  390. state->z.avail_out = state->bufsize;
  391. err = deflate(&state->z, Z_FINISH);
  392. if (state->z.avail_out < state->bufsize)
  393. fz_write_data(ctx, state->chain, state->buf, state->bufsize - state->z.avail_out);
  394. } while (err == Z_OK);
  395. if (err != Z_STREAM_END)
  396. fz_throw(ctx, FZ_ERROR_LIBRARY, "zlib compression failed: %d", err);
  397. }
  398. static void deflate_reset(fz_context *ctx, void *opaque)
  399. {
  400. struct deflate *state = opaque;
  401. int err = deflateReset(&state->z);
  402. if (err != Z_OK)
  403. fz_throw(ctx, FZ_ERROR_LIBRARY, "zlib reset failed: %d", err);
  404. fz_reset_output(ctx, state->chain);
  405. }
  406. static void deflate_drop(fz_context *ctx, void *opaque)
  407. {
  408. struct deflate *state = opaque;
  409. (void)deflateEnd(&state->z);
  410. fz_free(ctx, state->buf);
  411. fz_free(ctx, state);
  412. }
  413. fz_output *
  414. fz_new_deflate_output(fz_context *ctx, fz_output *chain, int effort, int raw)
  415. {
  416. fz_output *out;
  417. int err;
  418. struct deflate *state = fz_malloc_struct(ctx, struct deflate);
  419. state->chain = chain;
  420. state->z.opaque = ctx;
  421. state->z.zalloc = fz_zlib_alloc;
  422. state->z.zfree = fz_zlib_free;
  423. err = deflateInit2(&state->z, effort, Z_DEFLATED, raw ? -15 : 15, 8, Z_DEFAULT_STRATEGY);
  424. if (err != Z_OK)
  425. {
  426. (void)deflateEnd(&state->z);
  427. fz_free(ctx, state);
  428. fz_throw(ctx, FZ_ERROR_LIBRARY, "zlib deflateInit2 failed: %d", err);
  429. }
  430. out = fz_new_output(ctx, 8192, state, deflate_write, deflate_close, deflate_drop);
  431. out->reset = deflate_reset;
  432. return out;
  433. }