1 1.1 christos /* 2 1.1 christos * Copyright (c) Yann Collet, Meta Platforms, Inc. 3 1.1 christos * All rights reserved. 4 1.1 christos * 5 1.1 christos * This source code is licensed under both the BSD-style license (found in the 6 1.1 christos * LICENSE file in the root directory of this source tree) and the GPLv2 (found 7 1.1 christos * in the COPYING file in the root directory of this source tree). 8 1.1 christos * You may select, at your option, one of the above-listed licenses. 9 1.1 christos */ 10 1.1 christos 11 1.1 christos #include "zstd_compress_internal.h" 12 1.1 christos #include "sequence_producer.h" 13 1.1 christos 14 1.1 christos #define HSIZE 1024 15 1.1 christos static U32 const HLOG = 10; 16 1.1 christos static U32 const MLS = 4; 17 1.1 christos static U32 const BADIDX = 0xffffffff; 18 1.1 christos 19 1.1 christos size_t simpleSequenceProducer( 20 1.1 christos void* sequenceProducerState, 21 1.1 christos ZSTD_Sequence* outSeqs, size_t outSeqsCapacity, 22 1.1 christos const void* src, size_t srcSize, 23 1.1 christos const void* dict, size_t dictSize, 24 1.1 christos int compressionLevel, 25 1.1 christos size_t windowSize 26 1.1 christos ) { 27 1.1 christos const BYTE* const istart = (const BYTE*)src; 28 1.1 christos const BYTE* const iend = istart + srcSize; 29 1.1 christos const BYTE* ip = istart; 30 1.1 christos const BYTE* anchor = istart; 31 1.1 christos size_t seqCount = 0; 32 1.1 christos U32 hashTable[HSIZE]; 33 1.1 christos 34 1.1 christos (void)sequenceProducerState; 35 1.1 christos (void)dict; 36 1.1 christos (void)dictSize; 37 1.1 christos (void)outSeqsCapacity; 38 1.1 christos (void)compressionLevel; 39 1.1 christos 40 1.1 christos { int i; 41 1.1 christos for (i=0; i < HSIZE; i++) { 42 1.1 christos hashTable[i] = BADIDX; 43 1.1 christos } } 44 1.1 christos 45 1.1 christos while (ip + MLS < iend) { 46 1.1 christos size_t const hash = ZSTD_hashPtr(ip, HLOG, MLS); 47 1.1 christos U32 const matchIndex = hashTable[hash]; 48 1.1 christos hashTable[hash] = (U32)(ip - istart); 49 1.1 christos 50 1.1 christos if (matchIndex != BADIDX) { 51 1.1 christos const BYTE* const match = istart + matchIndex; 52 1.1 christos U32 const matchLen = (U32)ZSTD_count(ip, match, iend); 53 1.1 christos if (matchLen >= ZSTD_MINMATCH_MIN) { 54 1.1 christos U32 const litLen = (U32)(ip - anchor); 55 1.1 christos U32 const offset = (U32)(ip - match); 56 1.1 christos ZSTD_Sequence const seq = { 57 1.1 christos offset, litLen, matchLen, 0 58 1.1 christos }; 59 1.1 christos 60 1.1 christos /* Note: it's crucial to stay within the window size! */ 61 1.1 christos if (offset <= windowSize) { 62 1.1 christos outSeqs[seqCount++] = seq; 63 1.1 christos ip += matchLen; 64 1.1 christos anchor = ip; 65 1.1 christos continue; 66 1.1 christos } 67 1.1 christos } 68 1.1 christos } 69 1.1 christos 70 1.1 christos ip++; 71 1.1 christos } 72 1.1 christos 73 1.1 christos { ZSTD_Sequence const finalSeq = { 74 1.1 christos 0, (U32)(iend - anchor), 0, 0 75 1.1 christos }; 76 1.1 christos outSeqs[seqCount++] = finalSeq; 77 1.1 christos } 78 1.1 christos 79 1.1 christos return seqCount; 80 1.1 christos } 81