summaryrefslogtreecommitdiffstats
path: root/examples/frameCompress.c
diff options
context:
space:
mode:
authorYann Collet <Cyan4973@users.noreply.github.com>2018-09-11 19:52:06 (GMT)
committerGitHub <noreply@github.com>2018-09-11 19:52:06 (GMT)
commit641b453d9db536ee020851bfcb1dc39f61006f0a (patch)
tree0a0c78c393d0732bf85464275f5053ad96ff4eab /examples/frameCompress.c
parentb3692db46d2b23a7c0af2d5e69988c94f126e10a (diff)
parent08d347b5b217b011ff7487130b79480d8cfdaeb8 (diff)
downloadlz4-1.8.3.zip
lz4-1.8.3.tar.gz
lz4-1.8.3.tar.bz2
Merge pull request #572 from lz4/devv1.8.3
v1.8.3
Diffstat (limited to 'examples/frameCompress.c')
-rw-r--r--examples/frameCompress.c38
1 files changed, 25 insertions, 13 deletions
diff --git a/examples/frameCompress.c b/examples/frameCompress.c
index 9bfea48..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.
*/
@@ -260,7 +272,7 @@ decompress_file_allocDst(FILE* f_in, FILE* f_out,
int const decompressionResult = decompress_file_internal(
f_in, f_out,
dctx,
- src, srcCapacity, readSize, consumedSize,
+ src, srcCapacity, readSize-consumedSize, consumedSize,
dst, dstCapacity);
free(dst);
@@ -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));
} }