capacitor-dex-editor 0.0.69 → 0.0.71
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/android/build.gradle +22 -0
- package/android/src/main/cpp/CMakeLists.txt +57 -0
- package/android/src/main/cpp/apk/apk_handler.cpp +121 -0
- package/android/src/main/cpp/apk/zip_utils.cpp +425 -0
- package/android/src/main/cpp/arsc/arsc_parser.cpp +390 -0
- package/android/src/main/cpp/dex/dex_builder.cpp +752 -0
- package/android/src/main/cpp/dex/dex_parser.cpp +620 -0
- package/android/src/main/cpp/dex/smali_disasm.cpp +1223 -0
- package/android/src/main/cpp/dex/smali_to_java.cpp +576 -0
- package/android/src/main/cpp/include/apk/apk_handler.h +41 -0
- package/android/src/main/cpp/include/apk/zip_utils.h +57 -0
- package/android/src/main/cpp/include/arsc/arsc_parser.h +98 -0
- package/android/src/main/cpp/include/dex/dex_builder.h +189 -0
- package/android/src/main/cpp/include/dex/dex_parser.h +137 -0
- package/android/src/main/cpp/include/dex/smali_disasm.h +127 -0
- package/android/src/main/cpp/include/dex/smali_to_java.h +50 -0
- package/android/src/main/cpp/include/xml/android_resources.h +495 -0
- package/android/src/main/cpp/include/xml/axml_parser.h +147 -0
- package/android/src/main/cpp/jni_bridge.cpp +872 -0
- package/android/src/main/cpp/third_party/miniz.c +646 -0
- package/android/src/main/cpp/third_party/miniz.h +605 -0
- package/android/src/main/cpp/third_party/miniz_common.h +97 -0
- package/android/src/main/cpp/third_party/miniz_export.h +6 -0
- package/android/src/main/cpp/third_party/miniz_tdef.c +1597 -0
- package/android/src/main/cpp/third_party/miniz_tdef.h +199 -0
- package/android/src/main/cpp/third_party/miniz_tinfl.c +770 -0
- package/android/src/main/cpp/third_party/miniz_tinfl.h +150 -0
- package/android/src/main/cpp/third_party/miniz_zip.c +4895 -0
- package/android/src/main/cpp/third_party/miniz_zip.h +454 -0
- package/android/src/main/cpp/third_party/nlohmann_json/CMakeLists.txt +0 -0
- package/android/src/main/cpp/third_party/nlohmann_json/single_include/nlohmann/json.hpp +24765 -0
- package/android/src/main/cpp/xml/axml_parser.cpp +1701 -0
- package/android/src/main/java/com/aetherlink/dexeditor/CppDex.java +295 -0
- package/android/src/main/java/com/aetherlink/dexeditor/DexManager.java +146 -50
- package/package.json +1 -1
- package/android/src/main/java/com/aetherlink/dexeditor/RustDex.java +0 -203
- package/android/src/main/jniLibs/arm64-v8a/libdex_rust.so +0 -0
- package/android/src/main/jniLibs/armeabi-v7a/libdex_rust.so +0 -0
- package/android/src/main/jniLibs/x86/libdex_rust.so +0 -0
- package/android/src/main/jniLibs/x86_64/libdex_rust.so +0 -0
|
@@ -0,0 +1,1597 @@
|
|
|
1
|
+
/**************************************************************************
|
|
2
|
+
*
|
|
3
|
+
* Copyright 2013-2014 RAD Game Tools and Valve Software
|
|
4
|
+
* Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC
|
|
5
|
+
* All Rights Reserved.
|
|
6
|
+
*
|
|
7
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
8
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
9
|
+
* in the Software without restriction, including without limitation the rights
|
|
10
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
11
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
12
|
+
* furnished to do so, subject to the following conditions:
|
|
13
|
+
*
|
|
14
|
+
* The above copyright notice and this permission notice shall be included in
|
|
15
|
+
* all copies or substantial portions of the Software.
|
|
16
|
+
*
|
|
17
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
18
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
19
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
20
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
21
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
22
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
23
|
+
* THE SOFTWARE.
|
|
24
|
+
*
|
|
25
|
+
**************************************************************************/
|
|
26
|
+
|
|
27
|
+
#include "miniz.h"
|
|
28
|
+
|
|
29
|
+
#ifndef MINIZ_NO_DEFLATE_APIS
|
|
30
|
+
|
|
31
|
+
#ifdef __cplusplus
|
|
32
|
+
extern "C"
|
|
33
|
+
{
|
|
34
|
+
#endif
|
|
35
|
+
|
|
36
|
+
/* ------------------- Low-level Compression (independent from all decompression API's) */
|
|
37
|
+
|
|
38
|
+
/* Purposely making these tables static for faster init and thread safety. */
|
|
39
|
+
static const mz_uint16 s_tdefl_len_sym[256] = {
|
|
40
|
+
257, 258, 259, 260, 261, 262, 263, 264, 265, 265, 266, 266, 267, 267, 268, 268, 269, 269, 269, 269, 270, 270, 270, 270, 271, 271, 271, 271, 272, 272, 272, 272,
|
|
41
|
+
273, 273, 273, 273, 273, 273, 273, 273, 274, 274, 274, 274, 274, 274, 274, 274, 275, 275, 275, 275, 275, 275, 275, 275, 276, 276, 276, 276, 276, 276, 276, 276,
|
|
42
|
+
277, 277, 277, 277, 277, 277, 277, 277, 277, 277, 277, 277, 277, 277, 277, 277, 278, 278, 278, 278, 278, 278, 278, 278, 278, 278, 278, 278, 278, 278, 278, 278,
|
|
43
|
+
279, 279, 279, 279, 279, 279, 279, 279, 279, 279, 279, 279, 279, 279, 279, 279, 280, 280, 280, 280, 280, 280, 280, 280, 280, 280, 280, 280, 280, 280, 280, 280,
|
|
44
|
+
281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281,
|
|
45
|
+
282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282,
|
|
46
|
+
283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283,
|
|
47
|
+
284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 285
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
static const mz_uint8 s_tdefl_len_extra[256] = {
|
|
51
|
+
0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
|
|
52
|
+
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
|
|
53
|
+
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
|
|
54
|
+
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 0
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
static const mz_uint8 s_tdefl_small_dist_sym[512] = {
|
|
58
|
+
0, 1, 2, 3, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11,
|
|
59
|
+
11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13,
|
|
60
|
+
13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
|
|
61
|
+
14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
|
|
62
|
+
14, 14, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
|
|
63
|
+
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
|
|
64
|
+
16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
|
|
65
|
+
16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
|
|
66
|
+
16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
|
|
67
|
+
17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
|
|
68
|
+
17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
|
|
69
|
+
17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
static const mz_uint8 s_tdefl_small_dist_extra[512] = {
|
|
73
|
+
0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5,
|
|
74
|
+
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
|
|
75
|
+
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
|
|
76
|
+
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
|
|
77
|
+
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
|
|
78
|
+
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
|
|
79
|
+
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
|
|
80
|
+
7, 7, 7, 7, 7, 7, 7, 7
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
static const mz_uint8 s_tdefl_large_dist_sym[128] = {
|
|
84
|
+
0, 0, 18, 19, 20, 20, 21, 21, 22, 22, 22, 22, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 25, 25, 25, 25, 25, 25, 25, 25, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
|
|
85
|
+
26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
|
|
86
|
+
28, 28, 28, 28, 28, 28, 28, 28, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
static const mz_uint8 s_tdefl_large_dist_extra[128] = {
|
|
90
|
+
0, 0, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
|
|
91
|
+
12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
|
|
92
|
+
13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
/* Radix sorts tdefl_sym_freq[] array by 16-bit key m_key. Returns ptr to sorted values. */
|
|
96
|
+
typedef struct
|
|
97
|
+
{
|
|
98
|
+
mz_uint16 m_key, m_sym_index;
|
|
99
|
+
} tdefl_sym_freq;
|
|
100
|
+
static tdefl_sym_freq *tdefl_radix_sort_syms(mz_uint num_syms, tdefl_sym_freq *pSyms0, tdefl_sym_freq *pSyms1)
|
|
101
|
+
{
|
|
102
|
+
mz_uint32 total_passes = 2, pass_shift, pass, i, hist[256 * 2];
|
|
103
|
+
tdefl_sym_freq *pCur_syms = pSyms0, *pNew_syms = pSyms1;
|
|
104
|
+
MZ_CLEAR_ARR(hist);
|
|
105
|
+
for (i = 0; i < num_syms; i++)
|
|
106
|
+
{
|
|
107
|
+
mz_uint freq = pSyms0[i].m_key;
|
|
108
|
+
hist[freq & 0xFF]++;
|
|
109
|
+
hist[256 + ((freq >> 8) & 0xFF)]++;
|
|
110
|
+
}
|
|
111
|
+
while ((total_passes > 1) && (num_syms == hist[(total_passes - 1) * 256]))
|
|
112
|
+
total_passes--;
|
|
113
|
+
for (pass_shift = 0, pass = 0; pass < total_passes; pass++, pass_shift += 8)
|
|
114
|
+
{
|
|
115
|
+
const mz_uint32 *pHist = &hist[pass << 8];
|
|
116
|
+
mz_uint offsets[256], cur_ofs = 0;
|
|
117
|
+
for (i = 0; i < 256; i++)
|
|
118
|
+
{
|
|
119
|
+
offsets[i] = cur_ofs;
|
|
120
|
+
cur_ofs += pHist[i];
|
|
121
|
+
}
|
|
122
|
+
for (i = 0; i < num_syms; i++)
|
|
123
|
+
pNew_syms[offsets[(pCur_syms[i].m_key >> pass_shift) & 0xFF]++] = pCur_syms[i];
|
|
124
|
+
{
|
|
125
|
+
tdefl_sym_freq *t = pCur_syms;
|
|
126
|
+
pCur_syms = pNew_syms;
|
|
127
|
+
pNew_syms = t;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
return pCur_syms;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/* tdefl_calculate_minimum_redundancy() originally written by: Alistair Moffat, alistair@cs.mu.oz.au, Jyrki Katajainen, jyrki@diku.dk, November 1996. */
|
|
134
|
+
static void tdefl_calculate_minimum_redundancy(tdefl_sym_freq *A, int n)
|
|
135
|
+
{
|
|
136
|
+
int root, leaf, next, avbl, used, dpth;
|
|
137
|
+
if (n == 0)
|
|
138
|
+
return;
|
|
139
|
+
else if (n == 1)
|
|
140
|
+
{
|
|
141
|
+
A[0].m_key = 1;
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
A[0].m_key += A[1].m_key;
|
|
145
|
+
root = 0;
|
|
146
|
+
leaf = 2;
|
|
147
|
+
for (next = 1; next < n - 1; next++)
|
|
148
|
+
{
|
|
149
|
+
if (leaf >= n || A[root].m_key < A[leaf].m_key)
|
|
150
|
+
{
|
|
151
|
+
A[next].m_key = A[root].m_key;
|
|
152
|
+
A[root++].m_key = (mz_uint16)next;
|
|
153
|
+
}
|
|
154
|
+
else
|
|
155
|
+
A[next].m_key = A[leaf++].m_key;
|
|
156
|
+
if (leaf >= n || (root < next && A[root].m_key < A[leaf].m_key))
|
|
157
|
+
{
|
|
158
|
+
A[next].m_key = (mz_uint16)(A[next].m_key + A[root].m_key);
|
|
159
|
+
A[root++].m_key = (mz_uint16)next;
|
|
160
|
+
}
|
|
161
|
+
else
|
|
162
|
+
A[next].m_key = (mz_uint16)(A[next].m_key + A[leaf++].m_key);
|
|
163
|
+
}
|
|
164
|
+
A[n - 2].m_key = 0;
|
|
165
|
+
for (next = n - 3; next >= 0; next--)
|
|
166
|
+
A[next].m_key = A[A[next].m_key].m_key + 1;
|
|
167
|
+
avbl = 1;
|
|
168
|
+
used = dpth = 0;
|
|
169
|
+
root = n - 2;
|
|
170
|
+
next = n - 1;
|
|
171
|
+
while (avbl > 0)
|
|
172
|
+
{
|
|
173
|
+
while (root >= 0 && (int)A[root].m_key == dpth)
|
|
174
|
+
{
|
|
175
|
+
used++;
|
|
176
|
+
root--;
|
|
177
|
+
}
|
|
178
|
+
while (avbl > used)
|
|
179
|
+
{
|
|
180
|
+
A[next--].m_key = (mz_uint16)(dpth);
|
|
181
|
+
avbl--;
|
|
182
|
+
}
|
|
183
|
+
avbl = 2 * used;
|
|
184
|
+
dpth++;
|
|
185
|
+
used = 0;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/* Limits canonical Huffman code table's max code size. */
|
|
190
|
+
enum
|
|
191
|
+
{
|
|
192
|
+
TDEFL_MAX_SUPPORTED_HUFF_CODESIZE = 32
|
|
193
|
+
};
|
|
194
|
+
static void tdefl_huffman_enforce_max_code_size(int *pNum_codes, int code_list_len, int max_code_size)
|
|
195
|
+
{
|
|
196
|
+
int i;
|
|
197
|
+
mz_uint32 total = 0;
|
|
198
|
+
if (code_list_len <= 1)
|
|
199
|
+
return;
|
|
200
|
+
for (i = max_code_size + 1; i <= TDEFL_MAX_SUPPORTED_HUFF_CODESIZE; i++)
|
|
201
|
+
pNum_codes[max_code_size] += pNum_codes[i];
|
|
202
|
+
for (i = max_code_size; i > 0; i--)
|
|
203
|
+
total += (((mz_uint32)pNum_codes[i]) << (max_code_size - i));
|
|
204
|
+
while (total != (1UL << max_code_size))
|
|
205
|
+
{
|
|
206
|
+
pNum_codes[max_code_size]--;
|
|
207
|
+
for (i = max_code_size - 1; i > 0; i--)
|
|
208
|
+
if (pNum_codes[i])
|
|
209
|
+
{
|
|
210
|
+
pNum_codes[i]--;
|
|
211
|
+
pNum_codes[i + 1] += 2;
|
|
212
|
+
break;
|
|
213
|
+
}
|
|
214
|
+
total--;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
static void tdefl_optimize_huffman_table(tdefl_compressor *d, int table_num, int table_len, int code_size_limit, int static_table)
|
|
219
|
+
{
|
|
220
|
+
int i, j, l, num_codes[1 + TDEFL_MAX_SUPPORTED_HUFF_CODESIZE];
|
|
221
|
+
mz_uint next_code[TDEFL_MAX_SUPPORTED_HUFF_CODESIZE + 1];
|
|
222
|
+
MZ_CLEAR_ARR(num_codes);
|
|
223
|
+
if (static_table)
|
|
224
|
+
{
|
|
225
|
+
for (i = 0; i < table_len; i++)
|
|
226
|
+
num_codes[d->m_huff_code_sizes[table_num][i]]++;
|
|
227
|
+
}
|
|
228
|
+
else
|
|
229
|
+
{
|
|
230
|
+
tdefl_sym_freq syms0[TDEFL_MAX_HUFF_SYMBOLS], syms1[TDEFL_MAX_HUFF_SYMBOLS], *pSyms;
|
|
231
|
+
int num_used_syms = 0;
|
|
232
|
+
const mz_uint16 *pSym_count = &d->m_huff_count[table_num][0];
|
|
233
|
+
for (i = 0; i < table_len; i++)
|
|
234
|
+
if (pSym_count[i])
|
|
235
|
+
{
|
|
236
|
+
syms0[num_used_syms].m_key = (mz_uint16)pSym_count[i];
|
|
237
|
+
syms0[num_used_syms++].m_sym_index = (mz_uint16)i;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
pSyms = tdefl_radix_sort_syms(num_used_syms, syms0, syms1);
|
|
241
|
+
tdefl_calculate_minimum_redundancy(pSyms, num_used_syms);
|
|
242
|
+
|
|
243
|
+
for (i = 0; i < num_used_syms; i++)
|
|
244
|
+
num_codes[pSyms[i].m_key]++;
|
|
245
|
+
|
|
246
|
+
tdefl_huffman_enforce_max_code_size(num_codes, num_used_syms, code_size_limit);
|
|
247
|
+
|
|
248
|
+
MZ_CLEAR_ARR(d->m_huff_code_sizes[table_num]);
|
|
249
|
+
MZ_CLEAR_ARR(d->m_huff_codes[table_num]);
|
|
250
|
+
for (i = 1, j = num_used_syms; i <= code_size_limit; i++)
|
|
251
|
+
for (l = num_codes[i]; l > 0; l--)
|
|
252
|
+
d->m_huff_code_sizes[table_num][pSyms[--j].m_sym_index] = (mz_uint8)(i);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
next_code[1] = 0;
|
|
256
|
+
for (j = 0, i = 2; i <= code_size_limit; i++)
|
|
257
|
+
next_code[i] = j = ((j + num_codes[i - 1]) << 1);
|
|
258
|
+
|
|
259
|
+
for (i = 0; i < table_len; i++)
|
|
260
|
+
{
|
|
261
|
+
mz_uint rev_code = 0, code, code_size;
|
|
262
|
+
if ((code_size = d->m_huff_code_sizes[table_num][i]) == 0)
|
|
263
|
+
continue;
|
|
264
|
+
code = next_code[code_size]++;
|
|
265
|
+
for (l = code_size; l > 0; l--, code >>= 1)
|
|
266
|
+
rev_code = (rev_code << 1) | (code & 1);
|
|
267
|
+
d->m_huff_codes[table_num][i] = (mz_uint16)rev_code;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
#define TDEFL_PUT_BITS(b, l) \
|
|
272
|
+
do \
|
|
273
|
+
{ \
|
|
274
|
+
mz_uint bits = b; \
|
|
275
|
+
mz_uint len = l; \
|
|
276
|
+
MZ_ASSERT(bits <= ((1U << len) - 1U)); \
|
|
277
|
+
d->m_bit_buffer |= (bits << d->m_bits_in); \
|
|
278
|
+
d->m_bits_in += len; \
|
|
279
|
+
while (d->m_bits_in >= 8) \
|
|
280
|
+
{ \
|
|
281
|
+
if (d->m_pOutput_buf < d->m_pOutput_buf_end) \
|
|
282
|
+
*d->m_pOutput_buf++ = (mz_uint8)(d->m_bit_buffer); \
|
|
283
|
+
d->m_bit_buffer >>= 8; \
|
|
284
|
+
d->m_bits_in -= 8; \
|
|
285
|
+
} \
|
|
286
|
+
} \
|
|
287
|
+
MZ_MACRO_END
|
|
288
|
+
|
|
289
|
+
#define TDEFL_RLE_PREV_CODE_SIZE() \
|
|
290
|
+
{ \
|
|
291
|
+
if (rle_repeat_count) \
|
|
292
|
+
{ \
|
|
293
|
+
if (rle_repeat_count < 3) \
|
|
294
|
+
{ \
|
|
295
|
+
d->m_huff_count[2][prev_code_size] = (mz_uint16)(d->m_huff_count[2][prev_code_size] + rle_repeat_count); \
|
|
296
|
+
while (rle_repeat_count--) \
|
|
297
|
+
packed_code_sizes[num_packed_code_sizes++] = prev_code_size; \
|
|
298
|
+
} \
|
|
299
|
+
else \
|
|
300
|
+
{ \
|
|
301
|
+
d->m_huff_count[2][16] = (mz_uint16)(d->m_huff_count[2][16] + 1); \
|
|
302
|
+
packed_code_sizes[num_packed_code_sizes++] = 16; \
|
|
303
|
+
packed_code_sizes[num_packed_code_sizes++] = (mz_uint8)(rle_repeat_count - 3); \
|
|
304
|
+
} \
|
|
305
|
+
rle_repeat_count = 0; \
|
|
306
|
+
} \
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
#define TDEFL_RLE_ZERO_CODE_SIZE() \
|
|
310
|
+
{ \
|
|
311
|
+
if (rle_z_count) \
|
|
312
|
+
{ \
|
|
313
|
+
if (rle_z_count < 3) \
|
|
314
|
+
{ \
|
|
315
|
+
d->m_huff_count[2][0] = (mz_uint16)(d->m_huff_count[2][0] + rle_z_count); \
|
|
316
|
+
while (rle_z_count--) \
|
|
317
|
+
packed_code_sizes[num_packed_code_sizes++] = 0; \
|
|
318
|
+
} \
|
|
319
|
+
else if (rle_z_count <= 10) \
|
|
320
|
+
{ \
|
|
321
|
+
d->m_huff_count[2][17] = (mz_uint16)(d->m_huff_count[2][17] + 1); \
|
|
322
|
+
packed_code_sizes[num_packed_code_sizes++] = 17; \
|
|
323
|
+
packed_code_sizes[num_packed_code_sizes++] = (mz_uint8)(rle_z_count - 3); \
|
|
324
|
+
} \
|
|
325
|
+
else \
|
|
326
|
+
{ \
|
|
327
|
+
d->m_huff_count[2][18] = (mz_uint16)(d->m_huff_count[2][18] + 1); \
|
|
328
|
+
packed_code_sizes[num_packed_code_sizes++] = 18; \
|
|
329
|
+
packed_code_sizes[num_packed_code_sizes++] = (mz_uint8)(rle_z_count - 11); \
|
|
330
|
+
} \
|
|
331
|
+
rle_z_count = 0; \
|
|
332
|
+
} \
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
static const mz_uint8 s_tdefl_packed_code_size_syms_swizzle[] = { 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 };
|
|
336
|
+
|
|
337
|
+
static void tdefl_start_dynamic_block(tdefl_compressor *d)
|
|
338
|
+
{
|
|
339
|
+
int num_lit_codes, num_dist_codes, num_bit_lengths;
|
|
340
|
+
mz_uint i, total_code_sizes_to_pack, num_packed_code_sizes, rle_z_count, rle_repeat_count, packed_code_sizes_index;
|
|
341
|
+
mz_uint8 code_sizes_to_pack[TDEFL_MAX_HUFF_SYMBOLS_0 + TDEFL_MAX_HUFF_SYMBOLS_1], packed_code_sizes[TDEFL_MAX_HUFF_SYMBOLS_0 + TDEFL_MAX_HUFF_SYMBOLS_1], prev_code_size = 0xFF;
|
|
342
|
+
|
|
343
|
+
d->m_huff_count[0][256] = 1;
|
|
344
|
+
|
|
345
|
+
tdefl_optimize_huffman_table(d, 0, TDEFL_MAX_HUFF_SYMBOLS_0, 15, MZ_FALSE);
|
|
346
|
+
tdefl_optimize_huffman_table(d, 1, TDEFL_MAX_HUFF_SYMBOLS_1, 15, MZ_FALSE);
|
|
347
|
+
|
|
348
|
+
for (num_lit_codes = 286; num_lit_codes > 257; num_lit_codes--)
|
|
349
|
+
if (d->m_huff_code_sizes[0][num_lit_codes - 1])
|
|
350
|
+
break;
|
|
351
|
+
for (num_dist_codes = 30; num_dist_codes > 1; num_dist_codes--)
|
|
352
|
+
if (d->m_huff_code_sizes[1][num_dist_codes - 1])
|
|
353
|
+
break;
|
|
354
|
+
|
|
355
|
+
memcpy(code_sizes_to_pack, &d->m_huff_code_sizes[0][0], num_lit_codes);
|
|
356
|
+
memcpy(code_sizes_to_pack + num_lit_codes, &d->m_huff_code_sizes[1][0], num_dist_codes);
|
|
357
|
+
total_code_sizes_to_pack = num_lit_codes + num_dist_codes;
|
|
358
|
+
num_packed_code_sizes = 0;
|
|
359
|
+
rle_z_count = 0;
|
|
360
|
+
rle_repeat_count = 0;
|
|
361
|
+
|
|
362
|
+
memset(&d->m_huff_count[2][0], 0, sizeof(d->m_huff_count[2][0]) * TDEFL_MAX_HUFF_SYMBOLS_2);
|
|
363
|
+
for (i = 0; i < total_code_sizes_to_pack; i++)
|
|
364
|
+
{
|
|
365
|
+
mz_uint8 code_size = code_sizes_to_pack[i];
|
|
366
|
+
if (!code_size)
|
|
367
|
+
{
|
|
368
|
+
TDEFL_RLE_PREV_CODE_SIZE();
|
|
369
|
+
if (++rle_z_count == 138)
|
|
370
|
+
{
|
|
371
|
+
TDEFL_RLE_ZERO_CODE_SIZE();
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
else
|
|
375
|
+
{
|
|
376
|
+
TDEFL_RLE_ZERO_CODE_SIZE();
|
|
377
|
+
if (code_size != prev_code_size)
|
|
378
|
+
{
|
|
379
|
+
TDEFL_RLE_PREV_CODE_SIZE();
|
|
380
|
+
d->m_huff_count[2][code_size] = (mz_uint16)(d->m_huff_count[2][code_size] + 1);
|
|
381
|
+
packed_code_sizes[num_packed_code_sizes++] = code_size;
|
|
382
|
+
}
|
|
383
|
+
else if (++rle_repeat_count == 6)
|
|
384
|
+
{
|
|
385
|
+
TDEFL_RLE_PREV_CODE_SIZE();
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
prev_code_size = code_size;
|
|
389
|
+
}
|
|
390
|
+
if (rle_repeat_count)
|
|
391
|
+
{
|
|
392
|
+
TDEFL_RLE_PREV_CODE_SIZE();
|
|
393
|
+
}
|
|
394
|
+
else
|
|
395
|
+
{
|
|
396
|
+
TDEFL_RLE_ZERO_CODE_SIZE();
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
tdefl_optimize_huffman_table(d, 2, TDEFL_MAX_HUFF_SYMBOLS_2, 7, MZ_FALSE);
|
|
400
|
+
|
|
401
|
+
TDEFL_PUT_BITS(2, 2);
|
|
402
|
+
|
|
403
|
+
TDEFL_PUT_BITS(num_lit_codes - 257, 5);
|
|
404
|
+
TDEFL_PUT_BITS(num_dist_codes - 1, 5);
|
|
405
|
+
|
|
406
|
+
for (num_bit_lengths = 18; num_bit_lengths >= 0; num_bit_lengths--)
|
|
407
|
+
if (d->m_huff_code_sizes[2][s_tdefl_packed_code_size_syms_swizzle[num_bit_lengths]])
|
|
408
|
+
break;
|
|
409
|
+
num_bit_lengths = MZ_MAX(4, (num_bit_lengths + 1));
|
|
410
|
+
TDEFL_PUT_BITS(num_bit_lengths - 4, 4);
|
|
411
|
+
for (i = 0; (int)i < num_bit_lengths; i++)
|
|
412
|
+
TDEFL_PUT_BITS(d->m_huff_code_sizes[2][s_tdefl_packed_code_size_syms_swizzle[i]], 3);
|
|
413
|
+
|
|
414
|
+
for (packed_code_sizes_index = 0; packed_code_sizes_index < num_packed_code_sizes;)
|
|
415
|
+
{
|
|
416
|
+
mz_uint code = packed_code_sizes[packed_code_sizes_index++];
|
|
417
|
+
MZ_ASSERT(code < TDEFL_MAX_HUFF_SYMBOLS_2);
|
|
418
|
+
TDEFL_PUT_BITS(d->m_huff_codes[2][code], d->m_huff_code_sizes[2][code]);
|
|
419
|
+
if (code >= 16)
|
|
420
|
+
TDEFL_PUT_BITS(packed_code_sizes[packed_code_sizes_index++], "\02\03\07"[code - 16]);
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
static void tdefl_start_static_block(tdefl_compressor *d)
|
|
425
|
+
{
|
|
426
|
+
mz_uint i;
|
|
427
|
+
mz_uint8 *p = &d->m_huff_code_sizes[0][0];
|
|
428
|
+
|
|
429
|
+
for (i = 0; i <= 143; ++i)
|
|
430
|
+
*p++ = 8;
|
|
431
|
+
for (; i <= 255; ++i)
|
|
432
|
+
*p++ = 9;
|
|
433
|
+
for (; i <= 279; ++i)
|
|
434
|
+
*p++ = 7;
|
|
435
|
+
for (; i <= 287; ++i)
|
|
436
|
+
*p++ = 8;
|
|
437
|
+
|
|
438
|
+
memset(d->m_huff_code_sizes[1], 5, 32);
|
|
439
|
+
|
|
440
|
+
tdefl_optimize_huffman_table(d, 0, 288, 15, MZ_TRUE);
|
|
441
|
+
tdefl_optimize_huffman_table(d, 1, 32, 15, MZ_TRUE);
|
|
442
|
+
|
|
443
|
+
TDEFL_PUT_BITS(1, 2);
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
static const mz_uint mz_bitmasks[17] = { 0x0000, 0x0001, 0x0003, 0x0007, 0x000F, 0x001F, 0x003F, 0x007F, 0x00FF, 0x01FF, 0x03FF, 0x07FF, 0x0FFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF };
|
|
447
|
+
|
|
448
|
+
#if MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN && MINIZ_HAS_64BIT_REGISTERS
|
|
449
|
+
static mz_bool tdefl_compress_lz_codes(tdefl_compressor *d)
|
|
450
|
+
{
|
|
451
|
+
mz_uint flags;
|
|
452
|
+
mz_uint8 *pLZ_codes;
|
|
453
|
+
mz_uint8 *pOutput_buf = d->m_pOutput_buf;
|
|
454
|
+
mz_uint8 *pLZ_code_buf_end = d->m_pLZ_code_buf;
|
|
455
|
+
mz_uint64 bit_buffer = d->m_bit_buffer;
|
|
456
|
+
mz_uint bits_in = d->m_bits_in;
|
|
457
|
+
|
|
458
|
+
#define TDEFL_PUT_BITS_FAST(b, l) \
|
|
459
|
+
{ \
|
|
460
|
+
bit_buffer |= (((mz_uint64)(b)) << bits_in); \
|
|
461
|
+
bits_in += (l); \
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
flags = 1;
|
|
465
|
+
for (pLZ_codes = d->m_lz_code_buf; pLZ_codes < pLZ_code_buf_end; flags >>= 1)
|
|
466
|
+
{
|
|
467
|
+
if (flags == 1)
|
|
468
|
+
flags = *pLZ_codes++ | 0x100;
|
|
469
|
+
|
|
470
|
+
if (flags & 1)
|
|
471
|
+
{
|
|
472
|
+
mz_uint s0, s1, n0, n1, sym, num_extra_bits;
|
|
473
|
+
mz_uint match_len = pLZ_codes[0];
|
|
474
|
+
mz_uint match_dist = (pLZ_codes[1] | (pLZ_codes[2] << 8));
|
|
475
|
+
pLZ_codes += 3;
|
|
476
|
+
|
|
477
|
+
MZ_ASSERT(d->m_huff_code_sizes[0][s_tdefl_len_sym[match_len]]);
|
|
478
|
+
TDEFL_PUT_BITS_FAST(d->m_huff_codes[0][s_tdefl_len_sym[match_len]], d->m_huff_code_sizes[0][s_tdefl_len_sym[match_len]]);
|
|
479
|
+
TDEFL_PUT_BITS_FAST(match_len & mz_bitmasks[s_tdefl_len_extra[match_len]], s_tdefl_len_extra[match_len]);
|
|
480
|
+
|
|
481
|
+
/* This sequence coaxes MSVC into using cmov's vs. jmp's. */
|
|
482
|
+
s0 = s_tdefl_small_dist_sym[match_dist & 511];
|
|
483
|
+
n0 = s_tdefl_small_dist_extra[match_dist & 511];
|
|
484
|
+
s1 = s_tdefl_large_dist_sym[match_dist >> 8];
|
|
485
|
+
n1 = s_tdefl_large_dist_extra[match_dist >> 8];
|
|
486
|
+
sym = (match_dist < 512) ? s0 : s1;
|
|
487
|
+
num_extra_bits = (match_dist < 512) ? n0 : n1;
|
|
488
|
+
|
|
489
|
+
MZ_ASSERT(d->m_huff_code_sizes[1][sym]);
|
|
490
|
+
TDEFL_PUT_BITS_FAST(d->m_huff_codes[1][sym], d->m_huff_code_sizes[1][sym]);
|
|
491
|
+
TDEFL_PUT_BITS_FAST(match_dist & mz_bitmasks[num_extra_bits], num_extra_bits);
|
|
492
|
+
}
|
|
493
|
+
else
|
|
494
|
+
{
|
|
495
|
+
mz_uint lit = *pLZ_codes++;
|
|
496
|
+
MZ_ASSERT(d->m_huff_code_sizes[0][lit]);
|
|
497
|
+
TDEFL_PUT_BITS_FAST(d->m_huff_codes[0][lit], d->m_huff_code_sizes[0][lit]);
|
|
498
|
+
|
|
499
|
+
if (((flags & 2) == 0) && (pLZ_codes < pLZ_code_buf_end))
|
|
500
|
+
{
|
|
501
|
+
flags >>= 1;
|
|
502
|
+
lit = *pLZ_codes++;
|
|
503
|
+
MZ_ASSERT(d->m_huff_code_sizes[0][lit]);
|
|
504
|
+
TDEFL_PUT_BITS_FAST(d->m_huff_codes[0][lit], d->m_huff_code_sizes[0][lit]);
|
|
505
|
+
|
|
506
|
+
if (((flags & 2) == 0) && (pLZ_codes < pLZ_code_buf_end))
|
|
507
|
+
{
|
|
508
|
+
flags >>= 1;
|
|
509
|
+
lit = *pLZ_codes++;
|
|
510
|
+
MZ_ASSERT(d->m_huff_code_sizes[0][lit]);
|
|
511
|
+
TDEFL_PUT_BITS_FAST(d->m_huff_codes[0][lit], d->m_huff_code_sizes[0][lit]);
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
if (pOutput_buf >= d->m_pOutput_buf_end)
|
|
517
|
+
return MZ_FALSE;
|
|
518
|
+
|
|
519
|
+
memcpy(pOutput_buf, &bit_buffer, sizeof(mz_uint64));
|
|
520
|
+
pOutput_buf += (bits_in >> 3);
|
|
521
|
+
bit_buffer >>= (bits_in & ~7);
|
|
522
|
+
bits_in &= 7;
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
#undef TDEFL_PUT_BITS_FAST
|
|
526
|
+
|
|
527
|
+
d->m_pOutput_buf = pOutput_buf;
|
|
528
|
+
d->m_bits_in = 0;
|
|
529
|
+
d->m_bit_buffer = 0;
|
|
530
|
+
|
|
531
|
+
while (bits_in)
|
|
532
|
+
{
|
|
533
|
+
mz_uint32 n = MZ_MIN(bits_in, 16);
|
|
534
|
+
TDEFL_PUT_BITS((mz_uint)bit_buffer & mz_bitmasks[n], n);
|
|
535
|
+
bit_buffer >>= n;
|
|
536
|
+
bits_in -= n;
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
TDEFL_PUT_BITS(d->m_huff_codes[0][256], d->m_huff_code_sizes[0][256]);
|
|
540
|
+
|
|
541
|
+
return (d->m_pOutput_buf < d->m_pOutput_buf_end);
|
|
542
|
+
}
|
|
543
|
+
#else
|
|
544
|
+
static mz_bool tdefl_compress_lz_codes(tdefl_compressor *d)
|
|
545
|
+
{
|
|
546
|
+
mz_uint flags;
|
|
547
|
+
mz_uint8 *pLZ_codes;
|
|
548
|
+
|
|
549
|
+
flags = 1;
|
|
550
|
+
for (pLZ_codes = d->m_lz_code_buf; pLZ_codes < d->m_pLZ_code_buf; flags >>= 1)
|
|
551
|
+
{
|
|
552
|
+
if (flags == 1)
|
|
553
|
+
flags = *pLZ_codes++ | 0x100;
|
|
554
|
+
if (flags & 1)
|
|
555
|
+
{
|
|
556
|
+
mz_uint sym, num_extra_bits;
|
|
557
|
+
mz_uint match_len = pLZ_codes[0], match_dist = (pLZ_codes[1] | (pLZ_codes[2] << 8));
|
|
558
|
+
pLZ_codes += 3;
|
|
559
|
+
|
|
560
|
+
MZ_ASSERT(d->m_huff_code_sizes[0][s_tdefl_len_sym[match_len]]);
|
|
561
|
+
TDEFL_PUT_BITS(d->m_huff_codes[0][s_tdefl_len_sym[match_len]], d->m_huff_code_sizes[0][s_tdefl_len_sym[match_len]]);
|
|
562
|
+
TDEFL_PUT_BITS(match_len & mz_bitmasks[s_tdefl_len_extra[match_len]], s_tdefl_len_extra[match_len]);
|
|
563
|
+
|
|
564
|
+
if (match_dist < 512)
|
|
565
|
+
{
|
|
566
|
+
sym = s_tdefl_small_dist_sym[match_dist];
|
|
567
|
+
num_extra_bits = s_tdefl_small_dist_extra[match_dist];
|
|
568
|
+
}
|
|
569
|
+
else
|
|
570
|
+
{
|
|
571
|
+
sym = s_tdefl_large_dist_sym[match_dist >> 8];
|
|
572
|
+
num_extra_bits = s_tdefl_large_dist_extra[match_dist >> 8];
|
|
573
|
+
}
|
|
574
|
+
MZ_ASSERT(d->m_huff_code_sizes[1][sym]);
|
|
575
|
+
TDEFL_PUT_BITS(d->m_huff_codes[1][sym], d->m_huff_code_sizes[1][sym]);
|
|
576
|
+
TDEFL_PUT_BITS(match_dist & mz_bitmasks[num_extra_bits], num_extra_bits);
|
|
577
|
+
}
|
|
578
|
+
else
|
|
579
|
+
{
|
|
580
|
+
mz_uint lit = *pLZ_codes++;
|
|
581
|
+
MZ_ASSERT(d->m_huff_code_sizes[0][lit]);
|
|
582
|
+
TDEFL_PUT_BITS(d->m_huff_codes[0][lit], d->m_huff_code_sizes[0][lit]);
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
TDEFL_PUT_BITS(d->m_huff_codes[0][256], d->m_huff_code_sizes[0][256]);
|
|
587
|
+
|
|
588
|
+
return (d->m_pOutput_buf < d->m_pOutput_buf_end);
|
|
589
|
+
}
|
|
590
|
+
#endif /* MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN && MINIZ_HAS_64BIT_REGISTERS */
|
|
591
|
+
|
|
592
|
+
static mz_bool tdefl_compress_block(tdefl_compressor *d, mz_bool static_block)
|
|
593
|
+
{
|
|
594
|
+
if (static_block)
|
|
595
|
+
tdefl_start_static_block(d);
|
|
596
|
+
else
|
|
597
|
+
tdefl_start_dynamic_block(d);
|
|
598
|
+
return tdefl_compress_lz_codes(d);
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
static const mz_uint s_tdefl_num_probes[11] = { 0, 1, 6, 32, 16, 32, 128, 256, 512, 768, 1500 };
|
|
602
|
+
|
|
603
|
+
static int tdefl_flush_block(tdefl_compressor *d, int flush)
|
|
604
|
+
{
|
|
605
|
+
mz_uint saved_bit_buf, saved_bits_in;
|
|
606
|
+
mz_uint8 *pSaved_output_buf;
|
|
607
|
+
mz_bool comp_block_succeeded = MZ_FALSE;
|
|
608
|
+
int n, use_raw_block = ((d->m_flags & TDEFL_FORCE_ALL_RAW_BLOCKS) != 0) && (d->m_lookahead_pos - d->m_lz_code_buf_dict_pos) <= d->m_dict_size;
|
|
609
|
+
mz_uint8 *pOutput_buf_start = ((d->m_pPut_buf_func == NULL) && ((*d->m_pOut_buf_size - d->m_out_buf_ofs) >= TDEFL_OUT_BUF_SIZE)) ? ((mz_uint8 *)d->m_pOut_buf + d->m_out_buf_ofs) : d->m_output_buf;
|
|
610
|
+
|
|
611
|
+
d->m_pOutput_buf = pOutput_buf_start;
|
|
612
|
+
d->m_pOutput_buf_end = d->m_pOutput_buf + TDEFL_OUT_BUF_SIZE - 16;
|
|
613
|
+
|
|
614
|
+
MZ_ASSERT(!d->m_output_flush_remaining);
|
|
615
|
+
d->m_output_flush_ofs = 0;
|
|
616
|
+
d->m_output_flush_remaining = 0;
|
|
617
|
+
|
|
618
|
+
*d->m_pLZ_flags = (mz_uint8)(*d->m_pLZ_flags >> d->m_num_flags_left);
|
|
619
|
+
d->m_pLZ_code_buf -= (d->m_num_flags_left == 8);
|
|
620
|
+
|
|
621
|
+
if ((d->m_flags & TDEFL_WRITE_ZLIB_HEADER) && (!d->m_block_index))
|
|
622
|
+
{
|
|
623
|
+
const mz_uint8 cmf = 0x78;
|
|
624
|
+
mz_uint8 flg, flevel = 3;
|
|
625
|
+
mz_uint header, i, mz_un = sizeof(s_tdefl_num_probes) / sizeof(mz_uint);
|
|
626
|
+
|
|
627
|
+
/* Determine compression level by reversing the process in tdefl_create_comp_flags_from_zip_params() */
|
|
628
|
+
for (i = 0; i < mz_un; i++)
|
|
629
|
+
if (s_tdefl_num_probes[i] == (d->m_flags & 0xFFF))
|
|
630
|
+
break;
|
|
631
|
+
|
|
632
|
+
if (i < 2)
|
|
633
|
+
flevel = 0;
|
|
634
|
+
else if (i < 6)
|
|
635
|
+
flevel = 1;
|
|
636
|
+
else if (i == 6)
|
|
637
|
+
flevel = 2;
|
|
638
|
+
|
|
639
|
+
header = cmf << 8 | (flevel << 6);
|
|
640
|
+
header += 31 - (header % 31);
|
|
641
|
+
flg = header & 0xFF;
|
|
642
|
+
|
|
643
|
+
TDEFL_PUT_BITS(cmf, 8);
|
|
644
|
+
TDEFL_PUT_BITS(flg, 8);
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
TDEFL_PUT_BITS(flush == TDEFL_FINISH, 1);
|
|
648
|
+
|
|
649
|
+
pSaved_output_buf = d->m_pOutput_buf;
|
|
650
|
+
saved_bit_buf = d->m_bit_buffer;
|
|
651
|
+
saved_bits_in = d->m_bits_in;
|
|
652
|
+
|
|
653
|
+
if (!use_raw_block)
|
|
654
|
+
comp_block_succeeded = tdefl_compress_block(d, (d->m_flags & TDEFL_FORCE_ALL_STATIC_BLOCKS) || (d->m_total_lz_bytes < 48));
|
|
655
|
+
|
|
656
|
+
/* If the block gets expanded, forget the current contents of the output buffer and send a raw block instead. */
|
|
657
|
+
if (((use_raw_block) || ((d->m_total_lz_bytes) && ((d->m_pOutput_buf - pSaved_output_buf + 1U) >= d->m_total_lz_bytes))) &&
|
|
658
|
+
((d->m_lookahead_pos - d->m_lz_code_buf_dict_pos) <= d->m_dict_size))
|
|
659
|
+
{
|
|
660
|
+
mz_uint i;
|
|
661
|
+
d->m_pOutput_buf = pSaved_output_buf;
|
|
662
|
+
d->m_bit_buffer = saved_bit_buf, d->m_bits_in = saved_bits_in;
|
|
663
|
+
TDEFL_PUT_BITS(0, 2);
|
|
664
|
+
if (d->m_bits_in)
|
|
665
|
+
{
|
|
666
|
+
TDEFL_PUT_BITS(0, 8 - d->m_bits_in);
|
|
667
|
+
}
|
|
668
|
+
for (i = 2; i; --i, d->m_total_lz_bytes ^= 0xFFFF)
|
|
669
|
+
{
|
|
670
|
+
TDEFL_PUT_BITS(d->m_total_lz_bytes & 0xFFFF, 16);
|
|
671
|
+
}
|
|
672
|
+
for (i = 0; i < d->m_total_lz_bytes; ++i)
|
|
673
|
+
{
|
|
674
|
+
TDEFL_PUT_BITS(d->m_dict[(d->m_lz_code_buf_dict_pos + i) & TDEFL_LZ_DICT_SIZE_MASK], 8);
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
/* Check for the extremely unlikely (if not impossible) case of the compressed block not fitting into the output buffer when using dynamic codes. */
|
|
678
|
+
else if (!comp_block_succeeded)
|
|
679
|
+
{
|
|
680
|
+
d->m_pOutput_buf = pSaved_output_buf;
|
|
681
|
+
d->m_bit_buffer = saved_bit_buf, d->m_bits_in = saved_bits_in;
|
|
682
|
+
tdefl_compress_block(d, MZ_TRUE);
|
|
683
|
+
}
|
|
684
|
+
|
|
685
|
+
if (flush)
|
|
686
|
+
{
|
|
687
|
+
if (flush == TDEFL_FINISH)
|
|
688
|
+
{
|
|
689
|
+
if (d->m_bits_in)
|
|
690
|
+
{
|
|
691
|
+
TDEFL_PUT_BITS(0, 8 - d->m_bits_in);
|
|
692
|
+
}
|
|
693
|
+
if (d->m_flags & TDEFL_WRITE_ZLIB_HEADER)
|
|
694
|
+
{
|
|
695
|
+
mz_uint i, a = d->m_adler32;
|
|
696
|
+
for (i = 0; i < 4; i++)
|
|
697
|
+
{
|
|
698
|
+
TDEFL_PUT_BITS((a >> 24) & 0xFF, 8);
|
|
699
|
+
a <<= 8;
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
else
|
|
704
|
+
{
|
|
705
|
+
mz_uint i, z = 0;
|
|
706
|
+
TDEFL_PUT_BITS(0, 3);
|
|
707
|
+
if (d->m_bits_in)
|
|
708
|
+
{
|
|
709
|
+
TDEFL_PUT_BITS(0, 8 - d->m_bits_in);
|
|
710
|
+
}
|
|
711
|
+
for (i = 2; i; --i, z ^= 0xFFFF)
|
|
712
|
+
{
|
|
713
|
+
TDEFL_PUT_BITS(z & 0xFFFF, 16);
|
|
714
|
+
}
|
|
715
|
+
}
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
MZ_ASSERT(d->m_pOutput_buf < d->m_pOutput_buf_end);
|
|
719
|
+
|
|
720
|
+
memset(&d->m_huff_count[0][0], 0, sizeof(d->m_huff_count[0][0]) * TDEFL_MAX_HUFF_SYMBOLS_0);
|
|
721
|
+
memset(&d->m_huff_count[1][0], 0, sizeof(d->m_huff_count[1][0]) * TDEFL_MAX_HUFF_SYMBOLS_1);
|
|
722
|
+
|
|
723
|
+
d->m_pLZ_code_buf = d->m_lz_code_buf + 1;
|
|
724
|
+
d->m_pLZ_flags = d->m_lz_code_buf;
|
|
725
|
+
d->m_num_flags_left = 8;
|
|
726
|
+
d->m_lz_code_buf_dict_pos += d->m_total_lz_bytes;
|
|
727
|
+
d->m_total_lz_bytes = 0;
|
|
728
|
+
d->m_block_index++;
|
|
729
|
+
|
|
730
|
+
if ((n = (int)(d->m_pOutput_buf - pOutput_buf_start)) != 0)
|
|
731
|
+
{
|
|
732
|
+
if (d->m_pPut_buf_func)
|
|
733
|
+
{
|
|
734
|
+
*d->m_pIn_buf_size = d->m_pSrc - (const mz_uint8 *)d->m_pIn_buf;
|
|
735
|
+
if (!(*d->m_pPut_buf_func)(d->m_output_buf, n, d->m_pPut_buf_user))
|
|
736
|
+
return (d->m_prev_return_status = TDEFL_STATUS_PUT_BUF_FAILED);
|
|
737
|
+
}
|
|
738
|
+
else if (pOutput_buf_start == d->m_output_buf)
|
|
739
|
+
{
|
|
740
|
+
int bytes_to_copy = (int)MZ_MIN((size_t)n, (size_t)(*d->m_pOut_buf_size - d->m_out_buf_ofs));
|
|
741
|
+
memcpy((mz_uint8 *)d->m_pOut_buf + d->m_out_buf_ofs, d->m_output_buf, bytes_to_copy);
|
|
742
|
+
d->m_out_buf_ofs += bytes_to_copy;
|
|
743
|
+
if ((n -= bytes_to_copy) != 0)
|
|
744
|
+
{
|
|
745
|
+
d->m_output_flush_ofs = bytes_to_copy;
|
|
746
|
+
d->m_output_flush_remaining = n;
|
|
747
|
+
}
|
|
748
|
+
}
|
|
749
|
+
else
|
|
750
|
+
{
|
|
751
|
+
d->m_out_buf_ofs += n;
|
|
752
|
+
}
|
|
753
|
+
}
|
|
754
|
+
|
|
755
|
+
return d->m_output_flush_remaining;
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
#if MINIZ_USE_UNALIGNED_LOADS_AND_STORES
|
|
759
|
+
#ifdef MINIZ_UNALIGNED_USE_MEMCPY
|
|
760
|
+
static mz_uint16 TDEFL_READ_UNALIGNED_WORD(const mz_uint8 *p)
|
|
761
|
+
{
|
|
762
|
+
mz_uint16 ret;
|
|
763
|
+
memcpy(&ret, p, sizeof(mz_uint16));
|
|
764
|
+
return ret;
|
|
765
|
+
}
|
|
766
|
+
static mz_uint16 TDEFL_READ_UNALIGNED_WORD2(const mz_uint16 *p)
|
|
767
|
+
{
|
|
768
|
+
mz_uint16 ret;
|
|
769
|
+
memcpy(&ret, p, sizeof(mz_uint16));
|
|
770
|
+
return ret;
|
|
771
|
+
}
|
|
772
|
+
#else
|
|
773
|
+
#define TDEFL_READ_UNALIGNED_WORD(p) *(const mz_uint16 *)(p)
|
|
774
|
+
#define TDEFL_READ_UNALIGNED_WORD2(p) *(const mz_uint16 *)(p)
|
|
775
|
+
#endif
|
|
776
|
+
static MZ_FORCEINLINE void tdefl_find_match(tdefl_compressor *d, mz_uint lookahead_pos, mz_uint max_dist, mz_uint max_match_len, mz_uint *pMatch_dist, mz_uint *pMatch_len)
|
|
777
|
+
{
|
|
778
|
+
mz_uint dist, pos = lookahead_pos & TDEFL_LZ_DICT_SIZE_MASK, match_len = *pMatch_len, probe_pos = pos, next_probe_pos, probe_len;
|
|
779
|
+
mz_uint num_probes_left = d->m_max_probes[match_len >= 32];
|
|
780
|
+
const mz_uint16 *s = (const mz_uint16 *)(d->m_dict + pos), *p, *q;
|
|
781
|
+
mz_uint16 c01 = TDEFL_READ_UNALIGNED_WORD(&d->m_dict[pos + match_len - 1]), s01 = TDEFL_READ_UNALIGNED_WORD2(s);
|
|
782
|
+
MZ_ASSERT(max_match_len <= TDEFL_MAX_MATCH_LEN);
|
|
783
|
+
if (max_match_len <= match_len)
|
|
784
|
+
return;
|
|
785
|
+
for (;;)
|
|
786
|
+
{
|
|
787
|
+
for (;;)
|
|
788
|
+
{
|
|
789
|
+
if (--num_probes_left == 0)
|
|
790
|
+
return;
|
|
791
|
+
#define TDEFL_PROBE \
|
|
792
|
+
next_probe_pos = d->m_next[probe_pos]; \
|
|
793
|
+
if ((!next_probe_pos) || ((dist = (mz_uint16)(lookahead_pos - next_probe_pos)) > max_dist)) \
|
|
794
|
+
return; \
|
|
795
|
+
probe_pos = next_probe_pos & TDEFL_LZ_DICT_SIZE_MASK; \
|
|
796
|
+
if (TDEFL_READ_UNALIGNED_WORD(&d->m_dict[probe_pos + match_len - 1]) == c01) \
|
|
797
|
+
break;
|
|
798
|
+
TDEFL_PROBE;
|
|
799
|
+
TDEFL_PROBE;
|
|
800
|
+
TDEFL_PROBE;
|
|
801
|
+
}
|
|
802
|
+
if (!dist)
|
|
803
|
+
break;
|
|
804
|
+
q = (const mz_uint16 *)(d->m_dict + probe_pos);
|
|
805
|
+
if (TDEFL_READ_UNALIGNED_WORD2(q) != s01)
|
|
806
|
+
continue;
|
|
807
|
+
p = s;
|
|
808
|
+
probe_len = 32;
|
|
809
|
+
do
|
|
810
|
+
{
|
|
811
|
+
} while ((TDEFL_READ_UNALIGNED_WORD2(++p) == TDEFL_READ_UNALIGNED_WORD2(++q)) && (TDEFL_READ_UNALIGNED_WORD2(++p) == TDEFL_READ_UNALIGNED_WORD2(++q)) &&
|
|
812
|
+
(TDEFL_READ_UNALIGNED_WORD2(++p) == TDEFL_READ_UNALIGNED_WORD2(++q)) && (TDEFL_READ_UNALIGNED_WORD2(++p) == TDEFL_READ_UNALIGNED_WORD2(++q)) && (--probe_len > 0));
|
|
813
|
+
if (!probe_len)
|
|
814
|
+
{
|
|
815
|
+
*pMatch_dist = dist;
|
|
816
|
+
*pMatch_len = MZ_MIN(max_match_len, (mz_uint)TDEFL_MAX_MATCH_LEN);
|
|
817
|
+
break;
|
|
818
|
+
}
|
|
819
|
+
else if ((probe_len = ((mz_uint)(p - s) * 2) + (mz_uint)(*(const mz_uint8 *)p == *(const mz_uint8 *)q)) > match_len)
|
|
820
|
+
{
|
|
821
|
+
*pMatch_dist = dist;
|
|
822
|
+
if ((*pMatch_len = match_len = MZ_MIN(max_match_len, probe_len)) == max_match_len)
|
|
823
|
+
break;
|
|
824
|
+
c01 = TDEFL_READ_UNALIGNED_WORD(&d->m_dict[pos + match_len - 1]);
|
|
825
|
+
}
|
|
826
|
+
}
|
|
827
|
+
}
|
|
828
|
+
#else
|
|
829
|
+
static MZ_FORCEINLINE void tdefl_find_match(tdefl_compressor *d, mz_uint lookahead_pos, mz_uint max_dist, mz_uint max_match_len, mz_uint *pMatch_dist, mz_uint *pMatch_len)
|
|
830
|
+
{
|
|
831
|
+
mz_uint dist, pos = lookahead_pos & TDEFL_LZ_DICT_SIZE_MASK, match_len = *pMatch_len, probe_pos = pos, next_probe_pos, probe_len;
|
|
832
|
+
mz_uint num_probes_left = d->m_max_probes[match_len >= 32];
|
|
833
|
+
const mz_uint8 *s = d->m_dict + pos, *p, *q;
|
|
834
|
+
mz_uint8 c0 = d->m_dict[pos + match_len], c1 = d->m_dict[pos + match_len - 1];
|
|
835
|
+
MZ_ASSERT(max_match_len <= TDEFL_MAX_MATCH_LEN);
|
|
836
|
+
if (max_match_len <= match_len)
|
|
837
|
+
return;
|
|
838
|
+
for (;;)
|
|
839
|
+
{
|
|
840
|
+
for (;;)
|
|
841
|
+
{
|
|
842
|
+
if (--num_probes_left == 0)
|
|
843
|
+
return;
|
|
844
|
+
#define TDEFL_PROBE \
|
|
845
|
+
next_probe_pos = d->m_next[probe_pos]; \
|
|
846
|
+
if ((!next_probe_pos) || ((dist = (mz_uint16)(lookahead_pos - next_probe_pos)) > max_dist)) \
|
|
847
|
+
return; \
|
|
848
|
+
probe_pos = next_probe_pos & TDEFL_LZ_DICT_SIZE_MASK; \
|
|
849
|
+
if ((d->m_dict[probe_pos + match_len] == c0) && (d->m_dict[probe_pos + match_len - 1] == c1)) \
|
|
850
|
+
break;
|
|
851
|
+
TDEFL_PROBE;
|
|
852
|
+
TDEFL_PROBE;
|
|
853
|
+
TDEFL_PROBE;
|
|
854
|
+
}
|
|
855
|
+
if (!dist)
|
|
856
|
+
break;
|
|
857
|
+
p = s;
|
|
858
|
+
q = d->m_dict + probe_pos;
|
|
859
|
+
for (probe_len = 0; probe_len < max_match_len; probe_len++)
|
|
860
|
+
if (*p++ != *q++)
|
|
861
|
+
break;
|
|
862
|
+
if (probe_len > match_len)
|
|
863
|
+
{
|
|
864
|
+
*pMatch_dist = dist;
|
|
865
|
+
if ((*pMatch_len = match_len = probe_len) == max_match_len)
|
|
866
|
+
return;
|
|
867
|
+
c0 = d->m_dict[pos + match_len];
|
|
868
|
+
c1 = d->m_dict[pos + match_len - 1];
|
|
869
|
+
}
|
|
870
|
+
}
|
|
871
|
+
}
|
|
872
|
+
#endif /* #if MINIZ_USE_UNALIGNED_LOADS_AND_STORES */
|
|
873
|
+
|
|
874
|
+
#if MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN
|
|
875
|
+
#ifdef MINIZ_UNALIGNED_USE_MEMCPY
|
|
876
|
+
static mz_uint32 TDEFL_READ_UNALIGNED_WORD32(const mz_uint8 *p)
|
|
877
|
+
{
|
|
878
|
+
mz_uint32 ret;
|
|
879
|
+
memcpy(&ret, p, sizeof(mz_uint32));
|
|
880
|
+
return ret;
|
|
881
|
+
}
|
|
882
|
+
#else
|
|
883
|
+
#define TDEFL_READ_UNALIGNED_WORD32(p) *(const mz_uint32 *)(p)
|
|
884
|
+
#endif
|
|
885
|
+
static mz_bool tdefl_compress_fast(tdefl_compressor *d)
|
|
886
|
+
{
|
|
887
|
+
/* Faster, minimally featured LZRW1-style match+parse loop with better register utilization. Intended for applications where raw throughput is valued more highly than ratio. */
|
|
888
|
+
mz_uint lookahead_pos = d->m_lookahead_pos, lookahead_size = d->m_lookahead_size, dict_size = d->m_dict_size, total_lz_bytes = d->m_total_lz_bytes, num_flags_left = d->m_num_flags_left;
|
|
889
|
+
mz_uint8 *pLZ_code_buf = d->m_pLZ_code_buf, *pLZ_flags = d->m_pLZ_flags;
|
|
890
|
+
mz_uint cur_pos = lookahead_pos & TDEFL_LZ_DICT_SIZE_MASK;
|
|
891
|
+
|
|
892
|
+
while ((d->m_src_buf_left) || ((d->m_flush) && (lookahead_size)))
|
|
893
|
+
{
|
|
894
|
+
const mz_uint TDEFL_COMP_FAST_LOOKAHEAD_SIZE = 4096;
|
|
895
|
+
mz_uint dst_pos = (lookahead_pos + lookahead_size) & TDEFL_LZ_DICT_SIZE_MASK;
|
|
896
|
+
mz_uint num_bytes_to_process = (mz_uint)MZ_MIN(d->m_src_buf_left, TDEFL_COMP_FAST_LOOKAHEAD_SIZE - lookahead_size);
|
|
897
|
+
d->m_src_buf_left -= num_bytes_to_process;
|
|
898
|
+
lookahead_size += num_bytes_to_process;
|
|
899
|
+
|
|
900
|
+
while (num_bytes_to_process)
|
|
901
|
+
{
|
|
902
|
+
mz_uint32 n = MZ_MIN(TDEFL_LZ_DICT_SIZE - dst_pos, num_bytes_to_process);
|
|
903
|
+
memcpy(d->m_dict + dst_pos, d->m_pSrc, n);
|
|
904
|
+
if (dst_pos < (TDEFL_MAX_MATCH_LEN - 1))
|
|
905
|
+
memcpy(d->m_dict + TDEFL_LZ_DICT_SIZE + dst_pos, d->m_pSrc, MZ_MIN(n, (TDEFL_MAX_MATCH_LEN - 1) - dst_pos));
|
|
906
|
+
d->m_pSrc += n;
|
|
907
|
+
dst_pos = (dst_pos + n) & TDEFL_LZ_DICT_SIZE_MASK;
|
|
908
|
+
num_bytes_to_process -= n;
|
|
909
|
+
}
|
|
910
|
+
|
|
911
|
+
dict_size = MZ_MIN(TDEFL_LZ_DICT_SIZE - lookahead_size, dict_size);
|
|
912
|
+
if ((!d->m_flush) && (lookahead_size < TDEFL_COMP_FAST_LOOKAHEAD_SIZE))
|
|
913
|
+
break;
|
|
914
|
+
|
|
915
|
+
while (lookahead_size >= 4)
|
|
916
|
+
{
|
|
917
|
+
mz_uint cur_match_dist, cur_match_len = 1;
|
|
918
|
+
mz_uint8 *pCur_dict = d->m_dict + cur_pos;
|
|
919
|
+
mz_uint first_trigram = TDEFL_READ_UNALIGNED_WORD32(pCur_dict) & 0xFFFFFF;
|
|
920
|
+
mz_uint hash = (first_trigram ^ (first_trigram >> (24 - (TDEFL_LZ_HASH_BITS - 8)))) & TDEFL_LEVEL1_HASH_SIZE_MASK;
|
|
921
|
+
mz_uint probe_pos = d->m_hash[hash];
|
|
922
|
+
d->m_hash[hash] = (mz_uint16)lookahead_pos;
|
|
923
|
+
|
|
924
|
+
if (((cur_match_dist = (mz_uint16)(lookahead_pos - probe_pos)) <= dict_size) && ((TDEFL_READ_UNALIGNED_WORD32(d->m_dict + (probe_pos &= TDEFL_LZ_DICT_SIZE_MASK)) & 0xFFFFFF) == first_trigram))
|
|
925
|
+
{
|
|
926
|
+
const mz_uint16 *p = (const mz_uint16 *)pCur_dict;
|
|
927
|
+
const mz_uint16 *q = (const mz_uint16 *)(d->m_dict + probe_pos);
|
|
928
|
+
mz_uint32 probe_len = 32;
|
|
929
|
+
do
|
|
930
|
+
{
|
|
931
|
+
} while ((TDEFL_READ_UNALIGNED_WORD2(++p) == TDEFL_READ_UNALIGNED_WORD2(++q)) && (TDEFL_READ_UNALIGNED_WORD2(++p) == TDEFL_READ_UNALIGNED_WORD2(++q)) &&
|
|
932
|
+
(TDEFL_READ_UNALIGNED_WORD2(++p) == TDEFL_READ_UNALIGNED_WORD2(++q)) && (TDEFL_READ_UNALIGNED_WORD2(++p) == TDEFL_READ_UNALIGNED_WORD2(++q)) && (--probe_len > 0));
|
|
933
|
+
cur_match_len = ((mz_uint)(p - (const mz_uint16 *)pCur_dict) * 2) + (mz_uint)(*(const mz_uint8 *)p == *(const mz_uint8 *)q);
|
|
934
|
+
if (!probe_len)
|
|
935
|
+
cur_match_len = cur_match_dist ? TDEFL_MAX_MATCH_LEN : 0;
|
|
936
|
+
|
|
937
|
+
if ((cur_match_len < TDEFL_MIN_MATCH_LEN) || ((cur_match_len == TDEFL_MIN_MATCH_LEN) && (cur_match_dist >= 8U * 1024U)))
|
|
938
|
+
{
|
|
939
|
+
cur_match_len = 1;
|
|
940
|
+
*pLZ_code_buf++ = (mz_uint8)first_trigram;
|
|
941
|
+
*pLZ_flags = (mz_uint8)(*pLZ_flags >> 1);
|
|
942
|
+
d->m_huff_count[0][(mz_uint8)first_trigram]++;
|
|
943
|
+
}
|
|
944
|
+
else
|
|
945
|
+
{
|
|
946
|
+
mz_uint32 s0, s1;
|
|
947
|
+
cur_match_len = MZ_MIN(cur_match_len, lookahead_size);
|
|
948
|
+
|
|
949
|
+
MZ_ASSERT((cur_match_len >= TDEFL_MIN_MATCH_LEN) && (cur_match_dist >= 1) && (cur_match_dist <= TDEFL_LZ_DICT_SIZE));
|
|
950
|
+
|
|
951
|
+
cur_match_dist--;
|
|
952
|
+
|
|
953
|
+
pLZ_code_buf[0] = (mz_uint8)(cur_match_len - TDEFL_MIN_MATCH_LEN);
|
|
954
|
+
#ifdef MINIZ_UNALIGNED_USE_MEMCPY
|
|
955
|
+
memcpy(&pLZ_code_buf[1], &cur_match_dist, sizeof(cur_match_dist));
|
|
956
|
+
#else
|
|
957
|
+
*(mz_uint16 *)(&pLZ_code_buf[1]) = (mz_uint16)cur_match_dist;
|
|
958
|
+
#endif
|
|
959
|
+
pLZ_code_buf += 3;
|
|
960
|
+
*pLZ_flags = (mz_uint8)((*pLZ_flags >> 1) | 0x80);
|
|
961
|
+
|
|
962
|
+
s0 = s_tdefl_small_dist_sym[cur_match_dist & 511];
|
|
963
|
+
s1 = s_tdefl_large_dist_sym[cur_match_dist >> 8];
|
|
964
|
+
d->m_huff_count[1][(cur_match_dist < 512) ? s0 : s1]++;
|
|
965
|
+
|
|
966
|
+
d->m_huff_count[0][s_tdefl_len_sym[cur_match_len - TDEFL_MIN_MATCH_LEN]]++;
|
|
967
|
+
}
|
|
968
|
+
}
|
|
969
|
+
else
|
|
970
|
+
{
|
|
971
|
+
*pLZ_code_buf++ = (mz_uint8)first_trigram;
|
|
972
|
+
*pLZ_flags = (mz_uint8)(*pLZ_flags >> 1);
|
|
973
|
+
d->m_huff_count[0][(mz_uint8)first_trigram]++;
|
|
974
|
+
}
|
|
975
|
+
|
|
976
|
+
if (--num_flags_left == 0)
|
|
977
|
+
{
|
|
978
|
+
num_flags_left = 8;
|
|
979
|
+
pLZ_flags = pLZ_code_buf++;
|
|
980
|
+
}
|
|
981
|
+
|
|
982
|
+
total_lz_bytes += cur_match_len;
|
|
983
|
+
lookahead_pos += cur_match_len;
|
|
984
|
+
dict_size = MZ_MIN(dict_size + cur_match_len, (mz_uint)TDEFL_LZ_DICT_SIZE);
|
|
985
|
+
cur_pos = (cur_pos + cur_match_len) & TDEFL_LZ_DICT_SIZE_MASK;
|
|
986
|
+
MZ_ASSERT(lookahead_size >= cur_match_len);
|
|
987
|
+
lookahead_size -= cur_match_len;
|
|
988
|
+
|
|
989
|
+
if (pLZ_code_buf > &d->m_lz_code_buf[TDEFL_LZ_CODE_BUF_SIZE - 8])
|
|
990
|
+
{
|
|
991
|
+
int n;
|
|
992
|
+
d->m_lookahead_pos = lookahead_pos;
|
|
993
|
+
d->m_lookahead_size = lookahead_size;
|
|
994
|
+
d->m_dict_size = dict_size;
|
|
995
|
+
d->m_total_lz_bytes = total_lz_bytes;
|
|
996
|
+
d->m_pLZ_code_buf = pLZ_code_buf;
|
|
997
|
+
d->m_pLZ_flags = pLZ_flags;
|
|
998
|
+
d->m_num_flags_left = num_flags_left;
|
|
999
|
+
if ((n = tdefl_flush_block(d, 0)) != 0)
|
|
1000
|
+
return (n < 0) ? MZ_FALSE : MZ_TRUE;
|
|
1001
|
+
total_lz_bytes = d->m_total_lz_bytes;
|
|
1002
|
+
pLZ_code_buf = d->m_pLZ_code_buf;
|
|
1003
|
+
pLZ_flags = d->m_pLZ_flags;
|
|
1004
|
+
num_flags_left = d->m_num_flags_left;
|
|
1005
|
+
}
|
|
1006
|
+
}
|
|
1007
|
+
|
|
1008
|
+
while (lookahead_size)
|
|
1009
|
+
{
|
|
1010
|
+
mz_uint8 lit = d->m_dict[cur_pos];
|
|
1011
|
+
|
|
1012
|
+
total_lz_bytes++;
|
|
1013
|
+
*pLZ_code_buf++ = lit;
|
|
1014
|
+
*pLZ_flags = (mz_uint8)(*pLZ_flags >> 1);
|
|
1015
|
+
if (--num_flags_left == 0)
|
|
1016
|
+
{
|
|
1017
|
+
num_flags_left = 8;
|
|
1018
|
+
pLZ_flags = pLZ_code_buf++;
|
|
1019
|
+
}
|
|
1020
|
+
|
|
1021
|
+
d->m_huff_count[0][lit]++;
|
|
1022
|
+
|
|
1023
|
+
lookahead_pos++;
|
|
1024
|
+
dict_size = MZ_MIN(dict_size + 1, (mz_uint)TDEFL_LZ_DICT_SIZE);
|
|
1025
|
+
cur_pos = (cur_pos + 1) & TDEFL_LZ_DICT_SIZE_MASK;
|
|
1026
|
+
lookahead_size--;
|
|
1027
|
+
|
|
1028
|
+
if (pLZ_code_buf > &d->m_lz_code_buf[TDEFL_LZ_CODE_BUF_SIZE - 8])
|
|
1029
|
+
{
|
|
1030
|
+
int n;
|
|
1031
|
+
d->m_lookahead_pos = lookahead_pos;
|
|
1032
|
+
d->m_lookahead_size = lookahead_size;
|
|
1033
|
+
d->m_dict_size = dict_size;
|
|
1034
|
+
d->m_total_lz_bytes = total_lz_bytes;
|
|
1035
|
+
d->m_pLZ_code_buf = pLZ_code_buf;
|
|
1036
|
+
d->m_pLZ_flags = pLZ_flags;
|
|
1037
|
+
d->m_num_flags_left = num_flags_left;
|
|
1038
|
+
if ((n = tdefl_flush_block(d, 0)) != 0)
|
|
1039
|
+
return (n < 0) ? MZ_FALSE : MZ_TRUE;
|
|
1040
|
+
total_lz_bytes = d->m_total_lz_bytes;
|
|
1041
|
+
pLZ_code_buf = d->m_pLZ_code_buf;
|
|
1042
|
+
pLZ_flags = d->m_pLZ_flags;
|
|
1043
|
+
num_flags_left = d->m_num_flags_left;
|
|
1044
|
+
}
|
|
1045
|
+
}
|
|
1046
|
+
}
|
|
1047
|
+
|
|
1048
|
+
d->m_lookahead_pos = lookahead_pos;
|
|
1049
|
+
d->m_lookahead_size = lookahead_size;
|
|
1050
|
+
d->m_dict_size = dict_size;
|
|
1051
|
+
d->m_total_lz_bytes = total_lz_bytes;
|
|
1052
|
+
d->m_pLZ_code_buf = pLZ_code_buf;
|
|
1053
|
+
d->m_pLZ_flags = pLZ_flags;
|
|
1054
|
+
d->m_num_flags_left = num_flags_left;
|
|
1055
|
+
return MZ_TRUE;
|
|
1056
|
+
}
|
|
1057
|
+
#endif /* MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN */
|
|
1058
|
+
|
|
1059
|
+
static MZ_FORCEINLINE void tdefl_record_literal(tdefl_compressor *d, mz_uint8 lit)
|
|
1060
|
+
{
|
|
1061
|
+
d->m_total_lz_bytes++;
|
|
1062
|
+
*d->m_pLZ_code_buf++ = lit;
|
|
1063
|
+
*d->m_pLZ_flags = (mz_uint8)(*d->m_pLZ_flags >> 1);
|
|
1064
|
+
if (--d->m_num_flags_left == 0)
|
|
1065
|
+
{
|
|
1066
|
+
d->m_num_flags_left = 8;
|
|
1067
|
+
d->m_pLZ_flags = d->m_pLZ_code_buf++;
|
|
1068
|
+
}
|
|
1069
|
+
d->m_huff_count[0][lit]++;
|
|
1070
|
+
}
|
|
1071
|
+
|
|
1072
|
+
static MZ_FORCEINLINE void tdefl_record_match(tdefl_compressor *d, mz_uint match_len, mz_uint match_dist)
|
|
1073
|
+
{
|
|
1074
|
+
mz_uint32 s0, s1;
|
|
1075
|
+
|
|
1076
|
+
MZ_ASSERT((match_len >= TDEFL_MIN_MATCH_LEN) && (match_dist >= 1) && (match_dist <= TDEFL_LZ_DICT_SIZE));
|
|
1077
|
+
|
|
1078
|
+
d->m_total_lz_bytes += match_len;
|
|
1079
|
+
|
|
1080
|
+
d->m_pLZ_code_buf[0] = (mz_uint8)(match_len - TDEFL_MIN_MATCH_LEN);
|
|
1081
|
+
|
|
1082
|
+
match_dist -= 1;
|
|
1083
|
+
d->m_pLZ_code_buf[1] = (mz_uint8)(match_dist & 0xFF);
|
|
1084
|
+
d->m_pLZ_code_buf[2] = (mz_uint8)(match_dist >> 8);
|
|
1085
|
+
d->m_pLZ_code_buf += 3;
|
|
1086
|
+
|
|
1087
|
+
*d->m_pLZ_flags = (mz_uint8)((*d->m_pLZ_flags >> 1) | 0x80);
|
|
1088
|
+
if (--d->m_num_flags_left == 0)
|
|
1089
|
+
{
|
|
1090
|
+
d->m_num_flags_left = 8;
|
|
1091
|
+
d->m_pLZ_flags = d->m_pLZ_code_buf++;
|
|
1092
|
+
}
|
|
1093
|
+
|
|
1094
|
+
s0 = s_tdefl_small_dist_sym[match_dist & 511];
|
|
1095
|
+
s1 = s_tdefl_large_dist_sym[(match_dist >> 8) & 127];
|
|
1096
|
+
d->m_huff_count[1][(match_dist < 512) ? s0 : s1]++;
|
|
1097
|
+
d->m_huff_count[0][s_tdefl_len_sym[match_len - TDEFL_MIN_MATCH_LEN]]++;
|
|
1098
|
+
}
|
|
1099
|
+
|
|
1100
|
+
static mz_bool tdefl_compress_normal(tdefl_compressor *d)
|
|
1101
|
+
{
|
|
1102
|
+
const mz_uint8 *pSrc = d->m_pSrc;
|
|
1103
|
+
size_t src_buf_left = d->m_src_buf_left;
|
|
1104
|
+
tdefl_flush flush = d->m_flush;
|
|
1105
|
+
|
|
1106
|
+
while ((src_buf_left) || ((flush) && (d->m_lookahead_size)))
|
|
1107
|
+
{
|
|
1108
|
+
mz_uint len_to_move, cur_match_dist, cur_match_len, cur_pos;
|
|
1109
|
+
/* Update dictionary and hash chains. Keeps the lookahead size equal to TDEFL_MAX_MATCH_LEN. */
|
|
1110
|
+
if ((d->m_lookahead_size + d->m_dict_size) >= (TDEFL_MIN_MATCH_LEN - 1))
|
|
1111
|
+
{
|
|
1112
|
+
mz_uint dst_pos = (d->m_lookahead_pos + d->m_lookahead_size) & TDEFL_LZ_DICT_SIZE_MASK, ins_pos = d->m_lookahead_pos + d->m_lookahead_size - 2;
|
|
1113
|
+
mz_uint hash = (d->m_dict[ins_pos & TDEFL_LZ_DICT_SIZE_MASK] << TDEFL_LZ_HASH_SHIFT) ^ d->m_dict[(ins_pos + 1) & TDEFL_LZ_DICT_SIZE_MASK];
|
|
1114
|
+
mz_uint num_bytes_to_process = (mz_uint)MZ_MIN(src_buf_left, TDEFL_MAX_MATCH_LEN - d->m_lookahead_size);
|
|
1115
|
+
const mz_uint8 *pSrc_end = pSrc ? pSrc + num_bytes_to_process : NULL;
|
|
1116
|
+
src_buf_left -= num_bytes_to_process;
|
|
1117
|
+
d->m_lookahead_size += num_bytes_to_process;
|
|
1118
|
+
while (pSrc != pSrc_end)
|
|
1119
|
+
{
|
|
1120
|
+
mz_uint8 c = *pSrc++;
|
|
1121
|
+
d->m_dict[dst_pos] = c;
|
|
1122
|
+
if (dst_pos < (TDEFL_MAX_MATCH_LEN - 1))
|
|
1123
|
+
d->m_dict[TDEFL_LZ_DICT_SIZE + dst_pos] = c;
|
|
1124
|
+
hash = ((hash << TDEFL_LZ_HASH_SHIFT) ^ c) & (TDEFL_LZ_HASH_SIZE - 1);
|
|
1125
|
+
d->m_next[ins_pos & TDEFL_LZ_DICT_SIZE_MASK] = d->m_hash[hash];
|
|
1126
|
+
d->m_hash[hash] = (mz_uint16)(ins_pos);
|
|
1127
|
+
dst_pos = (dst_pos + 1) & TDEFL_LZ_DICT_SIZE_MASK;
|
|
1128
|
+
ins_pos++;
|
|
1129
|
+
}
|
|
1130
|
+
}
|
|
1131
|
+
else
|
|
1132
|
+
{
|
|
1133
|
+
while ((src_buf_left) && (d->m_lookahead_size < TDEFL_MAX_MATCH_LEN))
|
|
1134
|
+
{
|
|
1135
|
+
mz_uint8 c = *pSrc++;
|
|
1136
|
+
mz_uint dst_pos = (d->m_lookahead_pos + d->m_lookahead_size) & TDEFL_LZ_DICT_SIZE_MASK;
|
|
1137
|
+
src_buf_left--;
|
|
1138
|
+
d->m_dict[dst_pos] = c;
|
|
1139
|
+
if (dst_pos < (TDEFL_MAX_MATCH_LEN - 1))
|
|
1140
|
+
d->m_dict[TDEFL_LZ_DICT_SIZE + dst_pos] = c;
|
|
1141
|
+
if ((++d->m_lookahead_size + d->m_dict_size) >= TDEFL_MIN_MATCH_LEN)
|
|
1142
|
+
{
|
|
1143
|
+
mz_uint ins_pos = d->m_lookahead_pos + (d->m_lookahead_size - 1) - 2;
|
|
1144
|
+
mz_uint hash = ((d->m_dict[ins_pos & TDEFL_LZ_DICT_SIZE_MASK] << (TDEFL_LZ_HASH_SHIFT * 2)) ^ (d->m_dict[(ins_pos + 1) & TDEFL_LZ_DICT_SIZE_MASK] << TDEFL_LZ_HASH_SHIFT) ^ c) & (TDEFL_LZ_HASH_SIZE - 1);
|
|
1145
|
+
d->m_next[ins_pos & TDEFL_LZ_DICT_SIZE_MASK] = d->m_hash[hash];
|
|
1146
|
+
d->m_hash[hash] = (mz_uint16)(ins_pos);
|
|
1147
|
+
}
|
|
1148
|
+
}
|
|
1149
|
+
}
|
|
1150
|
+
d->m_dict_size = MZ_MIN(TDEFL_LZ_DICT_SIZE - d->m_lookahead_size, d->m_dict_size);
|
|
1151
|
+
if ((!flush) && (d->m_lookahead_size < TDEFL_MAX_MATCH_LEN))
|
|
1152
|
+
break;
|
|
1153
|
+
|
|
1154
|
+
/* Simple lazy/greedy parsing state machine. */
|
|
1155
|
+
len_to_move = 1;
|
|
1156
|
+
cur_match_dist = 0;
|
|
1157
|
+
cur_match_len = d->m_saved_match_len ? d->m_saved_match_len : (TDEFL_MIN_MATCH_LEN - 1);
|
|
1158
|
+
cur_pos = d->m_lookahead_pos & TDEFL_LZ_DICT_SIZE_MASK;
|
|
1159
|
+
if (d->m_flags & (TDEFL_RLE_MATCHES | TDEFL_FORCE_ALL_RAW_BLOCKS))
|
|
1160
|
+
{
|
|
1161
|
+
if ((d->m_dict_size) && (!(d->m_flags & TDEFL_FORCE_ALL_RAW_BLOCKS)))
|
|
1162
|
+
{
|
|
1163
|
+
mz_uint8 c = d->m_dict[(cur_pos - 1) & TDEFL_LZ_DICT_SIZE_MASK];
|
|
1164
|
+
cur_match_len = 0;
|
|
1165
|
+
while (cur_match_len < d->m_lookahead_size)
|
|
1166
|
+
{
|
|
1167
|
+
if (d->m_dict[cur_pos + cur_match_len] != c)
|
|
1168
|
+
break;
|
|
1169
|
+
cur_match_len++;
|
|
1170
|
+
}
|
|
1171
|
+
if (cur_match_len < TDEFL_MIN_MATCH_LEN)
|
|
1172
|
+
cur_match_len = 0;
|
|
1173
|
+
else
|
|
1174
|
+
cur_match_dist = 1;
|
|
1175
|
+
}
|
|
1176
|
+
}
|
|
1177
|
+
else
|
|
1178
|
+
{
|
|
1179
|
+
tdefl_find_match(d, d->m_lookahead_pos, d->m_dict_size, d->m_lookahead_size, &cur_match_dist, &cur_match_len);
|
|
1180
|
+
}
|
|
1181
|
+
if (((cur_match_len == TDEFL_MIN_MATCH_LEN) && (cur_match_dist >= 8U * 1024U)) || (cur_pos == cur_match_dist) || ((d->m_flags & TDEFL_FILTER_MATCHES) && (cur_match_len <= 5)))
|
|
1182
|
+
{
|
|
1183
|
+
cur_match_dist = cur_match_len = 0;
|
|
1184
|
+
}
|
|
1185
|
+
if (d->m_saved_match_len)
|
|
1186
|
+
{
|
|
1187
|
+
if (cur_match_len > d->m_saved_match_len)
|
|
1188
|
+
{
|
|
1189
|
+
tdefl_record_literal(d, (mz_uint8)d->m_saved_lit);
|
|
1190
|
+
if (cur_match_len >= 128)
|
|
1191
|
+
{
|
|
1192
|
+
tdefl_record_match(d, cur_match_len, cur_match_dist);
|
|
1193
|
+
d->m_saved_match_len = 0;
|
|
1194
|
+
len_to_move = cur_match_len;
|
|
1195
|
+
}
|
|
1196
|
+
else
|
|
1197
|
+
{
|
|
1198
|
+
d->m_saved_lit = d->m_dict[cur_pos];
|
|
1199
|
+
d->m_saved_match_dist = cur_match_dist;
|
|
1200
|
+
d->m_saved_match_len = cur_match_len;
|
|
1201
|
+
}
|
|
1202
|
+
}
|
|
1203
|
+
else
|
|
1204
|
+
{
|
|
1205
|
+
tdefl_record_match(d, d->m_saved_match_len, d->m_saved_match_dist);
|
|
1206
|
+
len_to_move = d->m_saved_match_len - 1;
|
|
1207
|
+
d->m_saved_match_len = 0;
|
|
1208
|
+
}
|
|
1209
|
+
}
|
|
1210
|
+
else if (!cur_match_dist)
|
|
1211
|
+
tdefl_record_literal(d, d->m_dict[MZ_MIN(cur_pos, sizeof(d->m_dict) - 1)]);
|
|
1212
|
+
else if ((d->m_greedy_parsing) || (d->m_flags & TDEFL_RLE_MATCHES) || (cur_match_len >= 128))
|
|
1213
|
+
{
|
|
1214
|
+
tdefl_record_match(d, cur_match_len, cur_match_dist);
|
|
1215
|
+
len_to_move = cur_match_len;
|
|
1216
|
+
}
|
|
1217
|
+
else
|
|
1218
|
+
{
|
|
1219
|
+
d->m_saved_lit = d->m_dict[MZ_MIN(cur_pos, sizeof(d->m_dict) - 1)];
|
|
1220
|
+
d->m_saved_match_dist = cur_match_dist;
|
|
1221
|
+
d->m_saved_match_len = cur_match_len;
|
|
1222
|
+
}
|
|
1223
|
+
/* Move the lookahead forward by len_to_move bytes. */
|
|
1224
|
+
d->m_lookahead_pos += len_to_move;
|
|
1225
|
+
MZ_ASSERT(d->m_lookahead_size >= len_to_move);
|
|
1226
|
+
d->m_lookahead_size -= len_to_move;
|
|
1227
|
+
d->m_dict_size = MZ_MIN(d->m_dict_size + len_to_move, (mz_uint)TDEFL_LZ_DICT_SIZE);
|
|
1228
|
+
/* Check if it's time to flush the current LZ codes to the internal output buffer. */
|
|
1229
|
+
if ((d->m_pLZ_code_buf > &d->m_lz_code_buf[TDEFL_LZ_CODE_BUF_SIZE - 8]) ||
|
|
1230
|
+
((d->m_total_lz_bytes > 31 * 1024) && (((((mz_uint)(d->m_pLZ_code_buf - d->m_lz_code_buf) * 115) >> 7) >= d->m_total_lz_bytes) || (d->m_flags & TDEFL_FORCE_ALL_RAW_BLOCKS))))
|
|
1231
|
+
{
|
|
1232
|
+
int n;
|
|
1233
|
+
d->m_pSrc = pSrc;
|
|
1234
|
+
d->m_src_buf_left = src_buf_left;
|
|
1235
|
+
if ((n = tdefl_flush_block(d, 0)) != 0)
|
|
1236
|
+
return (n < 0) ? MZ_FALSE : MZ_TRUE;
|
|
1237
|
+
}
|
|
1238
|
+
}
|
|
1239
|
+
|
|
1240
|
+
d->m_pSrc = pSrc;
|
|
1241
|
+
d->m_src_buf_left = src_buf_left;
|
|
1242
|
+
return MZ_TRUE;
|
|
1243
|
+
}
|
|
1244
|
+
|
|
1245
|
+
static tdefl_status tdefl_flush_output_buffer(tdefl_compressor *d)
|
|
1246
|
+
{
|
|
1247
|
+
if (d->m_pIn_buf_size)
|
|
1248
|
+
{
|
|
1249
|
+
*d->m_pIn_buf_size = d->m_pSrc - (const mz_uint8 *)d->m_pIn_buf;
|
|
1250
|
+
}
|
|
1251
|
+
|
|
1252
|
+
if (d->m_pOut_buf_size)
|
|
1253
|
+
{
|
|
1254
|
+
size_t n = MZ_MIN(*d->m_pOut_buf_size - d->m_out_buf_ofs, d->m_output_flush_remaining);
|
|
1255
|
+
memcpy((mz_uint8 *)d->m_pOut_buf + d->m_out_buf_ofs, d->m_output_buf + d->m_output_flush_ofs, n);
|
|
1256
|
+
d->m_output_flush_ofs += (mz_uint)n;
|
|
1257
|
+
d->m_output_flush_remaining -= (mz_uint)n;
|
|
1258
|
+
d->m_out_buf_ofs += n;
|
|
1259
|
+
|
|
1260
|
+
*d->m_pOut_buf_size = d->m_out_buf_ofs;
|
|
1261
|
+
}
|
|
1262
|
+
|
|
1263
|
+
return (d->m_finished && !d->m_output_flush_remaining) ? TDEFL_STATUS_DONE : TDEFL_STATUS_OKAY;
|
|
1264
|
+
}
|
|
1265
|
+
|
|
1266
|
+
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)
|
|
1267
|
+
{
|
|
1268
|
+
if (!d)
|
|
1269
|
+
{
|
|
1270
|
+
if (pIn_buf_size)
|
|
1271
|
+
*pIn_buf_size = 0;
|
|
1272
|
+
if (pOut_buf_size)
|
|
1273
|
+
*pOut_buf_size = 0;
|
|
1274
|
+
return TDEFL_STATUS_BAD_PARAM;
|
|
1275
|
+
}
|
|
1276
|
+
|
|
1277
|
+
d->m_pIn_buf = pIn_buf;
|
|
1278
|
+
d->m_pIn_buf_size = pIn_buf_size;
|
|
1279
|
+
d->m_pOut_buf = pOut_buf;
|
|
1280
|
+
d->m_pOut_buf_size = pOut_buf_size;
|
|
1281
|
+
d->m_pSrc = (const mz_uint8 *)(pIn_buf);
|
|
1282
|
+
d->m_src_buf_left = pIn_buf_size ? *pIn_buf_size : 0;
|
|
1283
|
+
d->m_out_buf_ofs = 0;
|
|
1284
|
+
d->m_flush = flush;
|
|
1285
|
+
|
|
1286
|
+
if (((d->m_pPut_buf_func != NULL) == ((pOut_buf != NULL) || (pOut_buf_size != NULL))) || (d->m_prev_return_status != TDEFL_STATUS_OKAY) ||
|
|
1287
|
+
(d->m_wants_to_finish && (flush != TDEFL_FINISH)) || (pIn_buf_size && *pIn_buf_size && !pIn_buf) || (pOut_buf_size && *pOut_buf_size && !pOut_buf))
|
|
1288
|
+
{
|
|
1289
|
+
if (pIn_buf_size)
|
|
1290
|
+
*pIn_buf_size = 0;
|
|
1291
|
+
if (pOut_buf_size)
|
|
1292
|
+
*pOut_buf_size = 0;
|
|
1293
|
+
return (d->m_prev_return_status = TDEFL_STATUS_BAD_PARAM);
|
|
1294
|
+
}
|
|
1295
|
+
d->m_wants_to_finish |= (flush == TDEFL_FINISH);
|
|
1296
|
+
|
|
1297
|
+
if ((d->m_output_flush_remaining) || (d->m_finished))
|
|
1298
|
+
return (d->m_prev_return_status = tdefl_flush_output_buffer(d));
|
|
1299
|
+
|
|
1300
|
+
#if MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN
|
|
1301
|
+
if (((d->m_flags & TDEFL_MAX_PROBES_MASK) == 1) &&
|
|
1302
|
+
((d->m_flags & TDEFL_GREEDY_PARSING_FLAG) != 0) &&
|
|
1303
|
+
((d->m_flags & (TDEFL_FILTER_MATCHES | TDEFL_FORCE_ALL_RAW_BLOCKS | TDEFL_RLE_MATCHES)) == 0))
|
|
1304
|
+
{
|
|
1305
|
+
if (!tdefl_compress_fast(d))
|
|
1306
|
+
return d->m_prev_return_status;
|
|
1307
|
+
}
|
|
1308
|
+
else
|
|
1309
|
+
#endif /* #if MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN */
|
|
1310
|
+
{
|
|
1311
|
+
if (!tdefl_compress_normal(d))
|
|
1312
|
+
return d->m_prev_return_status;
|
|
1313
|
+
}
|
|
1314
|
+
|
|
1315
|
+
if ((d->m_flags & (TDEFL_WRITE_ZLIB_HEADER | TDEFL_COMPUTE_ADLER32)) && (pIn_buf))
|
|
1316
|
+
d->m_adler32 = (mz_uint32)mz_adler32(d->m_adler32, (const mz_uint8 *)pIn_buf, d->m_pSrc - (const mz_uint8 *)pIn_buf);
|
|
1317
|
+
|
|
1318
|
+
if ((flush) && (!d->m_lookahead_size) && (!d->m_src_buf_left) && (!d->m_output_flush_remaining))
|
|
1319
|
+
{
|
|
1320
|
+
if (tdefl_flush_block(d, flush) < 0)
|
|
1321
|
+
return d->m_prev_return_status;
|
|
1322
|
+
d->m_finished = (flush == TDEFL_FINISH);
|
|
1323
|
+
if (flush == TDEFL_FULL_FLUSH)
|
|
1324
|
+
{
|
|
1325
|
+
MZ_CLEAR_ARR(d->m_hash);
|
|
1326
|
+
MZ_CLEAR_ARR(d->m_next);
|
|
1327
|
+
d->m_dict_size = 0;
|
|
1328
|
+
}
|
|
1329
|
+
}
|
|
1330
|
+
|
|
1331
|
+
return (d->m_prev_return_status = tdefl_flush_output_buffer(d));
|
|
1332
|
+
}
|
|
1333
|
+
|
|
1334
|
+
tdefl_status tdefl_compress_buffer(tdefl_compressor *d, const void *pIn_buf, size_t in_buf_size, tdefl_flush flush)
|
|
1335
|
+
{
|
|
1336
|
+
MZ_ASSERT(d->m_pPut_buf_func);
|
|
1337
|
+
return tdefl_compress(d, pIn_buf, &in_buf_size, NULL, NULL, flush);
|
|
1338
|
+
}
|
|
1339
|
+
|
|
1340
|
+
tdefl_status tdefl_init(tdefl_compressor *d, tdefl_put_buf_func_ptr pPut_buf_func, void *pPut_buf_user, int flags)
|
|
1341
|
+
{
|
|
1342
|
+
d->m_pPut_buf_func = pPut_buf_func;
|
|
1343
|
+
d->m_pPut_buf_user = pPut_buf_user;
|
|
1344
|
+
d->m_flags = (mz_uint)(flags);
|
|
1345
|
+
d->m_max_probes[0] = 1 + ((flags & 0xFFF) + 2) / 3;
|
|
1346
|
+
d->m_greedy_parsing = (flags & TDEFL_GREEDY_PARSING_FLAG) != 0;
|
|
1347
|
+
d->m_max_probes[1] = 1 + (((flags & 0xFFF) >> 2) + 2) / 3;
|
|
1348
|
+
if (!(flags & TDEFL_NONDETERMINISTIC_PARSING_FLAG))
|
|
1349
|
+
MZ_CLEAR_ARR(d->m_hash);
|
|
1350
|
+
d->m_lookahead_pos = d->m_lookahead_size = d->m_dict_size = d->m_total_lz_bytes = d->m_lz_code_buf_dict_pos = d->m_bits_in = 0;
|
|
1351
|
+
d->m_output_flush_ofs = d->m_output_flush_remaining = d->m_finished = d->m_block_index = d->m_bit_buffer = d->m_wants_to_finish = 0;
|
|
1352
|
+
d->m_pLZ_code_buf = d->m_lz_code_buf + 1;
|
|
1353
|
+
d->m_pLZ_flags = d->m_lz_code_buf;
|
|
1354
|
+
*d->m_pLZ_flags = 0;
|
|
1355
|
+
d->m_num_flags_left = 8;
|
|
1356
|
+
d->m_pOutput_buf = d->m_output_buf;
|
|
1357
|
+
d->m_pOutput_buf_end = d->m_output_buf;
|
|
1358
|
+
d->m_prev_return_status = TDEFL_STATUS_OKAY;
|
|
1359
|
+
d->m_saved_match_dist = d->m_saved_match_len = d->m_saved_lit = 0;
|
|
1360
|
+
d->m_adler32 = 1;
|
|
1361
|
+
d->m_pIn_buf = NULL;
|
|
1362
|
+
d->m_pOut_buf = NULL;
|
|
1363
|
+
d->m_pIn_buf_size = NULL;
|
|
1364
|
+
d->m_pOut_buf_size = NULL;
|
|
1365
|
+
d->m_flush = TDEFL_NO_FLUSH;
|
|
1366
|
+
d->m_pSrc = NULL;
|
|
1367
|
+
d->m_src_buf_left = 0;
|
|
1368
|
+
d->m_out_buf_ofs = 0;
|
|
1369
|
+
if (!(flags & TDEFL_NONDETERMINISTIC_PARSING_FLAG))
|
|
1370
|
+
MZ_CLEAR_ARR(d->m_dict);
|
|
1371
|
+
memset(&d->m_huff_count[0][0], 0, sizeof(d->m_huff_count[0][0]) * TDEFL_MAX_HUFF_SYMBOLS_0);
|
|
1372
|
+
memset(&d->m_huff_count[1][0], 0, sizeof(d->m_huff_count[1][0]) * TDEFL_MAX_HUFF_SYMBOLS_1);
|
|
1373
|
+
return TDEFL_STATUS_OKAY;
|
|
1374
|
+
}
|
|
1375
|
+
|
|
1376
|
+
tdefl_status tdefl_get_prev_return_status(tdefl_compressor *d)
|
|
1377
|
+
{
|
|
1378
|
+
return d->m_prev_return_status;
|
|
1379
|
+
}
|
|
1380
|
+
|
|
1381
|
+
mz_uint32 tdefl_get_adler32(tdefl_compressor *d)
|
|
1382
|
+
{
|
|
1383
|
+
return d->m_adler32;
|
|
1384
|
+
}
|
|
1385
|
+
|
|
1386
|
+
mz_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)
|
|
1387
|
+
{
|
|
1388
|
+
tdefl_compressor *pComp;
|
|
1389
|
+
mz_bool succeeded;
|
|
1390
|
+
if (((buf_len) && (!pBuf)) || (!pPut_buf_func))
|
|
1391
|
+
return MZ_FALSE;
|
|
1392
|
+
pComp = (tdefl_compressor *)MZ_MALLOC(sizeof(tdefl_compressor));
|
|
1393
|
+
if (!pComp)
|
|
1394
|
+
return MZ_FALSE;
|
|
1395
|
+
succeeded = (tdefl_init(pComp, pPut_buf_func, pPut_buf_user, flags) == TDEFL_STATUS_OKAY);
|
|
1396
|
+
succeeded = succeeded && (tdefl_compress_buffer(pComp, pBuf, buf_len, TDEFL_FINISH) == TDEFL_STATUS_DONE);
|
|
1397
|
+
MZ_FREE(pComp);
|
|
1398
|
+
return succeeded;
|
|
1399
|
+
}
|
|
1400
|
+
|
|
1401
|
+
typedef struct
|
|
1402
|
+
{
|
|
1403
|
+
size_t m_size, m_capacity;
|
|
1404
|
+
mz_uint8 *m_pBuf;
|
|
1405
|
+
mz_bool m_expandable;
|
|
1406
|
+
} tdefl_output_buffer;
|
|
1407
|
+
|
|
1408
|
+
static mz_bool tdefl_output_buffer_putter(const void *pBuf, int len, void *pUser)
|
|
1409
|
+
{
|
|
1410
|
+
tdefl_output_buffer *p = (tdefl_output_buffer *)pUser;
|
|
1411
|
+
size_t new_size = p->m_size + len;
|
|
1412
|
+
if (new_size > p->m_capacity)
|
|
1413
|
+
{
|
|
1414
|
+
size_t new_capacity = p->m_capacity;
|
|
1415
|
+
mz_uint8 *pNew_buf;
|
|
1416
|
+
if (!p->m_expandable)
|
|
1417
|
+
return MZ_FALSE;
|
|
1418
|
+
do
|
|
1419
|
+
{
|
|
1420
|
+
new_capacity = MZ_MAX(128U, new_capacity << 1U);
|
|
1421
|
+
} while (new_size > new_capacity);
|
|
1422
|
+
pNew_buf = (mz_uint8 *)MZ_REALLOC(p->m_pBuf, new_capacity);
|
|
1423
|
+
if (!pNew_buf)
|
|
1424
|
+
return MZ_FALSE;
|
|
1425
|
+
p->m_pBuf = pNew_buf;
|
|
1426
|
+
p->m_capacity = new_capacity;
|
|
1427
|
+
}
|
|
1428
|
+
memcpy((mz_uint8 *)p->m_pBuf + p->m_size, pBuf, len);
|
|
1429
|
+
p->m_size = new_size;
|
|
1430
|
+
return MZ_TRUE;
|
|
1431
|
+
}
|
|
1432
|
+
|
|
1433
|
+
void *tdefl_compress_mem_to_heap(const void *pSrc_buf, size_t src_buf_len, size_t *pOut_len, int flags)
|
|
1434
|
+
{
|
|
1435
|
+
tdefl_output_buffer out_buf;
|
|
1436
|
+
MZ_CLEAR_OBJ(out_buf);
|
|
1437
|
+
if (!pOut_len)
|
|
1438
|
+
return MZ_FALSE;
|
|
1439
|
+
else
|
|
1440
|
+
*pOut_len = 0;
|
|
1441
|
+
out_buf.m_expandable = MZ_TRUE;
|
|
1442
|
+
if (!tdefl_compress_mem_to_output(pSrc_buf, src_buf_len, tdefl_output_buffer_putter, &out_buf, flags))
|
|
1443
|
+
return NULL;
|
|
1444
|
+
*pOut_len = out_buf.m_size;
|
|
1445
|
+
return out_buf.m_pBuf;
|
|
1446
|
+
}
|
|
1447
|
+
|
|
1448
|
+
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)
|
|
1449
|
+
{
|
|
1450
|
+
tdefl_output_buffer out_buf;
|
|
1451
|
+
MZ_CLEAR_OBJ(out_buf);
|
|
1452
|
+
if (!pOut_buf)
|
|
1453
|
+
return 0;
|
|
1454
|
+
out_buf.m_pBuf = (mz_uint8 *)pOut_buf;
|
|
1455
|
+
out_buf.m_capacity = out_buf_len;
|
|
1456
|
+
if (!tdefl_compress_mem_to_output(pSrc_buf, src_buf_len, tdefl_output_buffer_putter, &out_buf, flags))
|
|
1457
|
+
return 0;
|
|
1458
|
+
return out_buf.m_size;
|
|
1459
|
+
}
|
|
1460
|
+
|
|
1461
|
+
/* level may actually range from [0,10] (10 is a "hidden" max level, where we want a bit more compression and it's fine if throughput to fall off a cliff on some files). */
|
|
1462
|
+
mz_uint tdefl_create_comp_flags_from_zip_params(int level, int window_bits, int strategy)
|
|
1463
|
+
{
|
|
1464
|
+
mz_uint comp_flags = s_tdefl_num_probes[(level >= 0) ? MZ_MIN(10, level) : MZ_DEFAULT_LEVEL] | ((level <= 3) ? TDEFL_GREEDY_PARSING_FLAG : 0);
|
|
1465
|
+
if (window_bits > 0)
|
|
1466
|
+
comp_flags |= TDEFL_WRITE_ZLIB_HEADER;
|
|
1467
|
+
|
|
1468
|
+
if (!level)
|
|
1469
|
+
comp_flags |= TDEFL_FORCE_ALL_RAW_BLOCKS;
|
|
1470
|
+
else if (strategy == MZ_FILTERED)
|
|
1471
|
+
comp_flags |= TDEFL_FILTER_MATCHES;
|
|
1472
|
+
else if (strategy == MZ_HUFFMAN_ONLY)
|
|
1473
|
+
comp_flags &= ~TDEFL_MAX_PROBES_MASK;
|
|
1474
|
+
else if (strategy == MZ_FIXED)
|
|
1475
|
+
comp_flags |= TDEFL_FORCE_ALL_STATIC_BLOCKS;
|
|
1476
|
+
else if (strategy == MZ_RLE)
|
|
1477
|
+
comp_flags |= TDEFL_RLE_MATCHES;
|
|
1478
|
+
|
|
1479
|
+
return comp_flags;
|
|
1480
|
+
}
|
|
1481
|
+
|
|
1482
|
+
#ifdef _MSC_VER
|
|
1483
|
+
#pragma warning(push)
|
|
1484
|
+
#pragma warning(disable : 4204) /* nonstandard extension used : non-constant aggregate initializer (also supported by GNU C and C99, so no big deal) */
|
|
1485
|
+
#endif
|
|
1486
|
+
|
|
1487
|
+
/* Simple PNG writer function by Alex Evans, 2011. Released into the public domain: https://gist.github.com/908299, more context at
|
|
1488
|
+
http://altdevblogaday.org/2011/04/06/a-smaller-jpg-encoder/.
|
|
1489
|
+
This is actually a modification of Alex's original code so PNG files generated by this function pass pngcheck. */
|
|
1490
|
+
void *tdefl_write_image_to_png_file_in_memory_ex(const void *pImage, int w, int h, int num_chans, size_t *pLen_out, mz_uint level, mz_bool flip)
|
|
1491
|
+
{
|
|
1492
|
+
/* Using a local copy of this array here in case MINIZ_NO_ZLIB_APIS was defined. */
|
|
1493
|
+
static const mz_uint s_tdefl_png_num_probes[11] = { 0, 1, 6, 32, 16, 32, 128, 256, 512, 768, 1500 };
|
|
1494
|
+
tdefl_compressor *pComp = (tdefl_compressor *)MZ_MALLOC(sizeof(tdefl_compressor));
|
|
1495
|
+
tdefl_output_buffer out_buf;
|
|
1496
|
+
int i, bpl = w * num_chans, y, z;
|
|
1497
|
+
mz_uint32 c;
|
|
1498
|
+
*pLen_out = 0;
|
|
1499
|
+
if (!pComp)
|
|
1500
|
+
return NULL;
|
|
1501
|
+
MZ_CLEAR_OBJ(out_buf);
|
|
1502
|
+
out_buf.m_expandable = MZ_TRUE;
|
|
1503
|
+
out_buf.m_capacity = 57 + MZ_MAX(64, (1 + bpl) * h);
|
|
1504
|
+
if (NULL == (out_buf.m_pBuf = (mz_uint8 *)MZ_MALLOC(out_buf.m_capacity)))
|
|
1505
|
+
{
|
|
1506
|
+
MZ_FREE(pComp);
|
|
1507
|
+
return NULL;
|
|
1508
|
+
}
|
|
1509
|
+
/* write dummy header */
|
|
1510
|
+
for (z = 41; z; --z)
|
|
1511
|
+
tdefl_output_buffer_putter(&z, 1, &out_buf);
|
|
1512
|
+
/* compress image data */
|
|
1513
|
+
tdefl_init(pComp, tdefl_output_buffer_putter, &out_buf, s_tdefl_png_num_probes[MZ_MIN(10, level)] | TDEFL_WRITE_ZLIB_HEADER);
|
|
1514
|
+
for (y = 0; y < h; ++y)
|
|
1515
|
+
{
|
|
1516
|
+
tdefl_compress_buffer(pComp, &z, 1, TDEFL_NO_FLUSH);
|
|
1517
|
+
tdefl_compress_buffer(pComp, (mz_uint8 *)pImage + (flip ? (h - 1 - y) : y) * bpl, bpl, TDEFL_NO_FLUSH);
|
|
1518
|
+
}
|
|
1519
|
+
if (tdefl_compress_buffer(pComp, NULL, 0, TDEFL_FINISH) != TDEFL_STATUS_DONE)
|
|
1520
|
+
{
|
|
1521
|
+
MZ_FREE(pComp);
|
|
1522
|
+
MZ_FREE(out_buf.m_pBuf);
|
|
1523
|
+
return NULL;
|
|
1524
|
+
}
|
|
1525
|
+
/* write real header */
|
|
1526
|
+
*pLen_out = out_buf.m_size - 41;
|
|
1527
|
+
{
|
|
1528
|
+
static const mz_uint8 chans[] = { 0x00, 0x00, 0x04, 0x02, 0x06 };
|
|
1529
|
+
mz_uint8 pnghdr[41] = { 0x89, 0x50, 0x4e, 0x47, 0x0d,
|
|
1530
|
+
0x0a, 0x1a, 0x0a, 0x00, 0x00,
|
|
1531
|
+
0x00, 0x0d, 0x49, 0x48, 0x44,
|
|
1532
|
+
0x52, 0x00, 0x00, 0x00, 0x00,
|
|
1533
|
+
0x00, 0x00, 0x00, 0x00, 0x08,
|
|
1534
|
+
0x00, 0x00, 0x00, 0x00, 0x00,
|
|
1535
|
+
0x00, 0x00, 0x00, 0x00, 0x00,
|
|
1536
|
+
0x00, 0x00, 0x49, 0x44, 0x41,
|
|
1537
|
+
0x54 };
|
|
1538
|
+
pnghdr[18] = (mz_uint8)(w >> 8);
|
|
1539
|
+
pnghdr[19] = (mz_uint8)w;
|
|
1540
|
+
pnghdr[22] = (mz_uint8)(h >> 8);
|
|
1541
|
+
pnghdr[23] = (mz_uint8)h;
|
|
1542
|
+
pnghdr[25] = chans[num_chans];
|
|
1543
|
+
pnghdr[33] = (mz_uint8)(*pLen_out >> 24);
|
|
1544
|
+
pnghdr[34] = (mz_uint8)(*pLen_out >> 16);
|
|
1545
|
+
pnghdr[35] = (mz_uint8)(*pLen_out >> 8);
|
|
1546
|
+
pnghdr[36] = (mz_uint8)*pLen_out;
|
|
1547
|
+
c = (mz_uint32)mz_crc32(MZ_CRC32_INIT, pnghdr + 12, 17);
|
|
1548
|
+
for (i = 0; i < 4; ++i, c <<= 8)
|
|
1549
|
+
((mz_uint8 *)(pnghdr + 29))[i] = (mz_uint8)(c >> 24);
|
|
1550
|
+
memcpy(out_buf.m_pBuf, pnghdr, 41);
|
|
1551
|
+
}
|
|
1552
|
+
/* write footer (IDAT CRC-32, followed by IEND chunk) */
|
|
1553
|
+
if (!tdefl_output_buffer_putter("\0\0\0\0\0\0\0\0\x49\x45\x4e\x44\xae\x42\x60\x82", 16, &out_buf))
|
|
1554
|
+
{
|
|
1555
|
+
*pLen_out = 0;
|
|
1556
|
+
MZ_FREE(pComp);
|
|
1557
|
+
MZ_FREE(out_buf.m_pBuf);
|
|
1558
|
+
return NULL;
|
|
1559
|
+
}
|
|
1560
|
+
c = (mz_uint32)mz_crc32(MZ_CRC32_INIT, out_buf.m_pBuf + 41 - 4, *pLen_out + 4);
|
|
1561
|
+
for (i = 0; i < 4; ++i, c <<= 8)
|
|
1562
|
+
(out_buf.m_pBuf + out_buf.m_size - 16)[i] = (mz_uint8)(c >> 24);
|
|
1563
|
+
/* compute final size of file, grab compressed data buffer and return */
|
|
1564
|
+
*pLen_out += 57;
|
|
1565
|
+
MZ_FREE(pComp);
|
|
1566
|
+
return out_buf.m_pBuf;
|
|
1567
|
+
}
|
|
1568
|
+
void *tdefl_write_image_to_png_file_in_memory(const void *pImage, int w, int h, int num_chans, size_t *pLen_out)
|
|
1569
|
+
{
|
|
1570
|
+
/* Level 6 corresponds to TDEFL_DEFAULT_MAX_PROBES or MZ_DEFAULT_LEVEL (but we can't depend on MZ_DEFAULT_LEVEL being available in case the zlib API's where #defined out) */
|
|
1571
|
+
return tdefl_write_image_to_png_file_in_memory_ex(pImage, w, h, num_chans, pLen_out, 6, MZ_FALSE);
|
|
1572
|
+
}
|
|
1573
|
+
|
|
1574
|
+
#ifndef MINIZ_NO_MALLOC
|
|
1575
|
+
/* Allocate the tdefl_compressor and tinfl_decompressor structures in C so that */
|
|
1576
|
+
/* non-C language bindings to tdefL_ and tinfl_ API don't need to worry about */
|
|
1577
|
+
/* structure size and allocation mechanism. */
|
|
1578
|
+
tdefl_compressor *tdefl_compressor_alloc(void)
|
|
1579
|
+
{
|
|
1580
|
+
return (tdefl_compressor *)MZ_MALLOC(sizeof(tdefl_compressor));
|
|
1581
|
+
}
|
|
1582
|
+
|
|
1583
|
+
void tdefl_compressor_free(tdefl_compressor *pComp)
|
|
1584
|
+
{
|
|
1585
|
+
MZ_FREE(pComp);
|
|
1586
|
+
}
|
|
1587
|
+
#endif
|
|
1588
|
+
|
|
1589
|
+
#ifdef _MSC_VER
|
|
1590
|
+
#pragma warning(pop)
|
|
1591
|
+
#endif
|
|
1592
|
+
|
|
1593
|
+
#ifdef __cplusplus
|
|
1594
|
+
}
|
|
1595
|
+
#endif
|
|
1596
|
+
|
|
1597
|
+
#endif /*#ifndef MINIZ_NO_DEFLATE_APIS*/
|