Lines Matching refs:dctx
1232 LZ4F_dctx* const dctx = (LZ4F_dctx*)LZ4F_calloc(sizeof(LZ4F_dctx), customMem);
1233 if (dctx == NULL) return NULL;
1235 dctx->cmem = customMem;
1236 dctx->version = version;
1237 return dctx;
1259 LZ4F_errorCode_t LZ4F_freeDecompressionContext(LZ4F_dctx* dctx)
1262 if (dctx != NULL) { /* can accept NULL input, like free() */
1263 result = (LZ4F_errorCode_t)dctx->dStage;
1264 LZ4F_free(dctx->tmpIn, dctx->cmem);
1265 LZ4F_free(dctx->tmpOutBuffer, dctx->cmem);
1266 LZ4F_free(dctx, dctx->cmem);
1274 void LZ4F_resetDecompressionContext(LZ4F_dctx* dctx)
1276 dctx->dStage = dstage_getFrameHeader;
1277 dctx->dict = NULL;
1278 dctx->dictSize = 0;
1279 dctx->skipChecksum = 0;
1285 * output : set internal values of dctx, such as
1286 * dctx->frameInfo and dctx->dStage.
1291 static size_t LZ4F_decodeHeader(LZ4F_dctx* dctx, const void* src, size_t srcSize)
1300 MEM_INIT(&(dctx->frameInfo), 0, sizeof(dctx->frameInfo));
1304 dctx->frameInfo.frameType = LZ4F_skippableFrame;
1305 if (src == (void*)(dctx->header)) {
1306 dctx->tmpInSize = srcSize;
1307 dctx->tmpInTarget = 8;
1308 dctx->dStage = dstage_storeSFrameSize;
1311 dctx->dStage = dstage_getSFrameSize;
1322 dctx->frameInfo.frameType = LZ4F_frame;
1342 if (srcPtr != dctx->header)
1343 memcpy(dctx->header, srcPtr, srcSize);
1344 dctx->tmpInSize = srcSize;
1345 dctx->tmpInTarget = frameHeaderSize;
1346 dctx->dStage = dstage_storeFrameHeader;
1367 dctx->frameInfo.blockMode = (LZ4F_blockMode_t)blockMode;
1368 dctx->frameInfo.blockChecksumFlag = (LZ4F_blockChecksum_t)blockChecksumFlag;
1369 dctx->frameInfo.contentChecksumFlag = (LZ4F_contentChecksum_t)contentChecksumFlag;
1370 dctx->frameInfo.blockSizeID = (LZ4F_blockSizeID_t)blockSizeID;
1371 dctx->maxBlockSize = LZ4F_getBlockSize((LZ4F_blockSizeID_t)blockSizeID);
1373 dctx->frameRemainingSize = dctx->frameInfo.contentSize = LZ4F_readLE64(srcPtr+6);
1375 dctx->frameInfo.dictID = LZ4F_readLE32(srcPtr + frameHeaderSize - 5);
1377 dctx->dStage = dstage_init;
1420 * - After decoding has been started. In which case, no input is read, frame parameters are extracted from dctx.
1425 * note 1 : in case of error, dctx is not modified. Decoding operations can resume from where they stopped.
1428 LZ4F_errorCode_t LZ4F_getFrameInfo(LZ4F_dctx* dctx,
1433 if (dctx->dStage > dstage_storeFrameHeader) {
1437 *frameInfoPtr = dctx->frameInfo;
1439 return LZ4F_decompress(dctx, NULL, &o, NULL, &i, NULL);
1441 if (dctx->dStage == dstage_storeFrameHeader) {
1453 { size_t decodeResult = LZ4F_decodeHeader(dctx, srcBuffer, hSize);
1460 *frameInfoPtr = dctx->frameInfo;
1470 static void LZ4F_updateDict(LZ4F_dctx* dctx,
1475 if (dctx->dictSize==0) dctx->dict = (const BYTE*)dstPtr; /* will lead to prefix mode */
1476 assert(dctx->dict != NULL);
1478 if (dctx->dict + dctx->dictSize == dstPtr) { /* prefix mode, everything within dstBuffer */
1479 dctx->dictSize += dstSize;
1485 dctx->dict = (const BYTE*)dstBufferStart;
1486 dctx->dictSize = (size_t)(dstPtr - dstBufferStart) + dstSize;
1493 assert(dctx->tmpOutBuffer != NULL);
1495 if (withinTmp && (dctx->dict == dctx->tmpOutBuffer)) { /* continue history within tmpOutBuffer */
1497 assert(dctx->dict + dctx->dictSize == dctx->tmpOut + dctx->tmpOutStart);
1498 dctx->dictSize += dstSize;
1503 size_t const preserveSize = (size_t)(dctx->tmpOut - dctx->tmpOutBuffer);
1504 size_t copySize = 64 KB - dctx->tmpOutSize;
1505 const BYTE* const oldDictEnd = dctx->dict + dctx->dictSize - dctx->tmpOutStart;
1506 if (dctx->tmpOutSize > 64 KB) copySize = 0;
1509 memcpy(dctx->tmpOutBuffer + preserveSize - copySize, oldDictEnd - copySize, copySize);
1511 dctx->dict = dctx->tmpOutBuffer;
1512 dctx->dictSize = preserveSize + dctx->tmpOutStart + dstSize;
1516 if (dctx->dict == dctx->tmpOutBuffer) { /* copy dst into tmp to complete dict */
1517 if (dctx->dictSize + dstSize > dctx->maxBufferSize) { /* tmp buffer not large enough */
1519 memcpy(dctx->tmpOutBuffer, dctx->dict + dctx->dictSize - preserveSize, preserveSize);
1520 dctx->dictSize = preserveSize;
1522 memcpy(dctx->tmpOutBuffer + dctx->dictSize, dstPtr, dstSize);
1523 dctx->dictSize += dstSize;
1529 if (preserveSize > dctx->dictSize) preserveSize = dctx->dictSize;
1530 memcpy(dctx->tmpOutBuffer, dctx->dict + dctx->dictSize - preserveSize, preserveSize);
1531 memcpy(dctx->tmpOutBuffer + preserveSize, dstPtr, dstSize);
1532 dctx->dict = dctx->tmpOutBuffer;
1533 dctx->dictSize = preserveSize + dstSize;
1556 size_t LZ4F_decompress(LZ4F_dctx* dctx,
1580 assert(dctx != NULL);
1581 dctx->skipChecksum |= (decompressOptionsPtr->skipChecksums != 0); /* once set, disable for the remainder of the frame */
1587 switch(dctx->dStage)
1593 size_t const hSize = LZ4F_decodeHeader(dctx, srcPtr, (size_t)(srcEnd-srcPtr)); /* will update dStage appropriately */
1598 dctx->tmpInSize = 0;
1600 dctx->tmpInTarget = minFHSize; /* minimum size to decode header */
1601 dctx->dStage = dstage_storeFrameHeader;
1606 { size_t const sizeToCopy = MIN(dctx->tmpInTarget - dctx->tmpInSize, (size_t)(srcEnd - srcPtr));
1607 memcpy(dctx->header + dctx->tmpInSize, srcPtr, sizeToCopy);
1608 dctx->tmpInSize += sizeToCopy;
1611 if (dctx->tmpInSize < dctx->tmpInTarget) {
1612 nextSrcSizeHint = (dctx->tmpInTarget - dctx->tmpInSize) + BHSize; /* rest of header + nextBlockHeader */
1616 FORWARD_IF_ERROR( LZ4F_decodeHeader(dctx, dctx->header, dctx->tmpInTarget) ); /* will update dStage appropriately */
1621 if (dctx->frameInfo.contentChecksumFlag) (void)XXH32_reset(&(dctx->xxh), 0);
1623 { size_t const bufferNeeded = dctx->maxBlockSize
1624 + ((dctx->frameInfo.blockMode==LZ4F_blockLinked) ? 128 KB : 0);
1625 if (bufferNeeded > dctx->maxBufferSize) { /* tmp buffers too small */
1626 dctx->maxBufferSize = 0; /* ensure allocation will be re-attempted on next entry*/
1627 LZ4F_free(dctx->tmpIn, dctx->cmem);
1628 dctx->tmpIn = (BYTE*)LZ4F_malloc(dctx->maxBlockSize + BFSize /* block checksum */, dctx->cmem);
1629 RETURN_ERROR_IF(dctx->tmpIn == NULL, allocation_failed);
1630 LZ4F_free(dctx->tmpOutBuffer, dctx->cmem);
1631 dctx->tmpOutBuffer= (BYTE*)LZ4F_malloc(bufferNeeded, dctx->cmem);
1632 RETURN_ERROR_IF(dctx->tmpOutBuffer== NULL, allocation_failed);
1633 dctx->maxBufferSize = bufferNeeded;
1635 dctx->tmpInSize = 0;
1636 dctx->tmpInTarget = 0;
1637 dctx->tmpOut = dctx->tmpOutBuffer;
1638 dctx->tmpOutStart = 0;
1639 dctx->tmpOutSize = 0;
1641 dctx->dStage = dstage_getBlockHeader;
1650 dctx->tmpInSize = 0;
1651 dctx->dStage = dstage_storeBlockHeader;
1654 if (dctx->dStage == dstage_storeBlockHeader) /* can be skipped */
1657 size_t const wantedData = BHSize - dctx->tmpInSize;
1659 memcpy(dctx->tmpIn + dctx->tmpInSize, srcPtr, sizeToCopy);
1661 dctx->tmpInSize += sizeToCopy;
1663 if (dctx->tmpInSize < BHSize) { /* not enough input for cBlockSize */
1664 nextSrcSizeHint = BHSize - dctx->tmpInSize;
1668 selectedIn = dctx->tmpIn;
1669 } /* if (dctx->dStage == dstage_storeBlockHeader) */
1674 size_t const crcSize = dctx->frameInfo.blockChecksumFlag * BFSize;
1677 dctx->dStage = dstage_getSuffix;
1680 if (nextCBlockSize > dctx->maxBlockSize) {
1685 dctx->tmpInTarget = nextCBlockSize;
1687 if (dctx->frameInfo.blockChecksumFlag) {
1688 (void)XXH32_reset(&dctx->blockChecksum, 0);
1690 dctx->dStage = dstage_copyDirect;
1694 dctx->tmpInTarget = nextCBlockSize + crcSize;
1695 dctx->dStage = dstage_getCBlock;
1710 sizeToCopy = MIN(dctx->tmpInTarget, minBuffSize);
1712 if (!dctx->skipChecksum) {
1713 if (dctx->frameInfo.blockChecksumFlag) {
1714 (void)XXH32_update(&dctx->blockChecksum, srcPtr, sizeToCopy);
1716 if (dctx->frameInfo.contentChecksumFlag)
1717 (void)XXH32_update(&dctx->xxh, srcPtr, sizeToCopy);
1719 if (dctx->frameInfo.contentSize)
1720 dctx->frameRemainingSize -= sizeToCopy;
1723 if (dctx->frameInfo.blockMode == LZ4F_blockLinked) {
1724 LZ4F_updateDict(dctx, dstPtr, sizeToCopy, dstStart, 0);
1729 if (sizeToCopy == dctx->tmpInTarget) { /* all done */
1730 if (dctx->frameInfo.blockChecksumFlag) {
1731 dctx->tmpInSize = 0;
1732 dctx->dStage = dstage_getBlockChecksum;
1734 dctx->dStage = dstage_getBlockHeader; /* new block */
1737 dctx->tmpInTarget -= sizeToCopy; /* need to copy more */
1739 nextSrcSizeHint = dctx->tmpInTarget +
1740 +(dctx->frameInfo.blockChecksumFlag ? BFSize : 0)
1749 if ((srcEnd-srcPtr >= 4) && (dctx->tmpInSize==0)) {
1753 size_t const stillToCopy = 4 - dctx->tmpInSize;
1755 memcpy(dctx->header + dctx->tmpInSize, srcPtr, sizeToCopy);
1756 dctx->tmpInSize += sizeToCopy;
1758 if (dctx->tmpInSize < 4) { /* all input consumed */
1762 crcSrc = dctx->header;
1764 if (!dctx->skipChecksum) {
1766 U32 const calcCRC = XXH32_digest(&dctx->blockChecksum);
1779 dctx->dStage = dstage_getBlockHeader; /* new block */
1784 if ((size_t)(srcEnd-srcPtr) < dctx->tmpInTarget) {
1785 dctx->tmpInSize = 0;
1786 dctx->dStage = dstage_storeCBlock;
1791 srcPtr += dctx->tmpInTarget;
1795 { size_t const wantedData = dctx->tmpInTarget - dctx->tmpInSize;
1798 memcpy(dctx->tmpIn + dctx->tmpInSize, srcPtr, sizeToCopy);
1799 dctx->tmpInSize += sizeToCopy;
1801 if (dctx->tmpInSize < dctx->tmpInTarget) { /* need more input */
1802 nextSrcSizeHint = (dctx->tmpInTarget - dctx->tmpInSize)
1803 + (dctx->frameInfo.blockChecksumFlag ? BFSize : 0)
1808 selectedIn = dctx->tmpIn;
1814 if (dctx->frameInfo.blockChecksumFlag) {
1815 assert(dctx->tmpInTarget >= 4);
1816 dctx->tmpInTarget -= 4;
1817 assert(selectedIn != NULL); /* selectedIn is defined at this stage (either srcPtr, or dctx->tmpIn) */
1818 { U32 const readBlockCrc = LZ4F_readLE32(selectedIn + dctx->tmpInTarget);
1819 U32 const calcBlockCrc = XXH32(selectedIn, dctx->tmpInTarget, 0);
1829 if ( ((size_t)(dstEnd-dstPtr) >= dctx->maxBlockSize)
1833 && !(dctx->dict!= NULL && (const BYTE*)dctx->dict + dctx->dictSize == dctx->tmpOut) )
1835 const char* dict = (const char*)dctx->dict;
1836 size_t dictSize = dctx->dictSize;
1840 /* overflow control : dctx->dictSize is an int, avoid truncation / sign issues */
1846 (int)dctx->tmpInTarget, (int)dctx->maxBlockSize,
1849 if ((dctx->frameInfo.contentChecksumFlag) && (!dctx->skipChecksum))
1850 XXH32_update(&(dctx->xxh), dstPtr, (size_t)decodedSize);
1851 if (dctx->frameInfo.contentSize)
1852 dctx->frameRemainingSize -= (size_t)decodedSize;
1855 if (dctx->frameInfo.blockMode==LZ4F_blockLinked) {
1856 LZ4F_updateDict(dctx, dstPtr, (size_t)decodedSize, dstStart, 0);
1860 dctx->dStage = dstage_getBlockHeader; /* end of block, let's get another one */
1867 if (dctx->frameInfo.blockMode == LZ4F_blockLinked) {
1868 if (dctx->dict == dctx->tmpOutBuffer) {
1870 if (dctx->dictSize > 128 KB) {
1871 memcpy(dctx->tmpOutBuffer, dctx->dict + dctx->dictSize - 64 KB, 64 KB);
1872 dctx->dictSize = 64 KB;
1874 dctx->tmpOut = dctx->tmpOutBuffer + dctx->dictSize;
1876 size_t const reservedDictSpace = MIN(dctx->dictSize, 64 KB);
1877 dctx->tmpOut = dctx->tmpOutBuffer + reservedDictSpace;
1881 { const char* dict = (const char*)dctx->dict;
1882 size_t dictSize = dctx->dictSize;
1890 (const char*)selectedIn, (char*)dctx->tmpOut,
1891 (int)dctx->tmpInTarget, (int)dctx->maxBlockSize,
1894 if (dctx->frameInfo.contentChecksumFlag && !dctx->skipChecksum)
1895 XXH32_update(&(dctx->xxh), dctx->tmpOut, (size_t)decodedSize);
1896 if (dctx->frameInfo.contentSize)
1897 dctx->frameRemainingSize -= (size_t)decodedSize;
1898 dctx->tmpOutSize = (size_t)decodedSize;
1899 dctx->tmpOutStart = 0;
1900 dctx->dStage = dstage_flushOut;
1907 size_t const sizeToCopy = MIN(dctx->tmpOutSize - dctx->tmpOutStart, (size_t)(dstEnd-dstPtr));
1908 memcpy(dstPtr, dctx->tmpOut + dctx->tmpOutStart, sizeToCopy);
1911 if (dctx->frameInfo.blockMode == LZ4F_blockLinked)
1912 LZ4F_updateDict(dctx, dstPtr, sizeToCopy, dstStart, 1 /*withinTmp*/);
1914 dctx->tmpOutStart += sizeToCopy;
1917 if (dctx->tmpOutStart == dctx->tmpOutSize) { /* all flushed */
1918 dctx->dStage = dstage_getBlockHeader; /* get next block */
1927 RETURN_ERROR_IF(dctx->frameRemainingSize, frameSize_wrong); /* incorrect frame size decoded */
1928 if (!dctx->frameInfo.contentChecksumFlag) { /* no checksum, frame is completed */
1930 LZ4F_resetDecompressionContext(dctx);
1935 dctx->tmpInSize = 0;
1936 dctx->dStage = dstage_storeSuffix;
1942 if (dctx->dStage == dstage_storeSuffix) /* can be skipped */
1945 size_t const wantedData = 4 - dctx->tmpInSize;
1947 memcpy(dctx->tmpIn + dctx->tmpInSize, srcPtr, sizeToCopy);
1949 dctx->tmpInSize += sizeToCopy;
1950 if (dctx->tmpInSize < 4) { /* not enough input to read complete suffix */
1951 nextSrcSizeHint = 4 - dctx->tmpInSize;
1955 selectedIn = dctx->tmpIn;
1956 } /* if (dctx->dStage == dstage_storeSuffix) */
1959 if (!dctx->skipChecksum) {
1961 U32 const resultCRC = XXH32_digest(&(dctx->xxh));
1970 LZ4F_resetDecompressionContext(dctx);
1980 dctx->tmpInSize = 4;
1981 dctx->tmpInTarget = 8;
1982 dctx->dStage = dstage_storeSFrameSize;
1985 if (dctx->dStage == dstage_storeSFrameSize)
1987 { size_t const sizeToCopy = MIN(dctx->tmpInTarget - dctx->tmpInSize,
1989 memcpy(dctx->header + dctx->tmpInSize, srcPtr, sizeToCopy);
1991 dctx->tmpInSize += sizeToCopy;
1992 if (dctx->tmpInSize < dctx->tmpInTarget) {
1994 nextSrcSizeHint = dctx->tmpInTarget - dctx->tmpInSize;
1998 selectedIn = dctx->header + 4;
1999 } /* if (dctx->dStage == dstage_storeSFrameSize) */
2003 dctx->frameInfo.contentSize = SFrameSize;
2004 dctx->tmpInTarget = SFrameSize;
2005 dctx->dStage = dstage_skipSkippable;
2010 { size_t const skipSize = MIN(dctx->tmpInTarget, (size_t)(srcEnd-srcPtr));
2012 dctx->tmpInTarget -= skipSize;
2014 nextSrcSizeHint = dctx->tmpInTarget;
2017 LZ4F_resetDecompressionContext(dctx);
2020 } /* switch (dctx->dStage) */
2025 if ( (dctx->frameInfo.blockMode==LZ4F_blockLinked) /* next block will use up to 64KB from previous ones */
2026 && (dctx->dict != dctx->tmpOutBuffer) /* dictionary is not already within tmp */
2027 && (dctx->dict != NULL) /* dictionary exists */
2029 && ((unsigned)(dctx->dStage)-2 < (unsigned)(dstage_getSuffix)-2) ) /* valid stages : [init ... getSuffix[ */
2031 if (dctx->dStage == dstage_flushOut) {
2032 size_t const preserveSize = (size_t)(dctx->tmpOut - dctx->tmpOutBuffer);
2033 size_t copySize = 64 KB - dctx->tmpOutSize;
2034 const BYTE* oldDictEnd = dctx->dict + dctx->dictSize - dctx->tmpOutStart;
2035 if (dctx->tmpOutSize > 64 KB) copySize = 0;
2037 assert(dctx->tmpOutBuffer != NULL);
2039 memcpy(dctx->tmpOutBuffer + preserveSize - copySize, oldDictEnd - copySize, copySize);
2041 dctx->dict = dctx->tmpOutBuffer;
2042 dctx->dictSize = preserveSize + dctx->tmpOutStart;
2044 const BYTE* const oldDictEnd = dctx->dict + dctx->dictSize;
2045 size_t const newDictSize = MIN(dctx->dictSize, 64 KB);
2047 memcpy(dctx->tmpOutBuffer, oldDictEnd - newDictSize, newDictSize);
2049 dctx->dict = dctx->tmpOutBuffer;
2050 dctx->dictSize = newDictSize;
2051 dctx->tmpOut = dctx->tmpOutBuffer + newDictSize;
2065 size_t LZ4F_decompress_usingDict(LZ4F_dctx* dctx,
2071 if (dctx->dStage <= dstage_init) {
2072 dctx->dict = (const BYTE*)dict;
2073 dctx->dictSize = dictSize;
2075 return LZ4F_decompress(dctx, dstBuffer, dstSizePtr,