4
4
#include "HDiffPatch/libHDiffPatch/HPatch/patch.h"
5
5
#include "HDiffPatch/file_for_patch.h"
6
6
7
+ //#define _CompressPlugin_zlib
8
+ //#define _CompressPlugin_bz2
9
+ //#define _CompressPlugin_lzma
10
+ #define _CompressPlugin_lzma2
11
+ #include "HDiffPatch/decompress_plugin_demo.h"
12
+
13
+ #define kMaxLoadMemOldSize ((1<<20)*8)
14
+
7
15
enum {
8
16
kHPatch_ok = 0 ,
9
- kHPatch_error_info = -1 ,
10
- kHPatch_error_old_fopen = -2 ,
11
- kHPatch_error_old_fread = -3 ,
12
- kHPatch_error_old_fclose = -4 ,
13
- kHPatch_error_pat_fopen = -5 ,
14
- kHPatch_error_pat_fread = -6 ,
15
- kHPatch_error_pat_fclose = -7 ,
16
- kHPatch_error_new_fopen = -8 ,
17
- kHPatch_error_new_fwrite = -9 ,
18
- kHPatch_error_new_fclose = -10 ,
17
+ kHPatch_error_malloc = -1 ,
18
+ kHPatch_error_info = -2 ,
19
+ kHPatch_error_compressType = -3 ,
20
+ kHPatch_error_patch = -4 ,
21
+ kHPatch_error_old_fopen = -5 ,
22
+ kHPatch_error_old_fread = -6 ,
23
+ kHPatch_error_old_fclose = -7 ,
24
+ kHPatch_error_pat_fopen = -8 ,
25
+ kHPatch_error_pat_fread = -9 ,
26
+ kHPatch_error_pat_fclose = -10 ,
27
+ kHPatch_error_new_fopen = -11 ,
28
+ kHPatch_error_new_fwrite = -12 ,
29
+ kHPatch_error_new_fclose = -13 ,
30
+ kHPatch_error_old_size = -14 ,
31
+ kHPatch_error_new_size = -15 ,
19
32
};
20
33
21
34
#define _check (v ,errorType ) do{ \
@@ -30,11 +43,71 @@ int hpatch_getInfo_by_mem(struct hpatch_singleCompressedDiffInfo* out_patinfo,
30
43
return kHPatch_ok ; //ok
31
44
}
32
45
33
-
34
- static int hpatch_by_stream (const hpatch_TStreamInput * old ,hpatch_BOOL isLoadOldToMem ,const hpatch_TStreamInput * pat ,
46
+ static hpatch_TDecompress * getDecompressPlugin (const char * compressType ){
47
+ #ifdef _CompressPlugin_zlib
48
+ if (zlibDecompressPlugin .is_can_open (compressType ))
49
+ return & zlibDecompressPlugin ;
50
+ #endif
51
+ #ifdef _CompressPlugin_bz2
52
+ if (bz2DecompressPlugin .is_can_open (compressType ))
53
+ return & bz2DecompressPlugin ;
54
+ #endif
55
+ #ifdef _CompressPlugin_lzma
56
+ if (lzmaDecompressPlugin .is_can_open (compressType ))
57
+ return & lzmaDecompressPlugin ;
58
+ #endif
59
+ #ifdef _CompressPlugin_lzma2
60
+ if (lzma2DecompressPlugin .is_can_open (compressType ))
61
+ return & lzma2DecompressPlugin ;
62
+ #endif
63
+ return 0 ;
64
+ }
65
+ static int hpatch_by_stream (const hpatch_TStreamInput * old ,hpatch_BOOL isLoadOldAllToMem ,const hpatch_TStreamInput * pat ,
35
66
hpatch_TStreamOutput * out_new ,const hpatch_singleCompressedDiffInfo * patInfo ){
36
- //todo:
37
- return -1 ;
67
+ int result = kHPatch_ok ;
68
+ int _isInClear = hpatch_FALSE ;
69
+ hpatch_TDecompress * decompressPlugin = 0 ;
70
+ uint8_t * temp_cache = 0 ;
71
+ size_t temp_cache_size ;
72
+ hpatch_singleCompressedDiffInfo _patinfo ;
73
+ hpatch_TStreamInput _old ;
74
+ {// info
75
+ if (!patInfo ){
76
+ _check (getSingleCompressedDiffInfo (& _patinfo ,pat ,0 ),kHPatch_error_info );
77
+ patInfo = & _patinfo ;
78
+ }
79
+ _check (old -> streamSize == patInfo -> oldDataSize ,kHPatch_error_old_size );
80
+ _check (out_new -> streamSize >=patInfo -> newDataSize ,kHPatch_error_new_size );
81
+ out_new -> streamSize = patInfo -> newDataSize ;
82
+ if (strlen (patInfo -> compressType )> 0 ){
83
+ decompressPlugin = getDecompressPlugin (patInfo -> compressType );
84
+ _check (decompressPlugin ,kHPatch_error_compressType );
85
+ }
86
+ }
87
+ {// mem
88
+ size_t mem_size ;
89
+ size_t oldSize = (size_t )old -> streamSize ;
90
+ isLoadOldAllToMem = isLoadOldAllToMem && (old -> streamSize <=kMaxLoadMemOldSize );
91
+ temp_cache_size = patInfo -> stepMemSize + hpatch_kFileIOBufBetterSize * 3 ;
92
+ mem_size = temp_cache_size + (isLoadOldAllToMem ?oldSize :0 );
93
+ temp_cache = malloc (mem_size );
94
+ _check (temp_cache ,kHPatch_error_malloc );
95
+ if (isLoadOldAllToMem ){//load old to mem
96
+ uint8_t * oldMem = temp_cache + temp_cache_size ;
97
+ _check (old -> read (old ,0 ,oldMem ,oldMem + oldSize ),kHPatch_error_old_fread );
98
+ mem_as_hStreamInput (& _old ,oldMem ,oldMem + oldSize );
99
+ old = & _old ;
100
+ }
101
+ }
102
+
103
+ _check (patch_single_compressed_diff (& out_new ,old ,pat ,patInfo -> diffDataPos ,
104
+ patInfo -> uncompressedSize ,decompressPlugin ,patInfo -> coverCount ,
105
+ patInfo -> stepMemSize ,temp_cache ,temp_cache + temp_cache_size ),kHPatch_error_patch );
106
+
107
+ _clear :
108
+ _isInClear = hpatch_TRUE ;
109
+ if (temp_cache ){ free (temp_cache ); temp_cache = 0 ; }
110
+ return result ;
38
111
}
39
112
40
113
int hpatch_by_mem (const uint8_t * old ,size_t oldsize ,uint8_t * newBuf ,size_t newsize ,
0 commit comments