node-liblzma 1.1.8 → 1.1.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/History.md CHANGED
@@ -1,8 +1,10 @@
1
+ # 1.1.9
2
+ * Fix building if no prebuilt binary found
3
+
1
4
  # 1.1.7
2
5
  * Fix build system
3
6
  * Fix release system
4
7
  * Fix documentation
5
- * Fix building if no prebuilt binary found
6
8
 
7
9
  # 1.1.0
8
10
  * Refactor to deprecate Nan in favor of N-API
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-liblzma",
3
- "version": "1.1.8",
3
+ "version": "1.1.9",
4
4
  "description": "NodeJS wrapper for liblzma",
5
5
  "main": "./lib/lzma.js",
6
6
  "scripts": {
Binary file
Binary file
@@ -0,0 +1,107 @@
1
+ /**
2
+ * node-liblzma - Node.js bindings for liblzma
3
+ * Copyright (C) 2014-2015 Olivier Orabona <olivier.orabona@gmail.com>
4
+ *
5
+ * This program is free software: you can redistribute it and/or modify
6
+ * it under the terms of the GNU Lesser General Public License as published by
7
+ * the Free Software Foundation, either version 3 of the License, or
8
+ * (at your option) any later version.
9
+ *
10
+ * This program is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ * GNU General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU Lesser General Public License
16
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
17
+ **/
18
+
19
+ #include "node-liblzma.hpp"
20
+
21
+ Napi::Object Init(Napi::Env env, Napi::Object exports)
22
+ {
23
+ LZMA::Init(env, exports);
24
+
25
+ // Constants
26
+ // enum lzma_ret
27
+ exports.Set(Napi::String::New(env, "LZMA_OK"), Napi::Number::New(env, LZMA_OK));
28
+ exports.Set(Napi::String::New(env, "LZMA_STREAM_END"), Napi::Number::New(env, LZMA_STREAM_END));
29
+ exports.Set(Napi::String::New(env, "LZMA_NO_CHECK"), Napi::Number::New(env, LZMA_NO_CHECK));
30
+ exports.Set(Napi::String::New(env, "LZMA_UNSUPPORTED_CHECK"), Napi::Number::New(env, LZMA_UNSUPPORTED_CHECK));
31
+ exports.Set(Napi::String::New(env, "LZMA_GET_CHECK"), Napi::Number::New(env, LZMA_GET_CHECK));
32
+ exports.Set(Napi::String::New(env, "LZMA_MEM_ERROR"), Napi::Number::New(env, LZMA_MEM_ERROR));
33
+ exports.Set(Napi::String::New(env, "LZMA_MEMLIMIT_ERROR"), Napi::Number::New(env, LZMA_MEMLIMIT_ERROR));
34
+ exports.Set(Napi::String::New(env, "LZMA_FORMAT_ERROR"), Napi::Number::New(env, LZMA_FORMAT_ERROR));
35
+ exports.Set(Napi::String::New(env, "LZMA_OPTIONS_ERROR"), Napi::Number::New(env, LZMA_OPTIONS_ERROR));
36
+ exports.Set(Napi::String::New(env, "LZMA_DATA_ERROR"), Napi::Number::New(env, LZMA_DATA_ERROR));
37
+ exports.Set(Napi::String::New(env, "LZMA_BUF_ERROR"), Napi::Number::New(env, LZMA_BUF_ERROR));
38
+ exports.Set(Napi::String::New(env, "LZMA_PROG_ERROR"), Napi::Number::New(env, LZMA_PROG_ERROR));
39
+
40
+ // enum lzma_action
41
+ exports.Set(Napi::String::New(env, "LZMA_RUN"), Napi::Number::New(env, LZMA_RUN));
42
+ exports.Set(Napi::String::New(env, "LZMA_SYNC_FLUSH"), Napi::Number::New(env, LZMA_SYNC_FLUSH));
43
+ exports.Set(Napi::String::New(env, "LZMA_FULL_FLUSH"), Napi::Number::New(env, LZMA_FULL_FLUSH));
44
+ exports.Set(Napi::String::New(env, "LZMA_FINISH"), Napi::Number::New(env, LZMA_FINISH));
45
+
46
+ // enum lzma_check
47
+ exports.Set(Napi::String::New(env, "LZMA_CHECK_NONE"), Napi::Number::New(env, LZMA_CHECK_NONE));
48
+ exports.Set(Napi::String::New(env, "LZMA_CHECK_CRC32"), Napi::Number::New(env, LZMA_CHECK_CRC32));
49
+ exports.Set(Napi::String::New(env, "LZMA_CHECK_CRC64"), Napi::Number::New(env, LZMA_CHECK_CRC64));
50
+ exports.Set(Napi::String::New(env, "LZMA_CHECK_SHA256"), Napi::Number::New(env, LZMA_CHECK_SHA256));
51
+
52
+ // lzma_match_finder
53
+ exports.Set(Napi::String::New(env, "MF_HC3"), Napi::Number::New(env, LZMA_MF_HC3));
54
+ exports.Set(Napi::String::New(env, "MF_HC4"), Napi::Number::New(env, LZMA_MF_HC4));
55
+ exports.Set(Napi::String::New(env, "MF_BT2"), Napi::Number::New(env, LZMA_MF_BT2));
56
+ exports.Set(Napi::String::New(env, "MF_BT3"), Napi::Number::New(env, LZMA_MF_BT3));
57
+ exports.Set(Napi::String::New(env, "MF_BT4"), Napi::Number::New(env, LZMA_MF_BT4));
58
+
59
+ // lzma_mode
60
+ exports.Set(Napi::String::New(env, "LZMA_MODE_FAST"), Napi::Number::New(env, LZMA_MODE_FAST));
61
+ exports.Set(Napi::String::New(env, "LZMA_MODE_NORMAL"), Napi::Number::New(env, LZMA_MODE_NORMAL));
62
+
63
+ // defines
64
+ exports.Set(Napi::String::New(env, "LZMA_FILTER_X86"), Napi::Number::New(env, LZMA_FILTER_X86));
65
+ exports.Set(Napi::String::New(env, "LZMA_FILTER_POWERPC"), Napi::Number::New(env, LZMA_FILTER_POWERPC));
66
+ exports.Set(Napi::String::New(env, "LZMA_FILTER_IA64"), Napi::Number::New(env, LZMA_FILTER_IA64));
67
+ exports.Set(Napi::String::New(env, "LZMA_FILTER_ARM"), Napi::Number::New(env, LZMA_FILTER_ARM));
68
+ exports.Set(Napi::String::New(env, "LZMA_FILTER_ARMTHUMB"), Napi::Number::New(env, LZMA_FILTER_ARMTHUMB));
69
+ exports.Set(Napi::String::New(env, "LZMA_FILTER_SPARC"), Napi::Number::New(env, LZMA_FILTER_SPARC));
70
+ exports.Set(Napi::String::New(env, "LZMA_FILTER_DELTA"), Napi::Number::New(env, LZMA_FILTER_DELTA));
71
+ exports.Set(Napi::String::New(env, "LZMA_FILTERS_MAX"), Napi::Number::New(env, LZMA_FILTERS_MAX));
72
+ exports.Set(Napi::String::New(env, "LZMA_FILTER_LZMA1"), Napi::Number::New(env, LZMA_FILTER_LZMA1));
73
+ exports.Set(Napi::String::New(env, "LZMA_FILTER_LZMA2"), Napi::Number::New(env, LZMA_FILTER_LZMA2));
74
+ exports.Set(Napi::String::New(env, "LZMA_VLI_UNKNOWN"), Napi::Number::New(env, LZMA_VLI_UNKNOWN));
75
+
76
+ exports.Set(Napi::String::New(env, "LZMA_VLI_BYTES_MAX"), Napi::Number::New(env, LZMA_VLI_BYTES_MAX));
77
+ exports.Set(Napi::String::New(env, "LZMA_CHECK_ID_MAX"), Napi::Number::New(env, LZMA_CHECK_ID_MAX));
78
+ exports.Set(Napi::String::New(env, "LZMA_CHECK_SIZE_MAX"), Napi::Number::New(env, LZMA_CHECK_SIZE_MAX));
79
+ exports.Set(Napi::String::New(env, "LZMA_PRESET_DEFAULT"), Napi::Number::New(env, LZMA_PRESET_DEFAULT));
80
+ exports.Set(Napi::String::New(env, "LZMA_PRESET_LEVEL_MASK"), Napi::Number::New(env, LZMA_PRESET_LEVEL_MASK));
81
+ exports.Set(Napi::String::New(env, "LZMA_PRESET_EXTREME"), Napi::Number::New(env, LZMA_PRESET_EXTREME));
82
+ exports.Set(Napi::String::New(env, "LZMA_TELL_NO_CHECK"), Napi::Number::New(env, LZMA_TELL_NO_CHECK));
83
+ exports.Set(Napi::String::New(env, "LZMA_TELL_UNSUPPORTED_CHECK"), Napi::Number::New(env, LZMA_TELL_UNSUPPORTED_CHECK));
84
+ exports.Set(Napi::String::New(env, "LZMA_TELL_ANY_CHECK"), Napi::Number::New(env, LZMA_TELL_ANY_CHECK));
85
+ exports.Set(Napi::String::New(env, "LZMA_CONCATENATED"), Napi::Number::New(env, LZMA_CONCATENATED));
86
+ exports.Set(Napi::String::New(env, "LZMA_STREAM_HEADER_SIZE"), Napi::Number::New(env, LZMA_STREAM_HEADER_SIZE));
87
+ exports.Set(Napi::String::New(env, "LZMA_VERSION_MAJOR"), Napi::Number::New(env, LZMA_VERSION_MAJOR));
88
+ exports.Set(Napi::String::New(env, "LZMA_VERSION_MINOR"), Napi::Number::New(env, LZMA_VERSION_MINOR));
89
+ exports.Set(Napi::String::New(env, "LZMA_VERSION_PATCH"), Napi::Number::New(env, LZMA_VERSION_PATCH));
90
+ exports.Set(Napi::String::New(env, "LZMA_VERSION_STABILITY"), Napi::Number::New(env, LZMA_VERSION_STABILITY));
91
+ exports.Set(Napi::String::New(env, "LZMA_VERSION_STABILITY_ALPHA"), Napi::Number::New(env, LZMA_VERSION_STABILITY_ALPHA));
92
+ exports.Set(Napi::String::New(env, "LZMA_VERSION_STABILITY_BETA"), Napi::Number::New(env, LZMA_VERSION_STABILITY_BETA));
93
+ exports.Set(Napi::String::New(env, "LZMA_VERSION_STABILITY_STABLE"), Napi::Number::New(env, LZMA_VERSION_STABILITY_STABLE));
94
+ exports.Set(Napi::String::New(env, "LZMA_VERSION"), Napi::Number::New(env, LZMA_VERSION));
95
+ exports.Set(Napi::String::New(env, "LZMA_VERSION_STRING"), Napi::String::New(env, LZMA_VERSION_STRING));
96
+
97
+ // LZMAStream flags
98
+ exports.Set(Napi::String::New(env, "STREAM_ENCODE"), Napi::Number::New(env, STREAM_ENCODE));
99
+ exports.Set(Napi::String::New(env, "STREAM_DECODE"), Napi::Number::New(env, STREAM_DECODE));
100
+ exports.Set(Napi::String::New(env, "BUFSIZ"), Napi::Number::New(env, BUFSIZ));
101
+
102
+ // Tell companion script if we are thread-able or not
103
+ exports.Set(Napi::String::New(env, "HAS_THREADS_SUPPORT"), Napi::Boolean::New(env, HAS_THREADS_SUPPORT));
104
+ return exports;
105
+ }
106
+
107
+ NODE_API_MODULE(NODE_GYP_MODULE_NAME, Init)
@@ -0,0 +1,340 @@
1
+ /**
2
+ * node-liblzma - Node.js bindings for liblzma
3
+ * Copyright (C) Olivier Orabona <olivier.orabona@gmail.com>
4
+ *
5
+ * This program is free software: you can redistribute it and/or modify
6
+ * it under the terms of the GNU Lesser General Public License as published by
7
+ * the Free Software Foundation, either version 3 of the License, or
8
+ * (at your option) any later version.
9
+ *
10
+ * This program is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ * GNU General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU Lesser General Public License
16
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
17
+ **/
18
+
19
+ #include "node-liblzma.hpp"
20
+ #include <node_buffer.h>
21
+
22
+ Napi::Value LZMA::Close(const Napi::CallbackInfo &info) {
23
+ Napi::Env env = info.Env();
24
+
25
+ return LZMA::Close(env);
26
+ }
27
+
28
+ Napi::Value LZMA::Close(const Napi::Env &env) {
29
+ Napi::MemoryManagement::AdjustExternalMemory(env, -int64_t(sizeof(LZMA)));
30
+
31
+ if(_wip) {
32
+ _pending_close = true;
33
+ return env.Undefined();
34
+ }
35
+
36
+ _pending_close = false;
37
+
38
+ lzma_end(&_stream);
39
+
40
+ return env.Undefined();
41
+ }
42
+
43
+ void LZMA::Init(Napi::Env env, Napi::Object exports) {
44
+ Napi::Function func =
45
+ DefineClass(env,
46
+ "LZMA",
47
+ {
48
+ InstanceMethod("code", &LZMA::Code<true>),
49
+ InstanceMethod("codeSync", &LZMA::Code<false>),
50
+ InstanceMethod("close", &LZMA::Close)
51
+ });
52
+
53
+ Napi::FunctionReference* constructor = new Napi::FunctionReference();
54
+ *constructor = Napi::Persistent(func);
55
+ env.SetInstanceData(constructor);
56
+
57
+ exports.Set("LZMA", func);
58
+ }
59
+
60
+ LZMA::LZMA(const Napi::CallbackInfo& info) : Napi::ObjectWrap<LZMA>(info), _stream(LZMA_STREAM_INIT),
61
+ _wip(false), _pending_close(false), _worker(nullptr)
62
+ {
63
+ Napi::Env env = info.Env();
64
+
65
+ if( info.Length() != 2 ) {
66
+ Napi::TypeError::New(env, "Wrong number of arguments, expected mode(int) and opts(object)").ThrowAsJavaScriptException();
67
+ return;
68
+ }
69
+
70
+ uint32_t mode = info[0].ToNumber().Uint32Value();
71
+
72
+ if (!info[1].IsObject()) {
73
+ Napi::TypeError::New(env, "Expected object as second argument").ThrowAsJavaScriptException();
74
+ return;
75
+ }
76
+
77
+ Napi::Object opts = info[1].ToObject();
78
+
79
+ Napi::Value optsCheck = opts.Get("check");
80
+ if (!optsCheck.IsNumber()) {
81
+ Napi::TypeError::New(env, "Expected 'check' to be an integer").ThrowAsJavaScriptException();
82
+ return;
83
+ }
84
+
85
+ lzma_check check = static_cast<lzma_check>(optsCheck.ToNumber().Uint32Value());
86
+
87
+ Napi::Value optsPreset = opts.Get("preset");
88
+ if (!optsPreset.IsNumber()) {
89
+ Napi::TypeError::New(env, "Expected 'preset' to be an integer").ThrowAsJavaScriptException();
90
+ return;
91
+ }
92
+
93
+ uint32_t preset = optsPreset.ToNumber().Uint32Value();
94
+
95
+ Napi::Value optsFilters = opts.Get("filters");
96
+ if (!optsFilters.IsArray()) {
97
+ Napi::TypeError::New(env, "Expected 'filters' to be an array").ThrowAsJavaScriptException();
98
+ return;
99
+ }
100
+
101
+ Napi::Array filters_handle = optsFilters.As<Napi::Array>();
102
+
103
+ uint32_t filters_len = filters_handle.Length();
104
+
105
+ // We will need to add LZMA_VLI_UNKNOWN after, so user defined filters may
106
+ // not exceed LZMA_FILTERS_MAX - 1.
107
+ if( filters_len > LZMA_FILTERS_MAX - 1) {
108
+ Napi::RangeError::New(env, "More filters than allowed maximum").ThrowAsJavaScriptException();
109
+ return;
110
+ }
111
+
112
+ lzma_options_lzma opt_lzma2;
113
+ if( lzma_lzma_preset(&opt_lzma2, preset) ) {
114
+ Napi::Error::New(env, "Unsupported preset, possibly a bug").ThrowAsJavaScriptException();
115
+ return;
116
+ }
117
+
118
+ // Add extra slot for LZMA_VLI_UNKNOWN.
119
+ this->filters = new lzma_filter[filters_len + 1];
120
+
121
+ for(uint32_t i = 0; i < filters_len; ++i) {
122
+ Napi::Value filter = filters_handle.Get(i);
123
+ if (!filter.IsNumber()) {
124
+ Napi::Error::New(env, "Filter must be an integer").ThrowAsJavaScriptException();
125
+ return;
126
+ }
127
+
128
+ uint64_t current = filter.ToNumber().Uint32Value();
129
+ filters[i].id = current;
130
+ if( current == LZMA_FILTER_LZMA2 ) {
131
+ filters[i].options = &opt_lzma2;
132
+ } else {
133
+ filters[i].options = nullptr;
134
+ }
135
+ }
136
+
137
+ filters[filters_len] = { .id = LZMA_VLI_UNKNOWN, .options = nullptr };
138
+
139
+ lzma_ret ret;
140
+ switch(mode) {
141
+ case STREAM_DECODE: {
142
+ ret = lzma_stream_decoder(&this->_stream, UINT64_MAX, check);
143
+ break;
144
+ }
145
+ case STREAM_ENCODE: {
146
+ Napi::Value optsThreads = opts.Get("threads");
147
+ if (!optsThreads.IsNumber()) {
148
+ Napi::Error::New(env, "Threads must be an integer");
149
+ return;
150
+ }
151
+
152
+ #ifdef ENABLE_THREAD_SUPPORT
153
+ unsigned int threads = optsThreads.ToNumber().Uint32Value();
154
+ #pragma GCC diagnostic push
155
+ #pragma GCC diagnostic ignored "-Wmissing-field-initializers"
156
+
157
+ lzma_mt mt = {
158
+ .flags = 0,
159
+ .threads = threads,
160
+ .block_size = 0,
161
+ .timeout = 0,
162
+ .preset = preset,
163
+ .filters = filters,
164
+ .check = check,
165
+ };
166
+
167
+ #pragma GCC diagnostic pop
168
+
169
+ if( threads > 1 ) {
170
+ ret = lzma_stream_encoder_mt(&this->_stream, &mt);
171
+ } else {
172
+ ret = lzma_stream_encoder(&this->_stream, filters, check);
173
+ }
174
+ #else
175
+ ret = lzma_stream_encoder(&this->_stream, filters, check);
176
+ #endif
177
+ break;
178
+ }
179
+ default:
180
+ ret = LZMA_OPTIONS_ERROR;
181
+ }
182
+
183
+ if (ret != LZMA_OK) {
184
+ Napi::Error::New(env, "LZMA failure, returned " + std::to_string(ret));
185
+ return;
186
+ }
187
+
188
+ Napi::MemoryManagement::AdjustExternalMemory(env, sizeof(LZMA));
189
+ }
190
+
191
+ /**<
192
+ * \brief Do the encoding/decoding with async support
193
+ *
194
+ * Function prototype is (sync):
195
+ * .codeSync flushFlag, input_buffer, input_offset, output_buffer, output_offset, callback
196
+ * Function prototype is (async):
197
+ * .code flushFlag, input_buffer, input_offset, availInBefore, output_buffer, output_offset, callback
198
+ *
199
+ * Where:
200
+ * flushFlag: type: Uint32
201
+ * input_buffer: type: Buffer
202
+ * input_offset: type: Uint32
203
+ * availInBefore: type: Uint32
204
+ * output_buffer: type: Buffer
205
+ * output_offset: type: Uint32
206
+ * callback: type: Function
207
+ */
208
+ template<bool async>
209
+ Napi::Value LZMA::Code(const Napi::CallbackInfo &info) {
210
+ Napi::Env env = info.Env();
211
+
212
+ this->_wip = true;
213
+ this->Ref();
214
+
215
+ // Neat trick but that does the job :)
216
+ if (info.Length() != 6+(int)async) {
217
+ Napi::Error::New(env, "BUG?: LZMA::Code requires all these arguments: "
218
+ "flushFlag, input_buffer, input_offset, availInBefore, "
219
+ "output_buffer, output_offset, [callback]").ThrowAsJavaScriptException();
220
+ return env.Undefined();
221
+ }
222
+
223
+ if (!info[0].IsNumber()) {
224
+ Napi::Error::New(env, "flushFlag must be an integer");
225
+ return env.Undefined();
226
+ }
227
+ this->_action = static_cast<lzma_action>(info[0].ToNumber().Uint32Value());
228
+
229
+ // Evaluate parameters passed to us
230
+ const uint8_t *in;
231
+ uint8_t *out;
232
+ size_t in_off, in_len, out_off, out_len;
233
+
234
+ // If we do not have input buffer data
235
+ if (info[1].IsNull()) {
236
+ // just a flush
237
+ // uint8_t nada[1] = { 0 };
238
+ uint8_t nada = 0;
239
+ in = &nada;
240
+ in_len = 0;
241
+ in_off = 0;
242
+ } else {
243
+ // if (!node::Buffer::HasInstance(info[1])) {
244
+ if (!info[1].IsBuffer()) {
245
+ Napi::TypeError::New(env, "BUG?: LZMA::Code 'input_buffer' argument must be a Buffer").ThrowAsJavaScriptException();
246
+ return env.Undefined();
247
+ }
248
+
249
+ uint8_t *in_buf = info[1].As<Napi::Buffer<uint8_t>>().Data();
250
+ in_off = info[2].ToNumber().Uint32Value();
251
+ in_len = info[3].ToNumber().Uint32Value();
252
+ size_t in_max = info[1].As<Napi::Buffer<uint8_t>>().Length();
253
+
254
+ if(!node::Buffer::IsWithinBounds(in_off, in_len, in_max)) {
255
+ Napi::Error::New(env, "Offset out of bounds!").ThrowAsJavaScriptException();
256
+ return env.Undefined();
257
+ }
258
+ in = in_buf + in_off;
259
+ }
260
+
261
+ // Check if output buffer is also a Buffer
262
+ if( !info[4].IsBuffer() ) {
263
+ Napi::TypeError::New(env, "BUG?: LZMA::Code 'output_buffer' argument must be a Buffer").ThrowAsJavaScriptException();
264
+ return env.Undefined();
265
+ }
266
+
267
+ uint8_t *out_buf = info[4].As<Napi::Buffer<uint8_t>>().Data();
268
+ out_off = info[5].ToNumber().Uint32Value();
269
+ out_len = info[4].As<Napi::Buffer<uint8_t>>().Length() - out_off;
270
+ out = out_buf + out_off;
271
+
272
+ // Only if async mode is enabled shall we need a callback function
273
+ if(async) {
274
+ this->_callback = Napi::Persistent(info[6].As<Napi::Function>());
275
+ }
276
+
277
+ this->_stream.next_in = in;
278
+ this->_stream.avail_in = in_len;
279
+ this->_stream.next_out = out;
280
+ this->_stream.avail_out = out_len;
281
+
282
+ // do it synchronously
283
+ if(async) {
284
+ this->_worker = new LZMAWorker(env, this);
285
+ this->_worker->Queue();
286
+ } else {
287
+ Process(this);
288
+ return AfterSync(info, this);
289
+ }
290
+
291
+ // otherwise queue work, make sure we get our work done by first calling Process and then After
292
+ // napi_create_async_work(uv_default_loop(), &(this->_req), LZMA::Process, LZMA::After);
293
+ return env.Undefined();
294
+ }
295
+
296
+ void LZMA::Process(LZMA* obj) {
297
+ // the real work is done here :)
298
+ obj->_wip = true;
299
+ obj->_ret = lzma_code(&(obj->_stream), obj->_action);
300
+ }
301
+
302
+ void LZMA::After(Napi::Env env, LZMA* obj /*, int status */) {
303
+
304
+ Napi::Number ret_code = Napi::Number::New(env, obj->_ret);
305
+ Napi::Number avail_in = Napi::Number::New(env, obj->_stream.avail_in);
306
+ Napi::Number avail_out = Napi::Number::New(env, obj->_stream.avail_out);
307
+
308
+ obj->_wip = false;
309
+
310
+ obj->_callback.Call({ ret_code, avail_in, avail_out });
311
+
312
+ obj->Unref();
313
+
314
+ if(obj->_pending_close) {
315
+ obj->Close(env);
316
+ }
317
+ }
318
+
319
+ Napi::Value LZMA::AfterSync(const Napi::CallbackInfo &info, LZMA* obj) {
320
+ Napi::Env env = info.Env();
321
+
322
+ Napi::Number ret_code = Napi::Number::New(env, obj->_ret);
323
+ Napi::Number avail_in = Napi::Number::New(env, obj->_stream.avail_in);
324
+ Napi::Number avail_out = Napi::Number::New(env, obj->_stream.avail_out);
325
+ Napi::Array result = Napi::Array::New(env, 3);
326
+
327
+ uint32_t i = 0;
328
+ result[i++] = ret_code;
329
+ result[i++] = avail_in;
330
+ result[i++] = avail_out;
331
+
332
+ obj->_wip = false;
333
+
334
+ obj->Unref();
335
+ if(obj->_pending_close) {
336
+ obj->Close(info);
337
+ }
338
+
339
+ return result;
340
+ }
@@ -0,0 +1,91 @@
1
+ /**
2
+ * node-liblzma - Node.js bindings for liblzma
3
+ * Copyright (C) 2014-2015 Olivier Orabona <olivier.orabona@gmail.com>
4
+ *
5
+ * This program is free software: you can redistribute it and/or modify
6
+ * it under the terms of the GNU Lesser General Public License as published by
7
+ * the Free Software Foundation, either version 3 of the License, or
8
+ * (at your option) any later version.
9
+ *
10
+ * This program is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ * GNU General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU Lesser General Public License
16
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
17
+ **/
18
+
19
+ #ifndef BUILDING_NODE_EXTENSION
20
+ #define BUILDING_NODE_EXTENSION
21
+ #endif
22
+
23
+ #ifndef NODE_LIBLZMA_H
24
+ #define NODE_LIBLZMA_H
25
+
26
+ #include <lzma.h>
27
+ #include <napi.h>
28
+
29
+ #include <sstream>
30
+
31
+ constexpr unsigned int STREAM_ENCODE = 0;
32
+ constexpr unsigned int STREAM_DECODE = 1;
33
+ #ifdef ENABLE_THREAD_SUPPORT
34
+ constexpr bool HAS_THREADS_SUPPORT = true;
35
+ #else
36
+ constexpr bool HAS_THREADS_SUPPORT = false;
37
+ #endif
38
+
39
+ class LZMAWorker;
40
+ class LZMA : public Napi::ObjectWrap<LZMA> {
41
+ public:
42
+ static void Init(Napi::Env env, Napi::Object exports);
43
+
44
+ explicit LZMA(const Napi::CallbackInfo& info);
45
+ ~LZMA() = default;
46
+
47
+ Napi::Value Close(const Napi::CallbackInfo &info);
48
+ Napi::Value Close(const Napi::Env &env);
49
+
50
+ template<bool async>
51
+ Napi::Value Code(const Napi::CallbackInfo &info);
52
+
53
+ static void Process(LZMA* obj);
54
+ static void After(Napi::Env env, LZMA* obj /*, int status */);
55
+ static Napi::Value AfterSync(const Napi::CallbackInfo &info, LZMA* obj);
56
+
57
+ private:
58
+ lzma_stream _stream;
59
+ bool _wip;
60
+ bool _pending_close;
61
+
62
+ LZMAWorker* _worker;
63
+
64
+ lzma_action _action;
65
+ Napi::FunctionReference _callback;
66
+ lzma_ret _ret;
67
+ lzma_filter *filters;
68
+ };
69
+
70
+ class LZMAWorker : public Napi::AsyncWorker {
71
+ public:
72
+ LZMAWorker(Napi::Env env, LZMA* instance) : AsyncWorker(env), lzma(instance) { }
73
+ ~LZMAWorker() = default;
74
+
75
+ void Execute() {
76
+ LZMA::Process(this->lzma);
77
+ }
78
+ void OnOK() {
79
+ Napi::HandleScope scope(Env());
80
+ LZMA::After(Env(), this->lzma);
81
+ }
82
+ void OnError(const Napi::Error& e) {
83
+ Napi::HandleScope scope(Env());
84
+ LZMA::After(Env(), this->lzma);
85
+ }
86
+
87
+ private:
88
+ LZMA *lzma;
89
+ };
90
+
91
+ #endif // NODE_LIBLZMA_H
@@ -0,0 +1,344 @@
1
+ ###
2
+ * node-liblzma - Node.js bindings for liblzma
3
+ * Copyright (C) Olivier Orabona <olivier.orabona@gmail.com>
4
+ *
5
+ * This program is free software: you can redistribute it and/or modify
6
+ * it under the terms of the GNU Lesser General Public License as published by
7
+ * the Free Software Foundation, either version 3 of the License, or
8
+ * (at your option) any later version.
9
+ *
10
+ * This program is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ * GNU General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU Lesser General Public License
16
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
17
+ ###
18
+
19
+ path = require 'path'
20
+ binding_path = path.resolve path.join __dirname, '..'
21
+
22
+ liblzma = require('node-gyp-build')(binding_path)
23
+ util = require 'util'
24
+ assert = require 'assert'
25
+ os = require 'os'
26
+ { Transform } = require 'stream'
27
+
28
+ # Should not change over time... :)
29
+ maxThreads = os.cpus().length
30
+
31
+ class XzStream extends Transform
32
+ constructor: (mode, @_opts = {}, options) ->
33
+ super options
34
+
35
+ @_opts.check ?= exports.check.NONE
36
+ @_opts.preset ?= exports.preset.DEFAULT
37
+ @_opts.filters ?= [exports.filter.LZMA2]
38
+ @_opts.mode ?= exports.mode.NORMAL
39
+ @_opts.threads ?= 1
40
+
41
+ @_chunkSize = if @_opts.chunkSize then @_opts.chunkSize else liblzma.BUFSIZ
42
+
43
+ # By default no flush, since there is no LZMA_NO_SYNC, we stick to
44
+ # default LZMA_RUN (0)
45
+ @_flushFlag = @_opts.flushFlag or liblzma.LZMA_RUN
46
+
47
+ assert Array.isArray(@_opts.filters), "Filters need to be in an array!"
48
+
49
+ # Add default filter LZMA2 if none provided
50
+ if @_opts.filters.indexOf(exports.filter.LZMA2) is -1
51
+ @_opts.filters.push exports.filter.LZMA2
52
+
53
+ # Multithreading is only available for encoding, so if we are encoding, check
54
+ # opts threads value.
55
+ if mode is liblzma.STREAM_ENCODE
56
+ @_opts.threads = 1 unless liblzma.HAS_THREADS_SUPPORT
57
+
58
+ # By default set to maximum available processors
59
+ if @_opts.threads is 0
60
+ @_opts.threads = maxThreads
61
+
62
+ # Initialize engine
63
+ @lzma = new liblzma.LZMA mode, @_opts
64
+ @_closed = false
65
+ @_hadError = false
66
+ @_offset = 0
67
+ @_buffer = Buffer.alloc @_chunkSize
68
+
69
+ @on 'onerror', (errno) =>
70
+ @_hadError = true
71
+ error = new Error exports.messages[errno]
72
+ error.errno = errno
73
+ error.code = errno
74
+
75
+ @emit 'error', error
76
+
77
+ @once 'end', @close
78
+
79
+ flush: (kind, callback) ->
80
+ ws = @_writableState
81
+
82
+ if (typeof kind == 'function' or (typeof kind == 'undefined' && !callback))
83
+ [callback, kind] = [kind, liblzma.LZMA_SYNC_FLUSH]
84
+
85
+ if ws.ended
86
+ if callback
87
+ process.nextTick callback
88
+ else if ws.ending
89
+ if callback
90
+ @once 'end', callback
91
+ else if ws.needDrain
92
+ @once 'drain', =>
93
+ @flush callback
94
+ return
95
+ else
96
+ @_flushFlag = kind
97
+ @write Buffer.alloc(0), '', callback
98
+
99
+ return
100
+
101
+ close: (callback) ->
102
+ if callback
103
+ process.nextTick callback
104
+
105
+ # We will trigger this case with #xz and #unxz
106
+ return if @_closed
107
+
108
+ @lzma.close()
109
+ @_closed = true
110
+
111
+ process.nextTick =>
112
+ @emit 'close'
113
+ return
114
+
115
+ return
116
+
117
+ _transform: (chunk, encoding, callback) ->
118
+ flushFlag = undefined
119
+ ws = @_writableState
120
+ ending = ws.ending or ws.ended
121
+ last = ending and (not chunk or ws.length is chunk.length)
122
+ return callback(new Error("invalid input")) if chunk != null and !(chunk instanceof Buffer)
123
+ return callback(new Error("lzma binding closed")) if @_closed
124
+
125
+ # If it's the last chunk, or a final flush, we use the LZMA_FINISH flush flag.
126
+ # If it's explicitly flushing at some other time, then we use
127
+ # LZMA_SYNC_FLUSH.
128
+ if last
129
+ flushFlag = liblzma.LZMA_FINISH
130
+ else
131
+ flushFlag = @_flushFlag
132
+
133
+ # once we've flushed the last of the queue, stop flushing and
134
+ # go back to the normal behavior.
135
+ @_flushFlag = @_opts.flushFlag or liblzma.LZMA_RUN if chunk.length >= ws.length
136
+
137
+ @_processChunk chunk, flushFlag, callback
138
+ return
139
+
140
+ _flush: (callback) ->
141
+ @_transform Buffer.alloc(0), '', callback
142
+ return
143
+
144
+ _processChunk: (chunk, flushFlag, cb) ->
145
+ # If user setting async is set to true, then it will all depend on whether
146
+ # we can actually be async, or not. If user set explicitly async to false
147
+ # then whether we have a callback or not becomes irrelevant..
148
+ # TODO: Works in v0.11
149
+ #async = util.isFunction cb
150
+ # until then...
151
+ async = typeof cb == 'function'
152
+
153
+ # Sanity checks
154
+ assert !@_closed, "Stream closed!"
155
+
156
+ availInBefore = chunk && chunk.length
157
+ availOutBefore = @_chunkSize - @_offset
158
+ inOff = 0
159
+
160
+ # So far it looks like Zlib _processChunk in CoffeeScript. But to make C++
161
+ # code easier when it comes to emitting events and callback calling sync/async
162
+ # we handle error codes here. If anything wrong is returned, we emit event
163
+ # and return false in case we are synchronous.
164
+ callback = (errno, availInAfter, availOutAfter) =>
165
+ return if @_hadError
166
+
167
+ # if LZMA engine returned something else, we are running into trouble!
168
+ if errno isnt liblzma.LZMA_OK and errno isnt liblzma.LZMA_STREAM_END
169
+ @emit 'onerror', errno
170
+ return false
171
+
172
+ used = availOutBefore - availOutAfter
173
+ assert used >= 0, "More bytes after than before! Delta = #{used}"
174
+
175
+ if used > 0
176
+ out = @_buffer[ @_offset ... @_offset + used ]
177
+ @_offset += used
178
+ if async
179
+ @push out
180
+ else
181
+ buffers.push out
182
+ nread += used
183
+
184
+ # exhausted the output buffer, or used all the input create a new one.
185
+ if availOutAfter is 0 or @_offset >= @_chunkSize
186
+ availOutBefore = @_chunkSize
187
+ @_offset = 0
188
+ @_buffer = Buffer.alloc @_chunkSize
189
+
190
+ if availOutAfter is 0 or availInAfter > 0
191
+ inOff += (availInBefore - availInAfter)
192
+ availInBefore = availInAfter
193
+
194
+ return true unless async
195
+ @lzma.code flushFlag, chunk, inOff, availInBefore, @_buffer, @_offset, callback
196
+ return
197
+
198
+ return false unless async
199
+ cb()
200
+ return
201
+
202
+ unless async
203
+ # Doing it synchronously
204
+ buffers = []
205
+ nread = 0
206
+
207
+ error = null
208
+ @on 'error', (e) -> error = e
209
+
210
+ loop
211
+ res = @lzma.codeSync flushFlag, chunk, inOff, availInBefore, @_buffer, @_offset
212
+ break unless not @_hadError and callback res[0], res[1], res[2]
213
+
214
+ throw error if @_hadError
215
+ @close()
216
+
217
+ buf = Buffer.concat buffers, nread
218
+ return buf
219
+
220
+ @lzma.code flushFlag, chunk, inOff, availInBefore, @_buffer, @_offset, callback
221
+
222
+ return
223
+
224
+ class Xz extends XzStream
225
+ constructor: (lzma_options, options) ->
226
+ super liblzma.STREAM_ENCODE, lzma_options, options
227
+
228
+ class Unxz extends XzStream
229
+ constructor: (lzma_options, options) ->
230
+ super liblzma.STREAM_DECODE, lzma_options, options
231
+
232
+
233
+ exports.Xz = Xz
234
+ exports.Unxz = Unxz
235
+
236
+ exports.hasThreads = ->
237
+ liblzma.HAS_THREADS_SUPPORT
238
+
239
+ exports.messages = [
240
+ "Operation completed successfully"
241
+ "End of stream was reached"
242
+ "Input stream has no integrity check"
243
+ "Cannot calculate the integrity check"
244
+ "Integrity check type is not available"
245
+ "Cannot allocate memory"
246
+ "Memory usage limit was reached"
247
+ "File format not recognized"
248
+ "Invalid or unsupported options"
249
+ "Data is corrupt"
250
+ "No progress is possible"
251
+ "Programming error"
252
+ ]
253
+
254
+ exports.check = {
255
+ "NONE": liblzma.LZMA_CHECK_NONE
256
+ "CRC32": liblzma.LZMA_CHECK_CRC32
257
+ "CRC64": liblzma.LZMA_CHECK_CRC64
258
+ "SHA256": liblzma.LZMA_CHECK_SHA256
259
+ }
260
+
261
+ exports.preset = {
262
+ "DEFAULT": liblzma.LZMA_PRESET_DEFAULT
263
+ "EXTREME": liblzma.LZMA_PRESET_EXTREME
264
+ }
265
+
266
+ exports.flag = {
267
+ "TELL_NO_CHECK": liblzma.LZMA_TELL_NO_CHECK
268
+ "TELL_UNSUPPORTED_CHECK": liblzma.LZMA_TELL_UNSUPPORTED_CHECK
269
+ "TELL_ANY_CHECK": liblzma.LZMA_TELL_ANY_CHECK
270
+ "CONCATENATED": liblzma.LZMA_CONCATENATED
271
+ }
272
+
273
+ exports.filter = {
274
+ "LZMA2": liblzma.LZMA_FILTER_LZMA2
275
+ "X86": liblzma.LZMA_FILTER_X86
276
+ "POWERPC": liblzma.LZMA_FILTER_POWERPC
277
+ "IA64": liblzma.LZMA_FILTER_IA64
278
+ "ARM": liblzma.LZMA_FILTER_ARM
279
+ "ARMTHUMB": liblzma.LZMA_FILTER_ARMTHUMB
280
+ "SPARC": liblzma.LZMA_FILTER_SPARC
281
+ }
282
+
283
+ exports.mode = {
284
+ "FAST": liblzma.LZMA_MODE_FAST
285
+ "NORMAL": liblzma.LZMA_MODE_NORMAL
286
+ }
287
+
288
+ exports.createXz = (lzma_options, options) ->
289
+ new Xz(lzma_options, options)
290
+
291
+ exports.createUnxz = (lzma_options, options) ->
292
+ new Unxz(lzma_options, options)
293
+
294
+ exports.unxz = (buffer, opts, callback) ->
295
+ if typeof opts == 'function'
296
+ [callback, opts] = [opts, {}]
297
+
298
+ xzBuffer new Unxz(opts), buffer, callback
299
+
300
+ exports.unxzSync = (buffer, opts) ->
301
+ xzBufferSync new Unxz(opts), buffer
302
+
303
+ exports.xz = (buffer, opts, callback) ->
304
+ if typeof opts == 'function'
305
+ [callback, opts] = [opts, {}]
306
+ xzBuffer new Xz(opts), buffer, callback
307
+
308
+ exports.xzSync = (buffer, opts) ->
309
+ xzBufferSync new Xz(opts), buffer
310
+
311
+ xzBuffer = (engine, buffer, callback) ->
312
+ buffers = []
313
+ nread = 0
314
+ flow = ->
315
+ chunk = undefined
316
+ while null isnt (chunk = engine.read())
317
+ buffers.push chunk
318
+ nread += chunk.length
319
+ engine.once 'readable', flow
320
+ return
321
+ onEnd = ->
322
+ buf = Buffer.concat(buffers, nread)
323
+ buffers = []
324
+ callback null, buf
325
+ engine.close()
326
+ return
327
+ onError = (err) ->
328
+ engine.removeListener 'end', onEnd
329
+ engine.removeListener 'readable', flow
330
+ callback err
331
+ return
332
+ engine.on 'error', onError
333
+ engine.on 'end', onEnd
334
+ engine.end buffer
335
+ flow()
336
+ return
337
+
338
+ xzBufferSync = (engine, buffer) ->
339
+ buffer = Buffer.from(buffer) if typeof buffer == 'string'
340
+ throw new TypeError("Not a string or buffer") unless buffer instanceof Buffer
341
+
342
+ engine._processChunk buffer, liblzma.LZMA_FINISH
343
+
344
+ module.exports = exports