-
Notifications
You must be signed in to change notification settings - Fork 5
/
dict_compress_plugin_demo.h
576 lines (526 loc) · 26 KB
/
dict_compress_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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
//dict_compress_plugin_demo.h
// dict compress plugin demo for hsync_make
/*
The MIT License (MIT)
Copyright (c) 2020-2024 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 dict_compress_plugin_demo_h
#define dict_compress_plugin_demo_h
//dict compress plugin demo:
// zlibDictCompressPlugin
// gzipDictCompressPlugin // compatible with gzip encoding
// ldefDictCompressPlugin // compatible with zlib's deflate encoding
// lgzipDictCompressPlugin // compatible with gzip encoding
// zstdDictCompressPlugin
#include "HDiffPatch/libhsync/sync_make/dict_compress_plugin.h"
#ifdef __cplusplus
extern "C" {
#endif
#ifndef HDiff_compress_plugin_demo_h
#ifndef _IsNeedIncludeDefaultCompressHead
# define _IsNeedIncludeDefaultCompressHead 1
#endif
#define _def_fun_compressType(_fun_name,cstr) \
static const char* _fun_name(void){ \
static const char* kCompressType=cstr; \
return kCompressType; \
}
hpatch_inline static
hpatch_StreamPos_t _default_maxCompressedSize(hpatch_StreamPos_t dataSize){
hpatch_StreamPos_t result=dataSize+(dataSize>>3)+64;
assert(result>dataSize);
return result;
}
#endif
#ifndef IS_REUSE_compress_handle
# define IS_REUSE_compress_handle 0
#endif
static size_t _default_getBestWorkBlockCount(hsync_TDictCompress* compressPlugin,size_t blockCount,
size_t blockSize,size_t defaultWorkBlockCount){
return defaultWorkBlockCount;
}
static size_t _getDictBitsByData(size_t bits,size_t kMinBits,hpatch_StreamPos_t dataSize){
while ((bits>kMinBits)&&(((hpatch_StreamPos_t)1)<<(bits-1)>=dataSize))
--bits;
return bits;
}
#define _checkCompress(v) do { if (!(v)) return kDictCompressError; } while(0)
static const char* k_gzip_dictCompressType="gzipD";
#ifdef _CompressPlugin_zlib
#if (_IsNeedIncludeDefaultCompressHead)
# include "zlib.h" // http://zlib.net/ https://github.com/madler/zlib
# define _IS_ZLIB_CompressContinueAfterEnd 1
# if (_IS_ZLIB_CompressContinueAfterEnd)
# include "deflate.h" // if no "deflate.h" file, define _IS_ZLIB_CompressContinueAfterEnd to 0
# endif
#endif
//zlibDictCompressPlugin
typedef struct{
hsync_TDictCompress base;
hpatch_byte compress_level; // 0..9
hpatch_byte mem_level; // 8..9
hpatch_byte dict_bits; // 9..15
hpatch_BOOL is_gzip;
} TDictCompressPlugin_zlib;
typedef struct{
z_stream stream;
_CacheBlockDict_t cache;
hpatch_BOOL dict_isInReset;
hpatch_BOOL is_gzip;
} _TDictCompressPlugin_zlib_data;
static size_t _zlib_getDictSize(struct hsync_TDictCompress* compressPlugin){
TDictCompressPlugin_zlib* plugin=(TDictCompressPlugin_zlib*)compressPlugin;
const size_t dictSize=((size_t)1<<plugin->dict_bits);
return dictSize;
}
static size_t _zlib_limitDictSizeByData(struct hsync_TDictCompress* compressPlugin,size_t blockCount,size_t blockSize){
TDictCompressPlugin_zlib* plugin=(TDictCompressPlugin_zlib*)compressPlugin;
const hpatch_StreamPos_t dataSize=((hpatch_StreamPos_t)blockCount)*blockSize;
size_t dictBits=_getDictBitsByData(plugin->dict_bits,9,dataSize);
plugin->dict_bits=(hpatch_byte)dictBits;
return ((size_t)1)<<dictBits;
}
static hsync_dictCompressHandle _zlib_dictCompressOpen(struct hsync_TDictCompress* compressPlugin,size_t blockCount,size_t blockSize){
const TDictCompressPlugin_zlib* plugin=(const TDictCompressPlugin_zlib*)compressPlugin;
const size_t dictSize=((size_t)1<<plugin->dict_bits);
const size_t cacheDictSize=_getCacheBlockDictSize(dictSize,blockCount,blockSize);
_TDictCompressPlugin_zlib_data* self=(_TDictCompressPlugin_zlib_data*)malloc(sizeof(_TDictCompressPlugin_zlib_data)+cacheDictSize);
if (self==0) return 0;
memset(self,0,sizeof(*self));
_CacheBlockDict_init(&self->cache,((hpatch_byte*)self)+sizeof(*self),
cacheDictSize,dictSize,blockCount,blockSize);
if (deflateInit2(&self->stream,plugin->compress_level,Z_DEFLATED,
-(int)plugin->dict_bits,plugin->mem_level,Z_DEFAULT_STRATEGY)!=Z_OK){
free(self);
return 0;// error
}
self->is_gzip=plugin->is_gzip;
return self;
}
static void _zlib_dictCompressClose(struct hsync_TDictCompress* compressPlugin,
hsync_dictCompressHandle dictHandle){
_TDictCompressPlugin_zlib_data* self=(_TDictCompressPlugin_zlib_data*)dictHandle;
if (self!=0){
if (self->stream.state!=0){
int ret;
#if (_IS_ZLIB_CompressContinueAfterEnd)
self->stream.state->status=FINISH_STATE;
#endif
ret=deflateEnd(&self->stream);
self->stream.state=0;
assert(Z_OK==ret);
}
free(self);
}
}
static hpatch_byte* _zlib_getResetDictBuffer(hsync_dictCompressHandle dictHandle,size_t blockIndex,
size_t* out_dictSize){
_TDictCompressPlugin_zlib_data* self=(_TDictCompressPlugin_zlib_data*)dictHandle;
assert(!self->dict_isInReset);
self->dict_isInReset=hpatch_TRUE;
return _CacheBlockDict_getResetDictBuffer(&self->cache,blockIndex,out_dictSize);
}
static size_t _zlib_dictCompress(hsync_dictCompressHandle dictHandle,size_t blockIndex,
hpatch_byte* out_code,hpatch_byte* out_codeEnd,
const hpatch_byte* in_dataBegin,size_t in_dataSize,size_t in_borderSize){
_TDictCompressPlugin_zlib_data* self=(_TDictCompressPlugin_zlib_data*)dictHandle;
const hpatch_BOOL in_isEnd=(blockIndex+1==self->cache.blockCount);
size_t result;
if (self->dict_isInReset){ //reset dict
hpatch_byte* dict;
size_t dictSize;
_CacheBlockDict_usedDict(&self->cache,blockIndex,&dict,&dictSize);
assert(dictSize==(uInt)dictSize);
_checkCompress(deflateReset(&self->stream)==Z_OK);
_checkCompress(deflateSetDictionary(&self->stream,dict,(uInt)dictSize)==Z_OK);
self->dict_isInReset=hpatch_FALSE;
}
self->stream.next_in =(Bytef*)in_dataBegin;
self->stream.avail_in=(uInt)in_dataSize;
assert(self->stream.avail_in==in_dataSize);
self->stream.next_out =out_code;
self->stream.avail_out=(uInt)(out_codeEnd-out_code);
assert(self->stream.avail_out==(size_t)(out_codeEnd-out_code));
if (self->is_gzip&&(!in_isEnd)){
int bits;
_checkCompress(deflate(&self->stream,Z_BLOCK)==Z_OK);
// add enough empty blocks to get to a byte boundary
_checkCompress(deflatePending(&self->stream,Z_NULL,&bits)==Z_OK);
if (bits & 1){
_checkCompress(deflate(&self->stream,Z_SYNC_FLUSH)==Z_OK);
} else if (bits & 7) {
do { // add static empty blocks
_checkCompress(deflatePrime(&self->stream,10,2)==Z_OK);
_checkCompress(deflatePending(&self->stream,Z_NULL,&bits)==Z_OK);
} while (bits & 7);
_checkCompress(deflate(&self->stream,Z_BLOCK)==Z_OK);
}
}else{
_checkCompress(deflate(&self->stream,Z_FINISH)==Z_STREAM_END);
if ((!self->is_gzip)&&(!in_isEnd)){
#if (_IS_ZLIB_CompressContinueAfterEnd)
//hack zlib state, compress continue
self->stream.state->status=BUSY_STATE;
#else
//update dict & deflateSetDictionary every block (NOTE: deflateSetDictionary slow)
_CacheBlockDict_dictUncompress(&self->cache,blockIndex,blockIndex+1,
in_dataBegin,in_dataBegin+in_dataSize);
self->dict_isInReset=hpatch_TRUE;
#endif
}
}
result=self->stream.total_out;
self->stream.total_out=0;
assert(self->stream.avail_in==0);
if ((result>=in_dataSize)&&(!self->is_gzip))
return kDictCompressCancel;// cancel compress
return result;
}
_def_fun_compressType(_zlib_dictCompressType,"zlibD");
static const TDictCompressPlugin_zlib zlibDictCompressPlugin={
{_zlib_dictCompressType,_default_maxCompressedSize,
_zlib_limitDictSizeByData,_default_getBestWorkBlockCount,
_zlib_getDictSize,_zlib_dictCompressOpen,_zlib_dictCompressClose,0,
_zlib_getResetDictBuffer,_zlib_dictCompress},
9,8,MAX_WBITS,hpatch_FALSE};
_def_fun_compressType(_gzip_dictCompressType,k_gzip_dictCompressType);
typedef TDictCompressPlugin_zlib TDictCompressPlugin_gzip;
static const TDictCompressPlugin_gzip gzipDictCompressPlugin={
{_gzip_dictCompressType,_default_maxCompressedSize,
_zlib_limitDictSizeByData,_default_getBestWorkBlockCount,
_zlib_getDictSize,_zlib_dictCompressOpen,_zlib_dictCompressClose,0,
_zlib_getResetDictBuffer,_zlib_dictCompress},
9,8,MAX_WBITS,hpatch_TRUE};
#endif//_CompressPlugin_zlib
#ifdef _CompressPlugin_ldef
#if (_IsNeedIncludeDefaultCompressHead)
# include "libdeflate.h" // https://github.com/ebiggers/libdeflate
#endif
#ifndef MAX_WBITS
# define MAX_WBITS 15
#endif
//ldefDictCompressPlugin
typedef struct{
hsync_TDictCompress base;
hpatch_byte compress_level; // 0..12
hpatch_byte dict_bits; // 9..15
hpatch_BOOL is_gzip;
} TDictCompressPlugin_ldef;
typedef struct{
struct libdeflate_compressor* c;
_CacheBlockDict_t cache;
hpatch_byte* tempDecBufEnd;
hpatch_BOOL dict_isInReset;
hpatch_BOOL is_gzip;
} _TDictCompressPlugin_ldef_data;
static size_t _ldef_getDictCompressBorder(){
return 258; //deflate max match length
}
static size_t _ldef_getDictSize(struct hsync_TDictCompress* compressPlugin){
TDictCompressPlugin_ldef* plugin=(TDictCompressPlugin_ldef*)compressPlugin;
const size_t dictSize=((size_t)1<<plugin->dict_bits);
return dictSize;
}
static size_t _ldef_limitDictSizeByData(struct hsync_TDictCompress* compressPlugin,size_t blockCount,size_t blockSize){
TDictCompressPlugin_ldef* plugin=(TDictCompressPlugin_ldef*)compressPlugin;
const hpatch_StreamPos_t dataSize=((hpatch_StreamPos_t)blockCount)*blockSize;
size_t dictBits=_getDictBitsByData(plugin->dict_bits,9,dataSize);
plugin->dict_bits=(hpatch_byte)dictBits;
return ((size_t)1)<<dictBits;
}
static hsync_dictCompressHandle _ldef_dictCompressOpen(struct hsync_TDictCompress* compressPlugin,size_t blockCount,size_t blockSize){
const TDictCompressPlugin_ldef* plugin=(const TDictCompressPlugin_ldef*)compressPlugin;
const size_t dictSize=((size_t)1<<plugin->dict_bits);
const size_t cacheDictSize=_getCacheBlockDictSize(dictSize,blockCount,blockSize);
const size_t memSize=sizeof(_TDictCompressPlugin_ldef_data)+cacheDictSize+blockSize+_ldef_getDictCompressBorder();
_TDictCompressPlugin_ldef_data* self=(_TDictCompressPlugin_ldef_data*)malloc(memSize);
if (self==0) return 0;
memset(self,0,sizeof(*self));
self->tempDecBufEnd=((hpatch_byte*)self)+memSize;
_CacheBlockDict_init(&self->cache,((hpatch_byte*)self)+sizeof(*self),
cacheDictSize,dictSize,blockCount,blockSize);
self->c=libdeflate_alloc_compressor(plugin->compress_level);
if (!self->c){
free(self);
return 0;// error
}
self->is_gzip=plugin->is_gzip;
return self;
}
static void _ldef_dictCompressClose(struct hsync_TDictCompress* compressPlugin,
hsync_dictCompressHandle dictHandle){
_TDictCompressPlugin_ldef_data* self=(_TDictCompressPlugin_ldef_data*)dictHandle;
if (self!=0){
libdeflate_free_compressor(self->c);
free(self);
}
}
static hpatch_byte* _ldef_getResetDictBuffer(hsync_dictCompressHandle dictHandle,size_t blockIndex,
size_t* out_dictSize){
_TDictCompressPlugin_ldef_data* self=(_TDictCompressPlugin_ldef_data*)dictHandle;
assert(!self->dict_isInReset);
self->dict_isInReset=hpatch_TRUE;
return _CacheBlockDict_getResetDictBuffer(&self->cache,blockIndex,out_dictSize);
}
static size_t _ldef_dictCompress(hsync_dictCompressHandle dictHandle,size_t blockIndex,
hpatch_byte* out_code,hpatch_byte* out_codeEnd,
const hpatch_byte* in_dataBegin,size_t in_dataSize,size_t in_borderSize){
_TDictCompressPlugin_ldef_data* self=(_TDictCompressPlugin_ldef_data*)dictHandle;
const hpatch_BOOL in_isEnd=(blockIndex+1==self->cache.blockCount);
size_t result;
hpatch_byte* dict;
size_t dictSize;
hpatch_BOOL isHaveDict;
const hpatch_BOOL isSetFinalTag=in_isEnd||(!self->is_gzip);
{ //reset dict?
_CacheBlockDict_usedDict(&self->cache,blockIndex,&dict,&dictSize);
isHaveDict=(dictSize>0);
if (isHaveDict){
_checkCompress(in_dataSize+in_borderSize<=(size_t)(self->tempDecBufEnd-dict-dictSize));
memcpy(dict+dictSize,in_dataBegin,in_dataSize+in_borderSize);
}
if (self->dict_isInReset){
self->dict_isInReset=hpatch_FALSE;
libdeflate_deflate_compress_block_reset(self->c);
}
}
//result=libdeflate_deflate_compress_block(self->c, isHaveDict?dict:in_dataBegin,
// dictSize,in_dataSize,isSetFinalTag, out_code,out_codeEnd-out_code,1);
result=libdeflate_deflate_compress_block_continue(self->c, isHaveDict?dict:in_dataBegin,
dictSize,in_dataSize,in_borderSize,isSetFinalTag, out_code,out_codeEnd-out_code,1);
_checkCompress(result>0);
if (!in_isEnd)
_CacheBlockDict_dictUncompress(&self->cache,blockIndex,blockIndex+1,
in_dataBegin,in_dataBegin+in_dataSize);
if ((result>=in_dataSize)&&(!self->is_gzip))
return kDictCompressCancel;// cancel compress
return result;
}
_def_fun_compressType(_ldef_dictCompressType,"zlibD");
_def_fun_compressType(_ldef_dictCompressTypeForDisplay,"ldefD (zlibD compatible)");
static const TDictCompressPlugin_ldef ldefDictCompressPlugin={
{_ldef_dictCompressType,_default_maxCompressedSize,
_ldef_limitDictSizeByData,_default_getBestWorkBlockCount,
_ldef_getDictSize,_ldef_dictCompressOpen,_ldef_dictCompressClose,0,
_ldef_getResetDictBuffer,_ldef_dictCompress,
_ldef_dictCompressTypeForDisplay,_ldef_getDictCompressBorder},
12,MAX_WBITS,hpatch_FALSE};
_def_fun_compressType(_lgzip_dictCompressType,k_gzip_dictCompressType);
_def_fun_compressType(_lgzip_dictCompressTypeForDisplay,"lgzipD (gzipD compatible)");
typedef TDictCompressPlugin_ldef TDictCompressPlugin_lgzip;
static const TDictCompressPlugin_lgzip lgzipDictCompressPlugin={
{_lgzip_dictCompressType,_default_maxCompressedSize,
_ldef_limitDictSizeByData,_default_getBestWorkBlockCount,
_ldef_getDictSize,_ldef_dictCompressOpen,_ldef_dictCompressClose,0,
_ldef_getResetDictBuffer,_ldef_dictCompress,
_lgzip_dictCompressTypeForDisplay,_ldef_getDictCompressBorder},
12,MAX_WBITS,hpatch_TRUE};
#endif//_CompressPlugin_ldef
#ifdef _CompressPlugin_zstd
#if (_IsNeedIncludeDefaultCompressHead)
#define ZSTD_STATIC_LINKING_ONLY
# include "zstd.h" // "zstd/lib/zstd.h" https://github.com/sisong/zstd
#endif
struct TDictCompressPlugin_zstd{
hsync_TDictCompress base;
hpatch_byte compress_level; //10..22
hpatch_byte dict_bits; // 15..30
};
typedef struct{
ZSTD_CCtx* s;
ZSTD_CDict* d;
_CacheBlockDict_t cache;
hpatch_BOOL isDeltaDict;
hpatch_BOOL dict_isInReset;
hpatch_byte compress_level;
} _TDictCompressPlugin_zstd_data;
static hpatch_BOOL __zstd_isCanDeltaDict(size_t dictSize,size_t kBlockCount,size_t kBlockSize,size_t* out_deltaSize){
const size_t __zstd_kMinCacheBlock=2;
const size_t backDictSize=dictSize;
if (kBlockCount<=1)
return hpatch_FALSE; //not need cache
size_t _deltaSize=dictSize/_kMinCacheBlockDictSize_r;
size_t deltaCount=_deltaSize/kBlockSize;
if (deltaCount>kBlockCount)
deltaCount=kBlockCount;
*out_deltaSize=deltaCount*kBlockSize;
return deltaCount>=__zstd_kMinCacheBlock;
}
static size_t _zstd_getDictSize(struct hsync_TDictCompress* compressPlugin){
TDictCompressPlugin_zstd* plugin=(TDictCompressPlugin_zstd*)compressPlugin;
const size_t dictSize=((size_t)1<<plugin->dict_bits);
return dictSize;
}
static size_t _zstd_limitDictSizeByData(struct hsync_TDictCompress* compressPlugin,size_t blockCount,size_t blockSize){
TDictCompressPlugin_zstd* plugin=(TDictCompressPlugin_zstd*)compressPlugin;
const hpatch_StreamPos_t dataSize=((hpatch_StreamPos_t)blockCount)*blockSize;
size_t dictBits=_getDictBitsByData(plugin->dict_bits,10,dataSize);
plugin->dict_bits=(hpatch_byte)dictBits;
return ((size_t)1)<<dictBits;
}
static size_t _zstd_getBestWorkBlockCount(hsync_TDictCompress* compressPlugin,size_t blockCount,
size_t blockSize,size_t workBlockCount){
#define _kMinWorkBlockDictSize_r _kMinCacheBlockDictSize_r // dictSize/_kMinWorkBlockDictSize_r
TDictCompressPlugin_zstd* plugin=(TDictCompressPlugin_zstd*)compressPlugin;
const size_t dictSize=((size_t)1)<<plugin->dict_bits;
const size_t minWorkBlockCount=(dictSize/_kMinWorkBlockDictSize_r+blockSize-1)/blockSize;
workBlockCount=(workBlockCount>=minWorkBlockCount)?workBlockCount:minWorkBlockCount;
size_t deltaSize;
const hpatch_BOOL isDeltaDict=__zstd_isCanDeltaDict(dictSize,blockCount,blockSize,&deltaSize);
if (isDeltaDict){
const size_t deltaCount=deltaSize/blockSize;
workBlockCount=(workBlockCount+deltaCount-1)/deltaCount*deltaCount;
}
return workBlockCount;
}
static hpatch_inline void __zstd_clear_cd(_TDictCompressPlugin_zstd_data* self){
ZSTD_CDict* d=self->d;
self->d=0;
if (d!=0){
size_t ret=ZSTD_freeCDict(d);
assert(ret==0);
}
}
static void _zstd_dictCompressClose(struct hsync_TDictCompress* compressPlugin,
hsync_dictCompressHandle dictHandle){
_TDictCompressPlugin_zstd_data* self=(_TDictCompressPlugin_zstd_data*)dictHandle;
if (self!=0){
ZSTD_CCtx* s=self->s;
__zstd_clear_cd(self);
self->s=0;
if (s!=0){
#if (IS_REUSE_compress_handle)
size_t ret=ZSTD_CCtx_reset(s,ZSTD_reset_parameters);
assert(ret==0);
#else
size_t ret=ZSTD_freeCCtx(s);
assert(ret==0);
#endif
}
free(self);
}
}
#define _zstd_checkOpen(v) do { if (ZSTD_isError(v)) goto _on_error; } while(0)
static hsync_dictCompressHandle _zstd_dictCompressOpen(struct hsync_TDictCompress* compressPlugin,size_t blockCount,size_t blockSize){
const TDictCompressPlugin_zstd* plugin=(const TDictCompressPlugin_zstd*)compressPlugin;
const size_t dictSize=((size_t)1<<plugin->dict_bits);
const size_t cacheDictSize=_getCacheBlockDictSize(dictSize,blockCount,blockSize);
size_t deltaSize;
const hpatch_BOOL isDeltaDict=__zstd_isCanDeltaDict(dictSize,blockCount,blockSize,&deltaSize);
_TDictCompressPlugin_zstd_data* self=(_TDictCompressPlugin_zstd_data*)
malloc(sizeof(_TDictCompressPlugin_zstd_data)+(isDeltaDict?dictSize:cacheDictSize));
if (self==0) return 0;
memset(self,0,sizeof(*self));
self->isDeltaDict=isDeltaDict;
self->compress_level=plugin->compress_level;
_CacheBlockDict_init(&self->cache,((hpatch_byte*)self)+sizeof(*self),
(isDeltaDict?dictSize:cacheDictSize),(isDeltaDict?dictSize-deltaSize:dictSize),
blockCount,blockSize);
#if (IS_REUSE_compress_handle)
static ZSTD_CCtx* _s=0;
if (_s==0) _s=ZSTD_createCCtx();
self->s=_s;
#else
self->s=ZSTD_createCCtx();
#endif
if (self->s==0) goto _on_error;
_zstd_checkOpen(ZSTD_CCtx_setParameter(self->s,ZSTD_c_compressionLevel,plugin->compress_level));
_zstd_checkOpen(ZSTD_CCtx_setParameter(self->s,ZSTD_c_windowLog,(int)_getDictBitsByData(plugin->dict_bits,10,blockSize)));
_zstd_checkOpen(ZSTD_CCtx_setParameter(self->s,ZSTD_c_format,ZSTD_f_zstd1_magicless));
return self;
_on_error:
_zstd_dictCompressClose(compressPlugin,self);
return 0; //error
}
static hpatch_byte* _zstd_getResetDictBuffer(hsync_dictCompressHandle dictHandle,size_t blockIndex,
size_t* out_dictSize){
_TDictCompressPlugin_zstd_data* self=(_TDictCompressPlugin_zstd_data*)dictHandle;
assert(!self->dict_isInReset);
self->dict_isInReset=hpatch_TRUE;
return _CacheBlockDict_getResetDictBuffer(&self->cache,blockIndex,out_dictSize);
}
#define _zstd_checkComp(v) _checkCompress(!ZSTD_isError(v))
static void __zstd_reCreateCDict(_TDictCompressPlugin_zstd_data* self){
const size_t kMaxDictSize=self->cache.uncompressEnd-self->cache.uncompress;
const size_t dataSize=self->cache.uncompressCur-self->cache.uncompress;
assert(self->isDeltaDict);
__zstd_clear_cd(self);
self->d=ZSTD_createCDict_delta(self->cache.uncompress,kMaxDictSize,
kMaxDictSize-dataSize,self->compress_level,0);
}
static size_t _zstd_dictCompress(hsync_dictCompressHandle dictHandle,size_t blockIndex,
hpatch_byte* out_code,hpatch_byte* out_codeEnd,
const hpatch_byte* in_dataBegin,size_t in_dataSize,size_t in_borderSize){
_TDictCompressPlugin_zstd_data* self=(_TDictCompressPlugin_zstd_data*)dictHandle;
ZSTD_CCtx* s=self->s;
ZSTD_inBuffer s_input;
ZSTD_outBuffer s_output;
const hpatch_BOOL in_isEnd=blockIndex+1==self->cache.blockCount;
if (_CacheBlockDict_isHaveDict(&self->cache)){ //reset dict
_zstd_checkComp(ZSTD_CCtx_reset(s,ZSTD_reset_session_only));
if (self->isDeltaDict){
if (self->dict_isInReset)
__zstd_reCreateCDict(self);
_zstd_checkComp(ZSTD_CCtx_refCDict(s,self->d));
}else{
hpatch_byte* dict;
size_t dictSize;
_CacheBlockDict_usedDict(&self->cache,blockIndex,&dict,&dictSize);
_zstd_checkComp(ZSTD_CCtx_refPrefix(s,dict,dictSize));
}
self->dict_isInReset=hpatch_FALSE;
}
s_input.src=in_dataBegin;
s_input.size=in_dataSize;
s_input.pos=0;
s_output.dst=out_code;
s_output.size=out_codeEnd-out_code;
s_output.pos=0;
_zstd_checkComp(ZSTD_compressStream2(s,&s_output,&s_input,in_isEnd?ZSTD_e_end:ZSTD_e_flush));
assert(s_input.pos==s_input.size);
if (!in_isEnd){//update cache & dict
const hpatch_byte* lastUpdated=self->cache.uncompressCur;
_CacheBlockDict_dictUncompress(&self->cache,blockIndex,blockIndex+1,in_dataBegin,in_dataBegin+in_dataSize);
if (self->isDeltaDict){
if ((self->d!=0)&&(lastUpdated+in_dataSize==self->cache.uncompressCur)){
_zstd_checkComp(ZSTD_updateCDict_delta(self->d,in_dataSize));
}else{
__zstd_reCreateCDict(self);
_checkCompress(self->d!=0);
}
}
}
if (s_output.pos>=s_input.size)
return kDictCompressCancel; // cancel compress
return s_output.pos;
}
_def_fun_compressType(_zstd_dictCompressType,"zstdD");
static const TDictCompressPlugin_zstd zstdDictCompressPlugin={
{_zstd_dictCompressType,_default_maxCompressedSize,
_zstd_limitDictSizeByData,_zstd_getBestWorkBlockCount,
_zstd_getDictSize,_zstd_dictCompressOpen,_zstd_dictCompressClose,0,
_zstd_getResetDictBuffer,_zstd_dictCompress},
21,24};
#endif//_CompressPlugin_zstd
#ifdef __cplusplus
}
#endif
#endif