Home | History | Annotate | Download | only in lib

Lines Matching defs:dctx

1021     LZ4F_dctx* const dctx = (LZ4F_dctx*)ALLOC_AND_ZERO(sizeof(LZ4F_dctx));
1022 if (dctx==NULL) return err0r(LZ4F_ERROR_GENERIC);
1024 dctx->version = versionNumber;
1025 *LZ4F_decompressionContextPtr = dctx;
1029 LZ4F_errorCode_t LZ4F_freeDecompressionContext(LZ4F_dctx* dctx)
1032 if (dctx != NULL) { /* can accept NULL input, like free() */
1033 result = (LZ4F_errorCode_t)dctx->dStage;
1034 FREEMEM(dctx->tmpIn);
1035 FREEMEM(dctx->tmpOutBuffer);
1036 FREEMEM(dctx);
1044 void LZ4F_resetDecompressionContext(LZ4F_dctx* dctx)
1046 dctx->dStage = dstage_getFrameHeader;
1047 dctx->dict = NULL;
1048 dctx->dictSize = 0;
1079 * output : set internal values of dctx, such as
1080 * dctx->frameInfo and dctx->dStage.
1085 static size_t LZ4F_decodeHeader(LZ4F_dctx* dctx, const void* src, size_t srcSize)
1093 MEM_INIT(&(dctx->frameInfo), 0, sizeof(dctx->frameInfo));
1097 dctx->frameInfo.frameType = LZ4F_skippableFrame;
1098 if (src == (void*)(dctx->header)) {
1099 dctx->tmpInSize = srcSize;
1100 dctx->tmpInTarget = 8;
1101 dctx->dStage = dstage_storeSFrameSize;
1104 dctx->dStage = dstage_getSFrameSize;
1112 dctx->frameInfo.frameType = LZ4F_frame;
1132 if (srcPtr != dctx->header)
1133 memcpy(dctx->header, srcPtr, srcSize);
1134 dctx->tmpInSize = srcSize;
1135 dctx->tmpInTarget = frameHeaderSize;
1136 dctx->dStage = dstage_storeFrameHeader;
1155 dctx->frameInfo.blockMode = (LZ4F_blockMode_t)blockMode;
1156 dctx->frameInfo.blockChecksumFlag = (LZ4F_blockChecksum_t)blockChecksumFlag;
1157 dctx->frameInfo.contentChecksumFlag = (LZ4F_contentChecksum_t)contentChecksumFlag;
1158 dctx->frameInfo.blockSizeID = (LZ4F_blockSizeID_t)blockSizeID;
1159 dctx->maxBlockSize = LZ4F_getBlockSize(blockSizeID);
1161 dctx->frameRemainingSize =
1162 dctx->frameInfo.contentSize = LZ4F_readLE64(srcPtr+6);
1164 dctx->frameInfo.dictID = LZ4F_readLE32(srcPtr + frameHeaderSize - 5);
1166 dctx->dStage = dstage_init;
1179 * - After decoding has been started. In which case, no input is read, frame parameters are extracted from dctx.
1184 * note 1 : in case of error, dctx is not modified. Decoding operations can resume from where they stopped.
1187 LZ4F_errorCode_t LZ4F_getFrameInfo(LZ4F_dctx* dctx, LZ4F_frameInfo_t* frameInfoPtr,
1190 if (dctx->dStage > dstage_storeFrameHeader) { /* assumption : dstage_* header enum at beginning of range */
1194 *frameInfoPtr = dctx->frameInfo;
1196 return LZ4F_decompress(dctx, NULL, &o, NULL, &i, NULL);
1198 if (dctx->dStage == dstage_storeFrameHeader) {
1211 decodeResult = LZ4F_decodeHeader(dctx, srcBuffer, hSize);
1218 *frameInfoPtr = dctx->frameInfo;
1226 static void LZ4F_updateDict(LZ4F_dctx* dctx,
1230 if (dctx->dictSize==0)
1231 dctx
1233 if (dctx->dict + dctx->dictSize == dstPtr) { /* dictionary continuity, directly within dstBuffer */
1234 dctx->dictSize += dstSize;
1239 dctx->dict = (const BYTE*)dstBufferStart;
1240 dctx->dictSize = dstPtr - dstBufferStart + dstSize;
1248 if ((withinTmp) && (dctx->dict == dctx->tmpOutBuffer)) { /* continue history within tmpOutBuffer */
1250 assert(dctx->dict + dctx->dictSize == dctx->tmpOut + dctx->tmpOutStart);
1251 dctx->dictSize += dstSize;
1256 size_t const preserveSize = dctx->tmpOut - dctx->tmpOutBuffer;
1257 size_t copySize = 64 KB - dctx->tmpOutSize;
1258 const BYTE* const oldDictEnd = dctx->dict + dctx->dictSize - dctx->tmpOutStart;
1259 if (dctx->tmpOutSize > 64 KB) copySize = 0;
1262 memcpy(dctx->tmpOutBuffer + preserveSize - copySize, oldDictEnd - copySize, copySize);
1264 dctx->dict = dctx->tmpOutBuffer;
1265 dctx->dictSize = preserveSize + dctx->tmpOutStart + dstSize;
1269 if (dctx->dict == dctx->tmpOutBuffer) { /* copy dst into tmp to complete dict */
1270 if (dctx->dictSize + dstSize > dctx->maxBufferSize) { /* tmp buffer not large enough */
1272 memcpy(dctx->tmpOutBuffer, dctx->dict + dctx->dictSize - preserveSize, preserveSize);
1273 dctx->dictSize = preserveSize;
1275 memcpy(dctx->tmpOutBuffer + dctx->dictSize, dstPtr, dstSize);
1276 dctx->dictSize += dstSize;
1282 if (preserveSize > dctx->dictSize) preserveSize = dctx->dictSize;
1283 memcpy(dctx->tmpOutBuffer, dctx->dict + dctx->dictSize - preserveSize, preserveSize);
1284 memcpy(dctx->tmpOutBuffer + preserveSize, dstPtr, dstSize);
1285 dctx->dict = dctx->tmpOutBuffer;
1286 dctx->dictSize = preserveSize + dstSize;
1310 size_t LZ4F_decompress(LZ4F_dctx* dctx,
1336 switch(dctx->dStage)
1341 size_t const hSize = LZ4F_decodeHeader(dctx, srcPtr, srcEnd-srcPtr); /* will update dStage appropriately */
1346 dctx->tmpInSize = 0;
1348 dctx->tmpInTarget = minFHSize; /* minimum size to decode header */
1349 dctx->dStage = dstage_storeFrameHeader;
1353 { size_t const sizeToCopy = MIN(dctx->tmpInTarget - dctx->tmpInSize, (size_t)(srcEnd - srcPtr));
1354 memcpy(dctx->header + dctx->tmpInSize, srcPtr, sizeToCopy);
1355 dctx->tmpInSize += sizeToCopy;
1358 if (dctx->tmpInSize < dctx->tmpInTarget) {
1359 nextSrcSizeHint = (dctx->tmpInTarget - dctx->tmpInSize) + BHSize; /* rest of header + nextBlockHeader */
1363 { size_t const hSize = LZ4F_decodeHeader(dctx, dctx->header, dctx->tmpInTarget); /* will update dStage appropriately */
1369 if (dctx->frameInfo.contentChecksumFlag) XXH32_reset(&(dctx->xxh), 0);
1371 { size_t const bufferNeeded = dctx->maxBlockSize
1372 + ((dctx->frameInfo.blockMode==LZ4F_blockLinked) * 128 KB);
1373 if (bufferNeeded > dctx->maxBufferSize) { /* tmp buffers too small */
1374 dctx->maxBufferSize = 0; /* ensure allocation will be re-attempted on next entry*/
1375 FREEMEM(dctx->tmpIn);
1376 dctx->tmpIn = (BYTE*)ALLOC(dctx->maxBlockSize + 4 /* block checksum */);
1377 if (dctx->tmpIn == NULL)
1379 FREEMEM(dctx->tmpOutBuffer);
1380 dctx->tmpOutBuffer= (BYTE*)ALLOC(bufferNeeded);
1381 if (dctx->tmpOutBuffer== NULL)
1383 dctx->maxBufferSize = bufferNeeded;
1385 dctx->tmpInSize = 0;
1386 dctx->tmpInTarget = 0;
1387 dctx->tmpOut = dctx->tmpOutBuffer;
1388 dctx->tmpOutStart = 0;
1389 dctx->tmpOutSize = 0;
1391 dctx->dStage = dstage_getBlockHeader;
1400 dctx->tmpInSize = 0;
1401 dctx->dStage = dstage_storeBlockHeader;
1404 if (dctx->dStage == dstage_storeBlockHeader) /* can be skipped */
1407 size_t const wantedData = BHSize - dctx->tmpInSize;
1409 memcpy(dctx->tmpIn + dctx->tmpInSize, srcPtr, sizeToCopy);
1411 dctx->tmpInSize += sizeToCopy;
1413 if (dctx->tmpInSize < BHSize) { /* not enough input for cBlockSize */
1414 nextSrcSizeHint = BHSize - dctx->tmpInSize;
1418 selectedIn = dctx->tmpIn;
1419 } /* if (dctx->dStage == dstage_storeBlockHeader) */
1423 size_t const crcSize = dctx->frameInfo.blockChecksumFlag * 4;
1425 dctx->dStage = dstage_getSuffix;
1428 if (nextCBlockSize > dctx->maxBlockSize)
1432 dctx->tmpInTarget = nextCBlockSize;
1433 if (dctx->frameInfo.blockChecksumFlag) {
1434 XXH32_reset(&dctx->blockChecksum, 0);
1436 dctx->dStage = dstage_copyDirect;
1440 dctx->tmpInTarget = nextCBlockSize + crcSize;
1441 dctx->dStage = dstage_getCBlock;
1451 size_t const sizeToCopy = MIN(dctx->tmpInTarget, minBuffSize);
1453 if (dctx->frameInfo.blockChecksumFlag) {
1454 XXH32_update(&dctx->blockChecksum, srcPtr, sizeToCopy);
1456 if (dctx->frameInfo.contentChecksumFlag)
1457 XXH32_update(&dctx->xxh, srcPtr, sizeToCopy);
1458 if (dctx->frameInfo.contentSize)
1459 dctx->frameRemainingSize -= sizeToCopy;
1462 if (dctx->frameInfo.blockMode == LZ4F_blockLinked)
1463 LZ4F_updateDict(dctx, dstPtr, sizeToCopy, dstStart, 0);
1467 if (sizeToCopy == dctx->tmpInTarget) { /* all done */
1468 if (dctx->frameInfo.blockChecksumFlag) {
1469 dctx->tmpInSize = 0;
1470 dctx->dStage = dstage_getBlockChecksum;
1472 dctx->dStage = dstage_getBlockHeader; /* new block */
1475 dctx->tmpInTarget -= sizeToCopy; /* need to copy more */
1476 nextSrcSizeHint = dctx->tmpInTarget +
1477 + dctx->frameInfo.contentChecksumFlag * 4 /* block checksum */
1486 if ((srcEnd-srcPtr >= 4) && (dctx->tmpInSize==0)) {
1490 size_t const stillToCopy = 4 - dctx->tmpInSize;
1492 memcpy(dctx->header + dctx->tmpInSize, srcPtr, sizeToCopy);
1493 dctx->tmpInSize += sizeToCopy;
1495 if (dctx->tmpInSize < 4) { /* all input consumed */
1499 crcSrc = dctx->header;
1502 U32 const calcCRC = XXH32_digest(&dctx->blockChecksum);
1506 dctx->dStage = dstage_getBlockHeader; /* new block */
1510 if ((size_t)(srcEnd-srcPtr) < dctx->tmpInTarget) {
1511 dctx->tmpInSize = 0;
1512 dctx->dStage = dstage_storeCBlock;
1517 srcPtr += dctx->tmpInTarget;
1521 { size_t const wantedData = dctx->tmpInTarget - dctx->tmpInSize;
1524 memcpy(dctx->tmpIn + dctx->tmpInSize, srcPtr, sizeToCopy);
1525 dctx->tmpInSize += sizeToCopy;
1527 if (dctx->tmpInSize < dctx->tmpInTarget) { /* need more input */
1528 nextSrcSizeHint = (dctx->tmpInTarget - dctx->tmpInSize) + BHSize;
1532 selectedIn = dctx->tmpIn;
1536 if (dctx->frameInfo.blockChecksumFlag) {
1537 dctx->tmpInTarget -= 4;
1538 assert(selectedIn != NULL); /* selectedIn is defined at this stage (either srcPtr, or dctx->tmpIn) */
1539 { U32 const readBlockCrc = LZ4F_readLE32(selectedIn + dctx->tmpInTarget);
1540 U32 const calcBlockCrc = XXH32(selectedIn, dctx->tmpInTarget, 0);
1545 if ((size_t)(dstEnd-dstPtr) >= dctx->maxBlockSize) {
1546 const char* dict = (const char*)dctx->dict;
1547 size_t dictSize = dctx->dictSize;
1557 (int)dctx->tmpInTarget, (int)dctx->maxBlockSize,
1560 if (dctx->frameInfo.contentChecksumFlag)
1561 XXH32_update(&(dctx->xxh), dstPtr, decodedSize);
1562 if (dctx->frameInfo.contentSize)
1563 dctx->frameRemainingSize -= decodedSize;
1566 if (dctx->frameInfo.blockMode==LZ4F_blockLinked)
1567 LZ4F_updateDict(dctx, dstPtr, decodedSize, dstStart, 0);
1570 dctx->dStage = dstage_getBlockHeader;
1576 if (dctx->frameInfo.blockMode == LZ4F_blockLinked) {
1577 if (dctx->dict == dctx
1578 if (dctx->dictSize > 128 KB) {
1579 memcpy(dctx->tmpOutBuffer, dctx->dict + dctx->dictSize - 64 KB, 64 KB);
1580 dctx->dictSize = 64 KB;
1582 dctx->tmpOut = dctx->tmpOutBuffer + dctx->dictSize;
1584 size_t const reservedDictSpace = MIN(dctx->dictSize, 64 KB);
1585 dctx->tmpOut = dctx->tmpOutBuffer + reservedDictSpace;
1589 { const char* dict = (const char*)dctx->dict;
1590 size_t dictSize = dctx->dictSize;
1598 (const char*)selectedIn, (char*)dctx->tmpOut,
1599 (int)dctx->tmpInTarget, (int)dctx->maxBlockSize,
1603 if (dctx->frameInfo.contentChecksumFlag)
1604 XXH32_update(&(dctx->xxh), dctx->tmpOut, decodedSize);
1605 if (dctx->frameInfo.contentSize)
1606 dctx->frameRemainingSize -= decodedSize;
1607 dctx->tmpOutSize = decodedSize;
1608 dctx->tmpOutStart = 0;
1609 dctx->dStage = dstage_flushOut;
1614 { size_t const sizeToCopy = MIN(dctx->tmpOutSize - dctx->tmpOutStart, (size_t)(dstEnd-dstPtr));
1615 memcpy(dstPtr, dctx->tmpOut + dctx->tmpOutStart, sizeToCopy);
1618 if (dctx->frameInfo.blockMode == LZ4F_blockLinked)
1619 LZ4F_updateDict(dctx, dstPtr, sizeToCopy, dstStart, 1 /*withinTmp*/);
1621 dctx->tmpOutStart += sizeToCopy;
1624 if (dctx->tmpOutStart == dctx->tmpOutSize) { /* all flushed */
1625 dctx->dStage = dstage_getBlockHeader; /* get next block */
1635 if (dctx->frameRemainingSize)
1637 if (!dctx->frameInfo.contentChecksumFlag) { /* no checksum, frame is completed */
1639 LZ4F_resetDecompressionContext(dctx);
1644 dctx->tmpInSize = 0;
1645 dctx->dStage = dstage_storeSuffix;
1651 if (dctx->dStage == dstage_storeSuffix) /* can be skipped */
1654 size_t const wantedData = 4 - dctx->tmpInSize;
1656 memcpy(dctx->tmpIn + dctx->tmpInSize, srcPtr, sizeToCopy);
1658 dctx->tmpInSize += sizeToCopy;
1659 if (dctx->tmpInSize < 4) { /* not enough input to read complete suffix */
1660 nextSrcSizeHint = 4 - dctx->tmpInSize;
1664 selectedIn = dctx->tmpIn;
1665 } /* if (dctx->dStage == dstage_storeSuffix) */
1669 U32 const resultCRC = XXH32_digest(&(dctx->xxh));
1673 LZ4F_resetDecompressionContext(dctx);
1684 dctx->tmpInSize = 4;
1685 dctx->tmpInTarget = 8;
1686 dctx->dStage = dstage_storeSFrameSize;
1689 if (dctx->dStage == dstage_storeSFrameSize)
1691 { size_t const sizeToCopy = MIN(dctx->tmpInTarget - dctx->tmpInSize,
1693 memcpy(dctx->header + dctx->tmpInSize, srcPtr, sizeToCopy);
1695 dctx->tmpInSize += sizeToCopy;
1696 if (dctx->tmpInSize < dctx->tmpInTarget) {
1698 nextSrcSizeHint = dctx->tmpInTarget - dctx->tmpInSize;
1702 selectedIn = dctx->header + 4;
1703 } /* if (dctx->dStage == dstage_storeSFrameSize) */
1707 dctx->frameInfo.contentSize = SFrameSize;
1708 dctx->tmpInTarget = SFrameSize;
1709 dctx->dStage = dstage_skipSkippable;
1714 { size_t const skipSize = MIN(dctx->tmpInTarget, (size_t)(srcEnd-srcPtr));
1716 dctx->tmpInTarget -= skipSize;
1718 nextSrcSizeHint = dctx->tmpInTarget;
1721 LZ4F_resetDecompressionContext(dctx);
1724 } /* switch (dctx->dStage) */
1729 if ( (dctx->frameInfo.blockMode==LZ4F_blockLinked) /* next block will use up to 64KB from previous ones */
1730 && (dctx->dict != dctx->tmpOutBuffer) /* dictionary is not already within tmp */
1732 && ((unsigned)(dctx->dStage)-2 < (unsigned)(dstage_getSuffix)-2) ) /* valid stages : [init ... getSuffix[ */
1734 if (dctx->dStage == dstage_flushOut) {
1735 size_t const preserveSize = dctx->tmpOut - dctx->tmpOutBuffer;
1736 size_t copySize = 64 KB - dctx->tmpOutSize;
1737 const BYTE* oldDictEnd = dctx->dict + dctx->dictSize - dctx->tmpOutStart;
1738 if (dctx->tmpOutSize > 64 KB) copySize = 0;
1742 memcpy(dctx->tmpOutBuffer + preserveSize - copySize, oldDictEnd - copySize, copySize);
1744 dctx->dict = dctx->tmpOutBuffer;
1745 dctx->dictSize = preserveSize + dctx->tmpOutStart;
1747 const BYTE* const oldDictEnd = dctx->dict + dctx->dictSize;
1748 size_t const newDictSize = MIN(dctx->dictSize, 64 KB);
1751 memcpy(dctx->tmpOutBuffer, oldDictEnd - newDictSize, newDictSize);
1753 dctx->dict = dctx->tmpOutBuffer;
1754 dctx->dictSize = newDictSize;
1755 dctx->tmpOut = dctx->tmpOutBuffer + newDictSize;
1769 size_t LZ4F_decompress_usingDict(LZ4F_dctx* dctx,
1775 if (dctx->dStage <= dstage_init) {
1776 dctx->dict = (const BYTE*)dict;
1777 dctx->dictSize = dictSize;
1779 return LZ4F_decompress(dctx, dstBuffer, dstSizePtr,