File: fuzz.cpp

package info (click to toggle)
ancient 2.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,168 kB
  • sloc: cpp: 15,188; makefile: 217; sh: 31
file content (94 lines) | stat: -rw-r--r-- 1,811 bytes parent folder | download | duplicates (2)
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
/* Copyright (C) Teemu Suutari */

#include <cstdint>

#include <memory>
#include <fstream>
#include <string>

#include "common/MemoryBuffer.hpp"
#include "common/SubBuffer.hpp"
#include "common/StaticBuffer.hpp"
#include "Decompressor.hpp"

using namespace ancient::internal;

std::shared_ptr<Buffer> readFile(const std::string &fileName)
{

	std::shared_ptr<Buffer> ret=std::make_shared<MemoryBuffer>(0);
	std::ifstream file(fileName.c_str(),std::ios::in|std::ios::binary);
	bool success=false;
	if (file.is_open())
	{
		file.seekg(0,std::ios::end);
		size_t length=size_t(file.tellg());
		file.seekg(0,std::ios::beg);
		ret->resize(length);
		file.read(reinterpret_cast<char*>(ret->data()),length);
		success=bool(file);
		file.close();
	}
	if (!success)
	{
		return {};
	}
	return ret;
}

int main(int argc,char **argv)
{
	(void)argc;
#ifdef __AFL_HAVE_MANUAL_CONTROL
	__AFL_INIT();
#endif
	
	auto packed{readFile(argv[1])};
	if (!packed)
	{
		return -1;
	}
	std::shared_ptr<Decompressor> decompressor;
	try
	{
		decompressor=Decompressor::create(*packed,true,true);
	} catch (const Decompressor::InvalidFormatError&)
	{
		return -1;
	} catch (const Decompressor::VerificationError&)
	{
		return -1;
	}

	std::shared_ptr<Buffer> raw;
	try
	{
		raw=std::make_shared<MemoryBuffer>((decompressor->getRawSize())?decompressor->getRawSize():Decompressor::getMaxRawSize());
	} catch (const Buffer::Error&) {
		return -1;
	}
		
	try
	{
		decompressor->decompress(*raw,true);
	} catch (const Decompressor::DecompressionError&)
	{
		return -1;
	} catch (const Decompressor::VerificationError&)
	{
		return -1;
	}
	try
	{
		raw->resize(decompressor->getRawSize());
	} catch (const Buffer::Error&) {
		return -1;
	}

	if (decompressor->getImageOffset() || decompressor->getImageSize())
	{
		return -1;
	}

	return 0;
}