/** \file QTarDecode.cpp \brief To read a tar data block \author alpha_one_x86 \licence GPL3, see the file COPYING */ #include "QTarDecode.h" #include #include #include #include static const std::regex isaunsignednumber("^[0-9]+$",std::regex::optimize); static const std::regex isaunoctalnumber("^[0-7]+$",std::regex::optimize); //static const char* const lut = "0123456789ABCDEF"; QTarDecode::QTarDecode() { error="Unknown error"; } uint64_t QTarDecode::octaltouint64(const std::string &string,bool *ok) { if(std::regex_match(string,isaunsignednumber)) { if(ok!=NULL) *ok=true; return std::stoi(string,0,8); } else { if(ok!=NULL) *ok=false; return 0; } } uint64_t QTarDecode::stringtouint64(const std::string &string,bool *ok) { if(std::regex_match(string,isaunsignednumber)) { if(ok!=NULL) *ok=true; return std::stoull(string); } else { if(ok!=NULL) *ok=false; return 0; } } std::string QTarDecode::errorString() { return error; } void QTarDecode::setErrorString(const std::string &error) { this->error=error; fileList.clear(); dataList.clear(); } bool QTarDecode::stringStartWith(std::string const &fullString, std::string const &starting) { if (fullString.length() >= starting.length()) { return (fullString.substr(0,starting.length())==starting); } else { return false; } } bool QTarDecode::decodeData(const std::vector &data) { setErrorString("Unknown error"); if(data.size()<1024) return false; unsigned int offset=0; while(offsetdata.size()) { setErrorString("The tar file seem be too short"); return false; } if(fileType=="0") //this code manage only the file, then only the file is returned { std::vector newdata; newdata.resize(finalSize); memcpy(newdata.data(),data.data()+512+offset,finalSize); fileList.push_back(fileName); dataList.push_back(newdata); } //calculate the offset for the next header bool retenu=((finalSize%512)!=0); //go to the next header offset+=512+(finalSize/512+retenu)*512; } if(fileList.size()>0) { std::string baseDirToTest=fileList.at(0); std::size_t found = baseDirToTest.find_last_of("/"); if(found!=std::string::npos && found>=baseDirToTest.size()) baseDirToTest.resize(baseDirToTest.size()-(baseDirToTest.size()-found)); bool isFoundForAllEntries=true; for (unsigned int i = 0; i < fileList.size(); ++i) if(!stringStartWith(fileList.at(i),baseDirToTest)) isFoundForAllEntries=false; if(isFoundForAllEntries) for (unsigned int i = 0; i < fileList.size(); ++i) fileList[i].erase(0,baseDirToTest.size()); } return true; } std::vector QTarDecode::getFileList() { return fileList; } std::vector > QTarDecode::getDataList() { return dataList; }