-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathszlib.h
2938 lines (2608 loc) · 86.9 KB
/
szlib.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
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef INC_SZLIB_H_
#define INC_SZLIB_H_
/*
# License
This software is distributed under two licenses, choose whichever you like.
## MIT License
Copyright (c) 2024 Takuro Sakai
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 or substantial portions 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.
## Public Domain
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
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 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.
For more information, please refer to <http://unlicense.org>
*/
/**
@author t-sakai
@date 2018/01/07 create
@date 2018/08/04 add createInflate
@date 2018/08/17 add deflate (no compression and static haffuman)
USAGE:
Put '#define SZLIB_IMPLEMENTATION' before including this file to create the implementation.
--- CPP sample
using namespace szlib;
sz_s32 srcSize; //source data size
sz_u8 src[srcSize]; //source data
sz_s32 ret;
szContext context;
ret = initInflate(&context, srcSize, src);
if(SZ_OK != ret){
return -1;
}
const sz_s32 Chunk = 16384;
sz_u8 out[Chunk];
sz_s32 outCount = 0;
size_t total = 0;
std::vector<sz_u8> dst;
for(;;){
context.availOut_ = Chunk;
context.nextOut_ = out;
ret = inflate(&context);
switch(ret)
{
case SZ_ERROR_MEMORY:
case SZ_ERROR_FORMAT:
break;
default:
total = outCount+context.thisTimeOut_;
if(dst.capacity()<total){
size_t capacity = dst.capacity();
while(capacity<total){
capacity += 1024;
}
dst.reserve(capacity);
}
dst.resize(total);
memcpy(&dst[0]+outCount, out, context.thisTimeOut_);
outCount += context.thisTimeOut_;
if(SZ_END!=ret){
continue;
}
break;
};
break;
}
termInflate(&context);
return ret == SZ_END? outCount : -1;
---
*/
/*
--- note
-------------------------------
zlib: https://tools.ietf.org/html/rfc1950
deflate: https://tools.ietf.org/html/rfc1951
Fixed Huffman Codes
Literal Bits Codes
------- ---- -----
0 - 143 8 00110000 - 10111111
144 - 255 9 110010000 - 111111111
256 - 279 7 0000000 - 0010111
280 - 287 8 11000000 - 11000111
Distance Bits Codes
-------- ---- -----
0-31 5 00000 - 11111
*/
#ifdef __cplusplus
#include <cstdint>
#include <cassert>
#include <cstddef>
#else
#include <stdint.h>
#include <assert.h>
#include <stddef.h>
#include <stdbool.h>
#endif
#ifdef __cplusplus
namespace szlib
{
#endif
//C++98 199711L
//C++11 201103L
#ifdef __cplusplus
# if 201103L<=__cplusplus || 1900<=_MSC_VER
# define SZ_CPP11 1
# endif
#endif
#ifdef __cplusplus
# ifdef SZ_CPP11
# define SZ_NULL nullptr
# else
# define SZ_NULL 0
# endif
#else
# define SZ_NULL ((void*)0)
#endif
typedef int8_t sz_s8;
typedef int16_t sz_s16;
typedef int32_t sz_s32;
typedef int64_t sz_s64;
typedef uint8_t sz_u8;
typedef uint16_t sz_u16;
typedef uint32_t sz_u32;
typedef uint64_t sz_u64;
typedef float sz_f32;
typedef double sz_f64;
typedef size_t sz_size_t;
typedef bool sz_bool;
//#define SZ_TRACE (1)
#ifdef _NDEBUG
#define SZ_ASSERT(exp)
#else
#define SZ_ASSERT(exp) assert(exp)
#endif
#ifdef __cplusplus
#define SZ_STATIC
static const sz_bool SZ_TRUE = true;
static const sz_bool SZ_FALSE = false;
static const sz_s32 SZ_CONTEXT_INFLATE = 0;
static const sz_s32 SZ_CONTEXT_DEFLATE = 1;
static const sz_u8 SZ_Z_COMPRESSION_TYPE = 8;
static const sz_u8 SZ_LZ77_WINDOWSIZE_MINUS_8 = 7;
static const sz_u8 SZ_Z_COMPRESSION_LEVEL_FARSTEST = 0;
static const sz_u8 SZ_Z_COMPRESSION_LEVEL_FARST = 1;
static const sz_u8 SZ_Z_COMPRESSION_LEVEL_DEFUALT = 2;
static const sz_u8 SZ_Z_COMPRESSION_LEVEL_SLOWEST = 3;
static const sz_s32 SZ_BLOCK_HEADER_SIZE = 3;
static const sz_u32 SZ_FLAG_LASTBLOCK = 0x01U;
static const sz_u32 SZ_FLAG_BLOCK_TYPE_MASK = 3;
static const sz_s32 SZ_BLOCK_TYPE_NOCOMPRESSION = 0;
static const sz_s32 SZ_BLOCK_TYPE_FIXED_HUFFMAN = 1;
static const sz_s32 SZ_BLOCK_TYPE_DYNAMIC_HUFFMAN = 2;
static const sz_s16 SZ_HUFFMAN_ENDCODE = 256;
static const sz_s32 SZ_LENGTH_CODES = 29;
static const sz_s32 SZ_DISTANCE_CODES = 30;
static const sz_s32 SZ_MAX_LENGTH = 258;
static const sz_s32 SZ_MAX_DISTANCE = 32767;
static const sz_s32 SZ_MAX_WINDOW_SIZE = SZ_MAX_DISTANCE + SZ_MAX_LENGTH;
static const sz_s32 SZ_MIN_INFLATE_OUTBUFF_SIZE = 258;
static const sz_s32 SZ_HCLENS = 15;
static const sz_s32 SZ_HCLEN_CODES = SZ_HCLENS+4;
static const sz_s32 SZ_HLENS = 286;
static const sz_s32 SZ_HDISTS = 30;
static const sz_s32 SZ_SYMBOL_LENGTH_SIZE = 19;
static const sz_s32 SZ_MAX_BITS_LITERAL_CODE = 15;
static const sz_s32 SZ_MAX_BITS_DISTANCE_CODE = 15;
static const sz_s16 SZ_TREE_LEFT = 0x01U;
static const sz_s16 SZ_TREE_RIGHT = 0x02U;
static const sz_s32 SZ_TREESIZE_LITERAL = 1<<(SZ_MAX_BITS_LITERAL_CODE+2);
static const sz_s32 SZ_TREESIZE_DISTANCE = 1<<(SZ_MAX_BITS_DISTANCE_CODE+2);
static const sz_s32 SZ_MINIMUM_OUT_BUFFER_SIZE = 16;
static const sz_s32 SZ_MAX_BLOCK_SIZE = 0xFFFF;
static const sz_s32 SZ_MAX_MATCH_LENGTH = 258;
static const sz_s32 SZ_MAX_CHAIN_SIZE = 16384;
static const sz_u32 SZ_CHAIN_MASK = SZ_MAX_CHAIN_SIZE-1;
static const sz_u16 SZ_CHAIN_EMPTY16 = 0xFFFFU;
static const sz_s32 SZ_MAX_LITERAL_BUFFER_SIZE = 4096-1;
static const sz_s32 SZ_HASH_LENGTH = 3;
static const sz_s32 SZ_LENGTH_CODE_BITS = 9;
static const sz_s32 SZ_LENGTH_MAX_EXTRA_BITS = 5;
static const sz_s32 SZ_DISTANCE_BITS = 5;
static const sz_s32 SZ_DISTANCE_MAX_EXTRA_BITS = 13;
static const sz_s32 SZ_MIN_DEFLATE_OUTBUFF_SIZE = 16;
static const sz_s32 SZ_REVERSE_PACKAGE_MERGE_BUFFER_SIZE = 3874;
static const sz_s32 SZ_MAX_SYMBOL_REPEAT = 138;
#define STATIC_CAST(TYPE, VALUE) static_cast<TYPE>(VALUE)
#define REINTERPRET_CAST(TYPE, VALUE) reinterpret_cast<TYPE>(VALUE)
#ifndef SZ_MALLOC
#define SZ_MALLOC(size) std::malloc(size)
#endif
#ifndef SZ_FREE
#define SZ_FREE(ptr) std::free(ptr)
#endif
#define SZ_EXTERN
#define SZ_PREFIX(name) name
#define SZ_STRUCT_BEGIN(name) struct name
#define SZ_STRUCT_END(name) ;
#define SZ_ENUM_BEGIN(name) enum name
#define SZ_ENUM_END(name) ;
template<class T>
inline void swap(T& x0, T& x1)
{
T tmp = x0;
x0 = x1;
x1 = tmp;
}
#else
#define SZ_STATIC static
#define SZ_TRUE true
#define SZ_FALSE false
#define SZ_CONTEXT_INFLATE (0)
#define SZ_CONTEXT_DEFLATE (1)
#define SZ_Z_COMPRESSION_TYPE (8)
#define SZ_LZ77_WINDOWSIZE_MINUS_8 (7)
#define SZ_Z_COMPRESSION_LEVEL_FARSTEST (0)
#define SZ_Z_COMPRESSION_LEVEL_FARST (1)
#define SZ_Z_COMPRESSION_LEVEL_DEFUALT (2)
#define SZ_Z_COMPRESSION_LEVEL_SLOWEST (3)
#define SZ_BLOCK_HEADER_SIZE (3)
#define SZ_FLAG_LASTBLOCK (0x01U)
#define SZ_FLAG_BLOCK_TYPE_MASK (3)
#define SZ_BLOCK_TYPE_NOCOMPRESSION (0)
#define SZ_BLOCK_TYPE_FIXED_HUFFMAN (1)
#define SZ_BLOCK_TYPE_DYNAMIC_HUFFMAN (2)
#define SZ_HUFFMAN_ENDCODE (256)
#define SZ_LENGTH_CODES (29)
#define SZ_DISTANCE_CODES (30)
#define SZ_MAX_LENGTH (258)
#define SZ_MAX_DISTANCE 32767
#define SZ_MAX_WINDOW_SIZE (SZ_MAX_DISTANCE+SZ_MAX_LENGTH)
#define SZ_MIN_INFLATE_OUTBUFF_SIZE (258)
#define SZ_HCLENS (15)
#define SZ_HCLEN_CODES (19)
#define SZ_HLENS (286)
#define SZ_HDISTS (30)
#define SZ_SYMBOL_LENGTH_SIZE(19)
#define SZ_MAX_BITS_LITERAL_CODE (15)
#define SZ_MAX_BITS_DISTANCE_CODE (15)
#define SZ_TREE_LEFT (0x01U)
#define SZ_TREE_RIGHT (0x02U)
#define SZ_TREESIZE_LITERAL (1<<(SZ_MAX_BITS_LITERAL_CODE+2))
#define SZ_TREESIZE_DISTANCE (1<<(SZ_MAX_BITS_DISTANCE_CODE+2))
#define SZ_MINIMUM_OUT_BUFFER_SIZE (16)
#define SZ_MAX_BLOCK_SIZE (0xFFFF)
#define SZ_MAX_MATCH_LENGTH (258)
#define SZ_MAX_CHAIN_SIZE (16384)
#define SZ_CHAIN_MASK (SZ_MAX_CHAIN_SIZE-1)
#define SZ_CHAIN_EMPTY16 (0xFFFFU)
#define SZ_MAX_LITERAL_BUFFER_SIZE (4096-1)
#define SZ_HASH_LENGTH (3)
#define SZ_MIN_DEFLATE_OUTBUFF_SIZE (16)
#define STATIC_CAST(TYPE, VALUE) (TYPE)(VALUE)
#define REINTERPRET_CAST(TYPE, VALUE) (TYPE)(VALUE)
#ifndef SZ_MALLOC
#define SZ_MALLOC(size) malloc(size)
#endif
#ifndef SZ_FREE
#define SZ_FREE(ptr) free(ptr)
#endif
#define SZ_EXTERN extern
#define SZ_PREFIX(name) sz_ ## name
#define SZ_STRUCT_BEGIN(name) typedef struct name ## _t
#define SZ_STRUCT_END(name) name;
#define SZ_ENUM_BEGIN(name) typedef enum name ## _t
#define SZ_ENUM_END(name) name;
#endif
typedef void*(*FUNC_MALLOC)(sz_size_t size, void* user);
typedef void(*FUNC_FREE)(void* ptr, void* user);
/**
Result status of processing
*/
SZ_ENUM_BEGIN(SZ_Status)
{
SZ_OK = 0,
SZ_END = 1,
SZ_PENDING = 2,
SZ_ERROR_MEMORY = -1,
SZ_ERROR_FORMAT = -2,
}
SZ_ENUM_END(SZ_Status)
/**
Internal states
*/
SZ_ENUM_BEGIN(SZ_State)
{
SZ_State_Init =0,
SZ_State_Block,
SZ_State_NoComp,
SZ_State_LZSS,
SZ_State_Fixed,
SZ_State_Dynamic,
SZ_State_Dynamic_Size,
SZ_State_Dynamic_Lengths,
SZ_State_End,
}
SZ_ENUM_END(SZ_State)
/**
Internal states
*/
SZ_ENUM_BEGIN(SZ_Level)
{
SZ_Level_NoCompression =0,
SZ_Level_Fixed,
SZ_Level_Dynamic,
}
SZ_ENUM_END(SZ_Level)
SZ_STRUCT_BEGIN(szZHeader)
{
sz_u8 compressionMethodInfo_; ///< allowed with only 8
sz_u8 flags_;
sz_u32 presetDictionary_; ///< id for determining preset dictionary
sz_u32 adler_; ///< adler32 check sum
}
SZ_STRUCT_END(szZHeader)
SZ_STRUCT_BEGIN(szBitStream)
{
sz_s32 bit_;
sz_s32 current_;
sz_s32 size_;
const sz_u8* src_;
}
SZ_STRUCT_END(szBitStream)
SZ_STRUCT_BEGIN(szWriteStream)
{
sz_s16 bit_;
sz_s16 pendingBitsLE_;
sz_u16 pendingLE_;
}
SZ_STRUCT_END(szWriteStream)
SZ_STRUCT_BEGIN(szCode)
{
sz_s16 literal_;
sz_s16 length_;
sz_s32 distance_;
}
SZ_STRUCT_END(szCode)
SZ_STRUCT_BEGIN(szLengthCode)
{
sz_s16 length_;
sz_s16 code_;
}
SZ_STRUCT_END(szLengthCode)
SZ_STRUCT_BEGIN(szCodeTree)
{
sz_s16 children_;
sz_s16 literal_;
}
SZ_STRUCT_END(szCodeTree)
struct szContextInflate;
struct szContextDeflate;
/**
A context for inflating
*/
SZ_STRUCT_BEGIN(szContext)
{
sz_s32 status_;
sz_s32 totalOut_;
sz_s32 thisTimeOut_;
sz_s32 availOut_;
sz_u8* nextOut_;
void* internal_;
}
SZ_STRUCT_END(szContext)
union Hash
{
sz_u32 value_;
sz_u8 bytes_[4];
};
SZ_STRUCT_BEGIN(szLZSSHEntry)
{
sz_u16 start_;
sz_u16 history_;
sz_u16 prev_;
sz_u16 next_;
Hash hash_;
sz_s32 position_;
}
SZ_STRUCT_END(szLZSSHEntry)
SZ_STRUCT_BEGIN(szLZSSHistory)
{
sz_u16 empty_;
sz_u16 count_;
szLZSSHEntry entries_[SZ_MAX_CHAIN_SIZE];
}
SZ_STRUCT_END(szLZSSHistory)
SZ_STRUCT_BEGIN(szLZSSLiteral)
{
sz_u32 literal_;
}
SZ_STRUCT_END(szLZSSLiteral)
SZ_STRUCT_BEGIN(szFreqCode)
{
sz_u32 frequency_;
sz_u16 code_;
sz_u16 huffCode_;
}
SZ_STRUCT_END(szFreqCode)
inline sz_u16 getLengthCode(szLZSSLiteral literal)
{
return literal.literal_ & ((0x01U<<SZ_LENGTH_CODE_BITS)-1);
}
inline szLZSSLiteral setLengthCode(szLZSSLiteral literal, sz_u16 code)
{
literal.literal_ |= code;
return literal;
}
inline sz_u16 getLengthExtra(szLZSSLiteral literal)
{
return (literal.literal_>>SZ_LENGTH_CODE_BITS) & ((0x01U<<SZ_LENGTH_MAX_EXTRA_BITS)-1);
}
inline szLZSSLiteral setLengthExtra(szLZSSLiteral literal, sz_u16 extra)
{
literal.literal_ |= extra << SZ_LENGTH_CODE_BITS;
return literal;
}
inline sz_u16 getDistanceCode(szLZSSLiteral literal)
{
return (literal.literal_>>(SZ_LENGTH_CODE_BITS+SZ_LENGTH_MAX_EXTRA_BITS)) & ((0x01U<<SZ_DISTANCE_BITS)-1);
}
inline szLZSSLiteral setDistanceCode(szLZSSLiteral literal, sz_u16 code)
{
literal.literal_ |= code << (SZ_LENGTH_CODE_BITS+SZ_LENGTH_MAX_EXTRA_BITS);
return literal;
}
inline sz_u16 getDistanceExtra(szLZSSLiteral literal)
{
return literal.literal_>>(SZ_LENGTH_CODE_BITS+SZ_LENGTH_MAX_EXTRA_BITS+SZ_DISTANCE_BITS) & ((0x01U<<SZ_DISTANCE_MAX_EXTRA_BITS)-1);
}
inline szLZSSLiteral setDistanceExtra(szLZSSLiteral literal, sz_u16 extra)
{
literal.literal_ |= extra << (SZ_LENGTH_CODE_BITS+SZ_LENGTH_MAX_EXTRA_BITS+SZ_DISTANCE_BITS);
return literal;
}
void heapsort(sz_s32 n, szFreqCode* v);
void takePackage(sz_s32 i, sz_s32 size, sz_u16* length, sz_u32* type[], sz_s32* currentPosition);
void getLengths(sz_s32 size, sz_u16* lengths, szFreqCode* frequencies, sz_s32 limit, sz_u32* valueBuffer, sz_u32* typeBuffer);
void calcHuffCodes(sz_s32 size, szFreqCode* codes, const sz_u16* lengths);
//--- Inflate
//--------------------------------------------------------------------------------------------------------------
/**
@brief Initialize context.
@param context ...
@param size ... size of input data "src"
@param src ... source
@param pMalloc ... user's malloc
@param pFree ... user's free
@param user ... user data for malloc/free functions
@warn Both pMalloc and pFree should be provided together.
*/
#ifdef __cplusplus
SZ_Status SZ_PREFIX(initInflate) (szContext* context, sz_s32 size, const sz_u8* src, FUNC_MALLOC pMalloc=SZ_NULL, FUNC_FREE pFree=SZ_NULL, void* user=SZ_NULL);
#else
SZ_EXTERN SZ_Status SZ_PREFIX(initInflate) (szContext* context, sz_s32 size, const sz_u8* src, FUNC_MALLOC pMalloc, FUNC_FREE pFree, void* user);
#endif
/**
@brief Create context only without initializing. The function `resetInflate' should be called.
@param context ...
@param pMalloc ... user's malloc
@param pFree ... user's free
@param user ... user data for malloc/free functions
@warn Both pMalloc and pFree should be provided together.
*/
#ifdef __cplusplus
SZ_Status SZ_PREFIX(createInflate) (szContext* context, FUNC_MALLOC pMalloc=SZ_NULL, FUNC_FREE pFree=SZ_NULL, void* user=SZ_NULL);
#else
SZ_EXTERN SZ_Status SZ_PREFIX(createInflate) (szContext* context, FUNC_MALLOC pMalloc, FUNC_FREE pFree, void* user);
#endif
/**
@brief Terminate context with deallocating used resources.
@param context ...
*/
SZ_EXTERN void SZ_PREFIX(termInflate) (szContext* context);
/**
@brief Reset internal states of context.
*/
SZ_EXTERN void SZ_PREFIX(resetInflate) (szContext* context, sz_s32 size, const sz_u8* src);
/**
@brief Process inflating.
@param context ...
@warning Size of output buffer "nextOut_", that is a number "availOut", needs above SZ_MIN_INFLATE_OUTBUFF_SIZE(258) in bytes.
*/
SZ_EXTERN SZ_Status SZ_PREFIX(inflate) (szContext* context);
//--- Deflate
//--------------------------------------------------------------------------------------------------------------
/**
@brief Initialize context.
@param context ...
@param size ... size of input data "src"
@param src ... source
@param pMalloc ... user's malloc
@param pFree ... user's free
@param user ... user data for malloc/free functions
@warn Both pMalloc and pFree should be provided together.
*/
#ifdef __cplusplus
SZ_Status SZ_PREFIX(initDeflate) (szContext* context, sz_s32 size, const sz_u8* src, FUNC_MALLOC pMalloc=SZ_NULL, FUNC_FREE pFree=SZ_NULL, void* user=SZ_NULL, SZ_Level level = SZ_Level_Fixed);
#else
SZ_EXTERN SZ_Status SZ_PREFIX(initDeflate) (szContext* context, sz_s32 size, const sz_u8* src, FUNC_MALLOC pMalloc, FUNC_FREE pFree, void* user, SZ_Level level);
#endif
/**
@brief Create context only without initializing. The function `resetDeflate' should be called.
@param context ...
@param pMalloc ... user's malloc
@param pFree ... user's free
@param user ... user data for malloc/free functions
@warn Both pMalloc and pFree should be provided together.
*/
#ifdef __cplusplus
SZ_Status SZ_PREFIX(createDeflate) (szContext* context, FUNC_MALLOC pMalloc=SZ_NULL, FUNC_FREE pFree=SZ_NULL, void* user=SZ_NULL);
#else
SZ_EXTERN SZ_Status SZ_PREFIX(createInflate) (szContext* context, FUNC_MALLOC pMalloc, FUNC_FREE pFree, void* user);
#endif
/**
@brief Terminate context with deallocating used resources.
@param context ...
*/
SZ_EXTERN void SZ_PREFIX(termDeflate) (szContext* context);
/**
@brief Reset internal states of context.
*/
#ifdef __cplusplus
void SZ_PREFIX(resetDeflate) (szContext* context, sz_s32 size, const sz_u8* src, SZ_Level level = SZ_Level_Fixed);
#else
SZ_EXTERN void SZ_PREFIX(resetDeflate) (szContext* context, sz_s32 size, const sz_u8* src, SZ_Level level);
#endif
/**
@brief Process deflating.
@param context ...
@warning Size of output buffer "nextOut_", that is a number "availOut", needs above SZ_MIN_INFLATE_OUTBUFF_SIZE(258) in bytes.
*/
SZ_EXTERN SZ_Status SZ_PREFIX(deflate) (szContext* context);
#ifdef __cplusplus
}
#endif
#endif //INC_SZLIB_H_
//--------------------------------------------------------------------------------------------------------------
//---
//--- Implementation
//---
//--------------------------------------------------------------------------------------------------------------
#ifdef SZLIB_IMPLEMENTATION
#ifdef __cplusplus
#include <cstdlib>
#include <cstring>
#else
#include <stdlib.h>
#include <string.h>
#endif
#include <stdio.h>
#ifdef _MSC_VER
#include <intrin.h>
#endif
#ifdef __cplusplus
namespace szlib
{
#endif
SZ_STRUCT_BEGIN(szContextInflate)
{
sz_s32 type_;
FUNC_MALLOC malloc_;
FUNC_FREE free_;
void* user_;
SZ_State state_;
szZHeader zheader_;
szBitStream bitStream_;
sz_s16 lastBlockHeader_;
sz_s32 lastRequestLength_;
szCode lastCode_;
sz_s32 windowPosition_;
sz_u8 buffer_[SZ_MAX_WINDOW_SIZE];
sz_u8* window_;
sz_u8* data_;
sz_s16 lenHlits_;
sz_s16 lenHdists_;
szLengthCode hlitsDists_[SZ_HLENS+SZ_HDISTS];
szLengthCode* hlits_;
szLengthCode* hdists_;
szCodeTree treeLiteral_[SZ_TREESIZE_LITERAL];
szCodeTree treeDistance_[SZ_TREESIZE_DISTANCE];
}
SZ_STRUCT_END(szContextInflate)
SZ_STRUCT_BEGIN(szContextDeflate)
{
sz_s32 type_;
FUNC_MALLOC malloc_;
FUNC_FREE free_;
void* user_;
SZ_Level level_;
SZ_State state_;
sz_s32 availIn_;
sz_s32 currentIn_;
sz_s32 sizeIn_;
const sz_u8* nextIn_;
szWriteStream stream_;
szLZSSHistory history_;
sz_s32 inLiteralSize_;
sz_s32 outLiteralSize_;
szLZSSLiteral literals_[SZ_MAX_LITERAL_BUFFER_SIZE+1];
szFreqCode freqCodes_[SZ_HLENS];
szFreqCode freqDists_[SZ_HDISTS];
szFreqCode freqCodeDists_[SZ_SYMBOL_LENGTH_SIZE];
sz_u32 valueBuffer_[SZ_REVERSE_PACKAGE_MERGE_BUFFER_SIZE];
sz_u32 typeBuffer_[SZ_REVERSE_PACKAGE_MERGE_BUFFER_SIZE];
sz_u16 symbols_[SZ_HLENS+SZ_HDISTS];
sz_u16 treeLengths_[SZ_SYMBOL_LENGTH_SIZE];
sz_u16 hlit_;
sz_u16 hdist_;
sz_u16 hclen_;
sz_u16 outSymbols_;
sz_u16 currentSymbol_;
sz_u32 adler_;
}
SZ_STRUCT_END(szContextDeflate)
void heapsort(sz_s32 n, szFreqCode* v)
{
SZ_ASSERT(0<=n);
SZ_ASSERT(SZ_NULL != v);
--v; //the first index is 1
sz_s32 i, j;
szFreqCode x;
for(sz_s32 k=n>>1; k>=1; --k){
i=k;
x = v[k];
while((j=i<<1)<=n){
if(j<n && (v[j+1].frequency_<v[j].frequency_)){
++j;
}
if(x.frequency_<=v[j].frequency_){
break;
}
v[i] = v[j];
i = j;
}
v[i] = x;
}
while(n>1){
x = v[n];
v[n] = v[1];
--n;
i = 1;
while((j=i<<1)<=n){
if(j<n && (v[j+1].frequency_<v[j].frequency_)){
++j;
}
if(x.frequency_<=v[j].frequency_){
break;
}
v[i] = v[j];
i = j;
}
v[i] = x;
}
}
void takePackage(sz_s32 i, sz_s32 size, sz_u16* length, sz_u32* type[], sz_s32* currentPosition)
{
sz_s32 x = type[i][currentPosition[i]];
if (size == x){
takePackage(i+1, size, length, type, currentPosition);
takePackage(i+1, size, length, type, currentPosition);
}
else{
--length[x];
}
++currentPosition[i];
}
void getLengths(sz_s32 size, sz_u16* lengths, szFreqCode* frequencies, sz_s32 limit, sz_u32* valueBuffer, sz_u32* typeBuffer)
{
SZ_ASSERT(0<=size && size<=0xFFFF);
SZ_ASSERT(SZ_NULL != lengths);
SZ_ASSERT(SZ_NULL != frequencies);
static const sz_s32 MAX_LIMIT = SZ_MAX_BITS_LITERAL_CODE;
sz_u32 minimumCost[MAX_LIMIT];
sz_u8 flag[MAX_LIMIT];
sz_u32* value[MAX_LIMIT];
sz_u32* type[MAX_LIMIT];
sz_s32 currentPosition[MAX_LIMIT];
heapsort(size, frequencies);
sz_s32 excess = ((1U << limit) - size);
const sz_s32 half = (1U << (limit-1));
minimumCost[limit-1] = size;
for(sz_s32 i=0; i<limit; ++i){
if(excess<half){
flag[i] = 0;
} else{
flag[i] = 1;
excess -= half;
}
excess <<= 1;
if((i+2)<=limit){
minimumCost[limit-2-i] = (minimumCost[limit-i-1] >> 1) + size;
}
}
minimumCost[0] = flag[0];
value[0] = valueBuffer;
type[0] = typeBuffer;
valueBuffer += minimumCost[0];
typeBuffer += minimumCost[0];
for(sz_s32 i=1; i<limit; ++i){
sz_u32 cost = 2*minimumCost[i-1]+flag[i];
if(cost<minimumCost[i]){
minimumCost[i] = cost;
}
value[i] = valueBuffer;
type[i] = typeBuffer;
valueBuffer += minimumCost[i];
typeBuffer += minimumCost[i];
}
for(sz_s32 i=0; i<size; ++i){
lengths[i] = STATIC_CAST(sz_u16, limit);
}
for(sz_u32 i=0; i<minimumCost[limit-1]; ++i){
value[limit-1][i] = frequencies[i].frequency_;
type[limit-1][i] = i;
}
for(sz_s32 i=0; i<limit; ++i){
currentPosition[i] = 0;
}
if(1 == flag[limit-1]){
--lengths[0];
++currentPosition[limit-1];
}
for(sz_s32 j=limit-2; 0<=j; --j){
sz_s32 i = 0;
sz_u32 weight = 0;
sz_s32 next = currentPosition[j+1];
for(sz_u32 k=0; k<minimumCost[j]; ++k){
weight = value[j+1][next] + value[j+1][next+1];
if(frequencies[i].frequency_ < weight){
value[j][k] = weight;
type[j][k] = size;
next += 2;
} else {
value[j][k] = frequencies[i].frequency_;
type[j][k] = i;
++i;
}
}
currentPosition[j] = 0;
if(1==flag[j]){
takePackage(j, size, lengths, type, currentPosition);
}
}
}
void calcHuffCodes(sz_s32 size, szFreqCode* codes, const sz_u16* lengths)
{
sz_u16 count[SZ_MAX_BITS_LITERAL_CODE+1];
sz_u16 startCode[SZ_MAX_BITS_LITERAL_CODE+1];
memset(count, 0, sizeof(sz_u16)*(SZ_MAX_BITS_LITERAL_CODE+1));
//
for(sz_s32 i=0; i<size; ++i){
count[lengths[i]] += 1;
}
//
startCode[0] = 0;
sz_u16 code = 0;
for(sz_s32 i=1; i<=SZ_MAX_BITS_LITERAL_CODE; ++i){
startCode[i] = code;
code += count[i];
code <<= 1;
}
//
for(sz_s32 i=0; i<size; ++i){
code = startCode[lengths[i]];
startCode[lengths[i]] += 1;
codes[i].huffCode_ = 0;
for(sz_s32 j=0; j<lengths[i]; ++j){
codes[i].huffCode_ = (codes[i].huffCode_<<1) | (code & 0x01U);
code >>= 1;
}
}
}
#ifdef __cplusplus
namespace
{
#endif
/**
Default malloc
*/
#ifdef __cplusplus
SZ_STATIC void* sz_malloc(sz_size_t size, void* /*user*/)
#else
SZ_STATIC void* sz_malloc(sz_size_t size, void* user)
#endif
{
return SZ_MALLOC(size);
}
/**
Default free
*/
#ifdef __cplusplus
SZ_STATIC void sz_free(void* ptr, void* /*user*/)
#else
SZ_STATIC void sz_free(void* ptr, void* user)
#endif
{
SZ_FREE(ptr);
}
/**
Extra bits for length code
*/
static const sz_s8 LengthExtraBits[SZ_LENGTH_CODES] =
{
0,0,0,0,0,0,0,0,1,1,
1,1,2,2,2,2,3,3,3,3,
4,4,4,4,5,5,5,5,0,
};
/**
Minimum length for length code
*/
static const sz_s16 LengthBase[SZ_LENGTH_CODES] =
{
3,4,5,6,7,8,9,10,11,13,
15,17,19,23,27,31,35,43,51,59,
67,83,99,115,131,163,195,227,258,
};
/**
Extra bits for distance code
*/
static const sz_s8 DistanceExtraBits[SZ_DISTANCE_CODES] =
{
0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13,
};
/**
Minimum length for distance code
*/
static const sz_s16 DistanceBase[SZ_DISTANCE_CODES] =
{
1,2,3,4,5,7,9,13,17,25,
33,49,65,97,129,193,257,385,513,769,
1025,1537,2049,3073,4097,6145,8193,12289,16385,24577,
};
/**
Order of huffman code length
*/
static const sz_s8 HCLENS_Order[SZ_HCLEN_CODES] =