tinf.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * uzlib - tiny deflate/inflate library (deflate, gzip, zlib)
  3. *
  4. * Copyright (c) 2003 by Joergen Ibsen / Jibz
  5. * All Rights Reserved
  6. * http://www.ibsensoftware.com/
  7. *
  8. * Copyright (c) 2014-2016 by Paul Sokolovsky
  9. */
  10. #ifndef TINF_H_INCLUDED
  11. #define TINF_H_INCLUDED
  12. #include <stdlib.h>
  13. #include <stdint.h>
  14. #include <stdbool.h>
  15. /* calling convention */
  16. #ifndef TINFCC
  17. #ifdef __WATCOMC__
  18. #define TINFCC __cdecl
  19. #else
  20. #define TINFCC
  21. #endif
  22. #endif
  23. #ifdef __cplusplus
  24. extern "C" {
  25. #endif
  26. /* ok status, more data produced */
  27. #define TINF_OK 0
  28. /* end of compressed stream reached */
  29. #define TINF_DONE 1
  30. #define TINF_DATA_ERROR (-3)
  31. #define TINF_CHKSUM_ERROR (-4)
  32. #define TINF_DICT_ERROR (-5)
  33. /* checksum types */
  34. #define TINF_CHKSUM_NONE 0
  35. #define TINF_CHKSUM_ADLER 1
  36. #define TINF_CHKSUM_CRC 2
  37. /* helper macros */
  38. #define TINF_ARRAY_SIZE(arr) (sizeof(arr) / sizeof(*(arr)))
  39. /* data structures */
  40. typedef struct {
  41. unsigned short table[16]; /* table of code length counts */
  42. unsigned short trans[288]; /* code -> symbol translation table */
  43. } TINF_TREE;
  44. struct TINF_DATA;
  45. typedef struct TINF_DATA {
  46. /* Pointer to the next byte in the input buffer */
  47. const unsigned char *source;
  48. /* Pointer to the next byte past the input buffer (source_limit = source + len) */
  49. const unsigned char *source_limit;
  50. /* If source_limit == NULL, or source >= source_limit, this function
  51. will be used to read next byte from source stream. The function may
  52. also return -1 in
  53. case of EOF (or irrecoverable error). Note that besides returning
  54. the next byte, it may also update source and sourceRemaining fields,
  55. thus allowing for buffered operation. */
  56. int (*readSource)(struct TINF_DATA *data);
  57. unsigned int tag;
  58. unsigned int bitcount;
  59. /* Buffer start */
  60. unsigned char *destStart;
  61. /* Buffer total size */
  62. unsigned int destSize;
  63. /* Current pointer in buffer */
  64. unsigned char *dest;
  65. /* Remaining bytes in buffer */
  66. unsigned int destRemaining;
  67. /* Accumulating checksum */
  68. unsigned int checksum;
  69. char checksum_type;
  70. bool eof;
  71. int btype;
  72. int bfinal;
  73. unsigned int curlen;
  74. int lzOff;
  75. unsigned char *dict_ring;
  76. unsigned int dict_size;
  77. unsigned int dict_idx;
  78. TINF_TREE ltree; /* dynamic length/symbol tree */
  79. TINF_TREE dtree; /* dynamic distance tree */
  80. } TINF_DATA;
  81. #define TINF_PUT(d, c) \
  82. { \
  83. *d->dest++ = c; \
  84. if (d->dict_ring) { d->dict_ring[d->dict_idx++] = c; if (d->dict_idx == d->dict_size) d->dict_idx = 0; } \
  85. }
  86. unsigned char TINFCC uzlib_get_byte(TINF_DATA *d);
  87. /* Decompression API */
  88. void TINFCC uzlib_init(void);
  89. void TINFCC uzlib_uncompress_init(TINF_DATA *d, void *dict, unsigned int dictLen);
  90. int TINFCC uzlib_uncompress(TINF_DATA *d);
  91. int TINFCC uzlib_uncompress_chksum(TINF_DATA *d);
  92. int TINFCC uzlib_zlib_parse_header(TINF_DATA *d);
  93. int TINFCC uzlib_gzip_parse_header(TINF_DATA *d);
  94. /* Compression API */
  95. void TINFCC uzlib_compress(void *data, const uint8_t *src, unsigned slen);
  96. /* Checksum API */
  97. /* prev_sum is previous value for incremental computation, 1 initially */
  98. uint32_t TINFCC uzlib_adler32(const void *data, unsigned int length, uint32_t prev_sum);
  99. /* crc is previous value for incremental computation, 0xffffffff initially */
  100. uint32_t TINFCC uzlib_crc32(const void *data, unsigned int length, uint32_t crc);
  101. #ifdef __cplusplus
  102. } /* extern "C" */
  103. #endif
  104. #endif /* TINF_H_INCLUDED */