node-liblzma 1.1.4 → 1.1.7

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.
@@ -1,339 +0,0 @@
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
- ret = lzma_stream_encoder(&this->_stream, filters, check);
147
- break;
148
- }
149
-
150
- case STREAM_ENCODE_MT: {
151
- Napi::Value optsThreads = opts.Get("threads");
152
- if (!optsThreads.IsNumber()) {
153
- Napi::Error::New(env, "Threads must be an integer");
154
- return;
155
- }
156
-
157
- unsigned int threads = optsThreads.ToNumber().Uint32Value();
158
-
159
- #pragma GCC diagnostic push
160
- #pragma GCC diagnostic ignored "-Wmissing-field-initializers"
161
-
162
- lzma_mt mt = {
163
- .flags = 0,
164
- .threads = threads,
165
- .block_size = 0,
166
- .timeout = 0,
167
- .preset = preset,
168
- .filters = filters,
169
- .check = check,
170
- };
171
-
172
- #pragma GCC diagnostic pop
173
-
174
- ret = lzma_stream_encoder_mt(&this->_stream, &mt);
175
- break;
176
- }
177
- // #endif
178
- default:
179
- ret = LZMA_OPTIONS_ERROR;
180
- }
181
-
182
- if (ret != LZMA_OK) {
183
- Napi::Error::New(env, "LZMA failure, returned " + std::to_string(ret));
184
- return;
185
- }
186
-
187
- Napi::MemoryManagement::AdjustExternalMemory(env, sizeof(LZMA));
188
- }
189
-
190
- /**<
191
- * \brief Do the encoding/decoding with async support
192
- *
193
- * Function prototype is (sync):
194
- * .codeSync flushFlag, input_buffer, input_offset, output_buffer, output_offset, callback
195
- * Function prototype is (async):
196
- * .code flushFlag, input_buffer, input_offset, availInBefore, output_buffer, output_offset, callback
197
- *
198
- * Where:
199
- * flushFlag: type: Uint32
200
- * input_buffer: type: Buffer
201
- * input_offset: type: Uint32
202
- * availInBefore: type: Uint32
203
- * output_buffer: type: Buffer
204
- * output_offset: type: Uint32
205
- * callback: type: Function
206
- */
207
- template<bool async>
208
- Napi::Value LZMA::Code(const Napi::CallbackInfo &info) {
209
- Napi::Env env = info.Env();
210
-
211
- this->_wip = true;
212
- this->Ref();
213
-
214
- // Neat trick but that does the job :)
215
- if (info.Length() != 6+(int)async) {
216
- Napi::Error::New(env, "BUG?: LZMA::Code requires all these arguments: "
217
- "flushFlag, input_buffer, input_offset, availInBefore, "
218
- "output_buffer, output_offset, [callback]").ThrowAsJavaScriptException();
219
- return env.Undefined();
220
- }
221
-
222
- if (!info[0].IsNumber()) {
223
- Napi::Error::New(env, "flushFlag must be an integer");
224
- return env.Undefined();
225
- }
226
- this->_action = static_cast<lzma_action>(info[0].ToNumber().Uint32Value());
227
-
228
- // Evaluate parameters passed to us
229
- const uint8_t *in;
230
- uint8_t *out;
231
- size_t in_off, in_len, out_off, out_len;
232
-
233
- // If we do not have input buffer data
234
- if (info[1].IsNull()) {
235
- // just a flush
236
- // uint8_t nada[1] = { 0 };
237
- uint8_t nada = 0;
238
- in = &nada;
239
- in_len = 0;
240
- in_off = 0;
241
- } else {
242
- // if (!node::Buffer::HasInstance(info[1])) {
243
- if (!info[1].IsBuffer()) {
244
- Napi::TypeError::New(env, "BUG?: LZMA::Code 'input_buffer' argument must be a Buffer").ThrowAsJavaScriptException();
245
- return env.Undefined();
246
- }
247
-
248
- uint8_t *in_buf = info[1].As<Napi::Buffer<uint8_t>>().Data();
249
- in_off = info[2].ToNumber().Uint32Value();
250
- in_len = info[3].ToNumber().Uint32Value();
251
- size_t in_max = info[1].As<Napi::Buffer<uint8_t>>().Length();
252
-
253
- if(!node::Buffer::IsWithinBounds(in_off, in_len, in_max)) {
254
- Napi::Error::New(env, "Offset out of bounds!").ThrowAsJavaScriptException();
255
- return env.Undefined();
256
- }
257
- in = in_buf + in_off;
258
- }
259
-
260
- // Check if output buffer is also a Buffer
261
- if( !info[4].IsBuffer() ) {
262
- Napi::TypeError::New(env, "BUG?: LZMA::Code 'output_buffer' argument must be a Buffer").ThrowAsJavaScriptException();
263
- return env.Undefined();
264
- }
265
-
266
- uint8_t *out_buf = info[4].As<Napi::Buffer<uint8_t>>().Data();
267
- out_off = info[5].ToNumber().Uint32Value();
268
- out_len = info[4].As<Napi::Buffer<uint8_t>>().Length() - out_off;
269
- out = out_buf + out_off;
270
-
271
- // Only if async mode is enabled shall we need a callback function
272
- if(async) {
273
- this->_callback = Napi::Persistent(info[6].As<Napi::Function>());
274
- }
275
-
276
- this->_stream.next_in = in;
277
- this->_stream.avail_in = in_len;
278
- this->_stream.next_out = out;
279
- this->_stream.avail_out = out_len;
280
-
281
- // do it synchronously
282
- if(async) {
283
- this->_worker = new LZMAWorker(env, this);
284
- this->_worker->Queue();
285
- } else {
286
- Process(this);
287
- return AfterSync(info, this);
288
- }
289
-
290
- // otherwise queue work, make sure we get our work done by first calling Process and then After
291
- // napi_create_async_work(uv_default_loop(), &(this->_req), LZMA::Process, LZMA::After);
292
- return env.Undefined();
293
- }
294
-
295
- void LZMA::Process(LZMA* obj) {
296
- // the real work is done here :)
297
- obj->_wip = true;
298
- obj->_ret = lzma_code(&(obj->_stream), obj->_action);
299
- }
300
-
301
- void LZMA::After(Napi::Env env, LZMA* obj /*, int status */) {
302
-
303
- Napi::Number ret_code = Napi::Number::New(env, obj->_ret);
304
- Napi::Number avail_in = Napi::Number::New(env, obj->_stream.avail_in);
305
- Napi::Number avail_out = Napi::Number::New(env, obj->_stream.avail_out);
306
-
307
- obj->_wip = false;
308
-
309
- obj->_callback.Call({ ret_code, avail_in, avail_out });
310
-
311
- obj->Unref();
312
-
313
- if(obj->_pending_close) {
314
- obj->Close(env);
315
- }
316
- }
317
-
318
- Napi::Value LZMA::AfterSync(const Napi::CallbackInfo &info, LZMA* obj) {
319
- Napi::Env env = info.Env();
320
-
321
- Napi::Number ret_code = Napi::Number::New(env, obj->_ret);
322
- Napi::Number avail_in = Napi::Number::New(env, obj->_stream.avail_in);
323
- Napi::Number avail_out = Napi::Number::New(env, obj->_stream.avail_out);
324
- Napi::Array result = Napi::Array::New(env, 3);
325
-
326
- uint32_t i = 0;
327
- result[i++] = ret_code;
328
- result[i++] = avail_in;
329
- result[i++] = avail_out;
330
-
331
- obj->_wip = false;
332
-
333
- obj->Unref();
334
- if(obj->_pending_close) {
335
- obj->Close(info);
336
- }
337
-
338
- return result;
339
- }
@@ -1,87 +0,0 @@
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
- constexpr unsigned int STREAM_ENCODE_MT = 2;
34
-
35
- class LZMAWorker;
36
- class LZMA : public Napi::ObjectWrap<LZMA> {
37
- public:
38
- static void Init(Napi::Env env, Napi::Object exports);
39
-
40
- explicit LZMA(const Napi::CallbackInfo& info);
41
- ~LZMA() = default;
42
-
43
- Napi::Value Close(const Napi::CallbackInfo &info);
44
- Napi::Value Close(const Napi::Env &env);
45
-
46
- template<bool async>
47
- Napi::Value Code(const Napi::CallbackInfo &info);
48
-
49
- static void Process(LZMA* obj);
50
- static void After(Napi::Env env, LZMA* obj /*, int status */);
51
- static Napi::Value AfterSync(const Napi::CallbackInfo &info, LZMA* obj);
52
-
53
- private:
54
- lzma_stream _stream;
55
- bool _wip;
56
- bool _pending_close;
57
-
58
- LZMAWorker* _worker;
59
-
60
- lzma_action _action;
61
- Napi::FunctionReference _callback;
62
- lzma_ret _ret;
63
- lzma_filter *filters;
64
- };
65
-
66
- class LZMAWorker : public Napi::AsyncWorker {
67
- public:
68
- LZMAWorker(Napi::Env env, LZMA* instance) : AsyncWorker(env), lzma(instance) { }
69
- ~LZMAWorker() = default;
70
-
71
- void Execute() {
72
- LZMA::Process(this->lzma);
73
- }
74
- void OnOK() {
75
- Napi::HandleScope scope(Env());
76
- LZMA::After(Env(), this->lzma);
77
- }
78
- void OnError(const Napi::Error& e) {
79
- Napi::HandleScope scope(Env());
80
- LZMA::After(Env(), this->lzma);
81
- }
82
-
83
- private:
84
- LZMA *lzma;
85
- };
86
-
87
- #endif // NODE_LIBLZMA_H