extlz4 0.3.4 → 0.3.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (75) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/Rakefile +21 -3
  4. data/contrib/lz4/CODING_STYLE +57 -0
  5. data/contrib/lz4/LICENSE +1 -1
  6. data/contrib/lz4/Makefile.inc +17 -15
  7. data/contrib/lz4/NEWS +25 -0
  8. data/contrib/lz4/README.md +16 -5
  9. data/contrib/lz4/SECURITY.md +17 -0
  10. data/contrib/lz4/build/README.md +4 -15
  11. data/contrib/lz4/build/VS2022/_build.bat +39 -0
  12. data/contrib/lz4/build/VS2022/_setup.bat +35 -0
  13. data/contrib/lz4/build/VS2022/_test.bat +38 -0
  14. data/contrib/lz4/build/VS2022/build-and-test-win32-debug.bat +26 -0
  15. data/contrib/lz4/build/VS2022/build-and-test-win32-release.bat +26 -0
  16. data/contrib/lz4/build/VS2022/build-and-test-x64-debug.bat +26 -0
  17. data/contrib/lz4/build/VS2022/build-and-test-x64-release.bat +26 -0
  18. data/contrib/lz4/build/VS2022/datagen/datagen.vcxproj +7 -3
  19. data/contrib/lz4/build/{VS2017 → VS2022}/lz4/lz4.vcxproj +21 -7
  20. data/contrib/lz4/build/VS2022/lz4.sln +5 -2
  21. data/contrib/lz4/build/cmake/CMakeLists.txt +95 -100
  22. data/contrib/lz4/build/meson/GetLz4LibraryVersion.py +39 -0
  23. data/contrib/lz4/build/meson/README.md +34 -0
  24. data/contrib/lz4/build/meson/meson/contrib/gen_manual/meson.build +42 -0
  25. data/contrib/lz4/build/meson/meson/contrib/meson.build +11 -0
  26. data/contrib/lz4/build/meson/meson/examples/meson.build +32 -0
  27. data/contrib/lz4/build/meson/meson/lib/meson.build +87 -0
  28. data/contrib/lz4/build/meson/meson/meson.build +135 -0
  29. data/contrib/lz4/build/meson/meson/ossfuzz/meson.build +35 -0
  30. data/contrib/lz4/build/meson/meson/programs/meson.build +91 -0
  31. data/contrib/lz4/build/meson/meson/tests/meson.build +162 -0
  32. data/contrib/lz4/build/meson/meson.build +31 -0
  33. data/contrib/lz4/build/meson/meson_options.txt +44 -0
  34. data/contrib/lz4/build/visual/README.md +5 -0
  35. data/contrib/lz4/build/visual/generate_solution.cmd +55 -0
  36. data/contrib/lz4/build/visual/generate_vs2015.cmd +3 -0
  37. data/contrib/lz4/build/visual/generate_vs2017.cmd +3 -0
  38. data/contrib/lz4/build/visual/generate_vs2019.cmd +3 -0
  39. data/contrib/lz4/build/visual/generate_vs2022.cmd +3 -0
  40. data/contrib/lz4/lib/README.md +25 -1
  41. data/contrib/lz4/lib/lz4.c +206 -99
  42. data/contrib/lz4/lib/lz4.h +111 -69
  43. data/contrib/lz4/lib/lz4file.c +111 -81
  44. data/contrib/lz4/lib/lz4file.h +2 -2
  45. data/contrib/lz4/lib/lz4frame.c +179 -121
  46. data/contrib/lz4/lib/lz4frame.h +162 -103
  47. data/contrib/lz4/lib/lz4hc.c +943 -382
  48. data/contrib/lz4/lib/lz4hc.h +43 -42
  49. data/contrib/lz4/lib/xxhash.c +21 -21
  50. data/contrib/lz4/ossfuzz/decompress_fuzzer.c +1 -1
  51. data/contrib/lz4/ossfuzz/fuzz_helpers.h +1 -1
  52. data/ext/blockapi.c +11 -11
  53. data/ext/frameapi.c +23 -23
  54. metadata +34 -28
  55. data/contrib/lz4/build/VS2010/datagen/datagen.vcxproj +0 -169
  56. data/contrib/lz4/build/VS2010/frametest/frametest.vcxproj +0 -176
  57. data/contrib/lz4/build/VS2010/fullbench/fullbench.vcxproj +0 -176
  58. data/contrib/lz4/build/VS2010/fullbench-dll/fullbench-dll.vcxproj +0 -180
  59. data/contrib/lz4/build/VS2010/fuzzer/fuzzer.vcxproj +0 -173
  60. data/contrib/lz4/build/VS2010/liblz4/liblz4.vcxproj +0 -175
  61. data/contrib/lz4/build/VS2010/liblz4-dll/liblz4-dll.rc +0 -51
  62. data/contrib/lz4/build/VS2010/liblz4-dll/liblz4-dll.vcxproj +0 -179
  63. data/contrib/lz4/build/VS2010/lz4/lz4.vcxproj +0 -189
  64. data/contrib/lz4/build/VS2010/lz4.sln +0 -98
  65. data/contrib/lz4/build/VS2017/datagen/datagen.vcxproj +0 -173
  66. data/contrib/lz4/build/VS2017/frametest/frametest.vcxproj +0 -180
  67. data/contrib/lz4/build/VS2017/fullbench/fullbench.vcxproj +0 -180
  68. data/contrib/lz4/build/VS2017/fullbench-dll/fullbench-dll.vcxproj +0 -184
  69. data/contrib/lz4/build/VS2017/fuzzer/fuzzer.vcxproj +0 -177
  70. data/contrib/lz4/build/VS2017/liblz4/liblz4.vcxproj +0 -179
  71. data/contrib/lz4/build/VS2017/liblz4-dll/liblz4-dll.rc +0 -51
  72. data/contrib/lz4/build/VS2017/liblz4-dll/liblz4-dll.vcxproj +0 -183
  73. data/contrib/lz4/build/VS2017/lz4/lz4.rc +0 -51
  74. data/contrib/lz4/build/VS2017/lz4.sln +0 -103
  75. /data/contrib/lz4/build/{VS2010 → VS2022}/lz4/lz4.rc +0 -0
@@ -1,7 +1,7 @@
1
1
  /*
2
2
  * LZ4 - Fast LZ compression algorithm
3
3
  * Header File
4
- * Copyright (C) 2011-2020, Yann Collet.
4
+ * Copyright (C) 2011-2023, Yann Collet.
5
5
 
6
6
  BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
7
7
 
@@ -129,8 +129,8 @@ extern "C" {
129
129
 
130
130
  /*------ Version ------*/
131
131
  #define LZ4_VERSION_MAJOR 1 /* for breaking interface changes */
132
- #define LZ4_VERSION_MINOR 9 /* for new (non-breaking) interface capabilities */
133
- #define LZ4_VERSION_RELEASE 4 /* for tweaks, bug-fixes, or development */
132
+ #define LZ4_VERSION_MINOR 10 /* for new (non-breaking) interface capabilities */
133
+ #define LZ4_VERSION_RELEASE 0 /* for tweaks, bug-fixes, or development */
134
134
 
135
135
  #define LZ4_VERSION_NUMBER (LZ4_VERSION_MAJOR *100*100 + LZ4_VERSION_MINOR *100 + LZ4_VERSION_RELEASE)
136
136
 
@@ -144,23 +144,25 @@ LZ4LIB_API const char* LZ4_versionString (void); /**< library version string;
144
144
 
145
145
 
146
146
  /*-************************************
147
- * Tuning parameter
147
+ * Tuning memory usage
148
148
  **************************************/
149
- #define LZ4_MEMORY_USAGE_MIN 10
150
- #define LZ4_MEMORY_USAGE_DEFAULT 14
151
- #define LZ4_MEMORY_USAGE_MAX 20
152
-
153
149
  /*!
154
150
  * LZ4_MEMORY_USAGE :
155
- * Memory usage formula : N->2^N Bytes (examples : 10 -> 1KB; 12 -> 4KB ; 16 -> 64KB; 20 -> 1MB; )
156
- * Increasing memory usage improves compression ratio, at the cost of speed.
151
+ * Can be selected at compile time, by setting LZ4_MEMORY_USAGE.
152
+ * Memory usage formula : N->2^N Bytes (examples : 10 -> 1KB; 12 -> 4KB ; 16 -> 64KB; 20 -> 1MB)
153
+ * Increasing memory usage improves compression ratio, generally at the cost of speed.
157
154
  * Reduced memory usage may improve speed at the cost of ratio, thanks to better cache locality.
158
- * Default value is 14, for 16KB, which nicely fits into Intel x86 L1 cache
155
+ * Default value is 14, for 16KB, which nicely fits into most L1 caches.
159
156
  */
160
157
  #ifndef LZ4_MEMORY_USAGE
161
158
  # define LZ4_MEMORY_USAGE LZ4_MEMORY_USAGE_DEFAULT
162
159
  #endif
163
160
 
161
+ /* These are absolute limits, they should not be changed by users */
162
+ #define LZ4_MEMORY_USAGE_MIN 10
163
+ #define LZ4_MEMORY_USAGE_DEFAULT 14
164
+ #define LZ4_MEMORY_USAGE_MAX 20
165
+
164
166
  #if (LZ4_MEMORY_USAGE < LZ4_MEMORY_USAGE_MIN)
165
167
  # error "LZ4_MEMORY_USAGE is too small !"
166
168
  #endif
@@ -189,8 +191,9 @@ LZ4LIB_API const char* LZ4_versionString (void); /**< library version string;
189
191
  LZ4LIB_API int LZ4_compress_default(const char* src, char* dst, int srcSize, int dstCapacity);
190
192
 
191
193
  /*! LZ4_decompress_safe() :
192
- * compressedSize : is the exact complete size of the compressed block.
193
- * dstCapacity : is the size of destination buffer (which must be already allocated), presumed an upper bound of decompressed size.
194
+ * @compressedSize : is the exact complete size of the compressed block.
195
+ * @dstCapacity : is the size of destination buffer (which must be already allocated),
196
+ * presumed an upper bound of decompressed size.
194
197
  * @return : the number of bytes decompressed into destination buffer (necessarily <= dstCapacity)
195
198
  * If destination buffer is not large enough, decoding will stop and output an error code (negative value).
196
199
  * If the source stream is detected malformed, the function will stop decoding and return a negative result.
@@ -242,20 +245,20 @@ LZ4LIB_API int LZ4_compress_fast (const char* src, char* dst, int srcSize, int d
242
245
  LZ4LIB_API int LZ4_sizeofState(void);
243
246
  LZ4LIB_API int LZ4_compress_fast_extState (void* state, const char* src, char* dst, int srcSize, int dstCapacity, int acceleration);
244
247
 
245
-
246
248
  /*! LZ4_compress_destSize() :
247
249
  * Reverse the logic : compresses as much data as possible from 'src' buffer
248
- * into already allocated buffer 'dst', of size >= 'targetDestSize'.
250
+ * into already allocated buffer 'dst', of size >= 'dstCapacity'.
249
251
  * This function either compresses the entire 'src' content into 'dst' if it's large enough,
250
252
  * or fill 'dst' buffer completely with as much data as possible from 'src'.
251
253
  * note: acceleration parameter is fixed to "default".
252
254
  *
253
- * *srcSizePtr : will be modified to indicate how many bytes where read from 'src' to fill 'dst'.
255
+ * *srcSizePtr : in+out parameter. Initially contains size of input.
256
+ * Will be modified to indicate how many bytes where read from 'src' to fill 'dst'.
254
257
  * New value is necessarily <= input value.
255
- * @return : Nb bytes written into 'dst' (necessarily <= targetDestSize)
258
+ * @return : Nb bytes written into 'dst' (necessarily <= dstCapacity)
256
259
  * or 0 if compression fails.
257
260
  *
258
- * Note : from v1.8.2 to v1.9.1, this function had a bug (fixed un v1.9.2+):
261
+ * Note : from v1.8.2 to v1.9.1, this function had a bug (fixed in v1.9.2+):
259
262
  * the produced compressed content could, in specific circumstances,
260
263
  * require to be decompressed into a destination buffer larger
261
264
  * by at least 1 byte than the content to decompress.
@@ -266,8 +269,7 @@ LZ4LIB_API int LZ4_compress_fast_extState (void* state, const char* src, char* d
266
269
  * a dstCapacity which is > decompressedSize, by at least 1 byte.
267
270
  * See https://github.com/lz4/lz4/issues/859 for details
268
271
  */
269
- LZ4LIB_API int LZ4_compress_destSize (const char* src, char* dst, int* srcSizePtr, int targetDstSize);
270
-
272
+ LZ4LIB_API int LZ4_compress_destSize(const char* src, char* dst, int* srcSizePtr, int targetDstSize);
271
273
 
272
274
  /*! LZ4_decompress_safe_partial() :
273
275
  * Decompress an LZ4 compressed block, of size 'srcSize' at position 'src',
@@ -311,7 +313,7 @@ LZ4LIB_API int LZ4_decompress_safe_partial (const char* src, char* dst, int srcS
311
313
  ***********************************************/
312
314
  typedef union LZ4_stream_u LZ4_stream_t; /* incomplete type (defined later) */
313
315
 
314
- /**
316
+ /*!
315
317
  Note about RC_INVOKED
316
318
 
317
319
  - RC_INVOKED is predefined symbol of rc.exe (the resource compiler which is part of MSVC/Visual Studio).
@@ -361,13 +363,58 @@ LZ4LIB_API void LZ4_resetStream_fast (LZ4_stream_t* streamPtr);
361
363
  * LZ4_loadDict() triggers a reset, so any previous data will be forgotten.
362
364
  * The same dictionary will have to be loaded on decompression side for successful decoding.
363
365
  * Dictionary are useful for better compression of small data (KB range).
364
- * While LZ4 accept any input as dictionary,
365
- * results are generally better when using Zstandard's Dictionary Builder.
366
+ * While LZ4 itself accepts any input as dictionary, dictionary efficiency is also a topic.
367
+ * When in doubt, employ the Zstandard's Dictionary Builder.
366
368
  * Loading a size of 0 is allowed, and is the same as reset.
367
- * @return : loaded dictionary size, in bytes (necessarily <= 64 KB)
369
+ * @return : loaded dictionary size, in bytes (note: only the last 64 KB are loaded)
368
370
  */
369
371
  LZ4LIB_API int LZ4_loadDict (LZ4_stream_t* streamPtr, const char* dictionary, int dictSize);
370
372
 
373
+ /*! LZ4_loadDictSlow() : v1.10.0+
374
+ * Same as LZ4_loadDict(),
375
+ * but uses a bit more cpu to reference the dictionary content more thoroughly.
376
+ * This is expected to slightly improve compression ratio.
377
+ * The extra-cpu cost is likely worth it if the dictionary is re-used across multiple sessions.
378
+ * @return : loaded dictionary size, in bytes (note: only the last 64 KB are loaded)
379
+ */
380
+ LZ4LIB_API int LZ4_loadDictSlow(LZ4_stream_t* streamPtr, const char* dictionary, int dictSize);
381
+
382
+ /*! LZ4_attach_dictionary() : stable since v1.10.0
383
+ *
384
+ * This allows efficient re-use of a static dictionary multiple times.
385
+ *
386
+ * Rather than re-loading the dictionary buffer into a working context before
387
+ * each compression, or copying a pre-loaded dictionary's LZ4_stream_t into a
388
+ * working LZ4_stream_t, this function introduces a no-copy setup mechanism,
389
+ * in which the working stream references @dictionaryStream in-place.
390
+ *
391
+ * Several assumptions are made about the state of @dictionaryStream.
392
+ * Currently, only states which have been prepared by LZ4_loadDict() or
393
+ * LZ4_loadDictSlow() should be expected to work.
394
+ *
395
+ * Alternatively, the provided @dictionaryStream may be NULL,
396
+ * in which case any existing dictionary stream is unset.
397
+ *
398
+ * If a dictionary is provided, it replaces any pre-existing stream history.
399
+ * The dictionary contents are the only history that can be referenced and
400
+ * logically immediately precede the data compressed in the first subsequent
401
+ * compression call.
402
+ *
403
+ * The dictionary will only remain attached to the working stream through the
404
+ * first compression call, at the end of which it is cleared.
405
+ * @dictionaryStream stream (and source buffer) must remain in-place / accessible / unchanged
406
+ * through the completion of the compression session.
407
+ *
408
+ * Note: there is no equivalent LZ4_attach_*() method on the decompression side
409
+ * because there is no initialization cost, hence no need to share the cost across multiple sessions.
410
+ * To decompress LZ4 blocks using dictionary, attached or not,
411
+ * just employ the regular LZ4_setStreamDecode() for streaming,
412
+ * or the stateless LZ4_decompress_safe_usingDict() for one-shot decompression.
413
+ */
414
+ LZ4LIB_API void
415
+ LZ4_attach_dictionary(LZ4_stream_t* workingStream,
416
+ const LZ4_stream_t* dictionaryStream);
417
+
371
418
  /*! LZ4_compress_fast_continue() :
372
419
  * Compress 'src' content using data from previously compressed blocks, for better compression ratio.
373
420
  * 'dst' buffer must be already allocated.
@@ -443,11 +490,24 @@ LZ4LIB_API int LZ4_setStreamDecode (LZ4_streamDecode_t* LZ4_streamDecode, const
443
490
  LZ4LIB_API int LZ4_decoderRingBufferSize(int maxBlockSize);
444
491
  #define LZ4_DECODER_RING_BUFFER_SIZE(maxBlockSize) (65536 + 14 + (maxBlockSize)) /* for static allocation; maxBlockSize presumed valid */
445
492
 
446
- /*! LZ4_decompress_*_continue() :
447
- * These decoding functions allow decompression of consecutive blocks in "streaming" mode.
448
- * A block is an unsplittable entity, it must be presented entirely to a decompression function.
449
- * Decompression functions only accepts one block at a time.
450
- * The last 64KB of previously decoded data *must* remain available and unmodified at the memory position where they were decoded.
493
+ /*! LZ4_decompress_safe_continue() :
494
+ * This decoding function allows decompression of consecutive blocks in "streaming" mode.
495
+ * The difference with the usual independent blocks is that
496
+ * new blocks are allowed to find references into former blocks.
497
+ * A block is an unsplittable entity, and must be presented entirely to the decompression function.
498
+ * LZ4_decompress_safe_continue() only accepts one block at a time.
499
+ * It's modeled after `LZ4_decompress_safe()` and behaves similarly.
500
+ *
501
+ * @LZ4_streamDecode : decompression state, tracking the position in memory of past data
502
+ * @compressedSize : exact complete size of one compressed block.
503
+ * @dstCapacity : size of destination buffer (which must be already allocated),
504
+ * must be an upper bound of decompressed size.
505
+ * @return : number of bytes decompressed into destination buffer (necessarily <= dstCapacity)
506
+ * If destination buffer is not large enough, decoding will stop and output an error code (negative value).
507
+ * If the source stream is detected malformed, the function will stop decoding and return a negative result.
508
+ *
509
+ * The last 64KB of previously decoded data *must* remain available and unmodified
510
+ * at the memory position where they were previously decoded.
451
511
  * If less than 64KB of data has been decoded, all the data must be present.
452
512
  *
453
513
  * Special : if decompression side sets a ring buffer, it must respect one of the following conditions :
@@ -474,10 +534,10 @@ LZ4_decompress_safe_continue (LZ4_streamDecode_t* LZ4_streamDecode,
474
534
  int srcSize, int dstCapacity);
475
535
 
476
536
 
477
- /*! LZ4_decompress_*_usingDict() :
478
- * These decoding functions work the same as
479
- * a combination of LZ4_setStreamDecode() followed by LZ4_decompress_*_continue()
480
- * They are stand-alone, and don't need an LZ4_streamDecode_t structure.
537
+ /*! LZ4_decompress_safe_usingDict() :
538
+ * Works the same as
539
+ * a combination of LZ4_setStreamDecode() followed by LZ4_decompress_safe_continue()
540
+ * However, it's stateless: it doesn't need any LZ4_streamDecode_t state.
481
541
  * Dictionary is presumed stable : it must remain accessible and unmodified during decompression.
482
542
  * Performance tip : Decompression speed can be substantially increased
483
543
  * when dst == dictStart + dictSize.
@@ -487,6 +547,12 @@ LZ4_decompress_safe_usingDict(const char* src, char* dst,
487
547
  int srcSize, int dstCapacity,
488
548
  const char* dictStart, int dictSize);
489
549
 
550
+ /*! LZ4_decompress_safe_partial_usingDict() :
551
+ * Behaves the same as LZ4_decompress_safe_partial()
552
+ * with the added ability to specify a memory segment for past data.
553
+ * Performance tip : Decompression speed can be substantially increased
554
+ * when dst == dictStart + dictSize.
555
+ */
490
556
  LZ4LIB_API int
491
557
  LZ4_decompress_safe_partial_usingDict(const char* src, char* dst,
492
558
  int compressedSize,
@@ -526,9 +592,9 @@ LZ4_decompress_safe_partial_usingDict(const char* src, char* dst,
526
592
  #define LZ4_STATIC_3504398509
527
593
 
528
594
  #ifdef LZ4_PUBLISH_STATIC_FUNCTIONS
529
- #define LZ4LIB_STATIC_API LZ4LIB_API
595
+ # define LZ4LIB_STATIC_API LZ4LIB_API
530
596
  #else
531
- #define LZ4LIB_STATIC_API
597
+ # define LZ4LIB_STATIC_API
532
598
  #endif
533
599
 
534
600
 
@@ -544,36 +610,11 @@ LZ4_decompress_safe_partial_usingDict(const char* src, char* dst,
544
610
  */
545
611
  LZ4LIB_STATIC_API int LZ4_compress_fast_extState_fastReset (void* state, const char* src, char* dst, int srcSize, int dstCapacity, int acceleration);
546
612
 
547
- /*! LZ4_attach_dictionary() :
548
- * This is an experimental API that allows
549
- * efficient use of a static dictionary many times.
550
- *
551
- * Rather than re-loading the dictionary buffer into a working context before
552
- * each compression, or copying a pre-loaded dictionary's LZ4_stream_t into a
553
- * working LZ4_stream_t, this function introduces a no-copy setup mechanism,
554
- * in which the working stream references the dictionary stream in-place.
555
- *
556
- * Several assumptions are made about the state of the dictionary stream.
557
- * Currently, only streams which have been prepared by LZ4_loadDict() should
558
- * be expected to work.
559
- *
560
- * Alternatively, the provided dictionaryStream may be NULL,
561
- * in which case any existing dictionary stream is unset.
562
- *
563
- * If a dictionary is provided, it replaces any pre-existing stream history.
564
- * The dictionary contents are the only history that can be referenced and
565
- * logically immediately precede the data compressed in the first subsequent
566
- * compression call.
567
- *
568
- * The dictionary will only remain attached to the working stream through the
569
- * first compression call, at the end of which it is cleared. The dictionary
570
- * stream (and source buffer) must remain in-place / accessible / unchanged
571
- * through the completion of the first compression call on the stream.
613
+ /*! LZ4_compress_destSize_extState() : introduced in v1.10.0
614
+ * Same as LZ4_compress_destSize(), but using an externally allocated state.
615
+ * Also: exposes @acceleration
572
616
  */
573
- LZ4LIB_STATIC_API void
574
- LZ4_attach_dictionary(LZ4_stream_t* workingStream,
575
- const LZ4_stream_t* dictionaryStream);
576
-
617
+ int LZ4_compress_destSize_extState(void* state, const char* src, char* dst, int* srcSizePtr, int targetDstSize, int acceleration);
577
618
 
578
619
  /*! In-place compression and decompression
579
620
  *
@@ -685,7 +726,7 @@ struct LZ4_stream_t_internal {
685
726
  /* Implicit padding to ensure structure is aligned */
686
727
  };
687
728
 
688
- #define LZ4_STREAM_MINSIZE ((1UL << LZ4_MEMORY_USAGE) + 32) /* static size, for inter-version compatibility */
729
+ #define LZ4_STREAM_MINSIZE ((1UL << (LZ4_MEMORY_USAGE)) + 32) /* static size, for inter-version compatibility */
689
730
  union LZ4_stream_u {
690
731
  char minStateSize[LZ4_STREAM_MINSIZE];
691
732
  LZ4_stream_t_internal internal_donotuse;
@@ -706,7 +747,7 @@ union LZ4_stream_u {
706
747
  * Note2: An LZ4_stream_t structure guarantees correct alignment and size.
707
748
  * Note3: Before v1.9.0, use LZ4_resetStream() instead
708
749
  **/
709
- LZ4LIB_API LZ4_stream_t* LZ4_initStream (void* buffer, size_t size);
750
+ LZ4LIB_API LZ4_stream_t* LZ4_initStream (void* stateBuffer, size_t size);
710
751
 
711
752
 
712
753
  /*! LZ4_streamDecode_t :
@@ -818,11 +859,12 @@ LZ4_DEPRECATED("use LZ4_decompress_fast_usingDict() instead") LZ4LIB_API int LZ4
818
859
  * But they may happen if input data is invalid (error or intentional tampering).
819
860
  * As a consequence, use these functions in trusted environments with trusted data **only**.
820
861
  */
821
- LZ4_DEPRECATED("This function is deprecated and unsafe. Consider using LZ4_decompress_safe() instead")
862
+ LZ4_DEPRECATED("This function is deprecated and unsafe. Consider using LZ4_decompress_safe_partial() instead")
822
863
  LZ4LIB_API int LZ4_decompress_fast (const char* src, char* dst, int originalSize);
823
- LZ4_DEPRECATED("This function is deprecated and unsafe. Consider using LZ4_decompress_safe_continue() instead")
864
+ LZ4_DEPRECATED("This function is deprecated and unsafe. Consider migrating towards LZ4_decompress_safe_continue() instead. "
865
+ "Note that the contract will change (requires block's compressed size, instead of decompressed size)")
824
866
  LZ4LIB_API int LZ4_decompress_fast_continue (LZ4_streamDecode_t* LZ4_streamDecode, const char* src, char* dst, int originalSize);
825
- LZ4_DEPRECATED("This function is deprecated and unsafe. Consider using LZ4_decompress_safe_usingDict() instead")
867
+ LZ4_DEPRECATED("This function is deprecated and unsafe. Consider using LZ4_decompress_safe_partial_usingDict() instead")
826
868
  LZ4LIB_API int LZ4_decompress_fast_usingDict (const char* src, char* dst, int originalSize, const char* dictStart, int dictSize);
827
869
 
828
870
  /*! LZ4_resetStream() :
@@ -31,11 +31,21 @@
31
31
  * - LZ4 homepage : http://www.lz4.org
32
32
  * - LZ4 source repository : https://github.com/lz4/lz4
33
33
  */
34
- #include <stdlib.h>
34
+ #include <stdlib.h> /* malloc, free */
35
35
  #include <string.h>
36
+ #include <assert.h>
36
37
  #include "lz4.h"
37
38
  #include "lz4file.h"
38
39
 
40
+ static LZ4F_errorCode_t returnErrorCode(LZ4F_errorCodes code)
41
+ {
42
+ return (LZ4F_errorCode_t)-(ptrdiff_t)code;
43
+ }
44
+ #undef RETURN_ERROR
45
+ #define RETURN_ERROR(e) return returnErrorCode(LZ4F_ERROR_ ## e)
46
+
47
+ /* ===== read API ===== */
48
+
39
49
  struct LZ4_readFile_s {
40
50
  LZ4F_dctx* dctxPtr;
41
51
  FILE* fp;
@@ -45,76 +55,80 @@ struct LZ4_readFile_s {
45
55
  size_t srcBufMaxSize;
46
56
  };
47
57
 
48
- struct LZ4_writeFile_s {
49
- LZ4F_cctx* cctxPtr;
50
- FILE* fp;
51
- LZ4_byte* dstBuf;
52
- size_t maxWriteSize;
53
- size_t dstBufMaxSize;
54
- LZ4F_errorCode_t errCode;
55
- };
58
+ static void LZ4F_freeReadFile(LZ4_readFile_t* lz4fRead)
59
+ {
60
+ if (lz4fRead==NULL) return;
61
+ LZ4F_freeDecompressionContext(lz4fRead->dctxPtr);
62
+ free(lz4fRead->srcBuf);
63
+ free(lz4fRead);
64
+ }
65
+
66
+ static void LZ4F_freeAndNullReadFile(LZ4_readFile_t** statePtr)
67
+ {
68
+ assert(statePtr != NULL);
69
+ LZ4F_freeReadFile(*statePtr);
70
+ *statePtr = NULL;
71
+ }
56
72
 
57
73
  LZ4F_errorCode_t LZ4F_readOpen(LZ4_readFile_t** lz4fRead, FILE* fp)
58
74
  {
59
75
  char buf[LZ4F_HEADER_SIZE_MAX];
60
76
  size_t consumedSize;
61
77
  LZ4F_errorCode_t ret;
62
- LZ4F_frameInfo_t info;
63
78
 
64
79
  if (fp == NULL || lz4fRead == NULL) {
65
- return -LZ4F_ERROR_GENERIC;
80
+ RETURN_ERROR(parameter_null);
66
81
  }
67
82
 
68
83
  *lz4fRead = (LZ4_readFile_t*)calloc(1, sizeof(LZ4_readFile_t));
69
84
  if (*lz4fRead == NULL) {
70
- return -LZ4F_ERROR_allocation_failed;
85
+ RETURN_ERROR(allocation_failed);
71
86
  }
72
87
 
73
- ret = LZ4F_createDecompressionContext(&(*lz4fRead)->dctxPtr, LZ4F_getVersion());
88
+ ret = LZ4F_createDecompressionContext(&(*lz4fRead)->dctxPtr, LZ4F_VERSION);
74
89
  if (LZ4F_isError(ret)) {
75
- free(*lz4fRead);
90
+ LZ4F_freeAndNullReadFile(lz4fRead);
76
91
  return ret;
77
92
  }
78
93
 
79
94
  (*lz4fRead)->fp = fp;
80
95
  consumedSize = fread(buf, 1, sizeof(buf), (*lz4fRead)->fp);
81
96
  if (consumedSize != sizeof(buf)) {
82
- free(*lz4fRead);
83
- return -LZ4F_ERROR_GENERIC;
97
+ LZ4F_freeAndNullReadFile(lz4fRead);
98
+ RETURN_ERROR(io_read);
84
99
  }
85
100
 
86
- ret = LZ4F_getFrameInfo((*lz4fRead)->dctxPtr, &info, buf, &consumedSize);
87
- if (LZ4F_isError(ret)) {
88
- LZ4F_freeDecompressionContext((*lz4fRead)->dctxPtr);
89
- free(*lz4fRead);
90
- return ret;
101
+ { LZ4F_frameInfo_t info;
102
+ LZ4F_errorCode_t const r = LZ4F_getFrameInfo((*lz4fRead)->dctxPtr, &info, buf, &consumedSize);
103
+ if (LZ4F_isError(r)) {
104
+ LZ4F_freeAndNullReadFile(lz4fRead);
105
+ return r;
91
106
  }
92
107
 
93
- switch (info.blockSizeID) {
94
- case LZ4F_default :
95
- case LZ4F_max64KB :
96
- (*lz4fRead)->srcBufMaxSize = 64 * 1024;
97
- break;
98
- case LZ4F_max256KB:
99
- (*lz4fRead)->srcBufMaxSize = 256 * 1024;
100
- break;
101
- case LZ4F_max1MB:
102
- (*lz4fRead)->srcBufMaxSize = 1 * 1024 * 1024;
103
- break;
104
- case LZ4F_max4MB:
105
- (*lz4fRead)->srcBufMaxSize = 4 * 1024 * 1024;
106
- break;
107
- default:
108
- LZ4F_freeDecompressionContext((*lz4fRead)->dctxPtr);
109
- free(*lz4fRead);
110
- return -LZ4F_ERROR_maxBlockSize_invalid;
108
+ switch (info.blockSizeID) {
109
+ case LZ4F_default :
110
+ case LZ4F_max64KB :
111
+ (*lz4fRead)->srcBufMaxSize = 64 * 1024;
112
+ break;
113
+ case LZ4F_max256KB:
114
+ (*lz4fRead)->srcBufMaxSize = 256 * 1024;
115
+ break;
116
+ case LZ4F_max1MB:
117
+ (*lz4fRead)->srcBufMaxSize = 1 * 1024 * 1024;
118
+ break;
119
+ case LZ4F_max4MB:
120
+ (*lz4fRead)->srcBufMaxSize = 4 * 1024 * 1024;
121
+ break;
122
+ default:
123
+ LZ4F_freeAndNullReadFile(lz4fRead);
124
+ RETURN_ERROR(maxBlockSize_invalid);
125
+ }
111
126
  }
112
127
 
113
128
  (*lz4fRead)->srcBuf = (LZ4_byte*)malloc((*lz4fRead)->srcBufMaxSize);
114
129
  if ((*lz4fRead)->srcBuf == NULL) {
115
- LZ4F_freeDecompressionContext((*lz4fRead)->dctxPtr);
116
- free(lz4fRead);
117
- return -LZ4F_ERROR_allocation_failed;
130
+ LZ4F_freeAndNullReadFile(lz4fRead);
131
+ RETURN_ERROR(allocation_failed);
118
132
  }
119
133
 
120
134
  (*lz4fRead)->srcBufSize = sizeof(buf) - consumedSize;
@@ -129,7 +143,7 @@ size_t LZ4F_read(LZ4_readFile_t* lz4fRead, void* buf, size_t size)
129
143
  size_t next = 0;
130
144
 
131
145
  if (lz4fRead == NULL || buf == NULL)
132
- return -LZ4F_ERROR_GENERIC;
146
+ RETURN_ERROR(parameter_null);
133
147
 
134
148
  while (next < size) {
135
149
  size_t srcsize = lz4fRead->srcBufSize - lz4fRead->srcBufNext;
@@ -142,12 +156,10 @@ size_t LZ4F_read(LZ4_readFile_t* lz4fRead, void* buf, size_t size)
142
156
  lz4fRead->srcBufSize = ret;
143
157
  srcsize = lz4fRead->srcBufSize;
144
158
  lz4fRead->srcBufNext = 0;
145
- }
146
- else if (ret == 0) {
159
+ } else if (ret == 0) {
147
160
  break;
148
- }
149
- else {
150
- return -LZ4F_ERROR_GENERIC;
161
+ } else {
162
+ RETURN_ERROR(io_read);
151
163
  }
152
164
  }
153
165
 
@@ -171,24 +183,48 @@ size_t LZ4F_read(LZ4_readFile_t* lz4fRead, void* buf, size_t size)
171
183
  LZ4F_errorCode_t LZ4F_readClose(LZ4_readFile_t* lz4fRead)
172
184
  {
173
185
  if (lz4fRead == NULL)
174
- return -LZ4F_ERROR_GENERIC;
175
- LZ4F_freeDecompressionContext(lz4fRead->dctxPtr);
176
- free(lz4fRead->srcBuf);
177
- free(lz4fRead);
186
+ RETURN_ERROR(parameter_null);
187
+ LZ4F_freeReadFile(lz4fRead);
178
188
  return LZ4F_OK_NoError;
179
189
  }
180
190
 
191
+ /* ===== write API ===== */
192
+
193
+ struct LZ4_writeFile_s {
194
+ LZ4F_cctx* cctxPtr;
195
+ FILE* fp;
196
+ LZ4_byte* dstBuf;
197
+ size_t maxWriteSize;
198
+ size_t dstBufMaxSize;
199
+ LZ4F_errorCode_t errCode;
200
+ };
201
+
202
+ static void LZ4F_freeWriteFile(LZ4_writeFile_t* state)
203
+ {
204
+ if (state == NULL) return;
205
+ LZ4F_freeCompressionContext(state->cctxPtr);
206
+ free(state->dstBuf);
207
+ free(state);
208
+ }
209
+
210
+ static void LZ4F_freeAndNullWriteFile(LZ4_writeFile_t** statePtr)
211
+ {
212
+ assert(statePtr != NULL);
213
+ LZ4F_freeWriteFile(*statePtr);
214
+ *statePtr = NULL;
215
+ }
216
+
181
217
  LZ4F_errorCode_t LZ4F_writeOpen(LZ4_writeFile_t** lz4fWrite, FILE* fp, const LZ4F_preferences_t* prefsPtr)
182
218
  {
183
219
  LZ4_byte buf[LZ4F_HEADER_SIZE_MAX];
184
220
  size_t ret;
185
221
 
186
222
  if (fp == NULL || lz4fWrite == NULL)
187
- return -LZ4F_ERROR_GENERIC;
223
+ RETURN_ERROR(parameter_null);
188
224
 
189
- *lz4fWrite = (LZ4_writeFile_t*)malloc(sizeof(LZ4_writeFile_t));
225
+ *lz4fWrite = (LZ4_writeFile_t*)calloc(1, sizeof(LZ4_writeFile_t));
190
226
  if (*lz4fWrite == NULL) {
191
- return -LZ4F_ERROR_allocation_failed;
227
+ RETURN_ERROR(allocation_failed);
192
228
  }
193
229
  if (prefsPtr != NULL) {
194
230
  switch (prefsPtr->frameInfo.blockSizeID) {
@@ -206,8 +242,8 @@ LZ4F_errorCode_t LZ4F_writeOpen(LZ4_writeFile_t** lz4fWrite, FILE* fp, const LZ4
206
242
  (*lz4fWrite)->maxWriteSize = 4 * 1024 * 1024;
207
243
  break;
208
244
  default:
209
- free(lz4fWrite);
210
- return -LZ4F_ERROR_maxBlockSize_invalid;
245
+ LZ4F_freeAndNullWriteFile(lz4fWrite);
246
+ RETURN_ERROR(maxBlockSize_invalid);
211
247
  }
212
248
  } else {
213
249
  (*lz4fWrite)->maxWriteSize = 64 * 1024;
@@ -216,30 +252,25 @@ LZ4F_errorCode_t LZ4F_writeOpen(LZ4_writeFile_t** lz4fWrite, FILE* fp, const LZ4
216
252
  (*lz4fWrite)->dstBufMaxSize = LZ4F_compressBound((*lz4fWrite)->maxWriteSize, prefsPtr);
217
253
  (*lz4fWrite)->dstBuf = (LZ4_byte*)malloc((*lz4fWrite)->dstBufMaxSize);
218
254
  if ((*lz4fWrite)->dstBuf == NULL) {
219
- free(*lz4fWrite);
220
- return -LZ4F_ERROR_allocation_failed;
255
+ LZ4F_freeAndNullWriteFile(lz4fWrite);
256
+ RETURN_ERROR(allocation_failed);
221
257
  }
222
258
 
223
- ret = LZ4F_createCompressionContext(&(*lz4fWrite)->cctxPtr, LZ4F_getVersion());
259
+ ret = LZ4F_createCompressionContext(&(*lz4fWrite)->cctxPtr, LZ4F_VERSION);
224
260
  if (LZ4F_isError(ret)) {
225
- free((*lz4fWrite)->dstBuf);
226
- free(*lz4fWrite);
261
+ LZ4F_freeAndNullWriteFile(lz4fWrite);
227
262
  return ret;
228
263
  }
229
264
 
230
265
  ret = LZ4F_compressBegin((*lz4fWrite)->cctxPtr, buf, LZ4F_HEADER_SIZE_MAX, prefsPtr);
231
266
  if (LZ4F_isError(ret)) {
232
- LZ4F_freeCompressionContext((*lz4fWrite)->cctxPtr);
233
- free((*lz4fWrite)->dstBuf);
234
- free(*lz4fWrite);
267
+ LZ4F_freeAndNullWriteFile(lz4fWrite);
235
268
  return ret;
236
269
  }
237
270
 
238
271
  if (ret != fwrite(buf, 1, ret, fp)) {
239
- LZ4F_freeCompressionContext((*lz4fWrite)->cctxPtr);
240
- free((*lz4fWrite)->dstBuf);
241
- free(*lz4fWrite);
242
- return -LZ4F_ERROR_GENERIC;
272
+ LZ4F_freeAndNullWriteFile(lz4fWrite);
273
+ RETURN_ERROR(io_write);
243
274
  }
244
275
 
245
276
  (*lz4fWrite)->fp = fp;
@@ -247,15 +278,15 @@ LZ4F_errorCode_t LZ4F_writeOpen(LZ4_writeFile_t** lz4fWrite, FILE* fp, const LZ4
247
278
  return LZ4F_OK_NoError;
248
279
  }
249
280
 
250
- size_t LZ4F_write(LZ4_writeFile_t* lz4fWrite, void* buf, size_t size)
281
+ size_t LZ4F_write(LZ4_writeFile_t* lz4fWrite, const void* buf, size_t size)
251
282
  {
252
- LZ4_byte* p = (LZ4_byte*)buf;
283
+ const LZ4_byte* p = (const LZ4_byte*)buf;
253
284
  size_t remain = size;
254
285
  size_t chunk;
255
286
  size_t ret;
256
287
 
257
288
  if (lz4fWrite == NULL || buf == NULL)
258
- return -LZ4F_ERROR_GENERIC;
289
+ RETURN_ERROR(parameter_null);
259
290
  while (remain) {
260
291
  if (remain > lz4fWrite->maxWriteSize)
261
292
  chunk = lz4fWrite->maxWriteSize;
@@ -271,9 +302,9 @@ size_t LZ4F_write(LZ4_writeFile_t* lz4fWrite, void* buf, size_t size)
271
302
  return ret;
272
303
  }
273
304
 
274
- if(ret != fwrite(lz4fWrite->dstBuf, 1, ret, lz4fWrite->fp)) {
275
- lz4fWrite->errCode = -LZ4F_ERROR_GENERIC;
276
- return -LZ4F_ERROR_GENERIC;
305
+ if (ret != fwrite(lz4fWrite->dstBuf, 1, ret, lz4fWrite->fp)) {
306
+ lz4fWrite->errCode = returnErrorCode(LZ4F_ERROR_io_write);
307
+ RETURN_ERROR(io_write);
277
308
  }
278
309
 
279
310
  p += chunk;
@@ -287,8 +318,9 @@ LZ4F_errorCode_t LZ4F_writeClose(LZ4_writeFile_t* lz4fWrite)
287
318
  {
288
319
  LZ4F_errorCode_t ret = LZ4F_OK_NoError;
289
320
 
290
- if (lz4fWrite == NULL)
291
- return -LZ4F_ERROR_GENERIC;
321
+ if (lz4fWrite == NULL) {
322
+ RETURN_ERROR(parameter_null);
323
+ }
292
324
 
293
325
  if (lz4fWrite->errCode == LZ4F_OK_NoError) {
294
326
  ret = LZ4F_compressEnd(lz4fWrite->cctxPtr,
@@ -299,13 +331,11 @@ LZ4F_errorCode_t LZ4F_writeClose(LZ4_writeFile_t* lz4fWrite)
299
331
  }
300
332
 
301
333
  if (ret != fwrite(lz4fWrite->dstBuf, 1, ret, lz4fWrite->fp)) {
302
- ret = -LZ4F_ERROR_GENERIC;
334
+ ret = returnErrorCode(LZ4F_ERROR_io_write);
303
335
  }
304
336
  }
305
337
 
306
338
  out:
307
- LZ4F_freeCompressionContext(lz4fWrite->cctxPtr);
308
- free(lz4fWrite->dstBuf);
309
- free(lz4fWrite);
339
+ LZ4F_freeWriteFile(lz4fWrite);
310
340
  return ret;
311
341
  }
@@ -38,7 +38,7 @@ extern "C" {
38
38
  #ifndef LZ4FILE_H
39
39
  #define LZ4FILE_H
40
40
 
41
- #include <stdio.h>
41
+ #include <stdio.h> /* FILE* */
42
42
  #include "lz4frame_static.h"
43
43
 
44
44
  typedef struct LZ4_readFile_s LZ4_readFile_t;
@@ -78,7 +78,7 @@ LZ4FLIB_STATIC_API LZ4F_errorCode_t LZ4F_writeOpen(LZ4_writeFile_t** lz4fWrite,
78
78
  * `buf` write data buffer.
79
79
  * `size` write data buffer size.
80
80
  */
81
- LZ4FLIB_STATIC_API size_t LZ4F_write(LZ4_writeFile_t* lz4fWrite, void* buf, size_t size);
81
+ LZ4FLIB_STATIC_API size_t LZ4F_write(LZ4_writeFile_t* lz4fWrite, const void* buf, size_t size);
82
82
 
83
83
  /*! LZ4F_writeClose() :
84
84
  * Close lz4file handle.