***************************************/
#include <limits.h> /* INT_MAX */
#include <string.h> /* memset */
+#include <stdlib.h>
#include "cpu.h"
#include "mem.h"
#include "hist.h" /* HIST_countFast_wksp */
ssPtr->longLengthID = 0;
}
-typedef struct {
- U32 matchPos;
- U32 offset;
- U32 litLength;
- U32 matchLength;
- int rep;
-} Sequence;
-
-static size_t ZSTD_getSequencesForOneBlock(ZSTD_CCtx* zc, ZSTD_CDict* cdict,
- void* dst, size_t dstSize,
- const void* src, size_t srcSize,
- Sequence* outSeqs, size_t outSeqsSize)
-{
- const seqStore_t* seqStore;
- const seqDef* seqs;
- size_t seqsSize;
-
- size_t i; int repIdx; size_t position;
-
- size_t blockSize = ZSTD_getBlockSize(zc);
- size_t maxOutput = ZSTD_compressBound(blockSize);
-
- assert(!ZSTD_isError(ZSTD_compressBegin_usingCDict(zc, cdict)));
- assert(dstSize >= maxOutput); dstSize = maxOutput;
- assert(srcSize >= blockSize); srcSize = blockSize;
- assert(!ZSTD_isError(ZSTD_compressBlock(zc, dst, dstSize, src, srcSize)));
-
- seqStore = ZSTD_getSeqStore(zc);
- seqs = seqStore->sequencesStart;
- seqsSize = seqStore->sequences - seqStore->sequencesStart;
-
- assert(outSeqsSize >= seqsSize); outSeqsSize = seqsSize;
-
- for (i = 0, position = 0; i < seqsSize; ++i) {
- outSeqs[i].offset = seqs[i].offset;
- outSeqs[i].litLength = seqs[i].litLength;
- outSeqs[i].matchLength = seqs[i].matchLength + 3 /* min match */;
-
- if (i == seqStore->longLengthPos) {
- if (seqStore->longLengthID == 1) {
- outSeqs[i].litLength += 0x10000;
- } else if (seqStore->longLengthID == 2) {
- outSeqs[i].matchLength += 0x10000;
- }
- }
-
- if (outSeqs[i].offset <= 3 /* num reps */) {
- outSeqs[i].rep = 1;
- repIdx = i - outSeqs[i].offset;
-
- if (repIdx >= 0) {
- outSeqs[i].offset = outSeqs[repIdx].offset;
- }
-
- if (repIdx == -1) {
- outSeqs[i].offset = 1;
- } else if (repIdx == -2) {
- outSeqs[i].offset = 4;
- } else if (repIdx == -3) {
- outSeqs[i].offset = 8;
- }
- } else {
- outSeqs[i].offset -= 3 /* num reps */;
- }
-
- position += outSeqs[i].litLength;
- outSeqs[i].matchPos = position;
- position += outSeqs[i].matchLength;
- }
-}
-
typedef enum { ZSTDbss_compress, ZSTDbss_noCompress } ZSTD_buildSeqStore_e;
static size_t ZSTD_buildSeqStore(ZSTD_CCtx* zc, const void* src, size_t srcSize)
}
}
+static void ZSTD_copyBlockSequences(const seqStore_t* seqStore, seqDef* seqs,
+ ZSTD_Sequence* outSeqs, size_t seqsSize)
+{
+ size_t i; size_t position; int repIdx;
+ for (i = 0, position = 0; i < seqsSize; ++i) {
+ outSeqs[i].offset = seqs[i].offset;
+ outSeqs[i].litLength = seqs[i].litLength;
+ outSeqs[i].matchLength = seqs[i].matchLength + 3 /* min match */;
+
+ if (i == seqStore->longLengthPos) {
+ if (seqStore->longLengthID == 1) {
+ outSeqs[i].litLength += 0x10000;
+ } else if (seqStore->longLengthID == 2) {
+ outSeqs[i].matchLength += 0x10000;
+ }
+ }
+
+ if (outSeqs[i].offset <= 3 /* num reps */) {
+ outSeqs[i].rep = 1;
+ repIdx = i - outSeqs[i].offset;
+
+ if (repIdx >= 0) {
+ outSeqs[i].offset = outSeqs[repIdx].offset;
+ }
+
+ if (repIdx == -1) {
+ outSeqs[i].offset = 1;
+ } else if (repIdx == -2) {
+ outSeqs[i].offset = 4;
+ } else if (repIdx == -3) {
+ outSeqs[i].offset = 8;
+ }
+ } else {
+ outSeqs[i].offset -= 3 /* num reps */;
+ }
+
+ position += outSeqs[i].litLength;
+ outSeqs[i].matchPos = position;
+ position += outSeqs[i].matchLength;
+ }
+}
+
+static void ZSTD_getBlockSequences(ZSTD_CCtx* cctx, const seqStore_t* seqStore)
+{
+ size_t seqsSize = seqStore->sequences - seqStore->sequencesStart;
+
+ assert(cctx->seqCollector.maxSequences >
+ (cctx->seqCollector.seqCurrent - cctx->seqCollector.seqStart) + seqsSize);
+
+ ZSTD_copyBlockSequences(seqStore, seqStore->sequencesStart,
+ cctx->seqCollector.seqCurrent, seqsSize);
+ cctx->seqCollector.seqCurrent += seqsSize;
+}
+
+size_t ZSTD_getSequences(ZSTD_CCtx* zc, const void* src,
+ size_t srcSize, ZSTD_Sequence* outSeqs, size_t outSeqsSize,
+ int level)
+{
+ size_t dstCapacity = ZSTD_compressBound(srcSize * sizeof(void*));
+ void* dst = malloc(dstCapacity);
+ size_t seqsSize;
+
+ SeqCollector seqCollector;
+ seqCollector.collectSequences = 1;
+ seqCollector.seqStart = outSeqs;
+ seqCollector.seqCurrent = outSeqs;
+ seqCollector.maxSequences = outSeqsSize;
+ zc->seqCollector = seqCollector;
+
+ ZSTD_compressCCtx(zc, dst, dstCapacity, src, srcSize, level);
+ seqsSize = zc->seqCollector.seqCurrent - zc->seqCollector.seqStart;
+
+ free(dst);
+ return seqsSize;
+}
/*! ZSTD_compress_frameChunk() :
* Compress a chunk of data into one or multiple blocks.
op+ZSTD_blockHeaderSize, dstCapacity-ZSTD_blockHeaderSize,
ip, blockSize);
FORWARD_IF_ERROR(cSize);
+ if (cctx->seqCollector.collectSequences) {
+ ZSTD_getBlockSequences(cctx, ZSTD_getSeqStore(cctx));
+ }
if (cSize == 0) { /* block is not compressible */
cSize = ZSTD_noCompressBlock(op, dstCapacity, ip, blockSize, lastBlock);