| 1 |
/* Copyright (C) Teemu Suutari */ |
| 2 |
|
| 3 |
#include <cstdint> |
| 4 |
|
| 5 |
#include <memory> |
| 6 |
#include <fstream> |
| 7 |
#include <string> |
| 8 |
|
| 9 |
#include "common/MemoryBuffer.hpp" |
| 10 |
#include "common/SubBuffer.hpp" |
| 11 |
#include "common/StaticBuffer.hpp" |
| 12 |
#include "Decompressor.hpp" |
| 13 |
|
| 14 |
using namespace ancient::internal; |
| 15 |
|
| 16 |
std::shared_ptr<Buffer> readFile(const std::string &fileName) |
| 17 |
{ |
| 18 |
|
| 19 |
std::shared_ptr<Buffer> ret=std::make_shared<MemoryBuffer>(0); |
| 20 |
std::ifstream file(fileName.c_str(),std::ios::in|std::ios::binary); |
| 21 |
bool success=false; |
| 22 |
if (file.is_open()) |
| 23 |
{ |
| 24 |
file.seekg(0,std::ios::end); |
| 25 |
size_t length=size_t(file.tellg()); |
| 26 |
file.seekg(0,std::ios::beg); |
| 27 |
ret->resize(length); |
| 28 |
file.read(reinterpret_cast<char*>(ret->data()),length); |
| 29 |
success=bool(file); |
| 30 |
file.close(); |
| 31 |
} |
| 32 |
if (!success) |
| 33 |
{ |
| 34 |
return {}; |
| 35 |
} |
| 36 |
return ret; |
| 37 |
} |
| 38 |
|
| 39 |
int main(int argc,char **argv) |
| 40 |
{ |
| 41 |
(void)argc; |
| 42 |
#ifdef __AFL_HAVE_MANUAL_CONTROL |
| 43 |
__AFL_INIT(); |
| 44 |
#endif |
| 45 |
|
| 46 |
auto packed{readFile(argv[1])}; |
| 47 |
if (!packed) |
| 48 |
{ |
| 49 |
return -1; |
| 50 |
} |
| 51 |
std::shared_ptr<Decompressor> decompressor; |
| 52 |
try |
| 53 |
{ |
| 54 |
decompressor=Decompressor::create(*packed,true,true); |
| 55 |
} catch (const Decompressor::InvalidFormatError&) |
| 56 |
{ |
| 57 |
return -1; |
| 58 |
} catch (const Decompressor::VerificationError&) |
| 59 |
{ |
| 60 |
return -1; |
| 61 |
} |
| 62 |
|
| 63 |
std::shared_ptr<Buffer> raw; |
| 64 |
try |
| 65 |
{ |
| 66 |
raw=std::make_shared<MemoryBuffer>((decompressor->getRawSize())?decompressor->getRawSize():Decompressor::getMaxRawSize()); |
| 67 |
} catch (const Buffer::Error&) { |
| 68 |
return -1; |
| 69 |
} |
| 70 |
|
| 71 |
try |
| 72 |
{ |
| 73 |
decompressor->decompress(*raw,true); |
| 74 |
} catch (const Decompressor::DecompressionError&) |
| 75 |
{ |
| 76 |
return -1; |
| 77 |
} catch (const Decompressor::VerificationError&) |
| 78 |
{ |
| 79 |
return -1; |
| 80 |
} |
| 81 |
try |
| 82 |
{ |
| 83 |
raw->resize(decompressor->getRawSize()); |
| 84 |
} catch (const Buffer::Error&) { |
| 85 |
return -1; |
| 86 |
} |
| 87 |
|
| 88 |
if (decompressor->getImageOffset() || decompressor->getImageSize()) |
| 89 |
{ |
| 90 |
return -1; |
| 91 |
} |
| 92 |
|
| 93 |
return 0; |
| 94 |
} |