2019-05-02 18:32:32 +00:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
#include <filesystem>
|
2018-02-15 18:30:23 +00:00
|
|
|
#include <fstream>
|
2017-11-13 18:48:31 +00:00
|
|
|
#include <memory>
|
2018-01-18 23:17:44 +00:00
|
|
|
#include <string>
|
2019-05-02 18:32:32 +00:00
|
|
|
#include <system_error>
|
2017-11-13 18:48:31 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "rar.hpp"
|
|
|
|
|
2019-05-02 18:32:32 +00:00
|
|
|
namespace fs = std::__fs::filesystem;
|
|
|
|
|
2017-11-13 18:48:31 +00:00
|
|
|
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
|
2019-05-02 18:32:32 +00:00
|
|
|
// unrar likes to create files in the current directory.
|
|
|
|
// So, the following few lines created and 'cd' to a directory named 'o'
|
|
|
|
// in the current working directory.
|
|
|
|
std::error_code code, ok;
|
|
|
|
fs::path original_path = fs::current_path(code);
|
2019-05-10 19:35:08 +00:00
|
|
|
if (code != ok) return 0;
|
2019-05-02 18:32:32 +00:00
|
|
|
|
|
|
|
fs::path out_path = original_path / "o";
|
|
|
|
bool created = fs::create_directory(out_path, code);
|
2019-05-10 19:35:08 +00:00
|
|
|
if (code != ok) return 0;
|
2019-05-02 18:32:32 +00:00
|
|
|
|
|
|
|
fs::current_path(out_path, code);
|
2019-05-10 19:35:08 +00:00
|
|
|
if (code != ok) return 0;
|
2019-05-02 18:32:32 +00:00
|
|
|
|
2018-01-18 23:17:44 +00:00
|
|
|
static const std::string filename = "temp.rar";
|
2018-02-15 18:30:23 +00:00
|
|
|
std::ofstream file(filename,
|
|
|
|
std::ios::binary | std::ios::out | std::ios::trunc);
|
|
|
|
if (!file.is_open()) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
file.write(reinterpret_cast<const char *>(data), size);
|
|
|
|
file.close();
|
2017-11-13 18:48:31 +00:00
|
|
|
|
|
|
|
std::unique_ptr<CommandData> cmd_data(new CommandData);
|
|
|
|
cmd_data->ParseArg(const_cast<wchar_t *>(L"-p"));
|
|
|
|
cmd_data->ParseArg(const_cast<wchar_t *>(L"x"));
|
|
|
|
cmd_data->ParseDone();
|
2017-11-17 22:47:43 +00:00
|
|
|
std::wstring wide_filename(filename.begin(), filename.end());
|
2017-11-13 18:48:31 +00:00
|
|
|
cmd_data->AddArcName(wide_filename.c_str());
|
|
|
|
|
|
|
|
try {
|
|
|
|
CmdExtract extractor(cmd_data.get());
|
|
|
|
extractor.DoExtract();
|
|
|
|
} catch (...) {
|
|
|
|
}
|
|
|
|
|
2018-02-15 18:30:23 +00:00
|
|
|
unlink(filename.c_str());
|
2019-05-02 18:32:32 +00:00
|
|
|
|
|
|
|
// 'cd' back to the original directory and delete 'o' along with
|
|
|
|
// all its contents.
|
2019-05-10 19:35:08 +00:00
|
|
|
fs::current_path(original_path, code);
|
|
|
|
if (code != ok) return 0;
|
|
|
|
fs::remove_all(out_path, code);
|
|
|
|
if (code != ok) return 0;
|
2017-11-13 18:48:31 +00:00
|
|
|
return 0;
|
|
|
|
}
|