summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYann Collet <Cyan4973@users.noreply.github.com>2018-09-10 20:11:56 (GMT)
committerGitHub <noreply@github.com>2018-09-10 20:11:56 (GMT)
commitf8519d454e1d129ac85fe8e690304b5555db8100 (patch)
tree3f1bd0f3145d0618c9b852d5fc822b55e6c07ffe
parent07dda6cca11b51a4c8f119327849c8f4a3c3b358 (diff)
parent26c42d7ad1c394538adb76879ec8d209b3ff5c84 (diff)
downloadlz4-f8519d454e1d129ac85fe8e690304b5555db8100.zip
lz4-f8519d454e1d129ac85fe8e690304b5555db8100.tar.gz
lz4-f8519d454e1d129ac85fe8e690304b5555db8100.tar.bz2
Merge pull request #565 from lz4/lz4f_init
Introduced constants LZ4F_INIT_*
-rw-r--r--examples/frameCompress.c36
-rw-r--r--lib/lz4frame.h29
2 files changed, 43 insertions, 22 deletions
diff --git a/examples/frameCompress.c b/examples/frameCompress.c
index 0f8910d..a0c5d3d 100644
--- a/examples/frameCompress.c
+++ b/examples/frameCompress.c
@@ -31,12 +31,13 @@ static const LZ4F_preferences_t kPrefs = {
static void safe_fwrite(void* buf, size_t eltSize, size_t nbElt, FILE* f)
{
size_t const writtenSize = fwrite(buf, eltSize, nbElt, f);
- size_t const expectedSize = eltSize * nbElt; /* note : should check for overflow */
+ size_t const expectedSize = eltSize * nbElt;
+ assert(expectedSize / nbElt == eltSize); /* check overflow */
if (writtenSize < expectedSize) {
if (ferror(f)) /* note : ferror() must follow fwrite */
- printf("Write failed\n");
+ fprintf(stderr, "Write failed \n");
else
- printf("Short write\n");
+ fprintf(stderr, "Short write \n");
exit(1);
}
}
@@ -54,9 +55,9 @@ typedef struct {
static compressResult_t
compress_file_internal(FILE* f_in, FILE* f_out,
- LZ4F_compressionContext_t ctx,
- void* inBuff, size_t inChunkSize,
- void* outBuff, size_t outCapacity)
+ LZ4F_compressionContext_t ctx,
+ void* inBuff, size_t inChunkSize,
+ void* outBuff, size_t outCapacity)
{
compressResult_t result = { 1, 0, 0 }; /* result for an error */
unsigned long long count_in = 0, count_out;
@@ -167,9 +168,9 @@ static size_t get_block_size(const LZ4F_frameInfo_t* info) {
/* @return : 1==error, 0==success */
static int
decompress_file_internal(FILE* f_in, FILE* f_out,
- LZ4F_dctx* dctx,
- void* src, size_t srcCapacity, size_t filled, size_t alreadyConsumed,
- void* dst, size_t dstCapacity)
+ LZ4F_dctx* dctx,
+ void* src, size_t srcCapacity, size_t filled, size_t alreadyConsumed,
+ void* dst, size_t dstCapacity)
{
int firstChunk = 1;
size_t ret = 1;
@@ -194,7 +195,7 @@ decompress_file_internal(FILE* f_in, FILE* f_out,
* Continue while there is more input to read (srcPtr != srcEnd)
* and the frame isn't over (ret != 0)
*/
- while (srcPtr != srcEnd && ret != 0) {
+ while (srcPtr < srcEnd && ret != 0) {
/* Any data within dst has been flushed at this stage */
size_t dstSize = dstCapacity;
size_t srcSize = srcEnd - srcPtr;
@@ -208,9 +209,20 @@ decompress_file_internal(FILE* f_in, FILE* f_out,
/* Update input */
srcPtr += srcSize;
}
+
+ assert(srcPtr <= srcEnd);
+
+ /* Ensure all input data has been consumed.
+ * It is valid to have multiple frames in the same file,
+ * but this example only supports one frame.
+ */
+ if (srcPtr < srcEnd) {
+ printf("Decompress: Trailing data left in file after frame\n");
+ return 1;
+ }
}
- /* Check that there isn't trailing input data after the frame.
+ /* Check that there isn't trailing data in the file after the frame.
* It is valid to have multiple frames in the same file,
* but this example only supports one frame.
*/
@@ -278,7 +290,7 @@ static int decompress_file(FILE* f_in, FILE* f_out)
if (!src) { perror("decompress_file(src)"); return 1; }
LZ4F_dctx* dctx;
- { size_t const dctxStatus = LZ4F_createDecompressionContext(&dctx, 100);
+ { size_t const dctxStatus = LZ4F_createDecompressionContext(&dctx, LZ4F_VERSION);
if (LZ4F_isError(dctxStatus)) {
printf("LZ4F_dctx creation error: %s\n", LZ4F_getErrorName(dctxStatus));
} }
diff --git a/lib/lz4frame.h b/lib/lz4frame.h
index fb434ff..75f1fd9 100644
--- a/lib/lz4frame.h
+++ b/lib/lz4frame.h
@@ -33,9 +33,10 @@
*/
/* LZ4F is a stand-alone API to create LZ4-compressed frames
- * conformant with specification v1.5.1.
+ * conformant with specification v1.6.1.
* It also offers streaming capabilities.
- * lz4.h is not required when using lz4frame.h.
+ * lz4.h is not required when using lz4frame.h,
+ * except to get constant such as LZ4_VERSION_NUMBER.
* */
#ifndef LZ4F_H_09782039843
@@ -159,8 +160,9 @@ typedef LZ4F_contentChecksum_t contentChecksum_t;
/*! LZ4F_frameInfo_t :
* makes it possible to set or read frame parameters.
- * It's not required to set all fields, as long as the structure was initially memset() to zero.
- * For all fields, 0 sets it to default value */
+ * Structure must be first init to 0, using memset() or LZ4F_INIT_FRAMEINFO,
+ * setting all parameters to default.
+ * It's then possible to update selectively some parameters */
typedef struct {
LZ4F_blockSizeID_t blockSizeID; /* max64KB, max256KB, max1MB, max4MB; 0 == default */
LZ4F_blockMode_t blockMode; /* LZ4F_blockLinked, LZ4F_blockIndependent; 0 == default */
@@ -171,24 +173,30 @@ typedef struct {
LZ4F_blockChecksum_t blockChecksumFlag; /* 1: each block followed by a checksum of block's compressed data; 0: disabled (default) */
} LZ4F_frameInfo_t;
+#define LZ4F_INIT_FRAMEINFO { 0, 0, 0, 0, 0, 0, 0 } /* v1.8.3+ */
+
/*! LZ4F_preferences_t :
- * makes it possible to supply detailed compression parameters to the stream interface.
- * Structure is presumed initially memset() to zero, representing default settings.
+ * makes it possible to supply advanced compression instructions to streaming interface.
+ * Structure must be first init to 0, using memset() or LZ4F_INIT_PREFERENCES,
+ * setting all parameters to default.
* All reserved fields must be set to zero. */
typedef struct {
LZ4F_frameInfo_t frameInfo;
int compressionLevel; /* 0: default (fast mode); values > LZ4HC_CLEVEL_MAX count as LZ4HC_CLEVEL_MAX; values < 0 trigger "fast acceleration" */
- unsigned autoFlush; /* 1: always flush, to reduce usage of internal buffers */
- unsigned favorDecSpeed; /* 1: parser favors decompression speed vs compression ratio. Only works for high compression modes (>= LZ4LZ4HC_CLEVEL_OPT_MIN) */ /* >= v1.8.2 */
+ unsigned autoFlush; /* 1: always flush; reduces usage of internal buffers */
+ unsigned favorDecSpeed; /* 1: parser favors decompression speed vs compression ratio. Only works for high compression modes (>= LZ4HC_CLEVEL_OPT_MIN) */ /* v1.8.2+ */
unsigned reserved[3]; /* must be zero for forward compatibility */
} LZ4F_preferences_t;
-LZ4FLIB_API int LZ4F_compressionLevel_max(void);
+#define LZ4F_INIT_PREFERENCES { LZ4F_INIT_FRAMEINFO, 0, 0, 0, { 0, 0, 0 } } /* v1.8.3+ */
/*-*********************************
* Simple compression function
***********************************/
+
+LZ4FLIB_API int LZ4F_compressionLevel_max(void);
+
/*! LZ4F_compressFrameBound() :
* Returns the maximum possible compressed size with LZ4F_compressFrame() given srcSize and preferences.
* `preferencesPtr` is optional. It can be replaced by NULL, in which case, the function will assume default preferences.
@@ -222,8 +230,9 @@ typedef struct {
/*--- Resource Management ---*/
-#define LZ4F_VERSION 100
+#define LZ4F_VERSION 100 /* This number can be used to check for an incompatible API breaking change */
LZ4FLIB_API unsigned LZ4F_getVersion(void);
+
/*! LZ4F_createCompressionContext() :
* The first thing to do is to create a compressionContext object, which will be used in all compression operations.
* This is achieved using LZ4F_createCompressionContext(), which takes as argument a version.