libdeflate.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /*
  2. * libdeflate.h - public header for libdeflate
  3. */
  4. #ifndef LIBDEFLATE_H
  5. #define LIBDEFLATE_H
  6. #ifdef __cplusplus
  7. extern "C" {
  8. #endif
  9. #define LIBDEFLATE_VERSION_MAJOR 1
  10. #define LIBDEFLATE_VERSION_MINOR 14
  11. #define LIBDEFLATE_VERSION_STRING "1.14"
  12. #include <stddef.h>
  13. #include <stdint.h>
  14. /*
  15. * On Windows, if you want to link to the DLL version of libdeflate, then
  16. * #define LIBDEFLATE_DLL. Note that the calling convention is "cdecl".
  17. */
  18. #ifdef LIBDEFLATE_DLL
  19. # ifdef BUILDING_LIBDEFLATE
  20. # define LIBDEFLATEEXPORT LIBEXPORT
  21. # elif defined(_WIN32) || defined(__CYGWIN__)
  22. # define LIBDEFLATEEXPORT __declspec(dllimport)
  23. # endif
  24. #endif
  25. #ifndef LIBDEFLATEEXPORT
  26. # define LIBDEFLATEEXPORT
  27. #endif
  28. #if defined(BUILDING_LIBDEFLATE) && defined(__GNUC__) && \
  29. defined(_WIN32) && !defined(_WIN64)
  30. /*
  31. * On 32-bit Windows, gcc assumes 16-byte stack alignment but MSVC only 4.
  32. * Realign the stack when entering libdeflate to avoid crashing in SSE/AVX
  33. * code when called from an MSVC-compiled application.
  34. */
  35. # define LIBDEFLATEAPI __attribute__((force_align_arg_pointer))
  36. #else
  37. # define LIBDEFLATEAPI
  38. #endif
  39. /* ========================================================================== */
  40. /* Compression */
  41. /* ========================================================================== */
  42. struct libdeflate_compressor;
  43. /*
  44. * libdeflate_alloc_compressor() allocates a new compressor that supports
  45. * DEFLATE, zlib, and gzip compression. 'compression_level' is the compression
  46. * level on a zlib-like scale but with a higher maximum value (1 = fastest, 6 =
  47. * medium/default, 9 = slow, 12 = slowest). Level 0 is also supported and means
  48. * "no compression", specifically "create a valid stream, but only emit
  49. * uncompressed blocks" (this will expand the data slightly).
  50. *
  51. * The return value is a pointer to the new compressor, or NULL if out of memory
  52. * or if the compression level is invalid (i.e. outside the range [0, 12]).
  53. *
  54. * Note: for compression, the sliding window size is defined at compilation time
  55. * to 32768, the largest size permissible in the DEFLATE format. It cannot be
  56. * changed at runtime.
  57. *
  58. * A single compressor is not safe to use by multiple threads concurrently.
  59. * However, different threads may use different compressors concurrently.
  60. */
  61. LIBDEFLATEEXPORT struct libdeflate_compressor * LIBDEFLATEAPI
  62. libdeflate_alloc_compressor(int compression_level);
  63. /*
  64. * libdeflate_deflate_compress() performs raw DEFLATE compression on a buffer of
  65. * data. The function attempts to compress 'in_nbytes' bytes of data located at
  66. * 'in' and write the results to 'out', which has space for 'out_nbytes_avail'
  67. * bytes. The return value is the compressed size in bytes, or 0 if the data
  68. * could not be compressed to 'out_nbytes_avail' bytes or fewer.
  69. */
  70. LIBDEFLATEEXPORT size_t LIBDEFLATEAPI
  71. libdeflate_deflate_compress(struct libdeflate_compressor *compressor,
  72. const void *in, size_t in_nbytes,
  73. void *out, size_t out_nbytes_avail);
  74. /*
  75. * libdeflate_deflate_compress_bound() returns a worst-case upper bound on the
  76. * number of bytes of compressed data that may be produced by compressing any
  77. * buffer of length less than or equal to 'in_nbytes' using
  78. * libdeflate_deflate_compress() with the specified compressor. Mathematically,
  79. * this bound will necessarily be a number greater than or equal to 'in_nbytes'.
  80. * It may be an overestimate of the true upper bound. The return value is
  81. * guaranteed to be the same for all invocations with the same compressor and
  82. * same 'in_nbytes'.
  83. *
  84. * As a special case, 'compressor' may be NULL. This causes the bound to be
  85. * taken across *any* libdeflate_compressor that could ever be allocated with
  86. * this build of the library, with any options.
  87. *
  88. * Note that this function is not necessary in many applications. With
  89. * block-based compression, it is usually preferable to separately store the
  90. * uncompressed size of each block and to store any blocks that did not compress
  91. * to less than their original size uncompressed. In that scenario, there is no
  92. * need to know the worst-case compressed size, since the maximum number of
  93. * bytes of compressed data that may be used would always be one less than the
  94. * input length. You can just pass a buffer of that size to
  95. * libdeflate_deflate_compress() and store the data uncompressed if
  96. * libdeflate_deflate_compress() returns 0, indicating that the compressed data
  97. * did not fit into the provided output buffer.
  98. */
  99. LIBDEFLATEEXPORT size_t LIBDEFLATEAPI
  100. libdeflate_deflate_compress_bound(struct libdeflate_compressor *compressor,
  101. size_t in_nbytes);
  102. /*
  103. * Like libdeflate_deflate_compress(), but stores the data in the zlib wrapper
  104. * format.
  105. */
  106. LIBDEFLATEEXPORT size_t LIBDEFLATEAPI
  107. libdeflate_zlib_compress(struct libdeflate_compressor *compressor,
  108. const void *in, size_t in_nbytes,
  109. void *out, size_t out_nbytes_avail);
  110. /*
  111. * Like libdeflate_deflate_compress_bound(), but assumes the data will be
  112. * compressed with libdeflate_zlib_compress() rather than with
  113. * libdeflate_deflate_compress().
  114. */
  115. LIBDEFLATEEXPORT size_t LIBDEFLATEAPI
  116. libdeflate_zlib_compress_bound(struct libdeflate_compressor *compressor,
  117. size_t in_nbytes);
  118. /*
  119. * Like libdeflate_deflate_compress(), but stores the data in the gzip wrapper
  120. * format.
  121. */
  122. LIBDEFLATEEXPORT size_t LIBDEFLATEAPI
  123. libdeflate_gzip_compress(struct libdeflate_compressor *compressor,
  124. const void *in, size_t in_nbytes,
  125. void *out, size_t out_nbytes_avail);
  126. /*
  127. * Like libdeflate_deflate_compress_bound(), but assumes the data will be
  128. * compressed with libdeflate_gzip_compress() rather than with
  129. * libdeflate_deflate_compress().
  130. */
  131. LIBDEFLATEEXPORT size_t LIBDEFLATEAPI
  132. libdeflate_gzip_compress_bound(struct libdeflate_compressor *compressor,
  133. size_t in_nbytes);
  134. /*
  135. * libdeflate_free_compressor() frees a compressor that was allocated with
  136. * libdeflate_alloc_compressor(). If a NULL pointer is passed in, no action is
  137. * taken.
  138. */
  139. LIBDEFLATEEXPORT void LIBDEFLATEAPI
  140. libdeflate_free_compressor(struct libdeflate_compressor *compressor);
  141. /* ========================================================================== */
  142. /* Decompression */
  143. /* ========================================================================== */
  144. struct libdeflate_decompressor;
  145. /*
  146. * libdeflate_alloc_decompressor() allocates a new decompressor that can be used
  147. * for DEFLATE, zlib, and gzip decompression. The return value is a pointer to
  148. * the new decompressor, or NULL if out of memory.
  149. *
  150. * This function takes no parameters, and the returned decompressor is valid for
  151. * decompressing data that was compressed at any compression level and with any
  152. * sliding window size.
  153. *
  154. * A single decompressor is not safe to use by multiple threads concurrently.
  155. * However, different threads may use different decompressors concurrently.
  156. */
  157. LIBDEFLATEEXPORT struct libdeflate_decompressor * LIBDEFLATEAPI
  158. libdeflate_alloc_decompressor(void);
  159. /*
  160. * Result of a call to libdeflate_deflate_decompress(),
  161. * libdeflate_zlib_decompress(), or libdeflate_gzip_decompress().
  162. */
  163. enum libdeflate_result {
  164. /* Decompression was successful. */
  165. LIBDEFLATE_SUCCESS = 0,
  166. /* Decompressed failed because the compressed data was invalid, corrupt,
  167. * or otherwise unsupported. */
  168. LIBDEFLATE_BAD_DATA = 1,
  169. /* A NULL 'actual_out_nbytes_ret' was provided, but the data would have
  170. * decompressed to fewer than 'out_nbytes_avail' bytes. */
  171. LIBDEFLATE_SHORT_OUTPUT = 2,
  172. /* The data would have decompressed to more than 'out_nbytes_avail'
  173. * bytes. */
  174. LIBDEFLATE_INSUFFICIENT_SPACE = 3,
  175. };
  176. /*
  177. * libdeflate_deflate_decompress() decompresses the DEFLATE-compressed stream
  178. * from the buffer 'in' with compressed size up to 'in_nbytes' bytes. The
  179. * uncompressed data is written to 'out', a buffer with size 'out_nbytes_avail'
  180. * bytes. If decompression succeeds, then 0 (LIBDEFLATE_SUCCESS) is returned.
  181. * Otherwise, a nonzero result code such as LIBDEFLATE_BAD_DATA is returned. If
  182. * a nonzero result code is returned, then the contents of the output buffer are
  183. * undefined.
  184. *
  185. * Decompression stops at the end of the DEFLATE stream (as indicated by the
  186. * BFINAL flag), even if it is actually shorter than 'in_nbytes' bytes.
  187. *
  188. * libdeflate_deflate_decompress() can be used in cases where the actual
  189. * uncompressed size is known (recommended) or unknown (not recommended):
  190. *
  191. * - If the actual uncompressed size is known, then pass the actual
  192. * uncompressed size as 'out_nbytes_avail' and pass NULL for
  193. * 'actual_out_nbytes_ret'. This makes libdeflate_deflate_decompress() fail
  194. * with LIBDEFLATE_SHORT_OUTPUT if the data decompressed to fewer than the
  195. * specified number of bytes.
  196. *
  197. * - If the actual uncompressed size is unknown, then provide a non-NULL
  198. * 'actual_out_nbytes_ret' and provide a buffer with some size
  199. * 'out_nbytes_avail' that you think is large enough to hold all the
  200. * uncompressed data. In this case, if the data decompresses to less than
  201. * or equal to 'out_nbytes_avail' bytes, then
  202. * libdeflate_deflate_decompress() will write the actual uncompressed size
  203. * to *actual_out_nbytes_ret and return 0 (LIBDEFLATE_SUCCESS). Otherwise,
  204. * it will return LIBDEFLATE_INSUFFICIENT_SPACE if the provided buffer was
  205. * not large enough but no other problems were encountered, or another
  206. * nonzero result code if decompression failed for another reason.
  207. */
  208. LIBDEFLATEEXPORT enum libdeflate_result LIBDEFLATEAPI
  209. libdeflate_deflate_decompress(struct libdeflate_decompressor *decompressor,
  210. const void *in, size_t in_nbytes,
  211. void *out, size_t out_nbytes_avail,
  212. size_t *actual_out_nbytes_ret);
  213. /*
  214. * Like libdeflate_deflate_decompress(), but adds the 'actual_in_nbytes_ret'
  215. * argument. If decompression succeeds and 'actual_in_nbytes_ret' is not NULL,
  216. * then the actual compressed size of the DEFLATE stream (aligned to the next
  217. * byte boundary) is written to *actual_in_nbytes_ret.
  218. */
  219. LIBDEFLATEEXPORT enum libdeflate_result LIBDEFLATEAPI
  220. libdeflate_deflate_decompress_ex(struct libdeflate_decompressor *decompressor,
  221. const void *in, size_t in_nbytes,
  222. void *out, size_t out_nbytes_avail,
  223. size_t *actual_in_nbytes_ret,
  224. size_t *actual_out_nbytes_ret);
  225. /*
  226. * Like libdeflate_deflate_decompress(), but assumes the zlib wrapper format
  227. * instead of raw DEFLATE.
  228. *
  229. * Decompression will stop at the end of the zlib stream, even if it is shorter
  230. * than 'in_nbytes'. If you need to know exactly where the zlib stream ended,
  231. * use libdeflate_zlib_decompress_ex().
  232. */
  233. LIBDEFLATEEXPORT enum libdeflate_result LIBDEFLATEAPI
  234. libdeflate_zlib_decompress(struct libdeflate_decompressor *decompressor,
  235. const void *in, size_t in_nbytes,
  236. void *out, size_t out_nbytes_avail,
  237. size_t *actual_out_nbytes_ret);
  238. /*
  239. * Like libdeflate_zlib_decompress(), but adds the 'actual_in_nbytes_ret'
  240. * argument. If 'actual_in_nbytes_ret' is not NULL and the decompression
  241. * succeeds (indicating that the first zlib-compressed stream in the input
  242. * buffer was decompressed), then the actual number of input bytes consumed is
  243. * written to *actual_in_nbytes_ret.
  244. */
  245. LIBDEFLATEEXPORT enum libdeflate_result LIBDEFLATEAPI
  246. libdeflate_zlib_decompress_ex(struct libdeflate_decompressor *decompressor,
  247. const void *in, size_t in_nbytes,
  248. void *out, size_t out_nbytes_avail,
  249. size_t *actual_in_nbytes_ret,
  250. size_t *actual_out_nbytes_ret);
  251. /*
  252. * Like libdeflate_deflate_decompress(), but assumes the gzip wrapper format
  253. * instead of raw DEFLATE.
  254. *
  255. * If multiple gzip-compressed members are concatenated, then only the first
  256. * will be decompressed. Use libdeflate_gzip_decompress_ex() if you need
  257. * multi-member support.
  258. */
  259. LIBDEFLATEEXPORT enum libdeflate_result LIBDEFLATEAPI
  260. libdeflate_gzip_decompress(struct libdeflate_decompressor *decompressor,
  261. const void *in, size_t in_nbytes,
  262. void *out, size_t out_nbytes_avail,
  263. size_t *actual_out_nbytes_ret);
  264. /*
  265. * Like libdeflate_gzip_decompress(), but adds the 'actual_in_nbytes_ret'
  266. * argument. If 'actual_in_nbytes_ret' is not NULL and the decompression
  267. * succeeds (indicating that the first gzip-compressed member in the input
  268. * buffer was decompressed), then the actual number of input bytes consumed is
  269. * written to *actual_in_nbytes_ret.
  270. */
  271. LIBDEFLATEEXPORT enum libdeflate_result LIBDEFLATEAPI
  272. libdeflate_gzip_decompress_ex(struct libdeflate_decompressor *decompressor,
  273. const void *in, size_t in_nbytes,
  274. void *out, size_t out_nbytes_avail,
  275. size_t *actual_in_nbytes_ret,
  276. size_t *actual_out_nbytes_ret);
  277. /*
  278. * libdeflate_free_decompressor() frees a decompressor that was allocated with
  279. * libdeflate_alloc_decompressor(). If a NULL pointer is passed in, no action
  280. * is taken.
  281. */
  282. LIBDEFLATEEXPORT void LIBDEFLATEAPI
  283. libdeflate_free_decompressor(struct libdeflate_decompressor *decompressor);
  284. /* ========================================================================== */
  285. /* Checksums */
  286. /* ========================================================================== */
  287. /*
  288. * libdeflate_adler32() updates a running Adler-32 checksum with 'len' bytes of
  289. * data and returns the updated checksum. When starting a new checksum, the
  290. * required initial value for 'adler' is 1. This value is also returned when
  291. * 'buffer' is specified as NULL.
  292. */
  293. LIBDEFLATEEXPORT uint32_t LIBDEFLATEAPI
  294. libdeflate_adler32(uint32_t adler, const void *buffer, size_t len);
  295. /*
  296. * libdeflate_crc32() updates a running CRC-32 checksum with 'len' bytes of data
  297. * and returns the updated checksum. When starting a new checksum, the required
  298. * initial value for 'crc' is 0. This value is also returned when 'buffer' is
  299. * specified as NULL.
  300. */
  301. LIBDEFLATEEXPORT uint32_t LIBDEFLATEAPI
  302. libdeflate_crc32(uint32_t crc, const void *buffer, size_t len);
  303. /* ========================================================================== */
  304. /* Custom memory allocator */
  305. /* ========================================================================== */
  306. /*
  307. * Install a custom memory allocator which libdeflate will use for all memory
  308. * allocations. 'malloc_func' is a function that must behave like malloc(), and
  309. * 'free_func' is a function that must behave like free().
  310. *
  311. * There must not be any libdeflate_compressor or libdeflate_decompressor
  312. * structures in existence when calling this function.
  313. */
  314. LIBDEFLATEEXPORT void LIBDEFLATEAPI
  315. libdeflate_set_memory_allocator(void *(*malloc_func)(size_t),
  316. void (*free_func)(void *));
  317. #ifdef __cplusplus
  318. }
  319. #endif
  320. #endif /* LIBDEFLATE_H */