patch_private.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. // patch_private.h
  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. #ifndef HPatch_patch_private_h
  26. #define HPatch_patch_private_h
  27. #include "patch_types.h"
  28. #ifdef __cplusplus
  29. extern "C" {
  30. #endif
  31. //byte rle type , ctrl code: high 2bit + packedLen(6bit+...)
  32. typedef enum TByteRleType{
  33. kByteRleType_rle0 = 0, //00 rle 0 , data code:0 byte
  34. kByteRleType_rle255= 1, //01 rle 255, data code:0 byte
  35. kByteRleType_rle = 2, //10 rle x(1--254), data code:1 byte (save x)
  36. kByteRleType_unrle = 3 //11 n byte data, data code:n byte(save no rle data)
  37. } TByteRleType;
  38. static const hpatch_uint kByteRleType_bit=2;
  39. typedef struct _THDiffzHead{
  40. hpatch_StreamPos_t coverCount;
  41. hpatch_StreamPos_t cover_buf_size;
  42. hpatch_StreamPos_t compress_cover_buf_size;
  43. hpatch_StreamPos_t rle_ctrlBuf_size;
  44. hpatch_StreamPos_t compress_rle_ctrlBuf_size;
  45. hpatch_StreamPos_t rle_codeBuf_size;
  46. hpatch_StreamPos_t compress_rle_codeBuf_size;
  47. hpatch_StreamPos_t newDataDiff_size;
  48. hpatch_StreamPos_t compress_newDataDiff_size;
  49. hpatch_StreamPos_t typesEndPos;
  50. hpatch_StreamPos_t compressSizeBeginPos;
  51. hpatch_StreamPos_t headEndPos;
  52. hpatch_StreamPos_t coverEndPos;
  53. } _THDiffzHead;
  54. hpatch_BOOL read_diffz_head(hpatch_compressedDiffInfo* out_diffInfo,_THDiffzHead* out_head,
  55. const hpatch_TStreamInput* compressedDiff);
  56. // Stream Clip cache
  57. typedef struct TStreamCacheClip{
  58. hpatch_StreamPos_t streamPos;
  59. hpatch_StreamPos_t streamPos_end;
  60. const hpatch_TStreamInput* srcStream;
  61. unsigned char* cacheBuf;
  62. hpatch_size_t cacheBegin;
  63. hpatch_size_t cacheEnd;
  64. } TStreamCacheClip;
  65. hpatch_inline static
  66. void _TStreamCacheClip_init(TStreamCacheClip* sclip,const hpatch_TStreamInput* srcStream,
  67. hpatch_StreamPos_t streamPos,hpatch_StreamPos_t streamPos_end,
  68. unsigned char* aCache,hpatch_size_t cacheSize){
  69. assert((streamPos<=streamPos_end)&&(streamPos_end<=(srcStream?srcStream->streamSize:0)));
  70. sclip->streamPos=streamPos;
  71. sclip->streamPos_end=streamPos_end;
  72. sclip->srcStream=srcStream;
  73. sclip->cacheBuf=aCache;
  74. sclip->cacheBegin=cacheSize;
  75. sclip->cacheEnd=cacheSize;
  76. }
  77. #define _TStreamCacheClip_isFinish(sclip) ( 0==_TStreamCacheClip_leaveSize(sclip) )
  78. #define _TStreamCacheClip_isCacheEmpty(sclip) ( (sclip)->cacheBegin==(sclip)->cacheEnd )
  79. #define _TStreamCacheClip_cachedSize(sclip) ( (hpatch_size_t)((sclip)->cacheEnd-(sclip)->cacheBegin) )
  80. #define _TStreamCacheClip_leaveSize(sclip) \
  81. ( (hpatch_StreamPos_t)((sclip)->streamPos_end-(sclip)->streamPos) \
  82. + (hpatch_StreamPos_t)_TStreamCacheClip_cachedSize(sclip) )
  83. #define _TStreamCacheClip_readPosOfSrcStream(sclip) ( \
  84. (sclip)->streamPos - _TStreamCacheClip_cachedSize(sclip) )
  85. hpatch_BOOL _TStreamCacheClip_updateCache(TStreamCacheClip* sclip);
  86. hpatch_inline static //error return 0
  87. unsigned char* _TStreamCacheClip_accessData(TStreamCacheClip* sclip,hpatch_size_t readSize){
  88. //assert(readSize<=sclip->cacheEnd);
  89. if (readSize>_TStreamCacheClip_cachedSize(sclip)){
  90. if (!_TStreamCacheClip_updateCache(sclip)) return 0;
  91. if (readSize>_TStreamCacheClip_cachedSize(sclip)) return 0;
  92. }
  93. return &sclip->cacheBuf[sclip->cacheBegin];
  94. }
  95. #define _TStreamCacheClip_skipData_noCheck(sclip,skipSize) ((sclip)->cacheBegin+=skipSize)
  96. hpatch_BOOL _TStreamCacheClip_skipData(TStreamCacheClip* sclip,hpatch_StreamPos_t skipLongSize);
  97. hpatch_inline static //error return 0
  98. unsigned char* _TStreamCacheClip_readData(TStreamCacheClip* sclip,hpatch_size_t readSize){
  99. unsigned char* result=_TStreamCacheClip_accessData(sclip,readSize);
  100. _TStreamCacheClip_skipData_noCheck(sclip,readSize);
  101. return result;
  102. }
  103. hpatch_BOOL _TStreamCacheClip_readDataTo(TStreamCacheClip* sclip,
  104. unsigned char* out_buf,unsigned char* bufEnd);
  105. hpatch_BOOL _TStreamCacheClip_addDataTo(TStreamCacheClip* self,unsigned char* dst,hpatch_size_t addLen);
  106. hpatch_BOOL _TStreamCacheClip_unpackUIntWithTag(TStreamCacheClip* sclip,
  107. hpatch_StreamPos_t* result,const hpatch_uint kTagBit);
  108. hpatch_BOOL _TStreamCacheClip_readType_end(TStreamCacheClip* sclip,unsigned char endTag,
  109. char out_type[hpatch_kMaxPluginTypeLength+1]);
  110. // Stream Clip cache
  111. typedef struct {
  112. hpatch_StreamPos_t writeToPos;
  113. const hpatch_TStreamOutput* dstStream;
  114. unsigned char* cacheBuf;
  115. hpatch_size_t cacheCur;
  116. hpatch_size_t cacheEnd;
  117. } _TOutStreamCache;
  118. static hpatch_inline void _TOutStreamCache_init(_TOutStreamCache* self,const hpatch_TStreamOutput* dstStream,
  119. unsigned char* aCache,hpatch_size_t aCacheSize){
  120. self->writeToPos=0;
  121. self->cacheCur=0;
  122. self->dstStream=dstStream;
  123. self->cacheBuf=aCache;
  124. self->cacheEnd=aCacheSize;
  125. }
  126. hpatch_inline static
  127. void _TOutStreamCache_resetCache(_TOutStreamCache* self,unsigned char* aCache,hpatch_size_t aCacheSize){
  128. assert(0==self->cacheCur);
  129. self->cacheBuf=aCache;
  130. self->cacheEnd=aCacheSize;
  131. }
  132. static hpatch_inline hpatch_StreamPos_t _TOutStreamCache_leaveSize(const _TOutStreamCache* self){
  133. return self->dstStream->streamSize-self->writeToPos;
  134. }
  135. static hpatch_inline hpatch_BOOL _TOutStreamCache_isFinish(const _TOutStreamCache* self){
  136. return self->writeToPos==self->dstStream->streamSize;
  137. }
  138. static hpatch_inline hpatch_size_t _TOutStreamCache_cachedDataSize(const _TOutStreamCache* self){
  139. return self->cacheCur;
  140. }
  141. hpatch_BOOL _TOutStreamCache_flush(_TOutStreamCache* self);
  142. hpatch_BOOL _TOutStreamCache_write(_TOutStreamCache* self,const unsigned char* data,hpatch_size_t dataSize);
  143. hpatch_BOOL _TOutStreamCache_fill(_TOutStreamCache* self,hpatch_byte fillValue,hpatch_StreamPos_t fillLength);
  144. hpatch_BOOL _TOutStreamCache_copyFromClip(_TOutStreamCache* self,TStreamCacheClip* src,hpatch_StreamPos_t copyLength);
  145. hpatch_BOOL _TOutStreamCache_copyFromStream(_TOutStreamCache* self,const hpatch_TStreamInput* src,
  146. hpatch_StreamPos_t srcPos,hpatch_StreamPos_t copyLength);
  147. hpatch_BOOL _TOutStreamCache_copyFromSelf(_TOutStreamCache* self,hpatch_StreamPos_t aheadLength,hpatch_StreamPos_t copyLength);
  148. typedef struct _TDecompressInputStream{
  149. hpatch_TStreamInput IInputStream;
  150. hpatch_TDecompress* decompressPlugin;
  151. hpatch_decompressHandle decompressHandle;
  152. } _TDecompressInputStream;
  153. hpatch_BOOL getStreamClip(TStreamCacheClip* out_clip,_TDecompressInputStream* out_stream,
  154. hpatch_StreamPos_t dataSize,hpatch_StreamPos_t compressedSize,
  155. const hpatch_TStreamInput* stream,hpatch_StreamPos_t* pCurStreamPos,
  156. hpatch_TDecompress* decompressPlugin,unsigned char* aCache,hpatch_size_t cacheSize);
  157. #define _TDiffToSingleStream_kBufSize hpatch_kStreamCacheSize
  158. typedef struct {
  159. hpatch_TStreamInput base;
  160. const hpatch_TStreamInput* diffStream;
  161. hpatch_StreamPos_t readedSize;
  162. hpatch_size_t cachedBufBegin;
  163. hpatch_BOOL isInSingleStream;
  164. unsigned char buf[_TDiffToSingleStream_kBufSize];
  165. } TDiffToSingleStream;
  166. void TDiffToSingleStream_init(TDiffToSingleStream* self,const hpatch_TStreamInput* diffStream);
  167. hpatch_inline static
  168. void TDiffToSingleStream_setInSingleStream(TDiffToSingleStream* self,hpatch_StreamPos_t singleStreamPos){
  169. self->isInSingleStream=hpatch_TRUE; }
  170. hpatch_inline static
  171. void TDiffToSingleStream_resetStream(TDiffToSingleStream* self,const hpatch_TStreamInput* diffStream){
  172. self->diffStream=diffStream; }
  173. #ifdef __cplusplus
  174. }
  175. #endif
  176. #endif