Files

26 lines
2.0 KiB
C++

// Offline, read-only ROOT export for all-locale FileDataID collision checks.
#include "CascLib.h"
#include <array>
#include <cctype>
#include <filesystem>
#include <fstream>
#include <stdexcept>
#include <string>
#include <vector>
#include <iostream>
int main(int argc,char** argv){try{
if(argc!=4)throw std::runtime_error("storage, ROOT CKey and output required");
auto storagePath=std::filesystem::canonical(argv[1]);auto output=std::filesystem::absolute(argv[3]).lexically_normal();
auto relative=output.lexically_relative(storagePath);if(!relative.empty()&&*relative.begin()!="..")throw std::runtime_error("output must be outside client storage");
std::string text(argv[2]);if(text.size()!=32)throw std::runtime_error("ROOT CKey length");
for(unsigned char c:text)if(!std::isxdigit(c))throw std::runtime_error("ROOT CKey hex");
std::array<unsigned char,16> key{};for(size_t i=0;i<16;++i)key[i]=static_cast<unsigned char>(std::stoul(text.substr(i*2,2),nullptr,16));
CASC_OPEN_STORAGE_ARGS args{};args.Size=sizeof(args);args.szLocalPath=argv[1];args.dwLocaleMask=CASC_LOCALE_ENUS;
HANDLE storage=nullptr,file=nullptr;if(!CascOpenStorageEx(nullptr,&args,false,&storage))throw std::runtime_error("offline CASC open");
if(!CascOpenFile(storage,key.data(),CASC_LOCALE_ENUS,CASC_OPEN_BY_CKEY|CASC_STRICT_DATA_CHECK,&file))throw std::runtime_error("ROOT CKey open");
DWORD high=0,size=CascGetFileSize(file,&high);if(high||size<24||size>134217728)throw std::runtime_error("ROOT size bound");
std::vector<unsigned char> bytes(size);DWORD read=0;if(!CascReadFile(file,bytes.data(),size,&read)||read!=size)throw std::runtime_error("ROOT strict read");
CascCloseFile(file);CascCloseStorage(storage);std::ofstream out(output,std::ios::binary);out.write(reinterpret_cast<const char*>(bytes.data()),bytes.size());if(!out)throw std::runtime_error("ROOT output");
std::cout<<"Exported ROOT bytes="<<bytes.size()<<"\n";return 0;
}catch(const std::exception& e){std::cerr<<e.what()<<"\n";return 1;}}