forked from sisong/HDiffPatch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
checksum_plugin_demo.h
380 lines (358 loc) · 14.5 KB
/
checksum_plugin_demo.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
//checksum_plugin_demo.h
// checksum plugin demo for HDiffz\HPatchz
/*
The MIT License (MIT)
Copyright (c) 2018-2021 HouSisong
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef HPatch_checksum_plugin_demo_h
#define HPatch_checksum_plugin_demo_h
//checksum plugin demo:
// crc32ChecksumPlugin
// adler32ChecksumPlugin
// adler64ChecksumPlugin
// fadler32ChecksumPlugin
// fadler64ChecksumPlugin
// fadler128ChecksumPlugin
// md5ChecksumPlugin
// blake3ChecksumPlugin
#include "libHDiffPatch/HPatch/checksum_plugin.h"
#ifndef _IsNeedIncludeDefaultChecksumHead
# define _IsNeedIncludeDefaultChecksumHead 1
#endif
#define __out_checksum4__(out_code,i0,v,shr) { \
(out_code)[i0 ]=(unsigned char)((v)>>(shr)); \
(out_code)[i0+1]=(unsigned char)((v)>>(shr+8)); \
(out_code)[i0+2]=(unsigned char)((v)>>(shr+16));\
(out_code)[i0+3]=(unsigned char)((v)>>(shr+24));\
}
#define __out_checksum4(out_code,v) __out_checksum4__(out_code,0,v,0)
#define __out_checksum8(out_code,v) { __out_checksum4__(out_code,0,v,0); \
__out_checksum4__(out_code,4,v,32); }
#ifdef _ChecksumPlugin_crc32
#if (_IsNeedIncludeDefaultChecksumHead)
# include "zlib.h" // http://zlib.net/ https://github.com/madler/zlib
#endif
static const char* _crc32_checksumType(void){
static const char* type="crc32";
return type;
}
static size_t _crc32_checksumByteSize(void){
return sizeof(hpatch_uint32_t);
}
static hpatch_checksumHandle _crc32_open(hpatch_TChecksum* plugin){
return malloc(sizeof(hpatch_uint32_t));
}
static void _crc32_close(hpatch_TChecksum* plugin,hpatch_checksumHandle handle){
if (handle) free(handle);
}
static void _crc32_begin(hpatch_checksumHandle handle){
hpatch_uint32_t* pv=(hpatch_uint32_t*)handle;
*pv=(hpatch_uint32_t)crc32(0,0,0);
}
static void _crc32_append(hpatch_checksumHandle handle,
const unsigned char* part_data,const unsigned char* part_data_end){
hpatch_uint32_t* pv=(hpatch_uint32_t*)handle;
uLong v=*pv;
while (part_data!=part_data_end) {
size_t dataSize=(size_t)(part_data_end-part_data);
uInt len=~(uInt)0;
//assert(len>0);
if (len>dataSize)
len=(uInt)dataSize;
v=crc32(v,part_data,len);
part_data+=len;
}
*pv=(hpatch_uint32_t)v;
}
static void _crc32_end(hpatch_checksumHandle handle,
unsigned char* checksum,unsigned char* checksum_end){
hpatch_uint32_t v=*(hpatch_uint32_t*)handle;
assert(4==checksum_end-checksum);
__out_checksum4(checksum,v);
}
static hpatch_TChecksum crc32ChecksumPlugin={ _crc32_checksumType,_crc32_checksumByteSize,_crc32_open,
_crc32_close,_crc32_begin,_crc32_append,_crc32_end};
#endif//_ChecksumPlugin_crc32
#ifdef _ChecksumPlugin_adler32
# ifndef _ChecksumPlugin_crc32
# include "libHDiffPatch/HDiff/private_diff/limit_mem_diff/adler_roll.h"
# endif
static const char* _adler32_checksumType(void){
static const char* type="adler32";
return type;
}
static size_t _adler32_checksumByteSize(void){
return sizeof(hpatch_uint32_t);
}
static hpatch_checksumHandle _adler32_open(hpatch_TChecksum* plugin){
return malloc(sizeof(hpatch_uint32_t));
}
static void _adler32_close(hpatch_TChecksum* plugin,hpatch_checksumHandle handle){
if (handle) free(handle);
}
static void _adler32_begin(hpatch_checksumHandle handle){
hpatch_uint32_t* pv=(hpatch_uint32_t*)handle;
#ifdef _ChecksumPlugin_crc32
*pv=(hpatch_uint32_t)adler32(0,0,0);
#else
*pv=adler32_start(0,0);
#endif
}
static void _adler32_append(hpatch_checksumHandle handle,
const unsigned char* part_data,const unsigned char* part_data_end){
hpatch_uint32_t* pv=(hpatch_uint32_t*)handle;
#ifdef _ChecksumPlugin_crc32
uLong v=*pv;
while (part_data!=part_data_end) {
size_t dataSize=(size_t)(part_data_end-part_data);
uInt len=~(uInt)0;
//assert(len>0);
if (len>dataSize)
len=(uInt)dataSize;
v=adler32(v,part_data,len);
part_data+=len;
}
*pv=(hpatch_uint32_t)v;
#else
*pv=_adler32_append(*pv,part_data,(size_t)(part_data_end-part_data));
#endif
}
static void _adler32_end(hpatch_checksumHandle handle,
unsigned char* checksum,unsigned char* checksum_end){
hpatch_uint32_t v=*(hpatch_uint32_t*)handle;
assert(4==checksum_end-checksum);
__out_checksum4(checksum,v);
}
static hpatch_TChecksum adler32ChecksumPlugin={ _adler32_checksumType,_adler32_checksumByteSize,_adler32_open,
_adler32_close,_adler32_begin,_adler32_append,_adler32_end};
#endif//_ChecksumPlugin_adler32
#ifdef _ChecksumPlugin_adler64
#include "libHDiffPatch/HDiff/private_diff/limit_mem_diff/adler_roll.h"
static const char* _adler64_checksumType(void){
static const char* type="adler64";
return type;
}
static size_t _adler64_checksumByteSize(void){
return sizeof(hpatch_uint64_t);
}
static hpatch_checksumHandle _adler64_open(hpatch_TChecksum* plugin){
return malloc(sizeof(hpatch_uint64_t));
}
static void _adler64_close(hpatch_TChecksum* plugin,hpatch_checksumHandle handle){
if (handle) free(handle);
}
static void _adler64_begin(hpatch_checksumHandle handle){
hpatch_uint64_t* pv=(hpatch_uint64_t*)handle;
*pv=adler64_start(0,0);
}
static void _adler64_append(hpatch_checksumHandle handle,
const unsigned char* part_data,const unsigned char* part_data_end){
hpatch_uint64_t* pv=(hpatch_uint64_t*)handle;
*pv=adler64_append(*pv,part_data,part_data_end-part_data);
}
static void _adler64_end(hpatch_checksumHandle handle,
unsigned char* checksum,unsigned char* checksum_end){
hpatch_uint64_t v=*(hpatch_uint64_t*)handle;
assert(8==checksum_end-checksum);
__out_checksum8(checksum,v);
}
static hpatch_TChecksum adler64ChecksumPlugin={ _adler64_checksumType,_adler64_checksumByteSize,_adler64_open,
_adler64_close,_adler64_begin,_adler64_append,_adler64_end};
#endif//_ChecksumPlugin_adler64
#ifdef _ChecksumPlugin_fadler32
#include "libHDiffPatch/HDiff/private_diff/limit_mem_diff/adler_roll.h"
static const char* _fadler32_checksumType(void){
static const char* type="fadler32";
return type;
}
static size_t _fadler32_checksumByteSize(void){
return sizeof(hpatch_uint32_t);
}
static hpatch_checksumHandle _fadler32_open(hpatch_TChecksum* plugin){
return malloc(sizeof(hpatch_uint32_t));
}
static void _fadler32_close(hpatch_TChecksum* plugin,hpatch_checksumHandle handle){
if (handle) free(handle);
}
static void _fadler32_begin(hpatch_checksumHandle handle){
hpatch_uint32_t* pv=(hpatch_uint32_t*)handle;
*pv=*pv=fast_adler32_start(0,0);
}
static void _fadler32_append(hpatch_checksumHandle handle,
const unsigned char* part_data,const unsigned char* part_data_end){
hpatch_uint32_t* pv=(hpatch_uint32_t*)handle;
*pv=fast_adler32_append(*pv,part_data,part_data_end-part_data);
}
static void _fadler32_end(hpatch_checksumHandle handle,
unsigned char* checksum,unsigned char* checksum_end){
hpatch_uint32_t v=*(hpatch_uint32_t*)handle;
assert(4==checksum_end-checksum);
__out_checksum4(checksum,v);
}
static hpatch_TChecksum fadler32ChecksumPlugin={ _fadler32_checksumType,_fadler32_checksumByteSize,_fadler32_open,
_fadler32_close,_fadler32_begin,_fadler32_append,_fadler32_end};
#endif//_ChecksumPlugin_fadler32
#ifdef _ChecksumPlugin_fadler64
#include "libHDiffPatch/HDiff/private_diff/limit_mem_diff/adler_roll.h"
static const char* _fadler64_checksumType(void){
static const char* type="fadler64";
return type;
}
static size_t _fadler64_checksumByteSize(void){
return sizeof(hpatch_uint64_t);
}
static hpatch_checksumHandle _fadler64_open(hpatch_TChecksum* plugin){
return malloc(sizeof(hpatch_uint64_t));
}
static void _fadler64_close(hpatch_TChecksum* plugin,hpatch_checksumHandle handle){
if (handle) free(handle);
}
static void _fadler64_begin(hpatch_checksumHandle handle){
hpatch_uint64_t* pv=(hpatch_uint64_t*)handle;
*pv=fast_adler64_start(0,0);
}
static void _fadler64_append(hpatch_checksumHandle handle,
const unsigned char* part_data,const unsigned char* part_data_end){
hpatch_uint64_t* pv=(hpatch_uint64_t*)handle;
*pv=fast_adler64_append(*pv,part_data,part_data_end-part_data);
}
static void _fadler64_end(hpatch_checksumHandle handle,
unsigned char* checksum,unsigned char* checksum_end){
hpatch_uint64_t v=*(hpatch_uint64_t*)handle;
assert(8==checksum_end-checksum);
__out_checksum8(checksum,v);
}
static hpatch_TChecksum fadler64ChecksumPlugin={ _fadler64_checksumType,_fadler64_checksumByteSize,_fadler64_open,
_fadler64_close,_fadler64_begin,_fadler64_append,_fadler64_end};
#endif//_ChecksumPlugin_fadler64
#ifdef _ChecksumPlugin_fadler128
#include "libHDiffPatch/HDiff/private_diff/limit_mem_diff/adler_roll.h"
static const char* _fadler128_checksumType(void){
static const char* type="fadler128";
return type;
}
static size_t _fadler128_checksumByteSize(void){
return sizeof(adler128_t);
}
static hpatch_checksumHandle _fadler128_open(hpatch_TChecksum* plugin){
return malloc(sizeof(adler128_t));
}
static void _fadler128_close(hpatch_TChecksum* plugin,hpatch_checksumHandle handle){
if (handle) free(handle);
}
static void _fadler128_begin(hpatch_checksumHandle handle){
adler128_t* pv=(adler128_t*)handle;
*pv=fast_adler128_start(0,0);
}
static void _fadler128_append(hpatch_checksumHandle handle,
const unsigned char* part_data,const unsigned char* part_data_end){
adler128_t* pv=(adler128_t*)handle;
*pv=fast_adler128_append(*pv,part_data,part_data_end-part_data);
}
static void _fadler128_end(hpatch_checksumHandle handle,
unsigned char* checksum,unsigned char* checksum_end){
const adler128_t* pv=(const adler128_t*)handle;
assert(16==checksum_end-checksum);
__out_checksum8(checksum,pv->adler);
__out_checksum8(checksum+8,pv->sum);
}
static hpatch_TChecksum fadler128ChecksumPlugin={ _fadler128_checksumType,_fadler128_checksumByteSize,
_fadler128_open,_fadler128_close,_fadler128_begin,_fadler128_append,_fadler128_end};
#endif//_ChecksumPlugin_fadler128
#ifdef _ChecksumPlugin_md5
#if (_IsNeedIncludeDefaultChecksumHead)
# include "md5.h" // https://sourceforge.net/projects/libmd5-rfc https://github.com/sisong/libmd5
#endif
static const char* _md5_checksumType(void){
static const char* type="md5";
return type;
}
static size_t _md5_checksumByteSize(void){
return 16;
}
static hpatch_checksumHandle _md5_open(hpatch_TChecksum* plugin){
return malloc(sizeof(md5_state_t));
}
static void _md5_close(hpatch_TChecksum* plugin,hpatch_checksumHandle handle){
if (handle) free(handle);
}
static void _md5_begin(hpatch_checksumHandle handle){
md5_state_t* ps=(md5_state_t*)handle;
md5_init(ps);
}
static void _md5_append(hpatch_checksumHandle handle,
const unsigned char* part_data,const unsigned char* part_data_end){
md5_state_t* ps=(md5_state_t*)handle;
while (part_data!=part_data_end) {
size_t dataSize=(size_t)(part_data_end-part_data);
int len=(int)((~(unsigned int)0)>>1);
//assert(len>0);
if ((unsigned int)len>dataSize)
len=(int)dataSize;
md5_append(ps,part_data,len);
part_data+=len;
}
}
static void _md5_end(hpatch_checksumHandle handle,
unsigned char* checksum,unsigned char* checksum_end){
md5_state_t* ps=(md5_state_t*)handle;
assert(16==checksum_end-checksum);
md5_finish(ps,checksum);
}
static hpatch_TChecksum md5ChecksumPlugin={ _md5_checksumType,_md5_checksumByteSize,_md5_open,
_md5_close,_md5_begin,_md5_append,_md5_end};
#endif//_ChecksumPlugin_md5
#ifdef _ChecksumPlugin_blake3
#if (_IsNeedIncludeDefaultChecksumHead)
# include "blake3.h" // https://github.com/BLAKE3-team/BLAKE3
#endif
static const char* _blake3_checksumType(void){
static const char* type="blake3";
return type;
}
static size_t _blake3_checksumByteSize(void){
return BLAKE3_OUT_LEN;
}
static hpatch_checksumHandle _blake3_open(hpatch_TChecksum* plugin){
return malloc(sizeof(blake3_hasher));
}
static void _blake3_close(hpatch_TChecksum* plugin,hpatch_checksumHandle handle){
if (handle) free(handle);
}
static void _blake3_begin(hpatch_checksumHandle handle){
blake3_hasher* ps=(blake3_hasher*)handle;
blake3_hasher_init(ps);
}
static void _blake3_append(hpatch_checksumHandle handle,
const unsigned char* part_data,const unsigned char* part_data_end){
blake3_hasher* ps=(blake3_hasher*)handle;
blake3_hasher_update(ps,part_data,(size_t)(part_data_end-part_data));
}
static void _blake3_end(hpatch_checksumHandle handle,
unsigned char* checksum,unsigned char* checksum_end){
blake3_hasher* ps=(blake3_hasher*)handle;
assert(BLAKE3_OUT_LEN==checksum_end-checksum);
blake3_hasher_finalize(ps,checksum,(size_t)(checksum_end-checksum));
}
static hpatch_TChecksum blake3ChecksumPlugin={ _blake3_checksumType,_blake3_checksumByteSize,_blake3_open,
_blake3_close,_blake3_begin,_blake3_append,_blake3_end};
#endif//_ChecksumPlugin_blake3
#endif