// hpatchz_test.cpp // Created by housisong on 2021/02/04. /* The MIT License (MIT) Copyright (c) 2012-2021 HouSisong Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include #include #include #include #include #include #include #include #include #include #include #define _IS_NEED_MAIN 0 #define _IS_NEED_PRINT_LOG 0 #include "../hpatchz.c" bool is_attack =true; size_t kLoopCount = 1000; const bool is_log_attack_tag=true; size_t cache_size_memory=0; int error_count=0; size_t test_i=0; size_t caseNum=0; const size_t kMaxSize=400*1024*1024; template inline static std::string _to_string(const T& v){ std::ostringstream os; os<& data,const char* fileName){ bool _isInClear=false; int result=0; const size_t kStepSize =1024*256; size_t readPos=0; size_t fileSize; hpatch_TFileStreamInput fStream; hpatch_TFileStreamInput_init(&fStream); check(hpatch_TFileStreamInput_open(&fStream,fileName),-1,"open file"); fileSize=(size_t)fStream.base.streamSize; check(fileSize==fStream.base.streamSize,-1,"file size"); try{ data.resize(fileSize); }catch(...){ check(false,-1,"memory alloc"); } while (readPosfileSize) readLen=fileSize-readPos; check(fStream.base.read(&fStream.base,readPos,data.data()+readPos, data.data()+readPos+readLen),-1,"read file data"); readPos+=readLen; } clear: _isInClear=true; check(hpatch_TFileStreamInput_close(&fStream),-1,"close file"); if (0!=result) throw result; } hpatch_BOOL _hpatchz(const hpatch_TStreamOutput* _out_newData, const hpatch_TStreamInput* oldData, const hpatch_TStreamInput* compressedDiff, TByte* temp_cache,TByte* temp_cache_end){ hpatch_TStreamOutput out_newData=*_out_newData; hpatch_TDecompress* decompressPlugin=0; hpatch_compressedDiffInfo diffInfo; if (!getCompressedDiffInfo(&diffInfo,compressedDiff)) return hpatch_FALSE; if (oldData->streamSize!=diffInfo.oldDataSize) return hpatch_FALSE; out_newData.streamSize=diffInfo.newDataSize; { if (strlen(diffInfo.compressType)>0){ #ifdef _CompressPlugin_zlib if ((!decompressPlugin)&&zlibDecompressPlugin.is_can_open(diffInfo.compressType)) decompressPlugin=&zlibDecompressPlugin; #endif #ifdef _CompressPlugin_bz2 if ((!decompressPlugin)&&bz2DecompressPlugin.is_can_open(diffInfo.compressType)) decompressPlugin=&bz2DecompressPlugin; #endif #ifdef _CompressPlugin_lzma if ((!decompressPlugin)&&lzmaDecompressPlugin.is_can_open(diffInfo.compressType)) decompressPlugin=&lzmaDecompressPlugin; #endif #if (defined(_CompressPlugin_lz4) || (defined(_CompressPlugin_lz4hc))) if ((!decompressPlugin)&&lz4DecompressPlugin.is_can_open(diffInfo.compressType)) decompressPlugin=&lz4DecompressPlugin; #endif #ifdef _CompressPlugin_zstd if ((!decompressPlugin)&&zstdDecompressPlugin.is_can_open(diffInfo.compressType)) decompressPlugin=&zstdDecompressPlugin; #endif } if (decompressPlugin==0) return hpatch_FALSE; } return patch_decompress_with_cache(&out_newData,oldData,compressedDiff,decompressPlugin, temp_cache,temp_cache_end); } int _attack_seed=1111; void testCaseByData(const std::string& tag,const std::vector& oldData,const std::vector& patData, const std::vector& newData,int& error_count){ double time0=clock_s(); static std::vector out_newData; static std::vector temp_cache; temp_cache.resize(cache_size_memory); if (out_newData.empty()) out_newData.reserve(kMaxSize); out_newData.resize(newData.size()); hpatch_TStreamOutput out_newStream; mem_as_hStreamOutput(&out_newStream,out_newData.data(),out_newData.data()+out_newData.size()); std::cout << tag; if (is_attack){ static std::vector _oldData; static std::vector _patData; if (_oldData.empty()) _oldData.reserve(kMaxSize); if (_patData.empty()) _patData.reserve(kMaxSize); _oldData.resize(oldData.size()); _patData.resize(patData.size()); hpatch_TStreamInput oldStream; hpatch_TStreamInput patStream; for (size_t i=0;i oldData; static std::vector patData; static std::vector newData; if (oldData.empty()) oldData.reserve(kMaxSize); if (patData.empty()) patData.reserve(kMaxSize); if (newData.empty()) newData.reserve(kMaxSize); if (oldPath.empty()) oldData.resize(0); else readFile(oldData,oldPath.c_str()); readFile(patData,patPath.c_str()); readFile(newData,newPath.c_str()); testCaseByData(tag,oldData,patData,newData,error_count); } void testCaseByLine(const std::string& dirPath,const std::string& line,int& error_count){ //1|12306_5.2.11.apk--12306_5.1.2.apk.hdiffz_-s-16.pat size_t pos0=line.find_first_of('|'); if (pos0==std::string::npos) return; pos0+=1; const std::string patFile=line.substr(pos0); size_t pos1=line.find(".apk--")+4; const std::string newFile=line.substr(pos0,pos1-pos0); pos0=pos1+2; pos1=line.find(".hdiffz_"); const std::string oldFile=line.substr(pos0,pos1-pos0); pos0=pos1+8; pos1=line.size()-4; const std::string diffOp=line.substr(pos0,pos1-pos0); std::string tag=newFile + " <- " + oldFile + " " + diffOp; testCaseByFile(tag,oldFile.empty()?oldFile:(dirPath+oldFile),dirPath+patFile,dirPath+newFile,error_count); } size_t aToSize(const char* str) { size_t result; if (!kmg_to_size(str, strlen(str), &result)) throw str; return result; } int main(int argc, const char* argv[]){ double time0=clock_s(); std::cout <<"hpatchz(attack code)\n"; std::cout <<"input \"patch_test.txt\" file path for test\n"; tcheck(argc==5); std::string txt=argv[1]; is_attack = (1==aToSize(argv[2])); kLoopCount= aToSize(argv[3]); cache_size_memory=aToSize(argv[4]); size_t pos=txt.find_last_of('\\'); if (pos==std::string::npos) pos=txt.find_last_of('/'); std::string dirPath=txt.substr(0,(pos==std::string::npos)?0:pos+1); std::vector lines; { std::fstream fs(txt); std::string line; while(std::getline(fs,line)){ size_t strLen=line.size(); if ((strLen>0)&&(line[strLen-1]=='\r')) line.resize(strLen-1); lines.push_back(line); } } for (size_t i=0;i