patch.c 105 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384
  1. //patch.c
  2. //
  3. /*
  4. The MIT License (MIT)
  5. Copyright (c) 2012-2018 HouSisong
  6. Permission is hereby granted, free of charge, to any person
  7. obtaining a copy of this software and associated documentation
  8. files (the "Software"), to deal in the Software without
  9. restriction, including without limitation the rights to use,
  10. copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the
  12. Software is furnished to do so, subject to the following
  13. conditions:
  14. The above copyright notice and this permission notice shall be
  15. included in all copies of the Software.
  16. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  18. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  20. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  21. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  22. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  23. OTHER DEALINGS IN THE SOFTWARE.
  24. */
  25. #include "patch.h"
  26. #if (_IS_NEED_CACHE_OLD_BY_COVERS)
  27. # include <stdlib.h> //qsort
  28. #endif
  29. #include "patch_private.h"
  30. #ifndef _IS_RUN_MEM_SAFE_CHECK
  31. # define _IS_RUN_MEM_SAFE_CHECK 1
  32. #endif
  33. #if (_IS_RUN_MEM_SAFE_CHECK)
  34. //__RUN_MEM_SAFE_CHECK用来启动内存访问越界检查,用以防御可能被意外或故意损坏的数据.
  35. # define __RUN_MEM_SAFE_CHECK
  36. #endif
  37. #ifdef __RUN_MEM_SAFE_CHECK
  38. # define _SAFE_CHECK_DO(code) do{ if (!(code)) return _hpatch_FALSE; }while(0)
  39. #else
  40. # define _SAFE_CHECK_DO(code) do{ code; }while(0)
  41. #endif
  42. #define _hpatch_FALSE hpatch_FALSE
  43. //hpatch_uint __hpatch_debug_check_false_x=0; //for debug
  44. //#define _hpatch_FALSE (1/__hpatch_debug_check_false_x)
  45. typedef unsigned char TByte;
  46. //变长正整数编码方案(x bit额外类型标志位,x<=7),从高位开始输出1--n byte:
  47. // x0* 7-x bit
  48. // x1* 0* 7+7-x bit
  49. // x1* 1* 0* 7+7+7-x bit
  50. // x1* 1* 1* 0* 7+7+7+7-x bit
  51. // x1* 1* 1* 1* 0* 7+7+7+7+7-x bit
  52. // ......
  53. hpatch_BOOL hpatch_packUIntWithTag(TByte** out_code,TByte* out_code_end,
  54. hpatch_StreamPos_t uValue,hpatch_uint highTag,
  55. const hpatch_uint kTagBit){//写入整数并前进指针.
  56. TByte* pcode=*out_code;
  57. const hpatch_StreamPos_t kMaxValueWithTag=((hpatch_StreamPos_t)1<<(7-kTagBit))-1;
  58. TByte codeBuf[hpatch_kMaxPackedUIntBytes];
  59. TByte* codeEnd=codeBuf;
  60. #ifdef __RUN_MEM_SAFE_CHECK
  61. //static const hpatch_uint kPackMaxTagBit=7;
  62. //assert((0<=kTagBit)&&(kTagBit<=kPackMaxTagBit));
  63. //assert((highTag>>kTagBit)==0);
  64. #endif
  65. while (uValue>kMaxValueWithTag) {
  66. *codeEnd=uValue&((1<<7)-1); ++codeEnd;
  67. uValue>>=7;
  68. }
  69. #ifdef __RUN_MEM_SAFE_CHECK
  70. if ((out_code_end-pcode)<(1+(codeEnd-codeBuf))) return _hpatch_FALSE;
  71. #endif
  72. *pcode=(TByte)( (TByte)uValue | (highTag<<(8-kTagBit))
  73. | (((codeBuf!=codeEnd)?1:0)<<(7-kTagBit)) );
  74. ++pcode;
  75. while (codeBuf!=codeEnd) {
  76. --codeEnd;
  77. *pcode=(*codeEnd) | (((codeBuf!=codeEnd)?1:0)<<7);
  78. ++pcode;
  79. }
  80. *out_code=pcode;
  81. return hpatch_TRUE;
  82. }
  83. hpatch_uint hpatch_packUIntWithTag_size(hpatch_StreamPos_t uValue,const hpatch_uint kTagBit){
  84. const hpatch_StreamPos_t kMaxValueWithTag=((hpatch_StreamPos_t)1<<(7-kTagBit))-1;
  85. hpatch_uint size=0;
  86. while (uValue>kMaxValueWithTag) {
  87. ++size;
  88. uValue>>=7;
  89. }
  90. ++size;
  91. return size;
  92. }
  93. hpatch_BOOL hpatch_unpackUIntWithTag(const TByte** src_code,const TByte* src_code_end,
  94. hpatch_StreamPos_t* result,const hpatch_uint kTagBit){//读出整数并前进指针.
  95. #ifdef __RUN_MEM_SAFE_CHECK
  96. //const hpatch_uint kPackMaxTagBit=7;
  97. #endif
  98. hpatch_StreamPos_t value;
  99. TByte code;
  100. const TByte* pcode=*src_code;
  101. #ifdef __RUN_MEM_SAFE_CHECK
  102. //assert(kTagBit<=kPackMaxTagBit);
  103. if (src_code_end<=pcode) return _hpatch_FALSE;
  104. #endif
  105. code=*pcode; ++pcode;
  106. value=code&((1<<(7-kTagBit))-1);
  107. if ((code&(1<<(7-kTagBit)))!=0){
  108. do {
  109. #ifdef __RUN_MEM_SAFE_CHECK
  110. if ((value>>(sizeof(value)*8-7))!=0) return _hpatch_FALSE;//cannot save 7bit
  111. if (src_code_end==pcode) return _hpatch_FALSE;
  112. #endif
  113. code=*pcode; ++pcode;
  114. value=(value<<7) | (code&((1<<7)-1));
  115. } while ((code&(1<<7))!=0);
  116. }
  117. (*src_code)=pcode;
  118. *result=value;
  119. return hpatch_TRUE;
  120. }
  121. static hpatch_BOOL _read_mem_stream(const hpatch_TStreamInput* stream,hpatch_StreamPos_t readFromPos,
  122. unsigned char* out_data,unsigned char* out_data_end){
  123. const unsigned char* src=(const unsigned char*)stream->streamImport;
  124. hpatch_size_t readLen=out_data_end-out_data;
  125. #ifdef __RUN_MEM_SAFE_CHECK
  126. if (readFromPos>stream->streamSize) return _hpatch_FALSE;
  127. if (readLen>(hpatch_StreamPos_t)(stream->streamSize-readFromPos)) return _hpatch_FALSE;
  128. #endif
  129. memcpy(out_data,src+readFromPos,readLen);
  130. return hpatch_TRUE;
  131. }
  132. const hpatch_TStreamInput* mem_as_hStreamInput(hpatch_TStreamInput* out_stream,
  133. const unsigned char* mem,const unsigned char* mem_end){
  134. out_stream->streamImport=(void*)mem;
  135. out_stream->streamSize=mem_end-mem;
  136. out_stream->read=_read_mem_stream;
  137. return out_stream;
  138. }
  139. static hpatch_BOOL _write_mem_stream(const hpatch_TStreamOutput* stream,hpatch_StreamPos_t writeToPos,
  140. const unsigned char* data,const unsigned char* data_end){
  141. unsigned char* out_dst=(unsigned char*)stream->streamImport;
  142. hpatch_size_t writeLen=data_end-data;
  143. #ifdef __RUN_MEM_SAFE_CHECK
  144. if (writeToPos>stream->streamSize) return _hpatch_FALSE;
  145. if (writeLen>(hpatch_StreamPos_t)(stream->streamSize-writeToPos)) return _hpatch_FALSE;
  146. #endif
  147. memcpy(out_dst+writeToPos,data,writeLen);
  148. return hpatch_TRUE;
  149. }
  150. typedef hpatch_BOOL (*_read_mem_stream_t)(const hpatch_TStreamOutput* stream,hpatch_StreamPos_t readFromPos,
  151. unsigned char* out_data,unsigned char* out_data_end);
  152. const hpatch_TStreamOutput* mem_as_hStreamOutput(hpatch_TStreamOutput* out_stream,
  153. unsigned char* mem,unsigned char* mem_end){
  154. out_stream->streamImport=mem;
  155. out_stream->streamSize=mem_end-mem;
  156. out_stream->read_writed=(_read_mem_stream_t)_read_mem_stream;
  157. out_stream->write=_write_mem_stream;
  158. return out_stream;
  159. }
  160. hpatch_BOOL hpatch_deccompress_mem(hpatch_TDecompress* decompressPlugin,
  161. const unsigned char* code,const unsigned char* code_end,
  162. unsigned char* out_data,unsigned char* out_data_end){
  163. hpatch_decompressHandle dec=0;
  164. hpatch_BOOL result,colose_rt;
  165. hpatch_TStreamInput codeStream;
  166. mem_as_hStreamInput(&codeStream,code,code_end);
  167. dec=decompressPlugin->open(decompressPlugin,(out_data_end-out_data),
  168. &codeStream,0,codeStream.streamSize);
  169. if (dec==0) return _hpatch_FALSE;
  170. result=decompressPlugin->decompress_part(dec,out_data,out_data_end);
  171. colose_rt=decompressPlugin->close(decompressPlugin,dec);
  172. assert(colose_rt);
  173. return result;
  174. }
  175. ////////
  176. //patch by memory
  177. static const hpatch_uint kSignTagBit=1;
  178. static hpatch_BOOL _bytesRle_load(TByte* out_data,TByte* out_dataEnd,
  179. const TByte* rle_code,const TByte* rle_code_end);
  180. static void addData(TByte* dst,const TByte* src,hpatch_size_t length);
  181. hpatch_inline
  182. static hpatch_BOOL _unpackUIntWithTag(const TByte** src_code,const TByte* src_code_end,
  183. hpatch_size_t* result,const hpatch_uint kTagBit){
  184. if (sizeof(hpatch_size_t)==sizeof(hpatch_StreamPos_t)){
  185. return hpatch_unpackUIntWithTag(src_code,src_code_end,(hpatch_StreamPos_t*)result,kTagBit);
  186. }else{
  187. hpatch_StreamPos_t u64=0;
  188. hpatch_BOOL rt=hpatch_unpackUIntWithTag(src_code,src_code_end,&u64,kTagBit);
  189. hpatch_size_t u=(hpatch_size_t)u64;
  190. *result=u;
  191. #ifdef __RUN_MEM_SAFE_CHECK
  192. return rt&(u==u64);
  193. #else
  194. return rt;
  195. #endif
  196. }
  197. }
  198. #define unpackUIntWithTagTo(puint,src_code,src_code_end,kTagBit) \
  199. _SAFE_CHECK_DO(_unpackUIntWithTag(src_code,src_code_end,puint,kTagBit))
  200. #define unpackUIntTo(puint,src_code,src_code_end) \
  201. unpackUIntWithTagTo(puint,src_code,src_code_end,0)
  202. hpatch_BOOL patch(TByte* out_newData,TByte* out_newData_end,
  203. const TByte* oldData,const TByte* oldData_end,
  204. const TByte* serializedDiff,const TByte* serializedDiff_end){
  205. const TByte *code_lengths, *code_lengths_end,
  206. *code_inc_newPos, *code_inc_newPos_end,
  207. *code_inc_oldPos, *code_inc_oldPos_end,
  208. *code_newDataDiff, *code_newDataDiff_end;
  209. hpatch_size_t coverCount;
  210. assert(out_newData<=out_newData_end);
  211. assert(oldData<=oldData_end);
  212. assert(serializedDiff<=serializedDiff_end);
  213. unpackUIntTo(&coverCount,&serializedDiff, serializedDiff_end);
  214. { //head
  215. hpatch_size_t lengthSize,inc_newPosSize,inc_oldPosSize,newDataDiffSize;
  216. unpackUIntTo(&lengthSize,&serializedDiff, serializedDiff_end);
  217. unpackUIntTo(&inc_newPosSize,&serializedDiff, serializedDiff_end);
  218. unpackUIntTo(&inc_oldPosSize,&serializedDiff, serializedDiff_end);
  219. unpackUIntTo(&newDataDiffSize,&serializedDiff, serializedDiff_end);
  220. #ifdef __RUN_MEM_SAFE_CHECK
  221. if (lengthSize>(hpatch_size_t)(serializedDiff_end-serializedDiff)) return _hpatch_FALSE;
  222. #endif
  223. code_lengths=serializedDiff; serializedDiff+=lengthSize;
  224. code_lengths_end=serializedDiff;
  225. #ifdef __RUN_MEM_SAFE_CHECK
  226. if (inc_newPosSize>(hpatch_size_t)(serializedDiff_end-serializedDiff)) return _hpatch_FALSE;
  227. #endif
  228. code_inc_newPos=serializedDiff; serializedDiff+=inc_newPosSize;
  229. code_inc_newPos_end=serializedDiff;
  230. #ifdef __RUN_MEM_SAFE_CHECK
  231. if (inc_oldPosSize>(hpatch_size_t)(serializedDiff_end-serializedDiff)) return _hpatch_FALSE;
  232. #endif
  233. code_inc_oldPos=serializedDiff; serializedDiff+=inc_oldPosSize;
  234. code_inc_oldPos_end=serializedDiff;
  235. #ifdef __RUN_MEM_SAFE_CHECK
  236. if (newDataDiffSize>(hpatch_size_t)(serializedDiff_end-serializedDiff)) return _hpatch_FALSE;
  237. #endif
  238. code_newDataDiff=serializedDiff; serializedDiff+=newDataDiffSize;
  239. code_newDataDiff_end=serializedDiff;
  240. }
  241. //decode rle ; rle data begin==cur serializedDiff;
  242. _SAFE_CHECK_DO(_bytesRle_load(out_newData, out_newData_end, serializedDiff, serializedDiff_end));
  243. { //patch
  244. const hpatch_size_t newDataSize=(hpatch_size_t)(out_newData_end-out_newData);
  245. hpatch_size_t oldPosBack=0;
  246. hpatch_size_t newPosBack=0;
  247. hpatch_size_t i;
  248. for (i=0; i<coverCount; ++i){
  249. hpatch_size_t copyLength,addLength, oldPos,inc_oldPos,inc_oldPos_sign;
  250. unpackUIntTo(&copyLength,&code_inc_newPos, code_inc_newPos_end);
  251. unpackUIntTo(&addLength,&code_lengths, code_lengths_end);
  252. #ifdef __RUN_MEM_SAFE_CHECK
  253. if (code_inc_oldPos>=code_inc_oldPos_end) return _hpatch_FALSE;
  254. #endif
  255. inc_oldPos_sign=(*code_inc_oldPos)>>(8-kSignTagBit);
  256. unpackUIntWithTagTo(&inc_oldPos,&code_inc_oldPos, code_inc_oldPos_end, kSignTagBit);
  257. if (inc_oldPos_sign==0)
  258. oldPos=oldPosBack+inc_oldPos;
  259. else
  260. oldPos=oldPosBack-inc_oldPos;
  261. if (copyLength>0){
  262. #ifdef __RUN_MEM_SAFE_CHECK
  263. if (copyLength>(hpatch_size_t)(newDataSize-newPosBack)) return _hpatch_FALSE;
  264. if (copyLength>(hpatch_size_t)(code_newDataDiff_end-code_newDataDiff)) return _hpatch_FALSE;
  265. #endif
  266. memcpy(out_newData+newPosBack,code_newDataDiff,copyLength);
  267. code_newDataDiff+=copyLength;
  268. newPosBack+=copyLength;
  269. }
  270. #ifdef __RUN_MEM_SAFE_CHECK
  271. if ( (addLength>(hpatch_size_t)(newDataSize-newPosBack)) ) return _hpatch_FALSE;
  272. if ( (oldPos>(hpatch_size_t)(oldData_end-oldData)) ||
  273. (addLength>(hpatch_size_t)(oldData_end-oldData-oldPos)) ) return _hpatch_FALSE;
  274. #endif
  275. addData(out_newData+newPosBack,oldData+oldPos,addLength);
  276. oldPosBack=oldPos;
  277. newPosBack+=addLength;
  278. }
  279. if (newPosBack<newDataSize){
  280. hpatch_size_t copyLength=newDataSize-newPosBack;
  281. #ifdef __RUN_MEM_SAFE_CHECK
  282. if (copyLength>(hpatch_size_t)(code_newDataDiff_end-code_newDataDiff)) return _hpatch_FALSE;
  283. #endif
  284. memcpy(out_newData+newPosBack,code_newDataDiff,copyLength);
  285. code_newDataDiff+=copyLength;
  286. //newPosBack=newDataSize;
  287. }
  288. }
  289. if ( (code_lengths==code_lengths_end)
  290. &&(code_inc_newPos==code_inc_newPos_end)
  291. &&(code_inc_oldPos==code_inc_oldPos_end)
  292. &&(code_newDataDiff==code_newDataDiff_end))
  293. return hpatch_TRUE;
  294. else
  295. return _hpatch_FALSE;
  296. }
  297. hpatch_inline static void addData(TByte* dst,const TByte* src,hpatch_size_t length){
  298. while (length--) { *dst++ += *src++; }
  299. }
  300. static hpatch_BOOL _bytesRle_load(TByte* out_data,TByte* out_dataEnd,
  301. const TByte* rle_code,const TByte* rle_code_end){
  302. const TByte* ctrlBuf,*ctrlBuf_end;
  303. hpatch_size_t ctrlSize;
  304. unpackUIntTo(&ctrlSize,&rle_code,rle_code_end);
  305. #ifdef __RUN_MEM_SAFE_CHECK
  306. if (ctrlSize>(hpatch_size_t)(rle_code_end-rle_code)) return _hpatch_FALSE;
  307. #endif
  308. ctrlBuf=rle_code;
  309. rle_code+=ctrlSize;
  310. ctrlBuf_end=rle_code;
  311. while (ctrlBuf_end-ctrlBuf>0){
  312. enum TByteRleType type=(enum TByteRleType)((*ctrlBuf)>>(8-kByteRleType_bit));
  313. hpatch_size_t length;
  314. unpackUIntWithTagTo(&length,&ctrlBuf,ctrlBuf_end,kByteRleType_bit);
  315. #ifdef __RUN_MEM_SAFE_CHECK
  316. if (length>=(hpatch_size_t)(out_dataEnd-out_data)) return _hpatch_FALSE;
  317. #endif
  318. ++length;
  319. switch (type){
  320. case kByteRleType_rle0:{
  321. memset(out_data,0,length);
  322. out_data+=length;
  323. }break;
  324. case kByteRleType_rle255:{
  325. memset(out_data,255,length);
  326. out_data+=length;
  327. }break;
  328. case kByteRleType_rle:{
  329. #ifdef __RUN_MEM_SAFE_CHECK
  330. if (1>(hpatch_size_t)(rle_code_end-rle_code)) return _hpatch_FALSE;
  331. #endif
  332. memset(out_data,*rle_code,length);
  333. ++rle_code;
  334. out_data+=length;
  335. }break;
  336. case kByteRleType_unrle:{
  337. #ifdef __RUN_MEM_SAFE_CHECK
  338. if (length>(hpatch_size_t)(rle_code_end-rle_code)) return _hpatch_FALSE;
  339. #endif
  340. memcpy(out_data,rle_code,length);
  341. rle_code+=length;
  342. out_data+=length;
  343. }break;
  344. }
  345. }
  346. if ( (ctrlBuf==ctrlBuf_end)
  347. &&(rle_code==rle_code_end)
  348. &&(out_data==out_dataEnd))
  349. return hpatch_TRUE;
  350. else
  351. return _hpatch_FALSE;
  352. }
  353. //----------------------
  354. //patch by stream
  355. static hpatch_BOOL _TStreamInputClip_read(const hpatch_TStreamInput* stream,
  356. hpatch_StreamPos_t readFromPos,
  357. unsigned char* out_data,unsigned char* out_data_end){
  358. TStreamInputClip* self=(TStreamInputClip*)stream->streamImport;
  359. #ifdef __RUN_MEM_SAFE_CHECK
  360. if (readFromPos+(out_data_end-out_data)>self->base.streamSize) return _hpatch_FALSE;
  361. #endif
  362. return self->srcStream->read(self->srcStream,readFromPos+self->clipBeginPos,out_data,out_data_end);
  363. }
  364. void TStreamInputClip_init(TStreamInputClip* self,const hpatch_TStreamInput* srcStream,
  365. hpatch_StreamPos_t clipBeginPos,hpatch_StreamPos_t clipEndPos){
  366. assert(self!=0);
  367. assert(srcStream!=0);
  368. assert(clipBeginPos<=clipEndPos);
  369. assert(clipEndPos<=srcStream->streamSize);
  370. self->srcStream=srcStream;
  371. self->clipBeginPos=clipBeginPos;
  372. self->base.streamImport=self;
  373. self->base.streamSize=clipEndPos-clipBeginPos;
  374. self->base.read=_TStreamInputClip_read;
  375. }
  376. static hpatch_BOOL _TStreamOutputClip_write(const hpatch_TStreamOutput* stream,
  377. hpatch_StreamPos_t writePos,
  378. const unsigned char* data,const unsigned char* data_end){
  379. TStreamOutputClip* self=(TStreamOutputClip*)stream->streamImport;
  380. #ifdef __RUN_MEM_SAFE_CHECK
  381. if (writePos+(data_end-data)>self->base.streamSize) return _hpatch_FALSE;
  382. #endif
  383. return self->srcStream->write(self->srcStream,writePos+self->clipBeginPos,data,data_end);
  384. }
  385. void TStreamOutputClip_init(TStreamOutputClip* self,const hpatch_TStreamOutput* srcStream,
  386. hpatch_StreamPos_t clipBeginPos,hpatch_StreamPos_t clipEndPos){
  387. assert(self!=0);
  388. assert(srcStream!=0);
  389. assert(clipBeginPos<=clipEndPos);
  390. assert(clipEndPos<=srcStream->streamSize);
  391. self->srcStream=srcStream;
  392. self->clipBeginPos=clipBeginPos;
  393. self->base.streamImport=self;
  394. self->base.streamSize=clipEndPos-clipBeginPos;
  395. ((TStreamInputClip*)self)->base.read=_TStreamInputClip_read;
  396. self->base.write=_TStreamOutputClip_write;
  397. }
  398. //assert(hpatch_kStreamCacheSize>=hpatch_kMaxPluginTypeLength+1);
  399. struct __private_hpatch_check_kMaxCompressTypeLength {
  400. char _[(hpatch_kStreamCacheSize>=(hpatch_kMaxPluginTypeLength+1))?1:-1];};
  401. hpatch_BOOL _TStreamCacheClip_readType_end(TStreamCacheClip* sclip,TByte endTag,
  402. char out_type[hpatch_kMaxPluginTypeLength+1]){
  403. const TByte* type_begin;
  404. hpatch_size_t i;
  405. hpatch_size_t readLen=hpatch_kMaxPluginTypeLength+1;
  406. if (readLen>_TStreamCacheClip_leaveSize(sclip))
  407. readLen=(hpatch_size_t)_TStreamCacheClip_leaveSize(sclip);
  408. type_begin=_TStreamCacheClip_accessData(sclip,readLen);
  409. if (type_begin==0) return _hpatch_FALSE;//not found
  410. for (i=0; i<readLen; ++i) {
  411. if (type_begin[i]!=endTag)
  412. continue;
  413. else{
  414. memcpy(out_type,type_begin,i); out_type[i]='\0';
  415. _TStreamCacheClip_skipData_noCheck(sclip,i+1);
  416. return hpatch_TRUE;
  417. }
  418. }
  419. return _hpatch_FALSE;//not found
  420. }
  421. hpatch_BOOL _TStreamCacheClip_updateCache(TStreamCacheClip* sclip){
  422. TByte* buf0=&sclip->cacheBuf[0];
  423. const hpatch_StreamPos_t streamSize=sclip->streamPos_end-sclip->streamPos;
  424. hpatch_size_t readSize=sclip->cacheBegin;
  425. if (readSize>streamSize)
  426. readSize=(hpatch_size_t)streamSize;
  427. if (readSize==0) return hpatch_TRUE;
  428. if (!_TStreamCacheClip_isCacheEmpty(sclip)){
  429. memmove(buf0+(hpatch_size_t)(sclip->cacheBegin-readSize),
  430. buf0+sclip->cacheBegin,_TStreamCacheClip_cachedSize(sclip));
  431. }
  432. if (!sclip->srcStream->read(sclip->srcStream,sclip->streamPos,
  433. buf0+(sclip->cacheEnd-readSize),buf0+sclip->cacheEnd))
  434. return _hpatch_FALSE;//read error
  435. sclip->cacheBegin-=readSize;
  436. sclip->streamPos+=readSize;
  437. return hpatch_TRUE;
  438. }
  439. hpatch_BOOL _TStreamCacheClip_skipData(TStreamCacheClip* sclip,hpatch_StreamPos_t skipLongSize){
  440. while (skipLongSize>0) {
  441. hpatch_size_t len=sclip->cacheEnd;
  442. if (len>skipLongSize)
  443. len=(hpatch_size_t)skipLongSize;
  444. if (_TStreamCacheClip_accessData(sclip,len)){
  445. _TStreamCacheClip_skipData_noCheck(sclip,len);
  446. skipLongSize-=len;
  447. }else{
  448. return _hpatch_FALSE;
  449. }
  450. }
  451. return hpatch_TRUE;
  452. }
  453. //assert(hpatch_kStreamCacheSize>=hpatch_kMaxPackedUIntBytes);
  454. struct __private_hpatch_check_hpatch_kMaxPackedUIntBytes {
  455. char _[(hpatch_kStreamCacheSize>=hpatch_kMaxPackedUIntBytes)?1:-1]; };
  456. hpatch_BOOL _TStreamCacheClip_unpackUIntWithTag(TStreamCacheClip* sclip,hpatch_StreamPos_t* result,const hpatch_uint kTagBit){
  457. TByte* curCode,*codeBegin;
  458. hpatch_size_t readSize=hpatch_kMaxPackedUIntBytes;
  459. const hpatch_StreamPos_t dataSize=_TStreamCacheClip_leaveSize(sclip);
  460. if (readSize>dataSize)
  461. readSize=(hpatch_size_t)dataSize;
  462. codeBegin=_TStreamCacheClip_accessData(sclip,readSize);
  463. if (codeBegin==0) return _hpatch_FALSE;
  464. curCode=codeBegin;
  465. _SAFE_CHECK_DO(hpatch_unpackUIntWithTag((const TByte**)&curCode,codeBegin+readSize,result,kTagBit));
  466. _TStreamCacheClip_skipData_noCheck(sclip,(hpatch_size_t)(curCode-codeBegin));
  467. return hpatch_TRUE;
  468. }
  469. hpatch_BOOL _TStreamCacheClip_readDataTo(TStreamCacheClip* sclip,TByte* out_buf,TByte* bufEnd){
  470. hpatch_size_t readLen=_TStreamCacheClip_cachedSize(sclip);
  471. hpatch_size_t outLen=bufEnd-out_buf;
  472. if (readLen>=outLen)
  473. readLen=outLen;
  474. memcpy(out_buf,&sclip->cacheBuf[sclip->cacheBegin],readLen);
  475. sclip->cacheBegin+=readLen;
  476. outLen-=readLen;
  477. if (outLen){
  478. out_buf += readLen;
  479. if (outLen<(sclip->cacheEnd>>1)){
  480. if (!_TStreamCacheClip_updateCache(sclip)) return _hpatch_FALSE;
  481. #ifdef __RUN_MEM_SAFE_CHECK
  482. if (outLen>_TStreamCacheClip_cachedSize(sclip)) return _hpatch_FALSE;
  483. #endif
  484. return _TStreamCacheClip_readDataTo(sclip, out_buf, bufEnd);
  485. }else{
  486. if (!sclip->srcStream->read(sclip->srcStream,sclip->streamPos,
  487. out_buf,bufEnd)) return _hpatch_FALSE;
  488. sclip->streamPos+=outLen;
  489. }
  490. }
  491. return hpatch_TRUE;
  492. }
  493. hpatch_BOOL _TStreamCacheClip_addDataTo(TStreamCacheClip* self,unsigned char* dst,hpatch_size_t addLen){
  494. const unsigned char* src=_TStreamCacheClip_readData(self,addLen);
  495. if (src==0) return _hpatch_FALSE;
  496. addData(dst,src,addLen);
  497. return hpatch_TRUE;
  498. }
  499. static hpatch_BOOL _decompress_read(const hpatch_TStreamInput* stream,
  500. const hpatch_StreamPos_t readFromPos,
  501. TByte* out_data,TByte* out_data_end){
  502. _TDecompressInputStream* self=(_TDecompressInputStream*)stream->streamImport;
  503. return self->decompressPlugin->decompress_part(self->decompressHandle,out_data,out_data_end);
  504. }
  505. hpatch_BOOL getStreamClip(TStreamCacheClip* out_clip,_TDecompressInputStream* out_stream,
  506. hpatch_StreamPos_t dataSize,hpatch_StreamPos_t compressedSize,
  507. const hpatch_TStreamInput* stream,hpatch_StreamPos_t* pCurStreamPos,
  508. hpatch_TDecompress* decompressPlugin,TByte* aCache,hpatch_size_t cacheSize){
  509. hpatch_StreamPos_t curStreamPos=*pCurStreamPos;
  510. if (compressedSize==0){
  511. #ifdef __RUN_MEM_SAFE_CHECK
  512. if ((curStreamPos+dataSize)<curStreamPos) return _hpatch_FALSE;
  513. if ((curStreamPos+dataSize)>stream->streamSize) return _hpatch_FALSE;
  514. #endif
  515. if (out_clip)
  516. _TStreamCacheClip_init(out_clip,stream,curStreamPos,curStreamPos+dataSize,aCache,cacheSize);
  517. curStreamPos+=dataSize;
  518. }else{
  519. #ifdef __RUN_MEM_SAFE_CHECK
  520. if ((curStreamPos+compressedSize)<curStreamPos) return _hpatch_FALSE;
  521. if ((curStreamPos+compressedSize)>stream->streamSize) return _hpatch_FALSE;
  522. #endif
  523. if (out_clip){
  524. out_stream->IInputStream.streamImport=out_stream;
  525. out_stream->IInputStream.streamSize=dataSize;
  526. out_stream->IInputStream.read=_decompress_read;
  527. out_stream->decompressPlugin=decompressPlugin;
  528. if (out_stream->decompressHandle==0){
  529. out_stream->decompressHandle=decompressPlugin->open(decompressPlugin,dataSize,stream,
  530. curStreamPos,curStreamPos+compressedSize);
  531. if (!out_stream->decompressHandle) return _hpatch_FALSE;
  532. }else{
  533. if (decompressPlugin->reset_code==0) return _hpatch_FALSE;
  534. if (!decompressPlugin->reset_code(out_stream->decompressHandle,dataSize,stream,curStreamPos,
  535. curStreamPos+compressedSize)) return _hpatch_FALSE;
  536. }
  537. _TStreamCacheClip_init(out_clip,&out_stream->IInputStream,0,
  538. out_stream->IInputStream.streamSize,aCache,cacheSize);
  539. }
  540. curStreamPos+=compressedSize;
  541. }
  542. *pCurStreamPos=curStreamPos;
  543. return hpatch_TRUE;
  544. }
  545. ///////
  546. static hpatch_inline hpatch_BOOL __TOutStreamCache_writeStream(_TOutStreamCache* self,const TByte* data,hpatch_size_t dataSize){
  547. if (!self->dstStream->write(self->dstStream,self->writeToPos,data,data+dataSize))
  548. return _hpatch_FALSE;
  549. self->writeToPos+=dataSize;
  550. return hpatch_TRUE;
  551. }
  552. hpatch_BOOL _TOutStreamCache_flush(_TOutStreamCache* self){
  553. hpatch_size_t curSize=self->cacheCur;
  554. if (curSize>0){
  555. if (!__TOutStreamCache_writeStream(self,self->cacheBuf,curSize))
  556. return _hpatch_FALSE;
  557. self->cacheCur=0;
  558. }
  559. return hpatch_TRUE;
  560. }
  561. hpatch_BOOL _TOutStreamCache_write(_TOutStreamCache* self,const TByte* data,hpatch_size_t dataSize){
  562. while (dataSize>0) {
  563. hpatch_size_t copyLen;
  564. hpatch_size_t curSize=self->cacheCur;
  565. if ((dataSize>=self->cacheEnd)&&(curSize==0)){
  566. return __TOutStreamCache_writeStream(self,data,dataSize);
  567. }
  568. copyLen=self->cacheEnd-curSize;
  569. copyLen=(copyLen<=dataSize)?copyLen:dataSize;
  570. memcpy(self->cacheBuf+curSize,data,copyLen);
  571. self->cacheCur=curSize+copyLen;
  572. data+=copyLen;
  573. dataSize-=copyLen;
  574. if (self->cacheCur==self->cacheEnd){
  575. if (!_TOutStreamCache_flush(self))
  576. return _hpatch_FALSE;
  577. }
  578. }
  579. return hpatch_TRUE;
  580. }
  581. hpatch_BOOL _TOutStreamCache_fill(_TOutStreamCache* self,hpatch_byte fillValue,hpatch_StreamPos_t fillLength){
  582. while (fillLength>0){
  583. hpatch_size_t curSize=self->cacheCur;
  584. hpatch_size_t runStep=self->cacheEnd-curSize;
  585. runStep=(runStep<=fillLength)?runStep:(hpatch_size_t)fillLength;
  586. memset(self->cacheBuf+curSize,fillValue,runStep);
  587. self->cacheCur=curSize+runStep;
  588. fillLength-=runStep;
  589. if (self->cacheCur==self->cacheEnd){
  590. if (!_TOutStreamCache_flush(self))
  591. return _hpatch_FALSE;
  592. }
  593. }
  594. return hpatch_TRUE;
  595. }
  596. hpatch_BOOL _TOutStreamCache_copyFromStream(_TOutStreamCache* self,const hpatch_TStreamInput* src,
  597. hpatch_StreamPos_t srcPos,hpatch_StreamPos_t copyLength){
  598. while (copyLength>0){
  599. hpatch_size_t curSize=self->cacheCur;
  600. hpatch_size_t runStep=self->cacheEnd-curSize;
  601. hpatch_byte* buf=self->cacheBuf+curSize;
  602. runStep=(runStep<=copyLength)?runStep:(hpatch_size_t)copyLength;
  603. if (!src->read(src,srcPos,buf,buf+runStep))
  604. return _hpatch_FALSE;
  605. srcPos+=runStep;
  606. self->cacheCur=curSize+runStep;
  607. copyLength-=runStep;
  608. if (self->cacheCur==self->cacheEnd){
  609. if (!_TOutStreamCache_flush(self))
  610. return _hpatch_FALSE;
  611. }
  612. }
  613. return hpatch_TRUE;
  614. }
  615. hpatch_BOOL _TOutStreamCache_copyFromClip(_TOutStreamCache* self,TStreamCacheClip* src,hpatch_StreamPos_t copyLength){
  616. while (copyLength>0){
  617. const TByte* data;
  618. hpatch_size_t runStep=(src->cacheEnd<=copyLength)?src->cacheEnd:(hpatch_size_t)copyLength;
  619. data=_TStreamCacheClip_readData(src,runStep);
  620. if (data==0) return
  621. _hpatch_FALSE;
  622. if (!_TOutStreamCache_write(self,data,runStep))
  623. return _hpatch_FALSE;
  624. copyLength-=runStep;
  625. }
  626. return hpatch_TRUE;
  627. }
  628. hpatch_BOOL _TOutStreamCache_copyFromSelf(_TOutStreamCache* self,hpatch_StreamPos_t aheadLength,hpatch_StreamPos_t copyLength){
  629. // [ writed ]
  630. // [ cached buf | empty buf ]
  631. const hpatch_TStreamInput* src=(const hpatch_TStreamInput*)self->dstStream;
  632. hpatch_StreamPos_t srcPos=self->writeToPos+self->cacheCur-aheadLength;
  633. if (src->read==0) //can't read
  634. return _hpatch_FALSE;
  635. if ((aheadLength<1)|(aheadLength>self->writeToPos+self->cacheCur))
  636. return _hpatch_FALSE;
  637. if (srcPos+copyLength<=self->writeToPos){//copy from stream
  638. // [ copyLength ]
  639. __copy_in_stream:
  640. return _TOutStreamCache_copyFromStream(self,src,srcPos,copyLength);
  641. }else if (srcPos>=self->writeToPos){ //copy in mem
  642. // [ copyLength ]
  643. __copy_in_mem:
  644. while (copyLength>0){
  645. hpatch_byte* dstBuf=self->cacheBuf+self->cacheCur;
  646. hpatch_byte* srcBuf=dstBuf-(hpatch_size_t)aheadLength;
  647. hpatch_size_t runLen=(self->cacheCur+copyLength<=self->cacheEnd)?(hpatch_size_t)copyLength:(self->cacheEnd-self->cacheCur);
  648. hpatch_size_t i;
  649. for (i=0;i<runLen;i++)
  650. dstBuf[i]=srcBuf[i];
  651. copyLength-=runLen;
  652. self->cacheCur+=runLen;
  653. if (self->cacheCur==self->cacheEnd){
  654. if (!_TOutStreamCache_flush(self))
  655. return _hpatch_FALSE;
  656. runLen=(hpatch_size_t)((aheadLength<=copyLength)?aheadLength:copyLength);
  657. memmove(self->cacheBuf,self->cacheBuf+self->cacheEnd-(hpatch_size_t)aheadLength,runLen);
  658. self->cacheCur=runLen;
  659. copyLength-=runLen;
  660. }else{
  661. assert(copyLength==0);
  662. }
  663. }
  664. return hpatch_TRUE;
  665. }else if (self->writeToPos+self->cacheCur<=srcPos+self->cacheEnd){
  666. // small data in stream,can as copy in mem
  667. hpatch_byte* dstBuf=self->cacheBuf+self->cacheCur;
  668. hpatch_size_t runLen=(hpatch_size_t)(self->writeToPos-srcPos);
  669. if (!src->read(src,srcPos,dstBuf,dstBuf+runLen))
  670. return _hpatch_FALSE;
  671. //srcPos+=runLen; //not used
  672. copyLength-=runLen;
  673. self->cacheCur+=runLen;
  674. if (self->cacheCur==self->cacheEnd){
  675. while (hpatch_TRUE){
  676. if (self->cacheCur==self->cacheEnd){
  677. if (!_TOutStreamCache_flush(self))
  678. return _hpatch_FALSE;
  679. }
  680. if (copyLength>0){
  681. runLen=(self->cacheEnd<=copyLength)?self->cacheEnd:(hpatch_size_t)copyLength;
  682. //srcPos+=runLen; //not used
  683. copyLength-=runLen;
  684. self->cacheCur=runLen;
  685. }else{
  686. return hpatch_TRUE;
  687. }
  688. }
  689. }else{
  690. goto __copy_in_mem;
  691. }
  692. }else{
  693. goto __copy_in_stream;
  694. }
  695. }
  696. typedef struct _TBytesRle_load_stream{
  697. hpatch_StreamPos_t memCopyLength;
  698. hpatch_StreamPos_t memSetLength;
  699. TByte memSetValue;
  700. TStreamCacheClip ctrlClip;
  701. TStreamCacheClip rleCodeClip;
  702. } _TBytesRle_load_stream;
  703. hpatch_inline
  704. static void _TBytesRle_load_stream_init(_TBytesRle_load_stream* loader){
  705. loader->memSetLength=0;
  706. loader->memSetValue=0;//nil;
  707. loader->memCopyLength=0;
  708. _TStreamCacheClip_init(&loader->ctrlClip,0,0,0,0,0);
  709. _TStreamCacheClip_init(&loader->rleCodeClip,0,0,0,0,0);
  710. }
  711. hpatch_inline static void memSet_add(TByte* dst,const TByte src,hpatch_size_t length){
  712. while (length--) { (*dst++) += src; }
  713. }
  714. static hpatch_BOOL _TBytesRle_load_stream_mem_add(_TBytesRle_load_stream* loader,
  715. hpatch_size_t* _decodeSize,TByte** _out_data){
  716. hpatch_size_t decodeSize=*_decodeSize;
  717. TByte* out_data=*_out_data;
  718. TStreamCacheClip* rleCodeClip=&loader->rleCodeClip;
  719. hpatch_StreamPos_t memSetLength=loader->memSetLength;
  720. if (memSetLength!=0){
  721. hpatch_size_t memSetStep=((memSetLength<=decodeSize)?(hpatch_size_t)memSetLength:decodeSize);
  722. const TByte byteSetValue=loader->memSetValue;
  723. if (out_data!=0){
  724. if (byteSetValue!=0)
  725. memSet_add(out_data,byteSetValue,memSetStep);
  726. out_data+=memSetStep;
  727. }
  728. decodeSize-=memSetStep;
  729. loader->memSetLength=memSetLength-memSetStep;
  730. }
  731. while ((loader->memCopyLength>0)&&(decodeSize>0)) {
  732. TByte* rleData;
  733. hpatch_size_t decodeStep=rleCodeClip->cacheEnd;
  734. if (decodeStep>loader->memCopyLength)
  735. decodeStep=(hpatch_size_t)loader->memCopyLength;
  736. if (decodeStep>decodeSize)
  737. decodeStep=decodeSize;
  738. rleData=_TStreamCacheClip_readData(rleCodeClip,decodeStep);
  739. if (rleData==0) return _hpatch_FALSE;
  740. if (out_data){
  741. addData(out_data,rleData,decodeStep);
  742. out_data+=decodeStep;
  743. }
  744. decodeSize-=decodeStep;
  745. loader->memCopyLength-=decodeStep;
  746. }
  747. *_decodeSize=decodeSize;
  748. *_out_data=out_data;
  749. return hpatch_TRUE;
  750. }
  751. hpatch_inline
  752. static hpatch_BOOL _TBytesRle_load_stream_isFinish(const _TBytesRle_load_stream* loader){
  753. return(loader->memSetLength==0)
  754. &&(loader->memCopyLength==0)
  755. &&(_TStreamCacheClip_isFinish(&loader->rleCodeClip))
  756. &&(_TStreamCacheClip_isFinish(&loader->ctrlClip));
  757. }
  758. #define _clip_unpackUIntWithTagTo(puint,sclip,kTagBit) \
  759. { if (!_TStreamCacheClip_unpackUIntWithTag(sclip,puint,kTagBit)) return _hpatch_FALSE; }
  760. #define _clip_unpackUIntTo(puint,sclip) _clip_unpackUIntWithTagTo(puint,sclip,0)
  761. static hpatch_BOOL _TBytesRle_load_stream_decode_add(_TBytesRle_load_stream* loader,
  762. TByte* out_data,hpatch_size_t decodeSize){
  763. if (!_TBytesRle_load_stream_mem_add(loader,&decodeSize,&out_data))
  764. return _hpatch_FALSE;
  765. while ((decodeSize>0)&&(!_TStreamCacheClip_isFinish(&loader->ctrlClip))){
  766. enum TByteRleType type;
  767. hpatch_StreamPos_t length;
  768. const TByte* pType=_TStreamCacheClip_accessData(&loader->ctrlClip,1);
  769. if (pType==0) return _hpatch_FALSE;
  770. type=(enum TByteRleType)((*pType)>>(8-kByteRleType_bit));
  771. _clip_unpackUIntWithTagTo(&length,&loader->ctrlClip,kByteRleType_bit);
  772. ++length;
  773. switch (type){
  774. case kByteRleType_rle0:{
  775. loader->memSetLength=length;
  776. loader->memSetValue=0;
  777. }break;
  778. case kByteRleType_rle255:{
  779. loader->memSetLength=length;
  780. loader->memSetValue=255;
  781. }break;
  782. case kByteRleType_rle:{
  783. const TByte* pSetValue=_TStreamCacheClip_readData(&loader->rleCodeClip,1);
  784. if (pSetValue==0) return _hpatch_FALSE;
  785. loader->memSetValue=*pSetValue;
  786. loader->memSetLength=length;
  787. }break;
  788. case kByteRleType_unrle:{
  789. loader->memCopyLength=length;
  790. }break;
  791. }
  792. if (!_TBytesRle_load_stream_mem_add(loader,&decodeSize,&out_data)) return _hpatch_FALSE;
  793. }
  794. if (decodeSize==0)
  795. return hpatch_TRUE;
  796. else
  797. return _hpatch_FALSE;
  798. }
  799. #define _TBytesRle_load_stream_decode_skip(loader,decodeSize) \
  800. _TBytesRle_load_stream_decode_add(loader,0,decodeSize)
  801. static hpatch_BOOL _patch_add_old_with_rle(_TOutStreamCache* outCache,_TBytesRle_load_stream* rle_loader,
  802. const hpatch_TStreamInput* old,hpatch_StreamPos_t oldPos,
  803. hpatch_StreamPos_t addLength,TByte* aCache,hpatch_size_t aCacheSize){
  804. while (addLength>0){
  805. hpatch_size_t decodeStep=aCacheSize;
  806. if (decodeStep>addLength)
  807. decodeStep=(hpatch_size_t)addLength;
  808. if (!old->read(old,oldPos,aCache,aCache+decodeStep)) return _hpatch_FALSE;
  809. if (!_TBytesRle_load_stream_decode_add(rle_loader,aCache,decodeStep)) return _hpatch_FALSE;
  810. if (!_TOutStreamCache_write(outCache,aCache,decodeStep)) return _hpatch_FALSE;
  811. oldPos+=decodeStep;
  812. addLength-=decodeStep;
  813. }
  814. return hpatch_TRUE;
  815. }
  816. typedef struct _TCovers{
  817. hpatch_TCovers ICovers;
  818. hpatch_StreamPos_t coverCount;
  819. hpatch_StreamPos_t oldPosBack;
  820. hpatch_StreamPos_t newPosBack;
  821. TStreamCacheClip* code_inc_oldPosClip;
  822. TStreamCacheClip* code_inc_newPosClip;
  823. TStreamCacheClip* code_lengthsClip;
  824. hpatch_BOOL isOldPosBackNeedAddLength;
  825. } _TCovers;
  826. static hpatch_StreamPos_t _covers_leaveCoverCount(const hpatch_TCovers* covers){
  827. const _TCovers* self=(const _TCovers*)covers;
  828. return self->coverCount;
  829. }
  830. static hpatch_BOOL _covers_close_nil(hpatch_TCovers* covers){
  831. //empty
  832. return hpatch_TRUE;
  833. }
  834. static hpatch_BOOL _covers_read_cover(hpatch_TCovers* covers,hpatch_TCover* out_cover){
  835. _TCovers* self=(_TCovers*)covers;
  836. hpatch_StreamPos_t oldPosBack=self->oldPosBack;
  837. hpatch_StreamPos_t newPosBack=self->newPosBack;
  838. hpatch_StreamPos_t coverCount=self->coverCount;
  839. if (coverCount>0)
  840. self->coverCount=coverCount-1;
  841. else
  842. return _hpatch_FALSE;
  843. {
  844. hpatch_StreamPos_t copyLength,coverLength, oldPos,inc_oldPos;
  845. TByte inc_oldPos_sign;
  846. const TByte* pSign=_TStreamCacheClip_accessData(self->code_inc_oldPosClip,1);
  847. if (pSign)
  848. inc_oldPos_sign=(*pSign)>>(8-kSignTagBit);
  849. else
  850. return _hpatch_FALSE;
  851. _clip_unpackUIntWithTagTo(&inc_oldPos,self->code_inc_oldPosClip,kSignTagBit);
  852. oldPos=(inc_oldPos_sign==0)?(oldPosBack+inc_oldPos):(oldPosBack-inc_oldPos);
  853. _clip_unpackUIntTo(&copyLength,self->code_inc_newPosClip);
  854. _clip_unpackUIntTo(&coverLength,self->code_lengthsClip);
  855. newPosBack+=copyLength;
  856. oldPosBack=oldPos;
  857. oldPosBack+=(self->isOldPosBackNeedAddLength)?coverLength:0;
  858. out_cover->oldPos=oldPos;
  859. out_cover->newPos=newPosBack;
  860. out_cover->length=coverLength;
  861. newPosBack+=coverLength;
  862. }
  863. self->oldPosBack=oldPosBack;
  864. self->newPosBack=newPosBack;
  865. return hpatch_TRUE;
  866. }
  867. static hpatch_BOOL _covers_is_finish(const struct hpatch_TCovers* covers){
  868. _TCovers* self=(_TCovers*)covers;
  869. return _TStreamCacheClip_isFinish(self->code_lengthsClip)
  870. && _TStreamCacheClip_isFinish(self->code_inc_newPosClip)
  871. && _TStreamCacheClip_isFinish(self->code_inc_oldPosClip);
  872. }
  873. static void _covers_init(_TCovers* covers,hpatch_StreamPos_t coverCount,
  874. TStreamCacheClip* code_inc_oldPosClip,
  875. TStreamCacheClip* code_inc_newPosClip,
  876. TStreamCacheClip* code_lengthsClip,
  877. hpatch_BOOL isOldPosBackNeedAddLength){
  878. covers->ICovers.leave_cover_count=_covers_leaveCoverCount;
  879. covers->ICovers.read_cover=_covers_read_cover;
  880. covers->ICovers.is_finish=_covers_is_finish;
  881. covers->ICovers.close=_covers_close_nil;
  882. covers->coverCount=coverCount;
  883. covers->newPosBack=0;
  884. covers->oldPosBack=0;
  885. covers->code_inc_oldPosClip=code_inc_oldPosClip;
  886. covers->code_inc_newPosClip=code_inc_newPosClip;
  887. covers->code_lengthsClip=code_lengthsClip;
  888. covers->isOldPosBackNeedAddLength=isOldPosBackNeedAddLength;
  889. }
  890. static hpatch_BOOL _rle_decode_skip(struct _TBytesRle_load_stream* rle_loader,hpatch_StreamPos_t copyLength){
  891. while (copyLength>0) {
  892. hpatch_size_t len=(~(hpatch_size_t)0);
  893. if (len>copyLength)
  894. len=(hpatch_size_t)copyLength;
  895. if (!_TBytesRle_load_stream_decode_skip(rle_loader,len)) return _hpatch_FALSE;
  896. copyLength-=len;
  897. }
  898. return hpatch_TRUE;
  899. }
  900. static hpatch_BOOL patchByClip(_TOutStreamCache* outCache,
  901. const hpatch_TStreamInput* oldData,
  902. hpatch_TCovers* covers,
  903. TStreamCacheClip* code_newDataDiffClip,
  904. struct _TBytesRle_load_stream* rle_loader,
  905. TByte* temp_cache,hpatch_size_t cache_size){
  906. const hpatch_StreamPos_t newDataSize=_TOutStreamCache_leaveSize(outCache);
  907. const hpatch_StreamPos_t oldDataSize=oldData->streamSize;
  908. hpatch_StreamPos_t coverCount=covers->leave_cover_count(covers);
  909. hpatch_StreamPos_t newPosBack=0;
  910. assert(cache_size>=hpatch_kMaxPackedUIntBytes);
  911. while (coverCount--){
  912. hpatch_TCover cover;
  913. if(!covers->read_cover(covers,&cover)) return _hpatch_FALSE;
  914. #ifdef __RUN_MEM_SAFE_CHECK
  915. if (cover.newPos<newPosBack) return _hpatch_FALSE;
  916. if (cover.length>(hpatch_StreamPos_t)(newDataSize-cover.newPos)) return _hpatch_FALSE;
  917. if (cover.oldPos>oldDataSize) return _hpatch_FALSE;
  918. if (cover.length>(hpatch_StreamPos_t)(oldDataSize-cover.oldPos)) return _hpatch_FALSE;
  919. #endif
  920. if (newPosBack<cover.newPos){
  921. hpatch_StreamPos_t copyLength=cover.newPos-newPosBack;
  922. if (!_TOutStreamCache_copyFromClip(outCache,code_newDataDiffClip,copyLength)) return _hpatch_FALSE;
  923. if (!_rle_decode_skip(rle_loader,copyLength)) return _hpatch_FALSE;
  924. }
  925. if (!_patch_add_old_with_rle(outCache,rle_loader,oldData,cover.oldPos,cover.length,
  926. temp_cache,cache_size)) return _hpatch_FALSE;
  927. newPosBack=cover.newPos+cover.length;
  928. }
  929. if (newPosBack<newDataSize){
  930. hpatch_StreamPos_t copyLength=newDataSize-newPosBack;
  931. if (!_TOutStreamCache_copyFromClip(outCache,code_newDataDiffClip,copyLength)) return _hpatch_FALSE;
  932. if (!_rle_decode_skip(rle_loader,copyLength)) return _hpatch_FALSE;
  933. newPosBack=newDataSize;
  934. }
  935. if (!_TOutStreamCache_flush(outCache))
  936. return _hpatch_FALSE;
  937. if ( _TBytesRle_load_stream_isFinish(rle_loader)
  938. && covers->is_finish(covers)
  939. && _TOutStreamCache_isFinish(outCache)
  940. && _TStreamCacheClip_isFinish(code_newDataDiffClip)
  941. && (newPosBack==newDataSize) )
  942. return hpatch_TRUE;
  943. else
  944. return _hpatch_FALSE;
  945. }
  946. #define _kCachePatCount 8
  947. #define _cache_alloc(dst,dst_type,memSize,temp_cache,temp_cache_end){ \
  948. if ((hpatch_size_t)(temp_cache_end-temp_cache) < \
  949. sizeof(hpatch_StreamPos_t)+(memSize)) return hpatch_FALSE; \
  950. (dst)=(dst_type*)_hpatch_align_upper(temp_cache,sizeof(hpatch_StreamPos_t));\
  951. temp_cache=(TByte*)(dst)+(hpatch_size_t)(memSize); \
  952. }
  953. typedef struct _TPackedCovers{
  954. _TCovers base;
  955. TStreamCacheClip code_inc_oldPosClip;
  956. TStreamCacheClip code_inc_newPosClip;
  957. TStreamCacheClip code_lengthsClip;
  958. } _TPackedCovers;
  959. typedef struct _THDiffHead{
  960. hpatch_StreamPos_t coverCount;
  961. hpatch_StreamPos_t lengthSize;
  962. hpatch_StreamPos_t inc_newPosSize;
  963. hpatch_StreamPos_t inc_oldPosSize;
  964. hpatch_StreamPos_t newDataDiffSize;
  965. hpatch_StreamPos_t headEndPos;
  966. hpatch_StreamPos_t coverEndPos;
  967. } _THDiffHead;
  968. static hpatch_BOOL read_diff_head(_THDiffHead* out_diffHead,
  969. const hpatch_TStreamInput* serializedDiff){
  970. hpatch_StreamPos_t diffPos0;
  971. const hpatch_StreamPos_t diffPos_end=serializedDiff->streamSize;
  972. TByte temp_cache[hpatch_kStreamCacheSize];
  973. TStreamCacheClip diffHeadClip;
  974. _TStreamCacheClip_init(&diffHeadClip,serializedDiff,0,diffPos_end,temp_cache,hpatch_kStreamCacheSize);
  975. _clip_unpackUIntTo(&out_diffHead->coverCount,&diffHeadClip);
  976. _clip_unpackUIntTo(&out_diffHead->lengthSize,&diffHeadClip);
  977. _clip_unpackUIntTo(&out_diffHead->inc_newPosSize,&diffHeadClip);
  978. _clip_unpackUIntTo(&out_diffHead->inc_oldPosSize,&diffHeadClip);
  979. _clip_unpackUIntTo(&out_diffHead->newDataDiffSize,&diffHeadClip);
  980. diffPos0=(hpatch_StreamPos_t)(_TStreamCacheClip_readPosOfSrcStream(&diffHeadClip));
  981. out_diffHead->headEndPos=diffPos0;
  982. #ifdef __RUN_MEM_SAFE_CHECK
  983. if (out_diffHead->lengthSize>(hpatch_StreamPos_t)(diffPos_end-diffPos0)) return _hpatch_FALSE;
  984. #endif
  985. diffPos0+=out_diffHead->lengthSize;
  986. #ifdef __RUN_MEM_SAFE_CHECK
  987. if (out_diffHead->inc_newPosSize>(hpatch_StreamPos_t)(diffPos_end-diffPos0)) return _hpatch_FALSE;
  988. #endif
  989. diffPos0+=out_diffHead->inc_newPosSize;
  990. #ifdef __RUN_MEM_SAFE_CHECK
  991. if (out_diffHead->inc_oldPosSize>(hpatch_StreamPos_t)(diffPos_end-diffPos0)) return _hpatch_FALSE;
  992. #endif
  993. diffPos0+=out_diffHead->inc_oldPosSize;
  994. out_diffHead->coverEndPos=diffPos0;
  995. #ifdef __RUN_MEM_SAFE_CHECK
  996. if (out_diffHead->newDataDiffSize>(hpatch_StreamPos_t)(diffPos_end-diffPos0)) return _hpatch_FALSE;
  997. #endif
  998. return hpatch_TRUE;
  999. }
  1000. static hpatch_BOOL _packedCovers_open(_TPackedCovers** out_self,
  1001. _THDiffHead* out_diffHead,
  1002. const hpatch_TStreamInput* serializedDiff,
  1003. TByte* temp_cache,TByte* temp_cache_end){
  1004. hpatch_size_t cacheSize;
  1005. _TPackedCovers* self=0;
  1006. _cache_alloc(self,_TPackedCovers,sizeof(_TPackedCovers),temp_cache,temp_cache_end);
  1007. cacheSize=(temp_cache_end-temp_cache)/3;
  1008. {
  1009. hpatch_StreamPos_t diffPos0;
  1010. if (!read_diff_head(out_diffHead,serializedDiff)) return _hpatch_FALSE;
  1011. diffPos0=out_diffHead->headEndPos;
  1012. _TStreamCacheClip_init(&self->code_lengthsClip,serializedDiff,diffPos0,
  1013. diffPos0+out_diffHead->lengthSize,temp_cache,cacheSize);
  1014. temp_cache+=cacheSize;
  1015. diffPos0+=out_diffHead->lengthSize;
  1016. _TStreamCacheClip_init(&self->code_inc_newPosClip,serializedDiff,diffPos0,
  1017. diffPos0+out_diffHead->inc_newPosSize,temp_cache,cacheSize);
  1018. temp_cache+=cacheSize;
  1019. diffPos0+=out_diffHead->inc_newPosSize;
  1020. _TStreamCacheClip_init(&self->code_inc_oldPosClip,serializedDiff,diffPos0,
  1021. diffPos0+out_diffHead->inc_oldPosSize,temp_cache,cacheSize);
  1022. }
  1023. _covers_init(&self->base,out_diffHead->coverCount,&self->code_inc_oldPosClip,
  1024. &self->code_inc_newPosClip,&self->code_lengthsClip,hpatch_FALSE);
  1025. *out_self=self;
  1026. return hpatch_TRUE;
  1027. }
  1028. static hpatch_BOOL _patch_stream_with_cache(const hpatch_TStreamOutput* out_newData,
  1029. const hpatch_TStreamInput* oldData,
  1030. const hpatch_TStreamInput* serializedDiff,
  1031. hpatch_TCovers* cached_covers,
  1032. TByte* temp_cache,TByte* temp_cache_end){
  1033. struct _THDiffHead diffHead;
  1034. TStreamCacheClip code_newDataDiffClip;
  1035. struct _TBytesRle_load_stream rle_loader;
  1036. hpatch_TCovers* pcovers=0;
  1037. hpatch_StreamPos_t diffPos0;
  1038. const hpatch_StreamPos_t diffPos_end=serializedDiff->streamSize;
  1039. const hpatch_size_t cacheSize=(temp_cache_end-temp_cache)/(cached_covers?(_kCachePatCount-3):_kCachePatCount);
  1040. assert(out_newData!=0);
  1041. assert(out_newData->write!=0);
  1042. assert(oldData!=0);
  1043. assert(oldData->read!=0);
  1044. assert(serializedDiff!=0);
  1045. assert(serializedDiff->read!=0);
  1046. //covers
  1047. if (cached_covers==0){
  1048. struct _TPackedCovers* packedCovers;
  1049. if (!_packedCovers_open(&packedCovers,&diffHead,serializedDiff,temp_cache+cacheSize*(_kCachePatCount-3),
  1050. temp_cache_end)) return _hpatch_FALSE;
  1051. pcovers=&packedCovers->base.ICovers; //not need close before return
  1052. }else{
  1053. pcovers=cached_covers;
  1054. if (!read_diff_head(&diffHead,serializedDiff)) return _hpatch_FALSE;
  1055. }
  1056. //newDataDiff
  1057. diffPos0=diffHead.coverEndPos;
  1058. _TStreamCacheClip_init(&code_newDataDiffClip,serializedDiff,diffPos0,
  1059. diffPos0+diffHead.newDataDiffSize,temp_cache,cacheSize);
  1060. temp_cache+=cacheSize;
  1061. diffPos0+=diffHead.newDataDiffSize;
  1062. {//rle
  1063. hpatch_StreamPos_t rleCtrlSize;
  1064. hpatch_StreamPos_t rlePos0;
  1065. TStreamCacheClip* rleHeadClip=&rle_loader.ctrlClip;//rename, share address
  1066. #ifdef __RUN_MEM_SAFE_CHECK
  1067. if (cacheSize<hpatch_kMaxPackedUIntBytes) return _hpatch_FALSE;
  1068. #endif
  1069. _TStreamCacheClip_init(rleHeadClip,serializedDiff,diffPos0,diffPos_end,
  1070. temp_cache,hpatch_kMaxPackedUIntBytes);
  1071. _clip_unpackUIntTo(&rleCtrlSize,rleHeadClip);
  1072. rlePos0=(hpatch_StreamPos_t)(_TStreamCacheClip_readPosOfSrcStream(rleHeadClip));
  1073. #ifdef __RUN_MEM_SAFE_CHECK
  1074. if (rleCtrlSize>(hpatch_StreamPos_t)(diffPos_end-rlePos0)) return _hpatch_FALSE;
  1075. #endif
  1076. _TBytesRle_load_stream_init(&rle_loader);
  1077. _TStreamCacheClip_init(&rle_loader.ctrlClip,serializedDiff,rlePos0,rlePos0+rleCtrlSize,
  1078. temp_cache,cacheSize);
  1079. temp_cache+=cacheSize;
  1080. _TStreamCacheClip_init(&rle_loader.rleCodeClip,serializedDiff,rlePos0+rleCtrlSize,diffPos_end,
  1081. temp_cache,cacheSize);
  1082. temp_cache+=cacheSize;
  1083. }
  1084. {
  1085. _TOutStreamCache outCache;
  1086. _TOutStreamCache_init(&outCache,out_newData,temp_cache,cacheSize);
  1087. temp_cache+=cacheSize;
  1088. return patchByClip(&outCache,oldData,pcovers,&code_newDataDiffClip,
  1089. &rle_loader,temp_cache,cacheSize);
  1090. }
  1091. }
  1092. hpatch_BOOL read_diffz_head(hpatch_compressedDiffInfo* out_diffInfo,_THDiffzHead* out_head,
  1093. const hpatch_TStreamInput* compressedDiff){
  1094. TStreamCacheClip _diffHeadClip;
  1095. TStreamCacheClip* diffHeadClip=&_diffHeadClip;
  1096. TByte temp_cache[hpatch_kStreamCacheSize];
  1097. _TStreamCacheClip_init(&_diffHeadClip,compressedDiff,0,compressedDiff->streamSize,
  1098. temp_cache,hpatch_kStreamCacheSize);
  1099. {//type
  1100. const char* kVersionType="HDIFF13";
  1101. char* tempType=out_diffInfo->compressType;
  1102. if (!_TStreamCacheClip_readType_end(diffHeadClip,'&',tempType)) return _hpatch_FALSE;
  1103. if (0!=strcmp(tempType,kVersionType)) return _hpatch_FALSE;
  1104. }
  1105. {//read compressType
  1106. if (!_TStreamCacheClip_readType_end(diffHeadClip,'\0',
  1107. out_diffInfo->compressType)) return _hpatch_FALSE;
  1108. out_head->typesEndPos=_TStreamCacheClip_readPosOfSrcStream(diffHeadClip);
  1109. }
  1110. _clip_unpackUIntTo(&out_diffInfo->newDataSize,diffHeadClip);
  1111. _clip_unpackUIntTo(&out_diffInfo->oldDataSize,diffHeadClip);
  1112. _clip_unpackUIntTo(&out_head->coverCount,diffHeadClip);
  1113. out_head->compressSizeBeginPos=_TStreamCacheClip_readPosOfSrcStream(diffHeadClip);
  1114. _clip_unpackUIntTo(&out_head->cover_buf_size,diffHeadClip);
  1115. _clip_unpackUIntTo(&out_head->compress_cover_buf_size,diffHeadClip);
  1116. _clip_unpackUIntTo(&out_head->rle_ctrlBuf_size,diffHeadClip);
  1117. _clip_unpackUIntTo(&out_head->compress_rle_ctrlBuf_size,diffHeadClip);
  1118. _clip_unpackUIntTo(&out_head->rle_codeBuf_size,diffHeadClip);
  1119. _clip_unpackUIntTo(&out_head->compress_rle_codeBuf_size,diffHeadClip);
  1120. _clip_unpackUIntTo(&out_head->newDataDiff_size,diffHeadClip);
  1121. _clip_unpackUIntTo(&out_head->compress_newDataDiff_size,diffHeadClip);
  1122. out_head->headEndPos=_TStreamCacheClip_readPosOfSrcStream(diffHeadClip);
  1123. out_diffInfo->compressedCount=((out_head->compress_cover_buf_size)?1:0)
  1124. +((out_head->compress_rle_ctrlBuf_size)?1:0)
  1125. +((out_head->compress_rle_codeBuf_size)?1:0)
  1126. +((out_head->compress_newDataDiff_size)?1:0);
  1127. if (out_head->compress_cover_buf_size>0)
  1128. out_head->coverEndPos=out_head->headEndPos+out_head->compress_cover_buf_size;
  1129. else
  1130. out_head->coverEndPos=out_head->headEndPos+out_head->cover_buf_size;
  1131. return hpatch_TRUE;
  1132. }
  1133. hpatch_BOOL getCompressedDiffInfo(hpatch_compressedDiffInfo* out_diffInfo,
  1134. const hpatch_TStreamInput* compressedDiff){
  1135. _THDiffzHead head;
  1136. assert(out_diffInfo!=0);
  1137. assert(compressedDiff!=0);
  1138. assert(compressedDiff->read!=0);
  1139. return read_diffz_head(out_diffInfo,&head,compressedDiff);
  1140. }
  1141. #define _clear_return(exitValue) { result=exitValue; goto clear; }
  1142. #define _kCacheDecCount 6
  1143. static
  1144. hpatch_BOOL _patch_decompress_cache(const hpatch_TStreamOutput* out_newData,
  1145. hpatch_TStreamInput* once_in_newData,
  1146. const hpatch_TStreamInput* oldData,
  1147. const hpatch_TStreamInput* compressedDiff,
  1148. hpatch_TDecompress* decompressPlugin,
  1149. hpatch_TCovers* cached_covers,
  1150. TByte* temp_cache, TByte* temp_cache_end){
  1151. TStreamCacheClip coverClip;
  1152. TStreamCacheClip code_newDataDiffClip;
  1153. struct _TBytesRle_load_stream rle_loader;
  1154. _THDiffzHead head;
  1155. hpatch_compressedDiffInfo diffInfo;
  1156. _TDecompressInputStream decompressers[4];
  1157. hpatch_size_t i;
  1158. hpatch_StreamPos_t coverCount;
  1159. hpatch_BOOL result=hpatch_TRUE;
  1160. hpatch_StreamPos_t diffPos0=0;
  1161. const hpatch_StreamPos_t diffPos_end=compressedDiff->streamSize;
  1162. const hpatch_size_t cacheSize=(temp_cache_end-temp_cache)/(cached_covers?(_kCacheDecCount-1):_kCacheDecCount);
  1163. if (cacheSize<=hpatch_kMaxPluginTypeLength) return _hpatch_FALSE;
  1164. assert(out_newData!=0);
  1165. assert(out_newData->write!=0);
  1166. assert(oldData!=0);
  1167. assert(oldData->read!=0);
  1168. assert(compressedDiff!=0);
  1169. assert(compressedDiff->read!=0);
  1170. {//head
  1171. if (!read_diffz_head(&diffInfo,&head,compressedDiff)) return _hpatch_FALSE;
  1172. if ((diffInfo.oldDataSize!=oldData->streamSize)
  1173. ||(diffInfo.newDataSize!=out_newData->streamSize)) return _hpatch_FALSE;
  1174. if ((decompressPlugin==0)&&(diffInfo.compressedCount!=0)) return _hpatch_FALSE;
  1175. if ((decompressPlugin)&&(diffInfo.compressedCount>0))
  1176. if (!decompressPlugin->is_can_open(diffInfo.compressType)) return _hpatch_FALSE;
  1177. diffPos0=head.headEndPos;
  1178. }
  1179. for (i=0;i<sizeof(decompressers)/sizeof(_TDecompressInputStream);++i)
  1180. decompressers[i].decompressHandle=0;
  1181. _TBytesRle_load_stream_init(&rle_loader);
  1182. if (cached_covers){
  1183. diffPos0=head.coverEndPos;
  1184. }else{
  1185. if (!getStreamClip(&coverClip,&decompressers[0],
  1186. head.cover_buf_size,head.compress_cover_buf_size,compressedDiff,&diffPos0,
  1187. decompressPlugin,temp_cache+cacheSize*(_kCacheDecCount-1),cacheSize)) _clear_return(_hpatch_FALSE);
  1188. }
  1189. if (!getStreamClip(&rle_loader.ctrlClip,&decompressers[1],
  1190. head.rle_ctrlBuf_size,head.compress_rle_ctrlBuf_size,compressedDiff,&diffPos0,
  1191. decompressPlugin,temp_cache,cacheSize)) _clear_return(_hpatch_FALSE);
  1192. temp_cache+=cacheSize;
  1193. if (!getStreamClip(&rle_loader.rleCodeClip,&decompressers[2],
  1194. head.rle_codeBuf_size,head.compress_rle_codeBuf_size,compressedDiff,&diffPos0,
  1195. decompressPlugin,temp_cache,cacheSize)) _clear_return(_hpatch_FALSE);
  1196. temp_cache+=cacheSize;
  1197. if (!getStreamClip(&code_newDataDiffClip,&decompressers[3],
  1198. head.newDataDiff_size,head.compress_newDataDiff_size,compressedDiff,&diffPos0,
  1199. decompressPlugin,temp_cache,cacheSize)) _clear_return(_hpatch_FALSE);
  1200. temp_cache+=cacheSize;
  1201. #ifdef __RUN_MEM_SAFE_CHECK
  1202. if (diffPos0!=diffPos_end) _clear_return(_hpatch_FALSE);
  1203. #endif
  1204. coverCount=head.coverCount;
  1205. {
  1206. _TCovers covers;
  1207. hpatch_TCovers* pcovers=0;
  1208. _TOutStreamCache outCache;
  1209. _TOutStreamCache_init(&outCache,out_newData,temp_cache,cacheSize);
  1210. temp_cache+=cacheSize;
  1211. if (cached_covers){
  1212. pcovers=cached_covers;
  1213. }else{
  1214. _covers_init(&covers,coverCount,&coverClip,&coverClip,&coverClip,hpatch_TRUE);
  1215. pcovers=&covers.ICovers; //not need close before return
  1216. }
  1217. result=patchByClip(&outCache,oldData,pcovers,&code_newDataDiffClip,&rle_loader,
  1218. temp_cache,cacheSize);
  1219. //if ((pcovers!=cached_covers)&&(!pcovers->close(pcovers))) result=_hpatch_FALSE;
  1220. }
  1221. clear:
  1222. for (i=0;i<sizeof(decompressers)/sizeof(_TDecompressInputStream);++i) {
  1223. if (decompressers[i].decompressHandle){
  1224. if (!decompressPlugin->close(decompressPlugin,decompressers[i].decompressHandle))
  1225. result=_hpatch_FALSE;
  1226. decompressers[i].decompressHandle=0;
  1227. }
  1228. }
  1229. return result;
  1230. }
  1231. hpatch_inline static hpatch_BOOL _cache_load_all(const hpatch_TStreamInput* data,
  1232. TByte* cache,TByte* cache_end){
  1233. assert((hpatch_size_t)(cache_end-cache)==data->streamSize);
  1234. return data->read(data,0,cache,cache_end);
  1235. }
  1236. typedef struct _TCompressedCovers{
  1237. _TCovers base;
  1238. TStreamCacheClip coverClip;
  1239. _TDecompressInputStream decompresser;
  1240. } _TCompressedCovers;
  1241. static hpatch_BOOL _compressedCovers_close(hpatch_TCovers* covers){
  1242. hpatch_BOOL result=hpatch_TRUE;
  1243. _TCompressedCovers* self=(_TCompressedCovers*)covers;
  1244. if (self){
  1245. if (self->decompresser.decompressHandle){
  1246. result=self->decompresser.decompressPlugin->close(self->decompresser.decompressPlugin,
  1247. self->decompresser.decompressHandle);
  1248. self->decompresser.decompressHandle=0;
  1249. }
  1250. }
  1251. return result;
  1252. }
  1253. static hpatch_BOOL _compressedCovers_open(_TCompressedCovers** out_self,
  1254. hpatch_compressedDiffInfo* out_diffInfo,
  1255. const hpatch_TStreamInput* compressedDiff,
  1256. hpatch_TDecompress* decompressPlugin,
  1257. TByte* temp_cache,TByte* temp_cache_end){
  1258. _THDiffzHead head;
  1259. hpatch_StreamPos_t diffPos0=0;
  1260. _TCompressedCovers* self=0;
  1261. _cache_alloc(self,_TCompressedCovers,sizeof(_TCompressedCovers),temp_cache,temp_cache_end);
  1262. if (!read_diffz_head(out_diffInfo,&head,compressedDiff)) return _hpatch_FALSE;
  1263. diffPos0=head.headEndPos;
  1264. if (head.compress_cover_buf_size>0){
  1265. if (decompressPlugin==0) return _hpatch_FALSE;
  1266. if (!decompressPlugin->is_can_open(out_diffInfo->compressType)) return _hpatch_FALSE;
  1267. }
  1268. _covers_init(&self->base,head.coverCount,&self->coverClip,
  1269. &self->coverClip,&self->coverClip,hpatch_TRUE);
  1270. self->base.ICovers.close=_compressedCovers_close;
  1271. memset(&self->decompresser,0, sizeof(self->decompresser));
  1272. if (!getStreamClip(&self->coverClip,&self->decompresser,
  1273. head.cover_buf_size,head.compress_cover_buf_size,
  1274. compressedDiff,&diffPos0,decompressPlugin,
  1275. temp_cache,temp_cache_end-temp_cache)) {
  1276. return _hpatch_FALSE;
  1277. };
  1278. *out_self=self;
  1279. return hpatch_TRUE;
  1280. }
  1281. #if (_IS_NEED_CACHE_OLD_BY_COVERS)
  1282. typedef struct _TArrayCovers{
  1283. hpatch_TCovers ICovers;
  1284. void* pCCovers;
  1285. hpatch_size_t coverCount;
  1286. hpatch_size_t cur_index;
  1287. hpatch_BOOL is32;
  1288. } _TArrayCovers;
  1289. typedef struct hpatch_TCCover32{
  1290. hpatch_uint32_t oldPos;
  1291. hpatch_uint32_t newPos;
  1292. hpatch_uint32_t length;
  1293. hpatch_uint32_t cachePos; //todo:放到临时内存中,用完释放?逻辑会比较复杂;
  1294. } hpatch_TCCover32;
  1295. typedef struct hpatch_TCCover64{
  1296. hpatch_StreamPos_t oldPos;
  1297. hpatch_StreamPos_t newPos;
  1298. hpatch_StreamPos_t length;
  1299. hpatch_StreamPos_t cachePos;
  1300. } hpatch_TCCover64;
  1301. #define _arrayCovers_get(self,i,item) (((self)->is32)? \
  1302. ((const hpatch_uint32_t*)(self)->pCCovers)[(i)*4+(item)]:\
  1303. ((const hpatch_StreamPos_t*)(self)->pCCovers)[(i)*4+(item)])
  1304. #define _arrayCovers_get_oldPos(self,i) _arrayCovers_get(self,i,0)
  1305. #define _arrayCovers_get_len(self,i) _arrayCovers_get(self,i,2)
  1306. #define _arrayCovers_get_cachePos(self,i) _arrayCovers_get(self,i,3)
  1307. #define _arrayCovers_set(self,i,item,v) { if ((self)->is32){ \
  1308. ((hpatch_uint32_t*)(self)->pCCovers)[(i)*4+(item)]=(hpatch_uint32_t)(v); }else{ \
  1309. ((hpatch_StreamPos_t*)(self)->pCCovers)[(i)*4+(item)]=(v); } }
  1310. #define _arrayCovers_set_cachePos(self,i,v) _arrayCovers_set(self,i,3,v)
  1311. hpatch_inline static hpatch_StreamPos_t arrayCovers_memSize(hpatch_StreamPos_t coverCount,hpatch_BOOL is32){
  1312. return coverCount*(is32?sizeof(hpatch_TCCover32):sizeof(hpatch_TCCover64));
  1313. }
  1314. static hpatch_BOOL _arrayCovers_is_finish(const hpatch_TCovers* covers){
  1315. const _TArrayCovers* self=(const _TArrayCovers*)covers;
  1316. return (self->coverCount==self->cur_index);
  1317. }
  1318. static hpatch_StreamPos_t _arrayCovers_leaveCoverCount(const hpatch_TCovers* covers){
  1319. const _TArrayCovers* self=(const _TArrayCovers*)covers;
  1320. return self->coverCount-self->cur_index;
  1321. }
  1322. static hpatch_BOOL _arrayCovers_read_cover(struct hpatch_TCovers* covers,hpatch_TCover* out_cover){
  1323. _TArrayCovers* self=(_TArrayCovers*)covers;
  1324. hpatch_size_t i=self->cur_index;
  1325. if (i<self->coverCount){
  1326. if (self->is32){
  1327. const hpatch_TCCover32* pCover=((const hpatch_TCCover32*)self->pCCovers)+i;
  1328. out_cover->oldPos=pCover->oldPos;
  1329. out_cover->newPos=pCover->newPos;
  1330. out_cover->length=pCover->length;
  1331. }else{
  1332. const hpatch_TCCover64* pCover=((const hpatch_TCCover64*)self->pCCovers)+i;
  1333. out_cover->oldPos=pCover->oldPos;
  1334. out_cover->newPos=pCover->newPos;
  1335. out_cover->length=pCover->length;
  1336. }
  1337. self->cur_index=i+1;
  1338. return hpatch_TRUE;
  1339. }else{
  1340. return _hpatch_FALSE;
  1341. }
  1342. }
  1343. static hpatch_BOOL _arrayCovers_load(_TArrayCovers** out_self,hpatch_TCovers* src_covers,
  1344. hpatch_BOOL isUsedCover32,hpatch_BOOL* out_isReadError,
  1345. TByte** ptemp_cache,TByte* temp_cache_end){
  1346. TByte* temp_cache=*ptemp_cache;
  1347. hpatch_StreamPos_t _coverCount=src_covers->leave_cover_count(src_covers);
  1348. hpatch_StreamPos_t memSize=arrayCovers_memSize(_coverCount,isUsedCover32);
  1349. hpatch_size_t i;
  1350. void* pCovers;
  1351. _TArrayCovers* self=0;
  1352. hpatch_size_t coverCount=(hpatch_size_t)_coverCount;
  1353. *out_isReadError=hpatch_FALSE;
  1354. if (coverCount!=_coverCount) return hpatch_FALSE;
  1355. _cache_alloc(self,_TArrayCovers,sizeof(_TArrayCovers),temp_cache,temp_cache_end);
  1356. _cache_alloc(pCovers,void,memSize,temp_cache,temp_cache_end);
  1357. if (isUsedCover32){
  1358. hpatch_TCCover32* pdst=(hpatch_TCCover32*)pCovers;
  1359. for (i=0;i<coverCount;++i,++pdst) {
  1360. hpatch_TCover cover;
  1361. if (!src_covers->read_cover(src_covers,&cover))
  1362. { *out_isReadError=hpatch_TRUE; return _hpatch_FALSE; }
  1363. pdst->oldPos=(hpatch_uint32_t)cover.oldPos;
  1364. pdst->newPos=(hpatch_uint32_t)cover.newPos;
  1365. pdst->length=(hpatch_uint32_t)cover.length;
  1366. }
  1367. }else{
  1368. hpatch_TCCover64* pdst=(hpatch_TCCover64*)pCovers;
  1369. for (i=0;i<coverCount;++i,++pdst) {
  1370. if (!src_covers->read_cover(src_covers,(hpatch_TCover*)pdst))
  1371. { *out_isReadError=hpatch_TRUE; return _hpatch_FALSE; }
  1372. }
  1373. }
  1374. if (!src_covers->is_finish(src_covers))
  1375. { *out_isReadError=hpatch_TRUE; return _hpatch_FALSE; }
  1376. self->pCCovers=pCovers;
  1377. self->is32=isUsedCover32;
  1378. self->coverCount=coverCount;
  1379. self->cur_index=0;
  1380. self->ICovers.close=_covers_close_nil;
  1381. self->ICovers.is_finish=_arrayCovers_is_finish;
  1382. self->ICovers.leave_cover_count=_arrayCovers_leaveCoverCount;
  1383. self->ICovers.read_cover=_arrayCovers_read_cover;
  1384. *out_self=self;
  1385. *ptemp_cache=temp_cache;
  1386. return hpatch_TRUE;
  1387. }
  1388. #define _arrayCovers_comp(_uint_t,_x,_y,item){ \
  1389. _uint_t x=((const _uint_t*)_x)[item]; \
  1390. _uint_t y=((const _uint_t*)_y)[item]; \
  1391. return (x<y)?(-1):((x>y)?1:0); \
  1392. }
  1393. #ifdef _MSC_VER
  1394. # define __CALL_BACK_C __cdecl
  1395. #else
  1396. # define __CALL_BACK_C
  1397. #endif
  1398. static hpatch_int __CALL_BACK_C _arrayCovers_comp_by_old_32(const void* _x, const void *_y){
  1399. _arrayCovers_comp(hpatch_uint32_t,_x,_y,0);
  1400. }
  1401. static hpatch_int __CALL_BACK_C _arrayCovers_comp_by_old(const void* _x, const void *_y){
  1402. _arrayCovers_comp(hpatch_StreamPos_t,_x,_y,0);
  1403. }
  1404. static hpatch_int __CALL_BACK_C _arrayCovers_comp_by_new_32(const void* _x, const void *_y){
  1405. _arrayCovers_comp(hpatch_uint32_t,_x,_y,1);
  1406. }
  1407. static hpatch_int __CALL_BACK_C _arrayCovers_comp_by_new(const void* _x, const void *_y){
  1408. _arrayCovers_comp(hpatch_StreamPos_t,_x,_y,1);
  1409. }
  1410. static hpatch_int __CALL_BACK_C _arrayCovers_comp_by_len_32(const void* _x, const void *_y){
  1411. _arrayCovers_comp(hpatch_uint32_t,_x,_y,2);
  1412. }
  1413. static hpatch_int __CALL_BACK_C _arrayCovers_comp_by_len(const void* _x, const void *_y){
  1414. _arrayCovers_comp(hpatch_StreamPos_t,_x,_y,2);
  1415. }
  1416. static void _arrayCovers_sort_by_old(_TArrayCovers* self){
  1417. if (self->is32)
  1418. qsort(self->pCCovers,self->coverCount,sizeof(hpatch_TCCover32),_arrayCovers_comp_by_old_32);
  1419. else
  1420. qsort(self->pCCovers,self->coverCount,sizeof(hpatch_TCCover64),_arrayCovers_comp_by_old);
  1421. }
  1422. static void _arrayCovers_sort_by_new(_TArrayCovers* self){
  1423. if (self->is32)
  1424. qsort(self->pCCovers,self->coverCount,sizeof(hpatch_TCCover32),_arrayCovers_comp_by_new_32);
  1425. else
  1426. qsort(self->pCCovers,self->coverCount,sizeof(hpatch_TCCover64),_arrayCovers_comp_by_new);
  1427. }
  1428. static void _arrayCovers_sort_by_len(_TArrayCovers* self){
  1429. if (self->is32)
  1430. qsort(self->pCCovers,self->coverCount,sizeof(hpatch_TCCover32),_arrayCovers_comp_by_len_32);
  1431. else
  1432. qsort(self->pCCovers,self->coverCount,sizeof(hpatch_TCCover64),_arrayCovers_comp_by_len);
  1433. }
  1434. static hpatch_size_t _getMaxCachedLen(const _TArrayCovers* src_covers,
  1435. TByte* temp_cache,TByte* temp_cache_end,TByte* cache_buf_end){
  1436. const hpatch_size_t kMaxCachedLen =~((hpatch_size_t)0);//允许缓存的最长单个数据长度;
  1437. hpatch_StreamPos_t mlen=0;
  1438. hpatch_StreamPos_t sum=0;
  1439. const hpatch_size_t coverCount=src_covers->coverCount;
  1440. hpatch_size_t i;
  1441. _TArrayCovers cur_covers=*src_covers;
  1442. hpatch_size_t cacheSize=temp_cache_end-temp_cache;
  1443. hpatch_StreamPos_t memSize=arrayCovers_memSize(src_covers->coverCount,src_covers->is32);
  1444. _cache_alloc(cur_covers.pCCovers,void,memSize,temp_cache,temp_cache_end); //fail return 0
  1445. memcpy(cur_covers.pCCovers,src_covers->pCCovers,(hpatch_size_t)memSize);
  1446. _arrayCovers_sort_by_len(&cur_covers);
  1447. for (i=0; i<coverCount;++i) {
  1448. mlen=_arrayCovers_get_len(&cur_covers,i);
  1449. sum+=mlen;
  1450. if (sum<=cacheSize){
  1451. continue;
  1452. }else{
  1453. --mlen;
  1454. break;
  1455. }
  1456. }
  1457. if (mlen>kMaxCachedLen)
  1458. mlen=kMaxCachedLen;
  1459. return (hpatch_size_t)mlen;
  1460. }
  1461. static hpatch_size_t _set_cache_pos(_TArrayCovers* covers,hpatch_size_t maxCachedLen,
  1462. hpatch_StreamPos_t* poldPosBegin,hpatch_StreamPos_t* poldPosEnd){
  1463. const hpatch_size_t coverCount=covers->coverCount;
  1464. const hpatch_size_t kMinCacheCoverCount=coverCount/8+1; //控制最小缓存数量,否则缓存的意义太小;
  1465. hpatch_StreamPos_t oldPosBegin=hpatch_kNullStreamPos;
  1466. hpatch_StreamPos_t oldPosEnd=0;
  1467. hpatch_size_t cacheCoverCount=0;
  1468. hpatch_size_t sum=0;//result
  1469. hpatch_size_t i;
  1470. for (i=0; i<coverCount;++i) {
  1471. hpatch_StreamPos_t clen=_arrayCovers_get_len(covers,i);
  1472. if (clen<=maxCachedLen){
  1473. hpatch_StreamPos_t oldPos;
  1474. _arrayCovers_set_cachePos(covers,i,sum);
  1475. sum+=(hpatch_size_t)clen;
  1476. ++cacheCoverCount;
  1477. oldPos=_arrayCovers_get_oldPos(covers,i);
  1478. if (oldPos<oldPosBegin) oldPosBegin=oldPos;
  1479. if (oldPos+clen>oldPosEnd) oldPosEnd=oldPos+clen;
  1480. }
  1481. }
  1482. if (cacheCoverCount<kMinCacheCoverCount)
  1483. return 0;//fail
  1484. *poldPosBegin=oldPosBegin;
  1485. *poldPosEnd=oldPosEnd;
  1486. return sum;
  1487. }
  1488. //一个比较简单的缓存策略:
  1489. // 1. 根据缓冲区大小限制,选择出最短的一批覆盖线来缓存;
  1490. // 2. 顺序访问一次oldData文件,填充这些缓存;
  1491. // 3. 顺序访问时跳过中间过大的对缓存无用的区域;
  1492. static hpatch_BOOL _cache_old_load(const hpatch_TStreamInput*oldData,
  1493. hpatch_StreamPos_t oldPos,hpatch_StreamPos_t oldPosAllEnd,
  1494. _TArrayCovers* arrayCovers,hpatch_size_t maxCachedLen,hpatch_size_t sumCacheLen,
  1495. TByte* old_cache,TByte* old_cache_end,TByte* cache_buf_end){
  1496. const hpatch_size_t kMinSpaceLen =(1<<(20+2));//跳过seekTime*speed长度的空间(SSD可以更小)时间上划得来,否则就顺序访问;
  1497. const hpatch_size_t kAccessPageSize=(1<<(10+2));//页面对齐访问;
  1498. hpatch_BOOL result=hpatch_TRUE;
  1499. hpatch_size_t cur_i=0,i;
  1500. const hpatch_size_t coverCount=arrayCovers->coverCount;
  1501. TByte* cache_buf=old_cache_end;
  1502. assert((hpatch_size_t)(old_cache_end-old_cache)>=sumCacheLen);
  1503. if ((hpatch_size_t)(cache_buf_end-cache_buf)>=kAccessPageSize*2){
  1504. cache_buf=(TByte*)_hpatch_align_upper(cache_buf,kAccessPageSize);
  1505. if ((hpatch_size_t)(cache_buf_end-cache_buf)>=(kMinSpaceLen>>1))
  1506. cache_buf_end=cache_buf+(kMinSpaceLen>>1);
  1507. else
  1508. cache_buf_end=(TByte*)_hpatch_align_lower(cache_buf_end,kAccessPageSize);
  1509. }
  1510. oldPos=_hpatch_align_type_lower(hpatch_StreamPos_t,oldPos,kAccessPageSize);
  1511. if (oldPos<kMinSpaceLen) oldPos=0;
  1512. _arrayCovers_sort_by_old(arrayCovers);
  1513. while ((oldPos<oldPosAllEnd)&(cur_i<coverCount)) {
  1514. hpatch_StreamPos_t oldPosEnd;
  1515. hpatch_size_t readLen=(cache_buf_end-cache_buf);
  1516. if (readLen>(oldPosAllEnd-oldPos)) readLen=(hpatch_size_t)(oldPosAllEnd-oldPos);
  1517. if (!oldData->read(oldData,oldPos,cache_buf,
  1518. cache_buf+readLen)) { result=_hpatch_FALSE; break; } //error
  1519. oldPosEnd=oldPos+readLen;
  1520. for (i=cur_i;i<coverCount;++i){
  1521. hpatch_StreamPos_t ioldPos,ioldPosEnd;
  1522. hpatch_StreamPos_t ilen=_arrayCovers_get_len(arrayCovers,i);
  1523. if (ilen>maxCachedLen){//覆盖线比较长不需要缓存,下一个覆盖线;
  1524. if (i==cur_i)
  1525. ++cur_i;
  1526. continue;
  1527. }
  1528. ioldPos=_arrayCovers_get_oldPos(arrayCovers,i);
  1529. ioldPosEnd=ioldPos+ilen;
  1530. if (ioldPosEnd>oldPos){
  1531. // [oldPos oldPosEnd]
  1532. // ioldPosEnd]----or----]
  1533. if (ioldPos<oldPosEnd){//有交集,需要cache
  1534. // [----or----[ioldPos ioldPosEnd]----or----]
  1535. hpatch_StreamPos_t from;
  1536. hpatch_size_t copyLen;
  1537. hpatch_StreamPos_t dstPos=_arrayCovers_get_cachePos(arrayCovers,i);
  1538. //assert(dstPos<=(hpatch_size_t)(old_cache_end-old_cache));
  1539. if (ioldPos>=oldPos){
  1540. // [ioldPos ioldPosEnd]----or----]
  1541. from=ioldPos;
  1542. }else{
  1543. // [ioldPos ioldPosEnd]----or----]
  1544. from=oldPos;
  1545. dstPos+=(oldPos-ioldPos);
  1546. }
  1547. copyLen=(hpatch_size_t)(((ioldPosEnd<=oldPosEnd)?ioldPosEnd:oldPosEnd)-from);
  1548. //assert(dstPos+copyLen<=(hpatch_size_t)(old_cache_end-old_cache));
  1549. //assert(sumCacheLen>=copyLen);
  1550. memcpy(old_cache+(hpatch_size_t)dstPos,cache_buf+(from-oldPos),copyLen);
  1551. sumCacheLen-=copyLen;
  1552. if ((i==cur_i)&(oldPosEnd>=ioldPosEnd))
  1553. ++cur_i;
  1554. }else{//后面覆盖线暂时都不会与当前数据有交集了,下一块数据;
  1555. // [oldPos oldPosEnd]
  1556. // [ioldPos ioldPosEnd]
  1557. if ((i==cur_i)&&(ioldPos-oldPosEnd>=kMinSpaceLen))
  1558. oldPosEnd=_hpatch_align_type_lower(hpatch_StreamPos_t,ioldPos,kAccessPageSize);
  1559. break;
  1560. }
  1561. }else{//当前覆盖线已经落后于当前数据,下一个覆盖线;
  1562. // [oldPos oldPosEnd]
  1563. // [ioldPos ioldPosEnd]
  1564. if (i==cur_i)
  1565. ++cur_i;
  1566. }
  1567. }
  1568. oldPos=oldPosEnd;
  1569. }
  1570. _arrayCovers_sort_by_new(arrayCovers);
  1571. assert(sumCacheLen==0);
  1572. return result;
  1573. }
  1574. typedef struct _cache_old_TStreamInput{
  1575. _TArrayCovers arrayCovers;
  1576. hpatch_BOOL isInHitCache;
  1577. hpatch_size_t maxCachedLen;
  1578. hpatch_StreamPos_t readFromPos;
  1579. hpatch_StreamPos_t readFromPosEnd;
  1580. const TByte* caches;
  1581. const TByte* cachesEnd;
  1582. const hpatch_TStreamInput* oldData;
  1583. } _cache_old_TStreamInput;
  1584. static hpatch_BOOL _cache_old_StreamInput_read(const hpatch_TStreamInput* stream,
  1585. hpatch_StreamPos_t readFromPos,
  1586. unsigned char* out_data,unsigned char* out_data_end){
  1587. _cache_old_TStreamInput* self=(_cache_old_TStreamInput*)stream->streamImport;
  1588. hpatch_StreamPos_t dataLen=(hpatch_size_t)(self->readFromPosEnd-self->readFromPos);
  1589. hpatch_size_t readLen;
  1590. if (dataLen==0){//next cover
  1591. hpatch_StreamPos_t oldPos;
  1592. hpatch_size_t i=self->arrayCovers.cur_index++;
  1593. if (i>=self->arrayCovers.coverCount) return _hpatch_FALSE;//error;
  1594. oldPos=_arrayCovers_get_oldPos(&self->arrayCovers,i);
  1595. dataLen=_arrayCovers_get_len(&self->arrayCovers,i);
  1596. self->isInHitCache=(dataLen<=self->maxCachedLen);
  1597. self->readFromPos=oldPos;
  1598. self->readFromPosEnd=oldPos+dataLen;
  1599. }
  1600. readLen=out_data_end-out_data;
  1601. if ((readLen>dataLen)||(self->readFromPos!=readFromPos)) return _hpatch_FALSE; //error
  1602. self->readFromPos=readFromPos+readLen;
  1603. if (self->isInHitCache){
  1604. assert(readLen<=(hpatch_size_t)(self->cachesEnd-self->caches));
  1605. memcpy(out_data,self->caches,readLen);
  1606. self->caches+=readLen;
  1607. return hpatch_TRUE;
  1608. }else{
  1609. return self->oldData->read(self->oldData,readFromPos,out_data,out_data_end);
  1610. }
  1611. }
  1612. static hpatch_BOOL _cache_old(hpatch_TStreamInput** out_cachedOld,const hpatch_TStreamInput* oldData,
  1613. _TArrayCovers* arrayCovers,hpatch_BOOL* out_isReadError,
  1614. TByte* temp_cache,TByte** ptemp_cache_end,TByte* cache_buf_end){
  1615. _cache_old_TStreamInput* self;
  1616. TByte* temp_cache_end=*ptemp_cache_end;
  1617. hpatch_StreamPos_t oldPosBegin;
  1618. hpatch_StreamPos_t oldPosEnd;
  1619. hpatch_size_t sumCacheLen;
  1620. hpatch_size_t maxCachedLen;
  1621. *out_isReadError=hpatch_FALSE;
  1622. _cache_alloc(*out_cachedOld,hpatch_TStreamInput,sizeof(hpatch_TStreamInput),
  1623. temp_cache,temp_cache_end);
  1624. _cache_alloc(self,_cache_old_TStreamInput,sizeof(_cache_old_TStreamInput),
  1625. temp_cache,temp_cache_end);
  1626. maxCachedLen=_getMaxCachedLen(arrayCovers,temp_cache,temp_cache_end,cache_buf_end);
  1627. if (maxCachedLen==0) return hpatch_FALSE;
  1628. sumCacheLen=_set_cache_pos(arrayCovers,maxCachedLen,&oldPosBegin,&oldPosEnd);
  1629. if (sumCacheLen==0) return hpatch_FALSE;
  1630. temp_cache_end=temp_cache+sumCacheLen;
  1631. if (!_cache_old_load(oldData,oldPosBegin,oldPosEnd,arrayCovers,maxCachedLen,sumCacheLen,
  1632. temp_cache,temp_cache_end,cache_buf_end))
  1633. { *out_isReadError=hpatch_TRUE; return _hpatch_FALSE; }
  1634. {//out
  1635. self->arrayCovers=*arrayCovers;
  1636. self->arrayCovers.cur_index=0;
  1637. self->isInHitCache=hpatch_FALSE;
  1638. self->maxCachedLen=maxCachedLen;
  1639. self->caches=temp_cache;
  1640. self->cachesEnd=temp_cache_end;
  1641. self->readFromPos=0;
  1642. self->readFromPosEnd=0;
  1643. self->oldData=oldData;
  1644. (*out_cachedOld)->streamImport=self;
  1645. (*out_cachedOld)->streamSize=oldData->streamSize;
  1646. (*out_cachedOld)->read=_cache_old_StreamInput_read;
  1647. *ptemp_cache_end=temp_cache_end;
  1648. }
  1649. return hpatch_TRUE;
  1650. }
  1651. #endif //_IS_NEED_CACHE_OLD_BY_COVERS
  1652. static hpatch_BOOL _patch_cache(hpatch_TCovers** out_covers,
  1653. const hpatch_TStreamInput** poldData,hpatch_StreamPos_t newDataSize,
  1654. const hpatch_TStreamInput* diffData,hpatch_BOOL isCompressedDiff,
  1655. hpatch_TDecompress* decompressPlugin,size_t kCacheCount,
  1656. TByte** ptemp_cache,TByte** ptemp_cache_end,hpatch_BOOL* out_isReadError){
  1657. const hpatch_TStreamInput* oldData=*poldData;
  1658. const hpatch_size_t kMinCacheSize=hpatch_kStreamCacheSize*kCacheCount;
  1659. #if (_IS_NEED_CACHE_OLD_BY_COVERS)
  1660. const hpatch_size_t kBestACacheSize=hpatch_kFileIOBufBetterSize; //内存足够时比较好的hpatch_kStreamCacheSize值;
  1661. const hpatch_size_t _minActiveSize=(1<<20)*8;
  1662. const hpatch_StreamPos_t _betterActiveSize=kBestACacheSize*kCacheCount*2+oldData->streamSize/8;
  1663. const hpatch_size_t kActiveCacheOldMemorySize = //尝试激活CacheOld功能的内存下限;
  1664. (_betterActiveSize>_minActiveSize)?_minActiveSize:(hpatch_size_t)_betterActiveSize;
  1665. #endif //_IS_NEED_CACHE_OLD_BY_COVERS
  1666. TByte* temp_cache=*ptemp_cache;
  1667. TByte* temp_cache_end=*ptemp_cache_end;
  1668. *out_isReadError=hpatch_FALSE;
  1669. if ((hpatch_size_t)(temp_cache_end-temp_cache)>=oldData->streamSize+kMinCacheSize
  1670. +sizeof(hpatch_TStreamInput)+sizeof(hpatch_StreamPos_t)){//load all oldData
  1671. hpatch_TStreamInput* replace_oldData=0;
  1672. _cache_alloc(replace_oldData,hpatch_TStreamInput,sizeof(hpatch_TStreamInput),
  1673. temp_cache,temp_cache_end);
  1674. if (!_cache_load_all(oldData,temp_cache_end-oldData->streamSize,
  1675. temp_cache_end)){ *out_isReadError=hpatch_TRUE; return _hpatch_FALSE; }
  1676. mem_as_hStreamInput(replace_oldData,temp_cache_end-oldData->streamSize,temp_cache_end);
  1677. temp_cache_end-=oldData->streamSize;
  1678. // [ patch cache | oldData cache ]
  1679. // [ (cacheSize-oldData->streamSize) | (oldData->streamSize) ]
  1680. *out_covers=0;
  1681. *poldData=replace_oldData;
  1682. *ptemp_cache=temp_cache;
  1683. *ptemp_cache_end=temp_cache_end;
  1684. return hpatch_TRUE;
  1685. }
  1686. #if (_IS_NEED_CACHE_OLD_BY_COVERS)
  1687. else if ((hpatch_size_t)(temp_cache_end-temp_cache)>=kActiveCacheOldMemorySize) {
  1688. hpatch_BOOL isUsedCover32;
  1689. TByte* temp_cache_end_back=temp_cache_end;
  1690. _TArrayCovers* arrayCovers=0;
  1691. assert((hpatch_size_t)(temp_cache_end-temp_cache)>kBestACacheSize*kCacheCount);
  1692. assert(kBestACacheSize>sizeof(_TCompressedCovers)+sizeof(_TPackedCovers));
  1693. if (isCompressedDiff){
  1694. hpatch_compressedDiffInfo diffInfo;
  1695. _TCompressedCovers* compressedCovers=0;
  1696. if (!_compressedCovers_open(&compressedCovers,&diffInfo,diffData,decompressPlugin,
  1697. temp_cache_end-kBestACacheSize-sizeof(_TCompressedCovers),temp_cache_end))
  1698. { *out_isReadError=hpatch_TRUE; return _hpatch_FALSE; }
  1699. if ((oldData->streamSize!=diffInfo.oldDataSize)||(newDataSize!=diffInfo.newDataSize))
  1700. { *out_isReadError=hpatch_TRUE; return _hpatch_FALSE; }
  1701. temp_cache_end-=kBestACacheSize+sizeof(_TCompressedCovers);
  1702. // [ ... | compressedCovers cache ]
  1703. // [ (cacheSize-kBestACacheSize) | (kBestACacheSize) ]
  1704. *out_covers=&compressedCovers->base.ICovers;
  1705. isUsedCover32=(diffInfo.oldDataSize|diffInfo.newDataSize)<((hpatch_StreamPos_t)1<<32);
  1706. }else{
  1707. _TPackedCovers* packedCovers=0;
  1708. _THDiffHead diffHead;
  1709. hpatch_StreamPos_t oldDataSize=oldData->streamSize;
  1710. if (!_packedCovers_open(&packedCovers,&diffHead,diffData,
  1711. temp_cache_end-kBestACacheSize*3-sizeof(_TPackedCovers),temp_cache_end))
  1712. { *out_isReadError=hpatch_TRUE; return _hpatch_FALSE; }
  1713. temp_cache_end-=kBestACacheSize*3+sizeof(_TPackedCovers);
  1714. // [ ... | packedCovers cache ]
  1715. // [ (cacheSize-kBestACacheSize*3) | (kBestACacheSize*3) ]
  1716. *out_covers=&packedCovers->base.ICovers;
  1717. isUsedCover32=(oldDataSize|newDataSize)<((hpatch_StreamPos_t)1<<32);
  1718. }
  1719. if (!_arrayCovers_load(&arrayCovers,*out_covers,isUsedCover32,
  1720. out_isReadError,&temp_cache,temp_cache_end-kBestACacheSize)){
  1721. if (*out_isReadError) return _hpatch_FALSE;
  1722. // [ patch cache | *edCovers cache ]
  1723. // [ (cacheSize-kBestACacheSize*?) | (kBestACacheSize*?) ]
  1724. *ptemp_cache=temp_cache;
  1725. *ptemp_cache_end=temp_cache_end;
  1726. return hpatch_FALSE;
  1727. }else{
  1728. // [ arrayCovers cache | ... ]
  1729. // [((new temp_cache)-(old temp_cache))| (cacheSize-(arrayCovers cache size)) ]
  1730. TByte* old_cache_end;
  1731. hpatch_TStreamInput* replace_oldData=0;
  1732. assert(!(*out_isReadError));
  1733. if (!((*out_covers)->close(*out_covers))) return _hpatch_FALSE;
  1734. *out_covers=&arrayCovers->ICovers;
  1735. temp_cache_end=temp_cache_end_back; //free compressedCovers or packedCovers memory
  1736. old_cache_end=temp_cache_end-kBestACacheSize*kCacheCount;
  1737. // [ arrayCovers cache | ... | patch reserve cache ]
  1738. // [ | ... | (kBestACacheSize*kCacheCount) ]
  1739. if (((hpatch_size_t)(temp_cache_end-temp_cache)<=kBestACacheSize*kCacheCount)
  1740. ||(!_cache_old(&replace_oldData,oldData,arrayCovers,out_isReadError,
  1741. temp_cache,&old_cache_end,temp_cache_end))){
  1742. if (*out_isReadError) return _hpatch_FALSE;
  1743. // [ arrayCovers cache | patch cache ]
  1744. *ptemp_cache=temp_cache;
  1745. *ptemp_cache_end=temp_cache_end;
  1746. return hpatch_FALSE;
  1747. }else{
  1748. // [ arrayCovers cache | oldData cache | patch cache ]
  1749. // [ | |(temp_cache_end-(new old_cache_end))]
  1750. assert(!(*out_isReadError));
  1751. assert((hpatch_size_t)(temp_cache_end-old_cache_end)>=kBestACacheSize*kCacheCount);
  1752. temp_cache=old_cache_end;
  1753. *poldData=replace_oldData;
  1754. *ptemp_cache=temp_cache;
  1755. *ptemp_cache_end=temp_cache_end;
  1756. return hpatch_TRUE;
  1757. }
  1758. }
  1759. }
  1760. #endif//_IS_NEED_CACHE_OLD_BY_COVERS
  1761. return hpatch_FALSE;//not cache oldData
  1762. }
  1763. hpatch_BOOL patch_stream_with_cache(const struct hpatch_TStreamOutput* out_newData,
  1764. const struct hpatch_TStreamInput* oldData,
  1765. const struct hpatch_TStreamInput* serializedDiff,
  1766. TByte* temp_cache,TByte* temp_cache_end){
  1767. hpatch_BOOL result;
  1768. hpatch_TCovers* covers=0;//not need close before return
  1769. hpatch_BOOL isReadError=hpatch_FALSE;
  1770. _patch_cache(&covers,&oldData,out_newData->streamSize,serializedDiff,hpatch_FALSE,0,
  1771. _kCachePatCount,&temp_cache,&temp_cache_end,&isReadError);
  1772. if (isReadError) return _hpatch_FALSE;
  1773. result=_patch_stream_with_cache(out_newData,oldData,serializedDiff,covers,
  1774. temp_cache,temp_cache_end);
  1775. //if ((covers!=0)&&(!covers->close(covers))) result=_hpatch_FALSE;
  1776. return result;
  1777. }
  1778. hpatch_BOOL patch_stream(const hpatch_TStreamOutput* out_newData,
  1779. const hpatch_TStreamInput* oldData,
  1780. const hpatch_TStreamInput* serializedDiff){
  1781. TByte temp_cache[hpatch_kStreamCacheSize*_kCachePatCount];
  1782. return _patch_stream_with_cache(out_newData,oldData,serializedDiff,0,
  1783. temp_cache,temp_cache+sizeof(temp_cache)/sizeof(TByte));
  1784. }
  1785. hpatch_BOOL patch_decompress_with_cache(const hpatch_TStreamOutput* out_newData,
  1786. const hpatch_TStreamInput* oldData,
  1787. const hpatch_TStreamInput* compressedDiff,
  1788. hpatch_TDecompress* decompressPlugin,
  1789. TByte* temp_cache,TByte* temp_cache_end){
  1790. hpatch_BOOL result;
  1791. hpatch_TCovers* covers=0; //need close before return
  1792. hpatch_BOOL isReadError=hpatch_FALSE;
  1793. _patch_cache(&covers,&oldData,out_newData->streamSize,compressedDiff,hpatch_TRUE,
  1794. decompressPlugin,_kCacheDecCount,&temp_cache,&temp_cache_end,&isReadError);
  1795. if (isReadError) return _hpatch_FALSE;
  1796. result=_patch_decompress_cache(out_newData,0,oldData,compressedDiff,decompressPlugin,
  1797. covers,temp_cache,temp_cache_end);
  1798. if ((covers!=0)&&(!covers->close(covers))) result=_hpatch_FALSE;
  1799. return result;
  1800. }
  1801. hpatch_BOOL patch_decompress(const hpatch_TStreamOutput* out_newData,
  1802. const hpatch_TStreamInput* oldData,
  1803. const hpatch_TStreamInput* compressedDiff,
  1804. hpatch_TDecompress* decompressPlugin){
  1805. TByte temp_cache[hpatch_kStreamCacheSize*_kCacheDecCount];
  1806. return _patch_decompress_cache(out_newData,0,oldData,compressedDiff,decompressPlugin,
  1807. 0,temp_cache,temp_cache+sizeof(temp_cache)/sizeof(TByte));
  1808. }
  1809. hpatch_BOOL hpatch_coverList_open_serializedDiff(hpatch_TCoverList* out_coverList,
  1810. const hpatch_TStreamInput* serializedDiff){
  1811. TByte* temp_cache;
  1812. TByte* temp_cache_end;
  1813. _TPackedCovers* packedCovers=0;
  1814. _THDiffHead diffHead;
  1815. assert((out_coverList!=0)&&(out_coverList->ICovers==0));
  1816. temp_cache=out_coverList->_buf;
  1817. temp_cache_end=temp_cache+sizeof(out_coverList->_buf);
  1818. if (!_packedCovers_open(&packedCovers,&diffHead,serializedDiff,
  1819. temp_cache,temp_cache_end))
  1820. return _hpatch_FALSE;
  1821. out_coverList->ICovers=&packedCovers->base.ICovers;
  1822. return hpatch_TRUE;
  1823. }
  1824. hpatch_BOOL hpatch_coverList_open_compressedDiff(hpatch_TCoverList* out_coverList,
  1825. const hpatch_TStreamInput* compressedDiff,
  1826. hpatch_TDecompress* decompressPlugin){
  1827. TByte* temp_cache;
  1828. TByte* temp_cache_end;
  1829. _TCompressedCovers* compressedCovers=0;
  1830. hpatch_compressedDiffInfo diffInfo;
  1831. assert((out_coverList!=0)&&(out_coverList->ICovers==0));
  1832. temp_cache=out_coverList->_buf;
  1833. temp_cache_end=temp_cache+sizeof(out_coverList->_buf);
  1834. if (!_compressedCovers_open(&compressedCovers,&diffInfo,compressedDiff,decompressPlugin,
  1835. temp_cache,temp_cache_end))
  1836. return _hpatch_FALSE;
  1837. out_coverList->ICovers=&compressedCovers->base.ICovers;
  1838. return hpatch_TRUE;
  1839. }
  1840. //
  1841. #define _kCacheSgCount 3
  1842. hpatch_BOOL patch_single_compressed_diff(const hpatch_TStreamOutput* out_newData,
  1843. const hpatch_TStreamInput* oldData,
  1844. const hpatch_TStreamInput* singleCompressedDiff,
  1845. hpatch_StreamPos_t diffData_pos,
  1846. hpatch_StreamPos_t uncompressedSize,
  1847. hpatch_StreamPos_t compressedSize,
  1848. hpatch_TDecompress* decompressPlugin,
  1849. hpatch_StreamPos_t coverCount,hpatch_size_t stepMemSize,
  1850. unsigned char* temp_cache,unsigned char* temp_cache_end,
  1851. sspatch_coversListener_t* coversListener){
  1852. hpatch_BOOL result;
  1853. hpatch_TUncompresser_t uncompressedStream;
  1854. hpatch_StreamPos_t diffData_posEnd;
  1855. memset(&uncompressedStream,0,sizeof(uncompressedStream));
  1856. if (compressedSize==0){
  1857. decompressPlugin=0;
  1858. }else{
  1859. if (decompressPlugin==0) return _hpatch_FALSE;
  1860. }
  1861. diffData_posEnd=(decompressPlugin?compressedSize:uncompressedSize)+diffData_pos;
  1862. if (diffData_posEnd>singleCompressedDiff->streamSize) return _hpatch_FALSE;
  1863. if (decompressPlugin){
  1864. if (!compressed_stream_as_uncompressed(&uncompressedStream,uncompressedSize,decompressPlugin,singleCompressedDiff,
  1865. diffData_pos,diffData_posEnd)) return _hpatch_FALSE;
  1866. singleCompressedDiff=&uncompressedStream.base;
  1867. diffData_pos=0;
  1868. diffData_posEnd=singleCompressedDiff->streamSize;
  1869. }
  1870. result=patch_single_stream_diff(out_newData,oldData,singleCompressedDiff,diffData_pos,diffData_posEnd,
  1871. coverCount,stepMemSize,temp_cache,temp_cache_end,coversListener);
  1872. if (decompressPlugin)
  1873. close_compressed_stream_as_uncompressed(&uncompressedStream);
  1874. return result;
  1875. }
  1876. static const size_t _kStepMemSizeSafeLimit =(1<<20)*16;
  1877. hpatch_BOOL getSingleCompressedDiffInfo(hpatch_singleCompressedDiffInfo* out_diffInfo,
  1878. const hpatch_TStreamInput* singleCompressedDiff,
  1879. hpatch_StreamPos_t diffInfo_pos){
  1880. TStreamCacheClip _diffHeadClip;
  1881. TStreamCacheClip* diffHeadClip=&_diffHeadClip;
  1882. TByte temp_cache[hpatch_kStreamCacheSize];
  1883. _TStreamCacheClip_init(&_diffHeadClip,singleCompressedDiff,diffInfo_pos,singleCompressedDiff->streamSize,
  1884. temp_cache,hpatch_kStreamCacheSize);
  1885. {//type
  1886. const char* kVersionType="HDIFFSF20";
  1887. char* tempType=out_diffInfo->compressType;
  1888. if (!_TStreamCacheClip_readType_end(diffHeadClip,'&',tempType)) return _hpatch_FALSE;
  1889. if (0!=strcmp(tempType,kVersionType)) return _hpatch_FALSE;
  1890. }
  1891. {//read compressType
  1892. if (!_TStreamCacheClip_readType_end(diffHeadClip,'\0',
  1893. out_diffInfo->compressType)) return _hpatch_FALSE;
  1894. }
  1895. _clip_unpackUIntTo(&out_diffInfo->newDataSize,diffHeadClip);
  1896. _clip_unpackUIntTo(&out_diffInfo->oldDataSize,diffHeadClip);
  1897. _clip_unpackUIntTo(&out_diffInfo->coverCount,diffHeadClip);
  1898. _clip_unpackUIntTo(&out_diffInfo->stepMemSize,diffHeadClip);
  1899. _clip_unpackUIntTo(&out_diffInfo->uncompressedSize,diffHeadClip);
  1900. _clip_unpackUIntTo(&out_diffInfo->compressedSize,diffHeadClip);
  1901. out_diffInfo->diffDataPos=_TStreamCacheClip_readPosOfSrcStream(diffHeadClip)-diffInfo_pos;
  1902. if (out_diffInfo->compressedSize>out_diffInfo->uncompressedSize)
  1903. return _hpatch_FALSE;
  1904. if (out_diffInfo->stepMemSize>(out_diffInfo->newDataSize>=_kStepMemSizeSafeLimit?out_diffInfo->newDataSize:_kStepMemSizeSafeLimit))
  1905. return _hpatch_FALSE;
  1906. if (out_diffInfo->stepMemSize>out_diffInfo->uncompressedSize)
  1907. return _hpatch_FALSE;
  1908. return hpatch_TRUE;
  1909. }
  1910. static hpatch_BOOL _TUncompresser_read(const struct hpatch_TStreamInput* stream,hpatch_StreamPos_t readFromPos,
  1911. unsigned char* out_data,unsigned char* out_data_end){
  1912. hpatch_TUncompresser_t* self=(hpatch_TUncompresser_t*)stream->streamImport;
  1913. return self->_decompressPlugin->decompress_part(self->_decompressHandle,out_data,out_data_end);
  1914. }
  1915. hpatch_BOOL compressed_stream_as_uncompressed(hpatch_TUncompresser_t* uncompressedStream,hpatch_StreamPos_t uncompressedSize,
  1916. hpatch_TDecompress* decompressPlugin,const hpatch_TStreamInput* compressedStream,
  1917. hpatch_StreamPos_t compressed_pos,hpatch_StreamPos_t compressed_end){
  1918. hpatch_TUncompresser_t* self=uncompressedStream;
  1919. assert(decompressPlugin!=0);
  1920. assert(self->_decompressHandle==0);
  1921. self->_decompressHandle=decompressPlugin->open(decompressPlugin,uncompressedSize,compressedStream,
  1922. compressed_pos,compressed_end);
  1923. if (self->_decompressHandle==0) return _hpatch_FALSE;
  1924. self->_decompressPlugin=decompressPlugin;
  1925. self->base.streamImport=self;
  1926. self->base.streamSize=uncompressedSize;
  1927. self->base.read=_TUncompresser_read;
  1928. return hpatch_TRUE;
  1929. }
  1930. void close_compressed_stream_as_uncompressed(hpatch_TUncompresser_t* uncompressedStream){
  1931. hpatch_TUncompresser_t* self=uncompressedStream;
  1932. if (self==0) return;
  1933. if (self->_decompressHandle==0) return;
  1934. self->_decompressPlugin->close(self->_decompressPlugin,self->_decompressHandle);
  1935. self->_decompressHandle=0;
  1936. }
  1937. typedef struct{
  1938. const unsigned char* code;
  1939. const unsigned char* code_end;
  1940. hpatch_size_t len0;
  1941. hpatch_size_t lenv;
  1942. hpatch_BOOL isNeedDecode0;
  1943. } rle0_decoder_t;
  1944. static void _rle0_decoder_init(rle0_decoder_t* self,const unsigned char* code,const unsigned char* code_end){
  1945. self->code=code;
  1946. self->code_end=code_end;
  1947. self->len0=0;
  1948. self->lenv=0;
  1949. self->isNeedDecode0=hpatch_TRUE;
  1950. }
  1951. static hpatch_BOOL _rle0_decoder_add(rle0_decoder_t* self,TByte* out_data,hpatch_size_t decodeSize){
  1952. if (self->len0){
  1953. _0_process:
  1954. if (self->len0>=decodeSize){
  1955. self->len0-=decodeSize;
  1956. return hpatch_TRUE;
  1957. }else{
  1958. decodeSize-=self->len0;
  1959. out_data+=self->len0;
  1960. self->len0=0;
  1961. goto _decode_v_process;
  1962. }
  1963. }
  1964. if (self->lenv){
  1965. _v_process:
  1966. if (self->lenv>=decodeSize){
  1967. addData(out_data,self->code,decodeSize);
  1968. self->code+=decodeSize;
  1969. self->lenv-=decodeSize;
  1970. return hpatch_TRUE;
  1971. }else{
  1972. addData(out_data,self->code,self->lenv);
  1973. out_data+=self->lenv;
  1974. decodeSize-=self->lenv;
  1975. self->code+=self->lenv;
  1976. self->lenv=0;
  1977. goto _decode_0_process;
  1978. }
  1979. }
  1980. assert(decodeSize>0);
  1981. if (self->isNeedDecode0){
  1982. hpatch_StreamPos_t len0;
  1983. _decode_0_process:
  1984. self->isNeedDecode0=hpatch_FALSE;
  1985. if (!hpatch_unpackUInt(&self->code,self->code_end,&len0)) return _hpatch_FALSE;
  1986. if (len0!=(hpatch_size_t)len0) return _hpatch_FALSE;
  1987. self->len0=(hpatch_size_t)len0;
  1988. goto _0_process;
  1989. }else{
  1990. hpatch_StreamPos_t lenv;
  1991. _decode_v_process:
  1992. self->isNeedDecode0=hpatch_TRUE;
  1993. if (!hpatch_unpackUInt(&self->code,self->code_end,&lenv)) return _hpatch_FALSE;
  1994. if (lenv>(size_t)(self->code_end-self->code)) return _hpatch_FALSE;
  1995. self->lenv=(hpatch_size_t)lenv;
  1996. goto _v_process;
  1997. }
  1998. }
  1999. static hpatch_BOOL _patch_add_old_with_rle0(_TOutStreamCache* outCache,rle0_decoder_t* rle0_decoder,
  2000. const hpatch_TStreamInput* old,hpatch_StreamPos_t oldPos,
  2001. hpatch_StreamPos_t addLength,TByte* aCache,hpatch_size_t aCacheSize){
  2002. while (addLength>0){
  2003. hpatch_size_t decodeStep=aCacheSize;
  2004. if (decodeStep>addLength)
  2005. decodeStep=(hpatch_size_t)addLength;
  2006. if (!old->read(old,oldPos,aCache,aCache+decodeStep)) return _hpatch_FALSE;
  2007. if (!_rle0_decoder_add(rle0_decoder,aCache,decodeStep)) return _hpatch_FALSE;
  2008. if (!_TOutStreamCache_write(outCache,aCache,decodeStep)) return _hpatch_FALSE;
  2009. oldPos+=decodeStep;
  2010. addLength-=decodeStep;
  2011. }
  2012. return hpatch_TRUE;
  2013. }
  2014. hpatch_BOOL sspatch_covers_nextCover(sspatch_covers_t* self){
  2015. hpatch_BOOL inc_oldPos_sign=(*(self->covers_cache))>>(8-1);
  2016. self->lastOldEnd=self->cover.oldPos+self->cover.length;
  2017. self->lastNewEnd=self->cover.newPos+self->cover.length;
  2018. if (!hpatch_unpackUIntWithTag(&self->covers_cache,self->covers_cacheEnd,&self->cover.oldPos,1)) return _hpatch_FALSE;
  2019. if (inc_oldPos_sign==0)
  2020. self->cover.oldPos+=self->lastOldEnd;
  2021. else
  2022. self->cover.oldPos=self->lastOldEnd-self->cover.oldPos;
  2023. if (!hpatch_unpackUInt(&self->covers_cache,self->covers_cacheEnd,&self->cover.newPos)) return _hpatch_FALSE;
  2024. self->cover.newPos+=self->lastNewEnd;
  2025. if (!hpatch_unpackUInt(&self->covers_cache,self->covers_cacheEnd,&self->cover.length)) return _hpatch_FALSE;
  2026. return hpatch_TRUE;
  2027. }
  2028. hpatch_BOOL patch_single_stream_diff(const hpatch_TStreamOutput* out_newData,
  2029. const hpatch_TStreamInput* oldData,
  2030. const hpatch_TStreamInput* uncompressedDiffData,
  2031. hpatch_StreamPos_t diffData_pos,
  2032. hpatch_StreamPos_t diffData_posEnd,
  2033. hpatch_StreamPos_t coverCount,hpatch_size_t stepMemSize,
  2034. unsigned char* temp_cache,unsigned char* temp_cache_end,
  2035. sspatch_coversListener_t* coversListener){
  2036. unsigned char* step_cache=temp_cache;
  2037. hpatch_size_t cache_size;
  2038. TStreamCacheClip inClip;
  2039. _TOutStreamCache outCache;
  2040. sspatch_covers_t covers;
  2041. assert(diffData_posEnd<=uncompressedDiffData->streamSize);
  2042. sspatch_covers_init(&covers);
  2043. if (coversListener) assert(coversListener->onStepCovers);
  2044. {//cache
  2045. if ((size_t)(temp_cache_end-temp_cache)<stepMemSize+hpatch_kStreamCacheSize*_kCacheSgCount) return _hpatch_FALSE;
  2046. temp_cache+=stepMemSize;
  2047. cache_size=(temp_cache_end-temp_cache)/_kCacheSgCount;
  2048. _TStreamCacheClip_init(&inClip,uncompressedDiffData,diffData_pos,diffData_posEnd,
  2049. temp_cache,cache_size);
  2050. temp_cache+=cache_size;
  2051. _TOutStreamCache_init(&outCache,out_newData,temp_cache+cache_size,cache_size);
  2052. }
  2053. while (coverCount) {//step loop
  2054. rle0_decoder_t rle0_decoder;
  2055. {//read step info
  2056. unsigned char* covers_cacheEnd;
  2057. unsigned char* bufRle_cache_end;
  2058. {
  2059. hpatch_StreamPos_t bufCover_size;
  2060. hpatch_StreamPos_t bufRle_size;
  2061. _clip_unpackUIntTo(&bufCover_size,&inClip);
  2062. _clip_unpackUIntTo(&bufRle_size,&inClip);
  2063. #ifdef __RUN_MEM_SAFE_CHECK
  2064. if ((bufCover_size>stepMemSize)|(bufRle_size>stepMemSize)|
  2065. (bufCover_size+bufRle_size>stepMemSize)) return _hpatch_FALSE;
  2066. #endif
  2067. covers_cacheEnd=step_cache+(size_t)bufCover_size;
  2068. bufRle_cache_end=covers_cacheEnd+(size_t)bufRle_size;
  2069. }
  2070. if (coversListener&&coversListener->onStepCoversReset)
  2071. coversListener->onStepCoversReset(coversListener,coverCount);
  2072. if (!_TStreamCacheClip_readDataTo(&inClip,step_cache,bufRle_cache_end))
  2073. return _hpatch_FALSE;
  2074. if (coversListener)
  2075. coversListener->onStepCovers(coversListener,step_cache,covers_cacheEnd);
  2076. sspatch_covers_setCoversCache(&covers,step_cache,covers_cacheEnd);
  2077. _rle0_decoder_init(&rle0_decoder,covers_cacheEnd,bufRle_cache_end);
  2078. }
  2079. while (sspatch_covers_isHaveNextCover(&covers)) {//cover loop
  2080. if (!sspatch_covers_nextCover(&covers))
  2081. return _hpatch_FALSE;
  2082. if (covers.cover.newPos>covers.lastNewEnd){
  2083. if (!_TOutStreamCache_copyFromClip(&outCache,&inClip,covers.cover.newPos-covers.lastNewEnd))
  2084. return _hpatch_FALSE;
  2085. }
  2086. --coverCount;
  2087. if (covers.cover.length){
  2088. #ifdef __RUN_MEM_SAFE_CHECK
  2089. if ((covers.cover.oldPos>oldData->streamSize)|
  2090. (covers.cover.length>(hpatch_StreamPos_t)(oldData->streamSize-covers.cover.oldPos))) return _hpatch_FALSE;
  2091. #endif
  2092. if (!_patch_add_old_with_rle0(&outCache,&rle0_decoder,oldData,covers.cover.oldPos,covers.cover.length,
  2093. temp_cache,cache_size)) return _hpatch_FALSE;
  2094. }else{
  2095. #ifdef __RUN_MEM_SAFE_CHECK
  2096. if (coverCount!=0) return _hpatch_FALSE;
  2097. #endif
  2098. }
  2099. }
  2100. }
  2101. if (!_TOutStreamCache_flush(&outCache))
  2102. return _hpatch_FALSE;
  2103. if (_TStreamCacheClip_isFinish(&inClip)&_TOutStreamCache_isFinish(&outCache)&(coverCount==0))
  2104. return hpatch_TRUE;
  2105. else
  2106. return _hpatch_FALSE;
  2107. }
  2108. static hpatch_BOOL _TDiffToSingleStream_read(const struct hpatch_TStreamInput* stream,hpatch_StreamPos_t readFromPos,
  2109. unsigned char* out_data,unsigned char* out_data_end){
  2110. //[ |readedSize ]
  2111. // [ |cachedBufBegin _TDiffToSingleStream_kBufSize]
  2112. // readFromPos[out_data out_data_end]
  2113. TDiffToSingleStream* self=(TDiffToSingleStream*)stream->streamImport;
  2114. hpatch_StreamPos_t readedSize=self->readedSize;
  2115. while (1){
  2116. size_t rLen=out_data_end-out_data;
  2117. if (readFromPos==readedSize){
  2118. hpatch_BOOL result=self->diffStream->read(self->diffStream,readedSize,out_data,out_data_end);
  2119. self->readedSize=readedSize+rLen;
  2120. if ((self->isInSingleStream)||(rLen>_TDiffToSingleStream_kBufSize)){
  2121. self->cachedBufBegin=_TDiffToSingleStream_kBufSize;
  2122. }else{
  2123. //cache
  2124. if (rLen>=_TDiffToSingleStream_kBufSize){
  2125. memcpy(self->buf,out_data_end-_TDiffToSingleStream_kBufSize,_TDiffToSingleStream_kBufSize);
  2126. self->cachedBufBegin = 0;
  2127. }else{
  2128. size_t new_cachedBufBegin;
  2129. if (self->cachedBufBegin>=rLen){
  2130. new_cachedBufBegin=self->cachedBufBegin-rLen;
  2131. memmove(self->buf+new_cachedBufBegin,self->buf+self->cachedBufBegin,_TDiffToSingleStream_kBufSize-self->cachedBufBegin);
  2132. }else{
  2133. new_cachedBufBegin=0;
  2134. memmove(self->buf,self->buf+rLen,_TDiffToSingleStream_kBufSize-rLen);
  2135. }
  2136. memcpy(self->buf+(_TDiffToSingleStream_kBufSize-rLen),out_data,rLen);
  2137. self->cachedBufBegin=new_cachedBufBegin;
  2138. }
  2139. }
  2140. return result;
  2141. }else{
  2142. size_t cachedSize=_TDiffToSingleStream_kBufSize-self->cachedBufBegin;
  2143. size_t bufSize=(size_t)(readedSize-readFromPos);
  2144. if ((readFromPos<readedSize)&(bufSize<=cachedSize)){
  2145. if (rLen>bufSize)
  2146. rLen=bufSize;
  2147. memcpy(out_data,self->buf+(_TDiffToSingleStream_kBufSize-bufSize),rLen);
  2148. out_data+=rLen;
  2149. readFromPos+=rLen;
  2150. if (out_data==out_data_end)
  2151. return hpatch_TRUE;
  2152. else
  2153. continue;
  2154. }else{
  2155. return _hpatch_FALSE;
  2156. }
  2157. }
  2158. }
  2159. }
  2160. void TDiffToSingleStream_init(TDiffToSingleStream* self,const hpatch_TStreamInput* diffStream){
  2161. self->base.streamImport=self;
  2162. self->base.streamSize=diffStream->streamSize;
  2163. self->base.read=_TDiffToSingleStream_read;
  2164. self->base._private_reserved=0;
  2165. self->diffStream=diffStream;
  2166. self->readedSize=0;
  2167. self->cachedBufBegin=_TDiffToSingleStream_kBufSize;
  2168. self->isInSingleStream=hpatch_FALSE;
  2169. }
  2170. hpatch_BOOL patch_single_stream(sspatch_listener_t* listener,
  2171. const hpatch_TStreamOutput* __out_newData,
  2172. const hpatch_TStreamInput* oldData,
  2173. const hpatch_TStreamInput* singleCompressedDiff,
  2174. hpatch_StreamPos_t diffInfo_pos,
  2175. sspatch_coversListener_t* coversListener){
  2176. hpatch_BOOL result=hpatch_TRUE;
  2177. hpatch_TDecompress* decompressPlugin=0;
  2178. unsigned char* temp_cache=0;
  2179. unsigned char* temp_cacheEnd=0;
  2180. hpatch_singleCompressedDiffInfo diffInfo;
  2181. hpatch_TStreamOutput _out_newData=*__out_newData;
  2182. hpatch_TStreamOutput* out_newData=&_out_newData;
  2183. TDiffToSingleStream _toSStream;
  2184. assert((listener)&&(listener->onDiffInfo));
  2185. TDiffToSingleStream_init(&_toSStream,singleCompressedDiff);
  2186. singleCompressedDiff=&_toSStream.base;
  2187. if (!getSingleCompressedDiffInfo(&diffInfo,singleCompressedDiff,diffInfo_pos))
  2188. return _hpatch_FALSE;
  2189. if (diffInfo.newDataSize>out_newData->streamSize)
  2190. return _hpatch_FALSE;
  2191. out_newData->streamSize=diffInfo.newDataSize;
  2192. if (diffInfo.oldDataSize!=oldData->streamSize)
  2193. return _hpatch_FALSE;
  2194. if (!listener->onDiffInfo(listener,&diffInfo,&decompressPlugin,&temp_cache,&temp_cacheEnd))
  2195. return _hpatch_FALSE;
  2196. if ((temp_cache==0)||(temp_cache>=temp_cacheEnd))
  2197. result=_hpatch_FALSE;
  2198. if (result){
  2199. result=patch_single_compressed_diff(out_newData,oldData,singleCompressedDiff,diffInfo.diffDataPos,
  2200. diffInfo.uncompressedSize,diffInfo.compressedSize,decompressPlugin,
  2201. diffInfo.coverCount,(size_t)diffInfo.stepMemSize,
  2202. temp_cache,temp_cacheEnd,coversListener);
  2203. }
  2204. if (listener->onPatchFinish)
  2205. listener->onPatchFinish(listener,temp_cache,temp_cacheEnd);
  2206. return result;
  2207. }