Coverage Report

Created: 2024-07-27 06:12

/src/lzma-fuzz/ppmdenc_fuzzer.cc
Line
Count
Source (jump to first uncovered line)
1
/**
2
 *
3
 * @copyright Copyright (c) 2019 Joachim Bauch <mail@joachim-bauch.de>
4
 *
5
 * @license GNU GPL version 3 or any later version
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19
 *
20
 */
21
22
#include <assert.h>
23
#include <stdint.h>
24
25
#include "Ppmd7.h"
26
27
#include "common-alloc.h"
28
#include "common-buffer.h"
29
30
1.63k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
31
1.63k
  if (size <= 10) {
32
6
    return 0;
33
6
  }
34
35
1.62k
  CPpmd7 p_enc;
36
1.62k
  Ppmd7_Construct(&p_enc);
37
1.62k
  UInt32 memsize = PPMD7_MIN_MEM_SIZE;
38
1.62k
  if (!Ppmd7_Alloc(&p_enc, memsize, &CommonAlloc)) {
39
0
    return 0;
40
0
  }
41
42
1.62k
  int order = data[0];
43
1.62k
  if (order < PPMD7_MIN_ORDER || order > PPMD7_MAX_ORDER) {
44
9
    Ppmd7_Free(&p_enc, &CommonAlloc);
45
9
    return 0;
46
9
  }
47
1.61k
  Ppmd7_Init(&p_enc, order);
48
49
1.61k
  OutputByteBuffer out_buffer;
50
1.61k
  CPpmd7z_RangeEnc enc;
51
1.61k
  Ppmd7z_RangeEnc_Init(&enc);
52
1.61k
  enc.Stream = out_buffer.stream();
53
7.95M
  for (size_t i = 0; i < size; ++i) {
54
7.95M
    Ppmd7_EncodeSymbol(&p_enc, &enc, data[i]);
55
7.95M
  }
56
1.61k
  Ppmd7z_RangeEnc_FlushData(&enc);
57
1.61k
  Ppmd7_Free(&p_enc, &CommonAlloc);
58
59
1.61k
  {
60
1.61k
    assert(out_buffer.size() >= 5);
61
1.61k
    InputByteBuffer in_buffer(out_buffer.data(), out_buffer.size());
62
1.61k
    CPpmd7 p_dec;
63
1.61k
    Ppmd7_Construct(&p_dec);
64
1.61k
    assert(Ppmd7_Alloc(&p_dec, memsize, &CommonAlloc));
65
1.61k
    Ppmd7_Init(&p_dec, order);
66
1.61k
    CPpmd7z_RangeDec dec;
67
1.61k
    Ppmd7z_RangeDec_CreateVTable(&dec);
68
1.61k
    dec.Stream = in_buffer.stream();
69
1.61k
    assert(Ppmd7z_RangeDec_Init(&dec));
70
71
7.95M
    for (size_t i = 0; i < size; ++i) {
72
7.95M
      int sym = Ppmd7_DecodeSymbol(&p_dec, &dec.vt);
73
7.95M
      assert(sym >= 0);
74
7.95M
      assert(sym == data[i]);
75
7.95M
    }
76
1.61k
    Ppmd7_Free(&p_dec, &CommonAlloc);
77
1.61k
  }
78
0
  return 0;
79
1.61k
}