Coverage Report

Created: 2025-07-11 06:29

/src/lzma-fuzz/xzenc_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 <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
6.87k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
33
6.87k
  if (size < 3) {
34
2
    return 0;
35
2
  }
36
37
6.87k
  CrcGenerateTable();
38
6.87k
  Crc64GenerateTable();
39
40
6.87k
  SRes res;
41
6.87k
  CXzProps props;
42
6.87k
  XzProps_Init(&props);
43
44
6.87k
  switch (data[0]) {
45
1.36k
    case XZ_CHECK_NO:
46
1.59k
    case XZ_CHECK_CRC32:
47
1.99k
    case XZ_CHECK_CRC64:
48
2.66k
    case XZ_CHECK_SHA256:
49
2.66k
      props.checkId = data[0];
50
2.66k
      break;
51
6.87k
  }
52
53
6.87k
  switch (data[1]) {
54
550
    case XZ_ID_X86:
55
980
    case XZ_ID_PPC:
56
1.22k
    case XZ_ID_IA64:
57
1.49k
    case XZ_ID_ARM:
58
1.63k
    case XZ_ID_ARMT:
59
2.03k
    case XZ_ID_SPARC:
60
2.03k
      props.filterProps.id = data[1];
61
2.03k
      props.filterProps.ipDefined = True;
62
2.03k
      break;
63
585
    case XZ_ID_Delta:
64
585
      props.filterProps.id = data[1];
65
585
      props.filterProps.delta = data[2];
66
585
      if (props.filterProps.delta == 0) {
67
1
        return 0;
68
1
      }
69
584
      break;
70
6.87k
  }
71
72
6.87k
  OutputBuffer out_buffer;
73
6.87k
  InputBuffer in_buffer(data, size);
74
6.87k
  CXzEncHandle enc;
75
6.87k
  enc = XzEnc_Create(&CommonAlloc, &CommonAlloc);
76
6.87k
  if (XzEnc_SetProps(enc, &props) != SZ_OK) {
77
0
    goto exit;
78
0
  }
79
80
6.87k
  XzEnc_SetDataSize(enc, size);
81
6.87k
  res = XzEnc_Encode(enc, out_buffer.stream(), in_buffer.stream(), nullptr);
82
6.87k
  assert(res == SZ_OK);
83
84
6.87k
  {
85
    // Decompress and compare with input data.
86
6.87k
    CXzDecMtProps dec_props;
87
6.87k
    XzDecMtProps_Init(&dec_props);
88
89
6.87k
    OutputBuffer dec_out_buffer;
90
6.87k
    InputBuffer dec_in_buffer(out_buffer.data(), out_buffer.size());
91
6.87k
    CXzStatInfo stats;
92
6.87k
    int isMt;
93
94
6.87k
    CXzDecMtHandle dec = XzDecMt_Create(&CommonAlloc, &CommonAlloc);
95
6.87k
    res = XzDecMt_Decode(dec, &dec_props, nullptr, 1, dec_out_buffer.stream(),
96
6.87k
        dec_in_buffer.stream(), &stats, &isMt, nullptr);
97
6.87k
    assert(res == SZ_OK);
98
6.87k
    assert(dec_out_buffer.size() == size);
99
6.87k
    assert(memcmp(data, dec_out_buffer.data(), size) == 0);
100
6.87k
    XzDecMt_Destroy(dec);
101
6.87k
  }
102
103
6.87k
exit:
104
6.87k
  XzEnc_Destroy(enc);
105
6.87k
  return 0;
106
6.87k
}