extlzham 0.0.1.PROTOTYPE3-x86-mingw32

Sign up to get free protection for your applications and to get access to all the features.
Files changed (69) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE.md +27 -0
  3. data/README.md +74 -0
  4. data/Rakefile +152 -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/constants.c +64 -0
  55. data/ext/decoder.c +313 -0
  56. data/ext/depend +5 -0
  57. data/ext/encoder.c +372 -0
  58. data/ext/error.c +80 -0
  59. data/ext/extconf.rb +29 -0
  60. data/ext/extlzham.c +34 -0
  61. data/ext/extlzham.h +62 -0
  62. data/gemstub.rb +22 -0
  63. data/lib/2.0/extlzham.so +0 -0
  64. data/lib/2.1/extlzham.so +0 -0
  65. data/lib/2.2/extlzham.so +0 -0
  66. data/lib/extlzham.rb +158 -0
  67. data/lib/extlzham/version.rb +5 -0
  68. data/test/test_extlzham.rb +35 -0
  69. metadata +156 -0
@@ -0,0 +1,179 @@
1
+ // File: lzham_lib.cpp - Static library entrypoints.
2
+ // See Copyright Notice and license at the end of include/lzham.h
3
+ #include "lzham_core.h"
4
+ #include "lzham_decomp.h"
5
+ #include "lzham_comp.h"
6
+
7
+ extern "C" lzham_uint32 LZHAM_CDECL lzham_get_version(void)
8
+ {
9
+ return LZHAM_DLL_VERSION;
10
+ }
11
+
12
+ extern "C" void LZHAM_CDECL lzham_set_memory_callbacks(lzham_realloc_func pRealloc, lzham_msize_func pMSize, void* pUser_data)
13
+ {
14
+ lzham::lzham_lib_set_memory_callbacks(pRealloc, pMSize, pUser_data);
15
+ }
16
+
17
+ extern "C" lzham_decompress_state_ptr LZHAM_CDECL lzham_decompress_init(const lzham_decompress_params *pParams)
18
+ {
19
+ return lzham::lzham_lib_decompress_init(pParams);
20
+ }
21
+
22
+ extern "C" lzham_decompress_state_ptr LZHAM_CDECL lzham_decompress_reinit(lzham_decompress_state_ptr p, const lzham_decompress_params *pParams)
23
+ {
24
+ return lzham::lzham_lib_decompress_reinit(p, pParams);
25
+ }
26
+
27
+ extern "C" lzham_uint32 LZHAM_CDECL lzham_decompress_deinit(lzham_decompress_state_ptr p)
28
+ {
29
+ return lzham::lzham_lib_decompress_deinit(p);
30
+ }
31
+
32
+ extern "C" lzham_decompress_status_t LZHAM_CDECL lzham_decompress(
33
+ lzham_decompress_state_ptr p,
34
+ const lzham_uint8 *pIn_buf, size_t *pIn_buf_size,
35
+ lzham_uint8 *pOut_buf, size_t *pOut_buf_size,
36
+ lzham_bool no_more_input_bytes_flag)
37
+ {
38
+ return lzham::lzham_lib_decompress(p, pIn_buf, pIn_buf_size, pOut_buf, pOut_buf_size, no_more_input_bytes_flag);
39
+ }
40
+
41
+ extern "C" lzham_decompress_status_t LZHAM_CDECL lzham_decompress_memory(const lzham_decompress_params *pParams, lzham_uint8* pDst_buf, size_t *pDst_len, const lzham_uint8* pSrc_buf, size_t src_len, lzham_uint32 *pAdler32)
42
+ {
43
+ return lzham::lzham_lib_decompress_memory(pParams, pDst_buf, pDst_len, pSrc_buf, src_len, pAdler32);
44
+ }
45
+
46
+ extern "C" lzham_compress_state_ptr LZHAM_CDECL lzham_compress_init(const lzham_compress_params *pParams)
47
+ {
48
+ return lzham::lzham_lib_compress_init(pParams);
49
+ }
50
+
51
+ extern "C" lzham_compress_state_ptr LZHAM_CDECL lzham_compress_reinit(lzham_compress_state_ptr p)
52
+ {
53
+ return lzham::lzham_lib_compress_reinit(p);
54
+ }
55
+
56
+ extern "C" lzham_uint32 LZHAM_CDECL lzham_compress_deinit(lzham_compress_state_ptr p)
57
+ {
58
+ return lzham::lzham_lib_compress_deinit(p);
59
+ }
60
+
61
+ extern "C" lzham_compress_status_t LZHAM_CDECL lzham_compress(
62
+ lzham_compress_state_ptr p,
63
+ const lzham_uint8 *pIn_buf, size_t *pIn_buf_size,
64
+ lzham_uint8 *pOut_buf, size_t *pOut_buf_size,
65
+ lzham_bool no_more_input_bytes_flag)
66
+ {
67
+ return lzham::lzham_lib_compress(p, pIn_buf, pIn_buf_size, pOut_buf, pOut_buf_size, no_more_input_bytes_flag);
68
+ }
69
+
70
+ extern "C" lzham_compress_status_t LZHAM_CDECL lzham_compress2(
71
+ lzham_compress_state_ptr p,
72
+ const lzham_uint8 *pIn_buf, size_t *pIn_buf_size,
73
+ lzham_uint8 *pOut_buf, size_t *pOut_buf_size,
74
+ lzham_flush_t flush_type)
75
+ {
76
+ return lzham::lzham_lib_compress2(p, pIn_buf, pIn_buf_size, pOut_buf, pOut_buf_size, flush_type);
77
+ }
78
+
79
+ extern "C" lzham_compress_status_t LZHAM_CDECL lzham_compress_memory(const lzham_compress_params *pParams, lzham_uint8* pDst_buf, size_t *pDst_len, const lzham_uint8* pSrc_buf, size_t src_len, lzham_uint32 *pAdler32)
80
+ {
81
+ return lzham::lzham_lib_compress_memory(pParams, pDst_buf, pDst_len, pSrc_buf, src_len, pAdler32);
82
+ }
83
+
84
+ // ----------------- zlib-style API's
85
+
86
+ extern "C" const char * LZHAM_CDECL lzham_z_version(void)
87
+ {
88
+ return LZHAM_Z_VERSION;
89
+ }
90
+
91
+ extern "C" lzham_z_ulong LZHAM_CDECL lzham_z_adler32(lzham_z_ulong adler, const unsigned char *ptr, size_t buf_len)
92
+ {
93
+ return lzham::lzham_lib_z_adler32(adler, ptr, buf_len);
94
+ }
95
+
96
+ extern "C" lzham_z_ulong LZHAM_CDECL lzham_z_crc32(lzham_z_ulong crc, const lzham_uint8 *ptr, size_t buf_len)
97
+ {
98
+ return lzham::lzham_lib_z_crc32(crc, ptr, buf_len);
99
+ }
100
+
101
+ extern "C" int LZHAM_CDECL lzham_z_deflateInit(lzham_z_streamp pStream, int level)
102
+ {
103
+ return lzham::lzham_lib_z_deflateInit(pStream, level);
104
+ }
105
+
106
+ extern "C" int LZHAM_CDECL lzham_z_deflateInit2(lzham_z_streamp pStream, int level, int method, int window_bits, int mem_level, int strategy)
107
+ {
108
+ return lzham::lzham_lib_z_deflateInit2(pStream, level, method, window_bits, mem_level, strategy);
109
+ }
110
+
111
+ extern "C" int LZHAM_CDECL lzham_z_deflateReset(lzham_z_streamp pStream)
112
+ {
113
+ return lzham::lzham_lib_z_deflateReset(pStream);
114
+ }
115
+
116
+ extern "C" int LZHAM_CDECL lzham_z_deflate(lzham_z_streamp pStream, int flush)
117
+ {
118
+ return lzham::lzham_lib_z_deflate(pStream, flush);
119
+ }
120
+
121
+ extern "C" int LZHAM_CDECL lzham_z_deflateEnd(lzham_z_streamp pStream)
122
+ {
123
+ return lzham::lzham_lib_z_deflateEnd(pStream);
124
+ }
125
+
126
+ extern "C" lzham_z_ulong LZHAM_CDECL lzham_z_deflateBound(lzham_z_streamp pStream, lzham_z_ulong source_len)
127
+ {
128
+ return lzham::lzham_lib_z_deflateBound(pStream, source_len);
129
+ }
130
+
131
+ extern "C" int LZHAM_CDECL lzham_z_compress(unsigned char *pDest, lzham_z_ulong *pDest_len, const unsigned char *pSource, lzham_z_ulong source_len)
132
+ {
133
+ return lzham::lzham_lib_z_compress(pDest, pDest_len, pSource, source_len);
134
+ }
135
+
136
+ extern "C" int LZHAM_CDECL lzham_z_compress2(unsigned char *pDest, lzham_z_ulong *pDest_len, const unsigned char *pSource, lzham_z_ulong source_len, int level)
137
+ {
138
+ return lzham::lzham_lib_z_compress2(pDest, pDest_len, pSource, source_len, level);
139
+ }
140
+
141
+ extern "C" lzham_z_ulong LZHAM_CDECL lzham_z_compressBound(lzham_z_ulong source_len)
142
+ {
143
+ return lzham::lzham_lib_z_compressBound(source_len);
144
+ }
145
+
146
+ extern "C" int LZHAM_CDECL lzham_z_inflateInit(lzham_z_streamp pStream)
147
+ {
148
+ return lzham::lzham_lib_z_inflateInit(pStream);
149
+ }
150
+
151
+ extern "C" int LZHAM_CDECL lzham_z_inflateInit2(lzham_z_streamp pStream, int window_bits)
152
+ {
153
+ return lzham::lzham_lib_z_inflateInit2(pStream, window_bits);
154
+ }
155
+
156
+ extern "C" int LZHAM_CDECL lzham_z_inflateReset(lzham_z_streamp pStream)
157
+ {
158
+ return lzham::lzham_lib_z_inflateReset(pStream);
159
+ }
160
+
161
+ extern "C" int LZHAM_CDECL lzham_z_inflate(lzham_z_streamp pStream, int flush)
162
+ {
163
+ return lzham::lzham_lib_z_inflate(pStream, flush);
164
+ }
165
+
166
+ extern "C" int LZHAM_CDECL lzham_z_inflateEnd(lzham_z_streamp pStream)
167
+ {
168
+ return lzham::lzham_lib_z_inflateEnd(pStream);
169
+ }
170
+
171
+ extern "C" int LZHAM_CDECL lzham_z_uncompress(unsigned char *pDest, lzham_z_ulong *pDest_len, const unsigned char *pSource, lzham_z_ulong source_len)
172
+ {
173
+ return lzham::lzham_lib_z_uncompress(pDest, pDest_len, pSource, source_len);
174
+ }
175
+
176
+ extern "C" const char * LZHAM_CDECL lzham_z_error(int err)
177
+ {
178
+ return lzham::lzham_lib_z_error(err);
179
+ }
@@ -0,0 +1,48 @@
1
+ #!ruby
2
+
3
+ require "extlzham"
4
+
5
+ # directly string encode / decode
6
+ p data = LZHAM.encode("abcdefghijklmnopqrstuvwxyz" * 10)
7
+ p LZHAM.decode(data)
8
+
9
+
10
+ # directly string encode / decode with dictionaly size
11
+ # (IMPORTANT! MUST BE SAME SIZE both encoding and decoding)
12
+ dictsize = 20 # log(2)'d dictionaly size. This case is 1 MiB (2 ** 20).
13
+ p data = LZHAM.encode("abcdefghijklmnopqrstuvwxyz" * 10, dictsize: dictsize)
14
+ p LZHAM.decode(data, dictsize: dictsize)
15
+
16
+
17
+ # streaming processing
18
+
19
+ # setup streaming data
20
+ ss = StringIO.new("abcdefghijklmnopqrstuvwxyz" * 10) # source stream
21
+ es = StringIO.new # encoded stream
22
+ ds = StringIO.new # destination stream
23
+
24
+ # streaming encode with block
25
+ ss.rewind
26
+ es.rewind
27
+ LZHAM.encode(es) { |lzham| lzham << ss.read }
28
+
29
+ # streaming encode without block
30
+ ss.rewind
31
+ es.rewind
32
+ lzham = LZHAM.encode(es)
33
+ lzham << ss.read
34
+ lzham.finish # or lzham.close
35
+
36
+ # streaming decode with block
37
+ ss.rewind
38
+ es.rewind
39
+ ds.rewind
40
+ LZHAM.decode(ds) { |lzham| lzham << es.read }
41
+
42
+ # streaming decode without block
43
+ ss.rewind
44
+ es.rewind
45
+ ds.rewind
46
+ lzham = LZHAM.decode(ds)
47
+ lzham << es.read
48
+ lzham.finish # or lzham.close
@@ -0,0 +1,64 @@
1
+ #include "extlzham.h"
2
+
3
+ VALUE mConsts;
4
+
5
+ void
6
+ extlzham_init_constants(void)
7
+ {
8
+ RDOCFAKE(mLZHAM = rb_define_module("LZHAM"));
9
+ mConsts = rb_define_module_under(mLZHAM, "Constants");
10
+ rb_include_module(mLZHAM, mConsts);
11
+ rb_define_const(mConsts, "NO_FLUSH", INT2FIX(LZHAM_NO_FLUSH));
12
+ rb_define_const(mConsts, "SYNC_FLUSH", INT2FIX(LZHAM_SYNC_FLUSH));
13
+ rb_define_const(mConsts, "FULL_FLUSH", INT2FIX(LZHAM_FULL_FLUSH));
14
+ rb_define_const(mConsts, "FINISH", INT2FIX(LZHAM_FINISH));
15
+ rb_define_const(mConsts, "TABLE_FLUSH", INT2FIX(LZHAM_TABLE_FLUSH));
16
+ rb_define_const(mConsts, "MIN_DICT_SIZE_LOG2", INT2FIX(LZHAM_MIN_DICT_SIZE_LOG2));
17
+ rb_define_const(mConsts, "MAX_DICT_SIZE_LOG2", INT2FIX(LZHAM_MAX_DICT_SIZE_LOG2_X86));
18
+ rb_define_const(mConsts, "MAX_DICT_SIZE_LOG2_X86", INT2FIX(LZHAM_MAX_DICT_SIZE_LOG2_X86));
19
+ rb_define_const(mConsts, "MAX_DICT_SIZE_LOG2_X64", INT2FIX(LZHAM_MAX_DICT_SIZE_LOG2_X64));
20
+ rb_define_const(mConsts, "MAX_HELPER_THREADS", INT2FIX(LZHAM_MAX_HELPER_THREADS));
21
+ rb_define_const(mConsts, "COMP_LEVEL_FASTEST", INT2FIX(LZHAM_COMP_LEVEL_FASTEST));
22
+ rb_define_const(mConsts, "COMP_LEVEL_FASTER", INT2FIX(LZHAM_COMP_LEVEL_FASTER));
23
+ rb_define_const(mConsts, "COMP_LEVEL_DEFAULT", INT2FIX(LZHAM_COMP_LEVEL_DEFAULT));
24
+ rb_define_const(mConsts, "COMP_LEVEL_BETTER", INT2FIX(LZHAM_COMP_LEVEL_BETTER));
25
+ rb_define_const(mConsts, "COMP_LEVEL_UBER", INT2FIX(LZHAM_COMP_LEVEL_UBER));
26
+ rb_define_const(mConsts, "COMP_FLAG_EXTREME_PARSING", INT2FIX(LZHAM_COMP_FLAG_EXTREME_PARSING));
27
+ rb_define_const(mConsts, "COMP_FLAG_DETERMINISTIC_PARSING", INT2FIX(LZHAM_COMP_FLAG_DETERMINISTIC_PARSING));
28
+ rb_define_const(mConsts, "COMP_FLAG_TRADEOFF_DECOMPRESSION_RATE_FOR_COMP_RATIO", INT2FIX(LZHAM_COMP_FLAG_TRADEOFF_DECOMPRESSION_RATE_FOR_COMP_RATIO));
29
+ rb_define_const(mConsts, "COMP_FLAG_WRITE_ZLIB_STREAM", INT2FIX(LZHAM_COMP_FLAG_WRITE_ZLIB_STREAM));
30
+ rb_define_const(mConsts, "INSANELY_SLOW_TABLE_UPDATE_RATE", INT2FIX(LZHAM_INSANELY_SLOW_TABLE_UPDATE_RATE));
31
+ rb_define_const(mConsts, "SLOWEST_TABLE_UPDATE_RATE", INT2FIX(LZHAM_SLOWEST_TABLE_UPDATE_RATE));
32
+ rb_define_const(mConsts, "DEFAULT_TABLE_UPDATE_RATE", INT2FIX(LZHAM_DEFAULT_TABLE_UPDATE_RATE));
33
+ rb_define_const(mConsts, "FASTEST_TABLE_UPDATE_RATE", INT2FIX(LZHAM_FASTEST_TABLE_UPDATE_RATE));
34
+ rb_define_const(mConsts, "DECOMP_FLAG_OUTPUT_UNBUFFERED", INT2FIX(LZHAM_DECOMP_FLAG_OUTPUT_UNBUFFERED));
35
+ rb_define_const(mConsts, "DECOMP_FLAG_COMPUTE_ADLER32", INT2FIX(LZHAM_DECOMP_FLAG_COMPUTE_ADLER32));
36
+ rb_define_const(mConsts, "DECOMP_FLAG_READ_ZLIB_STREAM", INT2FIX(LZHAM_DECOMP_FLAG_READ_ZLIB_STREAM));
37
+
38
+ rb_define_const(mConsts, "LZHAM_NO_FLUSH", INT2FIX(LZHAM_NO_FLUSH));
39
+ rb_define_const(mConsts, "LZHAM_SYNC_FLUSH", INT2FIX(LZHAM_SYNC_FLUSH));
40
+ rb_define_const(mConsts, "LZHAM_FULL_FLUSH", INT2FIX(LZHAM_FULL_FLUSH));
41
+ rb_define_const(mConsts, "LZHAM_FINISH", INT2FIX(LZHAM_FINISH));
42
+ rb_define_const(mConsts, "LZHAM_TABLE_FLUSH", INT2FIX(LZHAM_TABLE_FLUSH));
43
+ rb_define_const(mConsts, "LZHAM_MIN_DICT_SIZE_LOG2", INT2FIX(LZHAM_MIN_DICT_SIZE_LOG2));
44
+ rb_define_const(mConsts, "LZHAM_MAX_DICT_SIZE_LOG2", INT2FIX(LZHAM_MAX_DICT_SIZE_LOG2_X86));
45
+ rb_define_const(mConsts, "LZHAM_MAX_DICT_SIZE_LOG2_X86", INT2FIX(LZHAM_MAX_DICT_SIZE_LOG2_X86));
46
+ rb_define_const(mConsts, "LZHAM_MAX_DICT_SIZE_LOG2_X64", INT2FIX(LZHAM_MAX_DICT_SIZE_LOG2_X64));
47
+ rb_define_const(mConsts, "LZHAM_MAX_HELPER_THREADS", INT2FIX(LZHAM_MAX_HELPER_THREADS));
48
+ rb_define_const(mConsts, "LZHAM_COMP_LEVEL_FASTEST", INT2FIX(LZHAM_COMP_LEVEL_FASTEST));
49
+ rb_define_const(mConsts, "LZHAM_COMP_LEVEL_FASTER", INT2FIX(LZHAM_COMP_LEVEL_FASTER));
50
+ rb_define_const(mConsts, "LZHAM_COMP_LEVEL_DEFAULT", INT2FIX(LZHAM_COMP_LEVEL_DEFAULT));
51
+ rb_define_const(mConsts, "LZHAM_COMP_LEVEL_BETTER", INT2FIX(LZHAM_COMP_LEVEL_BETTER));
52
+ rb_define_const(mConsts, "LZHAM_COMP_LEVEL_UBER", INT2FIX(LZHAM_COMP_LEVEL_UBER));
53
+ rb_define_const(mConsts, "LZHAM_COMP_FLAG_EXTREME_PARSING", INT2FIX(LZHAM_COMP_FLAG_EXTREME_PARSING));
54
+ rb_define_const(mConsts, "LZHAM_COMP_FLAG_DETERMINISTIC_PARSING", INT2FIX(LZHAM_COMP_FLAG_DETERMINISTIC_PARSING));
55
+ rb_define_const(mConsts, "LZHAM_COMP_FLAG_TRADEOFF_DECOMPRESSION_RATE_FOR_COMP_RATIO", INT2FIX(LZHAM_COMP_FLAG_TRADEOFF_DECOMPRESSION_RATE_FOR_COMP_RATIO));
56
+ rb_define_const(mConsts, "LZHAM_COMP_FLAG_WRITE_ZLIB_STREAM", INT2FIX(LZHAM_COMP_FLAG_WRITE_ZLIB_STREAM));
57
+ rb_define_const(mConsts, "LZHAM_INSANELY_SLOW_TABLE_UPDATE_RATE", INT2FIX(LZHAM_INSANELY_SLOW_TABLE_UPDATE_RATE));
58
+ rb_define_const(mConsts, "LZHAM_SLOWEST_TABLE_UPDATE_RATE", INT2FIX(LZHAM_SLOWEST_TABLE_UPDATE_RATE));
59
+ rb_define_const(mConsts, "LZHAM_DEFAULT_TABLE_UPDATE_RATE", INT2FIX(LZHAM_DEFAULT_TABLE_UPDATE_RATE));
60
+ rb_define_const(mConsts, "LZHAM_FASTEST_TABLE_UPDATE_RATE", INT2FIX(LZHAM_FASTEST_TABLE_UPDATE_RATE));
61
+ rb_define_const(mConsts, "LZHAM_DECOMP_FLAG_OUTPUT_UNBUFFERED", INT2FIX(LZHAM_DECOMP_FLAG_OUTPUT_UNBUFFERED));
62
+ rb_define_const(mConsts, "LZHAM_DECOMP_FLAG_COMPUTE_ADLER32", INT2FIX(LZHAM_DECOMP_FLAG_COMPUTE_ADLER32));
63
+ rb_define_const(mConsts, "LZHAM_DECOMP_FLAG_READ_ZLIB_STREAM", INT2FIX(LZHAM_DECOMP_FLAG_READ_ZLIB_STREAM));
64
+ }
@@ -0,0 +1,313 @@
1
+ #include "extlzham.h"
2
+
3
+ VALUE cDecoder;
4
+
5
+ /*
6
+ * decoder
7
+ */
8
+
9
+ static inline lzham_decompress_params
10
+ aux_conv_decode_params(VALUE opts)
11
+ {
12
+ lzham_decompress_params p;
13
+ memset(&p, 0, sizeof(p));
14
+ p.m_struct_size = sizeof(p);
15
+ if (NIL_P(opts)) {
16
+ p.m_dict_size_log2 = LZHAM_MIN_DICT_SIZE_LOG2;
17
+ p.m_table_update_rate = 0;
18
+ p.m_decompress_flags = 0; // (see lzham_decompress_flags enum)
19
+ p.m_num_seed_bytes = 0;
20
+ p.m_pSeed_bytes = NULL;
21
+ p.m_table_max_update_interval = 0;
22
+ p.m_table_update_interval_slow_rate = 0;
23
+ } else {
24
+ p.m_dict_size_log2 = aux_getoptu32(opts, id_dictsize, LZHAM_MIN_DICT_SIZE_LOG2);
25
+ p.m_table_update_rate = aux_getoptu32(opts, id_table_update_rate, 0);
26
+ p.m_decompress_flags = aux_getoptu32(opts, id_flags, 0);
27
+ p.m_num_seed_bytes = 0;
28
+ p.m_pSeed_bytes = NULL;
29
+ p.m_table_max_update_interval = aux_getoptu32(opts, id_table_max_update_interval, 0);
30
+ p.m_table_update_interval_slow_rate = aux_getoptu32(opts, id_table_update_interval_slow_rate, 0);
31
+ }
32
+
33
+ return p;
34
+ }
35
+
36
+ /*
37
+ * call-seq:
38
+ * decode(string, max_decoded_size, opts = {}) -> decoded string
39
+ */
40
+ static VALUE
41
+ dec_s_decode(int argc, VALUE argv[], VALUE mod)
42
+ {
43
+ VALUE src, size, opts;
44
+ rb_scan_args(argc, argv, "2:", &src, &size, &opts);
45
+ rb_check_type(src, RUBY_T_STRING);
46
+ rb_str_locktmp(src);
47
+ size_t srcsize = RSTRING_LEN(src);
48
+ size_t destsize = NUM2SIZET(size);
49
+ VALUE dest = rb_str_buf_new(destsize);
50
+ lzham_decompress_params p = aux_conv_decode_params(opts);
51
+ lzham_decompress_status_t s;
52
+ s = lzham_decompress_memory(&p,
53
+ (lzham_uint8 *)RSTRING_PTR(dest), &destsize,
54
+ (lzham_uint8 *)RSTRING_PTR(src), srcsize, NULL);
55
+
56
+ rb_str_unlocktmp(src);
57
+
58
+ if (s != LZHAM_DECOMP_STATUS_SUCCESS) {
59
+ rb_str_resize(dest, 0);
60
+ extlzham_decode_error(s);
61
+ }
62
+
63
+ rb_str_resize(dest, destsize);
64
+ rb_str_set_len(dest, destsize);
65
+
66
+ return dest;
67
+ }
68
+
69
+ struct decoder
70
+ {
71
+ lzham_decompress_state_ptr decoder;
72
+ VALUE outport;
73
+ VALUE outbuf;
74
+ };
75
+
76
+ static void
77
+ dec_mark(struct decoder *p)
78
+ {
79
+ if (p) {
80
+ rb_gc_mark(p->outport);
81
+ rb_gc_mark(p->outbuf);
82
+ }
83
+ }
84
+
85
+ static void
86
+ dec_free(struct decoder *p)
87
+ {
88
+ if (p) {
89
+ if (p->decoder) {
90
+ lzham_decompress_deinit(p->decoder);
91
+ }
92
+ }
93
+ }
94
+
95
+ static VALUE
96
+ dec_alloc(VALUE klass)
97
+ {
98
+ struct decoder *p;
99
+ VALUE obj = Data_Make_Struct(klass, struct decoder, dec_mark, dec_free, p);
100
+ return obj;
101
+ }
102
+
103
+ static inline struct decoder *
104
+ getdecoderp(VALUE obj)
105
+ {
106
+ struct decoder *p;
107
+ Data_Get_Struct(obj, struct decoder, p);
108
+ return p;
109
+ }
110
+
111
+ static inline struct decoder *
112
+ getdecoder(VALUE obj)
113
+ {
114
+ struct decoder *p = getdecoderp(obj);
115
+ if (!p || !p->decoder) {
116
+ rb_raise(eError,
117
+ "not initialized - #<%s:%p>",
118
+ rb_obj_classname(obj), (void *)obj);
119
+ }
120
+ return p;
121
+ }
122
+
123
+ /*
124
+ * call-seq:
125
+ * initialize(outport = nil, opts = {})
126
+ */
127
+ static VALUE
128
+ dec_init(int argc, VALUE argv[], VALUE dec)
129
+ {
130
+ struct decoder *p = DATA_PTR(dec);
131
+ if (p->decoder) {
132
+ rb_raise(eError,
133
+ "already initialized - #<%s:%p>",
134
+ rb_obj_classname(dec), (void *)dec);
135
+ }
136
+
137
+ VALUE outport, opts;
138
+ rb_scan_args(argc, argv, "01:", &outport, &opts);
139
+ if (NIL_P(outport)) { outport = rb_str_buf_new(0); }
140
+ lzham_decompress_params params = aux_conv_decode_params(opts);
141
+ p->decoder = lzham_decompress_init(&params);
142
+ if (!p->decoder) {
143
+ rb_raise(eError,
144
+ "failed lzham_decompress_init - #<%s:%p>",
145
+ rb_obj_classname(dec), (void *)dec);
146
+ }
147
+
148
+ p->outbuf = rb_str_buf_new(WORKBUF_SIZE);
149
+ p->outport = outport;
150
+
151
+ return dec;
152
+ }
153
+
154
+ struct aux_lzham_decompress_nogvl
155
+ {
156
+ lzham_decompress_state_ptr state;
157
+ const lzham_uint8 *inbuf;
158
+ size_t *insize;
159
+ lzham_uint8 *outbuf;
160
+ size_t *outsize;
161
+ lzham_bool flags;
162
+ };
163
+
164
+ static void *
165
+ aux_lzham_decompress_nogvl(void *px)
166
+ {
167
+ struct aux_lzham_decompress_nogvl *p = px;
168
+ return (void *)lzham_decompress(p->state, p->inbuf, p->insize, p->outbuf, p->outsize, p->flags);
169
+ }
170
+
171
+ static inline lzham_decompress_status_t
172
+ aux_lzham_decompress(lzham_decompress_state_ptr state,
173
+ const lzham_uint8 *inbuf, size_t *insize,
174
+ lzham_uint8 *outbuf, size_t *outsize,
175
+ lzham_bool flags)
176
+ {
177
+ struct aux_lzham_decompress_nogvl p = {
178
+ .state = state,
179
+ .inbuf = inbuf,
180
+ .insize = insize,
181
+ .outbuf = outbuf,
182
+ .outsize = outsize,
183
+ .flags = flags,
184
+ };
185
+
186
+ return (lzham_decompress_status_t)rb_thread_call_without_gvl(aux_lzham_decompress_nogvl, &p, 0, 0);
187
+ }
188
+
189
+ struct dec_update_args
190
+ {
191
+ VALUE decoder;
192
+ VALUE src;
193
+ int flush;
194
+ };
195
+
196
+ static VALUE
197
+ dec_update_protected(struct dec_update_args *args)
198
+ {
199
+ struct decoder *p = getdecoder(args->decoder);
200
+ const char *inbuf, *intail;
201
+
202
+ if (NIL_P(args->src)) {
203
+ inbuf = NULL;
204
+ intail = NULL;
205
+ } else {
206
+ inbuf = RSTRING_PTR(args->src);
207
+ intail = inbuf + RSTRING_LEN(args->src);
208
+ }
209
+
210
+ lzham_decompress_status_t s;
211
+ do {
212
+ size_t insize = intail - inbuf;
213
+ aux_str_reserve(p->outbuf, WORKBUF_SIZE);
214
+ rb_str_locktmp(p->outbuf);
215
+ size_t outsize = rb_str_capacity(p->outbuf);
216
+ s = aux_lzham_decompress(p->decoder,
217
+ (lzham_uint8 *)inbuf, &insize,
218
+ (lzham_uint8 *)RSTRING_PTR(p->outbuf), &outsize, args->flush);
219
+ rb_str_unlocktmp(p->outbuf);
220
+ //fprintf(stderr, "%s:%d:%s: status=%s (%d), insize=%zu, outsize=%zu\n", __FILE__, __LINE__, __func__, extlzham_decode_status_str(s), s, insize, outsize);
221
+ if (!NIL_P(args->src)) {
222
+ inbuf += insize;
223
+ }
224
+ if (s != LZHAM_DECOMP_STATUS_NOT_FINISHED &&
225
+ s != LZHAM_DECOMP_STATUS_HAS_MORE_OUTPUT &&
226
+ s != LZHAM_DECOMP_STATUS_NEEDS_MORE_INPUT &&
227
+ s != LZHAM_DECOMP_STATUS_SUCCESS) {
228
+
229
+ extlzham_decode_error(s);
230
+ }
231
+ if (outsize > 0) {
232
+ rb_str_set_len(p->outbuf, outsize);
233
+ rb_funcall2(p->outport, id_op_lshift, 1, &p->outbuf);
234
+ }
235
+ } while (inbuf < intail || s == LZHAM_DECOMP_STATUS_HAS_MORE_OUTPUT);
236
+
237
+ return 0;
238
+ }
239
+
240
+ static inline void
241
+ dec_update_common(VALUE dec, VALUE src, int flush)
242
+ {
243
+ struct dec_update_args args = { dec, src, flush };
244
+ if (NIL_P(src)) {
245
+ dec_update_protected(&args);
246
+ } else {
247
+ rb_str_locktmp(src);
248
+ int state;
249
+ rb_protect((VALUE (*)(VALUE))dec_update_protected, (VALUE)&args, &state);
250
+ rb_str_unlocktmp(src);
251
+ if (state) {
252
+ rb_jump_tag(state);
253
+ }
254
+ }
255
+ }
256
+
257
+ /*
258
+ * call-seq:
259
+ * update(src, flush = false) -> self
260
+ */
261
+ static VALUE
262
+ dec_update(int argc, VALUE argv[], VALUE dec)
263
+ {
264
+ VALUE src, flush;
265
+ rb_scan_args(argc, argv, "11", &src, &flush);
266
+ rb_check_type(src, RUBY_T_STRING);
267
+ dec_update_common(dec, src, RTEST(flush) ? 1 : 0);
268
+ return dec;
269
+ }
270
+
271
+ static VALUE
272
+ dec_finish(VALUE dec)
273
+ {
274
+ dec_update_common(dec, Qnil, 1);
275
+ return dec;
276
+ }
277
+
278
+ static VALUE
279
+ dec_op_lshift(VALUE dec, VALUE src)
280
+ {
281
+ rb_check_type(src, RUBY_T_STRING);
282
+ dec_update_common(dec, src, 0);
283
+ return dec;
284
+ }
285
+
286
+ static VALUE
287
+ dec_get_outport(VALUE dec)
288
+ {
289
+ return getdecoder(dec)->outport;
290
+ }
291
+
292
+ static VALUE
293
+ dec_set_outport(VALUE dec, VALUE outport)
294
+ {
295
+ return getdecoder(dec)->outport = outport;
296
+ }
297
+
298
+ void
299
+ extlzham_init_decoder(void)
300
+ {
301
+ RDOCFAKE(mLZHAM = rb_define_module("LZHAM"));
302
+ cDecoder = rb_define_class_under(mLZHAM, "Decoder", rb_cObject);
303
+ rb_include_module(cDecoder, mConsts);
304
+ rb_define_alloc_func(cDecoder, dec_alloc);
305
+ rb_define_singleton_method(cDecoder, "decode", RUBY_METHOD_FUNC(dec_s_decode), -1);
306
+ rb_define_method(cDecoder, "initialize", RUBY_METHOD_FUNC(dec_init), -1);
307
+ rb_define_method(cDecoder, "update", RUBY_METHOD_FUNC(dec_update), -1);
308
+ rb_define_method(cDecoder, "finish", RUBY_METHOD_FUNC(dec_finish), 0);
309
+ rb_define_method(cDecoder, "<<", RUBY_METHOD_FUNC(dec_op_lshift), 1);
310
+ rb_define_method(cDecoder, "outport", RUBY_METHOD_FUNC(dec_get_outport), 0);
311
+ rb_define_method(cDecoder, "outport=", RUBY_METHOD_FUNC(dec_set_outport), 1);
312
+
313
+ }