extlz4 0.3.3 → 0.3.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/Rakefile +22 -0
  4. data/contrib/lz4/LICENSE +2 -1
  5. data/contrib/lz4/Makefile.inc +39 -15
  6. data/contrib/lz4/NEWS +21 -0
  7. data/contrib/lz4/README.md +1 -1
  8. data/contrib/lz4/build/VS2010/liblz4-dll/liblz4-dll.rc +1 -1
  9. data/contrib/lz4/build/VS2010/lz4/lz4.rc +1 -1
  10. data/contrib/lz4/build/VS2017/liblz4-dll/liblz4-dll.rc +1 -1
  11. data/contrib/lz4/build/VS2017/lz4/lz4.rc +1 -1
  12. data/contrib/lz4/build/VS2017/lz4/lz4.vcxproj +12 -1
  13. data/contrib/lz4/build/VS2022/datagen/datagen.vcxproj +173 -0
  14. data/contrib/lz4/build/VS2022/frametest/frametest.vcxproj +180 -0
  15. data/contrib/lz4/build/VS2022/fullbench/fullbench.vcxproj +180 -0
  16. data/contrib/lz4/build/VS2022/fullbench-dll/fullbench-dll.vcxproj +184 -0
  17. data/contrib/lz4/build/VS2022/fuzzer/fuzzer.vcxproj +177 -0
  18. data/contrib/lz4/build/VS2022/liblz4/liblz4.vcxproj +179 -0
  19. data/contrib/lz4/build/VS2022/liblz4-dll/liblz4-dll.rc +51 -0
  20. data/contrib/lz4/build/VS2022/liblz4-dll/liblz4-dll.vcxproj +183 -0
  21. data/contrib/lz4/build/VS2022/lz4.sln +103 -0
  22. data/contrib/lz4/build/cmake/CMakeLists.txt +49 -11
  23. data/contrib/lz4/build/cmake/lz4Config.cmake.in +2 -0
  24. data/contrib/lz4/lib/LICENSE +1 -1
  25. data/contrib/lz4/lib/README.md +45 -13
  26. data/contrib/lz4/lib/liblz4-dll.rc.in +1 -1
  27. data/contrib/lz4/lib/liblz4.pc.in +3 -3
  28. data/contrib/lz4/lib/lz4.c +422 -195
  29. data/contrib/lz4/lib/lz4.h +114 -46
  30. data/contrib/lz4/lib/lz4file.c +311 -0
  31. data/contrib/lz4/lib/lz4file.h +93 -0
  32. data/contrib/lz4/lib/lz4frame.c +421 -242
  33. data/contrib/lz4/lib/lz4frame.h +122 -53
  34. data/contrib/lz4/lib/lz4frame_static.h +1 -1
  35. data/contrib/lz4/lib/lz4hc.c +127 -111
  36. data/contrib/lz4/lib/lz4hc.h +14 -14
  37. data/contrib/lz4/ossfuzz/Makefile +1 -0
  38. data/contrib/lz4/ossfuzz/decompress_fuzzer.c +18 -2
  39. data/contrib/lz4/ossfuzz/fuzz_helpers.h +3 -2
  40. data/contrib/lz4/ossfuzz/round_trip_frame_uncompressed_fuzzer.c +134 -0
  41. data/contrib/lz4/ossfuzz/round_trip_fuzzer.c +66 -6
  42. data/ext/blockapi.c +8 -8
  43. data/ext/extlz4.h +12 -0
  44. data/ext/frameapi.c +3 -3
  45. data/ext/hashargs.c +7 -1
  46. metadata +16 -5
  47. data/contrib/lz4/tmp +0 -0
  48. data/contrib/lz4/tmpsparse +0 -0
@@ -1,7 +1,7 @@
1
1
  /*
2
2
  * LZ4 - Fast LZ compression algorithm
3
3
  * Header File
4
- * Copyright (C) 2011-present, Yann Collet.
4
+ * Copyright (C) 2011-2020, Yann Collet.
5
5
 
6
6
  BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
7
7
 
@@ -97,36 +97,77 @@ extern "C" {
97
97
  # define LZ4LIB_API LZ4LIB_VISIBILITY
98
98
  #endif
99
99
 
100
+ /*! LZ4_FREESTANDING :
101
+ * When this macro is set to 1, it enables "freestanding mode" that is
102
+ * suitable for typical freestanding environment which doesn't support
103
+ * standard C library.
104
+ *
105
+ * - LZ4_FREESTANDING is a compile-time switch.
106
+ * - It requires the following macros to be defined:
107
+ * LZ4_memcpy, LZ4_memmove, LZ4_memset.
108
+ * - It only enables LZ4/HC functions which don't use heap.
109
+ * All LZ4F_* functions are not supported.
110
+ * - See tests/freestanding.c to check its basic setup.
111
+ */
112
+ #if defined(LZ4_FREESTANDING) && (LZ4_FREESTANDING == 1)
113
+ # define LZ4_HEAPMODE 0
114
+ # define LZ4HC_HEAPMODE 0
115
+ # define LZ4_STATIC_LINKING_ONLY_DISABLE_MEMORY_ALLOCATION 1
116
+ # if !defined(LZ4_memcpy)
117
+ # error "LZ4_FREESTANDING requires macro 'LZ4_memcpy'."
118
+ # endif
119
+ # if !defined(LZ4_memset)
120
+ # error "LZ4_FREESTANDING requires macro 'LZ4_memset'."
121
+ # endif
122
+ # if !defined(LZ4_memmove)
123
+ # error "LZ4_FREESTANDING requires macro 'LZ4_memmove'."
124
+ # endif
125
+ #elif ! defined(LZ4_FREESTANDING)
126
+ # define LZ4_FREESTANDING 0
127
+ #endif
128
+
129
+
100
130
  /*------ Version ------*/
101
131
  #define LZ4_VERSION_MAJOR 1 /* for breaking interface changes */
102
132
  #define LZ4_VERSION_MINOR 9 /* for new (non-breaking) interface capabilities */
103
- #define LZ4_VERSION_RELEASE 3 /* for tweaks, bug-fixes, or development */
133
+ #define LZ4_VERSION_RELEASE 4 /* for tweaks, bug-fixes, or development */
104
134
 
105
135
  #define LZ4_VERSION_NUMBER (LZ4_VERSION_MAJOR *100*100 + LZ4_VERSION_MINOR *100 + LZ4_VERSION_RELEASE)
106
136
 
107
137
  #define LZ4_LIB_VERSION LZ4_VERSION_MAJOR.LZ4_VERSION_MINOR.LZ4_VERSION_RELEASE
108
138
  #define LZ4_QUOTE(str) #str
109
139
  #define LZ4_EXPAND_AND_QUOTE(str) LZ4_QUOTE(str)
110
- #define LZ4_VERSION_STRING LZ4_EXPAND_AND_QUOTE(LZ4_LIB_VERSION)
140
+ #define LZ4_VERSION_STRING LZ4_EXPAND_AND_QUOTE(LZ4_LIB_VERSION) /* requires v1.7.3+ */
111
141
 
112
- LZ4LIB_API int LZ4_versionNumber (void); /**< library version number; useful to check dll version */
113
- LZ4LIB_API const char* LZ4_versionString (void); /**< library version string; useful to check dll version */
142
+ LZ4LIB_API int LZ4_versionNumber (void); /**< library version number; useful to check dll version; requires v1.3.0+ */
143
+ LZ4LIB_API const char* LZ4_versionString (void); /**< library version string; useful to check dll version; requires v1.7.5+ */
114
144
 
115
145
 
116
146
  /*-************************************
117
147
  * Tuning parameter
118
148
  **************************************/
149
+ #define LZ4_MEMORY_USAGE_MIN 10
150
+ #define LZ4_MEMORY_USAGE_DEFAULT 14
151
+ #define LZ4_MEMORY_USAGE_MAX 20
152
+
119
153
  /*!
120
154
  * LZ4_MEMORY_USAGE :
121
- * Memory usage formula : N->2^N Bytes (examples : 10 -> 1KB; 12 -> 4KB ; 16 -> 64KB; 20 -> 1MB; etc.)
122
- * Increasing memory usage improves compression ratio.
123
- * Reduced memory usage may improve speed, thanks to better cache locality.
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.
157
+ * Reduced memory usage may improve speed at the cost of ratio, thanks to better cache locality.
124
158
  * Default value is 14, for 16KB, which nicely fits into Intel x86 L1 cache
125
159
  */
126
160
  #ifndef LZ4_MEMORY_USAGE
127
- # define LZ4_MEMORY_USAGE 14
161
+ # define LZ4_MEMORY_USAGE LZ4_MEMORY_USAGE_DEFAULT
128
162
  #endif
129
163
 
164
+ #if (LZ4_MEMORY_USAGE < LZ4_MEMORY_USAGE_MIN)
165
+ # error "LZ4_MEMORY_USAGE is too small !"
166
+ #endif
167
+
168
+ #if (LZ4_MEMORY_USAGE > LZ4_MEMORY_USAGE_MAX)
169
+ # error "LZ4_MEMORY_USAGE is too large !"
170
+ #endif
130
171
 
131
172
  /*-************************************
132
173
  * Simple Functions
@@ -270,8 +311,25 @@ LZ4LIB_API int LZ4_decompress_safe_partial (const char* src, char* dst, int srcS
270
311
  ***********************************************/
271
312
  typedef union LZ4_stream_u LZ4_stream_t; /* incomplete type (defined later) */
272
313
 
314
+ /**
315
+ Note about RC_INVOKED
316
+
317
+ - RC_INVOKED is predefined symbol of rc.exe (the resource compiler which is part of MSVC/Visual Studio).
318
+ https://docs.microsoft.com/en-us/windows/win32/menurc/predefined-macros
319
+
320
+ - Since rc.exe is a legacy compiler, it truncates long symbol (> 30 chars)
321
+ and reports warning "RC4011: identifier truncated".
322
+
323
+ - To eliminate the warning, we surround long preprocessor symbol with
324
+ "#if !defined(RC_INVOKED) ... #endif" block that means
325
+ "skip this block when rc.exe is trying to read it".
326
+ */
327
+ #if !defined(RC_INVOKED) /* https://docs.microsoft.com/en-us/windows/win32/menurc/predefined-macros */
328
+ #if !defined(LZ4_STATIC_LINKING_ONLY_DISABLE_MEMORY_ALLOCATION)
273
329
  LZ4LIB_API LZ4_stream_t* LZ4_createStream(void);
274
330
  LZ4LIB_API int LZ4_freeStream (LZ4_stream_t* streamPtr);
331
+ #endif /* !defined(LZ4_STATIC_LINKING_ONLY_DISABLE_MEMORY_ALLOCATION) */
332
+ #endif
275
333
 
276
334
  /*! LZ4_resetStream_fast() : v1.9.0+
277
335
  * Use this to prepare an LZ4_stream_t for a new chain of dependent blocks
@@ -355,8 +413,12 @@ typedef union LZ4_streamDecode_u LZ4_streamDecode_t; /* tracking context */
355
413
  * creation / destruction of streaming decompression tracking context.
356
414
  * A tracking context can be re-used multiple times.
357
415
  */
416
+ #if !defined(RC_INVOKED) /* https://docs.microsoft.com/en-us/windows/win32/menurc/predefined-macros */
417
+ #if !defined(LZ4_STATIC_LINKING_ONLY_DISABLE_MEMORY_ALLOCATION)
358
418
  LZ4LIB_API LZ4_streamDecode_t* LZ4_createStreamDecode(void);
359
419
  LZ4LIB_API int LZ4_freeStreamDecode (LZ4_streamDecode_t* LZ4_stream);
420
+ #endif /* !defined(LZ4_STATIC_LINKING_ONLY_DISABLE_MEMORY_ALLOCATION) */
421
+ #endif
360
422
 
361
423
  /*! LZ4_setStreamDecode() :
362
424
  * An LZ4_streamDecode_t context can be allocated once and re-used multiple times.
@@ -406,7 +468,10 @@ LZ4LIB_API int LZ4_decoderRingBufferSize(int maxBlockSize);
406
468
  * save the last 64KB of decoded data into a safe buffer where it can't be modified during decompression,
407
469
  * then indicate where this data is saved using LZ4_setStreamDecode(), before decompressing next block.
408
470
  */
409
- LZ4LIB_API int LZ4_decompress_safe_continue (LZ4_streamDecode_t* LZ4_streamDecode, const char* src, char* dst, int srcSize, int dstCapacity);
471
+ LZ4LIB_API int
472
+ LZ4_decompress_safe_continue (LZ4_streamDecode_t* LZ4_streamDecode,
473
+ const char* src, char* dst,
474
+ int srcSize, int dstCapacity);
410
475
 
411
476
 
412
477
  /*! LZ4_decompress_*_usingDict() :
@@ -417,7 +482,16 @@ LZ4LIB_API int LZ4_decompress_safe_continue (LZ4_streamDecode_t* LZ4_streamDecod
417
482
  * Performance tip : Decompression speed can be substantially increased
418
483
  * when dst == dictStart + dictSize.
419
484
  */
420
- LZ4LIB_API int LZ4_decompress_safe_usingDict (const char* src, char* dst, int srcSize, int dstCapcity, const char* dictStart, int dictSize);
485
+ LZ4LIB_API int
486
+ LZ4_decompress_safe_usingDict(const char* src, char* dst,
487
+ int srcSize, int dstCapacity,
488
+ const char* dictStart, int dictSize);
489
+
490
+ LZ4LIB_API int
491
+ LZ4_decompress_safe_partial_usingDict(const char* src, char* dst,
492
+ int compressedSize,
493
+ int targetOutputSize, int maxOutputSize,
494
+ const char* dictStart, int dictSize);
421
495
 
422
496
  #endif /* LZ4_H_2983827168210 */
423
497
 
@@ -496,13 +570,15 @@ LZ4LIB_STATIC_API int LZ4_compress_fast_extState_fastReset (void* state, const c
496
570
  * stream (and source buffer) must remain in-place / accessible / unchanged
497
571
  * through the completion of the first compression call on the stream.
498
572
  */
499
- LZ4LIB_STATIC_API void LZ4_attach_dictionary(LZ4_stream_t* workingStream, const LZ4_stream_t* dictionaryStream);
573
+ LZ4LIB_STATIC_API void
574
+ LZ4_attach_dictionary(LZ4_stream_t* workingStream,
575
+ const LZ4_stream_t* dictionaryStream);
500
576
 
501
577
 
502
578
  /*! In-place compression and decompression
503
579
  *
504
580
  * It's possible to have input and output sharing the same buffer,
505
- * for highly contrained memory environments.
581
+ * for highly constrained memory environments.
506
582
  * In both cases, it requires input to lay at the end of the buffer,
507
583
  * and decompression to start at beginning of the buffer.
508
584
  * Buffer size must feature some margin, hence be larger than final size.
@@ -592,38 +668,26 @@ LZ4LIB_STATIC_API void LZ4_attach_dictionary(LZ4_stream_t* workingStream, const
592
668
  typedef unsigned int LZ4_u32;
593
669
  #endif
594
670
 
671
+ /*! LZ4_stream_t :
672
+ * Never ever use below internal definitions directly !
673
+ * These definitions are not API/ABI safe, and may change in future versions.
674
+ * If you need static allocation, declare or allocate an LZ4_stream_t object.
675
+ **/
676
+
595
677
  typedef struct LZ4_stream_t_internal LZ4_stream_t_internal;
596
678
  struct LZ4_stream_t_internal {
597
679
  LZ4_u32 hashTable[LZ4_HASH_SIZE_U32];
598
- LZ4_u32 currentOffset;
599
- LZ4_u32 tableType;
600
680
  const LZ4_byte* dictionary;
601
681
  const LZ4_stream_t_internal* dictCtx;
682
+ LZ4_u32 currentOffset;
683
+ LZ4_u32 tableType;
602
684
  LZ4_u32 dictSize;
685
+ /* Implicit padding to ensure structure is aligned */
603
686
  };
604
687
 
605
- typedef struct {
606
- const LZ4_byte* externalDict;
607
- size_t extDictSize;
608
- const LZ4_byte* prefixEnd;
609
- size_t prefixSize;
610
- } LZ4_streamDecode_t_internal;
611
-
612
-
613
- /*! LZ4_stream_t :
614
- * Do not use below internal definitions directly !
615
- * Declare or allocate an LZ4_stream_t instead.
616
- * LZ4_stream_t can also be created using LZ4_createStream(), which is recommended.
617
- * The structure definition can be convenient for static allocation
618
- * (on stack, or as part of larger structure).
619
- * Init this structure with LZ4_initStream() before first use.
620
- * note : only use this definition in association with static linking !
621
- * this definition is not API/ABI safe, and may change in future versions.
622
- */
623
- #define LZ4_STREAMSIZE 16416 /* static size, for inter-version compatibility */
624
- #define LZ4_STREAMSIZE_VOIDP (LZ4_STREAMSIZE / sizeof(void*))
688
+ #define LZ4_STREAM_MINSIZE ((1UL << LZ4_MEMORY_USAGE) + 32) /* static size, for inter-version compatibility */
625
689
  union LZ4_stream_u {
626
- void* table[LZ4_STREAMSIZE_VOIDP];
690
+ char minStateSize[LZ4_STREAM_MINSIZE];
627
691
  LZ4_stream_t_internal internal_donotuse;
628
692
  }; /* previously typedef'd to LZ4_stream_t */
629
693
 
@@ -641,21 +705,25 @@ union LZ4_stream_u {
641
705
  * In which case, the function will @return NULL.
642
706
  * Note2: An LZ4_stream_t structure guarantees correct alignment and size.
643
707
  * Note3: Before v1.9.0, use LZ4_resetStream() instead
644
- */
708
+ **/
645
709
  LZ4LIB_API LZ4_stream_t* LZ4_initStream (void* buffer, size_t size);
646
710
 
647
711
 
648
712
  /*! LZ4_streamDecode_t :
649
- * information structure to track an LZ4 stream during decompression.
650
- * init this structure using LZ4_setStreamDecode() before first use.
651
- * note : only use in association with static linking !
652
- * this definition is not API/ABI safe,
653
- * and may change in a future version !
654
- */
655
- #define LZ4_STREAMDECODESIZE_U64 (4 + ((sizeof(void*)==16) ? 2 : 0) /*AS-400*/ )
656
- #define LZ4_STREAMDECODESIZE (LZ4_STREAMDECODESIZE_U64 * sizeof(unsigned long long))
713
+ * Never ever use below internal definitions directly !
714
+ * These definitions are not API/ABI safe, and may change in future versions.
715
+ * If you need static allocation, declare or allocate an LZ4_streamDecode_t object.
716
+ **/
717
+ typedef struct {
718
+ const LZ4_byte* externalDict;
719
+ const LZ4_byte* prefixEnd;
720
+ size_t extDictSize;
721
+ size_t prefixSize;
722
+ } LZ4_streamDecode_t_internal;
723
+
724
+ #define LZ4_STREAMDECODE_MINSIZE 32
657
725
  union LZ4_streamDecode_u {
658
- unsigned long long table[LZ4_STREAMDECODESIZE_U64];
726
+ char minStateSize[LZ4_STREAMDECODE_MINSIZE];
659
727
  LZ4_streamDecode_t_internal internal_donotuse;
660
728
  } ; /* previously typedef'd to LZ4_streamDecode_t */
661
729
 
@@ -0,0 +1,311 @@
1
+ /*
2
+ * LZ4 file library
3
+ * Copyright (C) 2022, Xiaomi Inc.
4
+ *
5
+ * BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
6
+ *
7
+ * Redistribution and use in source and binary forms, with or without
8
+ * modification, are permitted provided that the following conditions are
9
+ * met:
10
+ *
11
+ * - Redistributions of source code must retain the above copyright
12
+ * notice, this list of conditions and the following disclaimer.
13
+ * - Redistributions in binary form must reproduce the above
14
+ * copyright notice, this list of conditions and the following disclaimer
15
+ * in the documentation and/or other materials provided with the
16
+ * distribution.
17
+ *
18
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
+ *
30
+ * You can contact the author at :
31
+ * - LZ4 homepage : http://www.lz4.org
32
+ * - LZ4 source repository : https://github.com/lz4/lz4
33
+ */
34
+ #include <stdlib.h>
35
+ #include <string.h>
36
+ #include "lz4.h"
37
+ #include "lz4file.h"
38
+
39
+ struct LZ4_readFile_s {
40
+ LZ4F_dctx* dctxPtr;
41
+ FILE* fp;
42
+ LZ4_byte* srcBuf;
43
+ size_t srcBufNext;
44
+ size_t srcBufSize;
45
+ size_t srcBufMaxSize;
46
+ };
47
+
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
+ };
56
+
57
+ LZ4F_errorCode_t LZ4F_readOpen(LZ4_readFile_t** lz4fRead, FILE* fp)
58
+ {
59
+ char buf[LZ4F_HEADER_SIZE_MAX];
60
+ size_t consumedSize;
61
+ LZ4F_errorCode_t ret;
62
+ LZ4F_frameInfo_t info;
63
+
64
+ if (fp == NULL || lz4fRead == NULL) {
65
+ return -LZ4F_ERROR_GENERIC;
66
+ }
67
+
68
+ *lz4fRead = (LZ4_readFile_t*)calloc(1, sizeof(LZ4_readFile_t));
69
+ if (*lz4fRead == NULL) {
70
+ return -LZ4F_ERROR_allocation_failed;
71
+ }
72
+
73
+ ret = LZ4F_createDecompressionContext(&(*lz4fRead)->dctxPtr, LZ4F_getVersion());
74
+ if (LZ4F_isError(ret)) {
75
+ free(*lz4fRead);
76
+ return ret;
77
+ }
78
+
79
+ (*lz4fRead)->fp = fp;
80
+ consumedSize = fread(buf, 1, sizeof(buf), (*lz4fRead)->fp);
81
+ if (consumedSize != sizeof(buf)) {
82
+ free(*lz4fRead);
83
+ return -LZ4F_ERROR_GENERIC;
84
+ }
85
+
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;
91
+ }
92
+
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;
111
+ }
112
+
113
+ (*lz4fRead)->srcBuf = (LZ4_byte*)malloc((*lz4fRead)->srcBufMaxSize);
114
+ if ((*lz4fRead)->srcBuf == NULL) {
115
+ LZ4F_freeDecompressionContext((*lz4fRead)->dctxPtr);
116
+ free(lz4fRead);
117
+ return -LZ4F_ERROR_allocation_failed;
118
+ }
119
+
120
+ (*lz4fRead)->srcBufSize = sizeof(buf) - consumedSize;
121
+ memcpy((*lz4fRead)->srcBuf, buf + consumedSize, (*lz4fRead)->srcBufSize);
122
+
123
+ return ret;
124
+ }
125
+
126
+ size_t LZ4F_read(LZ4_readFile_t* lz4fRead, void* buf, size_t size)
127
+ {
128
+ LZ4_byte* p = (LZ4_byte*)buf;
129
+ size_t next = 0;
130
+
131
+ if (lz4fRead == NULL || buf == NULL)
132
+ return -LZ4F_ERROR_GENERIC;
133
+
134
+ while (next < size) {
135
+ size_t srcsize = lz4fRead->srcBufSize - lz4fRead->srcBufNext;
136
+ size_t dstsize = size - next;
137
+ size_t ret;
138
+
139
+ if (srcsize == 0) {
140
+ ret = fread(lz4fRead->srcBuf, 1, lz4fRead->srcBufMaxSize, lz4fRead->fp);
141
+ if (ret > 0) {
142
+ lz4fRead->srcBufSize = ret;
143
+ srcsize = lz4fRead->srcBufSize;
144
+ lz4fRead->srcBufNext = 0;
145
+ }
146
+ else if (ret == 0) {
147
+ break;
148
+ }
149
+ else {
150
+ return -LZ4F_ERROR_GENERIC;
151
+ }
152
+ }
153
+
154
+ ret = LZ4F_decompress(lz4fRead->dctxPtr,
155
+ p, &dstsize,
156
+ lz4fRead->srcBuf + lz4fRead->srcBufNext,
157
+ &srcsize,
158
+ NULL);
159
+ if (LZ4F_isError(ret)) {
160
+ return ret;
161
+ }
162
+
163
+ lz4fRead->srcBufNext += srcsize;
164
+ next += dstsize;
165
+ p += dstsize;
166
+ }
167
+
168
+ return next;
169
+ }
170
+
171
+ LZ4F_errorCode_t LZ4F_readClose(LZ4_readFile_t* lz4fRead)
172
+ {
173
+ if (lz4fRead == NULL)
174
+ return -LZ4F_ERROR_GENERIC;
175
+ LZ4F_freeDecompressionContext(lz4fRead->dctxPtr);
176
+ free(lz4fRead->srcBuf);
177
+ free(lz4fRead);
178
+ return LZ4F_OK_NoError;
179
+ }
180
+
181
+ LZ4F_errorCode_t LZ4F_writeOpen(LZ4_writeFile_t** lz4fWrite, FILE* fp, const LZ4F_preferences_t* prefsPtr)
182
+ {
183
+ LZ4_byte buf[LZ4F_HEADER_SIZE_MAX];
184
+ size_t ret;
185
+
186
+ if (fp == NULL || lz4fWrite == NULL)
187
+ return -LZ4F_ERROR_GENERIC;
188
+
189
+ *lz4fWrite = (LZ4_writeFile_t*)malloc(sizeof(LZ4_writeFile_t));
190
+ if (*lz4fWrite == NULL) {
191
+ return -LZ4F_ERROR_allocation_failed;
192
+ }
193
+ if (prefsPtr != NULL) {
194
+ switch (prefsPtr->frameInfo.blockSizeID) {
195
+ case LZ4F_default :
196
+ case LZ4F_max64KB :
197
+ (*lz4fWrite)->maxWriteSize = 64 * 1024;
198
+ break;
199
+ case LZ4F_max256KB:
200
+ (*lz4fWrite)->maxWriteSize = 256 * 1024;
201
+ break;
202
+ case LZ4F_max1MB:
203
+ (*lz4fWrite)->maxWriteSize = 1 * 1024 * 1024;
204
+ break;
205
+ case LZ4F_max4MB:
206
+ (*lz4fWrite)->maxWriteSize = 4 * 1024 * 1024;
207
+ break;
208
+ default:
209
+ free(lz4fWrite);
210
+ return -LZ4F_ERROR_maxBlockSize_invalid;
211
+ }
212
+ } else {
213
+ (*lz4fWrite)->maxWriteSize = 64 * 1024;
214
+ }
215
+
216
+ (*lz4fWrite)->dstBufMaxSize = LZ4F_compressBound((*lz4fWrite)->maxWriteSize, prefsPtr);
217
+ (*lz4fWrite)->dstBuf = (LZ4_byte*)malloc((*lz4fWrite)->dstBufMaxSize);
218
+ if ((*lz4fWrite)->dstBuf == NULL) {
219
+ free(*lz4fWrite);
220
+ return -LZ4F_ERROR_allocation_failed;
221
+ }
222
+
223
+ ret = LZ4F_createCompressionContext(&(*lz4fWrite)->cctxPtr, LZ4F_getVersion());
224
+ if (LZ4F_isError(ret)) {
225
+ free((*lz4fWrite)->dstBuf);
226
+ free(*lz4fWrite);
227
+ return ret;
228
+ }
229
+
230
+ ret = LZ4F_compressBegin((*lz4fWrite)->cctxPtr, buf, LZ4F_HEADER_SIZE_MAX, prefsPtr);
231
+ if (LZ4F_isError(ret)) {
232
+ LZ4F_freeCompressionContext((*lz4fWrite)->cctxPtr);
233
+ free((*lz4fWrite)->dstBuf);
234
+ free(*lz4fWrite);
235
+ return ret;
236
+ }
237
+
238
+ if (ret != fwrite(buf, 1, ret, fp)) {
239
+ LZ4F_freeCompressionContext((*lz4fWrite)->cctxPtr);
240
+ free((*lz4fWrite)->dstBuf);
241
+ free(*lz4fWrite);
242
+ return -LZ4F_ERROR_GENERIC;
243
+ }
244
+
245
+ (*lz4fWrite)->fp = fp;
246
+ (*lz4fWrite)->errCode = LZ4F_OK_NoError;
247
+ return LZ4F_OK_NoError;
248
+ }
249
+
250
+ size_t LZ4F_write(LZ4_writeFile_t* lz4fWrite, void* buf, size_t size)
251
+ {
252
+ LZ4_byte* p = (LZ4_byte*)buf;
253
+ size_t remain = size;
254
+ size_t chunk;
255
+ size_t ret;
256
+
257
+ if (lz4fWrite == NULL || buf == NULL)
258
+ return -LZ4F_ERROR_GENERIC;
259
+ while (remain) {
260
+ if (remain > lz4fWrite->maxWriteSize)
261
+ chunk = lz4fWrite->maxWriteSize;
262
+ else
263
+ chunk = remain;
264
+
265
+ ret = LZ4F_compressUpdate(lz4fWrite->cctxPtr,
266
+ lz4fWrite->dstBuf, lz4fWrite->dstBufMaxSize,
267
+ p, chunk,
268
+ NULL);
269
+ if (LZ4F_isError(ret)) {
270
+ lz4fWrite->errCode = ret;
271
+ return ret;
272
+ }
273
+
274
+ if(ret != fwrite(lz4fWrite->dstBuf, 1, ret, lz4fWrite->fp)) {
275
+ lz4fWrite->errCode = -LZ4F_ERROR_GENERIC;
276
+ return -LZ4F_ERROR_GENERIC;
277
+ }
278
+
279
+ p += chunk;
280
+ remain -= chunk;
281
+ }
282
+
283
+ return size;
284
+ }
285
+
286
+ LZ4F_errorCode_t LZ4F_writeClose(LZ4_writeFile_t* lz4fWrite)
287
+ {
288
+ LZ4F_errorCode_t ret = LZ4F_OK_NoError;
289
+
290
+ if (lz4fWrite == NULL)
291
+ return -LZ4F_ERROR_GENERIC;
292
+
293
+ if (lz4fWrite->errCode == LZ4F_OK_NoError) {
294
+ ret = LZ4F_compressEnd(lz4fWrite->cctxPtr,
295
+ lz4fWrite->dstBuf, lz4fWrite->dstBufMaxSize,
296
+ NULL);
297
+ if (LZ4F_isError(ret)) {
298
+ goto out;
299
+ }
300
+
301
+ if (ret != fwrite(lz4fWrite->dstBuf, 1, ret, lz4fWrite->fp)) {
302
+ ret = -LZ4F_ERROR_GENERIC;
303
+ }
304
+ }
305
+
306
+ out:
307
+ LZ4F_freeCompressionContext(lz4fWrite->cctxPtr);
308
+ free(lz4fWrite->dstBuf);
309
+ free(lz4fWrite);
310
+ return ret;
311
+ }
@@ -0,0 +1,93 @@
1
+ /*
2
+ LZ4 file library
3
+ Header File
4
+ Copyright (C) 2022, Xiaomi Inc.
5
+ BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
6
+
7
+ Redistribution and use in source and binary forms, with or without
8
+ modification, are permitted provided that the following conditions are
9
+ met:
10
+
11
+ * Redistributions of source code must retain the above copyright
12
+ notice, this list of conditions and the following disclaimer.
13
+ * Redistributions in binary form must reproduce the above
14
+ copyright notice, this list of conditions and the following disclaimer
15
+ in the documentation and/or other materials provided with the
16
+ distribution.
17
+
18
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21
+ A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22
+ OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23
+ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24
+ LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25
+ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26
+ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
+
30
+ You can contact the author at :
31
+ - LZ4 source repository : https://github.com/lz4/lz4
32
+ - LZ4 public forum : https://groups.google.com/forum/#!forum/lz4c
33
+ */
34
+ #if defined (__cplusplus)
35
+ extern "C" {
36
+ #endif
37
+
38
+ #ifndef LZ4FILE_H
39
+ #define LZ4FILE_H
40
+
41
+ #include <stdio.h>
42
+ #include "lz4frame_static.h"
43
+
44
+ typedef struct LZ4_readFile_s LZ4_readFile_t;
45
+ typedef struct LZ4_writeFile_s LZ4_writeFile_t;
46
+
47
+ /*! LZ4F_readOpen() :
48
+ * Set read lz4file handle.
49
+ * `lz4f` will set a lz4file handle.
50
+ * `fp` must be the return value of the lz4 file opened by fopen.
51
+ */
52
+ LZ4FLIB_STATIC_API LZ4F_errorCode_t LZ4F_readOpen(LZ4_readFile_t** lz4fRead, FILE* fp);
53
+
54
+ /*! LZ4F_read() :
55
+ * Read lz4file content to buffer.
56
+ * `lz4f` must use LZ4_readOpen to set first.
57
+ * `buf` read data buffer.
58
+ * `size` read data buffer size.
59
+ */
60
+ LZ4FLIB_STATIC_API size_t LZ4F_read(LZ4_readFile_t* lz4fRead, void* buf, size_t size);
61
+
62
+ /*! LZ4F_readClose() :
63
+ * Close lz4file handle.
64
+ * `lz4f` must use LZ4_readOpen to set first.
65
+ */
66
+ LZ4FLIB_STATIC_API LZ4F_errorCode_t LZ4F_readClose(LZ4_readFile_t* lz4fRead);
67
+
68
+ /*! LZ4F_writeOpen() :
69
+ * Set write lz4file handle.
70
+ * `lz4f` will set a lz4file handle.
71
+ * `fp` must be the return value of the lz4 file opened by fopen.
72
+ */
73
+ LZ4FLIB_STATIC_API LZ4F_errorCode_t LZ4F_writeOpen(LZ4_writeFile_t** lz4fWrite, FILE* fp, const LZ4F_preferences_t* prefsPtr);
74
+
75
+ /*! LZ4F_write() :
76
+ * Write buffer to lz4file.
77
+ * `lz4f` must use LZ4F_writeOpen to set first.
78
+ * `buf` write data buffer.
79
+ * `size` write data buffer size.
80
+ */
81
+ LZ4FLIB_STATIC_API size_t LZ4F_write(LZ4_writeFile_t* lz4fWrite, void* buf, size_t size);
82
+
83
+ /*! LZ4F_writeClose() :
84
+ * Close lz4file handle.
85
+ * `lz4f` must use LZ4F_writeOpen to set first.
86
+ */
87
+ LZ4FLIB_STATIC_API LZ4F_errorCode_t LZ4F_writeClose(LZ4_writeFile_t* lz4fWrite);
88
+
89
+ #endif /* LZ4FILE_H */
90
+
91
+ #if defined (__cplusplus)
92
+ }
93
+ #endif