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 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
|
/* Copyright (C) Teemu Suutari */
#include <fstream>
#include <functional>
#include <new>
#include <memory>
#include <optional>
#include <string>
#include <vector>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <optional>
#include <sys/stat.h>
#include <ancient/ancient.hpp>
// enables scanning. Useful for testing/debugging. Not so useful otherwise
// Not good for default since creates dependency for dirent (i.e. windows blues)
//#define ENABLE_SCAN 1
#ifdef ENABLE_SCAN
#include <dirent.h>
#endif
std::unique_ptr<std::vector<uint8_t>> readFile(const std::string &fileName)
{
std::unique_ptr<std::vector<uint8_t>> ret=std::make_unique<std::vector<uint8_t>>();
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)
{
fprintf(stderr,"Could not read file %s\n",fileName.c_str());
return std::make_unique<std::vector<uint8_t>>();
}
return ret;
}
bool writeFile(const std::string &fileName,const uint8_t *data, size_t size)
{
bool ret=false;
std::ofstream file(fileName.c_str(),std::ios::out|std::ios::binary|std::ios::trunc);
if (file.is_open())
{
file.write(reinterpret_cast<const char*>(data),size);
ret=bool(file);
file.close();
}
if (!ret)
{
fprintf(stderr,"Could not write file %s\n",fileName.c_str());
}
return ret;
}
bool writeFile(const std::string &fileName,const std::vector<uint8_t> &content)
{
return writeFile(fileName,content.data(),content.size());
}
int main(int argc,char **argv)
{
auto usage=[]()
{
fprintf(stderr, "Ancient v2.2.0\n"
"Copyright (C) Teemu Suutari\n"
"\n"
"Usage: ancient i[dentify] packed_input_files...\n"
" - identifies compression used in a file(s)\n"
"Usage: ancient v[erify] packed_input_file unpacked_comparison_file\n"
" - verifies decompression against known good unpacked file\n"
"Usage: ancient d[ecompress] packed_input_file output_file\n"
" - decompresses single file\n");
#ifdef ENABLE_SCAN
fprintf(stderr, "Usage: ancient s[can] input_dir output_dir\n"
" - scans input directory recursively and stores all found\n"
" - known compressed streams to separate files in output directory\n");
#endif
};
if (argc<3)
{
usage();
return -1;
}
std::string cmd=argv[1];
if (cmd=="i" || cmd=="identify")
{
if (argc<3)
{
usage();
return -1;
}
for (int i=2;i<argc;i++)
{
auto packed{readFile(argv[i])};
std::optional<ancient::Decompressor> decompressor;
try
{
decompressor.emplace(*packed,true,true);
printf("Compression of %s is %s\n",argv[i],decompressor->getName().c_str());
} catch (const ancient::InvalidFormatError&)
{
fprintf(stderr,"Unknown or invalid compression format in file %s\n",argv[i]);
} catch (const ancient::VerificationError&)
{
fprintf(stderr,"Failed to validate file %s\n",argv[i]);
}
}
return 0;
} else if (cmd=="d" || cmd=="decompress" || cmd=="v" || cmd=="verify") {
if (argc!=4)
{
usage();
return -1;
}
auto packed{readFile(argv[2])};
std::optional<ancient::Decompressor> decompressor;
try
{
decompressor.emplace(*packed,true,true);
} catch (const ancient::InvalidFormatError&)
{
fprintf(stderr,"Unknown or invalid compression format in file %s\n",argv[2]);
return -1;
} catch (const ancient::VerificationError&)
{
fprintf(stderr,"Verify (packed) failed for %s\n",argv[2]);
return -1;
}
std::vector<uint8_t> raw;
try
{
raw=decompressor->decompress(true);
} catch (const ancient::DecompressionError&)
{
fprintf(stderr,"Decompression failed for %s\n",argv[2]);
return -1;
} catch (const ancient::VerificationError&)
{
fprintf(stderr,"Verify (raw) failed for %s\n",argv[2]);
return -1;
} catch (const std::bad_alloc&) {
fprintf(stderr,"Out of memory\n");
return -1;
}
if (cmd=="d" || cmd=="decompress")
{
if (decompressor->getImageOffset() || decompressor->getImageSize())
{
printf("File %s is disk image, decompressed stream offset is %zu, full image size is %zu, stream size is %zu\n",argv[2],decompressor->getImageOffset().value(),decompressor->getImageSize().value(),decompressor->getRawSize().value());
printf("!!! Please note !!!\n!!! The destination will be padded !!!\n\n");
}
if (decompressor->getImageSize())
{
std::vector<uint8_t> pad(decompressor->getImageSize().value());
std::memcpy(&pad[decompressor->getImageOffset()?decompressor->getImageOffset().value():0],raw.data(),raw.size());
writeFile(argv[3],pad);
} else {
writeFile(argv[3],raw);
}
return 0;
} else {
size_t actualSize=decompressor->getImageSize()?decompressor->getImageSize().value():raw.size();
auto verify{readFile(argv[3])};
if (verify->size()!=actualSize)
{
fprintf(stderr,"Verify failed for %s and %s - sizes differ\n",argv[2],argv[3]);
return -1;
}
size_t offset=decompressor->getImageOffset()?decompressor->getImageOffset().value():0;
for (size_t i=0;i<raw.size();i++)
{
if (raw.data()[i]!=verify->data()[i+offset])
{
fprintf(stderr,"Verify failed for %s and %s - contents differ @ %zu\n",argv[2],argv[3],i);
return -1;
}
}
printf("Files match!\n");
return 0;
}
}
#ifdef ENABLE_SCAN
else if (cmd=="s" || cmd=="scan") {
if (argc!=4)
{
usage();
return -1;
}
uint32_t fileIndex=0;
std::function<void(std::string)> processDir=[&](std::string inputDir)
{
auto opendir=[](const char *f)->DIR* {
return ::opendir(f);
};
auto closedir=[](DIR *d)->int {
return ::closedir(d);
};
std::unique_ptr<DIR,decltype(closedir)> dir{opendir(inputDir.c_str()),closedir};
if (dir)
{
while (struct dirent *de=::readdir(dir.get()))
{
std::string subName(de->d_name);
if (subName=="." || subName=="..") continue;
std::string name=inputDir+"/"+subName;
struct stat st;
if (stat(name.c_str(),&st)<0) continue;
if (st.st_mode&S_IFDIR)
{
processDir(name);
} else if (st.st_mode&S_IFREG) {
auto packed{readFile(name)};
for (size_t i=0;i<packed->size();)
{
// We will detect first, before trying the format for real
if (!ancient::Decompressor::detect(packed->data()+i,packed->size()-i))
{
i++;
continue;
}
try
{
ancient::Decompressor decompressor{packed->data()+i,packed->size()-i,false,true};
printf("trying %s\n",decompressor.getName().c_str());
if (!decompressor.getPackedSize())
{
// for formats that do not encode packed size.
// we will get it from decompressor
decompressor.decompress(true);
}
// final checks with the limited buffer and fresh decompressor
const uint8_t *finalData=packed->data()+i;
size_t finalSize=decompressor.getPackedSize().value();
ancient::Decompressor decompressor2{finalData,finalSize,true,true};
try
{
decompressor2.decompress(true);
} catch (const std::bad_alloc&) {
fprintf(stderr,"Out of memory\n");
i++;
continue;
}
std::string outputName=std::string(argv[3])+"/file"+std::to_string(fileIndex++)+".pack";
printf("Found compressed stream at %zu, size %zu in file %s with type '%s', storing it into %s\n",i,decompressor2.getPackedSize().value(),name.c_str(),decompressor2.getName().c_str(),outputName.c_str());
writeFile(outputName,finalData,finalSize);
i+=finalSize;
continue;
} catch (const ancient::Error&) {
// full steam ahead (with next offset)
} catch (const std::bad_alloc&) {
// full steam ahead (with next offset)
}
i++;
}
}
}
} else {
fprintf(stderr,"Could not process directory %s\n",inputDir.c_str());
}
};
processDir(std::string(argv[2]));
return 0;
}
#endif
else {
fprintf(stderr,"Unknown command\n");
usage();
return -1;
}
}
|