From 96b0ff78865c3952827e573268103ff93e01560e Mon Sep 17 00:00:00 2001 From: senhuang42 Date: Tue, 27 Oct 2020 10:36:06 -0400 Subject: [PATCH] Improve documentation regarding various operations in copyBlockSequences --- lib/compress/zstd_compress.c | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/lib/compress/zstd_compress.c b/lib/compress/zstd_compress.c index 119b9094..b3ada04f 100644 --- a/lib/compress/zstd_compress.c +++ b/lib/compress/zstd_compress.c @@ -2452,10 +2452,13 @@ static void ZSTD_copyBlockSequences(ZSTD_CCtx* zc) assert(zc->seqCollector.seqIndex + 1 < zc->seqCollector.maxSequences); for (i = 0, position = 0; i < seqStoreSeqSize; ++i) { - outSeqs[i].offset = seqStoreSeqs[i].offset - ZSTD_REP_NUM; outSeqs[i].litLength = seqStoreSeqs[i].litLength; outSeqs[i].matchLength = seqStoreSeqs[i].matchLength + MINMATCH; + /* matchLength and litLength are stored with U16. longLengthPos + * and longLengthID to allow us to represent a single litLength or matchLength + * in the seqStore that has a value larger than U16 (if it exists). + */ if (i == seqStore->longLengthPos) { if (seqStore->longLengthID == 1) { outSeqs[i].litLength += 0x10000; @@ -2465,14 +2468,15 @@ static void ZSTD_copyBlockSequences(ZSTD_CCtx* zc) } /* Repcode handling: + * See docs/format.md for more detail about repeat offset codes * If litLength != 0: - * rep == 1 --> offset == repeat offset 1 - * rep == 2 --> offset == repeat offset 2 - * rep == 3 --> offset == repeat offset 3 + * rep == 1 --> offset == repeat_offset_1 + * rep == 2 --> offset == repeat_offset_2 + * rep == 3 --> offset == repeat_offset_3 * If litLength == 0: - * rep == 1 --> offset == repeat offset 2 - * rep == 2 --> offset == repeat offset 3 - * rep == 3 --> offset == repeat offset 1 - 1 + * rep == 1 --> offset == repeat_offset_2 + * rep == 2 --> offset == repeat_offset_3 + * rep == 3 --> offset == repeat_offset_1 - 1 */ if (seqStoreSeqs[i].offset <= ZSTD_REP_NUM) { outSeqs[i].rep = seqStoreSeqs[i].offset; @@ -2486,10 +2490,15 @@ static void ZSTD_copyBlockSequences(ZSTD_CCtx* zc) } } assert(repIdx >= -3); + /* Use default repcodes if repcode references an offset that doesn't exist yet + * This can only occur within the first two sequences. + */ outSeqs[i].offset = repIdx >= 0 ? outSeqs[repIdx].offset : repStartValue[-repIdx - 1]; if (outSeqs[i].rep == 3 && outSeqs[i].litLength == 0) { --outSeqs[i].offset; } + } else { + outSeqs[i].offset = seqStoreSeqs[i].offset - ZSTD_REP_NUM; } position += outSeqs[i].litLength + outSeqs[i].matchLength; }