patch.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. //patch.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_h
  26. #define HPatch_patch_h
  27. #include "patch_types.h"
  28. #ifdef __cplusplus
  29. extern "C" {
  30. #endif
  31. //all patch*() functions do not allocate memory
  32. //optimize speed for patch_stream_with_cache() && patch_decompress_with_cache():
  33. // preload part of oldData into cache,
  34. // cache memory size (temp_cache_end-temp_cache) the larger the better for large oldData file
  35. #ifndef _IS_NEED_CACHE_OLD_BY_COVERS
  36. # define _IS_NEED_CACHE_OLD_BY_COVERS 1
  37. #endif
  38. //generate newData by patch(oldData + serializedDiff)
  39. // serializedDiff create by create_diff()
  40. hpatch_BOOL patch(unsigned char* out_newData,unsigned char* out_newData_end,
  41. const unsigned char* oldData,const unsigned char* oldData_end,
  42. const unsigned char* serializedDiff,const unsigned char* serializedDiff_end);
  43. //patch by stream, see patch()
  44. // used (hpatch_kStreamCacheSize*8 stack memory) for I/O cache
  45. // if use patch_stream_with_cache(), can passing larger memory cache to optimize speed
  46. // serializedDiff create by create_diff()
  47. hpatch_BOOL patch_stream(const hpatch_TStreamOutput* out_newData, //sequential write
  48. const hpatch_TStreamInput* oldData, //random read
  49. const hpatch_TStreamInput* serializedDiff); //random read
  50. //see patch_stream()
  51. // can passing more memory for I/O cache to optimize speed
  52. // note: (temp_cache_end-temp_cache)>=2048
  53. hpatch_BOOL patch_stream_with_cache(const hpatch_TStreamOutput* out_newData, //sequential write
  54. const hpatch_TStreamInput* oldData, //random read
  55. const hpatch_TStreamInput* serializedDiff, //random read
  56. unsigned char* temp_cache,unsigned char* temp_cache_end);
  57. //get compressedDiff info
  58. // compressedDiff created by create_compressed_diff() or create_compressed_diff_stream()
  59. hpatch_BOOL getCompressedDiffInfo(hpatch_compressedDiffInfo* out_diffInfo,
  60. const hpatch_TStreamInput* compressedDiff);
  61. //see getCompressedDiffInfo()
  62. hpatch_inline static hpatch_BOOL
  63. getCompressedDiffInfo_mem(hpatch_compressedDiffInfo* out_diffInfo,
  64. const unsigned char* compressedDiff,
  65. const unsigned char* compressedDiff_end){
  66. hpatch_TStreamInput diffStream;
  67. mem_as_hStreamInput(&diffStream,compressedDiff,compressedDiff_end);
  68. return getCompressedDiffInfo(out_diffInfo,&diffStream);
  69. }
  70. //patch with decompress plugin
  71. // used (hpatch_kStreamCacheSize*6 stack memory) + (decompress buffer*4)
  72. // compressedDiff create by create_compressed_diff() or create_compressed_diff_stream()
  73. // decompressPlugin can null when no compressed data in compressedDiff
  74. // if use patch_decompress_with_cache(), can passing larger memory cache to optimize speed
  75. hpatch_BOOL patch_decompress(const hpatch_TStreamOutput* out_newData, //sequential write
  76. const hpatch_TStreamInput* oldData, //random read
  77. const hpatch_TStreamInput* compressedDiff, //random read
  78. hpatch_TDecompress* decompressPlugin);
  79. //see patch_decompress()
  80. // can passing larger memory cache to optimize speed
  81. // note: (temp_cache_end-temp_cache)>=2048
  82. hpatch_BOOL patch_decompress_with_cache(const hpatch_TStreamOutput* out_newData, //sequential write
  83. const hpatch_TStreamInput* oldData, //random read
  84. const hpatch_TStreamInput* compressedDiff, //random read
  85. hpatch_TDecompress* decompressPlugin,
  86. unsigned char* temp_cache,unsigned char* temp_cache_end);
  87. //see patch_decompress()
  88. hpatch_inline static hpatch_BOOL
  89. patch_decompress_mem(unsigned char* out_newData,unsigned char* out_newData_end,
  90. const unsigned char* oldData,const unsigned char* oldData_end,
  91. const unsigned char* compressedDiff,const unsigned char* compressedDiff_end,
  92. hpatch_TDecompress* decompressPlugin){
  93. hpatch_TStreamOutput out_newStream;
  94. hpatch_TStreamInput oldStream;
  95. hpatch_TStreamInput diffStream;
  96. mem_as_hStreamOutput(&out_newStream,out_newData,out_newData_end);
  97. mem_as_hStreamInput(&oldStream,oldData,oldData_end);
  98. mem_as_hStreamInput(&diffStream,compressedDiff,compressedDiff_end);
  99. return patch_decompress(&out_newStream,&oldStream,&diffStream,decompressPlugin);
  100. }
  101. // hpatch_TCoverList: open diffData and read coverList
  102. typedef struct hpatch_TCoverList{
  103. hpatch_TCovers* ICovers;
  104. //private:
  105. unsigned char _buf[hpatch_kStreamCacheSize*4];
  106. } hpatch_TCoverList;
  107. hpatch_inline static
  108. void hpatch_coverList_init(hpatch_TCoverList* coverList) {
  109. assert(coverList!=0); memset(coverList,0,sizeof(*coverList)-sizeof(coverList->_buf)); }
  110. // serializedDiff create by create_diff()
  111. hpatch_BOOL hpatch_coverList_open_serializedDiff(hpatch_TCoverList* out_coverList,
  112. const hpatch_TStreamInput* serializedDiff);
  113. // compressedDiff create by create_compressed_diff() or create_compressed_diff_stream()
  114. hpatch_BOOL hpatch_coverList_open_compressedDiff(hpatch_TCoverList* out_coverList,
  115. const hpatch_TStreamInput* compressedDiff,
  116. hpatch_TDecompress* decompressPlugin);
  117. hpatch_inline static
  118. hpatch_BOOL hpatch_coverList_close(hpatch_TCoverList* coverList) {
  119. hpatch_BOOL result=hpatch_TRUE;
  120. if ((coverList!=0)&&(coverList->ICovers)){
  121. result=coverList->ICovers->close(coverList->ICovers);
  122. hpatch_coverList_init(coverList); } return result; }
  123. //patch singleCompressedDiff with listener
  124. // used (stepMemSize memory) + (I/O cache memory) + (decompress buffer*1)
  125. // every byte in singleCompressedDiff will only be read once in order
  126. // singleCompressedDiff create by create_single_compressed_diff() or create_single_compressed_diff_stream()
  127. // you can download&patch diffData at the same time, without saving it to disk
  128. // same as call getSingleCompressedDiffInfo() + listener->onDiffInfo() + patch_single_compressed_diff()
  129. hpatch_BOOL patch_single_stream(sspatch_listener_t* listener, //call back when got diffInfo
  130. const hpatch_TStreamOutput* out_newData, //sequential write
  131. const hpatch_TStreamInput* oldData, //random read
  132. const hpatch_TStreamInput* singleCompressedDiff, //sequential read every byte
  133. hpatch_StreamPos_t diffInfo_pos, //default 0, begin pos in singleCompressedDiff
  134. sspatch_coversListener_t* coversListener //default NULL, call by on got covers
  135. );
  136. static hpatch_inline hpatch_BOOL
  137. patch_single_stream_mem(sspatch_listener_t* listener,
  138. unsigned char* out_newData,unsigned char* out_newData_end,
  139. const unsigned char* oldData,const unsigned char* oldData_end,
  140. const unsigned char* diff,const unsigned char* diff_end,
  141. sspatch_coversListener_t* coversListener){
  142. hpatch_TStreamOutput out_newStream;
  143. hpatch_TStreamInput oldStream;
  144. hpatch_TStreamInput diffStream;
  145. mem_as_hStreamOutput(&out_newStream,out_newData,out_newData_end);
  146. mem_as_hStreamInput(&oldStream,oldData,oldData_end);
  147. mem_as_hStreamInput(&diffStream,diff,diff_end);
  148. return patch_single_stream(listener,&out_newStream,&oldStream,&diffStream,0,coversListener);
  149. }
  150. //get singleCompressedDiff info
  151. // singleCompressedDiff create by create_single_compressed_diff() or create_single_compressed_diff_stream()
  152. hpatch_BOOL getSingleCompressedDiffInfo(hpatch_singleCompressedDiffInfo* out_diffInfo,
  153. const hpatch_TStreamInput* singleCompressedDiff, //sequential read
  154. hpatch_StreamPos_t diffInfo_pos//default 0, begin pos in singleCompressedDiff
  155. );
  156. hpatch_inline static hpatch_BOOL
  157. getSingleCompressedDiffInfo_mem(hpatch_singleCompressedDiffInfo* out_diffInfo,
  158. const unsigned char* singleCompressedDiff,
  159. const unsigned char* singleCompressedDiff_end){
  160. hpatch_TStreamInput diffStream;
  161. mem_as_hStreamInput(&diffStream,singleCompressedDiff,singleCompressedDiff_end);
  162. return getSingleCompressedDiffInfo(out_diffInfo,&diffStream,0);
  163. }
  164. //patch singleCompressedDiff with diffInfo
  165. // used (stepMemSize memory) + (I/O cache memory) + (decompress buffer*1)
  166. // note: (I/O cache memory) >= hpatch_kStreamCacheSize*3
  167. // temp_cache_end-temp_cache == stepMemSize + (I/O cache memory)
  168. // singleCompressedDiff create by create_single_compressed_diff() or create_single_compressed_diff_stream()
  169. // decompressPlugin can null when no compressed data in singleCompressedDiff
  170. // same as call compressed_stream_as_uncompressed() + patch_single_stream_diff()
  171. hpatch_BOOL patch_single_compressed_diff(const hpatch_TStreamOutput* out_newData, //sequential write
  172. const hpatch_TStreamInput* oldData, //random read
  173. const hpatch_TStreamInput* singleCompressedDiff, //sequential read
  174. hpatch_StreamPos_t diffData_pos, //diffData begin pos in singleCompressedDiff
  175. hpatch_StreamPos_t uncompressedSize,
  176. hpatch_StreamPos_t compressedSize,
  177. hpatch_TDecompress* decompressPlugin,
  178. hpatch_StreamPos_t coverCount,hpatch_size_t stepMemSize,
  179. unsigned char* temp_cache,unsigned char* temp_cache_end,
  180. sspatch_coversListener_t* coversListener //default NULL, call by on got covers
  181. );
  182. hpatch_BOOL compressed_stream_as_uncompressed(hpatch_TUncompresser_t* uncompressedStream,hpatch_StreamPos_t uncompressedSize,
  183. hpatch_TDecompress* decompressPlugin,const hpatch_TStreamInput* compressedStream,
  184. hpatch_StreamPos_t compressed_pos,hpatch_StreamPos_t compressed_end);
  185. void close_compressed_stream_as_uncompressed(hpatch_TUncompresser_t* uncompressedStream);
  186. hpatch_BOOL patch_single_stream_diff(const hpatch_TStreamOutput* out_newData, //sequential write
  187. const hpatch_TStreamInput* oldData, //random read
  188. const hpatch_TStreamInput* uncompressedDiffData, //sequential read
  189. hpatch_StreamPos_t diffData_pos,//diffData begin pos in uncompressedDiffData
  190. hpatch_StreamPos_t diffData_posEnd,//diffData end pos in uncompressedDiffData
  191. hpatch_StreamPos_t coverCount,hpatch_size_t stepMemSize,
  192. unsigned char* temp_cache,unsigned char* temp_cache_end,
  193. sspatch_coversListener_t* coversListener);
  194. #ifdef __cplusplus
  195. }
  196. #endif
  197. #endif