-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompressor.c
4507 lines (4507 loc) · 202 KB
/
compressor.c
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
// Max Base - MaxBase.org
// https://github.com/BaseMax/ZMiniCArchiveCompressor
/*
Tested on: Linux base 5.3.0-40-generic
Compile: $ gcc compressor.c -o compressor -O3
Using: $ ./compressor c input.txt output.x
$ ./compressor d output.x input.txt
max@base:~/compress$ ./compressor d o.txt oi.txt
Mode: d, Level: 10
Input File: "o.txt"
Output File: "oi.txt"
Input file size: 2107
Total input bytes: 2107
Total output bytes: 4275
Done.
max@base:~/compress$ ./compressor c i.txt o.txt
Mode: c, Level: 10
Input File: "i.txt"
Output File: "o.txt"
Input file size: 4275
Total input bytes: 4275
Total output bytes: 2107
Done.
*/
#ifndef MINI_HEADER_INCLUDED
#define MINI_HEADER_INCLUDED
#include <stdlib.h>
#if defined(__TINYC__) && (defined(__linux) || defined(__linux__))
#define MINI_NO_TIME
#endif
#if !defined(MINI_NO_TIME) && !defined(MINI_NO_ARCHIVE_APIS)
#include <time.h>
#endif
#if defined(_M_IX86) || defined(_M_X64) || defined(__i386__) || \
defined(__i386) || defined(__i486__) || defined(__i486) || \
defined(i386) || defined(__ia64__) || defined(__x86_64__)
#define MINI_X86_OR_X64_CPU 1
#endif
#if (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) || MINI_X86_OR_X64_CPU
#define MINI_LITTLE_ENDIAN 1
#endif
#if MINI_X86_OR_X64_CPU
#define MINI_USE_UNALIGNED_LOADS_AND_STORES 1
#endif
#if defined(_M_X64) || defined(_WIN64) || defined(__MINGW64__) || \
defined(_LP64) || defined(__LP64__) || defined(__ia64__) || \
defined(__x86_64__)
#define MINI_HAS_64BIT_REGISTERS 1
#endif
#ifdef __cplusplus
extern "C" {
#endif
typedef unsigned long MINI_ulong;
void MINI_free(void *p);
#define MINI_ADLER32_INIT (1)
MINI_ulong MINI_adler32(MINI_ulong adler, const unsigned char *ptr, size_t buf_len);
#define MINI_CRC32_INIT (0)
MINI_ulong MINI_crc32(MINI_ulong crc, const unsigned char *ptr, size_t buf_len);
enum {
MINI_DEFAULT_STRATEGY = 0, MINI_FILTERED = 1, MINI_HUFFMAN_ONLY = 2, MINI_RLE = 3, MINI_FIXED = 4
};
#define MINI_DEFLATED 8
#ifndef MINI_NO_ZLIB_APIS
typedef void *(*MINI_alloc_func)(void *opaque, size_t items, size_t size);
typedef void (*MINI_free_func)(void *opaque, void *address);
typedef void *(*MINI_realloc_func)(void *opaque, void *address, size_t items, size_t size);
#define MINI_VERSION "9.1.15"
#define MINI_VERNUM 0x91F0
#define MINI_VER_MAJOR 9
#define MINI_VER_MINOR 1
#define MINI_VER_REVISION 15
#define MINI_VER_SUBREVISION 0
enum {
MINI_NO_FLUSH = 0, MINI_PARTIAL_FLUSH = 1, MINI_SYNC_FLUSH = 2, MINI_FULL_FLUSH = 3, MINI_FINISH = 4, MINI_BLOCK = 5
};
enum {
MINI_OK = 0, MINI_STREAM_END = 1, MINI_NEED_DICT = 2, MINI_ERRNO = -1, MINI_STREAM_ERROR = -2, MINI_DATA_ERROR = -3, MINI_MEM_ERROR = -4, MINI_BUF_ERROR = -5, MINI_VERSION_ERROR = -6, MINI_PARAM_ERROR = -10000
};
enum {
MINI_NO_COMPRESSION = 0, MINI_BEST_SPEED = 1, MINI_BEST_COMPRESSION = 9, MINI_UBER_COMPRESSION = 10, MINI_DEFAULT_LEVEL = 6, MINI_DEFAULT_COMPRESSION = -1
};
#define MINI_DEFAULT_WINDOW_BITS 15
struct MINI_internal_state;
typedef struct MINI_stream_s {
const unsigned char *next_in;
unsigned int avail_in;
MINI_ulong total_in;
unsigned char *next_out;
unsigned int avail_out;
MINI_ulong total_out;
char *msg;
struct MINI_internal_state *state;
MINI_alloc_func zalloc;
MINI_free_func zfree;
void *opaque;
int data_type;
MINI_ulong adler;
MINI_ulong reserved;
} MINI_stream;
typedef MINI_stream *MINI_streamp;
const char *MINI_version(void);
int MINI_deflateInit(MINI_streamp pStream, int level);
int MINI_deflateInit2(MINI_streamp pStream, int level, int method, int window_bits, int mem_level, int strategy);
int MINI_deflateReset(MINI_streamp pStream);
int MINI_deflate(MINI_streamp pStream, int flush);
int MINI_deflateEnd(MINI_streamp pStream);
MINI_ulong MINI_deflateBound(MINI_streamp pStream, MINI_ulong source_len);
int MINI_compress(unsigned char *pDest, MINI_ulong *pDest_len, const unsigned char *pSource, MINI_ulong source_len);
int MINI_compress2(unsigned char *pDest, MINI_ulong *pDest_len, const unsigned char *pSource, MINI_ulong source_len, int level);
MINI_ulong MINI_compressBound(MINI_ulong source_len);
int MINI_inflateInit(MINI_streamp pStream);
int MINI_inflateInit2(MINI_streamp pStream, int window_bits);
int MINI_inflate(MINI_streamp pStream, int flush);
int MINI_inflateEnd(MINI_streamp pStream);
int MINI_uncompress(unsigned char *pDest, MINI_ulong *pDest_len, const unsigned char *pSource, MINI_ulong source_len);
const char *MINI_error(int err);
#ifndef MINI_NO_ZLIB_COMPATIBLE_NAMES
typedef unsigned char Byte;
typedef unsigned int uInt;
typedef MINI_ulong uLong;
typedef Byte Bytef;
typedef uInt uIntf;
typedef char charf;
typedef int intf;
typedef void *voidpf;
typedef uLong uLongf;
typedef void *voidp;
typedef void *const voidpc;
#define Z_NULL 0
#define Z_NO_FLUSH MINI_NO_FLUSH
#define Z_PARTIAL_FLUSH MINI_PARTIAL_FLUSH
#define Z_SYNC_FLUSH MINI_SYNC_FLUSH
#define Z_FULL_FLUSH MINI_FULL_FLUSH
#define Z_FINISH MINI_FINISH
#define Z_BLOCK MINI_BLOCK
#define Z_OK MINI_OK
#define Z_STREAM_END MINI_STREAM_END
#define Z_NEED_DICT MINI_NEED_DICT
#define Z_ERRNO MINI_ERRNO
#define Z_STREAM_ERROR MINI_STREAM_ERROR
#define Z_DATA_ERROR MINI_DATA_ERROR
#define Z_MEM_ERROR MINI_MEM_ERROR
#define Z_BUF_ERROR MINI_BUF_ERROR
#define Z_VERSION_ERROR MINI_VERSION_ERROR
#define Z_PARAM_ERROR MINI_PARAM_ERROR
#define Z_NO_COMPRESSION MINI_NO_COMPRESSION
#define Z_BEST_SPEED MINI_BEST_SPEED
#define Z_BEST_COMPRESSION MINI_BEST_COMPRESSION
#define Z_DEFAULT_COMPRESSION MINI_DEFAULT_COMPRESSION
#define Z_DEFAULT_STRATEGY MINI_DEFAULT_STRATEGY
#define Z_FILTERED MINI_FILTERED
#define Z_HUFFMAN_ONLY MINI_HUFFMAN_ONLY
#define Z_RLE MINI_RLE
#define Z_FIXED MINI_FIXED
#define Z_DEFLATED MINI_DEFLATED
#define Z_DEFAULT_WINDOW_BITS MINI_DEFAULT_WINDOW_BITS
#define alloc_func MINI_alloc_func
#define free_func MINI_free_func
#define internal_state MINI_internal_state
#define z_stream MINI_stream
#define deflateInit MINI_deflateInit
#define deflateInit2 MINI_deflateInit2
#define deflateReset MINI_deflateReset
#define deflate MINI_deflate
#define deflateEnd MINI_deflateEnd
#define deflateBound MINI_deflateBound
#define compress MINI_compress
#define compress2 MINI_compress2
#define compressBound MINI_compressBound
#define inflateInit MINI_inflateInit
#define inflateInit2 MINI_inflateInit2
#define inflate MINI_inflate
#define inflateEnd MINI_inflateEnd
#define uncompress MINI_uncompress
#define crc32 MINI_crc32
#define adler32 MINI_adler32
#define MAX_WBITS 15
#define MAX_MEM_LEVEL 9
#define zError MINI_error
#define ZLIB_VERSION MINI_VERSION
#define ZLIB_VERNUM MINI_VERNUM
#define ZLIB_VER_MAJOR MINI_VER_MAJOR
#define ZLIB_VER_MINOR MINI_VER_MINOR
#define ZLIB_VER_REVISION MINI_VER_REVISION
#define ZLIB_VER_SUBREVISION MINI_VER_SUBREVISION
#define zlibVersion MINI_version
#define zlib_version MINI_version()
#endif
#endif
typedef unsigned char MINI_uint8;
typedef signed short MINI_int16;
typedef unsigned short MINI_uint16;
typedef unsigned int MINI_uint32;
typedef unsigned int MINI_uint;
typedef long long MINI_int64;
typedef unsigned long long MINI_uint64;
typedef int MINI_bool;
#define MINI_FALSE (0)
#define MINI_TRUE (1)
#ifdef _MSC_VER
#define MINI_MACRO_END while (0, 0)
#else
#define MINI_MACRO_END while (0)
#endif
#ifndef MINI_NO_ARCHIVE_APIS
enum {
MINI_ZIP_MAX_IO_BUF_SIZE = 64 * 1024, MINI_ZIP_MAX_ARCHIVE_FILENAME_SIZE = 260, MINI_ZIP_MAX_ARCHIVE_FILE_COMMENT_SIZE = 256
};
typedef struct {
MINI_uint32 m_file_index;
MINI_uint32 m_central_dir_ofs;
MINI_uint16 m_version_made_by;
MINI_uint16 m_version_needed;
MINI_uint16 m_bit_flag;
MINI_uint16 m_method;
#ifndef MINI_NO_TIME
time_t m_time;
#endif
MINI_uint32 m_crc32;
MINI_uint64 m_comp_size;
MINI_uint64 m_uncomp_size;
MINI_uint16 m_internal_attr;
MINI_uint32 m_external_attr;
MINI_uint64 m_local_header_ofs;
MINI_uint32 m_comment_size;
char m_filename[MINI_ZIP_MAX_ARCHIVE_FILENAME_SIZE];
char m_comment[MINI_ZIP_MAX_ARCHIVE_FILE_COMMENT_SIZE];
} MINI_zip_archive_file_stat;
typedef size_t (*MINI_file_read_func)(void *pOpaque, MINI_uint64 file_ofs, void *pBuf, size_t n);
typedef size_t (*MINI_file_write_func)(void *pOpaque, MINI_uint64 file_ofs, const void *pBuf, size_t n);
struct MINI_zip_internal_state_tag;
typedef struct MINI_zip_internal_state_tag MINI_zip_internal_state;
typedef enum {
MINI_ZIP_MODE_INVALID = 0, MINI_ZIP_MODE_READING = 1, MINI_ZIP_MODE_WRITING = 2, MINI_ZIP_MODE_WRITING_HAS_BEEN_FINALIZED = 3
} MINI_zip_mode;
typedef struct MINI_zip_archive_tag {
MINI_uint64 m_archive_size;
MINI_uint64 m_central_directory_file_ofs;
MINI_uint m_total_files;
MINI_zip_mode m_zip_mode;
MINI_uint m_file_offset_alignment;
MINI_alloc_func m_pAlloc;
MINI_free_func m_pFree;
MINI_realloc_func m_pRealloc;
void *m_pAlloc_opaque;
MINI_file_read_func m_pRead;
MINI_file_write_func m_pWrite;
void *m_pIO_opaque;
MINI_zip_internal_state *m_pState;
} MINI_zip_archive;
typedef enum {
MINI_ZIP_FLAG_CASE_SENSITIVE = 0x0100, MINI_ZIP_FLAG_IGNORE_PATH = 0x0200, MINI_ZIP_FLAG_COMPRESSED_DATA = 0x0400, MINI_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY = 0x0800
} MINI_zip_flags;
MINI_bool MINI_zip_reader_init(MINI_zip_archive *pZip, MINI_uint64 size, MINI_uint32 flags);
MINI_bool MINI_zip_reader_init_mem(MINI_zip_archive *pZip, const void *pMem, size_t size, MINI_uint32 flags);
#ifndef MINI_NO_STDIO
MINI_bool MINI_zip_reader_init_file(MINI_zip_archive *pZip, const char *pFilename, MINI_uint32 flags);
#endif
MINI_uint MINI_zip_reader_get_num_files(MINI_zip_archive *pZip);
MINI_bool MINI_zip_reader_file_stat(MINI_zip_archive *pZip, MINI_uint file_index, MINI_zip_archive_file_stat *pStat);
MINI_bool MINI_zip_reader_is_file_a_directory(MINI_zip_archive *pZip, MINI_uint file_index);
MINI_bool MINI_zip_reader_is_file_encrypted(MINI_zip_archive *pZip, MINI_uint file_index);
MINI_uint MINI_zip_reader_get_filename(MINI_zip_archive *pZip, MINI_uint file_index, char *pFilename, MINI_uint filename_buf_size);
int MINI_zip_reader_locate_file(MINI_zip_archive *pZip, const char *pName, const char *pComment, MINI_uint flags);
MINI_bool MINI_zip_reader_extract_to_mem_no_alloc(MINI_zip_archive *pZip, MINI_uint file_index, void *pBuf, size_t buf_size, MINI_uint flags, void *pUser_read_buf, size_t user_read_buf_size);
MINI_bool MINI_zip_reader_extract_file_to_mem_no_alloc(
MINI_zip_archive *pZip, const char *pFilename, void *pBuf, size_t buf_size, MINI_uint flags, void *pUser_read_buf, size_t user_read_buf_size);
MINI_bool MINI_zip_reader_extract_to_mem(MINI_zip_archive *pZip, MINI_uint file_index, void *pBuf, size_t buf_size, MINI_uint flags);
MINI_bool MINI_zip_reader_extract_file_to_mem(MINI_zip_archive *pZip, const char *pFilename, void *pBuf, size_t buf_size, MINI_uint flags);
void *MINI_zip_reader_extract_to_heap(MINI_zip_archive *pZip, MINI_uint file_index, size_t *pSize, MINI_uint flags);
void *MINI_zip_reader_extract_file_to_heap(MINI_zip_archive *pZip, const char *pFilename, size_t *pSize, MINI_uint flags);
MINI_bool MINI_zip_reader_extract_to_callback(MINI_zip_archive *pZip, MINI_uint file_index, MINI_file_write_func pCallback, void *pOpaque, MINI_uint flags);
MINI_bool MINI_zip_reader_extract_file_to_callback(MINI_zip_archive *pZip, const char *pFilename, MINI_file_write_func pCallback, void *pOpaque, MINI_uint flags);
#ifndef MINI_NO_STDIO
MINI_bool MINI_zip_reader_extract_to_file(MINI_zip_archive *pZip, MINI_uint file_index, const char *pDst_filename, MINI_uint flags);
MINI_bool MINI_zip_reader_extract_file_to_file(MINI_zip_archive *pZip, const char *pArchive_filename, const char *pDst_filename, MINI_uint flags);
#endif
MINI_bool MINI_zip_reader_end(MINI_zip_archive *pZip);
#ifndef MINI_NO_ARCHIVE_WRITING_APIS
MINI_bool MINI_zip_writer_init(MINI_zip_archive *pZip, MINI_uint64 existing_size);
MINI_bool MINI_zip_writer_init_heap(MINI_zip_archive *pZip, size_t size_to_reserve_at_beginning, size_t initial_allocation_size);
#ifndef MINI_NO_STDIO
MINI_bool MINI_zip_writer_init_file(MINI_zip_archive *pZip, const char *pFilename, MINI_uint64 size_to_reserve_at_beginning);
#endif
MINI_bool MINI_zip_writer_init_from_reader(MINI_zip_archive *pZip, const char *pFilename);
MINI_bool MINI_zip_writer_add_mem(MINI_zip_archive *pZip, const char *pArchive_name, const void *pBuf, size_t buf_size, MINI_uint level_and_flags);
MINI_bool MINI_zip_writer_add_mem_ex(MINI_zip_archive *pZip, const char *pArchive_name, const void *pBuf, size_t buf_size, const void *pComment, MINI_uint16 comment_size, MINI_uint level_and_flags, MINI_uint64 uncomp_size, MINI_uint32 uncomp_crc32);
#ifndef MINI_NO_STDIO
MINI_bool MINI_zip_writer_add_file(MINI_zip_archive *pZip, const char *pArchive_name, const char *pSrc_filename, const void *pComment, MINI_uint16 comment_size, MINI_uint level_and_flags);
#endif
MINI_bool MINI_zip_writer_add_from_zip_reader(MINI_zip_archive *pZip, MINI_zip_archive *pSource_zip, MINI_uint file_index);
MINI_bool MINI_zip_writer_finalize_archive(MINI_zip_archive *pZip);
MINI_bool MINI_zip_writer_finalize_heap_archive(MINI_zip_archive *pZip, void **pBuf, size_t *pSize);
MINI_bool MINI_zip_writer_end(MINI_zip_archive *pZip);
MINI_bool MINI_zip_add_mem_to_archive_file_in_place(
const char *pZip_filename, const char *pArchive_name, const void *pBuf, size_t buf_size, const void *pComment, MINI_uint16 comment_size, MINI_uint level_and_flags);
void *MINI_zip_extract_archive_file_to_heap(const char *pZip_filename, const char *pArchive_name, size_t *pSize, MINI_uint zip_flags);
#endif
#endif
enum {
TINFL_FLAG_PARSE_ZLIB_HEADER = 1, TINFL_FLAG_HAS_MORE_INPUT = 2, TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF = 4, TINFL_FLAG_COMPUTE_ADLER32 = 8
};
void *tinfl_decompress_mem_to_heap(const void *pSrc_buf, size_t src_buf_len, size_t *pOut_len, int flags);
#define TINFL_DECOMPRESS_MEM_TO_MEM_FAILED ((size_t)(-1))
size_t tinfl_decompress_mem_to_mem(void *pOut_buf, size_t out_buf_len, const void *pSrc_buf, size_t src_buf_len, int flags);
typedef int (*tinfl_put_buf_func_ptr)(const void *pBuf, int len, void *pUser);
int tinfl_decompress_mem_to_callback(const void *pIn_buf, size_t *pIn_buf_size, tinfl_put_buf_func_ptr pPut_buf_func, void *pPut_buf_user, int flags);
struct tinfl_decompressor_tag;
typedef struct tinfl_decompressor_tag tinfl_decompressor;
#define TINFL_LZ_DICT_SIZE 32768
typedef enum {
TINFL_STATUS_FAILED_CANNOT_MAKE_PROGRESS = -4,
TINFL_STATUS_BAD_PARAM = -3,
TINFL_STATUS_ADLER32_MISMATCH = -2,
TINFL_STATUS_FAILED = -1,
TINFL_STATUS_DONE = 0,
TINFL_STATUS_NEEDS_MORE_INPUT = 1,
TINFL_STATUS_HAS_MORE_OUTPUT = 2
} tinfl_status;
#define tinfl_init(r) \
do { \
(r)->m_state = 0; \
} \
MINI_MACRO_END
#define tinfl_get_adler32(r) (r)->m_check_adler32
tinfl_status tinfl_decompress(tinfl_decompressor *r, const MINI_uint8 *pIn_buf_next, size_t *pIn_buf_size, MINI_uint8 *pOut_buf_start, MINI_uint8 *pOut_buf_next, size_t *pOut_buf_size, const MINI_uint32 decomp_flags);
enum {
TINFL_MAX_HUFF_TABLES = 3, TINFL_MAX_HUFF_SYMBOLS_0 = 288, TINFL_MAX_HUFF_SYMBOLS_1 = 32, TINFL_MAX_HUFF_SYMBOLS_2 = 19, TINFL_FAST_LOOKUP_BITS = 10, TINFL_FAST_LOOKUP_SIZE = 1 << TINFL_FAST_LOOKUP_BITS
};
typedef struct {
MINI_uint8 m_code_size[TINFL_MAX_HUFF_SYMBOLS_0];
MINI_int16 m_look_up[TINFL_FAST_LOOKUP_SIZE], m_tree[TINFL_MAX_HUFF_SYMBOLS_0 * 2];
} tinfl_huff_table;
#if MINI_HAS_64BIT_REGISTERS
#define TINFL_USE_64BIT_BITBUF 1
#endif
#if TINFL_USE_64BIT_BITBUF
typedef MINI_uint64 tinfl_bit_buf_t;
#define TINFL_BITBUF_SIZE (64)
#else
typedef MINI_uint32 tinfl_bit_buf_t;
#define TINFL_BITBUF_SIZE (32)
#endif
struct tinfl_decompressor_tag {
MINI_uint32 m_state, m_num_bits, m_zhdr0, m_zhdr1, m_z_adler32, m_final, m_type, m_check_adler32, m_dist, m_counter, m_num_extra, m_table_sizes[TINFL_MAX_HUFF_TABLES];
tinfl_bit_buf_t m_bit_buf;
size_t m_dist_from_out_buf_start;
tinfl_huff_table m_tables[TINFL_MAX_HUFF_TABLES];
MINI_uint8 m_raw_header[4], m_len_codes[TINFL_MAX_HUFF_SYMBOLS_0 + TINFL_MAX_HUFF_SYMBOLS_1 + 137];
};
#define TDEFL_LESS_MEMORY 0
enum {
TDEFL_HUFFMAN_ONLY = 0, TDEFL_DEFAULT_MAX_PROBES = 128, TDEFL_MAX_PROBES_MASK = 0xFFF
};
enum {
TDEFL_WRITE_ZLIB_HEADER = 0x01000, TDEFL_COMPUTE_ADLER32 = 0x02000, TDEFL_GREEDY_PARSING_FLAG = 0x04000, TDEFL_NONDETERMINISTIC_PARSING_FLAG = 0x08000, TDEFL_RLE_MATCHES = 0x10000, TDEFL_FILTER_MATCHES = 0x20000, TDEFL_FORCE_ALL_STATIC_BLOCKS = 0x40000, TDEFL_FORCE_ALL_RAW_BLOCKS = 0x80000
};
void *tdefl_compress_mem_to_heap(const void *pSrc_buf, size_t src_buf_len, size_t *pOut_len, int flags);
size_t tdefl_compress_mem_to_mem(void *pOut_buf, size_t out_buf_len, const void *pSrc_buf, size_t src_buf_len, int flags);
void *tdefl_write_image_to_png_file_in_memory_ex(const void *pImage, int w, int h, int num_chans, size_t *pLen_out, MINI_uint level, MINI_bool flip);
void *tdefl_write_image_to_png_file_in_memory(const void *pImage, int w, int h, int num_chans, size_t *pLen_out);
typedef MINI_bool (*tdefl_put_buf_func_ptr)(const void *pBuf, int len, void *pUser);
MINI_bool tdefl_compress_mem_to_output(const void *pBuf, size_t buf_len, tdefl_put_buf_func_ptr pPut_buf_func, void *pPut_buf_user, int flags);
enum {
TDEFL_MAX_HUFF_TABLES = 3, TDEFL_MAX_HUFF_SYMBOLS_0 = 288, TDEFL_MAX_HUFF_SYMBOLS_1 = 32, TDEFL_MAX_HUFF_SYMBOLS_2 = 19, TDEFL_LZ_DICT_SIZE = 32768, TDEFL_LZ_DICT_SIZE_MASK = TDEFL_LZ_DICT_SIZE - 1, TDEFL_MIN_MATCH_LEN = 3, TDEFL_MAX_MATCH_LEN = 258
};
#if TDEFL_LESS_MEMORY
enum {
TDEFL_LZ_CODE_BUF_SIZE = 24 * 1024, TDEFL_OUT_BUF_SIZE = (TDEFL_LZ_CODE_BUF_SIZE * 13) / 10, TDEFL_MAX_HUFF_SYMBOLS = 288, TDEFL_LZ_HASH_BITS = 12, TDEFL_LEVEL1_HASH_SIZE_MASK = 4095, TDEFL_LZ_HASH_SHIFT = (TDEFL_LZ_HASH_BITS + 2) / 3, TDEFL_LZ_HASH_SIZE = 1 << TDEFL_LZ_HASH_BITS
};
#else
enum {
TDEFL_LZ_CODE_BUF_SIZE = 64 * 1024, TDEFL_OUT_BUF_SIZE = (TDEFL_LZ_CODE_BUF_SIZE * 13) / 10, TDEFL_MAX_HUFF_SYMBOLS = 288, TDEFL_LZ_HASH_BITS = 15, TDEFL_LEVEL1_HASH_SIZE_MASK = 4095, TDEFL_LZ_HASH_SHIFT = (TDEFL_LZ_HASH_BITS + 2) / 3, TDEFL_LZ_HASH_SIZE = 1 << TDEFL_LZ_HASH_BITS
};
#endif
typedef enum {
TDEFL_STATUS_BAD_PARAM = -2, TDEFL_STATUS_PUT_BUF_FAILED = -1, TDEFL_STATUS_OKAY = 0, TDEFL_STATUS_DONE = 1,
} tdefl_status;
typedef enum {
TDEFL_NO_FLUSH = 0, TDEFL_SYNC_FLUSH = 2, TDEFL_FULL_FLUSH = 3, TDEFL_FINISH = 4
} tdefl_flush;
typedef struct {
tdefl_put_buf_func_ptr m_pPut_buf_func;
void *m_pPut_buf_user;
MINI_uint m_flags, m_max_probes[2];
int m_greedy_parsing;
MINI_uint m_adler32, m_lookahead_pos, m_lookahead_size, m_dict_size;
MINI_uint8 *m_pLZ_code_buf, *m_pLZ_flags, *m_pOutput_buf, *m_pOutput_buf_end;
MINI_uint m_num_flags_left, m_total_lz_bytes, m_lz_code_buf_dict_pos, m_bits_in, m_bit_buffer;
MINI_uint m_saved_match_dist, m_saved_match_len, m_saved_lit, m_output_flush_ofs, m_output_flush_remaining, m_finished, m_block_index, m_wants_to_finish;
tdefl_status m_prev_return_status;
const void *m_pIn_buf;
void *m_pOut_buf;
size_t *m_pIn_buf_size, *m_pOut_buf_size;
tdefl_flush m_flush;
const MINI_uint8 *m_pSrc;
size_t m_src_buf_left, m_out_buf_ofs;
MINI_uint8 m_dict[TDEFL_LZ_DICT_SIZE + TDEFL_MAX_MATCH_LEN - 1];
MINI_uint16 m_huff_count[TDEFL_MAX_HUFF_TABLES][TDEFL_MAX_HUFF_SYMBOLS];
MINI_uint16 m_huff_codes[TDEFL_MAX_HUFF_TABLES][TDEFL_MAX_HUFF_SYMBOLS];
MINI_uint8 m_huff_code_sizes[TDEFL_MAX_HUFF_TABLES][TDEFL_MAX_HUFF_SYMBOLS];
MINI_uint8 m_lz_code_buf[TDEFL_LZ_CODE_BUF_SIZE];
MINI_uint16 m_next[TDEFL_LZ_DICT_SIZE];
MINI_uint16 m_hash[TDEFL_LZ_HASH_SIZE];
MINI_uint8 m_output_buf[TDEFL_OUT_BUF_SIZE];
} tdefl_compressor;
tdefl_status tdefl_init(tdefl_compressor *d, tdefl_put_buf_func_ptr pPut_buf_func, void *pPut_buf_user, int flags);
tdefl_status tdefl_compress(tdefl_compressor *d, const void *pIn_buf, size_t *pIn_buf_size, void *pOut_buf, size_t *pOut_buf_size, tdefl_flush flush);
tdefl_status tdefl_compress_buffer(tdefl_compressor *d, const void *pIn_buf, size_t in_buf_size, tdefl_flush flush);
tdefl_status tdefl_get_prev_return_status(tdefl_compressor *d);
MINI_uint32 tdefl_get_adler32(tdefl_compressor *d);
#ifndef MINI_NO_ZLIB_APIS
MINI_uint tdefl_create_comp_flags_from_zip_params(int level, int window_bits, int strategy);
#endif
tdefl_compressor *tdefl_compressor_alloc();
void tdefl_compressor_free(tdefl_compressor *pComp);
tinfl_decompressor *tinfl_decompressor_alloc();
void tinfl_decompressor_free(tinfl_decompressor *pDecomp);
#ifdef __cplusplus
}
#endif
#endif
#ifndef MINI_HEADER_FILE_ONLY
typedef unsigned char MINI_validate_uint16[sizeof(MINI_uint16) == 2 ? 1 : -1];
typedef unsigned char MINI_validate_uint32[sizeof(MINI_uint32) == 4 ? 1 : -1];
typedef unsigned char MINI_validate_uint64[sizeof(MINI_uint64) == 8 ? 1 : -1];
#include <assert.h>
#include <string.h>
#define MINI_ASSERT(x) assert(x)
#ifdef MINI_NO_MALLOC
#define MINI_MALLOC(x) NULL
#define MINI_FREE(x) (void)x, ((void)0)
#define MINI_REALLOC(p, x) NULL
#else
#define MINI_MALLOC(x) malloc(x)
#define MINI_FREE(x) free(x)
#define MINI_REALLOC(p, x) realloc(p, x)
#endif
#define MINI_MAX(a, b) (((a) > (b)) ? (a) : (b))
#define MINI_MIN(a, b) (((a) < (b)) ? (a) : (b))
#define MINI_CLEAR_OBJ(obj) memset(&(obj), 0, sizeof(obj))
#if MINI_USE_UNALIGNED_LOADS_AND_STORES && MINI_LITTLE_ENDIAN
#define MINI_READ_LE16(p) *((const MINI_uint16 *)(p))
#define MINI_READ_LE32(p) *((const MINI_uint32 *)(p))
#else
#define MINI_READ_LE16(p) \
((MINI_uint32)(((const MINI_uint8 *)(p))[0]) | \
((MINI_uint32)(((const MINI_uint8 *)(p))[1]) << 8U))
#define MINI_READ_LE32(p) \
((MINI_uint32)(((const MINI_uint8 *)(p))[0]) | \
((MINI_uint32)(((const MINI_uint8 *)(p))[1]) << 8U) | \
((MINI_uint32)(((const MINI_uint8 *)(p))[2]) << 16U) | \
((MINI_uint32)(((const MINI_uint8 *)(p))[3]) << 24U))
#endif
#ifdef _MSC_VER
#define MINI_FORCEINLINE __forceinline
#elif defined(__GNUC__)
#define MINI_FORCEINLINE inline __attribute__((__always_inline__))
#else
#define MINI_FORCEINLINE inline
#endif
#ifdef __cplusplus
extern "C" {
#endif
MINI_ulong MINI_adler32(MINI_ulong adler, const unsigned char *ptr, size_t buf_len) {
MINI_uint32 i, s1 = (MINI_uint32)(adler & 0xffff), s2 = (MINI_uint32)(adler >> 16);
size_t block_len = buf_len % 5552;
if (!ptr)
return MINI_ADLER32_INIT;
while (buf_len) {
for (i = 0; i + 7 < block_len; i += 8, ptr += 8) {
s1 += ptr[0], s2 += s1;
s1 += ptr[1], s2 += s1;
s1 += ptr[2], s2 += s1;
s1 += ptr[3], s2 += s1;
s1 += ptr[4], s2 += s1;
s1 += ptr[5], s2 += s1;
s1 += ptr[6], s2 += s1;
s1 += ptr[7], s2 += s1;
}
for (; i < block_len; ++i)
s1 += *ptr++, s2 += s1;
s1 %= 65521U, s2 %= 65521U;
buf_len -= block_len;
block_len = 5552;
}
return (s2 << 16) + s1;
}
MINI_ulong MINI_crc32(MINI_ulong crc, const MINI_uint8 *ptr, size_t buf_len) {
static const MINI_uint32 s_crc32[16] = {
0, 0x1db71064, 0x3b6e20c8, 0x26d930ac, 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c, 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c, 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c};
MINI_uint32 crcu32 = (MINI_uint32)crc;
if (!ptr)
return MINI_CRC32_INIT;
crcu32 = ~crcu32;
while (buf_len--) {
MINI_uint8 b = *ptr++;
crcu32 = (crcu32 >> 4) ^ s_crc32[(crcu32 & 0xF) ^ (b & 0xF)];
crcu32 = (crcu32 >> 4) ^ s_crc32[(crcu32 & 0xF) ^ (b >> 4)];
}
return ~crcu32;
}
void MINI_free(void *p) { MINI_FREE(p); }
#ifndef MINI_NO_ZLIB_APIS
static void *def_alloc_func(void *opaque, size_t items, size_t size) {
(void)opaque, (void)items, (void)size;
return MINI_MALLOC(items * size);
}
static void def_free_func(void *opaque, void *address) {
(void)opaque, (void)address;
MINI_FREE(address);
}
static void *def_realloc_func(void *opaque, void *address, size_t items, size_t size) {
(void)opaque, (void)address, (void)items, (void)size;
return MINI_REALLOC(address, items * size);
}
const char *MINI_version(void) { return MINI_VERSION; }
int MINI_deflateInit(MINI_streamp pStream, int level) {
return MINI_deflateInit2(pStream, level, MINI_DEFLATED, MINI_DEFAULT_WINDOW_BITS, 9, MINI_DEFAULT_STRATEGY);
}
int MINI_deflateInit2(MINI_streamp pStream, int level, int method, int window_bits, int mem_level, int strategy) {
tdefl_compressor *pComp;
MINI_uint comp_flags = TDEFL_COMPUTE_ADLER32 |
tdefl_create_comp_flags_from_zip_params(level, window_bits, strategy);
if (!pStream)
return MINI_STREAM_ERROR;
if ((method != MINI_DEFLATED) || ((mem_level < 1) || (mem_level > 9)) ||
((window_bits != MINI_DEFAULT_WINDOW_BITS) &&
(-window_bits != MINI_DEFAULT_WINDOW_BITS)))
return MINI_PARAM_ERROR;
pStream->data_type = 0;
pStream->adler = MINI_ADLER32_INIT;
pStream->msg = NULL;
pStream->reserved = 0;
pStream->total_in = 0;
pStream->total_out = 0;
if (!pStream->zalloc)
pStream->zalloc = def_alloc_func;
if (!pStream->zfree)
pStream->zfree = def_free_func;
pComp = (tdefl_compressor *)pStream->zalloc(pStream->opaque, 1, sizeof(tdefl_compressor));
if (!pComp)
return MINI_MEM_ERROR;
pStream->state = (struct MINI_internal_state *)pComp;
if (tdefl_init(pComp, NULL, NULL, comp_flags) != TDEFL_STATUS_OKAY) {
MINI_deflateEnd(pStream);
return MINI_PARAM_ERROR;
}
return MINI_OK;
}
int MINI_deflateReset(MINI_streamp pStream) {
if ((!pStream) || (!pStream->state) || (!pStream->zalloc) ||
(!pStream->zfree))
return MINI_STREAM_ERROR;
pStream->total_in = pStream->total_out = 0;
tdefl_init((tdefl_compressor *)pStream->state, NULL, NULL, ((tdefl_compressor *)pStream->state)->m_flags);
return MINI_OK;
}
int MINI_deflate(MINI_streamp pStream, int flush) {
size_t in_bytes, out_bytes;
MINI_ulong orig_total_in, orig_total_out;
int MINI_status = MINI_OK;
if ((!pStream) || (!pStream->state) || (flush < 0) || (flush > MINI_FINISH) ||
(!pStream->next_out))
return MINI_STREAM_ERROR;
if (!pStream->avail_out)
return MINI_BUF_ERROR;
if (flush == MINI_PARTIAL_FLUSH)
flush = MINI_SYNC_FLUSH;
if (((tdefl_compressor *)pStream->state)->m_prev_return_status == TDEFL_STATUS_DONE)
return (flush == MINI_FINISH) ? MINI_STREAM_END : MINI_BUF_ERROR;
orig_total_in = pStream->total_in;
orig_total_out = pStream->total_out;
for (;;) {
tdefl_status defl_status;
in_bytes = pStream->avail_in;
out_bytes = pStream->avail_out;
defl_status = tdefl_compress((tdefl_compressor *)pStream->state, pStream->next_in, &in_bytes, pStream->next_out, &out_bytes, (tdefl_flush)flush);
pStream->next_in += (MINI_uint)in_bytes;
pStream->avail_in -= (MINI_uint)in_bytes;
pStream->total_in += (MINI_uint)in_bytes;
pStream->adler = tdefl_get_adler32((tdefl_compressor *)pStream->state);
pStream->next_out += (MINI_uint)out_bytes;
pStream->avail_out -= (MINI_uint)out_bytes;
pStream->total_out += (MINI_uint)out_bytes;
if (defl_status < 0) {
MINI_status = MINI_STREAM_ERROR;
break;
} else if (defl_status == TDEFL_STATUS_DONE) {
MINI_status = MINI_STREAM_END;
break;
} else if (!pStream->avail_out)
break;
else if ((!pStream->avail_in) && (flush != MINI_FINISH)) {
if ((flush) || (pStream->total_in != orig_total_in) ||
(pStream->total_out != orig_total_out))
break;
return MINI_BUF_ERROR;
}
}
return MINI_status;
}
int MINI_deflateEnd(MINI_streamp pStream) {
if (!pStream)
return MINI_STREAM_ERROR;
if (pStream->state) {
pStream->zfree(pStream->opaque, pStream->state);
pStream->state = NULL;
}
return MINI_OK;
}
MINI_ulong MINI_deflateBound(MINI_streamp pStream, MINI_ulong source_len) {
(void)pStream;
return MINI_MAX(128 + (source_len * 110) / 100, 128 + source_len + ((source_len / (31 * 1024)) + 1) * 5);
}
int MINI_compress2(unsigned char *pDest, MINI_ulong *pDest_len, const unsigned char *pSource, MINI_ulong source_len, int level) {
int status;
MINI_stream stream;
memset(&stream, 0, sizeof(stream));
if ((source_len | *pDest_len) > 0xFFFFFFFFU)
return MINI_PARAM_ERROR;
stream.next_in = pSource;
stream.avail_in = (MINI_uint32)source_len;
stream.next_out = pDest;
stream.avail_out = (MINI_uint32)*pDest_len;
status = MINI_deflateInit(&stream, level);
if (status != MINI_OK)
return status;
status = MINI_deflate(&stream, MINI_FINISH);
if (status != MINI_STREAM_END) {
MINI_deflateEnd(&stream);
return (status == MINI_OK) ? MINI_BUF_ERROR : status;
}
*pDest_len = stream.total_out;
return MINI_deflateEnd(&stream);
}
int MINI_compress(unsigned char *pDest, MINI_ulong *pDest_len, const unsigned char *pSource, MINI_ulong source_len) {
return MINI_compress2(pDest, pDest_len, pSource, source_len, MINI_DEFAULT_COMPRESSION);
}
MINI_ulong MINI_compressBound(MINI_ulong source_len) {
return MINI_deflateBound(NULL, source_len);
}
typedef struct {
tinfl_decompressor m_decomp;
MINI_uint m_dict_ofs, m_dict_avail, m_first_call, m_has_flushed;
int m_window_bits;
MINI_uint8 m_dict[TINFL_LZ_DICT_SIZE];
tinfl_status m_last_status;
} inflate_state;
int MINI_inflateInit2(MINI_streamp pStream, int window_bits) {
inflate_state *pDecomp;
if (!pStream)
return MINI_STREAM_ERROR;
if ((window_bits != MINI_DEFAULT_WINDOW_BITS) &&
(-window_bits != MINI_DEFAULT_WINDOW_BITS))
return MINI_PARAM_ERROR;
pStream->data_type = 0;
pStream->adler = 0;
pStream->msg = NULL;
pStream->total_in = 0;
pStream->total_out = 0;
pStream->reserved = 0;
if (!pStream->zalloc)
pStream->zalloc = def_alloc_func;
if (!pStream->zfree)
pStream->zfree = def_free_func;
pDecomp = (inflate_state *)pStream->zalloc(pStream->opaque, 1, sizeof(inflate_state));
if (!pDecomp)
return MINI_MEM_ERROR;
pStream->state = (struct MINI_internal_state *)pDecomp;
tinfl_init(&pDecomp->m_decomp);
pDecomp->m_dict_ofs = 0;
pDecomp->m_dict_avail = 0;
pDecomp->m_last_status = TINFL_STATUS_NEEDS_MORE_INPUT;
pDecomp->m_first_call = 1;
pDecomp->m_has_flushed = 0;
pDecomp->m_window_bits = window_bits;
return MINI_OK;
}
int MINI_inflateInit(MINI_streamp pStream) {
return MINI_inflateInit2(pStream, MINI_DEFAULT_WINDOW_BITS);
}
int MINI_inflate(MINI_streamp pStream, int flush) {
inflate_state *pState;
MINI_uint n, first_call, decomp_flags = TINFL_FLAG_COMPUTE_ADLER32;
size_t in_bytes, out_bytes, orig_avail_in;
tinfl_status status;
if ((!pStream) || (!pStream->state))
return MINI_STREAM_ERROR;
if (flush == MINI_PARTIAL_FLUSH)
flush = MINI_SYNC_FLUSH;
if ((flush) && (flush != MINI_SYNC_FLUSH) && (flush != MINI_FINISH))
return MINI_STREAM_ERROR;
pState = (inflate_state *)pStream->state;
if (pState->m_window_bits > 0)
decomp_flags |= TINFL_FLAG_PARSE_ZLIB_HEADER;
orig_avail_in = pStream->avail_in;
first_call = pState->m_first_call;
pState->m_first_call = 0;
if (pState->m_last_status < 0)
return MINI_DATA_ERROR;
if (pState->m_has_flushed && (flush != MINI_FINISH))
return MINI_STREAM_ERROR;
pState->m_has_flushed |= (flush == MINI_FINISH);
if ((flush == MINI_FINISH) && (first_call)) {
decomp_flags |= TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF;
in_bytes = pStream->avail_in;
out_bytes = pStream->avail_out;
status = tinfl_decompress(&pState->m_decomp, pStream->next_in, &in_bytes, pStream->next_out, pStream->next_out, &out_bytes, decomp_flags);
pState->m_last_status = status;
pStream->next_in += (MINI_uint)in_bytes;
pStream->avail_in -= (MINI_uint)in_bytes;
pStream->total_in += (MINI_uint)in_bytes;
pStream->adler = tinfl_get_adler32(&pState->m_decomp);
pStream->next_out += (MINI_uint)out_bytes;
pStream->avail_out -= (MINI_uint)out_bytes;
pStream->total_out += (MINI_uint)out_bytes;
if (status < 0)
return MINI_DATA_ERROR;
else if (status != TINFL_STATUS_DONE) {
pState->m_last_status = TINFL_STATUS_FAILED;
return MINI_BUF_ERROR;
}
return MINI_STREAM_END;
}
if (flush != MINI_FINISH)
decomp_flags |= TINFL_FLAG_HAS_MORE_INPUT;
if (pState->m_dict_avail) {
n = MINI_MIN(pState->m_dict_avail, pStream->avail_out);
memcpy(pStream->next_out, pState->m_dict + pState->m_dict_ofs, n);
pStream->next_out += n;
pStream->avail_out -= n;
pStream->total_out += n;
pState->m_dict_avail -= n;
pState->m_dict_ofs = (pState->m_dict_ofs + n) & (TINFL_LZ_DICT_SIZE - 1);
return ((pState->m_last_status == TINFL_STATUS_DONE) &&
(!pState->m_dict_avail))
? MINI_STREAM_END
: MINI_OK;
}
for (;;) {
in_bytes = pStream->avail_in;
out_bytes = TINFL_LZ_DICT_SIZE - pState->m_dict_ofs;
status = tinfl_decompress(
&pState->m_decomp, pStream->next_in, &in_bytes, pState->m_dict, pState->m_dict + pState->m_dict_ofs, &out_bytes, decomp_flags);
pState->m_last_status = status;
pStream->next_in += (MINI_uint)in_bytes;
pStream->avail_in -= (MINI_uint)in_bytes;
pStream->total_in += (MINI_uint)in_bytes;
pStream->adler = tinfl_get_adler32(&pState->m_decomp);
pState->m_dict_avail = (MINI_uint)out_bytes;
n = MINI_MIN(pState->m_dict_avail, pStream->avail_out);
memcpy(pStream->next_out, pState->m_dict + pState->m_dict_ofs, n);
pStream->next_out += n;
pStream->avail_out -= n;
pStream->total_out += n;
pState->m_dict_avail -= n;
pState->m_dict_ofs = (pState->m_dict_ofs + n) & (TINFL_LZ_DICT_SIZE - 1);
if (status < 0)
return MINI_DATA_ERROR;
else if ((status == TINFL_STATUS_NEEDS_MORE_INPUT) && (!orig_avail_in))
return MINI_BUF_ERROR;
else if (flush == MINI_FINISH) {
if (status == TINFL_STATUS_DONE)
return pState->m_dict_avail ? MINI_BUF_ERROR : MINI_STREAM_END;
else if (!pStream->avail_out)
return MINI_BUF_ERROR;
} else if ((status == TINFL_STATUS_DONE) || (!pStream->avail_in) ||
(!pStream->avail_out) || (pState->m_dict_avail))
break;
}
return ((status == TINFL_STATUS_DONE) && (!pState->m_dict_avail))
? MINI_STREAM_END
: MINI_OK;
}
int MINI_inflateEnd(MINI_streamp pStream) {
if (!pStream)
return MINI_STREAM_ERROR;
if (pStream->state) {
pStream->zfree(pStream->opaque, pStream->state);
pStream->state = NULL;
}
return MINI_OK;
}
int MINI_uncompress(unsigned char *pDest, MINI_ulong *pDest_len, const unsigned char *pSource, MINI_ulong source_len) {
MINI_stream stream;
int status;
memset(&stream, 0, sizeof(stream));
if ((source_len | *pDest_len) > 0xFFFFFFFFU)
return MINI_PARAM_ERROR;
stream.next_in = pSource;
stream.avail_in = (MINI_uint32)source_len;
stream.next_out = pDest;
stream.avail_out = (MINI_uint32)*pDest_len;
status = MINI_inflateInit(&stream);
if (status != MINI_OK)
return status;
status = MINI_inflate(&stream, MINI_FINISH);
if (status != MINI_STREAM_END) {
MINI_inflateEnd(&stream);
return ((status == MINI_BUF_ERROR) && (!stream.avail_in)) ? MINI_DATA_ERROR
: status;
}
*pDest_len = stream.total_out;
return MINI_inflateEnd(&stream);
}
const char *MINI_error(int err) {
static struct {
int m_err;
const char *m_pDesc;
} s_error_descs[] = {{MINI_OK, ""}, {MINI_STREAM_END, "stream end"}, {MINI_NEED_DICT, "need dictionary"}, {MINI_ERRNO, "file error"}, {MINI_STREAM_ERROR, "stream error"}, {MINI_DATA_ERROR, "data error"}, {MINI_MEM_ERROR, "out of memory"}, {MINI_BUF_ERROR, "buf error"}, {MINI_VERSION_ERROR, "version error"}, {MINI_PARAM_ERROR, "parameter error"}};
MINI_uint i;
for (i = 0; i < sizeof(s_error_descs) / sizeof(s_error_descs[0]); ++i)
if (s_error_descs[i].m_err == err)
return s_error_descs[i].m_pDesc;
return NULL;
}
#endif
#define TINFL_MEMCPY(d, s, l) memcpy(d, s, l)
#define TINFL_MEMSET(p, c, l) memset(p, c, l)
#define TINFL_CR_BEGIN \
switch (r->m_state) { \
case 0:
#define TINFL_CR_RETURN(state_index, result) \
do { \
status = result; \
r->m_state = state_index; \
goto common_exit; \
case state_index:; \
} \
MINI_MACRO_END
#define TINFL_CR_RETURN_FOREVER(state_index, result) \
do { \
for (;;) { \
TINFL_CR_RETURN(state_index, result); \
} \
} \
MINI_MACRO_END
#define TINFL_CR_FINISH }
#define TINFL_GET_BYTE(state_index, c) \
do { \
while (pIn_buf_cur >= pIn_buf_end) { \
TINFL_CR_RETURN(state_index, \
(decomp_flags & TINFL_FLAG_HAS_MORE_INPUT) \
? TINFL_STATUS_NEEDS_MORE_INPUT \
: TINFL_STATUS_FAILED_CANNOT_MAKE_PROGRESS); \
} \
c = *pIn_buf_cur++; \
} \
MINI_MACRO_END
#define TINFL_NEED_BITS(state_index, n) \
do { \
MINI_uint c; \
TINFL_GET_BYTE(state_index, c); \
bit_buf |= (((tinfl_bit_buf_t)c) << num_bits); \
num_bits += 8; \
} while (num_bits < (MINI_uint)(n))
#define TINFL_SKIP_BITS(state_index, n) \
do { \
if (num_bits < (MINI_uint)(n)) { \
TINFL_NEED_BITS(state_index, n); \
} \
bit_buf >>= (n); \
num_bits -= (n); \
} \
MINI_MACRO_END
#define TINFL_GET_BITS(state_index, b, n) \
do { \
if (num_bits < (MINI_uint)(n)) { \
TINFL_NEED_BITS(state_index, n); \
} \
b = bit_buf & ((1 << (n)) - 1); \
bit_buf >>= (n); \
num_bits -= (n); \
} \
MINI_MACRO_END
#define TINFL_HUFF_BITBUF_FILL(state_index, pHuff) \
do { \
temp = (pHuff)->m_look_up[bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]; \
if (temp >= 0) { \
code_len = temp >> 9; \
if ((code_len) && (num_bits >= code_len)) \
break; \
} else if (num_bits > TINFL_FAST_LOOKUP_BITS) { \
code_len = TINFL_FAST_LOOKUP_BITS; \
do { \
temp = (pHuff)->m_tree[~temp + ((bit_buf >> code_len++) & 1)]; \
} while ((temp < 0) && (num_bits >= (code_len + 1))); \
if (temp >= 0) \
break; \
} \
TINFL_GET_BYTE(state_index, c); \
bit_buf |= (((tinfl_bit_buf_t)c) << num_bits); \
num_bits += 8; \
} while (num_bits < 15);
#define TINFL_HUFF_DECODE(state_index, sym, pHuff) \
do { \
int temp; \
MINI_uint code_len, c; \
if (num_bits < 15) { \
if ((pIn_buf_end - pIn_buf_cur) < 2) { \
TINFL_HUFF_BITBUF_FILL(state_index, pHuff); \
} else { \
bit_buf |= (((tinfl_bit_buf_t)pIn_buf_cur[0]) << num_bits) | \
(((tinfl_bit_buf_t)pIn_buf_cur[1]) << (num_bits + 8)); \
pIn_buf_cur += 2; \
num_bits += 16; \
} \
} \
if ((temp = (pHuff)->m_look_up[bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]) >= \
0) \
code_len = temp >> 9, temp &= 511; \
else { \
code_len = TINFL_FAST_LOOKUP_BITS; \
do { \
temp = (pHuff)->m_tree[~temp + ((bit_buf >> code_len++) & 1)]; \
} while (temp < 0); \
} \
sym = temp; \
bit_buf >>= code_len; \
num_bits -= code_len; \
} \
MINI_MACRO_END
tinfl_status tinfl_decompress(tinfl_decompressor *r, const MINI_uint8 *pIn_buf_next, size_t *pIn_buf_size, MINI_uint8 *pOut_buf_start, MINI_uint8 *pOut_buf_next, size_t *pOut_buf_size, const MINI_uint32 decomp_flags) {
static const int s_length_base[31] = {
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, 0, 0};
static const int s_length_extra[31] = {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, 0, 0};
static const int s_dist_base[32] = {
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, 0, 0};
static const int s_dist_extra[32] = {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};
static const MINI_uint8 s_length_dezigzag[19] = {
16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
static const int s_min_table_sizes[3] = {257, 1, 4};
tinfl_status status = TINFL_STATUS_FAILED;
MINI_uint32 num_bits, dist, counter, num_extra;
tinfl_bit_buf_t bit_buf;
const MINI_uint8 *pIn_buf_cur = pIn_buf_next, *const pIn_buf_end = pIn_buf_next + *pIn_buf_size;
MINI_uint8 *pOut_buf_cur = pOut_buf_next, *const pOut_buf_end = pOut_buf_next + *pOut_buf_size;
size_t out_buf_size_mask = (decomp_flags & TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF)
? (size_t)-1
: ((pOut_buf_next - pOut_buf_start) + *pOut_buf_size) - 1, dist_from_out_buf_start;
if (((out_buf_size_mask + 1) & out_buf_size_mask) ||
(pOut_buf_next < pOut_buf_start)) {
*pIn_buf_size = *pOut_buf_size = 0;
return TINFL_STATUS_BAD_PARAM;
}
num_bits = r->m_num_bits;
bit_buf = r->m_bit_buf;
dist = r->m_dist;
counter = r->m_counter;
num_extra = r->m_num_extra;
dist_from_out_buf_start = r->m_dist_from_out_buf_start;
TINFL_CR_BEGIN
bit_buf = num_bits = dist = counter = num_extra = r->m_zhdr0 = r->m_zhdr1 = 0;
r->m_z_adler32 = r->m_check_adler32 = 1;
if (decomp_flags & TINFL_FLAG_PARSE_ZLIB_HEADER) {
TINFL_GET_BYTE(1, r->m_zhdr0);
TINFL_GET_BYTE(2, r->m_zhdr1);
counter = (((r->m_zhdr0 * 256 + r->m_zhdr1) % 31 != 0) ||
(r->m_zhdr1 & 32) || ((r->m_zhdr0 & 15) != 8));
if (!(decomp_flags & TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF))
counter |= (((1U << (8U + (r->m_zhdr0 >> 4))) > 32768U) ||
((out_buf_size_mask + 1) <
(size_t)(1U << (8U + (r->m_zhdr0 >> 4)))));
if (counter) {
TINFL_CR_RETURN_FOREVER(36, TINFL_STATUS_FAILED);
}
}
do {
TINFL_GET_BITS(3, r->m_final, 3);
r->m_type = r->m_final >> 1;
if (r->m_type == 0) {
TINFL_SKIP_BITS(5, num_bits & 7);
for (counter = 0; counter < 4; ++counter) {
if (num_bits)
TINFL_GET_BITS(6, r->m_raw_header[counter], 8);
else
TINFL_GET_BYTE(7, r->m_raw_header[counter]);
}
if ((counter = (r->m_raw_header[0] | (r->m_raw_header[1] << 8))) != (MINI_uint)(0xFFFF ^
(r->m_raw_header[2] | (r->m_raw_header[3] << 8)))) {
TINFL_CR_RETURN_FOREVER(39, TINFL_STATUS_FAILED);
}
while ((counter) && (num_bits)) {
TINFL_GET_BITS(51, dist, 8);
while (pOut_buf_cur >= pOut_buf_end) {
TINFL_CR_RETURN(52, TINFL_STATUS_HAS_MORE_OUTPUT);
}
*pOut_buf_cur++ = (MINI_uint8)dist;
counter--;
}
while (counter) {
size_t n;
while (pOut_buf_cur >= pOut_buf_end) {
TINFL_CR_RETURN(9, TINFL_STATUS_HAS_MORE_OUTPUT);
}
while (pIn_buf_cur >= pIn_buf_end) {
TINFL_CR_RETURN(38, (decomp_flags & TINFL_FLAG_HAS_MORE_INPUT)
? TINFL_STATUS_NEEDS_MORE_INPUT
: TINFL_STATUS_FAILED_CANNOT_MAKE_PROGRESS);