patch.c 82 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895
  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. #include "patch_private.h"
  27. #ifndef _IS_RUN_MEM_SAFE_CHECK
  28. # define _IS_RUN_MEM_SAFE_CHECK 1
  29. #endif
  30. #if (_IS_RUN_MEM_SAFE_CHECK)
  31. //__RUN_MEM_SAFE_CHECK用来启动内存访问越界检查,用以防御可能被意外或故意损坏的数据.
  32. # define __RUN_MEM_SAFE_CHECK
  33. #endif
  34. #ifdef __RUN_MEM_SAFE_CHECK
  35. # define _SAFE_CHECK_DO(code) do{ if (!(code)) return _hpatch_FALSE; }while(0)
  36. #else
  37. # define _SAFE_CHECK_DO(code) do{ code; }while(0)
  38. #endif
  39. #define _hpatch_FALSE hpatch_FALSE
  40. //hpatch_uint __hpatch_debug_check_false_x=0; //for debug
  41. //#define _hpatch_FALSE (1/__hpatch_debug_check_false_x)
  42. typedef unsigned char TByte;
  43. //变长正整数编码方案(x bit额外类型标志位,x<=7),从高位开始输出1--n byte:
  44. // x0* 7-x bit
  45. // x1* 0* 7+7-x bit
  46. // x1* 1* 0* 7+7+7-x bit
  47. // x1* 1* 1* 0* 7+7+7+7-x bit
  48. // x1* 1* 1* 1* 0* 7+7+7+7+7-x bit
  49. // ......
  50. hpatch_BOOL hpatch_packUIntWithTag(TByte** out_code,TByte* out_code_end,
  51. hpatch_StreamPos_t uValue,hpatch_uint highTag,
  52. const hpatch_uint kTagBit){//写入整数并前进指针.
  53. TByte* pcode=*out_code;
  54. const hpatch_StreamPos_t kMaxValueWithTag=((hpatch_StreamPos_t)1<<(7-kTagBit))-1;
  55. TByte codeBuf[hpatch_kMaxPackedUIntBytes];
  56. TByte* codeEnd=codeBuf;
  57. #ifdef __RUN_MEM_SAFE_CHECK
  58. //static const hpatch_uint kPackMaxTagBit=7;
  59. //assert((0<=kTagBit)&&(kTagBit<=kPackMaxTagBit));
  60. //assert((highTag>>kTagBit)==0);
  61. #endif
  62. while (uValue>kMaxValueWithTag) {
  63. *codeEnd=uValue&((1<<7)-1); ++codeEnd;
  64. uValue>>=7;
  65. }
  66. #ifdef __RUN_MEM_SAFE_CHECK
  67. if ((out_code_end-pcode)<(1+(codeEnd-codeBuf))) return _hpatch_FALSE;
  68. #endif
  69. *pcode=(TByte)( (TByte)uValue | (highTag<<(8-kTagBit))
  70. | (((codeBuf!=codeEnd)?1:0)<<(7-kTagBit)) );
  71. ++pcode;
  72. while (codeBuf!=codeEnd) {
  73. --codeEnd;
  74. *pcode=(*codeEnd) | (((codeBuf!=codeEnd)?1:0)<<7);
  75. ++pcode;
  76. }
  77. *out_code=pcode;
  78. return hpatch_TRUE;
  79. }
  80. hpatch_uint hpatch_packUIntWithTag_size(hpatch_StreamPos_t uValue,const hpatch_uint kTagBit){
  81. const hpatch_StreamPos_t kMaxValueWithTag=((hpatch_StreamPos_t)1<<(7-kTagBit))-1;
  82. hpatch_uint size=0;
  83. while (uValue>kMaxValueWithTag) {
  84. ++size;
  85. uValue>>=7;
  86. }
  87. ++size;
  88. return size;
  89. }
  90. hpatch_BOOL hpatch_unpackUIntWithTag(const TByte** src_code,const TByte* src_code_end,
  91. hpatch_StreamPos_t* result,const hpatch_uint kTagBit){//读出整数并前进指针.
  92. #ifdef __RUN_MEM_SAFE_CHECK
  93. //const hpatch_uint kPackMaxTagBit=7;
  94. #endif
  95. hpatch_StreamPos_t value;
  96. TByte code;
  97. const TByte* pcode=*src_code;
  98. #ifdef __RUN_MEM_SAFE_CHECK
  99. //assert(kTagBit<=kPackMaxTagBit);
  100. if (src_code_end<=pcode) return _hpatch_FALSE;
  101. #endif
  102. code=*pcode; ++pcode;
  103. value=code&((1<<(7-kTagBit))-1);
  104. if ((code&(1<<(7-kTagBit)))!=0){
  105. do {
  106. #ifdef __RUN_MEM_SAFE_CHECK
  107. if ((value>>(sizeof(value)*8-7))!=0) return _hpatch_FALSE;//cannot save 7bit
  108. if (src_code_end==pcode) return _hpatch_FALSE;
  109. #endif
  110. code=*pcode; ++pcode;
  111. value=(value<<7) | (code&((1<<7)-1));
  112. } while ((code&(1<<7))!=0);
  113. }
  114. (*src_code)=pcode;
  115. *result=value;
  116. return hpatch_TRUE;
  117. }
  118. static hpatch_BOOL _read_mem_stream(const hpatch_TStreamInput* stream,hpatch_StreamPos_t readFromPos,
  119. unsigned char* out_data,unsigned char* out_data_end){
  120. const unsigned char* src=(const unsigned char*)stream->streamImport;
  121. hpatch_size_t readLen=out_data_end-out_data;
  122. #ifdef __RUN_MEM_SAFE_CHECK
  123. if (readFromPos>stream->streamSize) return _hpatch_FALSE;
  124. if (readLen>(hpatch_StreamPos_t)(stream->streamSize-readFromPos)) return _hpatch_FALSE;
  125. #endif
  126. memcpy(out_data,src+readFromPos,readLen);
  127. return hpatch_TRUE;
  128. }
  129. const hpatch_TStreamInput* mem_as_hStreamInput(hpatch_TStreamInput* out_stream,
  130. const unsigned char* mem,const unsigned char* mem_end){
  131. out_stream->streamImport=(void*)mem;
  132. out_stream->streamSize=mem_end-mem;
  133. out_stream->read=_read_mem_stream;
  134. return out_stream;
  135. }
  136. static hpatch_BOOL _write_mem_stream(const hpatch_TStreamOutput* stream,hpatch_StreamPos_t writeToPos,
  137. const unsigned char* data,const unsigned char* data_end){
  138. unsigned char* out_dst=(unsigned char*)stream->streamImport;
  139. hpatch_size_t writeLen=data_end-data;
  140. #ifdef __RUN_MEM_SAFE_CHECK
  141. if (writeToPos>stream->streamSize) return _hpatch_FALSE;
  142. if (writeLen>(hpatch_StreamPos_t)(stream->streamSize-writeToPos)) return _hpatch_FALSE;
  143. #endif
  144. memcpy(out_dst+writeToPos,data,writeLen);
  145. return hpatch_TRUE;
  146. }
  147. typedef hpatch_BOOL (*_read_mem_stream_t)(const hpatch_TStreamOutput* stream,hpatch_StreamPos_t readFromPos,
  148. unsigned char* out_data,unsigned char* out_data_end);
  149. const hpatch_TStreamOutput* mem_as_hStreamOutput(hpatch_TStreamOutput* out_stream,
  150. unsigned char* mem,unsigned char* mem_end){
  151. out_stream->streamImport=mem;
  152. out_stream->streamSize=mem_end-mem;
  153. out_stream->read_writed=(_read_mem_stream_t)_read_mem_stream;
  154. out_stream->write=_write_mem_stream;
  155. return out_stream;
  156. }
  157. hpatch_BOOL hpatch_deccompress_mem(hpatch_TDecompress* decompressPlugin,
  158. const unsigned char* code,const unsigned char* code_end,
  159. unsigned char* out_data,unsigned char* out_data_end){
  160. hpatch_decompressHandle dec=0;
  161. hpatch_BOOL result,colose_rt;
  162. hpatch_TStreamInput codeStream;
  163. mem_as_hStreamInput(&codeStream,code,code_end);
  164. dec=decompressPlugin->open(decompressPlugin,(out_data_end-out_data),
  165. &codeStream,0,codeStream.streamSize);
  166. if (dec==0) return _hpatch_FALSE;
  167. result=decompressPlugin->decompress_part(dec,out_data,out_data_end);
  168. colose_rt=decompressPlugin->close(decompressPlugin,dec);
  169. assert(colose_rt);
  170. return result;
  171. }
  172. ////////
  173. //patch by memory
  174. static const hpatch_uint kSignTagBit=1;
  175. static hpatch_BOOL _bytesRle_load(TByte* out_data,TByte* out_dataEnd,
  176. const TByte* rle_code,const TByte* rle_code_end);
  177. static void addData(TByte* dst,const TByte* src,hpatch_size_t length);
  178. hpatch_inline
  179. static hpatch_BOOL _unpackUIntWithTag(const TByte** src_code,const TByte* src_code_end,
  180. hpatch_size_t* result,const hpatch_uint kTagBit){
  181. if (sizeof(hpatch_size_t)==sizeof(hpatch_StreamPos_t)){
  182. return hpatch_unpackUIntWithTag(src_code,src_code_end,(hpatch_StreamPos_t*)result,kTagBit);
  183. }else{
  184. hpatch_StreamPos_t u64=0;
  185. hpatch_BOOL rt=hpatch_unpackUIntWithTag(src_code,src_code_end,&u64,kTagBit);
  186. hpatch_size_t u=(hpatch_size_t)u64;
  187. *result=u;
  188. #ifdef __RUN_MEM_SAFE_CHECK
  189. return rt&(u==u64);
  190. #else
  191. return rt;
  192. #endif
  193. }
  194. }
  195. #define unpackUIntWithTagTo(puint,src_code,src_code_end,kTagBit) \
  196. _SAFE_CHECK_DO(_unpackUIntWithTag(src_code,src_code_end,puint,kTagBit))
  197. #define unpackUIntTo(puint,src_code,src_code_end) \
  198. unpackUIntWithTagTo(puint,src_code,src_code_end,0)
  199. hpatch_BOOL patch(TByte* out_newData,TByte* out_newData_end,
  200. const TByte* oldData,const TByte* oldData_end,
  201. const TByte* serializedDiff,const TByte* serializedDiff_end){
  202. const TByte *code_lengths, *code_lengths_end,
  203. *code_inc_newPos, *code_inc_newPos_end,
  204. *code_inc_oldPos, *code_inc_oldPos_end,
  205. *code_newDataDiff, *code_newDataDiff_end;
  206. hpatch_size_t coverCount;
  207. assert(out_newData<=out_newData_end);
  208. assert(oldData<=oldData_end);
  209. assert(serializedDiff<=serializedDiff_end);
  210. unpackUIntTo(&coverCount,&serializedDiff, serializedDiff_end);
  211. { //head
  212. hpatch_size_t lengthSize,inc_newPosSize,inc_oldPosSize,newDataDiffSize;
  213. unpackUIntTo(&lengthSize,&serializedDiff, serializedDiff_end);
  214. unpackUIntTo(&inc_newPosSize,&serializedDiff, serializedDiff_end);
  215. unpackUIntTo(&inc_oldPosSize,&serializedDiff, serializedDiff_end);
  216. unpackUIntTo(&newDataDiffSize,&serializedDiff, serializedDiff_end);
  217. #ifdef __RUN_MEM_SAFE_CHECK
  218. if (lengthSize>(hpatch_size_t)(serializedDiff_end-serializedDiff)) return _hpatch_FALSE;
  219. #endif
  220. code_lengths=serializedDiff; serializedDiff+=lengthSize;
  221. code_lengths_end=serializedDiff;
  222. #ifdef __RUN_MEM_SAFE_CHECK
  223. if (inc_newPosSize>(hpatch_size_t)(serializedDiff_end-serializedDiff)) return _hpatch_FALSE;
  224. #endif
  225. code_inc_newPos=serializedDiff; serializedDiff+=inc_newPosSize;
  226. code_inc_newPos_end=serializedDiff;
  227. #ifdef __RUN_MEM_SAFE_CHECK
  228. if (inc_oldPosSize>(hpatch_size_t)(serializedDiff_end-serializedDiff)) return _hpatch_FALSE;
  229. #endif
  230. code_inc_oldPos=serializedDiff; serializedDiff+=inc_oldPosSize;
  231. code_inc_oldPos_end=serializedDiff;
  232. #ifdef __RUN_MEM_SAFE_CHECK
  233. if (newDataDiffSize>(hpatch_size_t)(serializedDiff_end-serializedDiff)) return _hpatch_FALSE;
  234. #endif
  235. code_newDataDiff=serializedDiff; serializedDiff+=newDataDiffSize;
  236. code_newDataDiff_end=serializedDiff;
  237. }
  238. //decode rle ; rle data begin==cur serializedDiff;
  239. _SAFE_CHECK_DO(_bytesRle_load(out_newData, out_newData_end, serializedDiff, serializedDiff_end));
  240. { //patch
  241. const hpatch_size_t newDataSize=(hpatch_size_t)(out_newData_end-out_newData);
  242. hpatch_size_t oldPosBack=0;
  243. hpatch_size_t newPosBack=0;
  244. hpatch_size_t i;
  245. for (i=0; i<coverCount; ++i){
  246. hpatch_size_t copyLength,addLength, oldPos,inc_oldPos,inc_oldPos_sign;
  247. unpackUIntTo(&copyLength,&code_inc_newPos, code_inc_newPos_end);
  248. unpackUIntTo(&addLength,&code_lengths, code_lengths_end);
  249. #ifdef __RUN_MEM_SAFE_CHECK
  250. if (code_inc_oldPos>=code_inc_oldPos_end) return _hpatch_FALSE;
  251. #endif
  252. inc_oldPos_sign=(*code_inc_oldPos)>>(8-kSignTagBit);
  253. unpackUIntWithTagTo(&inc_oldPos,&code_inc_oldPos, code_inc_oldPos_end, kSignTagBit);
  254. if (inc_oldPos_sign==0)
  255. oldPos=oldPosBack+inc_oldPos;
  256. else
  257. oldPos=oldPosBack-inc_oldPos;
  258. if (copyLength>0){
  259. #ifdef __RUN_MEM_SAFE_CHECK
  260. if (copyLength>(hpatch_size_t)(newDataSize-newPosBack)) return _hpatch_FALSE;
  261. if (copyLength>(hpatch_size_t)(code_newDataDiff_end-code_newDataDiff)) return _hpatch_FALSE;
  262. #endif
  263. memcpy(out_newData+newPosBack,code_newDataDiff,copyLength);
  264. code_newDataDiff+=copyLength;
  265. newPosBack+=copyLength;
  266. }
  267. #ifdef __RUN_MEM_SAFE_CHECK
  268. if ( (addLength>(hpatch_size_t)(newDataSize-newPosBack)) ) return _hpatch_FALSE;
  269. if ( (oldPos>(hpatch_size_t)(oldData_end-oldData)) ||
  270. (addLength>(hpatch_size_t)(oldData_end-oldData-oldPos)) ) return _hpatch_FALSE;
  271. #endif
  272. addData(out_newData+newPosBack,oldData+oldPos,addLength);
  273. oldPosBack=oldPos;
  274. newPosBack+=addLength;
  275. }
  276. if (newPosBack<newDataSize){
  277. hpatch_size_t copyLength=newDataSize-newPosBack;
  278. #ifdef __RUN_MEM_SAFE_CHECK
  279. if (copyLength>(hpatch_size_t)(code_newDataDiff_end-code_newDataDiff)) return _hpatch_FALSE;
  280. #endif
  281. memcpy(out_newData+newPosBack,code_newDataDiff,copyLength);
  282. code_newDataDiff+=copyLength;
  283. //newPosBack=newDataSize;
  284. }
  285. }
  286. if ( (code_lengths==code_lengths_end)
  287. &&(code_inc_newPos==code_inc_newPos_end)
  288. &&(code_inc_oldPos==code_inc_oldPos_end)
  289. &&(code_newDataDiff==code_newDataDiff_end))
  290. return hpatch_TRUE;
  291. else
  292. return _hpatch_FALSE;
  293. }
  294. hpatch_inline static void addData(TByte* dst,const TByte* src,hpatch_size_t length){
  295. while (length--) { *dst++ += *src++; }
  296. }
  297. static hpatch_BOOL _bytesRle_load(TByte* out_data,TByte* out_dataEnd,
  298. const TByte* rle_code,const TByte* rle_code_end){
  299. const TByte* ctrlBuf,*ctrlBuf_end;
  300. hpatch_size_t ctrlSize;
  301. unpackUIntTo(&ctrlSize,&rle_code,rle_code_end);
  302. #ifdef __RUN_MEM_SAFE_CHECK
  303. if (ctrlSize>(hpatch_size_t)(rle_code_end-rle_code)) return _hpatch_FALSE;
  304. #endif
  305. ctrlBuf=rle_code;
  306. rle_code+=ctrlSize;
  307. ctrlBuf_end=rle_code;
  308. while (ctrlBuf_end-ctrlBuf>0){
  309. enum TByteRleType type=(enum TByteRleType)((*ctrlBuf)>>(8-kByteRleType_bit));
  310. hpatch_size_t length;
  311. unpackUIntWithTagTo(&length,&ctrlBuf,ctrlBuf_end,kByteRleType_bit);
  312. #ifdef __RUN_MEM_SAFE_CHECK
  313. if (length>=(hpatch_size_t)(out_dataEnd-out_data)) return _hpatch_FALSE;
  314. #endif
  315. ++length;
  316. switch (type){
  317. case kByteRleType_rle0:{
  318. memset(out_data,0,length);
  319. out_data+=length;
  320. }break;
  321. case kByteRleType_rle255:{
  322. memset(out_data,255,length);
  323. out_data+=length;
  324. }break;
  325. case kByteRleType_rle:{
  326. #ifdef __RUN_MEM_SAFE_CHECK
  327. if (1>(hpatch_size_t)(rle_code_end-rle_code)) return _hpatch_FALSE;
  328. #endif
  329. memset(out_data,*rle_code,length);
  330. ++rle_code;
  331. out_data+=length;
  332. }break;
  333. case kByteRleType_unrle:{
  334. #ifdef __RUN_MEM_SAFE_CHECK
  335. if (length>(hpatch_size_t)(rle_code_end-rle_code)) return _hpatch_FALSE;
  336. #endif
  337. memcpy(out_data,rle_code,length);
  338. rle_code+=length;
  339. out_data+=length;
  340. }break;
  341. }
  342. }
  343. if ( (ctrlBuf==ctrlBuf_end)
  344. &&(rle_code==rle_code_end)
  345. &&(out_data==out_dataEnd))
  346. return hpatch_TRUE;
  347. else
  348. return _hpatch_FALSE;
  349. }
  350. //----------------------
  351. //patch by stream
  352. static hpatch_BOOL _TStreamInputClip_read(const hpatch_TStreamInput* stream,
  353. hpatch_StreamPos_t readFromPos,
  354. unsigned char* out_data,unsigned char* out_data_end){
  355. TStreamInputClip* self=(TStreamInputClip*)stream->streamImport;
  356. #ifdef __RUN_MEM_SAFE_CHECK
  357. if (readFromPos+(out_data_end-out_data)>self->base.streamSize) return _hpatch_FALSE;
  358. #endif
  359. return self->srcStream->read(self->srcStream,readFromPos+self->clipBeginPos,out_data,out_data_end);
  360. }
  361. void TStreamInputClip_init(TStreamInputClip* self,const hpatch_TStreamInput* srcStream,
  362. hpatch_StreamPos_t clipBeginPos,hpatch_StreamPos_t clipEndPos){
  363. assert(self!=0);
  364. assert(srcStream!=0);
  365. assert(clipBeginPos<=clipEndPos);
  366. assert(clipEndPos<=srcStream->streamSize);
  367. self->srcStream=srcStream;
  368. self->clipBeginPos=clipBeginPos;
  369. self->base.streamImport=self;
  370. self->base.streamSize=clipEndPos-clipBeginPos;
  371. self->base.read=_TStreamInputClip_read;
  372. }
  373. static hpatch_BOOL _TStreamOutputClip_write(const hpatch_TStreamOutput* stream,
  374. hpatch_StreamPos_t writePos,
  375. const unsigned char* data,const unsigned char* data_end){
  376. TStreamOutputClip* self=(TStreamOutputClip*)stream->streamImport;
  377. #ifdef __RUN_MEM_SAFE_CHECK
  378. if (writePos+(data_end-data)>self->base.streamSize) return _hpatch_FALSE;
  379. #endif
  380. return self->srcStream->write(self->srcStream,writePos+self->clipBeginPos,data,data_end);
  381. }
  382. void TStreamOutputClip_init(TStreamOutputClip* self,const hpatch_TStreamOutput* srcStream,
  383. hpatch_StreamPos_t clipBeginPos,hpatch_StreamPos_t clipEndPos){
  384. assert(self!=0);
  385. assert(srcStream!=0);
  386. assert(clipBeginPos<=clipEndPos);
  387. assert(clipEndPos<=srcStream->streamSize);
  388. self->srcStream=srcStream;
  389. self->clipBeginPos=clipBeginPos;
  390. self->base.streamImport=self;
  391. self->base.streamSize=clipEndPos-clipBeginPos;
  392. ((TStreamInputClip*)self)->base.read=_TStreamInputClip_read;
  393. self->base.write=_TStreamOutputClip_write;
  394. }
  395. //assert(hpatch_kStreamCacheSize>=hpatch_kMaxPluginTypeLength+1);
  396. struct __private_hpatch_check_kMaxCompressTypeLength {
  397. char _[(hpatch_kStreamCacheSize>=(hpatch_kMaxPluginTypeLength+1))?1:-1];};
  398. hpatch_BOOL _TStreamCacheClip_readType_end(TStreamCacheClip* sclip,TByte endTag,
  399. char out_type[hpatch_kMaxPluginTypeLength+1]){
  400. const TByte* type_begin;
  401. hpatch_size_t i;
  402. hpatch_size_t readLen=hpatch_kMaxPluginTypeLength+1;
  403. if (readLen>_TStreamCacheClip_leaveSize(sclip))
  404. readLen=(hpatch_size_t)_TStreamCacheClip_leaveSize(sclip);
  405. type_begin=_TStreamCacheClip_accessData(sclip,readLen);
  406. if (type_begin==0) return _hpatch_FALSE;//not found
  407. for (i=0; i<readLen; ++i) {
  408. if (type_begin[i]!=endTag)
  409. continue;
  410. else{
  411. memcpy(out_type,type_begin,i); out_type[i]='\0';
  412. _TStreamCacheClip_skipData_noCheck(sclip,i+1);
  413. return hpatch_TRUE;
  414. }
  415. }
  416. return _hpatch_FALSE;//not found
  417. }
  418. hpatch_BOOL _TStreamCacheClip_updateCache(TStreamCacheClip* sclip){
  419. TByte* buf0=&sclip->cacheBuf[0];
  420. const hpatch_StreamPos_t streamSize=sclip->streamPos_end-sclip->streamPos;
  421. hpatch_size_t readSize=sclip->cacheBegin;
  422. if (readSize>streamSize)
  423. readSize=(hpatch_size_t)streamSize;
  424. if (readSize==0) return hpatch_TRUE;
  425. if (!_TStreamCacheClip_isCacheEmpty(sclip)){
  426. memmove(buf0+(hpatch_size_t)(sclip->cacheBegin-readSize),
  427. buf0+sclip->cacheBegin,_TStreamCacheClip_cachedSize(sclip));
  428. }
  429. if (!sclip->srcStream->read(sclip->srcStream,sclip->streamPos,
  430. buf0+(sclip->cacheEnd-readSize),buf0+sclip->cacheEnd))
  431. return _hpatch_FALSE;//read error
  432. sclip->cacheBegin-=readSize;
  433. sclip->streamPos+=readSize;
  434. return hpatch_TRUE;
  435. }
  436. hpatch_BOOL _TStreamCacheClip_skipData(TStreamCacheClip* sclip,hpatch_StreamPos_t skipLongSize){
  437. while (skipLongSize>0) {
  438. hpatch_size_t len=sclip->cacheEnd;
  439. if (len>skipLongSize)
  440. len=(hpatch_size_t)skipLongSize;
  441. if (_TStreamCacheClip_accessData(sclip,len)){
  442. _TStreamCacheClip_skipData_noCheck(sclip,len);
  443. skipLongSize-=len;
  444. }else{
  445. return _hpatch_FALSE;
  446. }
  447. }
  448. return hpatch_TRUE;
  449. }
  450. //assert(hpatch_kStreamCacheSize>=hpatch_kMaxPackedUIntBytes);
  451. struct __private_hpatch_check_hpatch_kMaxPackedUIntBytes {
  452. char _[(hpatch_kStreamCacheSize>=hpatch_kMaxPackedUIntBytes)?1:-1]; };
  453. hpatch_BOOL _TStreamCacheClip_unpackUIntWithTag(TStreamCacheClip* sclip,hpatch_StreamPos_t* result,const hpatch_uint kTagBit){
  454. TByte* curCode,*codeBegin;
  455. hpatch_size_t readSize=hpatch_kMaxPackedUIntBytes;
  456. const hpatch_StreamPos_t dataSize=_TStreamCacheClip_leaveSize(sclip);
  457. if (readSize>dataSize)
  458. readSize=(hpatch_size_t)dataSize;
  459. codeBegin=_TStreamCacheClip_accessData(sclip,readSize);
  460. if (codeBegin==0) return _hpatch_FALSE;
  461. curCode=codeBegin;
  462. _SAFE_CHECK_DO(hpatch_unpackUIntWithTag((const TByte**)&curCode,codeBegin+readSize,result,kTagBit));
  463. _TStreamCacheClip_skipData_noCheck(sclip,(hpatch_size_t)(curCode-codeBegin));
  464. return hpatch_TRUE;
  465. }
  466. hpatch_BOOL _TStreamCacheClip_readDataTo(TStreamCacheClip* sclip,TByte* out_buf,TByte* bufEnd){
  467. hpatch_size_t readLen=_TStreamCacheClip_cachedSize(sclip);
  468. hpatch_size_t outLen=bufEnd-out_buf;
  469. if (readLen>=outLen)
  470. readLen=outLen;
  471. memcpy(out_buf,&sclip->cacheBuf[sclip->cacheBegin],readLen);
  472. sclip->cacheBegin+=readLen;
  473. outLen-=readLen;
  474. if (outLen){
  475. out_buf += readLen;
  476. if (outLen<(sclip->cacheEnd>>1)){
  477. if (!_TStreamCacheClip_updateCache(sclip)) return _hpatch_FALSE;
  478. #ifdef __RUN_MEM_SAFE_CHECK
  479. if (outLen>_TStreamCacheClip_cachedSize(sclip)) return _hpatch_FALSE;
  480. #endif
  481. return _TStreamCacheClip_readDataTo(sclip, out_buf, bufEnd);
  482. }else{
  483. if (!sclip->srcStream->read(sclip->srcStream,sclip->streamPos,
  484. out_buf,bufEnd)) return _hpatch_FALSE;
  485. sclip->streamPos+=outLen;
  486. }
  487. }
  488. return hpatch_TRUE;
  489. }
  490. hpatch_BOOL _TStreamCacheClip_addDataTo(TStreamCacheClip* self,unsigned char* dst,hpatch_size_t addLen){
  491. const unsigned char* src=_TStreamCacheClip_readData(self,addLen);
  492. if (src==0) return _hpatch_FALSE;
  493. addData(dst,src,addLen);
  494. return hpatch_TRUE;
  495. }
  496. static hpatch_BOOL _decompress_read(const hpatch_TStreamInput* stream,
  497. const hpatch_StreamPos_t readFromPos,
  498. TByte* out_data,TByte* out_data_end){
  499. _TDecompressInputStream* self=(_TDecompressInputStream*)stream->streamImport;
  500. return self->decompressPlugin->decompress_part(self->decompressHandle,out_data,out_data_end);
  501. }
  502. hpatch_BOOL getStreamClip(TStreamCacheClip* out_clip,_TDecompressInputStream* out_stream,
  503. hpatch_StreamPos_t dataSize,hpatch_StreamPos_t compressedSize,
  504. const hpatch_TStreamInput* stream,hpatch_StreamPos_t* pCurStreamPos,
  505. hpatch_TDecompress* decompressPlugin,TByte* aCache,hpatch_size_t cacheSize){
  506. hpatch_StreamPos_t curStreamPos=*pCurStreamPos;
  507. if (compressedSize==0){
  508. #ifdef __RUN_MEM_SAFE_CHECK
  509. if ((curStreamPos+dataSize)<curStreamPos) return _hpatch_FALSE;
  510. if ((curStreamPos+dataSize)>stream->streamSize) return _hpatch_FALSE;
  511. #endif
  512. if (out_clip)
  513. _TStreamCacheClip_init(out_clip,stream,curStreamPos,curStreamPos+dataSize,aCache,cacheSize);
  514. curStreamPos+=dataSize;
  515. }else{
  516. #ifdef __RUN_MEM_SAFE_CHECK
  517. if ((curStreamPos+compressedSize)<curStreamPos) return _hpatch_FALSE;
  518. if ((curStreamPos+compressedSize)>stream->streamSize) return _hpatch_FALSE;
  519. #endif
  520. if (out_clip){
  521. out_stream->IInputStream.streamImport=out_stream;
  522. out_stream->IInputStream.streamSize=dataSize;
  523. out_stream->IInputStream.read=_decompress_read;
  524. out_stream->decompressPlugin=decompressPlugin;
  525. if (out_stream->decompressHandle==0){
  526. out_stream->decompressHandle=decompressPlugin->open(decompressPlugin,dataSize,stream,
  527. curStreamPos,curStreamPos+compressedSize);
  528. if (!out_stream->decompressHandle) return _hpatch_FALSE;
  529. }else{
  530. if (decompressPlugin->reset_code==0) return _hpatch_FALSE;
  531. if (!decompressPlugin->reset_code(out_stream->decompressHandle,dataSize,stream,curStreamPos,
  532. curStreamPos+compressedSize)) return _hpatch_FALSE;
  533. }
  534. _TStreamCacheClip_init(out_clip,&out_stream->IInputStream,0,
  535. out_stream->IInputStream.streamSize,aCache,cacheSize);
  536. }
  537. curStreamPos+=compressedSize;
  538. }
  539. *pCurStreamPos=curStreamPos;
  540. return hpatch_TRUE;
  541. }
  542. ///////
  543. static hpatch_inline hpatch_BOOL __TOutStreamCache_writeStream(_TOutStreamCache* self,const TByte* data,hpatch_size_t dataSize){
  544. if (!self->dstStream->write(self->dstStream,self->writeToPos,data,data+dataSize))
  545. return _hpatch_FALSE;
  546. self->writeToPos+=dataSize;
  547. return hpatch_TRUE;
  548. }
  549. hpatch_BOOL _TOutStreamCache_flush(_TOutStreamCache* self){
  550. hpatch_size_t curSize=self->cacheCur;
  551. if (curSize>0){
  552. if (!__TOutStreamCache_writeStream(self,self->cacheBuf,curSize))
  553. return _hpatch_FALSE;
  554. self->cacheCur=0;
  555. }
  556. return hpatch_TRUE;
  557. }
  558. hpatch_BOOL _TOutStreamCache_write(_TOutStreamCache* self,const TByte* data,hpatch_size_t dataSize){
  559. while (dataSize>0) {
  560. hpatch_size_t copyLen;
  561. hpatch_size_t curSize=self->cacheCur;
  562. if ((dataSize>=self->cacheEnd)&&(curSize==0)){
  563. return __TOutStreamCache_writeStream(self,data,dataSize);
  564. }
  565. copyLen=self->cacheEnd-curSize;
  566. copyLen=(copyLen<=dataSize)?copyLen:dataSize;
  567. memcpy(self->cacheBuf+curSize,data,copyLen);
  568. self->cacheCur=curSize+copyLen;
  569. data+=copyLen;
  570. dataSize-=copyLen;
  571. if (self->cacheCur==self->cacheEnd){
  572. if (!_TOutStreamCache_flush(self))
  573. return _hpatch_FALSE;
  574. }
  575. }
  576. return hpatch_TRUE;
  577. }
  578. hpatch_BOOL _TOutStreamCache_fill(_TOutStreamCache* self,hpatch_byte fillValue,hpatch_StreamPos_t fillLength){
  579. while (fillLength>0){
  580. hpatch_size_t curSize=self->cacheCur;
  581. hpatch_size_t runStep=self->cacheEnd-curSize;
  582. runStep=(runStep<=fillLength)?runStep:(hpatch_size_t)fillLength;
  583. memset(self->cacheBuf+curSize,fillValue,runStep);
  584. self->cacheCur=curSize+runStep;
  585. fillLength-=runStep;
  586. if (self->cacheCur==self->cacheEnd){
  587. if (!_TOutStreamCache_flush(self))
  588. return _hpatch_FALSE;
  589. }
  590. }
  591. return hpatch_TRUE;
  592. }
  593. hpatch_BOOL _TOutStreamCache_copyFromStream(_TOutStreamCache* self,const hpatch_TStreamInput* src,
  594. hpatch_StreamPos_t srcPos,hpatch_StreamPos_t copyLength){
  595. while (copyLength>0){
  596. hpatch_size_t curSize=self->cacheCur;
  597. hpatch_size_t runStep=self->cacheEnd-curSize;
  598. hpatch_byte* buf=self->cacheBuf+curSize;
  599. runStep=(runStep<=copyLength)?runStep:(hpatch_size_t)copyLength;
  600. if (!src->read(src,srcPos,buf,buf+runStep))
  601. return _hpatch_FALSE;
  602. srcPos+=runStep;
  603. self->cacheCur=curSize+runStep;
  604. copyLength-=runStep;
  605. if (self->cacheCur==self->cacheEnd){
  606. if (!_TOutStreamCache_flush(self))
  607. return _hpatch_FALSE;
  608. }
  609. }
  610. return hpatch_TRUE;
  611. }
  612. hpatch_BOOL _TOutStreamCache_copyFromClip(_TOutStreamCache* self,TStreamCacheClip* src,hpatch_StreamPos_t copyLength){
  613. while (copyLength>0){
  614. const TByte* data;
  615. hpatch_size_t runStep=(src->cacheEnd<=copyLength)?src->cacheEnd:(hpatch_size_t)copyLength;
  616. data=_TStreamCacheClip_readData(src,runStep);
  617. if (data==0) return
  618. _hpatch_FALSE;
  619. if (!_TOutStreamCache_write(self,data,runStep))
  620. return _hpatch_FALSE;
  621. copyLength-=runStep;
  622. }
  623. return hpatch_TRUE;
  624. }
  625. hpatch_BOOL _TOutStreamCache_copyFromSelf(_TOutStreamCache* self,hpatch_StreamPos_t aheadLength,hpatch_StreamPos_t copyLength){
  626. // [ writed ]
  627. // [ cached buf | empty buf ]
  628. const hpatch_TStreamInput* src=(const hpatch_TStreamInput*)self->dstStream;
  629. hpatch_StreamPos_t srcPos=self->writeToPos+self->cacheCur-aheadLength;
  630. if (src->read==0) //can't read
  631. return _hpatch_FALSE;
  632. if ((aheadLength<1)|(aheadLength>self->writeToPos+self->cacheCur))
  633. return _hpatch_FALSE;
  634. if (srcPos+copyLength<=self->writeToPos){//copy from stream
  635. // [ copyLength ]
  636. __copy_in_stream:
  637. return _TOutStreamCache_copyFromStream(self,src,srcPos,copyLength);
  638. }else if (srcPos>=self->writeToPos){ //copy in mem
  639. // [ copyLength ]
  640. __copy_in_mem:
  641. while (copyLength>0){
  642. hpatch_byte* dstBuf=self->cacheBuf+self->cacheCur;
  643. hpatch_byte* srcBuf=dstBuf-(hpatch_size_t)aheadLength;
  644. hpatch_size_t runLen=(self->cacheCur+copyLength<=self->cacheEnd)?(hpatch_size_t)copyLength:(self->cacheEnd-self->cacheCur);
  645. hpatch_size_t i;
  646. for (i=0;i<runLen;i++)
  647. dstBuf[i]=srcBuf[i];
  648. copyLength-=runLen;
  649. self->cacheCur+=runLen;
  650. if (self->cacheCur==self->cacheEnd){
  651. if (!_TOutStreamCache_flush(self))
  652. return _hpatch_FALSE;
  653. runLen=(hpatch_size_t)((aheadLength<=copyLength)?aheadLength:copyLength);
  654. memmove(self->cacheBuf,self->cacheBuf+self->cacheEnd-(hpatch_size_t)aheadLength,runLen);
  655. self->cacheCur=runLen;
  656. copyLength-=runLen;
  657. }else{
  658. assert(copyLength==0);
  659. }
  660. }
  661. return hpatch_TRUE;
  662. }else if (self->writeToPos+self->cacheCur<=srcPos+self->cacheEnd){
  663. // small data in stream,can as copy in mem
  664. hpatch_byte* dstBuf=self->cacheBuf+self->cacheCur;
  665. hpatch_size_t runLen=(hpatch_size_t)(self->writeToPos-srcPos);
  666. if (!src->read(src,srcPos,dstBuf,dstBuf+runLen))
  667. return _hpatch_FALSE;
  668. //srcPos+=runLen; //not used
  669. copyLength-=runLen;
  670. self->cacheCur+=runLen;
  671. if (self->cacheCur==self->cacheEnd){
  672. while (hpatch_TRUE){
  673. if (self->cacheCur==self->cacheEnd){
  674. if (!_TOutStreamCache_flush(self))
  675. return _hpatch_FALSE;
  676. }
  677. if (copyLength>0){
  678. runLen=(self->cacheEnd<=copyLength)?self->cacheEnd:(hpatch_size_t)copyLength;
  679. //srcPos+=runLen; //not used
  680. copyLength-=runLen;
  681. self->cacheCur=runLen;
  682. }else{
  683. return hpatch_TRUE;
  684. }
  685. }
  686. }else{
  687. goto __copy_in_mem;
  688. }
  689. }else{
  690. goto __copy_in_stream;
  691. }
  692. }
  693. typedef struct _TBytesRle_load_stream{
  694. hpatch_StreamPos_t memCopyLength;
  695. hpatch_StreamPos_t memSetLength;
  696. TByte memSetValue;
  697. TStreamCacheClip ctrlClip;
  698. TStreamCacheClip rleCodeClip;
  699. } _TBytesRle_load_stream;
  700. hpatch_inline
  701. static void _TBytesRle_load_stream_init(_TBytesRle_load_stream* loader){
  702. loader->memSetLength=0;
  703. loader->memSetValue=0;//nil;
  704. loader->memCopyLength=0;
  705. _TStreamCacheClip_init(&loader->ctrlClip,0,0,0,0,0);
  706. _TStreamCacheClip_init(&loader->rleCodeClip,0,0,0,0,0);
  707. }
  708. hpatch_inline static void memSet_add(TByte* dst,const TByte src,hpatch_size_t length){
  709. while (length--) { (*dst++) += src; }
  710. }
  711. static hpatch_BOOL _TBytesRle_load_stream_mem_add(_TBytesRle_load_stream* loader,
  712. hpatch_size_t* _decodeSize,TByte** _out_data){
  713. hpatch_size_t decodeSize=*_decodeSize;
  714. TByte* out_data=*_out_data;
  715. TStreamCacheClip* rleCodeClip=&loader->rleCodeClip;
  716. hpatch_StreamPos_t memSetLength=loader->memSetLength;
  717. if (memSetLength!=0){
  718. hpatch_size_t memSetStep=((memSetLength<=decodeSize)?(hpatch_size_t)memSetLength:decodeSize);
  719. const TByte byteSetValue=loader->memSetValue;
  720. if (out_data!=0){
  721. if (byteSetValue!=0)
  722. memSet_add(out_data,byteSetValue,memSetStep);
  723. out_data+=memSetStep;
  724. }
  725. decodeSize-=memSetStep;
  726. loader->memSetLength=memSetLength-memSetStep;
  727. }
  728. while ((loader->memCopyLength>0)&&(decodeSize>0)) {
  729. TByte* rleData;
  730. hpatch_size_t decodeStep=rleCodeClip->cacheEnd;
  731. if (decodeStep>loader->memCopyLength)
  732. decodeStep=(hpatch_size_t)loader->memCopyLength;
  733. if (decodeStep>decodeSize)
  734. decodeStep=decodeSize;
  735. rleData=_TStreamCacheClip_readData(rleCodeClip,decodeStep);
  736. if (rleData==0) return _hpatch_FALSE;
  737. if (out_data){
  738. addData(out_data,rleData,decodeStep);
  739. out_data+=decodeStep;
  740. }
  741. decodeSize-=decodeStep;
  742. loader->memCopyLength-=decodeStep;
  743. }
  744. *_decodeSize=decodeSize;
  745. *_out_data=out_data;
  746. return hpatch_TRUE;
  747. }
  748. hpatch_inline
  749. static hpatch_BOOL _TBytesRle_load_stream_isFinish(const _TBytesRle_load_stream* loader){
  750. return(loader->memSetLength==0)
  751. &&(loader->memCopyLength==0)
  752. &&(_TStreamCacheClip_isFinish(&loader->rleCodeClip))
  753. &&(_TStreamCacheClip_isFinish(&loader->ctrlClip));
  754. }
  755. #define _clip_unpackUIntWithTagTo(puint,sclip,kTagBit) \
  756. { if (!_TStreamCacheClip_unpackUIntWithTag(sclip,puint,kTagBit)) return _hpatch_FALSE; }
  757. #define _clip_unpackUIntTo(puint,sclip) _clip_unpackUIntWithTagTo(puint,sclip,0)
  758. static hpatch_BOOL _TBytesRle_load_stream_decode_add(_TBytesRle_load_stream* loader,
  759. TByte* out_data,hpatch_size_t decodeSize){
  760. if (!_TBytesRle_load_stream_mem_add(loader,&decodeSize,&out_data))
  761. return _hpatch_FALSE;
  762. while ((decodeSize>0)&&(!_TStreamCacheClip_isFinish(&loader->ctrlClip))){
  763. enum TByteRleType type;
  764. hpatch_StreamPos_t length;
  765. const TByte* pType=_TStreamCacheClip_accessData(&loader->ctrlClip,1);
  766. if (pType==0) return _hpatch_FALSE;
  767. type=(enum TByteRleType)((*pType)>>(8-kByteRleType_bit));
  768. _clip_unpackUIntWithTagTo(&length,&loader->ctrlClip,kByteRleType_bit);
  769. ++length;
  770. switch (type){
  771. case kByteRleType_rle0:{
  772. loader->memSetLength=length;
  773. loader->memSetValue=0;
  774. }break;
  775. case kByteRleType_rle255:{
  776. loader->memSetLength=length;
  777. loader->memSetValue=255;
  778. }break;
  779. case kByteRleType_rle:{
  780. const TByte* pSetValue=_TStreamCacheClip_readData(&loader->rleCodeClip,1);
  781. if (pSetValue==0) return _hpatch_FALSE;
  782. loader->memSetValue=*pSetValue;
  783. loader->memSetLength=length;
  784. }break;
  785. case kByteRleType_unrle:{
  786. loader->memCopyLength=length;
  787. }break;
  788. }
  789. if (!_TBytesRle_load_stream_mem_add(loader,&decodeSize,&out_data)) return _hpatch_FALSE;
  790. }
  791. if (decodeSize==0)
  792. return hpatch_TRUE;
  793. else
  794. return _hpatch_FALSE;
  795. }
  796. #define _TBytesRle_load_stream_decode_skip(loader,decodeSize) \
  797. _TBytesRle_load_stream_decode_add(loader,0,decodeSize)
  798. static hpatch_BOOL _patch_add_old_with_rle(_TOutStreamCache* outCache,_TBytesRle_load_stream* rle_loader,
  799. const hpatch_TStreamInput* old,hpatch_StreamPos_t oldPos,
  800. hpatch_StreamPos_t addLength,TByte* aCache,hpatch_size_t aCacheSize){
  801. while (addLength>0){
  802. hpatch_size_t decodeStep=aCacheSize;
  803. if (decodeStep>addLength)
  804. decodeStep=(hpatch_size_t)addLength;
  805. if (!old->read(old,oldPos,aCache,aCache+decodeStep)) return _hpatch_FALSE;
  806. if (!_TBytesRle_load_stream_decode_add(rle_loader,aCache,decodeStep)) return _hpatch_FALSE;
  807. if (!_TOutStreamCache_write(outCache,aCache,decodeStep)) return _hpatch_FALSE;
  808. oldPos+=decodeStep;
  809. addLength-=decodeStep;
  810. }
  811. return hpatch_TRUE;
  812. }
  813. typedef struct _TCovers{
  814. hpatch_TCovers ICovers;
  815. hpatch_StreamPos_t coverCount;
  816. hpatch_StreamPos_t oldPosBack;
  817. hpatch_StreamPos_t newPosBack;
  818. TStreamCacheClip* code_inc_oldPosClip;
  819. TStreamCacheClip* code_inc_newPosClip;
  820. TStreamCacheClip* code_lengthsClip;
  821. hpatch_BOOL isOldPosBackNeedAddLength;
  822. } _TCovers;
  823. static hpatch_StreamPos_t _covers_leaveCoverCount(const hpatch_TCovers* covers){
  824. const _TCovers* self=(const _TCovers*)covers;
  825. return self->coverCount;
  826. }
  827. static hpatch_BOOL _covers_close_nil(hpatch_TCovers* covers){
  828. //empty
  829. return hpatch_TRUE;
  830. }
  831. static hpatch_BOOL _covers_read_cover(hpatch_TCovers* covers,hpatch_TCover* out_cover){
  832. _TCovers* self=(_TCovers*)covers;
  833. hpatch_StreamPos_t oldPosBack=self->oldPosBack;
  834. hpatch_StreamPos_t newPosBack=self->newPosBack;
  835. hpatch_StreamPos_t coverCount=self->coverCount;
  836. if (coverCount>0)
  837. self->coverCount=coverCount-1;
  838. else
  839. return _hpatch_FALSE;
  840. {
  841. hpatch_StreamPos_t copyLength,coverLength, oldPos,inc_oldPos;
  842. TByte inc_oldPos_sign;
  843. const TByte* pSign=_TStreamCacheClip_accessData(self->code_inc_oldPosClip,1);
  844. if (pSign)
  845. inc_oldPos_sign=(*pSign)>>(8-kSignTagBit);
  846. else
  847. return _hpatch_FALSE;
  848. _clip_unpackUIntWithTagTo(&inc_oldPos,self->code_inc_oldPosClip,kSignTagBit);
  849. oldPos=(inc_oldPos_sign==0)?(oldPosBack+inc_oldPos):(oldPosBack-inc_oldPos);
  850. _clip_unpackUIntTo(&copyLength,self->code_inc_newPosClip);
  851. _clip_unpackUIntTo(&coverLength,self->code_lengthsClip);
  852. newPosBack+=copyLength;
  853. oldPosBack=oldPos;
  854. oldPosBack+=(self->isOldPosBackNeedAddLength)?coverLength:0;
  855. out_cover->oldPos=oldPos;
  856. out_cover->newPos=newPosBack;
  857. out_cover->length=coverLength;
  858. newPosBack+=coverLength;
  859. }
  860. self->oldPosBack=oldPosBack;
  861. self->newPosBack=newPosBack;
  862. return hpatch_TRUE;
  863. }
  864. static hpatch_BOOL _covers_is_finish(const struct hpatch_TCovers* covers){
  865. _TCovers* self=(_TCovers*)covers;
  866. return _TStreamCacheClip_isFinish(self->code_lengthsClip)
  867. && _TStreamCacheClip_isFinish(self->code_inc_newPosClip)
  868. && _TStreamCacheClip_isFinish(self->code_inc_oldPosClip);
  869. }
  870. static void _covers_init(_TCovers* covers,hpatch_StreamPos_t coverCount,
  871. TStreamCacheClip* code_inc_oldPosClip,
  872. TStreamCacheClip* code_inc_newPosClip,
  873. TStreamCacheClip* code_lengthsClip,
  874. hpatch_BOOL isOldPosBackNeedAddLength){
  875. covers->ICovers.leave_cover_count=_covers_leaveCoverCount;
  876. covers->ICovers.read_cover=_covers_read_cover;
  877. covers->ICovers.is_finish=_covers_is_finish;
  878. covers->ICovers.close=_covers_close_nil;
  879. covers->coverCount=coverCount;
  880. covers->newPosBack=0;
  881. covers->oldPosBack=0;
  882. covers->code_inc_oldPosClip=code_inc_oldPosClip;
  883. covers->code_inc_newPosClip=code_inc_newPosClip;
  884. covers->code_lengthsClip=code_lengthsClip;
  885. covers->isOldPosBackNeedAddLength=isOldPosBackNeedAddLength;
  886. }
  887. static hpatch_BOOL _rle_decode_skip(struct _TBytesRle_load_stream* rle_loader,hpatch_StreamPos_t copyLength){
  888. while (copyLength>0) {
  889. hpatch_size_t len=(~(hpatch_size_t)0);
  890. if (len>copyLength)
  891. len=(hpatch_size_t)copyLength;
  892. if (!_TBytesRle_load_stream_decode_skip(rle_loader,len)) return _hpatch_FALSE;
  893. copyLength-=len;
  894. }
  895. return hpatch_TRUE;
  896. }
  897. static hpatch_BOOL patchByClip(_TOutStreamCache* outCache,
  898. const hpatch_TStreamInput* oldData,
  899. hpatch_TCovers* covers,
  900. TStreamCacheClip* code_newDataDiffClip,
  901. struct _TBytesRle_load_stream* rle_loader,
  902. TByte* temp_cache,hpatch_size_t cache_size){
  903. const hpatch_StreamPos_t newDataSize=_TOutStreamCache_leaveSize(outCache);
  904. const hpatch_StreamPos_t oldDataSize=oldData->streamSize;
  905. hpatch_StreamPos_t coverCount=covers->leave_cover_count(covers);
  906. hpatch_StreamPos_t newPosBack=0;
  907. assert(cache_size>=hpatch_kMaxPackedUIntBytes);
  908. while (coverCount--){
  909. hpatch_TCover cover;
  910. if(!covers->read_cover(covers,&cover)) return _hpatch_FALSE;
  911. #ifdef __RUN_MEM_SAFE_CHECK
  912. if (cover.newPos<newPosBack) return _hpatch_FALSE;
  913. if (cover.length>(hpatch_StreamPos_t)(newDataSize-cover.newPos)) return _hpatch_FALSE;
  914. if (cover.oldPos>oldDataSize) return _hpatch_FALSE;
  915. if (cover.length>(hpatch_StreamPos_t)(oldDataSize-cover.oldPos)) return _hpatch_FALSE;
  916. #endif
  917. if (newPosBack<cover.newPos){
  918. hpatch_StreamPos_t copyLength=cover.newPos-newPosBack;
  919. if (!_TOutStreamCache_copyFromClip(outCache,code_newDataDiffClip,copyLength)) return _hpatch_FALSE;
  920. if (!_rle_decode_skip(rle_loader,copyLength)) return _hpatch_FALSE;
  921. }
  922. if (!_patch_add_old_with_rle(outCache,rle_loader,oldData,cover.oldPos,cover.length,
  923. temp_cache,cache_size)) return _hpatch_FALSE;
  924. newPosBack=cover.newPos+cover.length;
  925. }
  926. if (newPosBack<newDataSize){
  927. hpatch_StreamPos_t copyLength=newDataSize-newPosBack;
  928. if (!_TOutStreamCache_copyFromClip(outCache,code_newDataDiffClip,copyLength)) return _hpatch_FALSE;
  929. if (!_rle_decode_skip(rle_loader,copyLength)) return _hpatch_FALSE;
  930. newPosBack=newDataSize;
  931. }
  932. if (!_TOutStreamCache_flush(outCache))
  933. return _hpatch_FALSE;
  934. if ( _TBytesRle_load_stream_isFinish(rle_loader)
  935. && covers->is_finish(covers)
  936. && _TOutStreamCache_isFinish(outCache)
  937. && _TStreamCacheClip_isFinish(code_newDataDiffClip)
  938. && (newPosBack==newDataSize) )
  939. return hpatch_TRUE;
  940. else
  941. return _hpatch_FALSE;
  942. }
  943. #define _kCachePatCount 8
  944. #define _cache_alloc(dst,dst_type,memSize,temp_cache,temp_cache_end){ \
  945. if ((hpatch_size_t)(temp_cache_end-temp_cache) < \
  946. sizeof(hpatch_StreamPos_t)+(memSize)) return hpatch_FALSE; \
  947. (dst)=(dst_type*)_hpatch_align_upper(temp_cache,sizeof(hpatch_StreamPos_t));\
  948. temp_cache=(TByte*)(dst)+(hpatch_size_t)(memSize); \
  949. }
  950. typedef struct _TPackedCovers{
  951. _TCovers base;
  952. TStreamCacheClip code_inc_oldPosClip;
  953. TStreamCacheClip code_inc_newPosClip;
  954. TStreamCacheClip code_lengthsClip;
  955. } _TPackedCovers;
  956. typedef struct _THDiffHead{
  957. hpatch_StreamPos_t coverCount;
  958. hpatch_StreamPos_t lengthSize;
  959. hpatch_StreamPos_t inc_newPosSize;
  960. hpatch_StreamPos_t inc_oldPosSize;
  961. hpatch_StreamPos_t newDataDiffSize;
  962. hpatch_StreamPos_t headEndPos;
  963. hpatch_StreamPos_t coverEndPos;
  964. } _THDiffHead;
  965. static hpatch_BOOL read_diff_head(_THDiffHead* out_diffHead,
  966. const hpatch_TStreamInput* serializedDiff){
  967. hpatch_StreamPos_t diffPos0;
  968. const hpatch_StreamPos_t diffPos_end=serializedDiff->streamSize;
  969. TByte temp_cache[hpatch_kStreamCacheSize];
  970. TStreamCacheClip diffHeadClip;
  971. _TStreamCacheClip_init(&diffHeadClip,serializedDiff,0,diffPos_end,temp_cache,hpatch_kStreamCacheSize);
  972. _clip_unpackUIntTo(&out_diffHead->coverCount,&diffHeadClip);
  973. _clip_unpackUIntTo(&out_diffHead->lengthSize,&diffHeadClip);
  974. _clip_unpackUIntTo(&out_diffHead->inc_newPosSize,&diffHeadClip);
  975. _clip_unpackUIntTo(&out_diffHead->inc_oldPosSize,&diffHeadClip);
  976. _clip_unpackUIntTo(&out_diffHead->newDataDiffSize,&diffHeadClip);
  977. diffPos0=(hpatch_StreamPos_t)(_TStreamCacheClip_readPosOfSrcStream(&diffHeadClip));
  978. out_diffHead->headEndPos=diffPos0;
  979. #ifdef __RUN_MEM_SAFE_CHECK
  980. if (out_diffHead->lengthSize>(hpatch_StreamPos_t)(diffPos_end-diffPos0)) return _hpatch_FALSE;
  981. #endif
  982. diffPos0+=out_diffHead->lengthSize;
  983. #ifdef __RUN_MEM_SAFE_CHECK
  984. if (out_diffHead->inc_newPosSize>(hpatch_StreamPos_t)(diffPos_end-diffPos0)) return _hpatch_FALSE;
  985. #endif
  986. diffPos0+=out_diffHead->inc_newPosSize;
  987. #ifdef __RUN_MEM_SAFE_CHECK
  988. if (out_diffHead->inc_oldPosSize>(hpatch_StreamPos_t)(diffPos_end-diffPos0)) return _hpatch_FALSE;
  989. #endif
  990. diffPos0+=out_diffHead->inc_oldPosSize;
  991. out_diffHead->coverEndPos=diffPos0;
  992. #ifdef __RUN_MEM_SAFE_CHECK
  993. if (out_diffHead->newDataDiffSize>(hpatch_StreamPos_t)(diffPos_end-diffPos0)) return _hpatch_FALSE;
  994. #endif
  995. return hpatch_TRUE;
  996. }
  997. static hpatch_BOOL _packedCovers_open(_TPackedCovers** out_self,
  998. _THDiffHead* out_diffHead,
  999. const hpatch_TStreamInput* serializedDiff,
  1000. TByte* temp_cache,TByte* temp_cache_end){
  1001. hpatch_size_t cacheSize;
  1002. _TPackedCovers* self=0;
  1003. _cache_alloc(self,_TPackedCovers,sizeof(_TPackedCovers),temp_cache,temp_cache_end);
  1004. cacheSize=(temp_cache_end-temp_cache)/3;
  1005. {
  1006. hpatch_StreamPos_t diffPos0;
  1007. if (!read_diff_head(out_diffHead,serializedDiff)) return _hpatch_FALSE;
  1008. diffPos0=out_diffHead->headEndPos;
  1009. _TStreamCacheClip_init(&self->code_lengthsClip,serializedDiff,diffPos0,
  1010. diffPos0+out_diffHead->lengthSize,temp_cache,cacheSize);
  1011. temp_cache+=cacheSize;
  1012. diffPos0+=out_diffHead->lengthSize;
  1013. _TStreamCacheClip_init(&self->code_inc_newPosClip,serializedDiff,diffPos0,
  1014. diffPos0+out_diffHead->inc_newPosSize,temp_cache,cacheSize);
  1015. temp_cache+=cacheSize;
  1016. diffPos0+=out_diffHead->inc_newPosSize;
  1017. _TStreamCacheClip_init(&self->code_inc_oldPosClip,serializedDiff,diffPos0,
  1018. diffPos0+out_diffHead->inc_oldPosSize,temp_cache,cacheSize);
  1019. }
  1020. _covers_init(&self->base,out_diffHead->coverCount,&self->code_inc_oldPosClip,
  1021. &self->code_inc_newPosClip,&self->code_lengthsClip,hpatch_FALSE);
  1022. *out_self=self;
  1023. return hpatch_TRUE;
  1024. }
  1025. static hpatch_BOOL _patch_stream_with_cache(const hpatch_TStreamOutput* out_newData,
  1026. const hpatch_TStreamInput* oldData,
  1027. const hpatch_TStreamInput* serializedDiff,
  1028. hpatch_TCovers* cached_covers,
  1029. TByte* temp_cache,TByte* temp_cache_end){
  1030. struct _THDiffHead diffHead;
  1031. TStreamCacheClip code_newDataDiffClip;
  1032. struct _TBytesRle_load_stream rle_loader;
  1033. hpatch_TCovers* pcovers=0;
  1034. hpatch_StreamPos_t diffPos0;
  1035. const hpatch_StreamPos_t diffPos_end=serializedDiff->streamSize;
  1036. const hpatch_size_t cacheSize=(temp_cache_end-temp_cache)/(cached_covers?(_kCachePatCount-3):_kCachePatCount);
  1037. assert(out_newData!=0);
  1038. assert(out_newData->write!=0);
  1039. assert(oldData!=0);
  1040. assert(oldData->read!=0);
  1041. assert(serializedDiff!=0);
  1042. assert(serializedDiff->read!=0);
  1043. //covers
  1044. if (cached_covers==0){
  1045. struct _TPackedCovers* packedCovers;
  1046. if (!_packedCovers_open(&packedCovers,&diffHead,serializedDiff,temp_cache+cacheSize*(_kCachePatCount-3),
  1047. temp_cache_end)) return _hpatch_FALSE;
  1048. pcovers=&packedCovers->base.ICovers; //not need close before return
  1049. }else{
  1050. pcovers=cached_covers;
  1051. if (!read_diff_head(&diffHead,serializedDiff)) return _hpatch_FALSE;
  1052. }
  1053. //newDataDiff
  1054. diffPos0=diffHead.coverEndPos;
  1055. _TStreamCacheClip_init(&code_newDataDiffClip,serializedDiff,diffPos0,
  1056. diffPos0+diffHead.newDataDiffSize,temp_cache,cacheSize);
  1057. temp_cache+=cacheSize;
  1058. diffPos0+=diffHead.newDataDiffSize;
  1059. {//rle
  1060. hpatch_StreamPos_t rleCtrlSize;
  1061. hpatch_StreamPos_t rlePos0;
  1062. TStreamCacheClip* rleHeadClip=&rle_loader.ctrlClip;//rename, share address
  1063. #ifdef __RUN_MEM_SAFE_CHECK
  1064. if (cacheSize<hpatch_kMaxPackedUIntBytes) return _hpatch_FALSE;
  1065. #endif
  1066. _TStreamCacheClip_init(rleHeadClip,serializedDiff,diffPos0,diffPos_end,
  1067. temp_cache,hpatch_kMaxPackedUIntBytes);
  1068. _clip_unpackUIntTo(&rleCtrlSize,rleHeadClip);
  1069. rlePos0=(hpatch_StreamPos_t)(_TStreamCacheClip_readPosOfSrcStream(rleHeadClip));
  1070. #ifdef __RUN_MEM_SAFE_CHECK
  1071. if (rleCtrlSize>(hpatch_StreamPos_t)(diffPos_end-rlePos0)) return _hpatch_FALSE;
  1072. #endif
  1073. _TBytesRle_load_stream_init(&rle_loader);
  1074. _TStreamCacheClip_init(&rle_loader.ctrlClip,serializedDiff,rlePos0,rlePos0+rleCtrlSize,
  1075. temp_cache,cacheSize);
  1076. temp_cache+=cacheSize;
  1077. _TStreamCacheClip_init(&rle_loader.rleCodeClip,serializedDiff,rlePos0+rleCtrlSize,diffPos_end,
  1078. temp_cache,cacheSize);
  1079. temp_cache+=cacheSize;
  1080. }
  1081. {
  1082. _TOutStreamCache outCache;
  1083. _TOutStreamCache_init(&outCache,out_newData,temp_cache,cacheSize);
  1084. temp_cache+=cacheSize;
  1085. return patchByClip(&outCache,oldData,pcovers,&code_newDataDiffClip,
  1086. &rle_loader,temp_cache,cacheSize);
  1087. }
  1088. }
  1089. hpatch_BOOL read_diffz_head(hpatch_compressedDiffInfo* out_diffInfo,_THDiffzHead* out_head,
  1090. const hpatch_TStreamInput* compressedDiff){
  1091. TStreamCacheClip _diffHeadClip;
  1092. TStreamCacheClip* diffHeadClip=&_diffHeadClip;
  1093. TByte temp_cache[hpatch_kStreamCacheSize];
  1094. _TStreamCacheClip_init(&_diffHeadClip,compressedDiff,0,compressedDiff->streamSize,
  1095. temp_cache,hpatch_kStreamCacheSize);
  1096. {//type
  1097. const char* kVersionType="HDIFF13";
  1098. char* tempType=out_diffInfo->compressType;
  1099. if (!_TStreamCacheClip_readType_end(diffHeadClip,'&',tempType)) return _hpatch_FALSE;
  1100. if (0!=strcmp(tempType,kVersionType)) return _hpatch_FALSE;
  1101. }
  1102. {//read compressType
  1103. if (!_TStreamCacheClip_readType_end(diffHeadClip,'\0',
  1104. out_diffInfo->compressType)) return _hpatch_FALSE;
  1105. out_head->typesEndPos=_TStreamCacheClip_readPosOfSrcStream(diffHeadClip);
  1106. }
  1107. _clip_unpackUIntTo(&out_diffInfo->newDataSize,diffHeadClip);
  1108. _clip_unpackUIntTo(&out_diffInfo->oldDataSize,diffHeadClip);
  1109. _clip_unpackUIntTo(&out_head->coverCount,diffHeadClip);
  1110. out_head->compressSizeBeginPos=_TStreamCacheClip_readPosOfSrcStream(diffHeadClip);
  1111. _clip_unpackUIntTo(&out_head->cover_buf_size,diffHeadClip);
  1112. _clip_unpackUIntTo(&out_head->compress_cover_buf_size,diffHeadClip);
  1113. _clip_unpackUIntTo(&out_head->rle_ctrlBuf_size,diffHeadClip);
  1114. _clip_unpackUIntTo(&out_head->compress_rle_ctrlBuf_size,diffHeadClip);
  1115. _clip_unpackUIntTo(&out_head->rle_codeBuf_size,diffHeadClip);
  1116. _clip_unpackUIntTo(&out_head->compress_rle_codeBuf_size,diffHeadClip);
  1117. _clip_unpackUIntTo(&out_head->newDataDiff_size,diffHeadClip);
  1118. _clip_unpackUIntTo(&out_head->compress_newDataDiff_size,diffHeadClip);
  1119. out_head->headEndPos=_TStreamCacheClip_readPosOfSrcStream(diffHeadClip);
  1120. out_diffInfo->compressedCount=((out_head->compress_cover_buf_size)?1:0)
  1121. +((out_head->compress_rle_ctrlBuf_size)?1:0)
  1122. +((out_head->compress_rle_codeBuf_size)?1:0)
  1123. +((out_head->compress_newDataDiff_size)?1:0);
  1124. if (out_head->compress_cover_buf_size>0)
  1125. out_head->coverEndPos=out_head->headEndPos+out_head->compress_cover_buf_size;
  1126. else
  1127. out_head->coverEndPos=out_head->headEndPos+out_head->cover_buf_size;
  1128. return hpatch_TRUE;
  1129. }
  1130. hpatch_BOOL getCompressedDiffInfo(hpatch_compressedDiffInfo* out_diffInfo,
  1131. const hpatch_TStreamInput* compressedDiff){
  1132. _THDiffzHead head;
  1133. assert(out_diffInfo!=0);
  1134. assert(compressedDiff!=0);
  1135. assert(compressedDiff->read!=0);
  1136. return read_diffz_head(out_diffInfo,&head,compressedDiff);
  1137. }
  1138. #define _clear_return(exitValue) { result=exitValue; goto clear; }
  1139. #define _kCacheDecCount 6
  1140. static
  1141. hpatch_BOOL _patch_decompress_cache(const hpatch_TStreamOutput* out_newData,
  1142. hpatch_TStreamInput* once_in_newData,
  1143. const hpatch_TStreamInput* oldData,
  1144. const hpatch_TStreamInput* compressedDiff,
  1145. hpatch_TDecompress* decompressPlugin,
  1146. hpatch_TCovers* cached_covers,
  1147. TByte* temp_cache, TByte* temp_cache_end){
  1148. TStreamCacheClip coverClip;
  1149. TStreamCacheClip code_newDataDiffClip;
  1150. struct _TBytesRle_load_stream rle_loader;
  1151. _THDiffzHead head;
  1152. hpatch_compressedDiffInfo diffInfo;
  1153. _TDecompressInputStream decompressers[4];
  1154. hpatch_size_t i;
  1155. hpatch_StreamPos_t coverCount;
  1156. hpatch_BOOL result=hpatch_TRUE;
  1157. hpatch_StreamPos_t diffPos0=0;
  1158. const hpatch_StreamPos_t diffPos_end=compressedDiff->streamSize;
  1159. const hpatch_size_t cacheSize=(temp_cache_end-temp_cache)/(cached_covers?(_kCacheDecCount-1):_kCacheDecCount);
  1160. if (cacheSize<=hpatch_kMaxPluginTypeLength) return _hpatch_FALSE;
  1161. assert(out_newData!=0);
  1162. assert(out_newData->write!=0);
  1163. assert(oldData!=0);
  1164. assert(oldData->read!=0);
  1165. assert(compressedDiff!=0);
  1166. assert(compressedDiff->read!=0);
  1167. {//head
  1168. if (!read_diffz_head(&diffInfo,&head,compressedDiff)) return _hpatch_FALSE;
  1169. if ((diffInfo.oldDataSize!=oldData->streamSize)
  1170. ||(diffInfo.newDataSize!=out_newData->streamSize)) return _hpatch_FALSE;
  1171. if ((decompressPlugin==0)&&(diffInfo.compressedCount!=0)) return _hpatch_FALSE;
  1172. if ((decompressPlugin)&&(diffInfo.compressedCount>0))
  1173. if (!decompressPlugin->is_can_open(diffInfo.compressType)) return _hpatch_FALSE;
  1174. diffPos0=head.headEndPos;
  1175. }
  1176. for (i=0;i<sizeof(decompressers)/sizeof(_TDecompressInputStream);++i)
  1177. decompressers[i].decompressHandle=0;
  1178. _TBytesRle_load_stream_init(&rle_loader);
  1179. if (cached_covers){
  1180. diffPos0=head.coverEndPos;
  1181. }else{
  1182. if (!getStreamClip(&coverClip,&decompressers[0],
  1183. head.cover_buf_size,head.compress_cover_buf_size,compressedDiff,&diffPos0,
  1184. decompressPlugin,temp_cache+cacheSize*(_kCacheDecCount-1),cacheSize)) _clear_return(_hpatch_FALSE);
  1185. }
  1186. if (!getStreamClip(&rle_loader.ctrlClip,&decompressers[1],
  1187. head.rle_ctrlBuf_size,head.compress_rle_ctrlBuf_size,compressedDiff,&diffPos0,
  1188. decompressPlugin,temp_cache,cacheSize)) _clear_return(_hpatch_FALSE);
  1189. temp_cache+=cacheSize;
  1190. if (!getStreamClip(&rle_loader.rleCodeClip,&decompressers[2],
  1191. head.rle_codeBuf_size,head.compress_rle_codeBuf_size,compressedDiff,&diffPos0,
  1192. decompressPlugin,temp_cache,cacheSize)) _clear_return(_hpatch_FALSE);
  1193. temp_cache+=cacheSize;
  1194. if (!getStreamClip(&code_newDataDiffClip,&decompressers[3],
  1195. head.newDataDiff_size,head.compress_newDataDiff_size,compressedDiff,&diffPos0,
  1196. decompressPlugin,temp_cache,cacheSize)) _clear_return(_hpatch_FALSE);
  1197. temp_cache+=cacheSize;
  1198. #ifdef __RUN_MEM_SAFE_CHECK
  1199. if (diffPos0!=diffPos_end) _clear_return(_hpatch_FALSE);
  1200. #endif
  1201. coverCount=head.coverCount;
  1202. {
  1203. _TCovers covers;
  1204. hpatch_TCovers* pcovers=0;
  1205. _TOutStreamCache outCache;
  1206. _TOutStreamCache_init(&outCache,out_newData,temp_cache,cacheSize);
  1207. temp_cache+=cacheSize;
  1208. if (cached_covers){
  1209. pcovers=cached_covers;
  1210. }else{
  1211. _covers_init(&covers,coverCount,&coverClip,&coverClip,&coverClip,hpatch_TRUE);
  1212. pcovers=&covers.ICovers; //not need close before return
  1213. }
  1214. result=patchByClip(&outCache,oldData,pcovers,&code_newDataDiffClip,&rle_loader,
  1215. temp_cache,cacheSize);
  1216. //if ((pcovers!=cached_covers)&&(!pcovers->close(pcovers))) result=_hpatch_FALSE;
  1217. }
  1218. clear:
  1219. for (i=0;i<sizeof(decompressers)/sizeof(_TDecompressInputStream);++i) {
  1220. if (decompressers[i].decompressHandle){
  1221. if (!decompressPlugin->close(decompressPlugin,decompressers[i].decompressHandle))
  1222. result=_hpatch_FALSE;
  1223. decompressers[i].decompressHandle=0;
  1224. }
  1225. }
  1226. return result;
  1227. }
  1228. hpatch_inline static hpatch_BOOL _cache_load_all(const hpatch_TStreamInput* data,
  1229. TByte* cache,TByte* cache_end){
  1230. assert((hpatch_size_t)(cache_end-cache)==data->streamSize);
  1231. return data->read(data,0,cache,cache_end);
  1232. }
  1233. typedef struct _TCompressedCovers{
  1234. _TCovers base;
  1235. TStreamCacheClip coverClip;
  1236. _TDecompressInputStream decompresser;
  1237. } _TCompressedCovers;
  1238. static hpatch_BOOL _compressedCovers_close(hpatch_TCovers* covers){
  1239. hpatch_BOOL result=hpatch_TRUE;
  1240. _TCompressedCovers* self=(_TCompressedCovers*)covers;
  1241. if (self){
  1242. if (self->decompresser.decompressHandle){
  1243. result=self->decompresser.decompressPlugin->close(self->decompresser.decompressPlugin,
  1244. self->decompresser.decompressHandle);
  1245. self->decompresser.decompressHandle=0;
  1246. }
  1247. }
  1248. return result;
  1249. }
  1250. static hpatch_BOOL _compressedCovers_open(_TCompressedCovers** out_self,
  1251. hpatch_compressedDiffInfo* out_diffInfo,
  1252. const hpatch_TStreamInput* compressedDiff,
  1253. hpatch_TDecompress* decompressPlugin,
  1254. TByte* temp_cache,TByte* temp_cache_end){
  1255. _THDiffzHead head;
  1256. hpatch_StreamPos_t diffPos0=0;
  1257. _TCompressedCovers* self=0;
  1258. _cache_alloc(self,_TCompressedCovers,sizeof(_TCompressedCovers),temp_cache,temp_cache_end);
  1259. if (!read_diffz_head(out_diffInfo,&head,compressedDiff)) return _hpatch_FALSE;
  1260. diffPos0=head.headEndPos;
  1261. if (head.compress_cover_buf_size>0){
  1262. if (decompressPlugin==0) return _hpatch_FALSE;
  1263. if (!decompressPlugin->is_can_open(out_diffInfo->compressType)) return _hpatch_FALSE;
  1264. }
  1265. _covers_init(&self->base,head.coverCount,&self->coverClip,
  1266. &self->coverClip,&self->coverClip,hpatch_TRUE);
  1267. self->base.ICovers.close=_compressedCovers_close;
  1268. memset(&self->decompresser,0, sizeof(self->decompresser));
  1269. if (!getStreamClip(&self->coverClip,&self->decompresser,
  1270. head.cover_buf_size,head.compress_cover_buf_size,
  1271. compressedDiff,&diffPos0,decompressPlugin,
  1272. temp_cache,temp_cache_end-temp_cache)) {
  1273. return _hpatch_FALSE;
  1274. };
  1275. *out_self=self;
  1276. return hpatch_TRUE;
  1277. }
  1278. static hpatch_BOOL _patch_cache(hpatch_TCovers** out_covers,
  1279. const hpatch_TStreamInput** poldData,hpatch_StreamPos_t newDataSize,
  1280. const hpatch_TStreamInput* diffData,hpatch_BOOL isCompressedDiff,
  1281. hpatch_TDecompress* decompressPlugin,size_t kCacheCount,
  1282. TByte** ptemp_cache,TByte** ptemp_cache_end,hpatch_BOOL* out_isReadError){
  1283. const hpatch_TStreamInput* oldData=*poldData;
  1284. const hpatch_size_t kMinCacheSize=hpatch_kStreamCacheSize*kCacheCount;
  1285. TByte* temp_cache=*ptemp_cache;
  1286. TByte* temp_cache_end=*ptemp_cache_end;
  1287. *out_isReadError=hpatch_FALSE;
  1288. if ((hpatch_size_t)(temp_cache_end-temp_cache)>=oldData->streamSize+kMinCacheSize
  1289. +sizeof(hpatch_TStreamInput)+sizeof(hpatch_StreamPos_t)){//load all oldData
  1290. hpatch_TStreamInput* replace_oldData=0;
  1291. _cache_alloc(replace_oldData,hpatch_TStreamInput,sizeof(hpatch_TStreamInput),
  1292. temp_cache,temp_cache_end);
  1293. if (!_cache_load_all(oldData,temp_cache_end-oldData->streamSize,
  1294. temp_cache_end)){ *out_isReadError=hpatch_TRUE; return _hpatch_FALSE; }
  1295. mem_as_hStreamInput(replace_oldData,temp_cache_end-oldData->streamSize,temp_cache_end);
  1296. temp_cache_end-=oldData->streamSize;
  1297. // [ patch cache | oldData cache ]
  1298. // [ (cacheSize-oldData->streamSize) | (oldData->streamSize) ]
  1299. *out_covers=0;
  1300. *poldData=replace_oldData;
  1301. *ptemp_cache=temp_cache;
  1302. *ptemp_cache_end=temp_cache_end;
  1303. return hpatch_TRUE;
  1304. }
  1305. return hpatch_FALSE;//not cache oldData
  1306. }
  1307. hpatch_BOOL patch_stream_with_cache(const struct hpatch_TStreamOutput* out_newData,
  1308. const struct hpatch_TStreamInput* oldData,
  1309. const struct hpatch_TStreamInput* serializedDiff,
  1310. TByte* temp_cache,TByte* temp_cache_end){
  1311. hpatch_BOOL result;
  1312. hpatch_TCovers* covers=0;//not need close before return
  1313. hpatch_BOOL isReadError=hpatch_FALSE;
  1314. _patch_cache(&covers,&oldData,out_newData->streamSize,serializedDiff,hpatch_FALSE,0,
  1315. _kCachePatCount,&temp_cache,&temp_cache_end,&isReadError);
  1316. if (isReadError) return _hpatch_FALSE;
  1317. result=_patch_stream_with_cache(out_newData,oldData,serializedDiff,covers,
  1318. temp_cache,temp_cache_end);
  1319. //if ((covers!=0)&&(!covers->close(covers))) result=_hpatch_FALSE;
  1320. return result;
  1321. }
  1322. hpatch_BOOL patch_stream(const hpatch_TStreamOutput* out_newData,
  1323. const hpatch_TStreamInput* oldData,
  1324. const hpatch_TStreamInput* serializedDiff){
  1325. TByte temp_cache[hpatch_kStreamCacheSize*_kCachePatCount];
  1326. return _patch_stream_with_cache(out_newData,oldData,serializedDiff,0,
  1327. temp_cache,temp_cache+sizeof(temp_cache)/sizeof(TByte));
  1328. }
  1329. hpatch_BOOL patch_decompress_with_cache(const hpatch_TStreamOutput* out_newData,
  1330. const hpatch_TStreamInput* oldData,
  1331. const hpatch_TStreamInput* compressedDiff,
  1332. hpatch_TDecompress* decompressPlugin,
  1333. TByte* temp_cache,TByte* temp_cache_end){
  1334. hpatch_BOOL result;
  1335. hpatch_TCovers* covers=0; //need close before return
  1336. hpatch_BOOL isReadError=hpatch_FALSE;
  1337. _patch_cache(&covers,&oldData,out_newData->streamSize,compressedDiff,hpatch_TRUE,
  1338. decompressPlugin,_kCacheDecCount,&temp_cache,&temp_cache_end,&isReadError);
  1339. if (isReadError) return _hpatch_FALSE;
  1340. result=_patch_decompress_cache(out_newData,0,oldData,compressedDiff,decompressPlugin,
  1341. covers,temp_cache,temp_cache_end);
  1342. if ((covers!=0)&&(!covers->close(covers))) result=_hpatch_FALSE;
  1343. return result;
  1344. }
  1345. hpatch_BOOL patch_decompress(const hpatch_TStreamOutput* out_newData,
  1346. const hpatch_TStreamInput* oldData,
  1347. const hpatch_TStreamInput* compressedDiff,
  1348. hpatch_TDecompress* decompressPlugin){
  1349. TByte temp_cache[hpatch_kStreamCacheSize*_kCacheDecCount];
  1350. return _patch_decompress_cache(out_newData,0,oldData,compressedDiff,decompressPlugin,
  1351. 0,temp_cache,temp_cache+sizeof(temp_cache)/sizeof(TByte));
  1352. }
  1353. hpatch_BOOL hpatch_coverList_open_serializedDiff(hpatch_TCoverList* out_coverList,
  1354. const hpatch_TStreamInput* serializedDiff){
  1355. TByte* temp_cache;
  1356. TByte* temp_cache_end;
  1357. _TPackedCovers* packedCovers=0;
  1358. _THDiffHead diffHead;
  1359. assert((out_coverList!=0)&&(out_coverList->ICovers==0));
  1360. temp_cache=out_coverList->_buf;
  1361. temp_cache_end=temp_cache+sizeof(out_coverList->_buf);
  1362. if (!_packedCovers_open(&packedCovers,&diffHead,serializedDiff,
  1363. temp_cache,temp_cache_end))
  1364. return _hpatch_FALSE;
  1365. out_coverList->ICovers=&packedCovers->base.ICovers;
  1366. return hpatch_TRUE;
  1367. }
  1368. hpatch_BOOL hpatch_coverList_open_compressedDiff(hpatch_TCoverList* out_coverList,
  1369. const hpatch_TStreamInput* compressedDiff,
  1370. hpatch_TDecompress* decompressPlugin){
  1371. TByte* temp_cache;
  1372. TByte* temp_cache_end;
  1373. _TCompressedCovers* compressedCovers=0;
  1374. hpatch_compressedDiffInfo diffInfo;
  1375. assert((out_coverList!=0)&&(out_coverList->ICovers==0));
  1376. temp_cache=out_coverList->_buf;
  1377. temp_cache_end=temp_cache+sizeof(out_coverList->_buf);
  1378. if (!_compressedCovers_open(&compressedCovers,&diffInfo,compressedDiff,decompressPlugin,
  1379. temp_cache,temp_cache_end))
  1380. return _hpatch_FALSE;
  1381. out_coverList->ICovers=&compressedCovers->base.ICovers;
  1382. return hpatch_TRUE;
  1383. }
  1384. //
  1385. #define _kCacheSgCount 3
  1386. hpatch_BOOL patch_single_compressed_diff(const hpatch_TStreamOutput* out_newData,
  1387. const hpatch_TStreamInput* oldData,
  1388. const hpatch_TStreamInput* singleCompressedDiff,
  1389. hpatch_StreamPos_t diffData_pos,
  1390. hpatch_StreamPos_t uncompressedSize,
  1391. hpatch_StreamPos_t compressedSize,
  1392. hpatch_TDecompress* decompressPlugin,
  1393. hpatch_StreamPos_t coverCount,hpatch_size_t stepMemSize,
  1394. unsigned char* temp_cache,unsigned char* temp_cache_end,
  1395. sspatch_coversListener_t* coversListener){
  1396. hpatch_BOOL result;
  1397. hpatch_TUncompresser_t uncompressedStream;
  1398. hpatch_StreamPos_t diffData_posEnd;
  1399. memset(&uncompressedStream,0,sizeof(uncompressedStream));
  1400. if (compressedSize==0){
  1401. decompressPlugin=0;
  1402. }else{
  1403. if (decompressPlugin==0) return _hpatch_FALSE;
  1404. }
  1405. diffData_posEnd=(decompressPlugin?compressedSize:uncompressedSize)+diffData_pos;
  1406. if (diffData_posEnd>singleCompressedDiff->streamSize) return _hpatch_FALSE;
  1407. if (decompressPlugin){
  1408. if (!compressed_stream_as_uncompressed(&uncompressedStream,uncompressedSize,decompressPlugin,singleCompressedDiff,
  1409. diffData_pos,diffData_posEnd)) return _hpatch_FALSE;
  1410. singleCompressedDiff=&uncompressedStream.base;
  1411. diffData_pos=0;
  1412. diffData_posEnd=singleCompressedDiff->streamSize;
  1413. }
  1414. result=patch_single_stream_diff(out_newData,oldData,singleCompressedDiff,diffData_pos,diffData_posEnd,
  1415. coverCount,stepMemSize,temp_cache,temp_cache_end,coversListener);
  1416. if (decompressPlugin)
  1417. close_compressed_stream_as_uncompressed(&uncompressedStream);
  1418. return result;
  1419. }
  1420. static const size_t _kStepMemSizeSafeLimit =(1<<20)*16;
  1421. hpatch_BOOL getSingleCompressedDiffInfo(hpatch_singleCompressedDiffInfo* out_diffInfo,
  1422. const hpatch_TStreamInput* singleCompressedDiff,
  1423. hpatch_StreamPos_t diffInfo_pos){
  1424. TStreamCacheClip _diffHeadClip;
  1425. TStreamCacheClip* diffHeadClip=&_diffHeadClip;
  1426. TByte temp_cache[hpatch_kStreamCacheSize];
  1427. _TStreamCacheClip_init(&_diffHeadClip,singleCompressedDiff,diffInfo_pos,singleCompressedDiff->streamSize,
  1428. temp_cache,hpatch_kStreamCacheSize);
  1429. {//type
  1430. const char* kVersionType="HDIFFSF20";
  1431. char* tempType=out_diffInfo->compressType;
  1432. if (!_TStreamCacheClip_readType_end(diffHeadClip,'&',tempType)) return _hpatch_FALSE;
  1433. if (0!=strcmp(tempType,kVersionType)) return _hpatch_FALSE;
  1434. }
  1435. {//read compressType
  1436. if (!_TStreamCacheClip_readType_end(diffHeadClip,'\0',
  1437. out_diffInfo->compressType)) return _hpatch_FALSE;
  1438. }
  1439. _clip_unpackUIntTo(&out_diffInfo->newDataSize,diffHeadClip);
  1440. _clip_unpackUIntTo(&out_diffInfo->oldDataSize,diffHeadClip);
  1441. _clip_unpackUIntTo(&out_diffInfo->coverCount,diffHeadClip);
  1442. _clip_unpackUIntTo(&out_diffInfo->stepMemSize,diffHeadClip);
  1443. _clip_unpackUIntTo(&out_diffInfo->uncompressedSize,diffHeadClip);
  1444. _clip_unpackUIntTo(&out_diffInfo->compressedSize,diffHeadClip);
  1445. out_diffInfo->diffDataPos=_TStreamCacheClip_readPosOfSrcStream(diffHeadClip)-diffInfo_pos;
  1446. if (out_diffInfo->compressedSize>out_diffInfo->uncompressedSize)
  1447. return _hpatch_FALSE;
  1448. if (out_diffInfo->stepMemSize>(out_diffInfo->newDataSize>=_kStepMemSizeSafeLimit?out_diffInfo->newDataSize:_kStepMemSizeSafeLimit))
  1449. return _hpatch_FALSE;
  1450. if (out_diffInfo->stepMemSize>out_diffInfo->uncompressedSize)
  1451. return _hpatch_FALSE;
  1452. return hpatch_TRUE;
  1453. }
  1454. static hpatch_BOOL _TUncompresser_read(const struct hpatch_TStreamInput* stream,hpatch_StreamPos_t readFromPos,
  1455. unsigned char* out_data,unsigned char* out_data_end){
  1456. hpatch_TUncompresser_t* self=(hpatch_TUncompresser_t*)stream->streamImport;
  1457. return self->_decompressPlugin->decompress_part(self->_decompressHandle,out_data,out_data_end);
  1458. }
  1459. hpatch_BOOL compressed_stream_as_uncompressed(hpatch_TUncompresser_t* uncompressedStream,hpatch_StreamPos_t uncompressedSize,
  1460. hpatch_TDecompress* decompressPlugin,const hpatch_TStreamInput* compressedStream,
  1461. hpatch_StreamPos_t compressed_pos,hpatch_StreamPos_t compressed_end){
  1462. hpatch_TUncompresser_t* self=uncompressedStream;
  1463. assert(decompressPlugin!=0);
  1464. assert(self->_decompressHandle==0);
  1465. self->_decompressHandle=decompressPlugin->open(decompressPlugin,uncompressedSize,compressedStream,
  1466. compressed_pos,compressed_end);
  1467. if (self->_decompressHandle==0) return _hpatch_FALSE;
  1468. self->_decompressPlugin=decompressPlugin;
  1469. self->base.streamImport=self;
  1470. self->base.streamSize=uncompressedSize;
  1471. self->base.read=_TUncompresser_read;
  1472. return hpatch_TRUE;
  1473. }
  1474. void close_compressed_stream_as_uncompressed(hpatch_TUncompresser_t* uncompressedStream){
  1475. hpatch_TUncompresser_t* self=uncompressedStream;
  1476. if (self==0) return;
  1477. if (self->_decompressHandle==0) return;
  1478. self->_decompressPlugin->close(self->_decompressPlugin,self->_decompressHandle);
  1479. self->_decompressHandle=0;
  1480. }
  1481. typedef struct{
  1482. const unsigned char* code;
  1483. const unsigned char* code_end;
  1484. hpatch_size_t len0;
  1485. hpatch_size_t lenv;
  1486. hpatch_BOOL isNeedDecode0;
  1487. } rle0_decoder_t;
  1488. static void _rle0_decoder_init(rle0_decoder_t* self,const unsigned char* code,const unsigned char* code_end){
  1489. self->code=code;
  1490. self->code_end=code_end;
  1491. self->len0=0;
  1492. self->lenv=0;
  1493. self->isNeedDecode0=hpatch_TRUE;
  1494. }
  1495. static hpatch_BOOL _rle0_decoder_add(rle0_decoder_t* self,TByte* out_data,hpatch_size_t decodeSize){
  1496. if (self->len0){
  1497. _0_process:
  1498. if (self->len0>=decodeSize){
  1499. self->len0-=decodeSize;
  1500. return hpatch_TRUE;
  1501. }else{
  1502. decodeSize-=self->len0;
  1503. out_data+=self->len0;
  1504. self->len0=0;
  1505. goto _decode_v_process;
  1506. }
  1507. }
  1508. if (self->lenv){
  1509. _v_process:
  1510. if (self->lenv>=decodeSize){
  1511. addData(out_data,self->code,decodeSize);
  1512. self->code+=decodeSize;
  1513. self->lenv-=decodeSize;
  1514. return hpatch_TRUE;
  1515. }else{
  1516. addData(out_data,self->code,self->lenv);
  1517. out_data+=self->lenv;
  1518. decodeSize-=self->lenv;
  1519. self->code+=self->lenv;
  1520. self->lenv=0;
  1521. goto _decode_0_process;
  1522. }
  1523. }
  1524. assert(decodeSize>0);
  1525. if (self->isNeedDecode0){
  1526. hpatch_StreamPos_t len0;
  1527. _decode_0_process:
  1528. self->isNeedDecode0=hpatch_FALSE;
  1529. if (!hpatch_unpackUInt(&self->code,self->code_end,&len0)) return _hpatch_FALSE;
  1530. if (len0!=(hpatch_size_t)len0) return _hpatch_FALSE;
  1531. self->len0=(hpatch_size_t)len0;
  1532. goto _0_process;
  1533. }else{
  1534. hpatch_StreamPos_t lenv;
  1535. _decode_v_process:
  1536. self->isNeedDecode0=hpatch_TRUE;
  1537. if (!hpatch_unpackUInt(&self->code,self->code_end,&lenv)) return _hpatch_FALSE;
  1538. if (lenv>(size_t)(self->code_end-self->code)) return _hpatch_FALSE;
  1539. self->lenv=(hpatch_size_t)lenv;
  1540. goto _v_process;
  1541. }
  1542. }
  1543. static hpatch_BOOL _patch_add_old_with_rle0(_TOutStreamCache* outCache,rle0_decoder_t* rle0_decoder,
  1544. const hpatch_TStreamInput* old,hpatch_StreamPos_t oldPos,
  1545. hpatch_StreamPos_t addLength,TByte* aCache,hpatch_size_t aCacheSize){
  1546. while (addLength>0){
  1547. hpatch_size_t decodeStep=aCacheSize;
  1548. if (decodeStep>addLength)
  1549. decodeStep=(hpatch_size_t)addLength;
  1550. if (!old->read(old,oldPos,aCache,aCache+decodeStep)) return _hpatch_FALSE;
  1551. if (!_rle0_decoder_add(rle0_decoder,aCache,decodeStep)) return _hpatch_FALSE;
  1552. if (!_TOutStreamCache_write(outCache,aCache,decodeStep)) return _hpatch_FALSE;
  1553. oldPos+=decodeStep;
  1554. addLength-=decodeStep;
  1555. }
  1556. return hpatch_TRUE;
  1557. }
  1558. hpatch_BOOL sspatch_covers_nextCover(sspatch_covers_t* self){
  1559. hpatch_BOOL inc_oldPos_sign=(*(self->covers_cache))>>(8-1);
  1560. self->lastOldEnd=self->cover.oldPos+self->cover.length;
  1561. self->lastNewEnd=self->cover.newPos+self->cover.length;
  1562. if (!hpatch_unpackUIntWithTag(&self->covers_cache,self->covers_cacheEnd,&self->cover.oldPos,1)) return _hpatch_FALSE;
  1563. if (inc_oldPos_sign==0)
  1564. self->cover.oldPos+=self->lastOldEnd;
  1565. else
  1566. self->cover.oldPos=self->lastOldEnd-self->cover.oldPos;
  1567. if (!hpatch_unpackUInt(&self->covers_cache,self->covers_cacheEnd,&self->cover.newPos)) return _hpatch_FALSE;
  1568. self->cover.newPos+=self->lastNewEnd;
  1569. if (!hpatch_unpackUInt(&self->covers_cache,self->covers_cacheEnd,&self->cover.length)) return _hpatch_FALSE;
  1570. return hpatch_TRUE;
  1571. }
  1572. hpatch_BOOL patch_single_stream_diff(const hpatch_TStreamOutput* out_newData,
  1573. const hpatch_TStreamInput* oldData,
  1574. const hpatch_TStreamInput* uncompressedDiffData,
  1575. hpatch_StreamPos_t diffData_pos,
  1576. hpatch_StreamPos_t diffData_posEnd,
  1577. hpatch_StreamPos_t coverCount,hpatch_size_t stepMemSize,
  1578. unsigned char* temp_cache,unsigned char* temp_cache_end,
  1579. sspatch_coversListener_t* coversListener){
  1580. unsigned char* step_cache=temp_cache;
  1581. hpatch_size_t cache_size;
  1582. TStreamCacheClip inClip;
  1583. _TOutStreamCache outCache;
  1584. sspatch_covers_t covers;
  1585. assert(diffData_posEnd<=uncompressedDiffData->streamSize);
  1586. sspatch_covers_init(&covers);
  1587. if (coversListener) assert(coversListener->onStepCovers);
  1588. {//cache
  1589. if ((size_t)(temp_cache_end-temp_cache)<stepMemSize+hpatch_kStreamCacheSize*_kCacheSgCount) return _hpatch_FALSE;
  1590. temp_cache+=stepMemSize;
  1591. cache_size=(temp_cache_end-temp_cache)/_kCacheSgCount;
  1592. _TStreamCacheClip_init(&inClip,uncompressedDiffData,diffData_pos,diffData_posEnd,
  1593. temp_cache,cache_size);
  1594. temp_cache+=cache_size;
  1595. _TOutStreamCache_init(&outCache,out_newData,temp_cache+cache_size,cache_size);
  1596. }
  1597. while (coverCount) {//step loop
  1598. rle0_decoder_t rle0_decoder;
  1599. {//read step info
  1600. unsigned char* covers_cacheEnd;
  1601. unsigned char* bufRle_cache_end;
  1602. {
  1603. hpatch_StreamPos_t bufCover_size;
  1604. hpatch_StreamPos_t bufRle_size;
  1605. _clip_unpackUIntTo(&bufCover_size,&inClip);
  1606. _clip_unpackUIntTo(&bufRle_size,&inClip);
  1607. #ifdef __RUN_MEM_SAFE_CHECK
  1608. if ((bufCover_size>stepMemSize)|(bufRle_size>stepMemSize)|
  1609. (bufCover_size+bufRle_size>stepMemSize)) return _hpatch_FALSE;
  1610. #endif
  1611. covers_cacheEnd=step_cache+(size_t)bufCover_size;
  1612. bufRle_cache_end=covers_cacheEnd+(size_t)bufRle_size;
  1613. }
  1614. if (coversListener&&coversListener->onStepCoversReset)
  1615. coversListener->onStepCoversReset(coversListener,coverCount);
  1616. if (!_TStreamCacheClip_readDataTo(&inClip,step_cache,bufRle_cache_end))
  1617. return _hpatch_FALSE;
  1618. if (coversListener)
  1619. coversListener->onStepCovers(coversListener,step_cache,covers_cacheEnd);
  1620. sspatch_covers_setCoversCache(&covers,step_cache,covers_cacheEnd);
  1621. _rle0_decoder_init(&rle0_decoder,covers_cacheEnd,bufRle_cache_end);
  1622. }
  1623. while (sspatch_covers_isHaveNextCover(&covers)) {//cover loop
  1624. if (!sspatch_covers_nextCover(&covers))
  1625. return _hpatch_FALSE;
  1626. if (covers.cover.newPos>covers.lastNewEnd){
  1627. if (!_TOutStreamCache_copyFromClip(&outCache,&inClip,covers.cover.newPos-covers.lastNewEnd))
  1628. return _hpatch_FALSE;
  1629. }
  1630. --coverCount;
  1631. if (covers.cover.length){
  1632. #ifdef __RUN_MEM_SAFE_CHECK
  1633. if ((covers.cover.oldPos>oldData->streamSize)|
  1634. (covers.cover.length>(hpatch_StreamPos_t)(oldData->streamSize-covers.cover.oldPos))) return _hpatch_FALSE;
  1635. #endif
  1636. if (!_patch_add_old_with_rle0(&outCache,&rle0_decoder,oldData,covers.cover.oldPos,covers.cover.length,
  1637. temp_cache,cache_size)) return _hpatch_FALSE;
  1638. }else{
  1639. #ifdef __RUN_MEM_SAFE_CHECK
  1640. if (coverCount!=0) return _hpatch_FALSE;
  1641. #endif
  1642. }
  1643. }
  1644. }
  1645. if (!_TOutStreamCache_flush(&outCache))
  1646. return _hpatch_FALSE;
  1647. if (_TStreamCacheClip_isFinish(&inClip)&_TOutStreamCache_isFinish(&outCache)&(coverCount==0))
  1648. return hpatch_TRUE;
  1649. else
  1650. return _hpatch_FALSE;
  1651. }
  1652. static hpatch_BOOL _TDiffToSingleStream_read(const struct hpatch_TStreamInput* stream,hpatch_StreamPos_t readFromPos,
  1653. unsigned char* out_data,unsigned char* out_data_end){
  1654. //[ |readedSize ]
  1655. // [ |cachedBufBegin _TDiffToSingleStream_kBufSize]
  1656. // readFromPos[out_data out_data_end]
  1657. TDiffToSingleStream* self=(TDiffToSingleStream*)stream->streamImport;
  1658. hpatch_StreamPos_t readedSize=self->readedSize;
  1659. while (1){
  1660. size_t rLen=out_data_end-out_data;
  1661. if (readFromPos==readedSize){
  1662. hpatch_BOOL result=self->diffStream->read(self->diffStream,readedSize,out_data,out_data_end);
  1663. self->readedSize=readedSize+rLen;
  1664. if ((self->isInSingleStream)||(rLen>_TDiffToSingleStream_kBufSize)){
  1665. self->cachedBufBegin=_TDiffToSingleStream_kBufSize;
  1666. }else{
  1667. //cache
  1668. if (rLen>=_TDiffToSingleStream_kBufSize){
  1669. memcpy(self->buf,out_data_end-_TDiffToSingleStream_kBufSize,_TDiffToSingleStream_kBufSize);
  1670. self->cachedBufBegin = 0;
  1671. }else{
  1672. size_t new_cachedBufBegin;
  1673. if (self->cachedBufBegin>=rLen){
  1674. new_cachedBufBegin=self->cachedBufBegin-rLen;
  1675. memmove(self->buf+new_cachedBufBegin,self->buf+self->cachedBufBegin,_TDiffToSingleStream_kBufSize-self->cachedBufBegin);
  1676. }else{
  1677. new_cachedBufBegin=0;
  1678. memmove(self->buf,self->buf+rLen,_TDiffToSingleStream_kBufSize-rLen);
  1679. }
  1680. memcpy(self->buf+(_TDiffToSingleStream_kBufSize-rLen),out_data,rLen);
  1681. self->cachedBufBegin=new_cachedBufBegin;
  1682. }
  1683. }
  1684. return result;
  1685. }else{
  1686. size_t cachedSize=_TDiffToSingleStream_kBufSize-self->cachedBufBegin;
  1687. size_t bufSize=(size_t)(readedSize-readFromPos);
  1688. if ((readFromPos<readedSize)&(bufSize<=cachedSize)){
  1689. if (rLen>bufSize)
  1690. rLen=bufSize;
  1691. memcpy(out_data,self->buf+(_TDiffToSingleStream_kBufSize-bufSize),rLen);
  1692. out_data+=rLen;
  1693. readFromPos+=rLen;
  1694. if (out_data==out_data_end)
  1695. return hpatch_TRUE;
  1696. else
  1697. continue;
  1698. }else{
  1699. return _hpatch_FALSE;
  1700. }
  1701. }
  1702. }
  1703. }
  1704. void TDiffToSingleStream_init(TDiffToSingleStream* self,const hpatch_TStreamInput* diffStream){
  1705. self->base.streamImport=self;
  1706. self->base.streamSize=diffStream->streamSize;
  1707. self->base.read=_TDiffToSingleStream_read;
  1708. self->base._private_reserved=0;
  1709. self->diffStream=diffStream;
  1710. self->readedSize=0;
  1711. self->cachedBufBegin=_TDiffToSingleStream_kBufSize;
  1712. self->isInSingleStream=hpatch_FALSE;
  1713. }
  1714. hpatch_BOOL patch_single_stream(sspatch_listener_t* listener,
  1715. const hpatch_TStreamOutput* __out_newData,
  1716. const hpatch_TStreamInput* oldData,
  1717. const hpatch_TStreamInput* singleCompressedDiff,
  1718. hpatch_StreamPos_t diffInfo_pos,
  1719. sspatch_coversListener_t* coversListener){
  1720. hpatch_BOOL result=hpatch_TRUE;
  1721. hpatch_TDecompress* decompressPlugin=0;
  1722. unsigned char* temp_cache=0;
  1723. unsigned char* temp_cacheEnd=0;
  1724. hpatch_singleCompressedDiffInfo diffInfo;
  1725. hpatch_TStreamOutput _out_newData=*__out_newData;
  1726. hpatch_TStreamOutput* out_newData=&_out_newData;
  1727. TDiffToSingleStream _toSStream;
  1728. assert((listener)&&(listener->onDiffInfo));
  1729. TDiffToSingleStream_init(&_toSStream,singleCompressedDiff);
  1730. singleCompressedDiff=&_toSStream.base;
  1731. if (!getSingleCompressedDiffInfo(&diffInfo,singleCompressedDiff,diffInfo_pos))
  1732. return _hpatch_FALSE;
  1733. if (diffInfo.newDataSize>out_newData->streamSize)
  1734. return _hpatch_FALSE;
  1735. out_newData->streamSize=diffInfo.newDataSize;
  1736. if (diffInfo.oldDataSize!=oldData->streamSize)
  1737. return _hpatch_FALSE;
  1738. if (!listener->onDiffInfo(listener,&diffInfo,&decompressPlugin,&temp_cache,&temp_cacheEnd))
  1739. return _hpatch_FALSE;
  1740. if ((temp_cache==0)||(temp_cache>=temp_cacheEnd))
  1741. result=_hpatch_FALSE;
  1742. if (result){
  1743. result=patch_single_compressed_diff(out_newData,oldData,singleCompressedDiff,diffInfo.diffDataPos,
  1744. diffInfo.uncompressedSize,diffInfo.compressedSize,decompressPlugin,
  1745. diffInfo.coverCount,(size_t)diffInfo.stepMemSize,
  1746. temp_cache,temp_cacheEnd,coversListener);
  1747. }
  1748. if (listener->onPatchFinish)
  1749. listener->onPatchFinish(listener,temp_cache,temp_cacheEnd);
  1750. return result;
  1751. }