Coverage Report

Created: 2026-01-17 07:03

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/lzma-fuzz/xzenc_fuzzer.cc
Line
Count
Source
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 <stdint.h>
23
24
#include "7zCrc.h"
25
#include "Xz.h"
26
#include "XzCrc64.h"
27
#include "XzEnc.h"
28
29
#include "common-alloc.h"
30
#include "common-buffer.h"
31
32
7.13k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
33
7.13k
  if (size < 3) {
34
2
    return 0;
35
2
  }
36
37
7.13k
  CrcGenerateTable();
38
7.13k
  Crc64GenerateTable();
39
40
7.13k
  SRes res;
41
7.13k
  CXzProps props;
42
7.13k
  XzProps_Init(&props);
43
44
7.13k
  switch (data[0]) {
45
1.36k
    case XZ_CHECK_NO:
46
1.57k
    case XZ_CHECK_CRC32:
47
1.99k
    case XZ_CHECK_CRC64:
48
2.72k
    case XZ_CHECK_SHA256:
49
2.72k
      props.checkId = data[0];
50
2.72k
      break;
51
7.13k
  }
52
53
7.13k
  switch (data[1]) {
54
584
    case XZ_ID_X86:
55
1.03k
    case XZ_ID_PPC:
56
1.30k
    case XZ_ID_IA64:
57
1.59k
    case XZ_ID_ARM:
58
1.74k
    case XZ_ID_ARMT:
59
2.21k
    case XZ_ID_SPARC:
60
2.21k
      props.filterProps.id = data[1];
61
2.21k
      props.filterProps.ipDefined = True;
62
2.21k
      break;
63
637
    case XZ_ID_Delta:
64
637
      props.filterProps.id = data[1];
65
637
      props.filterProps.delta = data[2];
66
637
      if (props.filterProps.delta == 0) {
67
1
        return 0;
68
1
      }
69
636
      break;
70
7.13k
  }
71
72
7.13k
  OutputBuffer out_buffer;
73
7.13k
  InputBuffer in_buffer(data, size);
74
7.13k
  CXzEncHandle enc;
75
7.13k
  enc = XzEnc_Create(&CommonAlloc, &CommonAlloc);
76
7.13k
  if (XzEnc_SetProps(enc, &props) != SZ_OK) {
77
0
    goto exit;
78
0
  }
79
80
7.13k
  XzEnc_SetDataSize(enc, size);
81
7.13k
  res = XzEnc_Encode(enc, out_buffer.stream(), in_buffer.stream(), nullptr);
82
7.13k
  assert(res == SZ_OK);
83
84
7.13k
  {
85
    // Decompress and compare with input data.
86
7.13k
    CXzDecMtProps dec_props;
87
7.13k
    XzDecMtProps_Init(&dec_props);
88
89
7.13k
    OutputBuffer dec_out_buffer;
90
7.13k
    InputBuffer dec_in_buffer(out_buffer.data(), out_buffer.size());
91
7.13k
    CXzStatInfo stats;
92
7.13k
    int isMt;
93
94
7.13k
    CXzDecMtHandle dec = XzDecMt_Create(&CommonAlloc, &CommonAlloc);
95
7.13k
    res = XzDecMt_Decode(dec, &dec_props, nullptr, 1, dec_out_buffer.stream(),
96
7.13k
        dec_in_buffer.stream(), &stats, &isMt, nullptr);
97
7.13k
    assert(res == SZ_OK);
98
7.13k
    assert(dec_out_buffer.size() == size);
99
7.13k
    assert(memcmp(data, dec_out_buffer.data(), size) == 0);
100
7.13k
    XzDecMt_Destroy(dec);
101
7.13k
  }
102
103
7.13k
exit:
104
7.13k
  XzEnc_Destroy(enc);
105
7.13k
  return 0;
106
7.13k
}