patch_types.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. // patch_types.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_types_h
  26. #define HPatch_patch_types_h
  27. #include <string.h> //for size_t memset memcpy memmove
  28. #include <assert.h>
  29. #ifdef __cplusplus
  30. extern "C" {
  31. #endif
  32. #define HDIFFPATCH_VERSION_MAJOR 4
  33. #define HDIFFPATCH_VERSION_MINOR 6
  34. #define HDIFFPATCH_VERSION_RELEASE 6
  35. #define _HDIFFPATCH_VERSION HDIFFPATCH_VERSION_MAJOR.HDIFFPATCH_VERSION_MINOR.HDIFFPATCH_VERSION_RELEASE
  36. #define _HDIFFPATCH_QUOTE(str) #str
  37. #define _HDIFFPATCH_EXPAND_AND_QUOTE(str) _HDIFFPATCH_QUOTE(str)
  38. #define HDIFFPATCH_VERSION_STRING _HDIFFPATCH_EXPAND_AND_QUOTE(_HDIFFPATCH_VERSION)
  39. #ifndef hpatch_int
  40. typedef int hpatch_int;
  41. #endif
  42. #ifndef hpatch_uint
  43. typedef unsigned int hpatch_uint;
  44. #endif
  45. #ifndef hpatch_size_t
  46. typedef size_t hpatch_size_t;
  47. #endif
  48. #ifndef hpatch_uint32_t
  49. #ifdef _MSC_VER
  50. # if (_MSC_VER >= 1300)
  51. typedef unsigned __int32 hpatch_uint32_t;
  52. # else
  53. typedef unsigned int hpatch_uint32_t;
  54. # endif
  55. #else
  56. typedef unsigned int hpatch_uint32_t;
  57. #endif
  58. #endif
  59. #ifndef hpatch_uint64_t
  60. #ifdef _MSC_VER
  61. typedef unsigned __int64 hpatch_uint64_t;
  62. #else
  63. typedef unsigned long long hpatch_uint64_t;
  64. #endif
  65. #endif
  66. #ifndef hpatch_StreamPos_t
  67. typedef hpatch_uint64_t hpatch_StreamPos_t; // file size type
  68. #endif
  69. #define hpatch_kNullStreamPos (~(hpatch_StreamPos_t)0)
  70. #ifndef hpatch_BOOL
  71. typedef int hpatch_BOOL;
  72. #endif
  73. #define hpatch_FALSE 0
  74. #define hpatch_TRUE ((hpatch_BOOL)(!hpatch_FALSE))
  75. #ifndef hpatch_byte
  76. typedef unsigned char hpatch_byte;
  77. #endif
  78. #if (_HPATCH_IS_USED_errno)
  79. typedef int hpatch_FileError_t;// 0: no error; other: saved errno value;
  80. #else
  81. typedef hpatch_BOOL hpatch_FileError_t;// 0: no error; other: error;
  82. #endif
  83. #ifdef _MSC_VER
  84. # define hpatch_inline _inline
  85. #else
  86. # define hpatch_inline inline
  87. #endif
  88. //PRIu64 for printf type hpatch_StreamPos_t
  89. #ifndef PRIu64
  90. # ifdef _MSC_VER
  91. # define PRIu64 "I64u"
  92. # else
  93. # define PRIu64 "llu"
  94. # endif
  95. #endif
  96. #ifdef ANDROID
  97. # include <android/log.h>
  98. # define LOG_ERR(...) __android_log_print(ANDROID_LOG_ERROR, "hpatch", __VA_ARGS__)
  99. #else
  100. # include <stdio.h> //for stderr
  101. # define LOG_ERR(...) fprintf(stderr,__VA_ARGS__)
  102. #endif
  103. #ifndef _HPATCH_IS_USED_errno
  104. # define _HPATCH_IS_USED_errno 1
  105. #endif
  106. #define _hpatch_import_system_tag "call import system api"
  107. #if (_HPATCH_IS_USED_errno)
  108. # define LOG_ERRNO(_err_no) \
  109. LOG_ERR(_hpatch_import_system_tag" error! errno: %d, errmsg: %s.\n",_err_no,strerror(_err_no))
  110. #else
  111. # define LOG_ERRNO(_err_no) LOG_ERR(_hpatch_import_system_tag" error!\n")
  112. #endif
  113. #define _hpatch_align_type_lower(uint_type,p,align2pow) (((uint_type)(p)) & (~(uint_type)((align2pow)-1)))
  114. #define _hpatch_align_lower(p,align2pow) _hpatch_align_type_lower(hpatch_size_t,p,align2pow)
  115. #define _hpatch_align_upper(p,align2pow) _hpatch_align_lower(((hpatch_size_t)(p))+((align2pow)-1),align2pow)
  116. typedef void* hpatch_TStreamInputHandle;
  117. typedef void* hpatch_TStreamOutputHandle;
  118. typedef struct hpatch_TStreamInput{
  119. void* streamImport;
  120. hpatch_StreamPos_t streamSize; //stream size,max readable range;
  121. //read() must read (out_data_end-out_data), otherwise error return hpatch_FALSE
  122. hpatch_BOOL (*read)(const struct hpatch_TStreamInput* stream,hpatch_StreamPos_t readFromPos,
  123. unsigned char* out_data,unsigned char* out_data_end);
  124. void* _private_reserved;
  125. } hpatch_TStreamInput;
  126. typedef struct hpatch_TStreamOutput{
  127. void* streamImport;
  128. hpatch_StreamPos_t streamSize; //stream size,max writable range; not is write pos!
  129. //read_writed for ReadWriteIO, can null!
  130. hpatch_BOOL (*read_writed)(const struct hpatch_TStreamOutput* stream,hpatch_StreamPos_t readFromPos,
  131. unsigned char* out_data,unsigned char* out_data_end);
  132. //write() must wrote (out_data_end-out_data), otherwise error return hpatch_FALSE
  133. hpatch_BOOL (*write)(const struct hpatch_TStreamOutput* stream,hpatch_StreamPos_t writeToPos,
  134. const unsigned char* data,const unsigned char* data_end);
  135. } hpatch_TStreamOutput;
  136. //default once I/O (read/write) byte size
  137. #ifndef hpatch_kStreamCacheSize
  138. # define hpatch_kStreamCacheSize (1024*4)
  139. #endif
  140. #ifndef hpatch_kFileIOBufBetterSize
  141. # define hpatch_kFileIOBufBetterSize (1024*64)
  142. #endif
  143. #ifndef hpatch_kMaxPluginTypeLength
  144. # define hpatch_kMaxPluginTypeLength 259
  145. #endif
  146. typedef struct hpatch_compressedDiffInfo{
  147. hpatch_StreamPos_t newDataSize;
  148. hpatch_StreamPos_t oldDataSize;
  149. hpatch_uint compressedCount;//need open hpatch_decompressHandle number
  150. char compressType[hpatch_kMaxPluginTypeLength+1]; //ascii cstring
  151. } hpatch_compressedDiffInfo;
  152. typedef void* hpatch_decompressHandle;
  153. typedef enum{
  154. hpatch_dec_ok=0,
  155. hpatch_dec_mem_error,
  156. hpatch_dec_open_error,
  157. hpatch_dec_error,
  158. hpatch_dec_close_error,
  159. } hpatch_dec_error_t;
  160. typedef struct hpatch_TDecompress{
  161. hpatch_BOOL (*is_can_open)(const char* compresseType);
  162. //error return 0.
  163. hpatch_decompressHandle (*open)(struct hpatch_TDecompress* decompressPlugin,
  164. hpatch_StreamPos_t dataSize,
  165. const struct hpatch_TStreamInput* codeStream,
  166. hpatch_StreamPos_t code_begin,
  167. hpatch_StreamPos_t code_end);//codeSize==code_end-code_begin
  168. hpatch_BOOL (*close)(struct hpatch_TDecompress* decompressPlugin,
  169. hpatch_decompressHandle decompressHandle);
  170. //decompress_part() must out (out_part_data_end-out_part_data), otherwise error return hpatch_FALSE
  171. hpatch_BOOL (*decompress_part)(hpatch_decompressHandle decompressHandle,
  172. unsigned char* out_part_data,unsigned char* out_part_data_end);
  173. //reset_code add new compressed data; for support vcpatch, can NULL
  174. hpatch_BOOL (*reset_code)(hpatch_decompressHandle decompressHandle,
  175. hpatch_StreamPos_t dataSize,
  176. const struct hpatch_TStreamInput* codeStream,
  177. hpatch_StreamPos_t code_begin,
  178. hpatch_StreamPos_t code_end);
  179. volatile hpatch_dec_error_t decError; //if you used decError value, once patch must used it's own hpatch_TDecompress
  180. } hpatch_TDecompress;
  181. #define _hpatch_update_decError(decompressPlugin,errorCode) \
  182. do { if ((decompressPlugin)->decError==hpatch_dec_ok) \
  183. (decompressPlugin)->decError=errorCode; } while(0)
  184. const hpatch_TStreamInput* mem_as_hStreamInput(hpatch_TStreamInput* out_stream,
  185. const unsigned char* mem,const unsigned char* mem_end);
  186. const hpatch_TStreamOutput* mem_as_hStreamOutput(hpatch_TStreamOutput* out_stream,
  187. unsigned char* mem,unsigned char* mem_end);
  188. hpatch_BOOL hpatch_deccompress_mem(hpatch_TDecompress* decompressPlugin,
  189. const unsigned char* code,const unsigned char* code_end,
  190. unsigned char* out_data,unsigned char* out_data_end);
  191. typedef struct{
  192. hpatch_TStreamInput base;
  193. const hpatch_TStreamInput* srcStream;
  194. hpatch_StreamPos_t clipBeginPos;
  195. } TStreamInputClip;
  196. //clip srcStream from clipBeginPos to clipEndPos as a new StreamInput;
  197. void TStreamInputClip_init(TStreamInputClip* self,const hpatch_TStreamInput* srcStream,
  198. hpatch_StreamPos_t clipBeginPos,hpatch_StreamPos_t clipEndPos);
  199. typedef struct{
  200. hpatch_TStreamOutput base;
  201. const hpatch_TStreamOutput* srcStream;
  202. hpatch_StreamPos_t clipBeginPos;
  203. } TStreamOutputClip;
  204. //clip srcStream from clipBeginPos to clipEndPos as a new StreamInput;
  205. void TStreamOutputClip_init(TStreamOutputClip* self,const hpatch_TStreamOutput* srcStream,
  206. hpatch_StreamPos_t clipBeginPos,hpatch_StreamPos_t clipEndPos);
  207. #define hpatch_kMaxPackedUIntBytes ((sizeof(hpatch_StreamPos_t)*8+6)/7+1)
  208. hpatch_BOOL hpatch_packUIntWithTag(unsigned char** out_code,unsigned char* out_code_end,
  209. hpatch_StreamPos_t uValue,hpatch_uint highTag,const hpatch_uint kTagBit);
  210. hpatch_uint hpatch_packUIntWithTag_size(hpatch_StreamPos_t uValue,const hpatch_uint kTagBit);
  211. #define hpatch_packUInt(out_code,out_code_end,uValue) \
  212. hpatch_packUIntWithTag(out_code,out_code_end,uValue,0,0)
  213. #define hpatch_packUInt_size(uValue) hpatch_packUIntWithTag_size(uValue,0)
  214. hpatch_BOOL hpatch_unpackUIntWithTag(const unsigned char** src_code,const unsigned char* src_code_end,
  215. hpatch_StreamPos_t* result,const hpatch_uint kTagBit);
  216. #define hpatch_unpackUInt(src_code,src_code_end,result) \
  217. hpatch_unpackUIntWithTag(src_code,src_code_end,result,0)
  218. typedef struct hpatch_TCover{
  219. hpatch_StreamPos_t oldPos;
  220. hpatch_StreamPos_t newPos;
  221. hpatch_StreamPos_t length;
  222. } hpatch_TCover;
  223. typedef struct hpatch_TCover32{
  224. hpatch_uint32_t oldPos;
  225. hpatch_uint32_t newPos;
  226. hpatch_uint32_t length;
  227. } hpatch_TCover32;
  228. typedef struct hpatch_TCover_sz{
  229. size_t oldPos;
  230. size_t newPos;
  231. size_t length;
  232. } hpatch_TCover_sz;
  233. //opened input covers
  234. typedef struct hpatch_TCovers{
  235. hpatch_StreamPos_t (*leave_cover_count)(const struct hpatch_TCovers* covers);
  236. //read out a cover,and to next cover pos; if error then return false
  237. hpatch_BOOL (*read_cover)(struct hpatch_TCovers* covers,hpatch_TCover* out_cover);
  238. hpatch_BOOL (*is_finish)(const struct hpatch_TCovers* covers);
  239. hpatch_BOOL (*close)(struct hpatch_TCovers* covers);
  240. } hpatch_TCovers;
  241. //output covers
  242. typedef struct hpatch_TOutputCovers{
  243. hpatch_BOOL (*push_cover)(struct hpatch_TOutputCovers* out_covers,const hpatch_TCover* cover);
  244. void (*collate_covers)(struct hpatch_TOutputCovers* out_covers); // for support search covers by muti-thread
  245. } hpatch_TOutputCovers;
  246. typedef struct{
  247. hpatch_StreamPos_t newDataSize;
  248. hpatch_StreamPos_t oldDataSize;
  249. hpatch_StreamPos_t uncompressedSize;
  250. hpatch_StreamPos_t compressedSize;
  251. hpatch_StreamPos_t diffDataPos;
  252. hpatch_StreamPos_t coverCount;
  253. hpatch_StreamPos_t stepMemSize;
  254. char compressType[hpatch_kMaxPluginTypeLength+1]; //ascii cstring
  255. } hpatch_singleCompressedDiffInfo;
  256. typedef struct sspatch_listener_t{
  257. void* import;
  258. hpatch_BOOL (*onDiffInfo)(struct sspatch_listener_t* listener,
  259. const hpatch_singleCompressedDiffInfo* info,
  260. hpatch_TDecompress** out_decompressPlugin,//find decompressPlugin by info->compressType
  261. unsigned char** out_temp_cache, //*out_temp_cacheEnd-*out_temp_cache == info->stepMemSize + (I/O cache memory)
  262. unsigned char** out_temp_cacheEnd);// note: (I/O cache memory) >= hpatch_kStreamCacheSize*3
  263. void (*onPatchFinish)(struct sspatch_listener_t* listener, //onPatchFinish can null
  264. unsigned char* temp_cache, unsigned char* temp_cacheEnd);
  265. } sspatch_listener_t;
  266. typedef struct{
  267. hpatch_TStreamInput base;
  268. hpatch_TDecompress* _decompressPlugin;
  269. hpatch_decompressHandle _decompressHandle;
  270. } hpatch_TUncompresser_t;
  271. typedef struct sspatch_coversListener_t{
  272. void* import;
  273. void (*onStepCoversReset)(struct sspatch_coversListener_t* listener,hpatch_StreamPos_t leaveCoverCount);//can NULL, data(in covers_cache) will invalid
  274. void (*onStepCovers)(struct sspatch_coversListener_t* listener,
  275. const unsigned char* covers_cache,const unsigned char* covers_cacheEnd);
  276. } sspatch_coversListener_t;
  277. typedef struct{
  278. const unsigned char* covers_cache;
  279. const unsigned char* covers_cacheEnd;
  280. hpatch_StreamPos_t lastOldEnd;
  281. hpatch_StreamPos_t lastNewEnd;
  282. hpatch_TCover cover;
  283. } sspatch_covers_t;
  284. hpatch_inline static void sspatch_covers_init(sspatch_covers_t* self) { memset(self,0,sizeof(*self)); }
  285. hpatch_inline static void sspatch_covers_setCoversCache(sspatch_covers_t* self,const unsigned char* covers_cache,const unsigned char* covers_cacheEnd){
  286. self->covers_cache=covers_cache; self->covers_cacheEnd=covers_cacheEnd; }
  287. hpatch_inline static hpatch_BOOL sspatch_covers_isHaveNextCover(const sspatch_covers_t* self) { return (self->covers_cache!=(self)->covers_cacheEnd); }
  288. hpatch_BOOL sspatch_covers_nextCover(sspatch_covers_t* self);
  289. #ifdef __cplusplus
  290. }
  291. #endif
  292. #endif