blosc.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. /*********************************************************************
  2. Blosc - Blocked Shuffling and Compression Library
  3. Author: Francesc Alted <francesc@blosc.org>
  4. See LICENSES/BLOSC.txt for details about copyright and rights to use.
  5. **********************************************************************/
  6. #ifndef BLOSC_H
  7. #define BLOSC_H
  8. #include <limits.h>
  9. #include <stdlib.h>
  10. #include "blosc-export.h"
  11. #ifdef __cplusplus
  12. extern "C" {
  13. #endif
  14. /* Version numbers */
  15. #define BLOSC_VERSION_MAJOR 1 /* for major interface/format changes */
  16. #define BLOSC_VERSION_MINOR 14 /* for minor interface/format changes */
  17. #define BLOSC_VERSION_RELEASE 3 /* for tweaks, bug-fixes, or development */
  18. #define BLOSC_VERSION_STRING "1.14.3" /* string version. Sync with above! */
  19. #define BLOSC_VERSION_REVISION "$Rev$" /* revision version */
  20. #define BLOSC_VERSION_DATE "$Date:: 2018-04-06 #$" /* date version */
  21. #define BLOSCLZ_VERSION_STRING "1.1.0" /* the internal compressor version */
  22. /* The *_FORMAT symbols should be just 1-byte long */
  23. #define BLOSC_VERSION_FORMAT 2 /* Blosc format version, starting at 1 */
  24. /* Minimum header length */
  25. #define BLOSC_MIN_HEADER_LENGTH 16
  26. /* The maximum overhead during compression in bytes. This equals to
  27. BLOSC_MIN_HEADER_LENGTH now, but can be higher in future
  28. implementations */
  29. #define BLOSC_MAX_OVERHEAD BLOSC_MIN_HEADER_LENGTH
  30. /* Maximum source buffer size to be compressed */
  31. #define BLOSC_MAX_BUFFERSIZE (INT_MAX - BLOSC_MAX_OVERHEAD)
  32. /* Maximum typesize before considering source buffer as a stream of bytes */
  33. #define BLOSC_MAX_TYPESIZE 255 /* Cannot be larger than 255 */
  34. /* The maximum number of threads (for some static arrays) */
  35. #define BLOSC_MAX_THREADS 256
  36. /* Codes for shuffling (see blosc_compress) */
  37. #define BLOSC_NOSHUFFLE 0 /* no shuffle */
  38. #define BLOSC_SHUFFLE 1 /* byte-wise shuffle */
  39. #define BLOSC_BITSHUFFLE 2 /* bit-wise shuffle */
  40. /* Codes for internal flags (see blosc_cbuffer_metainfo) */
  41. #define BLOSC_DOSHUFFLE 0x1 /* byte-wise shuffle */
  42. #define BLOSC_MEMCPYED 0x2 /* plain copy */
  43. #define BLOSC_DOBITSHUFFLE 0x4 /* bit-wise shuffle */
  44. /* Codes for the different compressors shipped with Blosc */
  45. #define BLOSC_BLOSCLZ 0
  46. #define BLOSC_LZ4 1
  47. #define BLOSC_LZ4HC 2
  48. #define BLOSC_SNAPPY 3
  49. #define BLOSC_ZLIB 4
  50. #define BLOSC_ZSTD 5
  51. /* Names for the different compressors shipped with Blosc */
  52. #define BLOSC_BLOSCLZ_COMPNAME "blosclz"
  53. #define BLOSC_LZ4_COMPNAME "lz4"
  54. #define BLOSC_LZ4HC_COMPNAME "lz4hc"
  55. #define BLOSC_SNAPPY_COMPNAME "snappy"
  56. #define BLOSC_ZLIB_COMPNAME "zlib"
  57. #define BLOSC_ZSTD_COMPNAME "zstd"
  58. /* Codes for compression libraries shipped with Blosc (code must be < 8) */
  59. #define BLOSC_BLOSCLZ_LIB 0
  60. #define BLOSC_LZ4_LIB 1
  61. #define BLOSC_SNAPPY_LIB 2
  62. #define BLOSC_ZLIB_LIB 3
  63. #define BLOSC_ZSTD_LIB 4
  64. /* Names for the different compression libraries shipped with Blosc */
  65. #define BLOSC_BLOSCLZ_LIBNAME "BloscLZ"
  66. #define BLOSC_LZ4_LIBNAME "LZ4"
  67. #define BLOSC_SNAPPY_LIBNAME "Snappy"
  68. #define BLOSC_ZLIB_LIBNAME "Zlib"
  69. #define BLOSC_ZSTD_LIBNAME "Zstd"
  70. /* The codes for compressor formats shipped with Blosc */
  71. #define BLOSC_BLOSCLZ_FORMAT BLOSC_BLOSCLZ_LIB
  72. #define BLOSC_LZ4_FORMAT BLOSC_LZ4_LIB
  73. #define BLOSC_LZ4HC_FORMAT BLOSC_LZ4_LIB /* LZ4HC and LZ4 share the same format */
  74. #define BLOSC_SNAPPY_FORMAT BLOSC_SNAPPY_LIB
  75. #define BLOSC_ZLIB_FORMAT BLOSC_ZLIB_LIB
  76. #define BLOSC_ZSTD_FORMAT BLOSC_ZSTD_LIB
  77. /* The version formats for compressors shipped with Blosc */
  78. /* All versions here starts at 1 */
  79. #define BLOSC_BLOSCLZ_VERSION_FORMAT 1
  80. #define BLOSC_LZ4_VERSION_FORMAT 1
  81. #define BLOSC_LZ4HC_VERSION_FORMAT 1 /* LZ4HC and LZ4 share the same format */
  82. #define BLOSC_SNAPPY_VERSION_FORMAT 1
  83. #define BLOSC_ZLIB_VERSION_FORMAT 1
  84. #define BLOSC_ZSTD_VERSION_FORMAT 1
  85. /* Split mode for blocks. NEVER and ALWAYS are for experimenting with best compression ratio,
  86. * AUTO for optimal behaviour (based on experiments), and FORWARD_COMPAT provides
  87. * best forward compatibility */
  88. #define BLOSC_ALWAYS_SPLIT 1
  89. #define BLOSC_NEVER_SPLIT 2
  90. #define BLOSC_AUTO_SPLIT 3
  91. #define BLOSC_FORWARD_COMPAT_SPLIT 4
  92. /**
  93. Initialize the Blosc library environment.
  94. You must call this previous to any other Blosc call, unless you want
  95. Blosc to be used simultaneously in a multi-threaded environment, in
  96. which case you should *exclusively* use the
  97. blosc_compress_ctx()/blosc_decompress_ctx() pair (see below).
  98. */
  99. BLOSC_EXPORT void blosc_init(void);
  100. /**
  101. Destroy the Blosc library environment.
  102. You must call this after to you are done with all the Blosc calls,
  103. unless you have not used blosc_init() before (see blosc_init()
  104. above).
  105. */
  106. BLOSC_EXPORT void blosc_destroy(void);
  107. /**
  108. Compress a block of data in the `src` buffer and returns the size of
  109. the compressed block. The size of `src` buffer is specified by
  110. `nbytes`. There is not a minimum for `src` buffer size (`nbytes`).
  111. `clevel` is the desired compression level and must be a number
  112. between 0 (no compression) and 9 (maximum compression).
  113. `doshuffle` specifies whether the shuffle compression filters
  114. should be applied or not. BLOSC_NOSHUFFLE means not applying it,
  115. BLOSC_SHUFFLE means applying it at a byte level and BLOSC_BITSHUFFLE
  116. at a bit level (slower but may achieve better entropy alignment).
  117. `typesize` is the number of bytes for the atomic type in binary
  118. `src` buffer. This is mainly useful for the shuffle filters.
  119. For implementation reasons, only a 1 < `typesize` < 256 will allow the
  120. shuffle filter to work. When `typesize` is not in this range, shuffle
  121. will be silently disabled.
  122. The `dest` buffer must have at least the size of `destsize`. Blosc
  123. guarantees that if you set `destsize` to, at least,
  124. (`nbytes` + BLOSC_MAX_OVERHEAD), the compression will always succeed.
  125. The `src` buffer and the `dest` buffer can not overlap.
  126. Compression is memory safe and guaranteed not to write the `dest`
  127. buffer beyond what is specified in `destsize`.
  128. If `src` buffer cannot be compressed into `destsize`, the return
  129. value is zero and you should discard the contents of the `dest`
  130. buffer.
  131. A negative return value means that an internal error happened. This
  132. should never happen. If you see this, please report it back
  133. together with the buffer data causing this and compression settings.
  134. Environment variables
  135. ---------------------
  136. blosc_compress() honors different environment variables to control
  137. internal parameters without the need of doing that programatically.
  138. Here are the ones supported:
  139. BLOSC_CLEVEL=(INTEGER): This will overwrite the `clevel` parameter
  140. before the compression process starts.
  141. BLOSC_SHUFFLE=[NOSHUFFLE | SHUFFLE | BITSHUFFLE]: This will
  142. overwrite the `doshuffle` parameter before the compression process
  143. starts.
  144. BLOSC_TYPESIZE=(INTEGER): This will overwrite the `typesize`
  145. parameter before the compression process starts.
  146. BLOSC_COMPRESSOR=[BLOSCLZ | LZ4 | LZ4HC | SNAPPY | ZLIB]: This will
  147. call blosc_set_compressor(BLOSC_COMPRESSOR) before the compression
  148. process starts.
  149. BLOSC_NTHREADS=(INTEGER): This will call
  150. blosc_set_nthreads(BLOSC_NTHREADS) before the compression process
  151. starts.
  152. BLOSC_BLOCKSIZE=(INTEGER): This will call
  153. blosc_set_blocksize(BLOSC_BLOCKSIZE) before the compression process
  154. starts. *NOTE:* The blocksize is a critical parameter with
  155. important restrictions in the allowed values, so use this with care.
  156. BLOSC_NOLOCK=(ANY VALUE): This will call blosc_compress_ctx() under
  157. the hood, with the `compressor`, `blocksize` and
  158. `numinternalthreads` parameters set to the same as the last calls to
  159. blosc_set_compressor(), blosc_set_blocksize() and
  160. blosc_set_nthreads(). BLOSC_CLEVEL, BLOSC_SHUFFLE, BLOSC_TYPESIZE
  161. environment vars will also be honored.
  162. BLOSC_SPLITMODE=[ FORWARD_COMPAT | AUTO | ALWAYS | NEVER ]:
  163. This will call blosc_set_splitmode() with the different supported values.
  164. See blosc_set_splitmode() docstrings for more info on each mode.
  165. */
  166. BLOSC_EXPORT int blosc_compress(int clevel, int doshuffle, size_t typesize,
  167. size_t nbytes, const void *src, void *dest,
  168. size_t destsize);
  169. /**
  170. Context interface to blosc compression. This does not require a call
  171. to blosc_init() and can be called from multithreaded applications
  172. without the global lock being used, so allowing Blosc be executed
  173. simultaneously in those scenarios.
  174. It uses the same parameters than the blosc_compress() function plus:
  175. `compressor`: the string representing the type of compressor to use.
  176. `blocksize`: the requested size of the compressed blocks. If 0, an
  177. automatic blocksize will be used.
  178. `numinternalthreads`: the number of threads to use internally.
  179. A negative return value means that an internal error happened. This
  180. should never happen. If you see this, please report it back
  181. together with the buffer data causing this and compression settings.
  182. */
  183. BLOSC_EXPORT int blosc_compress_ctx(int clevel, int doshuffle, size_t typesize,
  184. size_t nbytes, const void* src, void* dest,
  185. size_t destsize, const char* compressor,
  186. size_t blocksize, int numinternalthreads);
  187. /**
  188. Decompress a block of compressed data in `src`, put the result in
  189. `dest` and returns the size of the decompressed block.
  190. The `src` buffer and the `dest` buffer can not overlap.
  191. Decompression is memory safe and guaranteed not to write the `dest`
  192. buffer beyond what is specified in `destsize`.
  193. If an error occurs, e.g. the compressed data is corrupted or the
  194. output buffer is not large enough, then 0 (zero) or a negative value
  195. will be returned instead.
  196. Environment variables
  197. ---------------------
  198. blosc_decompress() honors different environment variables to control
  199. internal parameters without the need of doing that programatically.
  200. Here are the ones supported:
  201. BLOSC_NTHREADS=(INTEGER): This will call
  202. blosc_set_nthreads(BLOSC_NTHREADS) before the proper decompression
  203. process starts.
  204. BLOSC_NOLOCK=(ANY VALUE): This will call blosc_decompress_ctx()
  205. under the hood, with the `numinternalthreads` parameter set to the
  206. same value as the last call to blosc_set_nthreads().
  207. */
  208. BLOSC_EXPORT int blosc_decompress(const void *src, void *dest, size_t destsize);
  209. /**
  210. Context interface to blosc decompression. This does not require a
  211. call to blosc_init() and can be called from multithreaded
  212. applications without the global lock being used, so allowing Blosc
  213. be executed simultaneously in those scenarios.
  214. It uses the same parameters than the blosc_decompress() function plus:
  215. `numinternalthreads`: number of threads to use internally.
  216. Decompression is memory safe and guaranteed not to write the `dest`
  217. buffer more than what is specified in `destsize`.
  218. If an error occurs, e.g. the compressed data is corrupted or the
  219. output buffer is not large enough, then 0 (zero) or a negative value
  220. will be returned instead.
  221. */
  222. BLOSC_EXPORT int blosc_decompress_ctx(const void *src, void *dest,
  223. size_t destsize, int numinternalthreads);
  224. /**
  225. Get `nitems` (of typesize size) in `src` buffer starting in `start`.
  226. The items are returned in `dest` buffer, which has to have enough
  227. space for storing all items.
  228. Returns the number of bytes copied to `dest` or a negative value if
  229. some error happens.
  230. */
  231. BLOSC_EXPORT int blosc_getitem(const void *src, int start, int nitems, void *dest);
  232. /**
  233. Returns the current number of threads that are used for
  234. compression/decompression.
  235. */
  236. BLOSC_EXPORT int blosc_get_nthreads(void);
  237. /**
  238. Initialize a pool of threads for compression/decompression. If
  239. `nthreads` is 1, then the serial version is chosen and a possible
  240. previous existing pool is ended. If this is not called, `nthreads`
  241. is set to 1 internally.
  242. Returns the previous number of threads.
  243. */
  244. BLOSC_EXPORT int blosc_set_nthreads(int nthreads);
  245. /**
  246. Returns the current compressor that is being used for compression.
  247. */
  248. BLOSC_EXPORT const char* blosc_get_compressor(void);
  249. /**
  250. Select the compressor to be used. The supported ones are "blosclz",
  251. "lz4", "lz4hc", "snappy", "zlib" and "ztsd". If this function is not
  252. called, then "blosclz" will be used by default.
  253. In case the compressor is not recognized, or there is not support
  254. for it in this build, it returns a -1. Else it returns the code for
  255. the compressor (>=0).
  256. */
  257. BLOSC_EXPORT int blosc_set_compressor(const char* compname);
  258. /**
  259. Get the `compname` associated with the `compcode`.
  260. If the compressor code is not recognized, or there is not support
  261. for it in this build, -1 is returned. Else, the compressor code is
  262. returned.
  263. */
  264. BLOSC_EXPORT int blosc_compcode_to_compname(int compcode, const char **compname);
  265. /**
  266. Return the compressor code associated with the compressor name.
  267. If the compressor name is not recognized, or there is not support
  268. for it in this build, -1 is returned instead.
  269. */
  270. BLOSC_EXPORT int blosc_compname_to_compcode(const char *compname);
  271. /**
  272. Get a list of compressors supported in the current build. The
  273. returned value is a string with a concatenation of "blosclz", "lz4",
  274. "lz4hc", "snappy", "zlib" or "zstd "separated by commas, depending
  275. on which ones are present in the build.
  276. This function does not leak, so you should not free() the returned
  277. list.
  278. This function should always succeed.
  279. */
  280. BLOSC_EXPORT const char* blosc_list_compressors(void);
  281. /**
  282. Return the version of the C-Blosc library in string format.
  283. Useful for dynamic libraries.
  284. */
  285. BLOSC_EXPORT const char* blosc_get_version_string(void);
  286. /**
  287. Get info from compression libraries included in the current build.
  288. In `compname` you pass the compressor name that you want info from.
  289. In `complib` and `version` you get a pointer to the compressor
  290. library name and the version in string format respectively. After
  291. using the name and version, you should free() them so as to avoid
  292. leaks.
  293. If the compressor is supported, it returns the code for the library
  294. (>=0). If it is not supported, this function returns -1.
  295. */
  296. BLOSC_EXPORT int blosc_get_complib_info(const char *compname, char **complib, char **version);
  297. /**
  298. Free possible memory temporaries and thread resources. Use this
  299. when you are not going to use Blosc for a long while. In case of
  300. problems releasing the resources, it returns a negative number, else
  301. it returns 0.
  302. */
  303. BLOSC_EXPORT int blosc_free_resources(void);
  304. /**
  305. Return information about a compressed buffer, namely the number of
  306. uncompressed bytes (`nbytes`) and compressed (`cbytes`). It also
  307. returns the `blocksize` (which is used internally for doing the
  308. compression by blocks).
  309. You only need to pass the first BLOSC_MIN_HEADER_LENGTH bytes of a
  310. compressed buffer for this call to work.
  311. If the format is not supported by the library, all output arguments will be
  312. filled with zeros.
  313. */
  314. BLOSC_EXPORT void blosc_cbuffer_sizes(const void *cbuffer, size_t *nbytes,
  315. size_t *cbytes, size_t *blocksize);
  316. /**
  317. Return meta-information about a compressed buffer, namely the type size
  318. (`typesize`), as well as some internal `flags`.
  319. The `flags` is a set of bits, where the used ones are:
  320. * bit 0: whether the shuffle filter has been applied or not
  321. * bit 1: whether the internal buffer is a pure memcpy or not
  322. * bit 2: whether the bit shuffle filter has been applied or not
  323. You can use the `BLOSC_DOSHUFFLE`, `BLOSC_DOBITSHUFFLE` and
  324. `BLOSC_MEMCPYED` symbols for extracting the interesting bits
  325. (e.g. ``flags & BLOSC_DOSHUFFLE`` says whether the buffer is
  326. byte-shuffled or not).
  327. You only need to pass the first BLOSC_MIN_HEADER_LENGTH bytes of a
  328. compressed buffer for this call to work.
  329. If the format is not supported by the library, all output arguments will be
  330. filled with zeros.
  331. */
  332. BLOSC_EXPORT void blosc_cbuffer_metainfo(const void *cbuffer, size_t *typesize,
  333. int *flags);
  334. /**
  335. Return information about a compressed buffer, namely the internal
  336. Blosc format version (`version`) and the format for the internal
  337. compressor used (`compversion`).
  338. This function should always succeed.
  339. */
  340. BLOSC_EXPORT void blosc_cbuffer_versions(const void *cbuffer, int *version,
  341. int *compversion);
  342. /**
  343. Return the compressor library/format used in a compressed buffer.
  344. This function should always succeed.
  345. */
  346. BLOSC_EXPORT const char *blosc_cbuffer_complib(const void *cbuffer);
  347. /*********************************************************************
  348. Low-level functions follows. Use them only if you are an expert!
  349. *********************************************************************/
  350. /**
  351. Get the internal blocksize to be used during compression. 0 means
  352. that an automatic blocksize is computed internally (the default).
  353. */
  354. BLOSC_EXPORT int blosc_get_blocksize(void);
  355. /**
  356. Force the use of a specific blocksize. If 0, an automatic
  357. blocksize will be used (the default).
  358. The blocksize is a critical parameter with important restrictions in
  359. the allowed values, so use this with care.
  360. */
  361. BLOSC_EXPORT void blosc_set_blocksize(size_t blocksize);
  362. /**
  363. Set the split mode.
  364. This function can take the next values:
  365. * BLOSC_FORWARD_COMPAT_SPLIT
  366. * BLOSC_AUTO_SPLIT
  367. * BLOSC_NEVER_SPLIT
  368. * BLOSC_ALWAYS_SPLIT
  369. BLOSC_FORWARD_COMPAT offers reasonably forward compatibility,
  370. BLOSC_AUTO_SPLIT is for nearly optimal results (based on heuristics),
  371. BLOSC_NEVER_SPLIT and BLOSC_ALWAYS_SPLIT are for the user experimenting
  372. when trying to get best compression ratios and/or speed.
  373. If not called, the default mode is BLOSC_FORWARD_COMPAT_SPLIT.
  374. This function should always succeed.
  375. */
  376. BLOSC_EXPORT void blosc_set_splitmode(int splitmode);
  377. #ifdef __cplusplus
  378. }
  379. #endif
  380. #endif