lz4.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. /* LZ4 Kernel Interface
  2. *
  3. * Copyright (C) 2013, LG Electronics, Kyungsik Lee <[email protected]>
  4. * Copyright (C) 2016, Sven Schmidt <[email protected]>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * This file is based on the original header file
  11. * for LZ4 - Fast LZ compression algorithm.
  12. *
  13. * LZ4 - Fast LZ compression algorithm
  14. * Copyright (C) 2011-2016, Yann Collet.
  15. * BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
  16. * Redistribution and use in source and binary forms, with or without
  17. * modification, are permitted provided that the following conditions are
  18. * met:
  19. * * Redistributions of source code must retain the above copyright
  20. * notice, this list of conditions and the following disclaimer.
  21. * * Redistributions in binary form must reproduce the above
  22. * copyright notice, this list of conditions and the following disclaimer
  23. * in the documentation and/or other materials provided with the
  24. * distribution.
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  26. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  27. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  28. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  29. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  30. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  31. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  32. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  33. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  34. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  35. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. * You can contact the author at :
  37. * - LZ4 homepage : http://www.lz4.org
  38. * - LZ4 source repository : https://github.com/lz4/lz4
  39. */
  40. #ifndef __LZ4_H__
  41. #define __LZ4_H__
  42. #include <linux/types.h>
  43. #include <linux/string.h> /* memset, memcpy */
  44. /*-************************************************************************
  45. * CONSTANTS
  46. **************************************************************************/
  47. /*
  48. * LZ4_MEMORY_USAGE :
  49. * Memory usage formula : N->2^N Bytes
  50. * (examples : 10 -> 1KB; 12 -> 4KB ; 16 -> 64KB; 20 -> 1MB; etc.)
  51. * Increasing memory usage improves compression ratio
  52. * Reduced memory usage can improve speed, due to cache effect
  53. * Default value is 14, for 16KB, which nicely fits into Intel x86 L1 cache
  54. */
  55. #define LZ4_MEMORY_USAGE 14
  56. #define LZ4_MAX_INPUT_SIZE 0x7E000000 /* 2 113 929 216 bytes */
  57. #define LZ4_COMPRESSBOUND(isize) (\
  58. (unsigned int)(isize) > (unsigned int)LZ4_MAX_INPUT_SIZE \
  59. ? 0 \
  60. : (isize) + ((isize)/255) + 16)
  61. #define LZ4_ACCELERATION_DEFAULT 1
  62. #define LZ4_HASHLOG (LZ4_MEMORY_USAGE-2)
  63. #define LZ4_HASHTABLESIZE (1 << LZ4_MEMORY_USAGE)
  64. #define LZ4_HASH_SIZE_U32 (1 << LZ4_HASHLOG)
  65. #define LZ4HC_MIN_CLEVEL 3
  66. #define LZ4HC_DEFAULT_CLEVEL 9
  67. #define LZ4HC_MAX_CLEVEL 16
  68. #define LZ4HC_DICTIONARY_LOGSIZE 16
  69. #define LZ4HC_MAXD (1<<LZ4HC_DICTIONARY_LOGSIZE)
  70. #define LZ4HC_MAXD_MASK (LZ4HC_MAXD - 1)
  71. #define LZ4HC_HASH_LOG (LZ4HC_DICTIONARY_LOGSIZE - 1)
  72. #define LZ4HC_HASHTABLESIZE (1 << LZ4HC_HASH_LOG)
  73. #define LZ4HC_HASH_MASK (LZ4HC_HASHTABLESIZE - 1)
  74. /*-************************************************************************
  75. * STREAMING CONSTANTS AND STRUCTURES
  76. **************************************************************************/
  77. #define LZ4_STREAMSIZE_U64 ((1 << (LZ4_MEMORY_USAGE - 3)) + 4)
  78. #define LZ4_STREAMSIZE (LZ4_STREAMSIZE_U64 * sizeof(unsigned long long))
  79. #define LZ4_STREAMHCSIZE 262192
  80. #define LZ4_STREAMHCSIZE_SIZET (262192 / sizeof(size_t))
  81. #define LZ4_STREAMDECODESIZE_U64 4
  82. #define LZ4_STREAMDECODESIZE (LZ4_STREAMDECODESIZE_U64 * \
  83. sizeof(unsigned long long))
  84. /*
  85. * LZ4_stream_t - information structure to track an LZ4 stream.
  86. */
  87. typedef struct {
  88. uint32_t hashTable[LZ4_HASH_SIZE_U32];
  89. uint32_t currentOffset;
  90. uint32_t initCheck;
  91. const uint8_t *dictionary;
  92. uint8_t *bufferStart;
  93. uint32_t dictSize;
  94. } LZ4_stream_t_internal;
  95. typedef union {
  96. unsigned long long table[LZ4_STREAMSIZE_U64];
  97. LZ4_stream_t_internal internal_donotuse;
  98. } LZ4_stream_t;
  99. /*
  100. * LZ4_streamHC_t - information structure to track an LZ4HC stream.
  101. */
  102. typedef struct {
  103. unsigned int hashTable[LZ4HC_HASHTABLESIZE];
  104. unsigned short chainTable[LZ4HC_MAXD];
  105. /* next block to continue on current prefix */
  106. const unsigned char *end;
  107. /* All index relative to this position */
  108. const unsigned char *base;
  109. /* alternate base for extDict */
  110. const unsigned char *dictBase;
  111. /* below that point, need extDict */
  112. unsigned int dictLimit;
  113. /* below that point, no more dict */
  114. unsigned int lowLimit;
  115. /* index from which to continue dict update */
  116. unsigned int nextToUpdate;
  117. unsigned int compressionLevel;
  118. } LZ4HC_CCtx_internal;
  119. typedef union {
  120. size_t table[LZ4_STREAMHCSIZE_SIZET];
  121. LZ4HC_CCtx_internal internal_donotuse;
  122. } LZ4_streamHC_t;
  123. /*
  124. * LZ4_streamDecode_t - information structure to track an
  125. * LZ4 stream during decompression.
  126. *
  127. * init this structure using LZ4_setStreamDecode (or memset()) before first use
  128. */
  129. typedef struct {
  130. const uint8_t *externalDict;
  131. size_t extDictSize;
  132. const uint8_t *prefixEnd;
  133. size_t prefixSize;
  134. } LZ4_streamDecode_t_internal;
  135. typedef union {
  136. unsigned long long table[LZ4_STREAMDECODESIZE_U64];
  137. LZ4_streamDecode_t_internal internal_donotuse;
  138. } LZ4_streamDecode_t;
  139. /*-************************************************************************
  140. * SIZE OF STATE
  141. **************************************************************************/
  142. #define LZ4_MEM_COMPRESS LZ4_STREAMSIZE
  143. #define LZ4HC_MEM_COMPRESS LZ4_STREAMHCSIZE
  144. /*-************************************************************************
  145. * Compression Functions
  146. **************************************************************************/
  147. /**
  148. * LZ4_compressBound() - Max. output size in worst case szenarios
  149. * @isize: Size of the input data
  150. *
  151. * Return: Max. size LZ4 may output in a "worst case" szenario
  152. * (data not compressible)
  153. */
  154. static inline int LZ4_compressBound(size_t isize)
  155. {
  156. return LZ4_COMPRESSBOUND(isize);
  157. }
  158. /**
  159. * LZ4_compress_default() - Compress data from source to dest
  160. * @source: source address of the original data
  161. * @dest: output buffer address of the compressed data
  162. * @inputSize: size of the input data. Max supported value is LZ4_MAX_INPUT_SIZE
  163. * @maxOutputSize: full or partial size of buffer 'dest'
  164. * which must be already allocated
  165. * @wrkmem: address of the working memory.
  166. * This requires 'workmem' of LZ4_MEM_COMPRESS.
  167. *
  168. * Compresses 'sourceSize' bytes from buffer 'source'
  169. * into already allocated 'dest' buffer of size 'maxOutputSize'.
  170. * Compression is guaranteed to succeed if
  171. * 'maxOutputSize' >= LZ4_compressBound(inputSize).
  172. * It also runs faster, so it's a recommended setting.
  173. * If the function cannot compress 'source' into a more limited 'dest' budget,
  174. * compression stops *immediately*, and the function result is zero.
  175. * As a consequence, 'dest' content is not valid.
  176. *
  177. * Return: Number of bytes written into buffer 'dest'
  178. * (necessarily <= maxOutputSize) or 0 if compression fails
  179. */
  180. int LZ4_compress_default(const char *source, char *dest, int inputSize,
  181. int maxOutputSize, void *wrkmem);
  182. /**
  183. * LZ4_compress_fast() - As LZ4_compress_default providing an acceleration param
  184. * @source: source address of the original data
  185. * @dest: output buffer address of the compressed data
  186. * @inputSize: size of the input data. Max supported value is LZ4_MAX_INPUT_SIZE
  187. * @maxOutputSize: full or partial size of buffer 'dest'
  188. * which must be already allocated
  189. * @acceleration: acceleration factor
  190. * @wrkmem: address of the working memory.
  191. * This requires 'workmem' of LZ4_MEM_COMPRESS.
  192. *
  193. * Same as LZ4_compress_default(), but allows to select an "acceleration"
  194. * factor. The larger the acceleration value, the faster the algorithm,
  195. * but also the lesser the compression. It's a trade-off. It can be fine tuned,
  196. * with each successive value providing roughly +~3% to speed.
  197. * An acceleration value of "1" is the same as regular LZ4_compress_default()
  198. * Values <= 0 will be replaced by LZ4_ACCELERATION_DEFAULT, which is 1.
  199. *
  200. * Return: Number of bytes written into buffer 'dest'
  201. * (necessarily <= maxOutputSize) or 0 if compression fails
  202. */
  203. int LZ4_compress_fast(const char *source, char *dest, int inputSize,
  204. int maxOutputSize, int acceleration, void *wrkmem);
  205. /**
  206. * LZ4_compress_destSize() - Compress as much data as possible
  207. * from source to dest
  208. * @source: source address of the original data
  209. * @dest: output buffer address of the compressed data
  210. * @sourceSizePtr: will be modified to indicate how many bytes where read
  211. * from 'source' to fill 'dest'. New value is necessarily <= old value.
  212. * @targetDestSize: Size of buffer 'dest' which must be already allocated
  213. * @wrkmem: address of the working memory.
  214. * This requires 'workmem' of LZ4_MEM_COMPRESS.
  215. *
  216. * Reverse the logic, by compressing as much data as possible
  217. * from 'source' buffer into already allocated buffer 'dest'
  218. * of size 'targetDestSize'.
  219. * This function either compresses the entire 'source' content into 'dest'
  220. * if it's large enough, or fill 'dest' buffer completely with as much data as
  221. * possible from 'source'.
  222. *
  223. * Return: Number of bytes written into 'dest' (necessarily <= targetDestSize)
  224. * or 0 if compression fails
  225. */
  226. int LZ4_compress_destSize(const char *source, char *dest, int *sourceSizePtr,
  227. int targetDestSize, void *wrkmem);
  228. /*-************************************************************************
  229. * Decompression Functions
  230. **************************************************************************/
  231. /**
  232. * LZ4_decompress_fast() - Decompresses data from 'source' into 'dest'
  233. * @source: source address of the compressed data
  234. * @dest: output buffer address of the uncompressed data
  235. * which must be already allocated with 'originalSize' bytes
  236. * @originalSize: is the original and therefore uncompressed size
  237. *
  238. * Decompresses data from 'source' into 'dest'.
  239. * This function fully respect memory boundaries for properly formed
  240. * compressed data.
  241. * It is a bit faster than LZ4_decompress_safe().
  242. * However, it does not provide any protection against intentionally
  243. * modified data stream (malicious input).
  244. * Use this function in trusted environment only
  245. * (data to decode comes from a trusted source).
  246. *
  247. * Return: number of bytes read from the source buffer
  248. * or a negative result if decompression fails.
  249. */
  250. int LZ4_decompress_fast(const char *source, char *dest, int originalSize);
  251. /**
  252. * LZ4_decompress_safe() - Decompression protected against buffer overflow
  253. * @source: source address of the compressed data
  254. * @dest: output buffer address of the uncompressed data
  255. * which must be already allocated
  256. * @compressedSize: is the precise full size of the compressed block
  257. * @maxDecompressedSize: is the size of 'dest' buffer
  258. *
  259. * Decompresses data from 'source' into 'dest'.
  260. * If the source stream is detected malformed, the function will
  261. * stop decoding and return a negative result.
  262. * This function is protected against buffer overflow exploits,
  263. * including malicious data packets. It never writes outside output buffer,
  264. * nor reads outside input buffer.
  265. *
  266. * Return: number of bytes decompressed into destination buffer
  267. * (necessarily <= maxDecompressedSize)
  268. * or a negative result in case of error
  269. */
  270. int LZ4_decompress_safe(const char *source, char *dest, int compressedSize,
  271. int maxDecompressedSize);
  272. /**
  273. * LZ4_decompress_safe_partial() - Decompress a block of size 'compressedSize'
  274. * at position 'source' into buffer 'dest'
  275. * @source: source address of the compressed data
  276. * @dest: output buffer address of the decompressed data which must be
  277. * already allocated
  278. * @compressedSize: is the precise full size of the compressed block.
  279. * @targetOutputSize: the decompression operation will try
  280. * to stop as soon as 'targetOutputSize' has been reached
  281. * @maxDecompressedSize: is the size of destination buffer
  282. *
  283. * This function decompresses a compressed block of size 'compressedSize'
  284. * at position 'source' into destination buffer 'dest'
  285. * of size 'maxDecompressedSize'.
  286. * The function tries to stop decompressing operation as soon as
  287. * 'targetOutputSize' has been reached, reducing decompression time.
  288. * This function never writes outside of output buffer,
  289. * and never reads outside of input buffer.
  290. * It is therefore protected against malicious data packets.
  291. *
  292. * Return: the number of bytes decoded in the destination buffer
  293. * (necessarily <= maxDecompressedSize)
  294. * or a negative result in case of error
  295. *
  296. */
  297. int LZ4_decompress_safe_partial(const char *source, char *dest,
  298. int compressedSize, int targetOutputSize, int maxDecompressedSize);
  299. /*-************************************************************************
  300. * LZ4 HC Compression
  301. **************************************************************************/
  302. /**
  303. * LZ4_compress_HC() - Compress data from `src` into `dst`, using HC algorithm
  304. * @src: source address of the original data
  305. * @dst: output buffer address of the compressed data
  306. * @srcSize: size of the input data. Max supported value is LZ4_MAX_INPUT_SIZE
  307. * @dstCapacity: full or partial size of buffer 'dst',
  308. * which must be already allocated
  309. * @compressionLevel: Recommended values are between 4 and 9, although any
  310. * value between 1 and LZ4HC_MAX_CLEVEL will work.
  311. * Values >LZ4HC_MAX_CLEVEL behave the same as 16.
  312. * @wrkmem: address of the working memory.
  313. * This requires 'wrkmem' of size LZ4HC_MEM_COMPRESS.
  314. *
  315. * Compress data from 'src' into 'dst', using the more powerful
  316. * but slower "HC" algorithm. Compression is guaranteed to succeed if
  317. * `dstCapacity >= LZ4_compressBound(srcSize)
  318. *
  319. * Return : the number of bytes written into 'dst' or 0 if compression fails.
  320. */
  321. int LZ4_compress_HC(const char *src, char *dst, int srcSize, int dstCapacity,
  322. int compressionLevel, void *wrkmem);
  323. /**
  324. * LZ4_resetStreamHC() - Init an allocated 'LZ4_streamHC_t' structure
  325. * @streamHCPtr: pointer to the 'LZ4_streamHC_t' structure
  326. * @compressionLevel: Recommended values are between 4 and 9, although any
  327. * value between 1 and LZ4HC_MAX_CLEVEL will work.
  328. * Values >LZ4HC_MAX_CLEVEL behave the same as 16.
  329. *
  330. * An LZ4_streamHC_t structure can be allocated once
  331. * and re-used multiple times.
  332. * Use this function to init an allocated `LZ4_streamHC_t` structure
  333. * and start a new compression.
  334. */
  335. void LZ4_resetStreamHC(LZ4_streamHC_t *streamHCPtr, int compressionLevel);
  336. /**
  337. * LZ4_loadDictHC() - Load a static dictionary into LZ4_streamHC
  338. * @streamHCPtr: pointer to the LZ4HC_stream_t
  339. * @dictionary: dictionary to load
  340. * @dictSize: size of dictionary
  341. *
  342. * Use this function to load a static dictionary into LZ4HC_stream.
  343. * Any previous data will be forgotten, only 'dictionary'
  344. * will remain in memory.
  345. * Loading a size of 0 is allowed.
  346. *
  347. * Return : dictionary size, in bytes (necessarily <= 64 KB)
  348. */
  349. int LZ4_loadDictHC(LZ4_streamHC_t *streamHCPtr, const char *dictionary,
  350. int dictSize);
  351. /**
  352. * LZ4_compress_HC_continue() - Compress 'src' using data from previously
  353. * compressed blocks as a dictionary using the HC algorithm
  354. * @streamHCPtr: Pointer to the previous 'LZ4_streamHC_t' structure
  355. * @src: source address of the original data
  356. * @dst: output buffer address of the compressed data,
  357. * which must be already allocated
  358. * @srcSize: size of the input data. Max supported value is LZ4_MAX_INPUT_SIZE
  359. * @maxDstSize: full or partial size of buffer 'dest'
  360. * which must be already allocated
  361. *
  362. * These functions compress data in successive blocks of any size, using
  363. * previous blocks as dictionary. One key assumption is that previous
  364. * blocks (up to 64 KB) remain read-accessible while
  365. * compressing next blocks. There is an exception for ring buffers,
  366. * which can be smaller than 64 KB.
  367. * Ring buffers scenario is automatically detected and handled by
  368. * LZ4_compress_HC_continue().
  369. * Before starting compression, state must be properly initialized,
  370. * using LZ4_resetStreamHC().
  371. * A first "fictional block" can then be designated as
  372. * initial dictionary, using LZ4_loadDictHC() (Optional).
  373. * Then, use LZ4_compress_HC_continue()
  374. * to compress each successive block. Previous memory blocks
  375. * (including initial dictionary when present) must remain accessible
  376. * and unmodified during compression.
  377. * 'dst' buffer should be sized to handle worst case scenarios, using
  378. * LZ4_compressBound(), to ensure operation success.
  379. * If, for any reason, previous data blocks can't be preserved unmodified
  380. * in memory during next compression block,
  381. * you must save it to a safer memory space, using LZ4_saveDictHC().
  382. * Return value of LZ4_saveDictHC() is the size of dictionary
  383. * effectively saved into 'safeBuffer'.
  384. *
  385. * Return: Number of bytes written into buffer 'dst' or 0 if compression fails
  386. */
  387. int LZ4_compress_HC_continue(LZ4_streamHC_t *streamHCPtr, const char *src,
  388. char *dst, int srcSize, int maxDstSize);
  389. /**
  390. * LZ4_saveDictHC() - Save static dictionary from LZ4HC_stream
  391. * @streamHCPtr: pointer to the 'LZ4HC_stream_t' structure
  392. * @safeBuffer: buffer to save dictionary to, must be already allocated
  393. * @maxDictSize: size of 'safeBuffer'
  394. *
  395. * If previously compressed data block is not guaranteed
  396. * to remain available at its memory location,
  397. * save it into a safer place (char *safeBuffer).
  398. * Note : you don't need to call LZ4_loadDictHC() afterwards,
  399. * dictionary is immediately usable, you can therefore call
  400. * LZ4_compress_HC_continue().
  401. *
  402. * Return : saved dictionary size in bytes (necessarily <= maxDictSize),
  403. * or 0 if error.
  404. */
  405. int LZ4_saveDictHC(LZ4_streamHC_t *streamHCPtr, char *safeBuffer,
  406. int maxDictSize);
  407. /*-*********************************************
  408. * Streaming Compression Functions
  409. ***********************************************/
  410. /**
  411. * LZ4_resetStream() - Init an allocated 'LZ4_stream_t' structure
  412. * @LZ4_stream: pointer to the 'LZ4_stream_t' structure
  413. *
  414. * An LZ4_stream_t structure can be allocated once
  415. * and re-used multiple times.
  416. * Use this function to init an allocated `LZ4_stream_t` structure
  417. * and start a new compression.
  418. */
  419. void LZ4_resetStream(LZ4_stream_t *LZ4_stream);
  420. /**
  421. * LZ4_loadDict() - Load a static dictionary into LZ4_stream
  422. * @streamPtr: pointer to the LZ4_stream_t
  423. * @dictionary: dictionary to load
  424. * @dictSize: size of dictionary
  425. *
  426. * Use this function to load a static dictionary into LZ4_stream.
  427. * Any previous data will be forgotten, only 'dictionary'
  428. * will remain in memory.
  429. * Loading a size of 0 is allowed.
  430. *
  431. * Return : dictionary size, in bytes (necessarily <= 64 KB)
  432. */
  433. int LZ4_loadDict(LZ4_stream_t *streamPtr, const char *dictionary,
  434. int dictSize);
  435. /**
  436. * LZ4_saveDict() - Save static dictionary from LZ4_stream
  437. * @streamPtr: pointer to the 'LZ4_stream_t' structure
  438. * @safeBuffer: buffer to save dictionary to, must be already allocated
  439. * @dictSize: size of 'safeBuffer'
  440. *
  441. * If previously compressed data block is not guaranteed
  442. * to remain available at its memory location,
  443. * save it into a safer place (char *safeBuffer).
  444. * Note : you don't need to call LZ4_loadDict() afterwards,
  445. * dictionary is immediately usable, you can therefore call
  446. * LZ4_compress_fast_continue().
  447. *
  448. * Return : saved dictionary size in bytes (necessarily <= dictSize),
  449. * or 0 if error.
  450. */
  451. int LZ4_saveDict(LZ4_stream_t *streamPtr, char *safeBuffer, int dictSize);
  452. /**
  453. * LZ4_compress_fast_continue() - Compress 'src' using data from previously
  454. * compressed blocks as a dictionary
  455. * @streamPtr: Pointer to the previous 'LZ4_stream_t' structure
  456. * @src: source address of the original data
  457. * @dst: output buffer address of the compressed data,
  458. * which must be already allocated
  459. * @srcSize: size of the input data. Max supported value is LZ4_MAX_INPUT_SIZE
  460. * @maxDstSize: full or partial size of buffer 'dest'
  461. * which must be already allocated
  462. * @acceleration: acceleration factor
  463. *
  464. * Compress buffer content 'src', using data from previously compressed blocks
  465. * as dictionary to improve compression ratio.
  466. * Important : Previous data blocks are assumed to still
  467. * be present and unmodified !
  468. * If maxDstSize >= LZ4_compressBound(srcSize),
  469. * compression is guaranteed to succeed, and runs faster.
  470. *
  471. * Return: Number of bytes written into buffer 'dst' or 0 if compression fails
  472. */
  473. int LZ4_compress_fast_continue(LZ4_stream_t *streamPtr, const char *src,
  474. char *dst, int srcSize, int maxDstSize, int acceleration);
  475. /**
  476. * LZ4_setStreamDecode() - Instruct where to find dictionary
  477. * @LZ4_streamDecode: the 'LZ4_streamDecode_t' structure
  478. * @dictionary: dictionary to use
  479. * @dictSize: size of dictionary
  480. *
  481. * Use this function to instruct where to find the dictionary.
  482. * Setting a size of 0 is allowed (same effect as reset).
  483. *
  484. * Return: 1 if OK, 0 if error
  485. */
  486. int LZ4_setStreamDecode(LZ4_streamDecode_t *LZ4_streamDecode,
  487. const char *dictionary, int dictSize);
  488. /**
  489. * LZ4_decompress_safe_continue() - Decompress blocks in streaming mode
  490. * @LZ4_streamDecode: the 'LZ4_streamDecode_t' structure
  491. * @source: source address of the compressed data
  492. * @dest: output buffer address of the uncompressed data
  493. * which must be already allocated
  494. * @compressedSize: is the precise full size of the compressed block
  495. * @maxDecompressedSize: is the size of 'dest' buffer
  496. *
  497. * This decoding function allows decompression of multiple blocks
  498. * in "streaming" mode.
  499. * Previously decoded blocks *must* remain available at the memory position
  500. * where they were decoded (up to 64 KB)
  501. * In the case of a ring buffers, decoding buffer must be either :
  502. * - Exactly same size as encoding buffer, with same update rule
  503. * (block boundaries at same positions) In which case,
  504. * the decoding & encoding ring buffer can have any size,
  505. * including very small ones ( < 64 KB).
  506. * - Larger than encoding buffer, by a minimum of maxBlockSize more bytes.
  507. * maxBlockSize is implementation dependent.
  508. * It's the maximum size you intend to compress into a single block.
  509. * In which case, encoding and decoding buffers do not need
  510. * to be synchronized, and encoding ring buffer can have any size,
  511. * including small ones ( < 64 KB).
  512. * - _At least_ 64 KB + 8 bytes + maxBlockSize.
  513. * In which case, encoding and decoding buffers do not need to be
  514. * synchronized, and encoding ring buffer can have any size,
  515. * including larger than decoding buffer. W
  516. * Whenever these conditions are not possible, save the last 64KB of decoded
  517. * data into a safe buffer, and indicate where it is saved
  518. * using LZ4_setStreamDecode()
  519. *
  520. * Return: number of bytes decompressed into destination buffer
  521. * (necessarily <= maxDecompressedSize)
  522. * or a negative result in case of error
  523. */
  524. int LZ4_decompress_safe_continue(LZ4_streamDecode_t *LZ4_streamDecode,
  525. const char *source, char *dest, int compressedSize,
  526. int maxDecompressedSize);
  527. /**
  528. * LZ4_decompress_fast_continue() - Decompress blocks in streaming mode
  529. * @LZ4_streamDecode: the 'LZ4_streamDecode_t' structure
  530. * @source: source address of the compressed data
  531. * @dest: output buffer address of the uncompressed data
  532. * which must be already allocated with 'originalSize' bytes
  533. * @originalSize: is the original and therefore uncompressed size
  534. *
  535. * This decoding function allows decompression of multiple blocks
  536. * in "streaming" mode.
  537. * Previously decoded blocks *must* remain available at the memory position
  538. * where they were decoded (up to 64 KB)
  539. * In the case of a ring buffers, decoding buffer must be either :
  540. * - Exactly same size as encoding buffer, with same update rule
  541. * (block boundaries at same positions) In which case,
  542. * the decoding & encoding ring buffer can have any size,
  543. * including very small ones ( < 64 KB).
  544. * - Larger than encoding buffer, by a minimum of maxBlockSize more bytes.
  545. * maxBlockSize is implementation dependent.
  546. * It's the maximum size you intend to compress into a single block.
  547. * In which case, encoding and decoding buffers do not need
  548. * to be synchronized, and encoding ring buffer can have any size,
  549. * including small ones ( < 64 KB).
  550. * - _At least_ 64 KB + 8 bytes + maxBlockSize.
  551. * In which case, encoding and decoding buffers do not need to be
  552. * synchronized, and encoding ring buffer can have any size,
  553. * including larger than decoding buffer. W
  554. * Whenever these conditions are not possible, save the last 64KB of decoded
  555. * data into a safe buffer, and indicate where it is saved
  556. * using LZ4_setStreamDecode()
  557. *
  558. * Return: number of bytes decompressed into destination buffer
  559. * (necessarily <= maxDecompressedSize)
  560. * or a negative result in case of error
  561. */
  562. int LZ4_decompress_fast_continue(LZ4_streamDecode_t *LZ4_streamDecode,
  563. const char *source, char *dest, int originalSize);
  564. /**
  565. * LZ4_decompress_safe_usingDict() - Same as LZ4_setStreamDecode()
  566. * followed by LZ4_decompress_safe_continue()
  567. * @source: source address of the compressed data
  568. * @dest: output buffer address of the uncompressed data
  569. * which must be already allocated
  570. * @compressedSize: is the precise full size of the compressed block
  571. * @maxDecompressedSize: is the size of 'dest' buffer
  572. * @dictStart: pointer to the start of the dictionary in memory
  573. * @dictSize: size of dictionary
  574. *
  575. * This decoding function works the same as
  576. * a combination of LZ4_setStreamDecode() followed by
  577. * LZ4_decompress_safe_continue()
  578. * It is stand-alone, and doesn't need an LZ4_streamDecode_t structure.
  579. *
  580. * Return: number of bytes decompressed into destination buffer
  581. * (necessarily <= maxDecompressedSize)
  582. * or a negative result in case of error
  583. */
  584. int LZ4_decompress_safe_usingDict(const char *source, char *dest,
  585. int compressedSize, int maxDecompressedSize, const char *dictStart,
  586. int dictSize);
  587. /**
  588. * LZ4_decompress_fast_usingDict() - Same as LZ4_setStreamDecode()
  589. * followed by LZ4_decompress_fast_continue()
  590. * @source: source address of the compressed data
  591. * @dest: output buffer address of the uncompressed data
  592. * which must be already allocated with 'originalSize' bytes
  593. * @originalSize: is the original and therefore uncompressed size
  594. * @dictStart: pointer to the start of the dictionary in memory
  595. * @dictSize: size of dictionary
  596. *
  597. * This decoding function works the same as
  598. * a combination of LZ4_setStreamDecode() followed by
  599. * LZ4_decompress_fast_continue()
  600. * It is stand-alone, and doesn't need an LZ4_streamDecode_t structure.
  601. *
  602. * Return: number of bytes decompressed into destination buffer
  603. * (necessarily <= maxDecompressedSize)
  604. * or a negative result in case of error
  605. */
  606. int LZ4_decompress_fast_usingDict(const char *source, char *dest,
  607. int originalSize, const char *dictStart, int dictSize);
  608. #endif