inflate.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /* inflate: freestanding DEFLATE / zlib / gzip decoder.
  2. *
  3. * No libc, no allocation: the caller provides the workspace (struct inflate,
  4. * about 33 KB) and an output sink. The compressed input is a sequence of
  5. * memory segments, so a PNG decoder can feed consecutive IDAT chunks without
  6. * copying them.
  7. *
  8. * Used by the image library (PNG) and by the kernel's compressed command pack
  9. * (gzip). See img_png.c and ramfs.c. */
  10. #ifndef INFLATE_H
  11. #define INFLATE_H
  12. #define INFLATE_MAXBITS 15
  13. struct inflate_src {
  14. const unsigned char *p;
  15. unsigned int len;
  16. /* Called when p/len are exhausted: return 1 and set a new segment,
  17. * or 0 at the end of the input. */
  18. int (*more)(void *ctx, const unsigned char **p, unsigned int *len);
  19. void *ctx;
  20. };
  21. struct inflate_sink {
  22. int (*write)(void *ctx, const unsigned char *data, unsigned int len);
  23. void *ctx;
  24. };
  25. struct inflate_huff {
  26. unsigned short count[INFLATE_MAXBITS + 1];
  27. unsigned short symbol[288];
  28. };
  29. struct inflate {
  30. struct inflate_src src;
  31. struct inflate_sink sink;
  32. unsigned int bitbuf;
  33. int bitcnt;
  34. unsigned long long total_bits;
  35. unsigned char win[32768]; /* LZ77 history / pending output */
  36. unsigned int wpos;
  37. unsigned int pending;
  38. unsigned long long total; /* bytes produced */
  39. unsigned int crc; /* running CRC32 (pre-inverted) */
  40. unsigned int adler_a, adler_b;
  41. };
  42. void inflate_init(struct inflate *z, struct inflate_src *src,
  43. struct inflate_sink *sink);
  44. /* Raw DEFLATE stream from the current source position. */
  45. int inflate_raw(struct inflate *z, unsigned int *out_len);
  46. /* zlib stream (2-byte header, DEFLATE, Adler-32). */
  47. int inflate_zlib(struct inflate *z, unsigned int *out_len);
  48. /* gzip stream (10+ byte header, DEFLATE, CRC32 + ISIZE). */
  49. int inflate_gzip(struct inflate *z, unsigned int *out_len);
  50. /* Entire in-memory helpers. `ws` must point at sizeof(struct inflate) bytes
  51. * with at least 16-byte alignment. Return 0 on success. */
  52. int inflate_zlib_mem(const void *src, unsigned int len, void *dst,
  53. unsigned int cap, unsigned int *out_len, void *ws);
  54. int inflate_gzip_mem(const void *src, unsigned int len, void *dst,
  55. unsigned int cap, unsigned int *out_len, void *ws);
  56. int inflate_raw_mem(const void *src, unsigned int len, void *dst,
  57. unsigned int cap, unsigned int *out_len, void *ws);
  58. unsigned int crc32_bytes(const void *data, unsigned int len);
  59. unsigned int adler32_bytes(const void *data, unsigned int len);
  60. #endif