uzlib.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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-2018 by Paul Sokolovsky
  9. *
  10. * This software is provided 'as-is', without any express
  11. * or implied warranty. In no event will the authors be
  12. * held liable for any damages arising from the use of
  13. * this software.
  14. *
  15. * Permission is granted to anyone to use this software
  16. * for any purpose, including commercial applications,
  17. * and to alter it and redistribute it freely, subject to
  18. * the following restrictions:
  19. *
  20. * 1. The origin of this software must not be
  21. * misrepresented; you must not claim that you
  22. * wrote the original software. If you use this
  23. * software in a product, an acknowledgment in
  24. * the product documentation would be appreciated
  25. * but is not required.
  26. *
  27. * 2. Altered source versions must be plainly marked
  28. * as such, and must not be misrepresented as
  29. * being the original software.
  30. *
  31. * 3. This notice may not be removed or altered from
  32. * any source distribution.
  33. */
  34. #ifndef UZLIB_H_INCLUDED
  35. #define UZLIB_H_INCLUDED
  36. #include <stdlib.h>
  37. #include <stdint.h>
  38. #include <stdbool.h>
  39. #include "defl_static.h"
  40. #include "uzlib_conf.h"
  41. #if UZLIB_CONF_DEBUG_LOG
  42. #include <stdio.h>
  43. #endif
  44. /* calling convention */
  45. #ifndef TINFCC
  46. #ifdef __WATCOMC__
  47. #define TINFCC __cdecl
  48. #else
  49. #define TINFCC
  50. #endif
  51. #endif
  52. #ifdef __cplusplus
  53. extern "C" {
  54. #endif
  55. /* ok status, more data produced */
  56. #define TINF_OK 0
  57. /* end of compressed stream reached */
  58. #define TINF_DONE 1
  59. #define TINF_DATA_ERROR (-3)
  60. #define TINF_CHKSUM_ERROR (-4)
  61. #define TINF_DICT_ERROR (-5)
  62. /* checksum types */
  63. #define TINF_CHKSUM_NONE 0
  64. #define TINF_CHKSUM_ADLER 1
  65. #define TINF_CHKSUM_CRC 2
  66. /* helper macros */
  67. #define TINF_ARRAY_SIZE(arr) (sizeof(arr) / sizeof(*(arr)))
  68. /* data structures */
  69. typedef struct {
  70. unsigned short table[16]; /* table of code length counts */
  71. unsigned short trans[288]; /* code -> symbol translation table */
  72. } TINF_TREE;
  73. struct uzlib_uncomp {
  74. /* Pointer to the next byte in the input buffer */
  75. const unsigned char *source;
  76. /* Pointer to the next byte past the input buffer (source_limit = source + len) */
  77. const unsigned char *source_limit;
  78. /* If source_limit == NULL, or source >= source_limit, this function
  79. will be used to read next byte from source stream. The function may
  80. also return -1 in case of EOF (or irrecoverable error). Note that
  81. besides returning the next byte, it may also update source and
  82. source_limit fields, thus allowing for buffered operation. */
  83. int (*source_read_cb)(struct uzlib_uncomp *uncomp);
  84. unsigned int tag;
  85. unsigned int bitcount;
  86. /* Destination (output) buffer start */
  87. unsigned char *dest_start;
  88. /* Current pointer in dest buffer */
  89. unsigned char *dest;
  90. /* Pointer past the end of the dest buffer, similar to source_limit */
  91. unsigned char *dest_limit;
  92. /* Accumulating checksum */
  93. unsigned int checksum;
  94. char checksum_type;
  95. bool eof;
  96. int btype;
  97. int bfinal;
  98. unsigned int curlen;
  99. int lzOff;
  100. unsigned char *dict_ring;
  101. unsigned int dict_size;
  102. unsigned int dict_idx;
  103. TINF_TREE ltree; /* dynamic length/symbol tree */
  104. TINF_TREE dtree; /* dynamic distance tree */
  105. };
  106. #include "tinf_compat.h"
  107. #define TINF_PUT(d, c) \
  108. { \
  109. *d->dest++ = c; \
  110. if (d->dict_ring) { d->dict_ring[d->dict_idx++] = c; if (d->dict_idx == d->dict_size) d->dict_idx = 0; } \
  111. }
  112. unsigned char TINFCC uzlib_get_byte(TINF_DATA *d);
  113. /* Decompression API */
  114. void TINFCC uzlib_init(void);
  115. void TINFCC uzlib_uncompress_init(TINF_DATA *d, void *dict, unsigned int dictLen);
  116. int TINFCC uzlib_uncompress(TINF_DATA *d);
  117. int TINFCC uzlib_uncompress_chksum(TINF_DATA *d);
  118. int TINFCC uzlib_zlib_parse_header(TINF_DATA *d);
  119. int TINFCC uzlib_gzip_parse_header(TINF_DATA *d);
  120. /* Compression API */
  121. typedef const uint8_t *uzlib_hash_entry_t;
  122. struct uzlib_comp {
  123. struct Outbuf out;
  124. uzlib_hash_entry_t *hash_table;
  125. unsigned int hash_bits;
  126. unsigned int dict_size;
  127. };
  128. void TINFCC uzlib_compress(struct uzlib_comp *c, const uint8_t *src, unsigned slen);
  129. /* Checksum API */
  130. /* prev_sum is previous value for incremental computation, 1 initially */
  131. uint32_t TINFCC uzlib_adler32(const void *data, unsigned int length, uint32_t prev_sum);
  132. /* crc is previous value for incremental computation, 0xffffffff initially */
  133. uint32_t TINFCC uzlib_crc32(const void *data, unsigned int length, uint32_t crc);
  134. #ifdef __cplusplus
  135. } /* extern "C" */
  136. #endif
  137. #endif /* UZLIB_H_INCLUDED */