@@ -2940,7 +2940,7 @@ static void ZSTD_copyBlockSequences(ZSTD_CCtx* zc)
29402940 /* seqStoreSeqs[i].offset == offCode+1, and ZSTD_updateRep() expects offCode
29412941 so we provide seqStoreSeqs[i].offset - 1 */
29422942 ZSTD_updateRep(updatedRepcodes.rep,
2943- seqStoreSeqs[i].offBase - 1 ,
2943+ seqStoreSeqs[i].offBase,
29442944 seqStoreSeqs[i].litLength == 0);
29452945 literalsRead += outSeqs[i].litLength;
29462946 }
@@ -3433,20 +3433,22 @@ static void ZSTD_deriveSeqStoreChunk(seqStore_t* resultSeqStore,
34333433}
34343434
34353435/**
3436- * Returns the raw offset represented by the combination of offCode , ll0, and repcode history.
3437- * offCode must represent a repcode in the numeric representation of ZSTD_storeSeq().
3436+ * Returns the raw offset represented by the combination of offBase , ll0, and repcode history.
3437+ * offBase must represent a repcode in the numeric representation of ZSTD_storeSeq().
34383438 */
34393439static U32
3440- ZSTD_resolveRepcodeToRawOffset(const U32 rep[ZSTD_REP_NUM], const U32 offCode , const U32 ll0)
3440+ ZSTD_resolveRepcodeToRawOffset(const U32 rep[ZSTD_REP_NUM], const U32 offBase , const U32 ll0)
34413441{
3442- U32 const adjustedOffCode = STORED_REPCODE(offCode) - 1 + ll0; /* [ 0 - 3 ] */
3443- assert(STORED_IS_REPCODE(offCode));
3444- if (adjustedOffCode == ZSTD_REP_NUM) {
3445- /* litlength == 0 and offCode == 2 implies selection of first repcode - 1 */
3446- assert(rep[0] > 0);
3442+ U32 const adjustedRepCode = OFFBASE_TO_REPCODE(offBase) - 1 + ll0; /* [ 0 - 3 ] */
3443+ assert(OFFBASE_IS_REPCODE(offBase));
3444+ if (adjustedRepCode == ZSTD_REP_NUM) {
3445+ /* litlength == 0 and offCode == 2 implies selection of first repcode - 1
3446+ * This is only valid if it results in a valid offset value, aka > 0.
3447+ */
3448+ assert(rep[0] > 1);
34473449 return rep[0] - 1;
34483450 }
3449- return rep[adjustedOffCode ];
3451+ return rep[adjustedRepCode ];
34503452}
34513453
34523454/**
@@ -3468,11 +3470,11 @@ static void ZSTD_seqStore_resolveOffCodes(repcodes_t* const dRepcodes, repcodes_
34683470 for (; idx < nbSeq; ++idx) {
34693471 seqDef* const seq = seqStore->sequencesStart + idx;
34703472 U32 const ll0 = (seq->litLength == 0);
3471- U32 const offCode = OFFBASE_TO_STORED( seq->offBase) ;
3473+ U32 const offBase = seq->offBase;
34723474 assert(seq->offBase > 0);
3473- if (STORED_IS_REPCODE(offCode )) {
3474- U32 const dRawOffset = ZSTD_resolveRepcodeToRawOffset(dRepcodes->rep, offCode , ll0);
3475- U32 const cRawOffset = ZSTD_resolveRepcodeToRawOffset(cRepcodes->rep, offCode , ll0);
3475+ if (OFFBASE_IS_REPCODE(offBase )) {
3476+ U32 const dRawOffset = ZSTD_resolveRepcodeToRawOffset(dRepcodes->rep, offBase , ll0);
3477+ U32 const cRawOffset = ZSTD_resolveRepcodeToRawOffset(cRepcodes->rep, offBase , ll0);
34763478 /* Adjust simulated decompression repcode history if we come across a mismatch. Replace
34773479 * the repcode with the offset it actually references, determined by the compression
34783480 * repcode history.
@@ -3484,8 +3486,8 @@ static void ZSTD_seqStore_resolveOffCodes(repcodes_t* const dRepcodes, repcodes_
34843486 /* Compression repcode history is always updated with values directly from the unmodified seqStore.
34853487 * Decompression repcode history may use modified seq->offset value taken from compression repcode history.
34863488 */
3487- ZSTD_updateRep(dRepcodes->rep, OFFBASE_TO_STORED( seq->offBase) , ll0);
3488- ZSTD_updateRep(cRepcodes->rep, offCode , ll0);
3489+ ZSTD_updateRep(dRepcodes->rep, seq->offBase, ll0);
3490+ ZSTD_updateRep(cRepcodes->rep, offBase , ll0);
34893491 }
34903492}
34913493
@@ -5770,26 +5772,26 @@ ZSTD_validateSequence(U32 offCode, U32 matchLength,
57705772 * window size. After output surpasses windowSize, we're limited to windowSize offsets again.
57715773 */
57725774 size_t const offsetBound = posInSrc > windowSize ? (size_t)windowSize : posInSrc + (size_t)dictSize;
5773- RETURN_ERROR_IF(offCode > STORE_OFFSET (offsetBound), corruption_detected, "Offset too large!");
5775+ RETURN_ERROR_IF(offCode > OFFSET_TO_OFFBASE (offsetBound), corruption_detected, "Offset too large!");
57745776 RETURN_ERROR_IF(matchLength < MINMATCH, corruption_detected, "Matchlength too small");
57755777 return 0;
57765778}
57775779
57785780/* Returns an offset code, given a sequence's raw offset, the ongoing repcode array, and whether litLength == 0 */
5779- static U32 ZSTD_finalizeOffCode (U32 rawOffset, const U32 rep[ZSTD_REP_NUM], U32 ll0)
5781+ static U32 ZSTD_finalizeOffBase (U32 rawOffset, const U32 rep[ZSTD_REP_NUM], U32 ll0)
57805782{
5781- U32 offCode = STORE_OFFSET (rawOffset);
5783+ U32 offBase = OFFSET_TO_OFFBASE (rawOffset);
57825784
57835785 if (!ll0 && rawOffset == rep[0]) {
5784- offCode = STORE_REPCODE_1 ;
5786+ offBase = REPCODE1_TO_OFFBASE ;
57855787 } else if (rawOffset == rep[1]) {
5786- offCode = STORE_REPCODE (2 - ll0);
5788+ offBase = REPCODE_TO_OFFBASE (2 - ll0);
57875789 } else if (rawOffset == rep[2]) {
5788- offCode = STORE_REPCODE (3 - ll0);
5790+ offBase = REPCODE_TO_OFFBASE (3 - ll0);
57895791 } else if (ll0 && rawOffset == rep[0] - 1) {
5790- offCode = STORE_REPCODE_3 ;
5792+ offBase = REPCODE3_TO_OFFBASE ;
57915793 }
5792- return offCode ;
5794+ return offBase ;
57935795}
57945796
57955797/* Returns 0 on success, and a ZSTD_error otherwise. This function scans through an array of
@@ -5819,19 +5821,19 @@ ZSTD_copySequencesToSeqStoreExplicitBlockDelim(ZSTD_CCtx* cctx,
58195821 U32 const litLength = inSeqs[idx].litLength;
58205822 U32 const ll0 = (litLength == 0);
58215823 U32 const matchLength = inSeqs[idx].matchLength;
5822- U32 const offCode = ZSTD_finalizeOffCode (inSeqs[idx].offset, updatedRepcodes.rep, ll0);
5823- ZSTD_updateRep(updatedRepcodes.rep, offCode , ll0);
5824+ U32 const offBase = ZSTD_finalizeOffBase (inSeqs[idx].offset, updatedRepcodes.rep, ll0);
5825+ ZSTD_updateRep(updatedRepcodes.rep, offBase , ll0);
58245826
5825- DEBUGLOG(6, "Storing sequence: (of: %u, ml: %u, ll: %u)", offCode , matchLength, litLength);
5827+ DEBUGLOG(6, "Storing sequence: (of: %u, ml: %u, ll: %u)", offBase , matchLength, litLength);
58265828 if (cctx->appliedParams.validateSequences) {
58275829 seqPos->posInSrc += litLength + matchLength;
5828- FORWARD_IF_ERROR(ZSTD_validateSequence(offCode , matchLength, seqPos->posInSrc,
5830+ FORWARD_IF_ERROR(ZSTD_validateSequence(offBase , matchLength, seqPos->posInSrc,
58295831 cctx->appliedParams.cParams.windowLog, dictSize),
58305832 "Sequence validation failed");
58315833 }
58325834 RETURN_ERROR_IF(idx - seqPos->idx > cctx->seqStore.maxNbSeq, memory_allocation,
58335835 "Not enough memory allocated. Try adjusting ZSTD_c_minMatch.");
5834- ZSTD_storeSeq(&cctx->seqStore, litLength, ip, iend, offCode , matchLength);
5836+ ZSTD_storeSeq(&cctx->seqStore, litLength, ip, iend, offBase , matchLength);
58355837 ip += matchLength + litLength;
58365838 }
58375839 ZSTD_memcpy(cctx->blockState.nextCBlock->rep, updatedRepcodes.rep, sizeof(repcodes_t));
@@ -5888,7 +5890,7 @@ ZSTD_copySequencesToSeqStoreNoBlockDelim(ZSTD_CCtx* cctx, ZSTD_sequencePosition*
58885890 U32 litLength = currSeq.litLength;
58895891 U32 matchLength = currSeq.matchLength;
58905892 U32 const rawOffset = currSeq.offset;
5891- U32 offCode ;
5893+ U32 offBase ;
58925894
58935895 /* Modify the sequence depending on where endPosInSequence lies */
58945896 if (endPosInSequence >= currSeq.litLength + currSeq.matchLength) {
@@ -5942,20 +5944,20 @@ ZSTD_copySequencesToSeqStoreNoBlockDelim(ZSTD_CCtx* cctx, ZSTD_sequencePosition*
59425944 }
59435945 /* Check if this offset can be represented with a repcode */
59445946 { U32 const ll0 = (litLength == 0);
5945- offCode = ZSTD_finalizeOffCode (rawOffset, updatedRepcodes.rep, ll0);
5946- ZSTD_updateRep(updatedRepcodes.rep, offCode , ll0);
5947+ offBase = ZSTD_finalizeOffBase (rawOffset, updatedRepcodes.rep, ll0);
5948+ ZSTD_updateRep(updatedRepcodes.rep, offBase , ll0);
59475949 }
59485950
59495951 if (cctx->appliedParams.validateSequences) {
59505952 seqPos->posInSrc += litLength + matchLength;
5951- FORWARD_IF_ERROR(ZSTD_validateSequence(offCode , matchLength, seqPos->posInSrc,
5953+ FORWARD_IF_ERROR(ZSTD_validateSequence(offBase , matchLength, seqPos->posInSrc,
59525954 cctx->appliedParams.cParams.windowLog, dictSize),
59535955 "Sequence validation failed");
59545956 }
5955- DEBUGLOG(6, "Storing sequence: (of: %u, ml: %u, ll: %u)", offCode , matchLength, litLength);
5957+ DEBUGLOG(6, "Storing sequence: (of: %u, ml: %u, ll: %u)", offBase , matchLength, litLength);
59565958 RETURN_ERROR_IF(idx - seqPos->idx > cctx->seqStore.maxNbSeq, memory_allocation,
59575959 "Not enough memory allocated. Try adjusting ZSTD_c_minMatch.");
5958- ZSTD_storeSeq(&cctx->seqStore, litLength, ip, iend, offCode , matchLength);
5960+ ZSTD_storeSeq(&cctx->seqStore, litLength, ip, iend, offBase , matchLength);
59595961 ip += matchLength + litLength;
59605962 }
59615963 DEBUGLOG(5, "Ending seq: idx: %u (of: %u ml: %u ll: %u)", idx, inSeqs[idx].offset, inSeqs[idx].matchLength, inSeqs[idx].litLength);
0 commit comments