extlzma 0.4-x86-mingw32

Sign up to get free protection for your applications and to get access to all the features.
data/ext/consts.c ADDED
@@ -0,0 +1,68 @@
1
+
2
+ #include "extlzma.h"
3
+
4
+ // SECTION: LZMA::Constants
5
+
6
+ static VALUE mConstants;
7
+
8
+ void
9
+ extlzma_init_Constants(void)
10
+ {
11
+ mConstants = rb_define_module_under(extlzma_mLZMA, "Constants");
12
+ rb_include_module(extlzma_mLZMA, mConstants);
13
+
14
+ // TODO: マクロを展開した状態で記述する。
15
+ // TODO: 目的は rdoc や yard で文書化できるようにするため。
16
+
17
+ #define DEFINE_CONSTANT(NAME, VALUE) \
18
+ { \
19
+ rb_define_const(mConstants, #NAME, VALUE); \
20
+ rb_define_const(mConstants, "LZMA_" #NAME, VALUE); \
21
+ } \
22
+
23
+ DEFINE_CONSTANT(NICE_DEFAULT, SIZET2NUM(64));
24
+ DEFINE_CONSTANT(DEPTH_DEFAULT, SIZET2NUM(0));
25
+
26
+ DEFINE_CONSTANT(PRESET_DEFAULT, UINT2NUM(LZMA_PRESET_DEFAULT));
27
+ DEFINE_CONSTANT(PRESET_LEVEL_MASK, UINT2NUM(LZMA_PRESET_LEVEL_MASK));
28
+ DEFINE_CONSTANT(PRESET_EXTREME, UINT2NUM(LZMA_PRESET_EXTREME));
29
+ DEFINE_CONSTANT(DICT_SIZE_MIN, UINT2NUM(LZMA_DICT_SIZE_MIN));
30
+ DEFINE_CONSTANT(DICT_SIZE_DEFAULT, UINT2NUM(LZMA_DICT_SIZE_DEFAULT));
31
+ DEFINE_CONSTANT(LCLP_MIN, UINT2NUM(LZMA_LCLP_MIN));
32
+ DEFINE_CONSTANT(LCLP_MAX, UINT2NUM(LZMA_LCLP_MAX));
33
+ DEFINE_CONSTANT(LC_DEFAULT, UINT2NUM(LZMA_LC_DEFAULT));
34
+ DEFINE_CONSTANT(LP_DEFAULT, UINT2NUM(LZMA_LP_DEFAULT));
35
+ DEFINE_CONSTANT(PB_MIN, UINT2NUM(LZMA_PB_MIN));
36
+ DEFINE_CONSTANT(PB_MAX, UINT2NUM(LZMA_PB_MAX));
37
+ DEFINE_CONSTANT(PB_DEFAULT, UINT2NUM(LZMA_PB_DEFAULT));
38
+ DEFINE_CONSTANT(MODE_FAST, UINT2NUM(LZMA_MODE_FAST));
39
+ DEFINE_CONSTANT(MODE_NORMAL, UINT2NUM(LZMA_MODE_NORMAL));
40
+ DEFINE_CONSTANT(MF_HC3, UINT2NUM(LZMA_MF_HC3));
41
+ DEFINE_CONSTANT(MF_HC4, UINT2NUM(LZMA_MF_HC4));
42
+ DEFINE_CONSTANT(MF_BT2, UINT2NUM(LZMA_MF_BT2));
43
+ DEFINE_CONSTANT(MF_BT3, UINT2NUM(LZMA_MF_BT3));
44
+ DEFINE_CONSTANT(MF_BT4, UINT2NUM(LZMA_MF_BT4));
45
+
46
+ DEFINE_CONSTANT(CHECK_NONE, UINT2NUM(LZMA_CHECK_NONE));
47
+ DEFINE_CONSTANT(CHECK_CRC32, UINT2NUM(LZMA_CHECK_CRC32));
48
+ DEFINE_CONSTANT(CHECK_CRC64, UINT2NUM(LZMA_CHECK_CRC64));
49
+ DEFINE_CONSTANT(CHECK_SHA256, UINT2NUM(LZMA_CHECK_SHA256));
50
+
51
+ DEFINE_CONSTANT(RUN, UINT2NUM(LZMA_RUN));
52
+ DEFINE_CONSTANT(FULL_FLUSH, UINT2NUM(LZMA_FULL_FLUSH));
53
+ DEFINE_CONSTANT(SYNC_FLUSH, UINT2NUM(LZMA_SYNC_FLUSH));
54
+ DEFINE_CONSTANT(FINISH, UINT2NUM(LZMA_FINISH));
55
+
56
+ DEFINE_CONSTANT(OK, UINT2NUM(LZMA_OK));
57
+ DEFINE_CONSTANT(STREAM_END, UINT2NUM(LZMA_STREAM_END));
58
+ DEFINE_CONSTANT(NO_CHECK, UINT2NUM(LZMA_NO_CHECK));
59
+ DEFINE_CONSTANT(UNSUPPORTED_CHECK, UINT2NUM(LZMA_UNSUPPORTED_CHECK));
60
+ DEFINE_CONSTANT(GET_CHECK, UINT2NUM(LZMA_GET_CHECK));
61
+ DEFINE_CONSTANT(MEM_ERROR, UINT2NUM(LZMA_MEM_ERROR));
62
+ DEFINE_CONSTANT(MEMLIMIT_ERROR, UINT2NUM(LZMA_MEMLIMIT_ERROR));
63
+ DEFINE_CONSTANT(FORMAT_ERROR, UINT2NUM(LZMA_FORMAT_ERROR));
64
+ DEFINE_CONSTANT(OPTIONS_ERROR, UINT2NUM(LZMA_OPTIONS_ERROR));
65
+ DEFINE_CONSTANT(DATA_ERROR, UINT2NUM(LZMA_DATA_ERROR));
66
+ DEFINE_CONSTANT(BUF_ERROR, UINT2NUM(LZMA_BUF_ERROR));
67
+ DEFINE_CONSTANT(PROG_ERROR, UINT2NUM(LZMA_PROG_ERROR));
68
+ }
data/ext/error.c ADDED
@@ -0,0 +1,77 @@
1
+
2
+ #include "extlzma.h"
3
+
4
+ // TODO: 例外を整理して数を減らす。
5
+
6
+ VALUE extlzma_mExceptions;
7
+ VALUE extlzma_eBasicException;
8
+ VALUE extlzma_eStreamEnd;
9
+ VALUE extlzma_eNoCheck;
10
+ VALUE extlzma_eUnsupportedCheck;
11
+ VALUE extlzma_eGetCheck;
12
+ VALUE extlzma_eMemError;
13
+ VALUE extlzma_eMemlimitError;
14
+ VALUE extlzma_eFormatError;
15
+ VALUE extlzma_eOptionsError;
16
+ VALUE extlzma_eDataError;
17
+ VALUE extlzma_eBufError;
18
+ VALUE extlzma_eProgError;
19
+ VALUE extlzma_eFilterTooLong;
20
+ VALUE extlzma_eBadPreset;
21
+
22
+ static inline VALUE
23
+ lookup_exception(lzma_ret status)
24
+ {
25
+ switch (status) {
26
+ case LZMA_OK: return Qnil;
27
+ case LZMA_STREAM_END: return extlzma_eStreamEnd;
28
+ case LZMA_NO_CHECK: return extlzma_eNoCheck;
29
+ case LZMA_UNSUPPORTED_CHECK: return extlzma_eUnsupportedCheck;
30
+ case LZMA_GET_CHECK: return extlzma_eGetCheck;
31
+ case LZMA_MEM_ERROR: return extlzma_eMemError;
32
+ case LZMA_MEMLIMIT_ERROR: return extlzma_eMemlimitError;
33
+ case LZMA_FORMAT_ERROR: return extlzma_eFormatError;
34
+ case LZMA_OPTIONS_ERROR: return extlzma_eOptionsError;
35
+ case LZMA_DATA_ERROR: return extlzma_eDataError;
36
+ case LZMA_BUF_ERROR: return extlzma_eBufError;
37
+ case LZMA_PROG_ERROR: return extlzma_eProgError;
38
+ default: return rb_eRuntimeError;
39
+ }
40
+ }
41
+
42
+ VALUE
43
+ extlzma_lookup_error(lzma_ret status)
44
+ {
45
+ return lookup_exception(status);
46
+ }
47
+
48
+ void
49
+ extlzma_init_Exceptions(void)
50
+ {
51
+ extlzma_mExceptions = rb_define_module_under(extlzma_mLZMA, "Exceptions");
52
+ rb_include_module(extlzma_mLZMA, extlzma_mExceptions);
53
+
54
+ extlzma_eBasicException = rb_define_class_under(extlzma_mExceptions, "BasicException", rb_eStandardError);
55
+ rb_define_class_under(extlzma_mExceptions, "FilterTooLong", extlzma_eBasicException);
56
+ rb_define_class_under(extlzma_mExceptions, "BadPreset", extlzma_eBasicException);
57
+
58
+ #define DEFINE_EXCEPTION(CLASS, STATUS) \
59
+ { \
60
+ extlzma_e ## CLASS = \
61
+ rb_define_class_under(extlzma_mExceptions, \
62
+ #CLASS, extlzma_eBasicException); \
63
+ rb_define_const(extlzma_e ## CLASS, "STATUS", SIZET2NUM(STATUS)); \
64
+ } \
65
+
66
+ DEFINE_EXCEPTION(StreamEnd, LZMA_STREAM_END);
67
+ DEFINE_EXCEPTION(NoCheck, LZMA_NO_CHECK);
68
+ DEFINE_EXCEPTION(UnsupportedCheck, LZMA_UNSUPPORTED_CHECK);
69
+ DEFINE_EXCEPTION(GetCheck, LZMA_GET_CHECK);
70
+ DEFINE_EXCEPTION(MemError, LZMA_MEM_ERROR);
71
+ DEFINE_EXCEPTION(MemlimitError, LZMA_MEMLIMIT_ERROR);
72
+ DEFINE_EXCEPTION(FormatError, LZMA_FORMAT_ERROR);
73
+ DEFINE_EXCEPTION(OptionsError, LZMA_OPTIONS_ERROR);
74
+ DEFINE_EXCEPTION(DataError, LZMA_DATA_ERROR);
75
+ DEFINE_EXCEPTION(BufError, LZMA_BUF_ERROR);
76
+ DEFINE_EXCEPTION(ProgError, LZMA_PROG_ERROR);
77
+ }
data/ext/extconf.rb ADDED
@@ -0,0 +1,27 @@
1
+ #!ruby
2
+
3
+ require "mkmf"
4
+
5
+ needlist = []
6
+
7
+ have_header "lzma.h" or needlist << "lzma.h"
8
+ have_library "lzma" or needlist << "liblzma.a"
9
+
10
+ unless needlist.empty?
11
+ abort "#$0: dependency files are not found (#{needlist.join(" ")})."
12
+ end
13
+
14
+ staticlink = arg_config("--liblzma-static-link", false)
15
+
16
+ if staticlink
17
+ # 静的ライブラリリンクを優先する
18
+ $libs = ["-Wl,-dn,-llzma", "-Wl,-llzma", $libs].join(" ")
19
+ end
20
+
21
+ if RbConfig::CONFIG["arch"] =~ /mingw/
22
+ $LDFLAGS << " -static-libgcc " if try_link "void main(void){}", " -static-libgcc "
23
+ end
24
+
25
+ $LDFLAGS << " -Wl,-Bsymbolic " if try_link "void main(void){}", " -Wl,-Bsymbolic "
26
+
27
+ create_makefile "extlzma"
data/ext/extlzma.c ADDED
@@ -0,0 +1,97 @@
1
+ /*
2
+ * liblzma.c -
3
+ * - Author: dearblue <dearblue@users.osdn.me>
4
+ * - Copyright: Copyright (c) 2010 dearblue
5
+ * - License: Distributed under the 2-clause BSD License
6
+ */
7
+
8
+ #include "extlzma.h"
9
+
10
+ VALUE extlzma_mLZMA;
11
+
12
+ ID extlzma_id_dictsize;
13
+ ID extlzma_id_predict;
14
+ ID extlzma_id_lc;
15
+ ID extlzma_id_lp;
16
+ ID extlzma_id_pb;
17
+ ID extlzma_id_mode;
18
+ ID extlzma_id_nice;
19
+ ID extlzma_id_mf;
20
+ ID extlzma_id_depth;
21
+ ID extlzma_id_check;
22
+ ID extlzma_id_none;
23
+ ID extlzma_id_crc32;
24
+ ID extlzma_id_crc64;
25
+ ID extlzma_id_sha256;
26
+
27
+ static VALUE
28
+ libver_major(VALUE obj)
29
+ {
30
+ return INT2FIX(LZMA_VERSION_MAJOR);
31
+ }
32
+
33
+ static VALUE
34
+ libver_minor(VALUE obj)
35
+ {
36
+ return INT2FIX(LZMA_VERSION_MINOR);
37
+ }
38
+
39
+ static VALUE
40
+ libver_patch(VALUE obj)
41
+ {
42
+ return INT2FIX(LZMA_VERSION_PATCH);
43
+ }
44
+
45
+ static VALUE
46
+ libver_to_s(VALUE obj)
47
+ {
48
+ return rb_str_new_cstr(LZMA_VERSION_STRING);
49
+ }
50
+
51
+
52
+ static void
53
+ extlzma_init_LIBVER(void)
54
+ {
55
+ VALUE vers[] = {
56
+ INT2FIX(LZMA_VERSION_MAJOR),
57
+ INT2FIX(LZMA_VERSION_MINOR),
58
+ INT2FIX(LZMA_VERSION_PATCH)
59
+ };
60
+
61
+ VALUE libver = rb_ary_new4(ELEMENTOF(vers), vers);
62
+ rb_define_singleton_method(libver, "major", RUBY_METHOD_FUNC(libver_major), 0);
63
+ rb_define_singleton_method(libver, "minor", RUBY_METHOD_FUNC(libver_minor), 0);
64
+ rb_define_singleton_method(libver, "patch", RUBY_METHOD_FUNC(libver_patch), 0);
65
+ rb_define_singleton_method(libver, "to_s", RUBY_METHOD_FUNC(libver_to_s), 0);
66
+ rb_define_const(extlzma_mLZMA, "LIBRARY_VERSION", rb_ary_freeze(libver));
67
+ }
68
+
69
+ void
70
+ Init_extlzma(void)
71
+ {
72
+ extlzma_id_dictsize = rb_intern("dictsize");
73
+ extlzma_id_predict = rb_intern("predict");
74
+ extlzma_id_lc = rb_intern("lc");
75
+ extlzma_id_lp = rb_intern("lp");
76
+ extlzma_id_pb = rb_intern("pb");
77
+ extlzma_id_mode = rb_intern("mode");
78
+ extlzma_id_nice = rb_intern("nice");
79
+ extlzma_id_mf = rb_intern("mf");
80
+ extlzma_id_depth = rb_intern("depth");
81
+ extlzma_id_check = rb_intern("check");
82
+ extlzma_id_none = rb_intern("none");
83
+ extlzma_id_crc32 = rb_intern("crc32");
84
+ extlzma_id_crc64 = rb_intern("crc64");
85
+ extlzma_id_sha256 = rb_intern("sha256");
86
+
87
+ extlzma_mLZMA = rb_define_module("LZMA");
88
+ rb_define_const(extlzma_mLZMA, "LZMA", extlzma_mLZMA);
89
+
90
+ extlzma_init_Utils();
91
+ extlzma_init_Constants();
92
+ extlzma_init_Exceptions();
93
+ extlzma_init_Filter();
94
+ extlzma_init_Stream();
95
+ extlzma_init_Index();
96
+ extlzma_init_LIBVER();
97
+ }
data/ext/extlzma.h ADDED
@@ -0,0 +1,177 @@
1
+ #ifndef EXTLZMA_H
2
+ #define EXTLZMA_H 1
3
+
4
+ #include <stdarg.h>
5
+ #include <lzma.h>
6
+ #include <ruby.h>
7
+ #include <ruby/thread.h>
8
+ #include <ruby/intern.h>
9
+
10
+ #define LOG { fprintf(stderr, "%s:%d:%s\n", __FILE__, __LINE__, __func__); }
11
+
12
+ #define LOGF(FORMAT, ...) \
13
+ { \
14
+ fprintf(stderr, "%s:%d:%s: " FORMAT "\n", \
15
+ __FILE__, __LINE__, __func__, ## __VA_ARGS__); \
16
+ } \
17
+
18
+ #define ELEMENTOF(VECT) (sizeof(VECT) / sizeof((VECT)[0]))
19
+
20
+ #define AUX_FUNCALL(RECV, METHOD, ...) \
21
+ ({ \
22
+ VALUE args__aux_funcall__[] = { __VA_ARGS__ }; \
23
+ rb_funcall2((RECV), (METHOD), \
24
+ ELEMENTOF(args__aux_funcall__), \
25
+ args__aux_funcall__); \
26
+ }) \
27
+
28
+ extern VALUE extlzma_mLZMA;
29
+ extern VALUE extlzma_cFilter;
30
+ extern VALUE extlzma_cStream;
31
+ extern VALUE extlzma_mExceptions;
32
+
33
+ extern VALUE extlzma_eBasicException;
34
+ extern VALUE extlzma_eStreamEnd;
35
+ extern VALUE extlzma_eNoCheck;
36
+ extern VALUE extlzma_eUnsupportedCheck;
37
+ extern VALUE extlzma_eGetCheck;
38
+ extern VALUE extlzma_eMemError;
39
+ extern VALUE extlzma_eMemlimitError;
40
+ extern VALUE extlzma_eFormatError;
41
+ extern VALUE extlzma_eOptionsError;
42
+ extern VALUE extlzma_eDataError;
43
+ extern VALUE extlzma_eBufError;
44
+ extern VALUE extlzma_eProgError;
45
+ extern VALUE extlzma_eFilterTooLong;
46
+ extern VALUE extlzma_eBadPreset;
47
+
48
+ extern ID extlzma_id_dictsize;
49
+ extern ID extlzma_id_predict;
50
+ extern ID extlzma_id_lc;
51
+ extern ID extlzma_id_lp;
52
+ extern ID extlzma_id_pb;
53
+ extern ID extlzma_id_mode;
54
+ extern ID extlzma_id_nice;
55
+ extern ID extlzma_id_mf;
56
+ extern ID extlzma_id_depth;
57
+ extern ID extlzma_id_check;
58
+ extern ID extlzma_id_none;
59
+ extern ID extlzma_id_crc32;
60
+ extern ID extlzma_id_crc64;
61
+ extern ID extlzma_id_sha256;
62
+
63
+ extern void extlzma_init_Stream(void);
64
+ extern void extlzma_init_Utils(void);
65
+ extern void extlzma_init_Constants(void);
66
+ extern void extlzma_init_Exceptions(void);
67
+ extern void extlzma_init_Filter(void);
68
+ extern void extlzma_init_Index(void);
69
+ extern VALUE extlzma_lookup_error(lzma_ret status);
70
+
71
+ static inline int
72
+ aux_lzma_isfailed(lzma_ret status)
73
+ {
74
+ return status != 0;
75
+ }
76
+
77
+ static inline void
78
+ aux_lzma_test(lzma_ret status)
79
+ {
80
+ if (aux_lzma_isfailed(status)) {
81
+ VALUE exc = extlzma_lookup_error(status);
82
+ exc = rb_exc_new2(exc, "failed lzma processing");
83
+ rb_exc_raise(exc);
84
+ }
85
+ }
86
+
87
+ #define AUX_LZMA_TEST(STATUS) aux_lzma_test((STATUS))
88
+
89
+ static inline void *
90
+ checkref(VALUE obj, void *p)
91
+ {
92
+ if (!p) {
93
+ rb_raise(rb_eArgError,
94
+ "not initialized yet - #<%s:%p>",
95
+ rb_obj_classname(obj), (void *)obj);
96
+ }
97
+
98
+ return p;
99
+ }
100
+
101
+ static inline void *
102
+ check_notref(VALUE obj, void *p)
103
+ {
104
+ if (p) {
105
+ rb_raise(rb_eArgError,
106
+ "already initialized - #<%s:%p>",
107
+ rb_obj_classname(obj), (void *)obj);
108
+ }
109
+
110
+ return p;
111
+ }
112
+
113
+ static inline void *
114
+ getrefp(VALUE obj)
115
+ {
116
+ void *p;
117
+ Data_Get_Struct(obj, void, p);
118
+ return p;
119
+ }
120
+
121
+ static inline void *
122
+ getref(VALUE obj)
123
+ {
124
+ return checkref(obj, getrefp(obj));
125
+ }
126
+
127
+ static inline lzma_filter *
128
+ getfilterp(VALUE obj)
129
+ {
130
+ return getrefp(obj);
131
+ }
132
+
133
+ static inline lzma_filter *
134
+ extlzma_getfilter(VALUE obj)
135
+ {
136
+ return getref(obj);
137
+ }
138
+
139
+ // rdoc に対して定義されているものと錯覚させるマクロ
140
+ #define RDOCFAKE(...)
141
+
142
+ #define EXTLZMA_NOT_REACHABLE \
143
+ rb_bug("NOT REACHABLE HERE - %s:%d(%s)", \
144
+ __FILE__, __LINE__, __func__); \
145
+
146
+
147
+ static inline void *
148
+ aux_thread_call_without_gvl_main(void *pp)
149
+ {
150
+ va_list *p = (va_list *)pp;
151
+ void *(*func)(void *) = va_arg(*p, void *(*)(void *));
152
+ va_list *va2 = va_arg(*p, va_list *);
153
+ return func(va2);
154
+ }
155
+
156
+ static inline void *
157
+ aux_thread_call_without_gvl_dummy(void *dummy, ...)
158
+ {
159
+ va_list va;
160
+ va_start(va, dummy);
161
+ void *p = rb_thread_call_without_gvl(
162
+ aux_thread_call_without_gvl_main, &va, RUBY_UBF_PROCESS, 0);
163
+ va_end(va);
164
+ return p;
165
+ }
166
+
167
+ static inline void *
168
+ aux_thread_call_without_gvl(void *(*func)(va_list *), ...)
169
+ {
170
+ va_list va;
171
+ va_start(va, func);
172
+ void *p = aux_thread_call_without_gvl_dummy(0, func, &va);
173
+ va_end(va);
174
+ return p;
175
+ }
176
+
177
+ #endif /* EXTLZMA_H */