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