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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
|
// 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 <functional>
#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();
}
}
const mkvparser::BlockEntry* GetBlockEntryFromCues(
const void* ctx, const mkvparser::CuePoint* cue,
const mkvparser::CuePoint::TrackPosition* track_pos) {
const auto* const cues = static_cast<const mkvparser::Cues*>(ctx);
return cues->GetBlock(cue, track_pos);
}
const mkvparser::BlockEntry* GetBlockEntryFromCluster(
const void* ctx, const mkvparser::CuePoint* cue,
const mkvparser::CuePoint::TrackPosition* track_pos) {
if (track_pos == nullptr) {
return nullptr;
}
const auto* const cluster = static_cast<const mkvparser::Cluster*>(ctx);
const mkvparser::BlockEntry* block_entry =
cluster->GetEntry(*cue, *track_pos);
return block_entry;
}
void WalkCues(const mkvparser::Segment& segment,
std::function<const mkvparser::BlockEntry*(
const void*, const mkvparser::CuePoint*,
const mkvparser::CuePoint::TrackPosition*)>
get_block_entry,
const void* ctx) {
const mkvparser::Cues* const cues = segment.GetCues();
const mkvparser::Tracks* tracks = segment.GetTracks();
if (cues == nullptr || tracks == nullptr) {
return;
}
const unsigned long num_tracks = tracks->GetTracksCount();
for (const mkvparser::CuePoint* cue = cues->GetFirst(); cue != nullptr;
cue = cues->GetNext(cue)) {
for (unsigned long track_num = 0; track_num < num_tracks; ++track_num) {
const mkvparser::Track* const track = tracks->GetTrackByIndex(track_num);
const mkvparser::CuePoint::TrackPosition* const track_pos =
cue->Find(track);
const mkvparser::BlockEntry* block_entry =
get_block_entry(ctx, cue, track_pos);
static_cast<void>(block_entry);
}
}
}
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 || 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;
}
ParseCues(*segment);
WalkCues(*segment, GetBlockEntryFromCues, segment->GetCues());
const mkvparser::Cluster* cluster = segment->GetFirst();
while (cluster != nullptr && !cluster->EOS()) {
ParseCluster(*cluster);
WalkCues(*segment, GetBlockEntryFromCluster, cluster);
cluster = segment->GetNext(cluster);
}
return 0;
}
|