refactored ZBUFF_compressEnd() for better maintainability

This commit is contained in:
Yann Collet 2016-06-21 15:18:11 +02:00
parent 446d442989
commit bda68c253b

View File

@ -162,7 +162,7 @@ size_t ZBUFF_compressInit_advanced(ZBUFF_CCtx* zbc,
zbc->inToCompress = 0; zbc->inToCompress = 0;
zbc->inBuffPos = 0; zbc->inBuffPos = 0;
zbc->inBuffTarget = zbc->blockSize; zbc->inBuffTarget = zbc->blockSize;
zbc->outBuffFlushedSize = 0; zbc->outBuffContentSize = zbc->outBuffFlushedSize = 0;
zbc->stage = ZBUFFcs_load; zbc->stage = ZBUFFcs_load;
return 0; /* ready to go */ return 0; /* ready to go */
} }
@ -243,17 +243,20 @@ static size_t ZBUFF_compressContinue_generic(ZBUFF_CCtx* zbc,
} }
case ZBUFFcs_flush: case ZBUFFcs_flush:
case ZBUFFcs_final: /* flush into dst */
{ size_t const toFlush = zbc->outBuffContentSize - zbc->outBuffFlushedSize; { size_t const toFlush = zbc->outBuffContentSize - zbc->outBuffFlushedSize;
size_t const flushed = ZBUFF_limitCopy(op, oend-op, zbc->outBuff + zbc->outBuffFlushedSize, toFlush); size_t const flushed = ZBUFF_limitCopy(op, oend-op, zbc->outBuff + zbc->outBuffFlushedSize, toFlush);
op += flushed; op += flushed;
zbc->outBuffFlushedSize += flushed; zbc->outBuffFlushedSize += flushed;
if (toFlush!=flushed) { notDone = 0; break; } /* dst too small to store flushed data : stop there */ if (toFlush!=flushed) { notDone = 0; break; } /* dst too small to store flushed data : stop there */
zbc->outBuffContentSize = zbc->outBuffFlushedSize = 0; zbc->outBuffContentSize = zbc->outBuffFlushedSize = 0;
if (zbc->stage==ZBUFFcs_flush) { zbc->stage = ZBUFFcs_load; break; } zbc->stage = ZBUFFcs_load;
notDone=0;
break; break;
} }
case ZBUFFcs_final:
notDone = 0; /* do nothing */
break;
default: default:
return ERROR(GENERIC); /* impossible */ return ERROR(GENERIC); /* impossible */
} }
@ -292,26 +295,28 @@ size_t ZBUFF_compressEnd(ZBUFF_CCtx* zbc, void* dst, size_t* dstCapacityPtr)
BYTE* const oend = ostart + *dstCapacityPtr; BYTE* const oend = ostart + *dstCapacityPtr;
BYTE* op = ostart; BYTE* op = ostart;
{ size_t outSize = *dstCapacityPtr; if (zbc->stage != ZBUFFcs_final) {
/* flush whatever remains */
size_t outSize = *dstCapacityPtr;
size_t const remainingToFlush = ZBUFF_compressFlush(zbc, dst, &outSize); size_t const remainingToFlush = ZBUFF_compressFlush(zbc, dst, &outSize);
op += outSize; op += outSize;
if (remainingToFlush) { if (remainingToFlush) {
*dstCapacityPtr = op-ostart; *dstCapacityPtr = op-ostart;
return remainingToFlush + (ZSTD_BLOCKHEADERSIZE * (zbc->stage != ZBUFFcs_final)); return remainingToFlush + ZSTD_BLOCKHEADERSIZE;
} } }
/* create epilogue */
if (zbc->stage == ZBUFFcs_final) { zbc->stage = ZBUFFcs_init; *dstCapacityPtr = op-ostart; return 0; } zbc->stage = ZBUFFcs_final;
/* outBuff is flushed */
{ size_t outSize = oend-op;
size_t remainingToFlush;
zbc->outBuffContentSize = ZSTD_compressEnd(zbc->zc, zbc->outBuff, zbc->outBuffSize); /* epilogue into outBuff */ zbc->outBuffContentSize = ZSTD_compressEnd(zbc->zc, zbc->outBuff, zbc->outBuffSize); /* epilogue into outBuff */
zbc->stage = ZBUFFcs_flush; }
remainingToFlush = ZBUFF_compressFlush(zbc, op, &outSize);
op += outSize; /* flush epilogue */
zbc->stage = remainingToFlush ? ZBUFFcs_final : ZBUFFcs_init; /* close only if nothing left to flush */ { size_t const toFlush = zbc->outBuffContentSize - zbc->outBuffFlushedSize;
*dstCapacityPtr = op-ostart; /* how many bytes were written */ size_t const flushed = ZBUFF_limitCopy(op, oend-op, zbc->outBuff + zbc->outBuffFlushedSize, toFlush);
return remainingToFlush; op += flushed;
zbc->outBuffFlushedSize += flushed;
*dstCapacityPtr = op-ostart;
if (toFlush==flushed) zbc->stage = ZBUFFcs_init; /* end reached */
return toFlush - flushed;
} }
} }