Fix streaming compression/decompression examples
* Handle compression of empty file * Error in decompression in case of trailing data
This commit is contained in:
parent
c9381c16be
commit
a8219903cf
@ -44,8 +44,8 @@ static void compressFile_orDie(const char* fname, const char* outName, int cLeve
|
|||||||
* and writes all output produced to the output file.
|
* and writes all output produced to the output file.
|
||||||
*/
|
*/
|
||||||
size_t const toRead = buffInSize;
|
size_t const toRead = buffInSize;
|
||||||
size_t read;
|
for (;;) {
|
||||||
while ((read = fread_orDie(buffIn, toRead, fin))) {
|
size_t read = fread_orDie(buffIn, toRead, fin);
|
||||||
/* Select the flush mode.
|
/* Select the flush mode.
|
||||||
* If the read may not be finished (read == toRead) we use
|
* If the read may not be finished (read == toRead) we use
|
||||||
* ZSTD_e_continue. If this is the last chunk, we use ZSTD_e_end.
|
* ZSTD_e_continue. If this is the last chunk, we use ZSTD_e_end.
|
||||||
@ -76,6 +76,10 @@ static void compressFile_orDie(const char* fname, const char* outName, int cLeve
|
|||||||
} while (!finished);
|
} while (!finished);
|
||||||
CHECK(input.pos == input.size,
|
CHECK(input.pos == input.size,
|
||||||
"Impossible: zstd only returns 0 when the input is completely consumed!");
|
"Impossible: zstd only returns 0 when the input is completely consumed!");
|
||||||
|
|
||||||
|
if (lastChunk) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ZSTD_freeCCtx(cctx);
|
ZSTD_freeCCtx(cctx);
|
||||||
|
@ -34,7 +34,10 @@ static void decompressFile_orDie(const char* fname)
|
|||||||
*/
|
*/
|
||||||
size_t const toRead = buffInSize;
|
size_t const toRead = buffInSize;
|
||||||
size_t read;
|
size_t read;
|
||||||
|
size_t lastRet = 0;
|
||||||
|
int isEmpty = 1;
|
||||||
while ( (read = fread_orDie(buffIn, toRead, fin)) ) {
|
while ( (read = fread_orDie(buffIn, toRead, fin)) ) {
|
||||||
|
isEmpty = 0;
|
||||||
ZSTD_inBuffer input = { buffIn, read, 0 };
|
ZSTD_inBuffer input = { buffIn, read, 0 };
|
||||||
/* Given a valid frame, zstd won't consume the last byte of the frame
|
/* Given a valid frame, zstd won't consume the last byte of the frame
|
||||||
* until it has flushed all of the decompressed data of the frame.
|
* until it has flushed all of the decompressed data of the frame.
|
||||||
@ -53,9 +56,24 @@ static void decompressFile_orDie(const char* fname)
|
|||||||
size_t const ret = ZSTD_decompressStream(dctx, &output , &input);
|
size_t const ret = ZSTD_decompressStream(dctx, &output , &input);
|
||||||
CHECK_ZSTD(ret);
|
CHECK_ZSTD(ret);
|
||||||
fwrite_orDie(buffOut, output.pos, fout);
|
fwrite_orDie(buffOut, output.pos, fout);
|
||||||
|
lastRet = ret;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (isEmpty) {
|
||||||
|
fprintf(stderr, "input is empty\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (lastRet != 0) {
|
||||||
|
/* The last return value from ZSTD_decompressStream did not end on a
|
||||||
|
* frame, but we reached the end of the file! We assume this is an
|
||||||
|
* error, and the input was truncated.
|
||||||
|
*/
|
||||||
|
fprintf(stderr, "EOF before end of stream: %zu\n", lastRet);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
ZSTD_freeDCtx(dctx);
|
ZSTD_freeDCtx(dctx);
|
||||||
fclose_orDie(fin);
|
fclose_orDie(fin);
|
||||||
fclose_orDie(fout);
|
fclose_orDie(fout);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user