extzstd 0.0.3.CONCEPT → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (138) hide show
  1. checksums.yaml +5 -5
  2. data/HISTORY.ja.md +39 -0
  3. data/LICENSE +6 -6
  4. data/README.md +26 -45
  5. data/contrib/zstd/CHANGELOG +555 -0
  6. data/contrib/zstd/CODE_OF_CONDUCT.md +5 -0
  7. data/contrib/zstd/CONTRIBUTING.md +392 -0
  8. data/contrib/zstd/COPYING +339 -0
  9. data/contrib/zstd/LICENSE +13 -9
  10. data/contrib/zstd/Makefile +414 -0
  11. data/contrib/zstd/README.md +170 -45
  12. data/contrib/zstd/TESTING.md +44 -0
  13. data/contrib/zstd/appveyor.yml +289 -0
  14. data/contrib/zstd/lib/BUCK +234 -0
  15. data/contrib/zstd/lib/Makefile +354 -0
  16. data/contrib/zstd/lib/README.md +179 -0
  17. data/contrib/zstd/{common → lib/common}/bitstream.h +170 -130
  18. data/contrib/zstd/lib/common/compiler.h +175 -0
  19. data/contrib/zstd/lib/common/cpu.h +215 -0
  20. data/contrib/zstd/lib/common/debug.c +24 -0
  21. data/contrib/zstd/lib/common/debug.h +114 -0
  22. data/contrib/zstd/{common → lib/common}/entropy_common.c +79 -94
  23. data/contrib/zstd/lib/common/error_private.c +55 -0
  24. data/contrib/zstd/lib/common/error_private.h +80 -0
  25. data/contrib/zstd/{common → lib/common}/fse.h +153 -93
  26. data/contrib/zstd/{common → lib/common}/fse_decompress.c +37 -82
  27. data/contrib/zstd/lib/common/huf.h +340 -0
  28. data/contrib/zstd/{common → lib/common}/mem.h +154 -78
  29. data/contrib/zstd/lib/common/pool.c +344 -0
  30. data/contrib/zstd/lib/common/pool.h +84 -0
  31. data/contrib/zstd/lib/common/threading.c +121 -0
  32. data/contrib/zstd/lib/common/threading.h +155 -0
  33. data/contrib/zstd/{common → lib/common}/xxhash.c +85 -75
  34. data/contrib/zstd/{common → lib/common}/xxhash.h +85 -73
  35. data/contrib/zstd/lib/common/zstd_common.c +83 -0
  36. data/contrib/zstd/lib/common/zstd_errors.h +94 -0
  37. data/contrib/zstd/lib/common/zstd_internal.h +447 -0
  38. data/contrib/zstd/{compress → lib/compress}/fse_compress.c +194 -303
  39. data/contrib/zstd/lib/compress/hist.c +183 -0
  40. data/contrib/zstd/lib/compress/hist.h +75 -0
  41. data/contrib/zstd/lib/compress/huf_compress.c +798 -0
  42. data/contrib/zstd/lib/compress/zstd_compress.c +4278 -0
  43. data/contrib/zstd/lib/compress/zstd_compress_internal.h +1125 -0
  44. data/contrib/zstd/lib/compress/zstd_compress_literals.c +158 -0
  45. data/contrib/zstd/lib/compress/zstd_compress_literals.h +29 -0
  46. data/contrib/zstd/lib/compress/zstd_compress_sequences.c +419 -0
  47. data/contrib/zstd/lib/compress/zstd_compress_sequences.h +54 -0
  48. data/contrib/zstd/lib/compress/zstd_compress_superblock.c +845 -0
  49. data/contrib/zstd/lib/compress/zstd_compress_superblock.h +32 -0
  50. data/contrib/zstd/lib/compress/zstd_cwksp.h +525 -0
  51. data/contrib/zstd/lib/compress/zstd_double_fast.c +521 -0
  52. data/contrib/zstd/lib/compress/zstd_double_fast.h +38 -0
  53. data/contrib/zstd/lib/compress/zstd_fast.c +496 -0
  54. data/contrib/zstd/lib/compress/zstd_fast.h +37 -0
  55. data/contrib/zstd/lib/compress/zstd_lazy.c +1138 -0
  56. data/contrib/zstd/lib/compress/zstd_lazy.h +67 -0
  57. data/contrib/zstd/lib/compress/zstd_ldm.c +619 -0
  58. data/contrib/zstd/lib/compress/zstd_ldm.h +110 -0
  59. data/contrib/zstd/lib/compress/zstd_opt.c +1200 -0
  60. data/contrib/zstd/lib/compress/zstd_opt.h +56 -0
  61. data/contrib/zstd/lib/compress/zstdmt_compress.c +2143 -0
  62. data/contrib/zstd/lib/compress/zstdmt_compress.h +192 -0
  63. data/contrib/zstd/lib/decompress/huf_decompress.c +1248 -0
  64. data/contrib/zstd/lib/decompress/zstd_ddict.c +244 -0
  65. data/contrib/zstd/lib/decompress/zstd_ddict.h +44 -0
  66. data/contrib/zstd/lib/decompress/zstd_decompress.c +1885 -0
  67. data/contrib/zstd/lib/decompress/zstd_decompress_block.c +1432 -0
  68. data/contrib/zstd/lib/decompress/zstd_decompress_block.h +59 -0
  69. data/contrib/zstd/lib/decompress/zstd_decompress_internal.h +189 -0
  70. data/contrib/zstd/{common → lib/deprecated}/zbuff.h +86 -69
  71. data/contrib/zstd/lib/deprecated/zbuff_common.c +26 -0
  72. data/contrib/zstd/lib/deprecated/zbuff_compress.c +147 -0
  73. data/contrib/zstd/lib/deprecated/zbuff_decompress.c +75 -0
  74. data/contrib/zstd/lib/dictBuilder/cover.c +1236 -0
  75. data/contrib/zstd/lib/dictBuilder/cover.h +157 -0
  76. data/contrib/zstd/{dictBuilder → lib/dictBuilder}/divsufsort.c +3 -3
  77. data/contrib/zstd/{dictBuilder → lib/dictBuilder}/divsufsort.h +5 -5
  78. data/contrib/zstd/lib/dictBuilder/fastcover.c +757 -0
  79. data/contrib/zstd/{dictBuilder → lib/dictBuilder}/zdict.c +437 -347
  80. data/contrib/zstd/lib/dictBuilder/zdict.h +305 -0
  81. data/contrib/zstd/lib/legacy/zstd_legacy.h +415 -0
  82. data/contrib/zstd/{legacy → lib/legacy}/zstd_v01.c +272 -292
  83. data/contrib/zstd/{legacy → lib/legacy}/zstd_v01.h +26 -32
  84. data/contrib/zstd/{legacy → lib/legacy}/zstd_v02.c +162 -392
  85. data/contrib/zstd/{legacy → lib/legacy}/zstd_v02.h +26 -32
  86. data/contrib/zstd/{legacy → lib/legacy}/zstd_v03.c +162 -391
  87. data/contrib/zstd/{legacy → lib/legacy}/zstd_v03.h +27 -33
  88. data/contrib/zstd/{legacy → lib/legacy}/zstd_v04.c +195 -604
  89. data/contrib/zstd/{legacy → lib/legacy}/zstd_v04.h +26 -32
  90. data/contrib/zstd/{legacy → lib/legacy}/zstd_v05.c +300 -575
  91. data/contrib/zstd/{legacy → lib/legacy}/zstd_v05.h +22 -31
  92. data/contrib/zstd/{legacy → lib/legacy}/zstd_v06.c +165 -592
  93. data/contrib/zstd/{legacy → lib/legacy}/zstd_v06.h +54 -67
  94. data/contrib/zstd/lib/legacy/zstd_v07.c +4541 -0
  95. data/contrib/zstd/lib/legacy/zstd_v07.h +187 -0
  96. data/contrib/zstd/lib/libzstd.pc.in +15 -0
  97. data/contrib/zstd/lib/zstd.h +2090 -0
  98. data/ext/depend +2 -0
  99. data/ext/extconf.rb +18 -5
  100. data/ext/extzstd.c +296 -214
  101. data/ext/extzstd.h +81 -36
  102. data/ext/extzstd_nogvls.h +0 -117
  103. data/ext/extzstd_stream.c +622 -0
  104. data/ext/libzstd_conf.h +8 -0
  105. data/ext/zstd_common.c +11 -0
  106. data/ext/zstd_compress.c +15 -0
  107. data/ext/zstd_decompress.c +6 -0
  108. data/ext/zstd_dictbuilder.c +10 -0
  109. data/ext/zstd_dictbuilder_fastcover.c +3 -0
  110. data/ext/zstd_legacy_v01.c +3 -1
  111. data/ext/zstd_legacy_v02.c +3 -1
  112. data/ext/zstd_legacy_v03.c +3 -1
  113. data/ext/zstd_legacy_v04.c +3 -1
  114. data/ext/zstd_legacy_v05.c +3 -1
  115. data/ext/zstd_legacy_v06.c +3 -1
  116. data/ext/zstd_legacy_v07.c +3 -0
  117. data/gemstub.rb +27 -21
  118. data/lib/extzstd.rb +82 -161
  119. data/lib/extzstd/version.rb +1 -1
  120. data/test/test_basic.rb +19 -6
  121. metadata +127 -59
  122. data/contrib/zstd/common/error_private.h +0 -125
  123. data/contrib/zstd/common/error_public.h +0 -77
  124. data/contrib/zstd/common/huf.h +0 -228
  125. data/contrib/zstd/common/zstd.h +0 -475
  126. data/contrib/zstd/common/zstd_common.c +0 -91
  127. data/contrib/zstd/common/zstd_internal.h +0 -238
  128. data/contrib/zstd/compress/huf_compress.c +0 -577
  129. data/contrib/zstd/compress/zbuff_compress.c +0 -327
  130. data/contrib/zstd/compress/zstd_compress.c +0 -3074
  131. data/contrib/zstd/compress/zstd_opt.h +0 -1046
  132. data/contrib/zstd/decompress/huf_decompress.c +0 -894
  133. data/contrib/zstd/decompress/zbuff_decompress.c +0 -294
  134. data/contrib/zstd/decompress/zstd_decompress.c +0 -1362
  135. data/contrib/zstd/dictBuilder/zdict.h +0 -113
  136. data/contrib/zstd/legacy/zstd_legacy.h +0 -140
  137. data/ext/extzstd_buffered.c +0 -265
  138. data/ext/zstd_amalgam.c +0 -18
@@ -1,91 +0,0 @@
1
- /*
2
- Common functions of Zstd compression library
3
- Copyright (C) 2015-2016, Yann Collet.
4
-
5
- BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
6
-
7
- Redistribution and use in source and binary forms, with or without
8
- modification, are permitted provided that the following conditions are
9
- met:
10
- * Redistributions of source code must retain the above copyright
11
- notice, this list of conditions and the following disclaimer.
12
- * Redistributions in binary form must reproduce the above
13
- copyright notice, this list of conditions and the following disclaimer
14
- in the documentation and/or other materials provided with the
15
- distribution.
16
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17
- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18
- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19
- A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20
- OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
- LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26
- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
-
28
- You can contact the author at :
29
- - zstd homepage : http://www.zstd.net/
30
- */
31
-
32
-
33
- /*-*************************************
34
- * Dependencies
35
- ***************************************/
36
- #include <stdlib.h> /* malloc */
37
- #include "error_private.h"
38
- #define ZSTD_STATIC_LINKING_ONLY
39
- #include "zstd.h" /* declaration of ZSTD_isError, ZSTD_getErrorName, ZSTD_getErrorCode, ZSTD_getErrorString, ZSTD_versionNumber */
40
- #include "zbuff.h" /* declaration of ZBUFF_isError, ZBUFF_getErrorName */
41
-
42
-
43
- /*-****************************************
44
- * Version
45
- ******************************************/
46
- unsigned ZSTD_versionNumber (void) { return ZSTD_VERSION_NUMBER; }
47
-
48
-
49
- /*-****************************************
50
- * ZSTD Error Management
51
- ******************************************/
52
- /*! ZSTD_isError() :
53
- * tells if a return value is an error code */
54
- unsigned ZSTD_isError(size_t code) { return ERR_isError(code); }
55
-
56
- /*! ZSTD_getErrorName() :
57
- * provides error code string from function result (useful for debugging) */
58
- const char* ZSTD_getErrorName(size_t code) { return ERR_getErrorName(code); }
59
-
60
- /*! ZSTD_getError() :
61
- * convert a `size_t` function result into a proper ZSTD_errorCode enum */
62
- ZSTD_ErrorCode ZSTD_getErrorCode(size_t code) { return ERR_getErrorCode(code); }
63
-
64
- /*! ZSTD_getErrorString() :
65
- * provides error code string from enum */
66
- const char* ZSTD_getErrorString(ZSTD_ErrorCode code) { return ERR_getErrorName(code); }
67
-
68
-
69
- /* **************************************************************
70
- * ZBUFF Error Management
71
- ****************************************************************/
72
- unsigned ZBUFF_isError(size_t errorCode) { return ERR_isError(errorCode); }
73
-
74
- const char* ZBUFF_getErrorName(size_t errorCode) { return ERR_getErrorName(errorCode); }
75
-
76
-
77
-
78
- void* ZSTD_defaultAllocFunction(void* opaque, size_t size)
79
- {
80
- void* address = malloc(size);
81
- (void)opaque;
82
- /* printf("alloc %p, %d opaque=%p \n", address, (int)size, opaque); */
83
- return address;
84
- }
85
-
86
- void ZSTD_defaultFreeFunction(void* opaque, void* address)
87
- {
88
- (void)opaque;
89
- /* if (address) printf("free %p opaque=%p \n", address, opaque); */
90
- free(address);
91
- }
@@ -1,238 +0,0 @@
1
- /*
2
- zstd_internal - common functions to include
3
- Header File for include
4
- Copyright (C) 2014-2016, Yann Collet.
5
-
6
- BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
7
-
8
- Redistribution and use in source and binary forms, with or without
9
- modification, are permitted provided that the following conditions are
10
- met:
11
- * Redistributions of source code must retain the above copyright
12
- notice, this list of conditions and the following disclaimer.
13
- * Redistributions in binary form must reproduce the above
14
- copyright notice, this list of conditions and the following disclaimer
15
- in the documentation and/or other materials provided with the
16
- distribution.
17
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18
- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19
- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20
- A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21
- OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22
- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23
- LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27
- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
-
29
- You can contact the author at :
30
- - zstd homepage : https://www.zstd.net
31
- */
32
- #ifndef ZSTD_CCOMMON_H_MODULE
33
- #define ZSTD_CCOMMON_H_MODULE
34
-
35
- /*-*************************************
36
- * Dependencies
37
- ***************************************/
38
- #include "mem.h"
39
- #include "error_private.h"
40
- #define ZSTD_STATIC_LINKING_ONLY
41
- #include "zstd.h"
42
-
43
-
44
- /*-*************************************
45
- * Common macros
46
- ***************************************/
47
- #define MIN(a,b) ((a)<(b) ? (a) : (b))
48
- #define MAX(a,b) ((a)>(b) ? (a) : (b))
49
-
50
-
51
- /*-*************************************
52
- * Common constants
53
- ***************************************/
54
- #define ZSTD_OPT_DEBUG 0 /* 3 = compression stats; 5 = check encoded sequences; 9 = full logs */
55
- #include <stdio.h>
56
- #if defined(ZSTD_OPT_DEBUG) && ZSTD_OPT_DEBUG>=9
57
- #define ZSTD_LOG_PARSER(...) printf(__VA_ARGS__)
58
- #define ZSTD_LOG_ENCODE(...) printf(__VA_ARGS__)
59
- #define ZSTD_LOG_BLOCK(...) printf(__VA_ARGS__)
60
- #else
61
- #define ZSTD_LOG_PARSER(...)
62
- #define ZSTD_LOG_ENCODE(...)
63
- #define ZSTD_LOG_BLOCK(...)
64
- #endif
65
-
66
- #define ZSTD_OPT_NUM (1<<12)
67
- #define ZSTD_DICT_MAGIC 0xEC30A437 /* v0.7 */
68
-
69
- #define ZSTD_REP_NUM 3
70
- #define ZSTD_REP_INIT ZSTD_REP_NUM
71
- #define ZSTD_REP_MOVE (ZSTD_REP_NUM-1)
72
- static const U32 repStartValue[ZSTD_REP_NUM] = { 1, 4, 8 };
73
-
74
- #define KB *(1 <<10)
75
- #define MB *(1 <<20)
76
- #define GB *(1U<<30)
77
-
78
- #define BIT7 128
79
- #define BIT6 64
80
- #define BIT5 32
81
- #define BIT4 16
82
- #define BIT1 2
83
- #define BIT0 1
84
-
85
- #define ZSTD_WINDOWLOG_ABSOLUTEMIN 10
86
- static const size_t ZSTD_fcs_fieldSize[4] = { 0, 2, 4, 8 };
87
- static const size_t ZSTD_did_fieldSize[4] = { 0, 1, 2, 4 };
88
-
89
- #define ZSTD_BLOCKHEADERSIZE 3 /* C standard doesn't allow `static const` variable to be init using another `static const` variable */
90
- static const size_t ZSTD_blockHeaderSize = ZSTD_BLOCKHEADERSIZE;
91
- typedef enum { bt_compressed, bt_raw, bt_rle, bt_end } blockType_t;
92
-
93
- #define MIN_SEQUENCES_SIZE 1 /* nbSeq==0 */
94
- #define MIN_CBLOCK_SIZE (1 /*litCSize*/ + 1 /* RLE or RAW */ + MIN_SEQUENCES_SIZE /* nbSeq==0 */) /* for a non-null block */
95
-
96
- #define HufLog 12
97
- typedef enum { lbt_huffman, lbt_repeat, lbt_raw, lbt_rle } litBlockType_t;
98
-
99
- #define LONGNBSEQ 0x7F00
100
-
101
- #define MINMATCH 3
102
- #define EQUAL_READ32 4
103
-
104
- #define Litbits 8
105
- #define MaxLit ((1<<Litbits) - 1)
106
- #define MaxML 52
107
- #define MaxLL 35
108
- #define MaxOff 28
109
- #define MaxSeq MAX(MaxLL, MaxML) /* Assumption : MaxOff < MaxLL,MaxML */
110
- #define MLFSELog 9
111
- #define LLFSELog 9
112
- #define OffFSELog 8
113
-
114
- #define FSE_ENCODING_RAW 0
115
- #define FSE_ENCODING_RLE 1
116
- #define FSE_ENCODING_STATIC 2
117
- #define FSE_ENCODING_DYNAMIC 3
118
-
119
- static const U32 LL_bits[MaxLL+1] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
120
- 1, 1, 1, 1, 2, 2, 3, 3, 4, 6, 7, 8, 9,10,11,12,
121
- 13,14,15,16 };
122
- static const S16 LL_defaultNorm[MaxLL+1] = { 4, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1,
123
- 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 2, 1, 1, 1, 1, 1,
124
- -1,-1,-1,-1 };
125
- static const U32 LL_defaultNormLog = 6;
126
-
127
- static const U32 ML_bits[MaxML+1] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
128
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
129
- 1, 1, 1, 1, 2, 2, 3, 3, 4, 4, 5, 7, 8, 9,10,11,
130
- 12,13,14,15,16 };
131
- static const S16 ML_defaultNorm[MaxML+1] = { 1, 4, 3, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1,
132
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
133
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,-1,-1,
134
- -1,-1,-1,-1,-1 };
135
- static const U32 ML_defaultNormLog = 6;
136
-
137
- static const S16 OF_defaultNorm[MaxOff+1] = { 1, 1, 1, 1, 1, 1, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1,
138
- 1, 1, 1, 1, 1, 1, 1, 1,-1,-1,-1,-1,-1 };
139
- static const U32 OF_defaultNormLog = 5;
140
-
141
-
142
- /*-*******************************************
143
- * Shared functions to include for inlining
144
- *********************************************/
145
- static void ZSTD_copy8(void* dst, const void* src) { memcpy(dst, src, 8); }
146
- #define COPY8(d,s) { ZSTD_copy8(d,s); d+=8; s+=8; }
147
-
148
- /*! ZSTD_wildcopy() :
149
- * custom version of memcpy(), can copy up to 7 bytes too many (8 bytes if length==0) */
150
- #define WILDCOPY_OVERLENGTH 8
151
- MEM_STATIC void ZSTD_wildcopy(void* dst, const void* src, size_t length)
152
- {
153
- const BYTE* ip = (const BYTE*)src;
154
- BYTE* op = (BYTE*)dst;
155
- BYTE* const oend = op + length;
156
- do
157
- COPY8(op, ip)
158
- while (op < oend);
159
- }
160
-
161
-
162
- /*-*******************************************
163
- * Private interfaces
164
- *********************************************/
165
- typedef struct ZSTD_stats_s ZSTD_stats_t;
166
-
167
- typedef struct {
168
- U32 off;
169
- U32 len;
170
- } ZSTD_match_t;
171
-
172
- typedef struct {
173
- U32 price;
174
- U32 off;
175
- U32 mlen;
176
- U32 litlen;
177
- U32 rep[ZSTD_REP_INIT];
178
- } ZSTD_optimal_t;
179
-
180
- #if ZSTD_OPT_DEBUG == 3
181
- #include ".debug/zstd_stats.h"
182
- #else
183
- struct ZSTD_stats_s { U32 unused; };
184
- MEM_STATIC void ZSTD_statsPrint(ZSTD_stats_t* stats, U32 searchLength) { (void)stats; (void)searchLength; }
185
- MEM_STATIC void ZSTD_statsInit(ZSTD_stats_t* stats) { (void)stats; }
186
- MEM_STATIC void ZSTD_statsResetFreqs(ZSTD_stats_t* stats) { (void)stats; }
187
- MEM_STATIC void ZSTD_statsUpdatePrices(ZSTD_stats_t* stats, size_t litLength, const BYTE* literals, size_t offset, size_t matchLength) { (void)stats; (void)litLength; (void)literals; (void)offset; (void)matchLength; }
188
- #endif /* #if ZSTD_OPT_DEBUG == 3 */
189
-
190
- typedef struct {
191
- void* buffer;
192
- U32* offsetStart;
193
- U32* offset;
194
- BYTE* offCodeStart;
195
- BYTE* litStart;
196
- BYTE* lit;
197
- U16* litLengthStart;
198
- U16* litLength;
199
- BYTE* llCodeStart;
200
- U16* matchLengthStart;
201
- U16* matchLength;
202
- BYTE* mlCodeStart;
203
- U32 longLengthID; /* 0 == no longLength; 1 == Lit.longLength; 2 == Match.longLength; */
204
- U32 longLengthPos;
205
- /* opt */
206
- ZSTD_optimal_t* priceTable;
207
- ZSTD_match_t* matchTable;
208
- U32* matchLengthFreq;
209
- U32* litLengthFreq;
210
- U32* litFreq;
211
- U32* offCodeFreq;
212
- U32 matchLengthSum;
213
- U32 matchSum;
214
- U32 litLengthSum;
215
- U32 litSum;
216
- U32 offCodeSum;
217
- U32 log2matchLengthSum;
218
- U32 log2matchSum;
219
- U32 log2litLengthSum;
220
- U32 log2litSum;
221
- U32 log2offCodeSum;
222
- U32 factor;
223
- U32 cachedPrice;
224
- U32 cachedLitLength;
225
- const BYTE* cachedLiterals;
226
- ZSTD_stats_t stats;
227
- } seqStore_t;
228
-
229
- const seqStore_t* ZSTD_getSeqStore(const ZSTD_CCtx* ctx);
230
- void ZSTD_seqToCodes(const seqStore_t* seqStorePtr, size_t const nbSeq);
231
- int ZSTD_isSkipFrame(ZSTD_DCtx* dctx);
232
-
233
- /* custom memory allocation functions */
234
- void* ZSTD_defaultAllocFunction(void* opaque, size_t size);
235
- void ZSTD_defaultFreeFunction(void* opaque, void* address);
236
- static const ZSTD_customMem defaultCustomMem = { ZSTD_defaultAllocFunction, ZSTD_defaultFreeFunction, NULL };
237
-
238
- #endif /* ZSTD_CCOMMON_H_MODULE */
@@ -1,577 +0,0 @@
1
- /* ******************************************************************
2
- Huffman encoder, part of New Generation Entropy library
3
- Copyright (C) 2013-2016, Yann Collet.
4
-
5
- BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
6
-
7
- Redistribution and use in source and binary forms, with or without
8
- modification, are permitted provided that the following conditions are
9
- met:
10
-
11
- * Redistributions of source code must retain the above copyright
12
- notice, this list of conditions and the following disclaimer.
13
- * Redistributions in binary form must reproduce the above
14
- copyright notice, this list of conditions and the following disclaimer
15
- in the documentation and/or other materials provided with the
16
- distribution.
17
-
18
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19
- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20
- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21
- A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22
- OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23
- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24
- LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
-
30
- You can contact the author at :
31
- - FSE+HUF source repository : https://github.com/Cyan4973/FiniteStateEntropy
32
- - Public forum : https://groups.google.com/forum/#!forum/lz4c
33
- ****************************************************************** */
34
-
35
- /* **************************************************************
36
- * Compiler specifics
37
- ****************************************************************/
38
- #if defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
39
- /* inline is defined */
40
- #elif defined(_MSC_VER)
41
- # define inline __inline
42
- #else
43
- # define inline /* disable inline */
44
- #endif
45
-
46
-
47
- #ifdef _MSC_VER /* Visual Studio */
48
- # define FORCE_INLINE static __forceinline
49
- # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
50
- #else
51
- # ifdef __GNUC__
52
- # define FORCE_INLINE static inline __attribute__((always_inline))
53
- # else
54
- # define FORCE_INLINE static inline
55
- # endif
56
- #endif
57
-
58
-
59
- /* **************************************************************
60
- * Includes
61
- ****************************************************************/
62
- #include <string.h> /* memcpy, memset */
63
- #include <stdio.h> /* printf (debug) */
64
- #include "bitstream.h"
65
- #define FSE_STATIC_LINKING_ONLY /* FSE_optimalTableLog_internal */
66
- #include "fse.h" /* header compression */
67
- #define HUF_STATIC_LINKING_ONLY
68
- #include "huf.h"
69
-
70
-
71
- /* **************************************************************
72
- * Error Management
73
- ****************************************************************/
74
- #define HUF_STATIC_ASSERT(c) { enum { HUF_static_assert = 1/(int)(!!(c)) }; } /* use only *after* variable declarations */
75
-
76
-
77
- /* **************************************************************
78
- * Utils
79
- ****************************************************************/
80
- unsigned HUF_optimalTableLog(unsigned maxTableLog, size_t srcSize, unsigned maxSymbolValue)
81
- {
82
- return FSE_optimalTableLog_internal(maxTableLog, srcSize, maxSymbolValue, 1);
83
- }
84
-
85
-
86
- /* *******************************************************
87
- * HUF : Huffman block compression
88
- *********************************************************/
89
- struct HUF_CElt_s {
90
- U16 val;
91
- BYTE nbBits;
92
- }; /* typedef'd to HUF_CElt within huf_static.h */
93
-
94
- typedef struct nodeElt_s {
95
- U32 count;
96
- U16 parent;
97
- BYTE byte;
98
- BYTE nbBits;
99
- } nodeElt;
100
-
101
- /*! HUF_writeCTable() :
102
- `CTable` : huffman tree to save, using huf representation.
103
- @return : size of saved CTable */
104
- size_t HUF_writeCTable (void* dst, size_t maxDstSize,
105
- const HUF_CElt* CTable, U32 maxSymbolValue, U32 huffLog)
106
- {
107
- BYTE bitsToWeight[HUF_TABLELOG_MAX + 1];
108
- BYTE huffWeight[HUF_SYMBOLVALUE_MAX + 1];
109
- U32 n;
110
- BYTE* op = (BYTE*)dst;
111
- size_t size;
112
-
113
- /* check conditions */
114
- if (maxSymbolValue > HUF_SYMBOLVALUE_MAX + 1)
115
- return ERROR(GENERIC);
116
-
117
- /* convert to weight */
118
- bitsToWeight[0] = 0;
119
- for (n=1; n<=huffLog; n++)
120
- bitsToWeight[n] = (BYTE)(huffLog + 1 - n);
121
- for (n=0; n<maxSymbolValue; n++)
122
- huffWeight[n] = bitsToWeight[CTable[n].nbBits];
123
-
124
- size = FSE_compress(op+1, maxDstSize-1, huffWeight, maxSymbolValue); /* don't need last symbol stat : implied */
125
- if (HUF_isError(size)) return size;
126
- if (size >= 128) return ERROR(GENERIC); /* should never happen, since maxSymbolValue <= 255 */
127
- if ((size <= 1) || (size >= maxSymbolValue/2)) {
128
- if (size==1) { /* RLE */
129
- /* only possible case : series of 1 (because there are at least 2) */
130
- /* can only be 2^n or (2^n-1), otherwise not an huffman tree */
131
- BYTE code;
132
- switch(maxSymbolValue)
133
- {
134
- case 1: code = 0; break;
135
- case 2: code = 1; break;
136
- case 3: code = 2; break;
137
- case 4: code = 3; break;
138
- case 7: code = 4; break;
139
- case 8: code = 5; break;
140
- case 15: code = 6; break;
141
- case 16: code = 7; break;
142
- case 31: code = 8; break;
143
- case 32: code = 9; break;
144
- case 63: code = 10; break;
145
- case 64: code = 11; break;
146
- case 127: code = 12; break;
147
- case 128: code = 13; break;
148
- default : return ERROR(corruption_detected);
149
- }
150
- op[0] = (BYTE)(255-13 + code);
151
- return 1;
152
- }
153
- /* Not compressible */
154
- if (maxSymbolValue > (241-128)) return ERROR(GENERIC); /* not implemented (not possible with current format) */
155
- if (((maxSymbolValue+1)/2) + 1 > maxDstSize) return ERROR(dstSize_tooSmall); /* not enough space within dst buffer */
156
- op[0] = (BYTE)(128 /*special case*/ + 0 /* Not Compressible */ + (maxSymbolValue-1));
157
- huffWeight[maxSymbolValue] = 0; /* to be sure it doesn't cause issue in final combination */
158
- for (n=0; n<maxSymbolValue; n+=2)
159
- op[(n/2)+1] = (BYTE)((huffWeight[n] << 4) + huffWeight[n+1]);
160
- return ((maxSymbolValue+1)/2) + 1;
161
- }
162
-
163
- /* normal header case */
164
- op[0] = (BYTE)size;
165
- return size+1;
166
- }
167
-
168
-
169
-
170
- size_t HUF_readCTable (HUF_CElt* CTable, U32 maxSymbolValue, const void* src, size_t srcSize)
171
- {
172
- BYTE huffWeight[HUF_SYMBOLVALUE_MAX + 1];
173
- U32 rankVal[HUF_TABLELOG_ABSOLUTEMAX + 1]; /* large enough for values from 0 to 16 */
174
- U32 tableLog = 0;
175
- size_t readSize;
176
- U32 nbSymbols = 0;
177
- //memset(huffWeight, 0, sizeof(huffWeight)); /* is not necessary, even though some analyzer complain ... */
178
-
179
- /* get symbol weights */
180
- readSize = HUF_readStats(huffWeight, HUF_SYMBOLVALUE_MAX+1, rankVal, &nbSymbols, &tableLog, src, srcSize);
181
- if (HUF_isError(readSize)) return readSize;
182
-
183
- /* check result */
184
- if (tableLog > HUF_TABLELOG_MAX) return ERROR(tableLog_tooLarge);
185
- if (nbSymbols > maxSymbolValue+1) return ERROR(maxSymbolValue_tooSmall);
186
-
187
- /* Prepare base value per rank */
188
- { U32 n, nextRankStart = 0;
189
- for (n=1; n<=tableLog; n++) {
190
- U32 current = nextRankStart;
191
- nextRankStart += (rankVal[n] << (n-1));
192
- rankVal[n] = current;
193
- } }
194
-
195
- /* fill nbBits */
196
- { U32 n; for (n=0; n<nbSymbols; n++) {
197
- const U32 w = huffWeight[n];
198
- CTable[n].nbBits = (BYTE)(tableLog + 1 - w);
199
- }}
200
-
201
- /* fill val */
202
- { U16 nbPerRank[HUF_TABLELOG_MAX+1] = {0};
203
- U16 valPerRank[HUF_TABLELOG_MAX+1] = {0};
204
- { U32 n; for (n=0; n<nbSymbols; n++) nbPerRank[CTable[n].nbBits]++; }
205
- /* determine stating value per rank */
206
- { U16 min = 0;
207
- U32 n; for (n=HUF_TABLELOG_MAX; n>0; n--) {
208
- valPerRank[n] = min; /* get starting value within each rank */
209
- min += nbPerRank[n];
210
- min >>= 1;
211
- } }
212
- /* assign value within rank, symbol order */
213
- { U32 n; for (n=0; n<=maxSymbolValue; n++) CTable[n].val = valPerRank[CTable[n].nbBits]++; }
214
- }
215
-
216
- return readSize;
217
- }
218
-
219
-
220
- static U32 HUF_setMaxHeight(nodeElt* huffNode, U32 lastNonNull, U32 maxNbBits)
221
- {
222
- const U32 largestBits = huffNode[lastNonNull].nbBits;
223
- if (largestBits <= maxNbBits) return largestBits; /* early exit : no elt > maxNbBits */
224
-
225
- /* there are several too large elements (at least >= 2) */
226
- { int totalCost = 0;
227
- const U32 baseCost = 1 << (largestBits - maxNbBits);
228
- U32 n = lastNonNull;
229
-
230
- while (huffNode[n].nbBits > maxNbBits) {
231
- totalCost += baseCost - (1 << (largestBits - huffNode[n].nbBits));
232
- huffNode[n].nbBits = (BYTE)maxNbBits;
233
- n --;
234
- } /* n stops at huffNode[n].nbBits <= maxNbBits */
235
- while (huffNode[n].nbBits == maxNbBits) n--; /* n end at index of smallest symbol using < maxNbBits */
236
-
237
- /* renorm totalCost */
238
- totalCost >>= (largestBits - maxNbBits); /* note : totalCost is necessarily a multiple of baseCost */
239
-
240
- /* repay normalized cost */
241
- { U32 const noSymbol = 0xF0F0F0F0;
242
- U32 rankLast[HUF_TABLELOG_MAX+2];
243
- int pos;
244
-
245
- /* Get pos of last (smallest) symbol per rank */
246
- memset(rankLast, 0xF0, sizeof(rankLast));
247
- { U32 currentNbBits = maxNbBits;
248
- for (pos=n ; pos >= 0; pos--) {
249
- if (huffNode[pos].nbBits >= currentNbBits) continue;
250
- currentNbBits = huffNode[pos].nbBits; /* < maxNbBits */
251
- rankLast[maxNbBits-currentNbBits] = pos;
252
- } }
253
-
254
- while (totalCost > 0) {
255
- U32 nBitsToDecrease = BIT_highbit32(totalCost) + 1;
256
- for ( ; nBitsToDecrease > 1; nBitsToDecrease--) {
257
- U32 highPos = rankLast[nBitsToDecrease];
258
- U32 lowPos = rankLast[nBitsToDecrease-1];
259
- if (highPos == noSymbol) continue;
260
- if (lowPos == noSymbol) break;
261
- { U32 const highTotal = huffNode[highPos].count;
262
- U32 const lowTotal = 2 * huffNode[lowPos].count;
263
- if (highTotal <= lowTotal) break;
264
- } }
265
- /* only triggered when no more rank 1 symbol left => find closest one (note : there is necessarily at least one !) */
266
- while ((nBitsToDecrease<=HUF_TABLELOG_MAX) && (rankLast[nBitsToDecrease] == noSymbol)) /* HUF_MAX_TABLELOG test just to please gcc 5+; but it should not be necessary */
267
- nBitsToDecrease ++;
268
- totalCost -= 1 << (nBitsToDecrease-1);
269
- if (rankLast[nBitsToDecrease-1] == noSymbol)
270
- rankLast[nBitsToDecrease-1] = rankLast[nBitsToDecrease]; /* this rank is no longer empty */
271
- huffNode[rankLast[nBitsToDecrease]].nbBits ++;
272
- if (rankLast[nBitsToDecrease] == 0) /* special case, reached largest symbol */
273
- rankLast[nBitsToDecrease] = noSymbol;
274
- else {
275
- rankLast[nBitsToDecrease]--;
276
- if (huffNode[rankLast[nBitsToDecrease]].nbBits != maxNbBits-nBitsToDecrease)
277
- rankLast[nBitsToDecrease] = noSymbol; /* this rank is now empty */
278
- } } /* while (totalCost > 0) */
279
-
280
- while (totalCost < 0) { /* Sometimes, cost correction overshoot */
281
- if (rankLast[1] == noSymbol) { /* special case : no rank 1 symbol (using maxNbBits-1); let's create one from largest rank 0 (using maxNbBits) */
282
- while (huffNode[n].nbBits == maxNbBits) n--;
283
- huffNode[n+1].nbBits--;
284
- rankLast[1] = n+1;
285
- totalCost++;
286
- continue;
287
- }
288
- huffNode[ rankLast[1] + 1 ].nbBits--;
289
- rankLast[1]++;
290
- totalCost ++;
291
- } } } /* there are several too large elements (at least >= 2) */
292
-
293
- return maxNbBits;
294
- }
295
-
296
-
297
- typedef struct {
298
- U32 base;
299
- U32 current;
300
- } rankPos;
301
-
302
- static void HUF_sort(nodeElt* huffNode, const U32* count, U32 maxSymbolValue)
303
- {
304
- rankPos rank[32];
305
- U32 n;
306
-
307
- memset(rank, 0, sizeof(rank));
308
- for (n=0; n<=maxSymbolValue; n++) {
309
- U32 r = BIT_highbit32(count[n] + 1);
310
- rank[r].base ++;
311
- }
312
- for (n=30; n>0; n--) rank[n-1].base += rank[n].base;
313
- for (n=0; n<32; n++) rank[n].current = rank[n].base;
314
- for (n=0; n<=maxSymbolValue; n++) {
315
- U32 const c = count[n];
316
- U32 const r = BIT_highbit32(c+1) + 1;
317
- U32 pos = rank[r].current++;
318
- while ((pos > rank[r].base) && (c > huffNode[pos-1].count)) huffNode[pos]=huffNode[pos-1], pos--;
319
- huffNode[pos].count = c;
320
- huffNode[pos].byte = (BYTE)n;
321
- }
322
- }
323
-
324
-
325
- #define STARTNODE (HUF_SYMBOLVALUE_MAX+1)
326
- size_t HUF_buildCTable (HUF_CElt* tree, const U32* count, U32 maxSymbolValue, U32 maxNbBits)
327
- {
328
- nodeElt huffNode0[2*HUF_SYMBOLVALUE_MAX+1 +1];
329
- nodeElt* huffNode = huffNode0 + 1;
330
- U32 n, nonNullRank;
331
- int lowS, lowN;
332
- U16 nodeNb = STARTNODE;
333
- U32 nodeRoot;
334
-
335
- /* safety checks */
336
- if (maxNbBits == 0) maxNbBits = HUF_TABLELOG_DEFAULT;
337
- if (maxSymbolValue > HUF_SYMBOLVALUE_MAX) return ERROR(GENERIC);
338
- memset(huffNode0, 0, sizeof(huffNode0));
339
-
340
- /* sort, decreasing order */
341
- HUF_sort(huffNode, count, maxSymbolValue);
342
-
343
- /* init for parents */
344
- nonNullRank = maxSymbolValue;
345
- while(huffNode[nonNullRank].count == 0) nonNullRank--;
346
- lowS = nonNullRank; nodeRoot = nodeNb + lowS - 1; lowN = nodeNb;
347
- huffNode[nodeNb].count = huffNode[lowS].count + huffNode[lowS-1].count;
348
- huffNode[lowS].parent = huffNode[lowS-1].parent = nodeNb;
349
- nodeNb++; lowS-=2;
350
- for (n=nodeNb; n<=nodeRoot; n++) huffNode[n].count = (U32)(1U<<30);
351
- huffNode0[0].count = (U32)(1U<<31);
352
-
353
- /* create parents */
354
- while (nodeNb <= nodeRoot) {
355
- U32 n1 = (huffNode[lowS].count < huffNode[lowN].count) ? lowS-- : lowN++;
356
- U32 n2 = (huffNode[lowS].count < huffNode[lowN].count) ? lowS-- : lowN++;
357
- huffNode[nodeNb].count = huffNode[n1].count + huffNode[n2].count;
358
- huffNode[n1].parent = huffNode[n2].parent = nodeNb;
359
- nodeNb++;
360
- }
361
-
362
- /* distribute weights (unlimited tree height) */
363
- huffNode[nodeRoot].nbBits = 0;
364
- for (n=nodeRoot-1; n>=STARTNODE; n--)
365
- huffNode[n].nbBits = huffNode[ huffNode[n].parent ].nbBits + 1;
366
- for (n=0; n<=nonNullRank; n++)
367
- huffNode[n].nbBits = huffNode[ huffNode[n].parent ].nbBits + 1;
368
-
369
- /* enforce maxTableLog */
370
- maxNbBits = HUF_setMaxHeight(huffNode, nonNullRank, maxNbBits);
371
-
372
- /* fill result into tree (val, nbBits) */
373
- { U16 nbPerRank[HUF_TABLELOG_MAX+1] = {0};
374
- U16 valPerRank[HUF_TABLELOG_MAX+1] = {0};
375
- if (maxNbBits > HUF_TABLELOG_MAX) return ERROR(GENERIC); /* check fit into table */
376
- for (n=0; n<=nonNullRank; n++)
377
- nbPerRank[huffNode[n].nbBits]++;
378
- /* determine stating value per rank */
379
- { U16 min = 0;
380
- for (n=maxNbBits; n>0; n--) {
381
- valPerRank[n] = min; /* get starting value within each rank */
382
- min += nbPerRank[n];
383
- min >>= 1;
384
- } }
385
- for (n=0; n<=maxSymbolValue; n++)
386
- tree[huffNode[n].byte].nbBits = huffNode[n].nbBits; /* push nbBits per symbol, symbol order */
387
- for (n=0; n<=maxSymbolValue; n++)
388
- tree[n].val = valPerRank[tree[n].nbBits]++; /* assign value within rank, symbol order */
389
- }
390
-
391
- return maxNbBits;
392
- }
393
-
394
- static void HUF_encodeSymbol(BIT_CStream_t* bitCPtr, U32 symbol, const HUF_CElt* CTable)
395
- {
396
- BIT_addBitsFast(bitCPtr, CTable[symbol].val, CTable[symbol].nbBits);
397
- }
398
-
399
- size_t HUF_compressBound(size_t size) { return HUF_COMPRESSBOUND(size); }
400
-
401
- #define HUF_FLUSHBITS(s) (fast ? BIT_flushBitsFast(s) : BIT_flushBits(s))
402
-
403
- #define HUF_FLUSHBITS_1(stream) \
404
- if (sizeof((stream)->bitContainer)*8 < HUF_TABLELOG_MAX*2+7) HUF_FLUSHBITS(stream)
405
-
406
- #define HUF_FLUSHBITS_2(stream) \
407
- if (sizeof((stream)->bitContainer)*8 < HUF_TABLELOG_MAX*4+7) HUF_FLUSHBITS(stream)
408
-
409
- size_t HUF_compress1X_usingCTable(void* dst, size_t dstSize, const void* src, size_t srcSize, const HUF_CElt* CTable)
410
- {
411
- const BYTE* ip = (const BYTE*) src;
412
- BYTE* const ostart = (BYTE*)dst;
413
- BYTE* const oend = ostart + dstSize;
414
- BYTE* op = ostart;
415
- size_t n;
416
- const unsigned fast = (dstSize >= HUF_BLOCKBOUND(srcSize));
417
- BIT_CStream_t bitC;
418
-
419
- /* init */
420
- if (dstSize < 8) return 0; /* not enough space to compress */
421
- { size_t const errorCode = BIT_initCStream(&bitC, op, oend-op);
422
- if (HUF_isError(errorCode)) return 0; }
423
-
424
- n = srcSize & ~3; /* join to mod 4 */
425
- switch (srcSize & 3)
426
- {
427
- case 3 : HUF_encodeSymbol(&bitC, ip[n+ 2], CTable);
428
- HUF_FLUSHBITS_2(&bitC);
429
- case 2 : HUF_encodeSymbol(&bitC, ip[n+ 1], CTable);
430
- HUF_FLUSHBITS_1(&bitC);
431
- case 1 : HUF_encodeSymbol(&bitC, ip[n+ 0], CTable);
432
- HUF_FLUSHBITS(&bitC);
433
- case 0 :
434
- default: ;
435
- }
436
-
437
- for (; n>0; n-=4) { /* note : n&3==0 at this stage */
438
- HUF_encodeSymbol(&bitC, ip[n- 1], CTable);
439
- HUF_FLUSHBITS_1(&bitC);
440
- HUF_encodeSymbol(&bitC, ip[n- 2], CTable);
441
- HUF_FLUSHBITS_2(&bitC);
442
- HUF_encodeSymbol(&bitC, ip[n- 3], CTable);
443
- HUF_FLUSHBITS_1(&bitC);
444
- HUF_encodeSymbol(&bitC, ip[n- 4], CTable);
445
- HUF_FLUSHBITS(&bitC);
446
- }
447
-
448
- return BIT_closeCStream(&bitC);
449
- }
450
-
451
-
452
- size_t HUF_compress4X_usingCTable(void* dst, size_t dstSize, const void* src, size_t srcSize, const HUF_CElt* CTable)
453
- {
454
- size_t const segmentSize = (srcSize+3)/4; /* first 3 segments */
455
- const BYTE* ip = (const BYTE*) src;
456
- const BYTE* const iend = ip + srcSize;
457
- BYTE* const ostart = (BYTE*) dst;
458
- BYTE* const oend = ostart + dstSize;
459
- BYTE* op = ostart;
460
-
461
- if (dstSize < 6 + 1 + 1 + 1 + 8) return 0; /* minimum space to compress successfully */
462
- if (srcSize < 12) return 0; /* no saving possible : too small input */
463
- op += 6; /* jumpTable */
464
-
465
- { size_t const cSize = HUF_compress1X_usingCTable(op, oend-op, ip, segmentSize, CTable);
466
- if (HUF_isError(cSize)) return cSize;
467
- if (cSize==0) return 0;
468
- MEM_writeLE16(ostart, (U16)cSize);
469
- op += cSize;
470
- }
471
-
472
- ip += segmentSize;
473
- { size_t const cSize = HUF_compress1X_usingCTable(op, oend-op, ip, segmentSize, CTable);
474
- if (HUF_isError(cSize)) return cSize;
475
- if (cSize==0) return 0;
476
- MEM_writeLE16(ostart+2, (U16)cSize);
477
- op += cSize;
478
- }
479
-
480
- ip += segmentSize;
481
- { size_t const cSize = HUF_compress1X_usingCTable(op, oend-op, ip, segmentSize, CTable);
482
- if (HUF_isError(cSize)) return cSize;
483
- if (cSize==0) return 0;
484
- MEM_writeLE16(ostart+4, (U16)cSize);
485
- op += cSize;
486
- }
487
-
488
- ip += segmentSize;
489
- { size_t const cSize = HUF_compress1X_usingCTable(op, oend-op, ip, iend-ip, CTable);
490
- if (HUF_isError(cSize)) return cSize;
491
- if (cSize==0) return 0;
492
- op += cSize;
493
- }
494
-
495
- return op-ostart;
496
- }
497
-
498
-
499
- static size_t HUF_compress_internal (
500
- void* dst, size_t dstSize,
501
- const void* src, size_t srcSize,
502
- unsigned maxSymbolValue, unsigned huffLog,
503
- unsigned singleStream)
504
- {
505
- BYTE* const ostart = (BYTE*)dst;
506
- BYTE* const oend = ostart + dstSize;
507
- BYTE* op = ostart;
508
-
509
- U32 count[HUF_SYMBOLVALUE_MAX+1];
510
- HUF_CElt CTable[HUF_SYMBOLVALUE_MAX+1];
511
-
512
- /* checks & inits */
513
- if (!srcSize) return 0; /* Uncompressed (note : 1 means rle, so first byte must be correct) */
514
- if (!dstSize) return 0; /* cannot fit within dst budget */
515
- if (srcSize > HUF_BLOCKSIZE_MAX) return ERROR(srcSize_wrong); /* current block size limit */
516
- if (huffLog > HUF_TABLELOG_MAX) return ERROR(tableLog_tooLarge);
517
- if (!maxSymbolValue) maxSymbolValue = HUF_SYMBOLVALUE_MAX;
518
- if (!huffLog) huffLog = HUF_TABLELOG_DEFAULT;
519
-
520
- /* Scan input and build symbol stats */
521
- { size_t const largest = FSE_count (count, &maxSymbolValue, (const BYTE*)src, srcSize);
522
- if (HUF_isError(largest)) return largest;
523
- if (largest == srcSize) { *ostart = ((const BYTE*)src)[0]; return 1; } /* rle */
524
- if (largest <= (srcSize >> 7)+1) return 0; /* Fast heuristic : not compressible enough */
525
- }
526
-
527
- /* Build Huffman Tree */
528
- huffLog = HUF_optimalTableLog(huffLog, srcSize, maxSymbolValue);
529
- { size_t const maxBits = HUF_buildCTable (CTable, count, maxSymbolValue, huffLog);
530
- if (HUF_isError(maxBits)) return maxBits;
531
- huffLog = (U32)maxBits;
532
- }
533
-
534
- /* Write table description header */
535
- { size_t const hSize = HUF_writeCTable (op, dstSize, CTable, maxSymbolValue, huffLog);
536
- if (HUF_isError(hSize)) return hSize;
537
- if (hSize + 12 >= srcSize) return 0; /* not useful to try compression */
538
- //static U64 totalHSize = 0; static U32 nbHSize = 0; totalHSize += hSize; nbHSize++; if ((nbHSize & 63) == 1) printf("average : %6.3f \n", (double)totalHSize / nbHSize);
539
- op += hSize;
540
- }
541
-
542
- /* Compress */
543
- { size_t const cSize = (singleStream) ?
544
- HUF_compress1X_usingCTable(op, oend - op, src, srcSize, CTable) : /* single segment */
545
- HUF_compress4X_usingCTable(op, oend - op, src, srcSize, CTable);
546
- if (HUF_isError(cSize)) return cSize;
547
- if (cSize==0) return 0; /* uncompressible */
548
- op += cSize;
549
- }
550
-
551
- /* check compressibility */
552
- if ((size_t)(op-ostart) >= srcSize-1)
553
- return 0;
554
-
555
- return op-ostart;
556
- }
557
-
558
-
559
- size_t HUF_compress1X (void* dst, size_t dstSize,
560
- const void* src, size_t srcSize,
561
- unsigned maxSymbolValue, unsigned huffLog)
562
- {
563
- return HUF_compress_internal(dst, dstSize, src, srcSize, maxSymbolValue, huffLog, 1);
564
- }
565
-
566
- size_t HUF_compress2 (void* dst, size_t dstSize,
567
- const void* src, size_t srcSize,
568
- unsigned maxSymbolValue, unsigned huffLog)
569
- {
570
- return HUF_compress_internal(dst, dstSize, src, srcSize, maxSymbolValue, huffLog, 0);
571
- }
572
-
573
-
574
- size_t HUF_compress (void* dst, size_t maxDstSize, const void* src, size_t srcSize)
575
- {
576
- return HUF_compress2(dst, maxDstSize, src, (U32)srcSize, 255, HUF_TABLELOG_DEFAULT);
577
- }