summaryrefslogtreecommitdiff
path: root/testing/mkvparser_fuzzer.cc
blob: a1a47b8c018d04a2fedf9d9d853da9e15edab5b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright (c) 2022 The WebM project authors. All Rights Reserved.
//
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file in the root of the source
// tree. An additional intellectual property rights grant can be found
// in the file PATENTS.  All contributing project authors may
// be found in the AUTHORS file in the root of the source tree.
#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <memory>
#include <new>

#include "mkvparser/mkvparser.h"
#include "mkvparser/mkvreader.h"

namespace {

class MemoryReader : public mkvparser::IMkvReader {
 public:
  MemoryReader(const uint8_t* data, size_t size) : data_(data), size_(size) {}

  int Read(long long pos, long len, unsigned char* buf) override {
    if (pos < 0 || len < 0) {
      abort();
    }
    if (pos >= size_ || size_ - pos < len) {
      return -1;
    }
    memcpy(buf, data_ + pos, len);
    return 0;
  }

  int Length(long long* total, long long* available) override {
    if (total != nullptr) {
      *total = size_;
    }
    if (available != nullptr) {
      *available = size_;
    }
    return 0;
  }

 private:
  const uint8_t* data_;
  size_t size_;
};

void ParseCues(const mkvparser::Segment& segment) {
  const mkvparser::Cues* const cues = segment.GetCues();
  if (cues == nullptr) {
    return;
  }

  while (!cues->DoneParsing()) {
    cues->LoadCuePoint();
  }
}

void ParseCluster(const mkvparser::Cluster& cluster) {
  const mkvparser::BlockEntry* block_entry;
  long status = cluster.GetFirst(block_entry);
  if (status != 0) {
    return;
  }

  while (block_entry != nullptr && !block_entry->EOS()) {
    const mkvparser::Block* const block = block_entry->GetBlock();
    if (block == nullptr) {
      return;
    }

    status = cluster.GetNext(block_entry, block_entry);
    if (status != 0) {
      return;
    }
  }
}

}  // namespace

extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
  MemoryReader reader(data, size);

  long long int pos = 0;
  std::unique_ptr<mkvparser::EBMLHeader> ebml_header(
      new (std::nothrow) mkvparser::EBMLHeader());  // NOLINT
  if (ebml_header->Parse(&reader, pos) < 0) {
    return 0;
  }

  mkvparser::Segment* temp_segment;
  if (mkvparser::Segment::CreateInstance(&reader, pos, temp_segment) != 0) {
    return 0;
  }
  std::unique_ptr<mkvparser::Segment> segment(temp_segment);

  if (segment->Load() < 0) {
    return 0;
  }

  const mkvparser::Cluster* cluster = segment->GetFirst();
  while (cluster != nullptr && !cluster->EOS()) {
    ParseCluster(*cluster);
    cluster = segment->GetNext(cluster);
  }
  ParseCues(*segment);

  return 0;
}