extlzham 0.0.1.PROTOTYPE

Sign up to get free protection for your applications and to get access to all the features.
Files changed (59) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE.md +27 -0
  3. data/README.md +21 -0
  4. data/Rakefile +143 -0
  5. data/contrib/lzham/LICENSE +22 -0
  6. data/contrib/lzham/README.md +209 -0
  7. data/contrib/lzham/include/lzham.h +781 -0
  8. data/contrib/lzham/lzhamcomp/lzham_comp.h +38 -0
  9. data/contrib/lzham/lzhamcomp/lzham_lzbase.cpp +244 -0
  10. data/contrib/lzham/lzhamcomp/lzham_lzbase.h +45 -0
  11. data/contrib/lzham/lzhamcomp/lzham_lzcomp.cpp +608 -0
  12. data/contrib/lzham/lzhamcomp/lzham_lzcomp_internal.cpp +1966 -0
  13. data/contrib/lzham/lzhamcomp/lzham_lzcomp_internal.h +472 -0
  14. data/contrib/lzham/lzhamcomp/lzham_lzcomp_state.cpp +1413 -0
  15. data/contrib/lzham/lzhamcomp/lzham_match_accel.cpp +562 -0
  16. data/contrib/lzham/lzhamcomp/lzham_match_accel.h +146 -0
  17. data/contrib/lzham/lzhamcomp/lzham_null_threading.h +97 -0
  18. data/contrib/lzham/lzhamcomp/lzham_pthreads_threading.cpp +229 -0
  19. data/contrib/lzham/lzhamcomp/lzham_pthreads_threading.h +520 -0
  20. data/contrib/lzham/lzhamcomp/lzham_threading.h +12 -0
  21. data/contrib/lzham/lzhamcomp/lzham_win32_threading.cpp +220 -0
  22. data/contrib/lzham/lzhamcomp/lzham_win32_threading.h +368 -0
  23. data/contrib/lzham/lzhamdecomp/lzham_assert.cpp +66 -0
  24. data/contrib/lzham/lzhamdecomp/lzham_assert.h +40 -0
  25. data/contrib/lzham/lzhamdecomp/lzham_checksum.cpp +73 -0
  26. data/contrib/lzham/lzhamdecomp/lzham_checksum.h +13 -0
  27. data/contrib/lzham/lzhamdecomp/lzham_config.h +23 -0
  28. data/contrib/lzham/lzhamdecomp/lzham_core.h +264 -0
  29. data/contrib/lzham/lzhamdecomp/lzham_decomp.h +37 -0
  30. data/contrib/lzham/lzhamdecomp/lzham_helpers.h +54 -0
  31. data/contrib/lzham/lzhamdecomp/lzham_huffman_codes.cpp +262 -0
  32. data/contrib/lzham/lzhamdecomp/lzham_huffman_codes.h +14 -0
  33. data/contrib/lzham/lzhamdecomp/lzham_lzdecomp.cpp +1527 -0
  34. data/contrib/lzham/lzhamdecomp/lzham_lzdecompbase.cpp +131 -0
  35. data/contrib/lzham/lzhamdecomp/lzham_lzdecompbase.h +89 -0
  36. data/contrib/lzham/lzhamdecomp/lzham_math.h +142 -0
  37. data/contrib/lzham/lzhamdecomp/lzham_mem.cpp +284 -0
  38. data/contrib/lzham/lzhamdecomp/lzham_mem.h +112 -0
  39. data/contrib/lzham/lzhamdecomp/lzham_platform.cpp +157 -0
  40. data/contrib/lzham/lzhamdecomp/lzham_platform.h +284 -0
  41. data/contrib/lzham/lzhamdecomp/lzham_prefix_coding.cpp +351 -0
  42. data/contrib/lzham/lzhamdecomp/lzham_prefix_coding.h +146 -0
  43. data/contrib/lzham/lzhamdecomp/lzham_symbol_codec.cpp +1484 -0
  44. data/contrib/lzham/lzhamdecomp/lzham_symbol_codec.h +556 -0
  45. data/contrib/lzham/lzhamdecomp/lzham_timer.cpp +147 -0
  46. data/contrib/lzham/lzhamdecomp/lzham_timer.h +99 -0
  47. data/contrib/lzham/lzhamdecomp/lzham_traits.h +141 -0
  48. data/contrib/lzham/lzhamdecomp/lzham_types.h +97 -0
  49. data/contrib/lzham/lzhamdecomp/lzham_utils.h +58 -0
  50. data/contrib/lzham/lzhamdecomp/lzham_vector.cpp +75 -0
  51. data/contrib/lzham/lzhamdecomp/lzham_vector.h +588 -0
  52. data/contrib/lzham/lzhamlib/lzham_lib.cpp +179 -0
  53. data/examples/basic.rb +48 -0
  54. data/ext/extconf.rb +26 -0
  55. data/ext/extlzham.c +741 -0
  56. data/gemstub.rb +22 -0
  57. data/lib/extlzham/version.rb +5 -0
  58. data/lib/extlzham.rb +153 -0
  59. metadata +135 -0
@@ -0,0 +1,351 @@
1
+ // File: lzham_prefix_coding.cpp
2
+ // See Copyright Notice and license at the end of include/lzham.h
3
+ #include "lzham_core.h"
4
+ #include "lzham_prefix_coding.h"
5
+
6
+ #ifdef LZHAM_BUILD_DEBUG
7
+ //#define TEST_DECODER_TABLES
8
+ #endif
9
+
10
+ namespace lzham
11
+ {
12
+ namespace prefix_coding
13
+ {
14
+ bool limit_max_code_size(uint num_syms, uint8* pCodesizes, uint max_code_size)
15
+ {
16
+ const uint cMaxEverCodeSize = 34;
17
+
18
+ if ((!num_syms) || (num_syms > cMaxSupportedSyms) || (max_code_size < 1) || (max_code_size > cMaxEverCodeSize))
19
+ return false;
20
+
21
+ uint num_codes[cMaxEverCodeSize + 1];
22
+ utils::zero_object(num_codes);
23
+
24
+ bool should_limit = false;
25
+
26
+ for (uint i = 0; i < num_syms; i++)
27
+ {
28
+ uint c = pCodesizes[i];
29
+
30
+ LZHAM_ASSERT(c <= cMaxEverCodeSize);
31
+
32
+ num_codes[c]++;
33
+ if (c > max_code_size)
34
+ should_limit = true;
35
+ }
36
+
37
+ if (!should_limit)
38
+ return true;
39
+
40
+ uint ofs = 0;
41
+ uint next_sorted_ofs[cMaxEverCodeSize + 1];
42
+ for (uint i = 1; i <= cMaxEverCodeSize; i++)
43
+ {
44
+ next_sorted_ofs[i] = ofs;
45
+ ofs += num_codes[i];
46
+ }
47
+
48
+ if ((ofs < 2) || (ofs > cMaxSupportedSyms))
49
+ return true;
50
+
51
+ if (ofs > (1U << max_code_size))
52
+ return false;
53
+
54
+ for (uint i = max_code_size + 1; i <= cMaxEverCodeSize; i++)
55
+ num_codes[max_code_size] += num_codes[i];
56
+
57
+ // Technique of adjusting tree to enforce maximum code size from LHArc.
58
+ // (If you remember what LHArc was, you've been doing this for a LONG time.)
59
+
60
+ uint total = 0;
61
+ for (uint i = max_code_size; i; --i)
62
+ total += (num_codes[i] << (max_code_size - i));
63
+
64
+ if (total == (1U << max_code_size))
65
+ return true;
66
+
67
+ do
68
+ {
69
+ num_codes[max_code_size]--;
70
+
71
+ uint i;
72
+ for (i = max_code_size - 1; i; --i)
73
+ {
74
+ if (!num_codes[i])
75
+ continue;
76
+ num_codes[i]--;
77
+ num_codes[i + 1] += 2;
78
+ break;
79
+ }
80
+ if (!i)
81
+ return false;
82
+
83
+ total--;
84
+ } while (total != (1U << max_code_size));
85
+
86
+ uint8 new_codesizes[cMaxSupportedSyms];
87
+ uint8* p = new_codesizes;
88
+ for (uint i = 1; i <= max_code_size; i++)
89
+ {
90
+ uint n = num_codes[i];
91
+ if (n)
92
+ {
93
+ memset(p, i, n);
94
+ p += n;
95
+ }
96
+ }
97
+
98
+ for (uint i = 0; i < num_syms; i++)
99
+ {
100
+ const uint c = pCodesizes[i];
101
+ if (c)
102
+ {
103
+ uint next_ofs = next_sorted_ofs[c];
104
+ next_sorted_ofs[c] = next_ofs + 1;
105
+
106
+ pCodesizes[i] = static_cast<uint8>(new_codesizes[next_ofs]);
107
+ }
108
+ }
109
+
110
+ return true;
111
+ }
112
+
113
+ bool generate_codes(uint num_syms, const uint8* pCodesizes, uint16* pCodes)
114
+ {
115
+ uint num_codes[cMaxExpectedCodeSize + 1];
116
+ utils::zero_object(num_codes);
117
+
118
+ for (uint i = 0; i < num_syms; i++)
119
+ {
120
+ uint c = pCodesizes[i];
121
+ LZHAM_ASSERT(c <= cMaxExpectedCodeSize);
122
+ num_codes[c]++;
123
+ }
124
+
125
+ uint code = 0;
126
+
127
+ uint next_code[cMaxExpectedCodeSize + 1];
128
+ next_code[0] = 0;
129
+
130
+ for (uint i = 1; i <= cMaxExpectedCodeSize; i++)
131
+ {
132
+ next_code[i] = code;
133
+
134
+ code = (code + num_codes[i]) << 1;
135
+ }
136
+
137
+ if (code != (1 << (cMaxExpectedCodeSize + 1)))
138
+ {
139
+ uint t = 0;
140
+ for (uint i = 1; i <= cMaxExpectedCodeSize; i++)
141
+ {
142
+ t += num_codes[i];
143
+ if (t > 1)
144
+ return false;
145
+ }
146
+ }
147
+
148
+ for (uint i = 0; i < num_syms; i++)
149
+ {
150
+ uint c = pCodesizes[i];
151
+
152
+ LZHAM_ASSERT(!c || (next_code[c] <= UINT16_MAX));
153
+
154
+ pCodes[i] = static_cast<uint16>(next_code[c]++);
155
+
156
+ LZHAM_ASSERT(!c || (math::total_bits(pCodes[i]) <= pCodesizes[i]));
157
+ }
158
+
159
+ return true;
160
+ }
161
+
162
+ bool generate_decoder_tables(uint num_syms, const uint8* pCodesizes, decoder_tables* pTables, uint table_bits)
163
+ {
164
+ uint min_codes[cMaxExpectedCodeSize];
165
+
166
+ if ((!num_syms) || (table_bits > cMaxTableBits))
167
+ return false;
168
+
169
+ pTables->m_num_syms = num_syms;
170
+
171
+ uint num_codes[cMaxExpectedCodeSize + 1];
172
+ utils::zero_object(num_codes);
173
+
174
+ for (uint i = 0; i < num_syms; i++)
175
+ {
176
+ uint c = pCodesizes[i];
177
+ num_codes[c]++;
178
+ }
179
+
180
+ uint sorted_positions[cMaxExpectedCodeSize + 1];
181
+
182
+ uint next_code = 0;
183
+
184
+ uint total_used_syms = 0;
185
+ uint max_code_size = 0;
186
+ uint min_code_size = UINT_MAX;
187
+ for (uint i = 1; i <= cMaxExpectedCodeSize; i++)
188
+ {
189
+ const uint n = num_codes[i];
190
+
191
+ if (!n)
192
+ pTables->m_max_codes[i - 1] = 0;//UINT_MAX;
193
+ else
194
+ {
195
+ min_code_size = math::minimum(min_code_size, i);
196
+ max_code_size = math::maximum(max_code_size, i);
197
+
198
+ min_codes[i - 1] = next_code;
199
+
200
+ pTables->m_max_codes[i - 1] = next_code + n - 1;
201
+ pTables->m_max_codes[i - 1] = 1 + ((pTables->m_max_codes[i - 1] << (16 - i)) | ((1 << (16 - i)) - 1));
202
+
203
+ pTables->m_val_ptrs[i - 1] = total_used_syms;
204
+
205
+ sorted_positions[i] = total_used_syms;
206
+
207
+ next_code += n;
208
+ total_used_syms += n;
209
+ }
210
+
211
+ next_code <<= 1;
212
+ }
213
+
214
+ pTables->m_total_used_syms = total_used_syms;
215
+
216
+ if (total_used_syms > pTables->m_cur_sorted_symbol_order_size)
217
+ {
218
+ pTables->m_cur_sorted_symbol_order_size = total_used_syms;
219
+
220
+ if (!math::is_power_of_2(total_used_syms))
221
+ pTables->m_cur_sorted_symbol_order_size = math::minimum<uint>(num_syms, math::next_pow2(total_used_syms));
222
+
223
+ if (pTables->m_sorted_symbol_order)
224
+ {
225
+ lzham_delete_array(pTables->m_sorted_symbol_order);
226
+ pTables->m_sorted_symbol_order = NULL;
227
+ }
228
+
229
+ pTables->m_sorted_symbol_order = lzham_new_array<uint16>(pTables->m_cur_sorted_symbol_order_size);
230
+ if (!pTables->m_sorted_symbol_order)
231
+ return false;
232
+ }
233
+
234
+ pTables->m_min_code_size = static_cast<uint8>(min_code_size);
235
+ pTables->m_max_code_size = static_cast<uint8>(max_code_size);
236
+
237
+ for (uint i = 0; i < num_syms; i++)
238
+ {
239
+ uint c = pCodesizes[i];
240
+ if (c)
241
+ {
242
+ LZHAM_ASSERT(num_codes[c]);
243
+
244
+ uint sorted_pos = sorted_positions[c]++;
245
+
246
+ LZHAM_ASSERT(sorted_pos < total_used_syms);
247
+
248
+ pTables->m_sorted_symbol_order[sorted_pos] = static_cast<uint16>(i);
249
+ }
250
+ }
251
+
252
+ if (table_bits <= pTables->m_min_code_size)
253
+ table_bits = 0;
254
+ pTables->m_table_bits = table_bits;
255
+
256
+ if (table_bits)
257
+ {
258
+ uint table_size = 1 << table_bits;
259
+ if (table_size > pTables->m_cur_lookup_size)
260
+ {
261
+ pTables->m_cur_lookup_size = table_size;
262
+
263
+ if (pTables->m_lookup)
264
+ {
265
+ lzham_delete_array(pTables->m_lookup);
266
+ pTables->m_lookup = NULL;
267
+ }
268
+
269
+ pTables->m_lookup = lzham_new_array<uint32>(table_size);
270
+ if (!pTables->m_lookup)
271
+ return false;
272
+ }
273
+
274
+ memset(pTables->m_lookup, 0xFF, static_cast<uint>(sizeof(pTables->m_lookup[0])) * (1UL << table_bits));
275
+
276
+ for (uint codesize = 1; codesize <= table_bits; codesize++)
277
+ {
278
+ if (!num_codes[codesize])
279
+ continue;
280
+
281
+ const uint fillsize = table_bits - codesize;
282
+ const uint fillnum = 1 << fillsize;
283
+
284
+ const uint min_code = min_codes[codesize - 1];
285
+ const uint max_code = pTables->get_unshifted_max_code(codesize);
286
+ const uint val_ptr = pTables->m_val_ptrs[codesize - 1];
287
+
288
+ for (uint code = min_code; code <= max_code; code++)
289
+ {
290
+ const uint sym_index = pTables->m_sorted_symbol_order[ val_ptr + code - min_code ];
291
+ LZHAM_ASSERT( pCodesizes[sym_index] == codesize );
292
+
293
+ for (uint j = 0; j < fillnum; j++)
294
+ {
295
+ const uint t = j + (code << fillsize);
296
+
297
+ LZHAM_ASSERT(t < (1U << table_bits));
298
+
299
+ LZHAM_ASSERT(pTables->m_lookup[t] == UINT32_MAX);
300
+
301
+ pTables->m_lookup[t] = sym_index | (codesize << 16U);
302
+ }
303
+ }
304
+ }
305
+ }
306
+
307
+ for (uint i = 0; i < cMaxExpectedCodeSize; i++)
308
+ pTables->m_val_ptrs[i] -= min_codes[i];
309
+
310
+ pTables->m_table_max_code = 0;
311
+ pTables->m_decode_start_code_size = pTables->m_min_code_size;
312
+
313
+ if (table_bits)
314
+ {
315
+ uint i;
316
+ for (i = table_bits; i >= 1; i--)
317
+ {
318
+ if (num_codes[i])
319
+ {
320
+ pTables->m_table_max_code = pTables->m_max_codes[i - 1];
321
+ break;
322
+ }
323
+ }
324
+ if (i >= 1)
325
+ {
326
+ pTables->m_decode_start_code_size = table_bits + 1;
327
+ for (i = table_bits + 1; i <= max_code_size; i++)
328
+ {
329
+ if (num_codes[i])
330
+ {
331
+ pTables->m_decode_start_code_size = i;
332
+ break;
333
+ }
334
+ }
335
+ }
336
+ }
337
+
338
+ // sentinels
339
+ pTables->m_max_codes[cMaxExpectedCodeSize] = UINT_MAX;
340
+ pTables->m_val_ptrs[cMaxExpectedCodeSize] = 0xFFFFF;
341
+
342
+ pTables->m_table_shift = 32 - pTables->m_table_bits;
343
+
344
+ return true;
345
+ }
346
+
347
+ } // namespace prefix_codig
348
+
349
+ } // namespace lzham
350
+
351
+
@@ -0,0 +1,146 @@
1
+ // File: lzham_prefix_coding.h
2
+ // See Copyright Notice and license at the end of include/lzham.h
3
+ #pragma once
4
+
5
+ namespace lzham
6
+ {
7
+ namespace prefix_coding
8
+ {
9
+ const uint cMaxExpectedCodeSize = 16;
10
+ const uint cMaxSupportedSyms = 1024;
11
+
12
+ // This value can be tuned for a specific CPU.
13
+ const uint cMaxTableBits = 11;
14
+
15
+ bool limit_max_code_size(uint num_syms, uint8* pCodesizes, uint max_code_size);
16
+
17
+ bool generate_codes(uint num_syms, const uint8* pCodesizes, uint16* pCodes);
18
+
19
+ class decoder_tables
20
+ {
21
+ public:
22
+ inline decoder_tables() :
23
+ m_table_shift(0), m_table_max_code(0), m_decode_start_code_size(0), m_cur_lookup_size(0), m_lookup(NULL), m_cur_sorted_symbol_order_size(0), m_sorted_symbol_order(NULL)
24
+ {
25
+ }
26
+
27
+ inline decoder_tables(const decoder_tables& other) :
28
+ m_table_shift(0), m_table_max_code(0), m_decode_start_code_size(0), m_cur_lookup_size(0), m_lookup(NULL), m_cur_sorted_symbol_order_size(0), m_sorted_symbol_order(NULL)
29
+ {
30
+ *this = other;
31
+ }
32
+
33
+ inline decoder_tables& operator= (const decoder_tables& rhs)
34
+ {
35
+ assign(rhs);
36
+ return *this;
37
+ }
38
+
39
+ inline bool assign(const decoder_tables& rhs)
40
+ {
41
+ if (this == &rhs)
42
+ return true;
43
+
44
+ uint32* pCur_lookup = m_lookup;
45
+ uint16* pCur_sorted_symbol_order = m_sorted_symbol_order;
46
+
47
+ memcpy(this, &rhs, sizeof(*this));
48
+
49
+ if ((pCur_lookup) && (pCur_sorted_symbol_order) && (rhs.m_cur_lookup_size == m_cur_lookup_size) && (rhs.m_cur_sorted_symbol_order_size == m_cur_sorted_symbol_order_size))
50
+ {
51
+ m_lookup = pCur_lookup;
52
+ m_sorted_symbol_order = pCur_sorted_symbol_order;
53
+
54
+ memcpy(m_lookup, rhs.m_lookup, sizeof(m_lookup[0]) * m_cur_lookup_size);
55
+ memcpy(m_sorted_symbol_order, rhs.m_sorted_symbol_order, sizeof(m_sorted_symbol_order[0]) * m_cur_sorted_symbol_order_size);
56
+ }
57
+ else
58
+ {
59
+ lzham_delete_array(pCur_lookup);
60
+ m_lookup = NULL;
61
+
62
+ if (rhs.m_lookup)
63
+ {
64
+ m_lookup = lzham_new_array<uint32>(m_cur_lookup_size);
65
+ if (!m_lookup)
66
+ return false;
67
+ memcpy(m_lookup, rhs.m_lookup, sizeof(m_lookup[0]) * m_cur_lookup_size);
68
+ }
69
+
70
+ lzham_delete_array(pCur_sorted_symbol_order);
71
+ m_sorted_symbol_order = NULL;
72
+
73
+ if (rhs.m_sorted_symbol_order)
74
+ {
75
+ m_sorted_symbol_order = lzham_new_array<uint16>(m_cur_sorted_symbol_order_size);
76
+ if (!m_sorted_symbol_order)
77
+ return false;
78
+ memcpy(m_sorted_symbol_order, rhs.m_sorted_symbol_order, sizeof(m_sorted_symbol_order[0]) * m_cur_sorted_symbol_order_size);
79
+ }
80
+ }
81
+
82
+ return true;
83
+ }
84
+
85
+ inline void clear()
86
+ {
87
+ if (m_lookup)
88
+ {
89
+ lzham_delete_array(m_lookup);
90
+ m_lookup = 0;
91
+ m_cur_lookup_size = 0;
92
+ }
93
+
94
+ if (m_sorted_symbol_order)
95
+ {
96
+ lzham_delete_array(m_sorted_symbol_order);
97
+ m_sorted_symbol_order = NULL;
98
+ m_cur_sorted_symbol_order_size = 0;
99
+ }
100
+ }
101
+
102
+ inline ~decoder_tables()
103
+ {
104
+ if (m_lookup)
105
+ lzham_delete_array(m_lookup);
106
+
107
+ if (m_sorted_symbol_order)
108
+ lzham_delete_array(m_sorted_symbol_order);
109
+ }
110
+
111
+ // DO NOT use any complex classes here - it is bitwise copied.
112
+
113
+ uint m_num_syms;
114
+ uint m_total_used_syms;
115
+ uint m_table_bits;
116
+ uint m_table_shift;
117
+ uint m_table_max_code;
118
+ uint m_decode_start_code_size;
119
+
120
+ uint8 m_min_code_size;
121
+ uint8 m_max_code_size;
122
+
123
+ uint m_max_codes[cMaxExpectedCodeSize + 1];
124
+ int m_val_ptrs[cMaxExpectedCodeSize + 1];
125
+
126
+ uint m_cur_lookup_size;
127
+ uint32* m_lookup;
128
+
129
+ uint m_cur_sorted_symbol_order_size;
130
+ uint16* m_sorted_symbol_order;
131
+
132
+ inline uint get_unshifted_max_code(uint len) const
133
+ {
134
+ LZHAM_ASSERT( (len >= 1) && (len <= cMaxExpectedCodeSize) );
135
+ uint k = m_max_codes[len - 1];
136
+ if (!k)
137
+ return UINT_MAX;
138
+ return (k - 1) >> (16 - len);
139
+ }
140
+ };
141
+
142
+ bool generate_decoder_tables(uint num_syms, const uint8* pCodesizes, decoder_tables* pTables, uint table_bits);
143
+
144
+ } // namespace prefix_coding
145
+
146
+ } // namespace lzham