Coverage Report

Created: 2026-02-24 06:32

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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
5.06M
#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.94k
) {
27
4.94k
    const BYTE* const istart = (const BYTE*)src;
28
4.94k
    const BYTE* const iend = istart + srcSize;
29
4.94k
    const BYTE* ip = istart;
30
4.94k
    const BYTE* anchor = istart;
31
4.94k
    size_t seqCount = 0;
32
4.94k
    U32 hashTable[HSIZE];
33
34
4.94k
    (void)sequenceProducerState;
35
4.94k
    (void)dict;
36
4.94k
    (void)dictSize;
37
4.94k
    (void)outSeqsCapacity;
38
4.94k
    (void)compressionLevel;
39
40
4.94k
    {   int i;
41
5.06M
        for (i=0; i < HSIZE; i++) {
42
5.06M
            hashTable[i] = BADIDX;
43
5.06M
    }   }
44
45
2.47M
    while (ip + MLS < iend) {
46
2.46M
        size_t const hash = ZSTD_hashPtr(ip, HLOG, MLS);
47
2.46M
        U32 const matchIndex = hashTable[hash];
48
2.46M
        hashTable[hash] = (U32)(ip - istart);
49
50
2.46M
        if (matchIndex != BADIDX) {
51
1.45M
            const BYTE* const match = istart + matchIndex;
52
1.45M
            U32 const matchLen = (U32)ZSTD_count(ip, match, iend);
53
1.45M
            if (matchLen >= ZSTD_MINMATCH_MIN) {
54
280k
                U32 const litLen = (U32)(ip - anchor);
55
280k
                U32 const offset = (U32)(ip - match);
56
280k
                ZSTD_Sequence const seq = {
57
280k
                    offset, litLen, matchLen, 0
58
280k
                };
59
60
                /* Note: it's crucial to stay within the window size! */
61
280k
                if (offset <= windowSize) {
62
280k
                    outSeqs[seqCount++] = seq;
63
280k
                    ip += matchLen;
64
280k
                    anchor = ip;
65
280k
                    continue;
66
280k
                }
67
280k
            }
68
1.45M
        }
69
70
2.18M
        ip++;
71
2.18M
    }
72
73
4.94k
    {   ZSTD_Sequence const finalSeq = {
74
4.94k
            0, (U32)(iend - anchor), 0, 0
75
4.94k
        };
76
4.94k
        outSeqs[seqCount++] = finalSeq;
77
4.94k
    }
78
79
4.94k
    return seqCount;
80
4.94k
}