forked from olcxjas-softworks/LarpixClient
Update gitignore (sorry)
This commit is contained in:
parent
a8f8c4d7ad
commit
cca8b02fea
6604 changed files with 1219661 additions and 4 deletions
65
electron/node_modules/lzma-native/src/filter-array.cpp
generated
vendored
Normal file
65
electron/node_modules/lzma-native/src/filter-array.cpp
generated
vendored
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
#include "liblzma-node.hpp"
|
||||
|
||||
namespace lzma {
|
||||
|
||||
FilterArray::FilterArray(Value val) {
|
||||
Env env = val.Env();
|
||||
HandleScope handle_scope(env);
|
||||
|
||||
if (!val.IsArray())
|
||||
throw TypeError::New(env, "Filter array expected");
|
||||
Array arr = val.As<Array>();
|
||||
|
||||
size_t len = arr.Length();
|
||||
|
||||
String id_ = String::New(env, "id");
|
||||
String options_ = String::New(env, "options");
|
||||
|
||||
for (size_t i = 0; i < len; ++i) {
|
||||
Value entry_v = arr[i];
|
||||
if (!entry_v.IsObject() || !entry_v.As<Object>().Has(id_))
|
||||
throw TypeError::New(env, "Filter array expected");
|
||||
Object entry = entry_v.As<Object>();
|
||||
|
||||
String id = Value(entry[id_]).ToString();
|
||||
Value opt_v = entry[options_];
|
||||
|
||||
lzma_filter f;
|
||||
f.id = FilterByName(id);
|
||||
f.options = nullptr;
|
||||
|
||||
bool has_options = !opt_v.IsUndefined() && !opt_v.IsNull();
|
||||
if (!has_options && (f.id != LZMA_FILTER_LZMA1 && f.id != LZMA_FILTER_LZMA2)) {
|
||||
filters.push_back(f);
|
||||
continue;
|
||||
}
|
||||
|
||||
Object opt = has_options ? opt_v.ToObject() : Object::New(env);
|
||||
|
||||
optbuf.push_back(options());
|
||||
union options& bopt = optbuf.back();
|
||||
|
||||
switch (f.id) {
|
||||
case LZMA_FILTER_DELTA:
|
||||
bopt.delta.type = (lzma_delta_type) GetIntegerProperty(opt, "type", LZMA_DELTA_TYPE_BYTE);
|
||||
bopt.delta.dist = GetIntegerProperty(opt, "dist", 1);
|
||||
f.options = &bopt.delta;
|
||||
break;
|
||||
case LZMA_FILTER_LZMA1:
|
||||
case LZMA_FILTER_LZMA2:
|
||||
bopt.lzma = parseOptionsLZMA(opt);
|
||||
f.options = &bopt.lzma;
|
||||
break;
|
||||
default:
|
||||
throw TypeError::New(env, "LZMA wrapper library understands .options only for DELTA and LZMA1, LZMA2 filters");
|
||||
}
|
||||
|
||||
filters.push_back(f);
|
||||
}
|
||||
|
||||
lzma_filter end;
|
||||
end.id = LZMA_VLI_UNKNOWN;
|
||||
filters.push_back(end);
|
||||
}
|
||||
|
||||
}
|
||||
596
electron/node_modules/lzma-native/src/index-parser.cpp
generated
vendored
Normal file
596
electron/node_modules/lzma-native/src/index-parser.cpp
generated
vendored
Normal file
|
|
@ -0,0 +1,596 @@
|
|||
// The contents from this file are from a proposed API that is not yet
|
||||
// implemented in upstream liblzma.
|
||||
|
||||
#include "index-parser.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
|
||||
#undef my_min
|
||||
#define my_min(x, y) ((x) < (y) ? (x) : (y))
|
||||
|
||||
namespace lzma {
|
||||
|
||||
void *
|
||||
lzma_alloc(size_t size, const lzma_allocator *allocator)
|
||||
{
|
||||
// Some malloc() variants return NULL if called with size == 0.
|
||||
if (size == 0)
|
||||
size = 1;
|
||||
|
||||
void *ptr;
|
||||
|
||||
if (allocator != NULL && allocator->alloc != NULL)
|
||||
ptr = allocator->alloc(allocator->opaque, 1, size);
|
||||
else
|
||||
ptr = malloc(size);
|
||||
|
||||
return ptr;
|
||||
}
|
||||
|
||||
void
|
||||
lzma_free(void *ptr, const lzma_allocator *allocator)
|
||||
{
|
||||
if (allocator != NULL && allocator->free != NULL)
|
||||
allocator->free(allocator->opaque, ptr);
|
||||
else
|
||||
free(ptr);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
enum lip_state {
|
||||
PARSE_INDEX_INITED,
|
||||
PARSE_INDEX_READ_FOOTER,
|
||||
PARSE_INDEX_READ_INDEX,
|
||||
PARSE_INDEX_READ_STREAM_HEADER
|
||||
};
|
||||
|
||||
struct lzma_index_parser_internal_s {
|
||||
/// Current state.
|
||||
lip_state state;
|
||||
|
||||
/// Current position in the file. We parse the file backwards so
|
||||
/// initialize it to point to the end of the file.
|
||||
int64_t pos;
|
||||
|
||||
/// The footer flags of the current XZ stream.
|
||||
lzma_stream_flags footer_flags;
|
||||
|
||||
/// All Indexes decoded so far.
|
||||
lzma_index *combined_index;
|
||||
|
||||
/// The Index currently being decoded.
|
||||
lzma_index *this_index;
|
||||
|
||||
/// Padding of the stream currently being decoded.
|
||||
lzma_vli stream_padding;
|
||||
|
||||
/// Size of the Index currently being decoded.
|
||||
lzma_vli index_size;
|
||||
|
||||
/// Keep track of how much memory is being used for Index decoding.
|
||||
uint64_t memused;
|
||||
|
||||
/// lzma_stream for the Index decoder.
|
||||
lzma_stream strm;
|
||||
|
||||
/// Keep the buffer coming as the last member to so all data that is
|
||||
/// ever actually used fits in a few cache lines.
|
||||
uint8_t buf[8192];
|
||||
};
|
||||
|
||||
static lzma_ret
|
||||
parse_indexes_read(lzma_index_parser_data *info,
|
||||
uint8_t *buf,
|
||||
size_t size,
|
||||
int64_t pos)
|
||||
{
|
||||
int64_t read = info->read_callback(info->opaque, buf, size, pos);
|
||||
|
||||
if (read < 0) {
|
||||
return LZMA_DATA_ERROR;
|
||||
}
|
||||
|
||||
if ((size_t)read != size) {
|
||||
info->message = "Unexpected end of file";
|
||||
return LZMA_DATA_ERROR;
|
||||
}
|
||||
|
||||
return LZMA_OK;
|
||||
}
|
||||
|
||||
extern lzma_ret
|
||||
my_lzma_parse_indexes_from_file(lzma_index_parser_data *info)
|
||||
{
|
||||
lzma_ret ret;
|
||||
lzma_index_parser_internal *internal = info->internal;
|
||||
info->message = NULL;
|
||||
|
||||
// Apparently, we are already done.
|
||||
if (info->index != NULL) {
|
||||
ret = LZMA_PROG_ERROR;
|
||||
goto error;
|
||||
}
|
||||
|
||||
// Passing file_size == SIZE_MAX can be used to safely clean up
|
||||
// everything when I/O failed asynchronously.
|
||||
if (info->file_size == SIZE_MAX) {
|
||||
ret = LZMA_OPTIONS_ERROR;
|
||||
goto error;
|
||||
}
|
||||
|
||||
if (info->memlimit == 0) {
|
||||
info->memlimit = UINT64_MAX;
|
||||
}
|
||||
|
||||
if (internal == NULL) {
|
||||
if (info->memlimit <= sizeof(lzma_index_parser_internal)) {
|
||||
// We don't really have a good figure for how much
|
||||
// memory may be necessary. Set memlimit to 0 to
|
||||
// indicate that something is obviously inacceptable.
|
||||
info->memlimit = 0;
|
||||
return LZMA_MEMLIMIT_ERROR;
|
||||
}
|
||||
|
||||
internal = (lzma_index_parser_internal*)lzma_alloc(sizeof(lzma_index_parser_internal),
|
||||
info->allocator);
|
||||
|
||||
if (internal == NULL)
|
||||
return LZMA_MEM_ERROR;
|
||||
|
||||
internal->state = PARSE_INDEX_INITED;
|
||||
internal->pos = info->file_size;
|
||||
internal->combined_index = NULL;
|
||||
internal->this_index = NULL;
|
||||
info->internal = internal;
|
||||
|
||||
lzma_stream strm_ = LZMA_STREAM_INIT;
|
||||
memcpy(&internal->strm, &strm_, sizeof(lzma_stream));
|
||||
internal->strm.allocator = info->allocator;
|
||||
}
|
||||
|
||||
// The header flags of the current stream are only ever used within a
|
||||
// call and don't need to go into the internals struct.
|
||||
lzma_stream_flags header_flags;
|
||||
|
||||
int i;
|
||||
uint64_t memlimit;
|
||||
|
||||
switch (internal->state) {
|
||||
case PARSE_INDEX_INITED:
|
||||
if (info->file_size <= 0) {
|
||||
// These strings are fixed so they can be translated by the xz
|
||||
// command line utility.
|
||||
info->message = "File is empty";
|
||||
return LZMA_DATA_ERROR;
|
||||
}
|
||||
|
||||
if (info->file_size < 2 * LZMA_STREAM_HEADER_SIZE) {
|
||||
info->message = "Too small to be a valid .xz file";
|
||||
return LZMA_DATA_ERROR;
|
||||
}
|
||||
|
||||
// Each loop iteration decodes one Index.
|
||||
do {
|
||||
// Check that there is enough data left to contain at least
|
||||
// the Stream Header and Stream Footer. This check cannot
|
||||
// fail in the first pass of this loop.
|
||||
if (internal->pos < 2 * LZMA_STREAM_HEADER_SIZE) {
|
||||
ret = LZMA_DATA_ERROR;
|
||||
goto error;
|
||||
}
|
||||
|
||||
internal->pos -= LZMA_STREAM_HEADER_SIZE;
|
||||
internal->stream_padding = 0;
|
||||
|
||||
// Locate the Stream Footer. There may be Stream Padding which
|
||||
// we must skip when reading backwards.
|
||||
while (true) {
|
||||
if (internal->pos < LZMA_STREAM_HEADER_SIZE) {
|
||||
ret = LZMA_DATA_ERROR;
|
||||
goto error;
|
||||
}
|
||||
|
||||
ret = parse_indexes_read(info,
|
||||
internal->buf,
|
||||
LZMA_STREAM_HEADER_SIZE,
|
||||
internal->pos);
|
||||
|
||||
if (ret != LZMA_OK)
|
||||
goto error;
|
||||
internal->state = PARSE_INDEX_READ_FOOTER;
|
||||
if (info->async) return LZMA_OK;
|
||||
case PARSE_INDEX_READ_FOOTER:
|
||||
|
||||
// Stream Padding is always a multiple of four bytes.
|
||||
i = 2;
|
||||
if (((uint32_t *)internal->buf)[i] != 0)
|
||||
break;
|
||||
|
||||
// To avoid calling the read callback for every four
|
||||
// bytes of Stream Padding, take advantage that we
|
||||
// read 12 bytes (LZMA_STREAM_HEADER_SIZE) already
|
||||
// and check them too before calling the read
|
||||
// callback again.
|
||||
do {
|
||||
internal->stream_padding += 4;
|
||||
internal->pos -= 4;
|
||||
--i;
|
||||
} while (i >= 0 && ((uint32_t *)internal->buf)[i] == 0);
|
||||
}
|
||||
|
||||
// Decode the Stream Footer.
|
||||
ret = lzma_stream_footer_decode(&internal->footer_flags,
|
||||
internal->buf);
|
||||
if (ret != LZMA_OK) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
// Check that the Stream Footer doesn't specify something
|
||||
// that we don't support. This can only happen if the xz
|
||||
// version is older than liblzma and liblzma supports
|
||||
// something new.
|
||||
//
|
||||
// It is enough to check Stream Footer. Stream Header must
|
||||
// match when it is compared against Stream Footer with
|
||||
// lzma_stream_flags_compare().
|
||||
if (internal->footer_flags.version != 0) {
|
||||
ret = LZMA_OPTIONS_ERROR;
|
||||
goto error;
|
||||
}
|
||||
|
||||
// Check that the size of the Index field looks sane.
|
||||
internal->index_size = internal->footer_flags.backward_size;
|
||||
if ((lzma_vli)(internal->pos) <
|
||||
internal->index_size +
|
||||
LZMA_STREAM_HEADER_SIZE) {
|
||||
ret = LZMA_DATA_ERROR;
|
||||
goto error;
|
||||
}
|
||||
|
||||
// Set pos to the beginning of the Index.
|
||||
internal->pos -= internal->index_size;
|
||||
|
||||
// See how much memory we can use for decoding this Index.
|
||||
memlimit = info->memlimit;
|
||||
internal->memused = sizeof(lzma_index_parser_internal);
|
||||
if (internal->combined_index != NULL) {
|
||||
internal->memused = lzma_index_memused(
|
||||
internal->combined_index);
|
||||
assert(internal->memused <= memlimit);
|
||||
|
||||
memlimit -= internal->memused;
|
||||
}
|
||||
|
||||
// Decode the Index.
|
||||
ret = lzma_index_decoder(&internal->strm,
|
||||
&internal->this_index,
|
||||
memlimit);
|
||||
if (ret != LZMA_OK) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
do {
|
||||
// Don't give the decoder more input than the
|
||||
// Index size.
|
||||
internal->strm.avail_in = my_min(sizeof(internal->buf),
|
||||
internal->index_size);
|
||||
|
||||
ret = parse_indexes_read(info,
|
||||
internal->buf,
|
||||
internal->strm.avail_in,
|
||||
internal->pos);
|
||||
|
||||
if (ret != LZMA_OK)
|
||||
goto error;
|
||||
internal->state = PARSE_INDEX_READ_INDEX;
|
||||
if (info->async) return LZMA_OK;
|
||||
case PARSE_INDEX_READ_INDEX:
|
||||
|
||||
internal->pos += internal->strm.avail_in;
|
||||
internal->index_size -= internal->strm.avail_in;
|
||||
|
||||
internal->strm.next_in = internal->buf;
|
||||
ret = lzma_code(&internal->strm, LZMA_RUN);
|
||||
|
||||
} while (ret == LZMA_OK);
|
||||
|
||||
// If the decoding seems to be successful, check also that
|
||||
// the Index decoder consumed as much input as indicated
|
||||
// by the Backward Size field.
|
||||
if (ret == LZMA_STREAM_END && (
|
||||
internal->index_size != 0 ||
|
||||
internal->strm.avail_in != 0)) {
|
||||
ret = LZMA_DATA_ERROR;
|
||||
}
|
||||
|
||||
if (ret != LZMA_STREAM_END) {
|
||||
// LZMA_BUFFER_ERROR means that the Index decoder
|
||||
// would have liked more input than what the Index
|
||||
// size should be according to Stream Footer.
|
||||
// The message for LZMA_DATA_ERROR makes more
|
||||
// sense in that case.
|
||||
if (ret == LZMA_BUF_ERROR)
|
||||
ret = LZMA_DATA_ERROR;
|
||||
|
||||
// If the error was too low memory usage limit,
|
||||
// indicate also how much memory would have been needed.
|
||||
if (ret == LZMA_MEMLIMIT_ERROR) {
|
||||
uint64_t needed = lzma_memusage(
|
||||
&internal->strm);
|
||||
if (UINT64_MAX - needed < internal->memused)
|
||||
needed = UINT64_MAX;
|
||||
else
|
||||
needed += internal->memused;
|
||||
|
||||
info->memlimit = needed;
|
||||
}
|
||||
|
||||
goto error;
|
||||
}
|
||||
|
||||
// Decode the Stream Header and check that its Stream Flags
|
||||
// match the Stream Footer.
|
||||
internal->pos -= internal->footer_flags.backward_size;
|
||||
internal->pos -= LZMA_STREAM_HEADER_SIZE;
|
||||
if ((lzma_vli)(internal->pos) <
|
||||
lzma_index_total_size(internal->this_index)) {
|
||||
ret = LZMA_DATA_ERROR;
|
||||
goto error;
|
||||
}
|
||||
|
||||
internal->pos -= lzma_index_total_size(internal->this_index);
|
||||
|
||||
ret = parse_indexes_read(info,
|
||||
internal->buf,
|
||||
LZMA_STREAM_HEADER_SIZE,
|
||||
internal->pos);
|
||||
|
||||
if (ret != LZMA_OK)
|
||||
goto error;
|
||||
|
||||
internal->state = PARSE_INDEX_READ_STREAM_HEADER;
|
||||
if (info->async) return LZMA_OK;
|
||||
case PARSE_INDEX_READ_STREAM_HEADER:
|
||||
|
||||
ret = lzma_stream_header_decode(&header_flags, internal->buf);
|
||||
if (ret != LZMA_OK) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
ret = lzma_stream_flags_compare(&header_flags,
|
||||
&internal->footer_flags);
|
||||
if (ret != LZMA_OK) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
// Store the decoded Stream Flags into this_index. This is
|
||||
// needed so that we can print which Check is used in each
|
||||
// Stream.
|
||||
ret = lzma_index_stream_flags(internal->this_index,
|
||||
&internal->footer_flags);
|
||||
assert(ret == LZMA_OK);
|
||||
|
||||
// Store also the size of the Stream Padding field. It is
|
||||
// needed to show the offsets of the Streams correctly.
|
||||
ret = lzma_index_stream_padding(internal->this_index,
|
||||
internal->stream_padding);
|
||||
assert(ret == LZMA_OK);
|
||||
|
||||
if (internal->combined_index != NULL) {
|
||||
// Append the earlier decoded Indexes
|
||||
// after this_index.
|
||||
ret = lzma_index_cat(
|
||||
internal->this_index,
|
||||
internal->combined_index,
|
||||
info->allocator);
|
||||
if (ret != LZMA_OK) {
|
||||
goto error;
|
||||
}
|
||||
}
|
||||
|
||||
internal->combined_index = internal->this_index;
|
||||
internal->this_index = NULL;
|
||||
|
||||
info->stream_padding += internal->stream_padding;
|
||||
|
||||
} while (internal->pos > 0);
|
||||
|
||||
lzma_end(&internal->strm);
|
||||
|
||||
// All OK. Make combined_index available to the caller.
|
||||
info->index = internal->combined_index;
|
||||
|
||||
lzma_free(internal, info->allocator);
|
||||
info->internal = NULL;
|
||||
return LZMA_STREAM_END;
|
||||
} // end switch(internal->state)
|
||||
|
||||
error:
|
||||
// Something went wrong, free the allocated memory.
|
||||
if (internal) {
|
||||
lzma_end(&internal->strm);
|
||||
lzma_index_end(internal->combined_index, info->allocator);
|
||||
lzma_index_end(internal->this_index, info->allocator);
|
||||
lzma_free(internal, info->allocator);
|
||||
}
|
||||
|
||||
info->internal = NULL;
|
||||
|
||||
// Doing this will prevent people from calling lzma_parse_indexes_from_file()
|
||||
// again without re-initializing.
|
||||
info->file_size = SIZE_MAX;
|
||||
return ret;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#include "liblzma-node.hpp"
|
||||
|
||||
namespace lzma {
|
||||
|
||||
void IndexParser::InitializeExports(Object exports) {
|
||||
exports["IndexParser"] = DefineClass(exports.Env(), "IndexParser", {
|
||||
InstanceMethod("init", &IndexParser::Init),
|
||||
InstanceMethod("feed", &IndexParser::Feed),
|
||||
InstanceMethod("parse", &IndexParser::Parse),
|
||||
});
|
||||
}
|
||||
|
||||
namespace {
|
||||
extern "C" int64_t LZMA_API_CALL
|
||||
read_cb(void* opaque, uint8_t* buf, size_t count, int64_t offset) {
|
||||
IndexParser* p = static_cast<IndexParser*>(opaque);
|
||||
return p->readCallback(opaque, buf, count, offset);
|
||||
}
|
||||
|
||||
extern "C" void* LZMA_API_CALL
|
||||
alloc_for_lzma_index(void *opaque, size_t nmemb, size_t size) {
|
||||
IndexParser* p = static_cast<IndexParser*>(opaque);
|
||||
size_t nBytes = nmemb * size + sizeof(size_t);
|
||||
|
||||
size_t* result = static_cast<size_t*>(::malloc(nBytes));
|
||||
if (!result)
|
||||
return result;
|
||||
|
||||
*result = nBytes;
|
||||
MemoryManagement::AdjustExternalMemory(p->Env(), static_cast<int64_t>(nBytes));
|
||||
return static_cast<void*>(result + 1);
|
||||
}
|
||||
|
||||
extern "C" void LZMA_API_CALL
|
||||
free_for_lzma_index(void *opaque, void *ptr) {
|
||||
IndexParser* p = static_cast<IndexParser*>(opaque);
|
||||
if (!ptr)
|
||||
return;
|
||||
|
||||
size_t* orig = static_cast<size_t*>(ptr) - 1;
|
||||
|
||||
MemoryManagement::AdjustExternalMemory(p->Env(), -static_cast<int64_t>(*orig));
|
||||
return ::free(static_cast<void*>(orig));
|
||||
}
|
||||
}
|
||||
|
||||
int64_t IndexParser::readCallback(void* opaque, uint8_t* buf, size_t count, int64_t offset) {
|
||||
currentReadBuffer = buf;
|
||||
currentReadSize = count;
|
||||
|
||||
napi_value argv[2] = {
|
||||
Uint64ToNumberMaxNull(Env(), count),
|
||||
Uint64ToNumberMaxNull(Env(), offset)
|
||||
};
|
||||
|
||||
Function read_cb = Napi::Value(Value()["read_cb"]).As<Function>();
|
||||
Napi::Value ret = read_cb.Call(Value(), 2, argv);
|
||||
|
||||
if (currentReadBuffer) {
|
||||
info.async = true;
|
||||
return count;
|
||||
} else {
|
||||
// .feed() has been alreay been called synchronously
|
||||
info.async = false;
|
||||
return NumberToUint64ClampNullMax(ret);
|
||||
}
|
||||
}
|
||||
|
||||
IndexParser::IndexParser(const CallbackInfo& args)
|
||||
: ObjectWrap(args),
|
||||
isCurrentlyInParseCall(false) {
|
||||
lzma_index_parser_data info_ = LZMA_INDEX_PARSER_DATA_INIT;
|
||||
info = info_;
|
||||
|
||||
allocator.alloc = alloc_for_lzma_index;
|
||||
allocator.free = free_for_lzma_index;
|
||||
allocator.opaque = static_cast<void*>(this);
|
||||
|
||||
info.read_callback = read_cb;
|
||||
info.opaque = static_cast<void*>(this);
|
||||
info.allocator = &allocator;
|
||||
}
|
||||
|
||||
void IndexParser::Init(const CallbackInfo& args) {
|
||||
info.file_size = NumberToUint64ClampNullMax(args[0]);
|
||||
info.memlimit = NumberToUint64ClampNullMax(args[1]);
|
||||
}
|
||||
|
||||
Object IndexParser::getObject() const {
|
||||
Napi::Env env = Env();
|
||||
Object obj = Object::New(env);
|
||||
|
||||
obj["streamPadding"] = Uint64ToNumberMaxNull(env, info.stream_padding);
|
||||
obj["memlimit"] = Uint64ToNumberMaxNull(env, info.memlimit);
|
||||
obj["streams"] = Uint64ToNumberMaxNull(env, lzma_index_stream_count(info.index));
|
||||
obj["blocks"] = Uint64ToNumberMaxNull(env, lzma_index_block_count(info.index));
|
||||
obj["fileSize"] = Uint64ToNumberMaxNull(env, lzma_index_file_size(info.index));
|
||||
obj["uncompressedSize"] = Uint64ToNumberMaxNull(env, lzma_index_uncompressed_size(info.index));
|
||||
obj["checks"] = Uint64ToNumberMaxNull(env, lzma_index_checks(info.index));
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
Value IndexParser::Parse(const CallbackInfo& args) {
|
||||
if (isCurrentlyInParseCall)
|
||||
throw Error::New(Env(), "Cannot call IndexParser::Parse recursively");
|
||||
|
||||
struct RecursionGuard {
|
||||
explicit RecursionGuard(IndexParser* p) : p(p) {
|
||||
p->isCurrentlyInParseCall = true;
|
||||
}
|
||||
~RecursionGuard() {
|
||||
p->isCurrentlyInParseCall = false;
|
||||
}
|
||||
IndexParser* p;
|
||||
};
|
||||
|
||||
lzma_ret ret;
|
||||
{
|
||||
RecursionGuard guard(this);
|
||||
ret = my_lzma_parse_indexes_from_file(&info);
|
||||
}
|
||||
|
||||
if (ret == LZMA_OK) {
|
||||
return Boolean::New(Env(), true);
|
||||
} else if (ret == LZMA_STREAM_END) {
|
||||
return getObject();
|
||||
}
|
||||
|
||||
Error error = lzmaRetError(Env(), ret);
|
||||
if (info.message) {
|
||||
error.Value()["message"] = String::New(Env(), info.message);
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
|
||||
Value IndexParser::Feed(const CallbackInfo& info) {
|
||||
Napi::Value value_v = info[0];
|
||||
if (!value_v.IsTypedArray())
|
||||
throw TypeError::New(Env(), "Expected Buffer as input");
|
||||
TypedArray value = value_v.As<TypedArray>();
|
||||
|
||||
if (currentReadBuffer == nullptr)
|
||||
throw Error::New(Env(), "No input data was expected");
|
||||
size_t length = value.ByteLength();
|
||||
|
||||
if (length > currentReadSize)
|
||||
length = currentReadSize;
|
||||
|
||||
memcpy(currentReadBuffer,
|
||||
static_cast<const uint8_t*>(value.ArrayBuffer().Data()) + value.ByteOffset(),
|
||||
length);
|
||||
currentReadBuffer = nullptr;
|
||||
|
||||
return Uint64ToNumberMaxNull(Env(), length);
|
||||
}
|
||||
|
||||
IndexParser::~IndexParser() {
|
||||
assert(!isCurrentlyInParseCall);
|
||||
info.file_size = SIZE_MAX;
|
||||
lzma_index_end(info.index, &allocator);
|
||||
info.index = nullptr;
|
||||
info.read_callback = nullptr;
|
||||
lzma_ret ret = my_lzma_parse_indexes_from_file(&info);
|
||||
assert(ret == LZMA_OPTIONS_ERROR);
|
||||
}
|
||||
|
||||
}
|
||||
208
electron/node_modules/lzma-native/src/index-parser.h
generated
vendored
Normal file
208
electron/node_modules/lzma-native/src/index-parser.h
generated
vendored
Normal file
|
|
@ -0,0 +1,208 @@
|
|||
// The contents from this file are from a proposed API that is not yet
|
||||
// implemented in upstream liblzma.
|
||||
|
||||
#ifndef INDEX_PARSER_H
|
||||
#define INDEX_PARSER_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <lzma.h>
|
||||
|
||||
namespace lzma {
|
||||
|
||||
/**
|
||||
* \brief Internal data structure
|
||||
*
|
||||
* The contents of this structure is not visible outside the library.
|
||||
*/
|
||||
typedef struct lzma_index_parser_internal_s lzma_index_parser_internal;
|
||||
|
||||
/**
|
||||
* \brief Reading the indexes of an .xz file
|
||||
*
|
||||
* The lzma_index_parser_data data structure is passed to
|
||||
* lzma_parse_indexes_from_file(), which can be used to retrieve the index
|
||||
* information for a given .xz file.
|
||||
*
|
||||
* It should be initialized with LZMA_INDEX_PARSER_DATA_INIT,
|
||||
* and, minimally, the file_size and read_callback() members need to be set.
|
||||
*
|
||||
* The allocation of internals happens transparently upon usage and does not
|
||||
* need to be taken care of.
|
||||
* In the case of an error, lzma_parse_indexes_from_file() performs all
|
||||
* necessary cleanup.
|
||||
* In the case of success, the index member will be set and needs to be
|
||||
* freed using lzma_index_end after the caller is done with it. If a custom
|
||||
* allocator was set on this struct, it needs to be used for freeing the
|
||||
* resulting index, too.
|
||||
*
|
||||
* Reading the data from the underlying file may happen synchronously or
|
||||
* asynchronously, see the description of the read_callback().
|
||||
*/
|
||||
typedef struct {
|
||||
/**
|
||||
* \brief Combined Index of all Streams in the file
|
||||
*
|
||||
* This will be set to an lzma_index * when parsing the file was
|
||||
* successful, as indicated by a LZMA_STREAM_END return status.
|
||||
*/
|
||||
lzma_index *index;
|
||||
|
||||
/**
|
||||
* \brief Total amount of Stream Padding
|
||||
*
|
||||
* This will be set when the file was successfully read.
|
||||
*/
|
||||
size_t stream_padding;
|
||||
|
||||
/**
|
||||
* \brief Callback for reading data from the input file
|
||||
*
|
||||
* This member needs to be set to a function that provides a slice of
|
||||
* the input file.
|
||||
*
|
||||
* The opaque pointer will have the same value as the opaque pointer
|
||||
* set on this struct.
|
||||
*
|
||||
* When being invoked, it should read count bytes from the underlying
|
||||
* file, starting at the specified offset, into buf.
|
||||
* The return value may be -1, in which case
|
||||
* lzma_parse_indexes_from_file() will return with LZMA_DATA_ERROR.
|
||||
* Otherwise, the number of read bytes should be returned. If this is
|
||||
* not the number of requested bytes, it will be assumed that the file
|
||||
* was truncated, and lzma_parse_indexes_from_file() will fail with
|
||||
* LZMA_DATA_ERROR.
|
||||
*
|
||||
* It is possible to perform the underlying I/O operations in an
|
||||
* asynchronous manner. To do so, set the async flag on this struct
|
||||
* to true. After read_callback() is invoked,
|
||||
* lzma_parse_indexes_from_file() will return immediately with
|
||||
* LZMA_OK (unless the read_callback() return value indicates failure),
|
||||
* and you are expected to call lzma_parse_indexes_from_file() with
|
||||
* the same struct as soon as the buffer has been filled.
|
||||
*
|
||||
* If asynchronous reading is used and the underlying read operation
|
||||
* fails, you should set file_size to SIZE_MAX and call
|
||||
* lzma_parse_indexes_from_file() to trigger an error clean up all
|
||||
* remaining internal state.
|
||||
*
|
||||
* You should not perform any operations on this structure until
|
||||
* the data has been read in any case.
|
||||
*
|
||||
* This function is modelled after pread(2), which is a available on
|
||||
* some platforms and can be easily wrapped to be used here.
|
||||
*/
|
||||
int64_t (LZMA_API_CALL *read_callback)(void *opaque,
|
||||
uint8_t *buf,
|
||||
size_t count,
|
||||
int64_t offset);
|
||||
|
||||
/// Opaque pointer that is passed to read_callback.
|
||||
void *opaque;
|
||||
|
||||
/// Whether to return after calling read_callback and wait for
|
||||
/// another call. Defaults to synchronous operations.
|
||||
lzma_bool async;
|
||||
|
||||
/** \brief Callback for reading data from the input file
|
||||
*
|
||||
* This needs to be set to the size of the input file before all
|
||||
* other operations. If this is set to SIZE_MAX, the parser will
|
||||
* fail with LZMA_OPTIONS_ERROR. This can be used to clean up
|
||||
* after a failed asynchronous read_callback().
|
||||
*
|
||||
* On error, this will be set to SIZE_MAX.
|
||||
*/
|
||||
size_t file_size;
|
||||
|
||||
/** \brief Memory limit for decoding the indexes.
|
||||
*
|
||||
* Set a memory limit for decoding. Default to UINT64_MAX for no limit.
|
||||
* If this is set too low to allocate the internal data structure
|
||||
* that is minimally required for parsing, this will be set to 0.
|
||||
* If this is set too low to parse the underlying .xz file,
|
||||
* this will be set to the amount of memory that would have
|
||||
* been necessary for parsing the file.
|
||||
*/
|
||||
uint64_t memlimit;
|
||||
|
||||
/// Message that may be set when additional information is available
|
||||
/// on error.
|
||||
const char *message;
|
||||
|
||||
/**
|
||||
* \brief Custom memory allocation functions
|
||||
*
|
||||
* In most cases this is NULL which makes liblzma use
|
||||
* the standard malloc() and free().
|
||||
*/
|
||||
const lzma_allocator *allocator;
|
||||
|
||||
/**
|
||||
* \brief Data which is internal to the index parser.
|
||||
*
|
||||
* Do not touch. You can check whether this is NULL to see if this
|
||||
* structure currently holds external resources, not counting the
|
||||
* possible index member that is set on success.
|
||||
*/
|
||||
lzma_index_parser_internal* internal;
|
||||
|
||||
/*
|
||||
* Reserved space to allow possible future extensions without
|
||||
* breaking the ABI. Excluding the initialization of this structure,
|
||||
* you should not touch these, because the names of these variables
|
||||
* may change.
|
||||
*/
|
||||
void *reserved_ptr1;
|
||||
void *reserved_ptr2;
|
||||
void *reserved_ptr3;
|
||||
void *reserved_ptr4;
|
||||
uint64_t reserved_int1;
|
||||
uint64_t reserved_int2;
|
||||
size_t reserved_int3;
|
||||
size_t reserved_int4;
|
||||
lzma_reserved_enum reserved_enum1;
|
||||
lzma_reserved_enum reserved_enum2;
|
||||
} lzma_index_parser_data;
|
||||
|
||||
/**
|
||||
* \brief Initialization for lzma_index_parser_data
|
||||
*
|
||||
* When you declare an instance of lzma_index_parser_data, you should
|
||||
* immediately initialize it to this value:
|
||||
*
|
||||
* lzma_index_parser_data strm = LZMA_INDEX_PARSER_DATA_INIT;
|
||||
*
|
||||
* Anything which applies for LZMA_STREAM_INIT applies here, too.
|
||||
*/
|
||||
#define LZMA_INDEX_PARSER_DATA_INIT \
|
||||
{ NULL, 0, NULL, NULL, 0, 0, 0, NULL, NULL, NULL, \
|
||||
NULL, NULL, NULL, NULL, 0, 0, 0, 0, \
|
||||
LZMA_RESERVED_ENUM, LZMA_RESERVED_ENUM }
|
||||
|
||||
/** \brief Parse the Index(es) from the given .xz file
|
||||
*
|
||||
* Read metadata from the underlying file.
|
||||
* The info pointer should refer to a lzma_index_parser_data struct that
|
||||
* has been initialized using LZMA_INDEX_PARSER_DATA_INIT.
|
||||
*
|
||||
* This will call info->read_callback() multiple times to read parts of the
|
||||
* underlying .xz file and, upon success, fill info->index with an
|
||||
* lzma_index pointer that contains metadata for the whole file, accumulated
|
||||
* across multiple streams.
|
||||
*
|
||||
* \param info Pointer to a lzma_index_parser_data structure.
|
||||
*
|
||||
* \return On success, LZMA_STREAM_END is returned.
|
||||
* On error, another value is returned, and info->message may
|
||||
* be set to provide additional information.
|
||||
* If info->async is set, LZMA_OK may be returned to indicate
|
||||
* that another call to lzma_parse_indexes_from_file() should be
|
||||
* performed after the data has been read.
|
||||
*/
|
||||
extern lzma_ret
|
||||
my_lzma_parse_indexes_from_file(lzma_index_parser_data *info);
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
84
electron/node_modules/lzma-native/src/liblzma-functions.cpp
generated
vendored
Normal file
84
electron/node_modules/lzma-native/src/liblzma-functions.cpp
generated
vendored
Normal file
|
|
@ -0,0 +1,84 @@
|
|||
#include "liblzma-node.hpp"
|
||||
|
||||
namespace lzma {
|
||||
|
||||
Value lzmaVersionNumber(const CallbackInfo& info) {
|
||||
return Number::New(info.Env(), lzma_version_number());
|
||||
}
|
||||
|
||||
Value lzmaVersionString(const CallbackInfo& info) {
|
||||
return String::New(info.Env(), lzma_version_string());
|
||||
}
|
||||
|
||||
Value lzmaCheckIsSupported(const CallbackInfo& info) {
|
||||
lzma_check arg = (lzma_check) info[0].ToNumber().Int64Value();
|
||||
|
||||
return Boolean::New(info.Env(), lzma_check_is_supported(arg));
|
||||
}
|
||||
|
||||
Value lzmaCheckSize(const CallbackInfo& info) {
|
||||
lzma_check arg = (lzma_check) info[0].ToNumber().Int64Value();
|
||||
|
||||
return Number::New(info.Env(), lzma_check_size(arg));
|
||||
}
|
||||
|
||||
Value lzmaFilterEncoderIsSupported(const CallbackInfo& info) {
|
||||
uint64_t arg = FilterByName(info[0]);
|
||||
|
||||
return Boolean::New(info.Env(), lzma_filter_encoder_is_supported(arg));
|
||||
}
|
||||
|
||||
Value lzmaFilterDecoderIsSupported(const CallbackInfo& info) {
|
||||
uint64_t arg = FilterByName(info[0]);
|
||||
|
||||
return Boolean::New(info.Env(), lzma_filter_decoder_is_supported(arg));
|
||||
}
|
||||
|
||||
Value lzmaMfIsSupported(const CallbackInfo& info) {
|
||||
lzma_match_finder arg = (lzma_match_finder) info[0].ToNumber().Int64Value();
|
||||
|
||||
return Boolean::New(info.Env(), lzma_mf_is_supported(arg));
|
||||
}
|
||||
|
||||
Value lzmaModeIsSupported(const CallbackInfo& info) {
|
||||
lzma_mode arg = (lzma_mode) info[0].ToNumber().Int64Value();
|
||||
|
||||
return Boolean::New(info.Env(), lzma_mode_is_supported(arg));
|
||||
}
|
||||
|
||||
Value lzmaEasyEncoderMemusage(const CallbackInfo& info) {
|
||||
int64_t arg = info[0].ToNumber();
|
||||
|
||||
return Uint64ToNumberMaxNull(info.Env(), lzma_easy_encoder_memusage(arg));
|
||||
}
|
||||
|
||||
Value lzmaEasyDecoderMemusage(const CallbackInfo& info) {
|
||||
int64_t arg = info[0].ToNumber();
|
||||
|
||||
return Uint64ToNumberMaxNull(info.Env(), lzma_easy_decoder_memusage(arg));
|
||||
}
|
||||
|
||||
Value lzmaCRC32(const CallbackInfo& info) {
|
||||
int64_t arg = info[1].ToNumber();
|
||||
|
||||
std::vector<uint8_t> data;
|
||||
|
||||
if (!readBufferFromObj(info[0], &data))
|
||||
throw TypeError::New(info.Env(), "CRC32 expects Buffer as input");
|
||||
|
||||
return Number::New(info.Env(), lzma_crc32(data.data(), data.size(), arg));
|
||||
}
|
||||
|
||||
Value lzmaRawEncoderMemusage(const CallbackInfo& info) {
|
||||
const FilterArray filters(info[0]);
|
||||
|
||||
return Uint64ToNumberMaxNull(info.Env(), lzma_raw_encoder_memusage(filters.array()));
|
||||
}
|
||||
|
||||
Value lzmaRawDecoderMemusage(const CallbackInfo& info) {
|
||||
const FilterArray filters(info[0]);
|
||||
|
||||
return Uint64ToNumberMaxNull(info.Env(), lzma_raw_decoder_memusage(filters.array()));
|
||||
}
|
||||
|
||||
}
|
||||
258
electron/node_modules/lzma-native/src/liblzma-node.hpp
generated
vendored
Normal file
258
electron/node_modules/lzma-native/src/liblzma-node.hpp
generated
vendored
Normal file
|
|
@ -0,0 +1,258 @@
|
|||
#ifndef BUILDING_NODE_EXTENSION
|
||||
#define BUILDING_NODE_EXTENSION
|
||||
#endif
|
||||
|
||||
#ifndef LIBLZMA_NODE_HPP
|
||||
#define LIBLZMA_NODE_HPP
|
||||
|
||||
#include <napi.h>
|
||||
|
||||
#include <lzma.h>
|
||||
#include "index-parser.h"
|
||||
|
||||
#include <vector>
|
||||
#include <list>
|
||||
#include <set>
|
||||
#include <queue>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <atomic>
|
||||
#include <mutex>
|
||||
#include <memory>
|
||||
|
||||
namespace lzma {
|
||||
using namespace Napi;
|
||||
|
||||
/* util */
|
||||
/**
|
||||
* Return the filter constant associated with a v8 String handle
|
||||
*/
|
||||
lzma_vli FilterByName(Value name);
|
||||
|
||||
/**
|
||||
* If rv represents an error, throw a javascript exception representing it.
|
||||
* Always returns rv as a v8 Integer.
|
||||
*/
|
||||
Number lzmaRet(Env env, lzma_ret rv);
|
||||
|
||||
/**
|
||||
* Return a javascript exception representing rv.
|
||||
*/
|
||||
Error lzmaRetError(Env env, lzma_ret rv);
|
||||
|
||||
/**
|
||||
* Takes a Node.js SlowBuffer or Buffer as input and populates data accordingly.
|
||||
* Returns true on success, false on failure.
|
||||
*/
|
||||
bool readBufferFromObj(Value value, std::vector<uint8_t>* data);
|
||||
|
||||
/**
|
||||
* Return a lzma_options_lzma struct as described by the v8 Object obj.
|
||||
*/
|
||||
lzma_options_lzma parseOptionsLZMA(Value obj);
|
||||
|
||||
/**
|
||||
* Return a v8 Number representation of an uint64_t where UINT64_MAX will be mapped to null
|
||||
*/
|
||||
Value Uint64ToNumberMaxNull(Env env, uint64_t in);
|
||||
|
||||
/**
|
||||
* Return a v8 Number representation of an uint64_t where 0 will be mapped to null
|
||||
*/
|
||||
Value Uint64ToNumber0Null(Env env, uint64_t in);
|
||||
|
||||
/**
|
||||
* Return a uint64_t representation of a v8 Number,
|
||||
* where values above UINT64_MAX map to UINT64_MAX and null to UINT64_MAX.
|
||||
* Throws an TypeError if the input is not a number.
|
||||
*/
|
||||
uint64_t NumberToUint64ClampNullMax(Value in);
|
||||
|
||||
/**
|
||||
* Return an integer property of an object (which can be passed to Nan::Get),
|
||||
* providing a default value if no such property is present
|
||||
*/
|
||||
inline int64_t GetIntegerProperty(Object obj, const char* name, int64_t def) {
|
||||
Value v = obj[name];
|
||||
|
||||
if (v.IsUndefined())
|
||||
return def;
|
||||
|
||||
return v.ToNumber().Int64Value();
|
||||
}
|
||||
|
||||
/* bindings in one-to-one correspondence to the lzma functions */
|
||||
Value lzmaVersionNumber(const CallbackInfo& info);
|
||||
Value lzmaVersionString(const CallbackInfo& info);
|
||||
Value lzmaCheckIsSupported(const CallbackInfo& info);
|
||||
Value lzmaCheckSize(const CallbackInfo& info);
|
||||
Value lzmaFilterEncoderIsSupported(const CallbackInfo& info);
|
||||
Value lzmaFilterDecoderIsSupported(const CallbackInfo& info);
|
||||
Value lzmaMfIsSupported(const CallbackInfo& info);
|
||||
Value lzmaModeIsSupported(const CallbackInfo& info);
|
||||
Value lzmaEasyEncoderMemusage(const CallbackInfo& info);
|
||||
Value lzmaEasyDecoderMemusage(const CallbackInfo& info);
|
||||
Value lzmaCRC32(const CallbackInfo& info);
|
||||
Value lzmaRawEncoderMemusage(const CallbackInfo& info);
|
||||
Value lzmaRawDecoderMemusage(const CallbackInfo& info);
|
||||
|
||||
/* wrappers */
|
||||
/**
|
||||
* List of liblzma filters with corresponding options
|
||||
*/
|
||||
class FilterArray {
|
||||
public:
|
||||
FilterArray() = default;
|
||||
explicit FilterArray(Value arr);
|
||||
|
||||
lzma_filter* array() { return filters.data(); }
|
||||
const lzma_filter* array() const { return filters.data(); }
|
||||
|
||||
private:
|
||||
FilterArray(const FilterArray&);
|
||||
FilterArray& operator=(const FilterArray&);
|
||||
|
||||
union options {
|
||||
lzma_options_delta delta;
|
||||
lzma_options_lzma lzma;
|
||||
};
|
||||
|
||||
std::vector<lzma_filter> filters;
|
||||
std::list<options> optbuf;
|
||||
};
|
||||
|
||||
/**
|
||||
* Wrapper for lzma_mt (multi-threading options).
|
||||
*/
|
||||
class MTOptions {
|
||||
public:
|
||||
MTOptions() = default;
|
||||
explicit MTOptions(Value val);
|
||||
|
||||
lzma_mt* opts() { return &opts_; }
|
||||
const lzma_mt* opts() const { return &opts_; }
|
||||
|
||||
private:
|
||||
std::unique_ptr<FilterArray> filters_;
|
||||
lzma_mt opts_;
|
||||
};
|
||||
|
||||
/**
|
||||
* Node.js object wrap for lzma_stream wrapper. Corresponds to exports.Stream
|
||||
*/
|
||||
class LZMAStream : public ObjectWrap<LZMAStream> {
|
||||
public:
|
||||
explicit LZMAStream(const CallbackInfo& info);
|
||||
~LZMAStream();
|
||||
static void InitializeExports(Object exports);
|
||||
|
||||
/* regard as private: */
|
||||
void doLZMACodeFromAsync();
|
||||
void invokeBufferHandlers(bool hasLock);
|
||||
void* alloc(size_t nmemb, size_t size);
|
||||
void free(void* ptr);
|
||||
|
||||
private:
|
||||
void resetUnderlying();
|
||||
void doLZMACode();
|
||||
|
||||
static Napi::Value New(const CallbackInfo& info);
|
||||
|
||||
void adjustExternalMemory(int64_t bytesChange);
|
||||
void reportAdjustedExternalMemoryToV8();
|
||||
|
||||
struct MemScope {
|
||||
explicit MemScope(LZMAStream* stream) : stream(stream) { }
|
||||
~MemScope() { stream->reportAdjustedExternalMemoryToV8(); }
|
||||
LZMAStream* stream;
|
||||
};
|
||||
|
||||
AsyncContext async_context;
|
||||
std::atomic<int64_t> nonAdjustedExternalMemory;
|
||||
std::mutex mutex;
|
||||
|
||||
void ResetUnderlying(const CallbackInfo& info);
|
||||
Napi::Value SetBufsize(const CallbackInfo& info);
|
||||
void Code(const CallbackInfo& info);
|
||||
Napi::Value Memusage(const CallbackInfo& info);
|
||||
Napi::Value MemlimitGet(const CallbackInfo& info);
|
||||
Napi::Value MemlimitSet(const CallbackInfo& info);
|
||||
Napi::Value RawEncoder(const CallbackInfo& info);
|
||||
Napi::Value RawDecoder(const CallbackInfo& info);
|
||||
Napi::Value FiltersUpdate(const CallbackInfo& info);
|
||||
Napi::Value EasyEncoder(const CallbackInfo& info);
|
||||
Napi::Value StreamEncoder(const CallbackInfo& info);
|
||||
Napi::Value AloneEncoder(const CallbackInfo& info);
|
||||
Napi::Value MTEncoder(const CallbackInfo& info);
|
||||
Napi::Value StreamDecoder(const CallbackInfo& info);
|
||||
Napi::Value AutoDecoder(const CallbackInfo& info);
|
||||
Napi::Value AloneDecoder(const CallbackInfo& info);
|
||||
|
||||
lzma_allocator allocator;
|
||||
lzma_stream _;
|
||||
size_t bufsize;
|
||||
std::string error;
|
||||
|
||||
bool shouldFinish;
|
||||
size_t processedChunks;
|
||||
lzma_ret lastCodeResult;
|
||||
std::queue<std::vector<uint8_t>> inbufs;
|
||||
std::queue<std::vector<uint8_t>> outbufs;
|
||||
};
|
||||
|
||||
/**
|
||||
* Async worker for a single coding step.
|
||||
*/
|
||||
class LZMAStreamCodingWorker : public AsyncWorker {
|
||||
public:
|
||||
LZMAStreamCodingWorker(LZMAStream* stream_)
|
||||
: AsyncWorker(Function(stream_->Env(), nullptr), "LZMAStreamCodingWorker"),
|
||||
stream(stream_) {
|
||||
Receiver().Set(static_cast<uint32_t>(0), stream->Value());
|
||||
}
|
||||
|
||||
~LZMAStreamCodingWorker() {}
|
||||
|
||||
void Execute() override {
|
||||
stream->doLZMACodeFromAsync();
|
||||
}
|
||||
|
||||
private:
|
||||
void OnOK() {
|
||||
stream->invokeBufferHandlers(false);
|
||||
}
|
||||
|
||||
void OnOK(const Error& e) {
|
||||
stream->invokeBufferHandlers(false);
|
||||
}
|
||||
|
||||
LZMAStream* stream;
|
||||
};
|
||||
|
||||
class IndexParser : public ObjectWrap<IndexParser> {
|
||||
public:
|
||||
explicit IndexParser(const CallbackInfo& info);
|
||||
~IndexParser();
|
||||
|
||||
static void InitializeExports(Object exports);
|
||||
|
||||
/* regard as private: */
|
||||
int64_t readCallback(void* opaque, uint8_t* buf, size_t count, int64_t offset);
|
||||
|
||||
private:
|
||||
lzma_index_parser_data info;
|
||||
lzma_allocator allocator;
|
||||
|
||||
uint8_t* currentReadBuffer;
|
||||
size_t currentReadSize;
|
||||
bool isCurrentlyInParseCall;
|
||||
|
||||
Object getObject() const;
|
||||
|
||||
void Init(const CallbackInfo& info);
|
||||
Napi::Value Feed(const CallbackInfo& info);
|
||||
Napi::Value Parse(const CallbackInfo& info);
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
416
electron/node_modules/lzma-native/src/lzma-stream.cpp
generated
vendored
Normal file
416
electron/node_modules/lzma-native/src/lzma-stream.cpp
generated
vendored
Normal file
|
|
@ -0,0 +1,416 @@
|
|||
#include "liblzma-node.hpp"
|
||||
#include <cstring>
|
||||
#include <cstdlib>
|
||||
#include <cassert>
|
||||
#include <climits>
|
||||
|
||||
namespace lzma {
|
||||
|
||||
namespace {
|
||||
extern "C" void* LZMA_API_CALL
|
||||
alloc_for_lzma(void *opaque, size_t nmemb, size_t size) {
|
||||
LZMAStream* strm = static_cast<LZMAStream*>(opaque);
|
||||
|
||||
return strm->alloc(nmemb, size);
|
||||
}
|
||||
|
||||
extern "C" void LZMA_API_CALL
|
||||
free_for_lzma(void *opaque, void *ptr) {
|
||||
LZMAStream* strm = static_cast<LZMAStream*>(opaque);
|
||||
|
||||
return strm->free(ptr);
|
||||
}
|
||||
}
|
||||
|
||||
LZMAStream::LZMAStream(const CallbackInfo& info) :
|
||||
ObjectWrap(info),
|
||||
async_context(info.Env(), "LZMAStream"),
|
||||
bufsize(65536),
|
||||
shouldFinish(false),
|
||||
processedChunks(0),
|
||||
lastCodeResult(LZMA_OK)
|
||||
{
|
||||
std::memset(&_, 0, sizeof(lzma_stream));
|
||||
|
||||
allocator.alloc = alloc_for_lzma;
|
||||
allocator.free = free_for_lzma;
|
||||
allocator.opaque = static_cast<void*>(this);
|
||||
_.allocator = &allocator;
|
||||
|
||||
nonAdjustedExternalMemory = 0;
|
||||
MemoryManagement::AdjustExternalMemory(info.Env(), sizeof(LZMAStream));
|
||||
}
|
||||
|
||||
void LZMAStream::resetUnderlying() {
|
||||
if (_.internal != nullptr)
|
||||
lzma_end(&_);
|
||||
|
||||
reportAdjustedExternalMemoryToV8();
|
||||
std::memset(&_, 0, sizeof(lzma_stream));
|
||||
_.allocator = &allocator;
|
||||
lastCodeResult = LZMA_OK;
|
||||
processedChunks = 0;
|
||||
}
|
||||
|
||||
LZMAStream::~LZMAStream() {
|
||||
resetUnderlying();
|
||||
|
||||
MemoryManagement::AdjustExternalMemory(Env(), -int64_t(sizeof(LZMAStream)));
|
||||
}
|
||||
|
||||
void* LZMAStream::alloc(size_t nmemb, size_t size) {
|
||||
size_t nBytes = nmemb * size + sizeof(size_t);
|
||||
|
||||
size_t* result = static_cast<size_t*>(::malloc(nBytes));
|
||||
if (!result)
|
||||
return result;
|
||||
|
||||
*result = nBytes;
|
||||
adjustExternalMemory(static_cast<int64_t>(nBytes));
|
||||
return static_cast<void*>(result + 1);
|
||||
}
|
||||
|
||||
void LZMAStream::free(void* ptr) {
|
||||
if (!ptr)
|
||||
return;
|
||||
|
||||
size_t* orig = static_cast<size_t*>(ptr) - 1;
|
||||
|
||||
adjustExternalMemory(-static_cast<int64_t>(*orig));
|
||||
return ::free(static_cast<void*>(orig));
|
||||
}
|
||||
|
||||
void LZMAStream::reportAdjustedExternalMemoryToV8() {
|
||||
int64_t to_be_reported = nonAdjustedExternalMemory.exchange(0);
|
||||
if (to_be_reported == 0)
|
||||
return;
|
||||
|
||||
MemoryManagement::AdjustExternalMemory(Env(), nonAdjustedExternalMemory);
|
||||
}
|
||||
|
||||
void LZMAStream::adjustExternalMemory(int64_t bytesChange) {
|
||||
nonAdjustedExternalMemory += bytesChange;
|
||||
}
|
||||
|
||||
void LZMAStream::ResetUnderlying(const CallbackInfo& info) {
|
||||
MemScope mem_scope(this);
|
||||
std::lock_guard<std::mutex> lock(mutex);
|
||||
|
||||
resetUnderlying();
|
||||
}
|
||||
|
||||
Value LZMAStream::SetBufsize(const CallbackInfo& info) {
|
||||
size_t oldBufsize, newBufsize = NumberToUint64ClampNullMax(info[0]);
|
||||
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex);
|
||||
|
||||
oldBufsize = bufsize;
|
||||
|
||||
if (newBufsize && newBufsize != UINT_MAX)
|
||||
bufsize = newBufsize;
|
||||
}
|
||||
|
||||
return Number::New(Env(), oldBufsize);
|
||||
}
|
||||
|
||||
void LZMAStream::Code(const CallbackInfo& info) {
|
||||
MemScope mem_scope(this);
|
||||
std::lock_guard<std::mutex> lock(mutex);
|
||||
|
||||
std::vector<uint8_t> inputData;
|
||||
|
||||
if (info[0].IsUndefined() || info[0].IsNull()) {
|
||||
shouldFinish = true;
|
||||
} else {
|
||||
if (!readBufferFromObj(info[0], &inputData))
|
||||
return;
|
||||
|
||||
if (inputData.empty())
|
||||
shouldFinish = true;
|
||||
}
|
||||
inbufs.push(std::move(inputData));
|
||||
|
||||
bool async = info[1].ToBoolean();
|
||||
|
||||
if (async) {
|
||||
(new LZMAStreamCodingWorker(this))->Queue();
|
||||
} else {
|
||||
doLZMACode();
|
||||
invokeBufferHandlers(true);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
struct Maybe {
|
||||
|
||||
};
|
||||
|
||||
void LZMAStream::invokeBufferHandlers(bool hasLock) {
|
||||
Napi::Env env = Env();
|
||||
HandleScope scope(env);
|
||||
MemScope mem_scope(this);
|
||||
|
||||
std::unique_lock<std::mutex> lock;
|
||||
if (!hasLock)
|
||||
lock = std::unique_lock<std::mutex>(mutex);
|
||||
|
||||
Function bufferHandler = Napi::Value(Value()["bufferHandler"]).As<Function>();
|
||||
std::vector<uint8_t> outbuf;
|
||||
|
||||
auto CallBufferHandlerWithArgv = [&](size_t argc, const napi_value* argv) {
|
||||
if (!hasLock) lock.unlock();
|
||||
bufferHandler.MakeCallback(Value(), 5, argv, async_context);
|
||||
if (!hasLock) lock.lock();
|
||||
};
|
||||
|
||||
uint64_t in = UINT64_MAX, out = UINT64_MAX;
|
||||
if (_.internal)
|
||||
lzma_get_progress(&_, &in, &out);
|
||||
Napi::Value in_ = Uint64ToNumberMaxNull(env, in);
|
||||
Napi::Value out_ = Uint64ToNumberMaxNull(env, out);
|
||||
|
||||
while (outbufs.size() > 0) {
|
||||
outbuf = std::move(outbufs.front());
|
||||
outbufs.pop();
|
||||
|
||||
napi_value argv[5] = {
|
||||
Buffer<char>::Copy(env, reinterpret_cast<const char*>(outbuf.data()), outbuf.size()),
|
||||
env.Undefined(), env.Undefined(), in_, out_
|
||||
};
|
||||
CallBufferHandlerWithArgv(5, argv);
|
||||
}
|
||||
|
||||
bool reset = false;
|
||||
if (lastCodeResult != LZMA_OK) {
|
||||
Napi::Value errorArg = env.Null();
|
||||
|
||||
if (lastCodeResult != LZMA_STREAM_END)
|
||||
errorArg = lzmaRetError(env, lastCodeResult).Value();
|
||||
|
||||
reset = true;
|
||||
|
||||
napi_value argv[5] = { env.Null(), env.Undefined(), errorArg, in_, out_ };
|
||||
CallBufferHandlerWithArgv(5, argv);
|
||||
}
|
||||
|
||||
if (processedChunks) {
|
||||
size_t pc = processedChunks;
|
||||
processedChunks = 0;
|
||||
|
||||
napi_value argv[5] = {
|
||||
env.Undefined(), Number::New(env, static_cast<uint32_t>(pc)),
|
||||
env.Undefined(), in_, out_
|
||||
};
|
||||
CallBufferHandlerWithArgv(5, argv);
|
||||
}
|
||||
|
||||
if (reset)
|
||||
resetUnderlying(); // resets lastCodeResult!
|
||||
}
|
||||
|
||||
void LZMAStream::doLZMACodeFromAsync() {
|
||||
std::lock_guard<std::mutex> lock(mutex);
|
||||
|
||||
doLZMACode();
|
||||
}
|
||||
|
||||
void LZMAStream::doLZMACode() {
|
||||
std::vector<uint8_t> outbuf(bufsize), inbuf;
|
||||
_.next_out = outbuf.data();
|
||||
_.avail_out = outbuf.size();
|
||||
_.avail_in = 0;
|
||||
|
||||
lzma_action action = LZMA_RUN;
|
||||
|
||||
size_t readChunks = 0;
|
||||
|
||||
// _.internal is set to nullptr when lzma_end() is called via resetUnderlying()
|
||||
while (_.internal) {
|
||||
if (_.avail_in == 0) { // more input neccessary?
|
||||
while (_.avail_in == 0 && !inbufs.empty()) {
|
||||
inbuf = std::move(inbufs.front());
|
||||
inbufs.pop();
|
||||
readChunks++;
|
||||
|
||||
_.next_in = inbuf.data();
|
||||
_.avail_in = inbuf.size();
|
||||
}
|
||||
}
|
||||
|
||||
if (shouldFinish && inbufs.empty())
|
||||
action = LZMA_FINISH;
|
||||
|
||||
_.next_out = outbuf.data();
|
||||
_.avail_out = outbuf.size();
|
||||
|
||||
lastCodeResult = lzma_code(&_, action);
|
||||
|
||||
if (lastCodeResult != LZMA_OK && lastCodeResult != LZMA_STREAM_END) {
|
||||
processedChunks += readChunks;
|
||||
readChunks = 0;
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
if (_.avail_out == 0 || _.avail_in == 0 || lastCodeResult == LZMA_STREAM_END) {
|
||||
size_t outsz = outbuf.size() - _.avail_out;
|
||||
|
||||
if (outsz > 0) {
|
||||
#ifndef LZMA_NO_CXX11_RVALUE_REFERENCES // C++11
|
||||
outbufs.emplace(outbuf.data(), outbuf.data() + outsz);
|
||||
#else
|
||||
outbufs.push(std::vector<uint8_t>(outbuf.data(), outbuf.data() + outsz));
|
||||
#endif
|
||||
}
|
||||
|
||||
if (lastCodeResult == LZMA_STREAM_END) {
|
||||
processedChunks += readChunks;
|
||||
readChunks = 0;
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (_.avail_out == outbuf.size()) { // no progress was made
|
||||
if (!shouldFinish) {
|
||||
processedChunks += readChunks;
|
||||
readChunks = 0;
|
||||
}
|
||||
|
||||
|
||||
if (!shouldFinish)
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void LZMAStream::InitializeExports(Object exports) {
|
||||
exports["Stream"] = DefineClass(exports.Env(), "LZMAStream", {
|
||||
InstanceMethod("setBufsize", &LZMAStream::SetBufsize),
|
||||
InstanceMethod("resetUnderlying", &LZMAStream::ResetUnderlying),
|
||||
InstanceMethod("code", &LZMAStream::Code),
|
||||
InstanceMethod("memusage", &LZMAStream::Memusage),
|
||||
InstanceMethod("memlimitGet", &LZMAStream::MemlimitGet),
|
||||
InstanceMethod("memlimitSet", &LZMAStream::MemlimitSet),
|
||||
InstanceMethod("rawEncoder_", &LZMAStream::RawEncoder),
|
||||
InstanceMethod("rawDecoder_", &LZMAStream::RawDecoder),
|
||||
InstanceMethod("filtersUpdate", &LZMAStream::FiltersUpdate),
|
||||
InstanceMethod("easyEncoder_", &LZMAStream::EasyEncoder),
|
||||
InstanceMethod("streamEncoder_", &LZMAStream::StreamEncoder),
|
||||
InstanceMethod("aloneEncoder", &LZMAStream::AloneEncoder),
|
||||
InstanceMethod("mtEncoder_", &LZMAStream::MTEncoder),
|
||||
InstanceMethod("streamDecoder_", &LZMAStream::StreamDecoder),
|
||||
InstanceMethod("autoDecoder_", &LZMAStream::AutoDecoder),
|
||||
InstanceMethod("aloneDecoder_", &LZMAStream::AloneDecoder),
|
||||
});
|
||||
}
|
||||
|
||||
Value LZMAStream::Memusage(const CallbackInfo& info) {
|
||||
std::lock_guard<std::mutex> lock(mutex);
|
||||
|
||||
return Uint64ToNumber0Null(Env(), lzma_memusage(&_));
|
||||
}
|
||||
|
||||
Value LZMAStream::MemlimitGet(const CallbackInfo& info) {
|
||||
std::lock_guard<std::mutex> lock(mutex);
|
||||
|
||||
return Uint64ToNumber0Null(Env(), lzma_memlimit_get(&_));
|
||||
}
|
||||
|
||||
Value LZMAStream::MemlimitSet(const CallbackInfo& info) {
|
||||
std::lock_guard<std::mutex> lock(mutex);
|
||||
|
||||
if (!info[0].IsNumber())
|
||||
throw TypeError::New(Env(), "memlimitSet() needs a numerical argument");
|
||||
|
||||
Number arg = info[0].As<Number>();
|
||||
|
||||
return lzmaRet(Env(), lzma_memlimit_set(&_, NumberToUint64ClampNullMax(arg)));
|
||||
}
|
||||
|
||||
Value LZMAStream::RawEncoder(const CallbackInfo& info) {
|
||||
std::lock_guard<std::mutex> lock(mutex);
|
||||
|
||||
const FilterArray filters(info[0]);
|
||||
|
||||
return lzmaRet(Env(), lzma_raw_encoder(&_, filters.array()));
|
||||
}
|
||||
|
||||
Value LZMAStream::RawDecoder(const CallbackInfo& info) {
|
||||
std::lock_guard<std::mutex> lock(mutex);
|
||||
|
||||
const FilterArray filters(info[0]);
|
||||
|
||||
return lzmaRet(Env(), lzma_raw_decoder(&_, filters.array()));
|
||||
}
|
||||
|
||||
Value LZMAStream::FiltersUpdate(const CallbackInfo& info) {
|
||||
std::lock_guard<std::mutex> lock(mutex);
|
||||
|
||||
const FilterArray filters(info[0]);
|
||||
|
||||
return lzmaRet(Env(), lzma_filters_update(&_, filters.array()));
|
||||
}
|
||||
|
||||
Value LZMAStream::EasyEncoder(const CallbackInfo& info) {
|
||||
std::lock_guard<std::mutex> lock(mutex);
|
||||
|
||||
int64_t preset = info[0].ToNumber().Int64Value();
|
||||
int64_t check = info[1].ToNumber().Int64Value();
|
||||
|
||||
return lzmaRet(Env(), lzma_easy_encoder(&_, preset, (lzma_check) check));
|
||||
}
|
||||
|
||||
Value LZMAStream::StreamEncoder(const CallbackInfo& info) {
|
||||
std::lock_guard<std::mutex> lock(mutex);
|
||||
|
||||
const FilterArray filters(info[0]);
|
||||
int64_t check = info[1].ToNumber().Int64Value();
|
||||
|
||||
return lzmaRet(Env(), lzma_stream_encoder(&_, filters.array(), (lzma_check) check));
|
||||
}
|
||||
|
||||
Value LZMAStream::MTEncoder(const CallbackInfo& info) {
|
||||
std::lock_guard<std::mutex> lock(mutex);
|
||||
|
||||
const MTOptions mt(info[0]);
|
||||
|
||||
return lzmaRet(Env(), lzma_stream_encoder_mt(&_, mt.opts()));
|
||||
}
|
||||
|
||||
Value LZMAStream::AloneEncoder(const CallbackInfo& info) {
|
||||
std::lock_guard<std::mutex> lock(mutex);
|
||||
|
||||
lzma_options_lzma o = parseOptionsLZMA(info[0]);
|
||||
|
||||
return lzmaRet(Env(), lzma_alone_encoder(&_, &o));
|
||||
}
|
||||
|
||||
Value LZMAStream::StreamDecoder(const CallbackInfo& info) {
|
||||
std::lock_guard<std::mutex> lock(mutex);
|
||||
|
||||
uint64_t memlimit = NumberToUint64ClampNullMax(info[0]);
|
||||
int64_t flags = info[1].ToNumber().Int64Value();
|
||||
|
||||
return lzmaRet(Env(), lzma_stream_decoder(&_, memlimit, flags));
|
||||
}
|
||||
|
||||
Value LZMAStream::AutoDecoder(const CallbackInfo& info) {
|
||||
std::lock_guard<std::mutex> lock(mutex);
|
||||
|
||||
uint64_t memlimit = NumberToUint64ClampNullMax(info[0]);
|
||||
int64_t flags = info[1].ToNumber().Int64Value();
|
||||
|
||||
return lzmaRet(Env(), lzma_auto_decoder(&_, memlimit, flags));
|
||||
}
|
||||
|
||||
Value LZMAStream::AloneDecoder(const CallbackInfo& info) {
|
||||
std::lock_guard<std::mutex> lock(mutex);
|
||||
|
||||
uint64_t memlimit = NumberToUint64ClampNullMax(info[0]);
|
||||
|
||||
return lzmaRet(Env(), lzma_alone_decoder(&_, memlimit));
|
||||
}
|
||||
|
||||
}
|
||||
99
electron/node_modules/lzma-native/src/module.cpp
generated
vendored
Normal file
99
electron/node_modules/lzma-native/src/module.cpp
generated
vendored
Normal file
|
|
@ -0,0 +1,99 @@
|
|||
#include "liblzma-node.hpp"
|
||||
|
||||
using namespace lzma;
|
||||
|
||||
static Napi::Object moduleInit(Env env, Object exports) {
|
||||
LZMAStream::InitializeExports(exports);
|
||||
IndexParser::InitializeExports(exports);
|
||||
|
||||
exports["versionNumber"] = Function::New(env, lzmaVersionNumber);
|
||||
exports["versionString"] = Function::New(env, lzmaVersionString);
|
||||
exports["checkIsSupported"] = Function::New(env, lzmaCheckIsSupported);
|
||||
exports["checkSize"] = Function::New(env, lzmaCheckSize);
|
||||
exports["crc32_"] = Function::New(env, lzmaCRC32);
|
||||
exports["filterEncoderIsSupported"] = Function::New(env, lzmaFilterEncoderIsSupported);
|
||||
exports["filterDecoderIsSupported"] = Function::New(env, lzmaFilterDecoderIsSupported);
|
||||
exports["rawEncoderMemusage"] = Function::New(env, lzmaRawEncoderMemusage);
|
||||
exports["rawDecoderMemusage"] = Function::New(env, lzmaRawDecoderMemusage);
|
||||
exports["mfIsSupported"] = Function::New(env, lzmaMfIsSupported);
|
||||
exports["modeIsSupported"] = Function::New(env, lzmaModeIsSupported);
|
||||
exports["easyEncoderMemusage"] = Function::New(env, lzmaEasyEncoderMemusage);
|
||||
exports["easyDecoderMemusage"] = Function::New(env, lzmaEasyDecoderMemusage);
|
||||
|
||||
// enum lzma_ret
|
||||
exports["OK"] = Number::New(env, LZMA_OK);
|
||||
exports["STREAM_END"] = Number::New(env, LZMA_STREAM_END);
|
||||
exports["NO_CHECK"] = Number::New(env, LZMA_NO_CHECK);
|
||||
exports["UNSUPPORTED_CHECK"] = Number::New(env, LZMA_UNSUPPORTED_CHECK);
|
||||
exports["GET_CHECK"] = Number::New(env, LZMA_GET_CHECK);
|
||||
exports["MEM_ERROR"] = Number::New(env, LZMA_MEM_ERROR);
|
||||
exports["MEMLIMIT_ERROR"] = Number::New(env, LZMA_MEMLIMIT_ERROR);
|
||||
exports["FORMAT_ERROR"] = Number::New(env, LZMA_FORMAT_ERROR);
|
||||
exports["OPTIONS_ERROR"] = Number::New(env, LZMA_OPTIONS_ERROR);
|
||||
exports["DATA_ERROR"] = Number::New(env, LZMA_DATA_ERROR);
|
||||
exports["BUF_ERROR"] = Number::New(env, LZMA_BUF_ERROR);
|
||||
exports["PROG_ERROR"] = Number::New(env, LZMA_PROG_ERROR);
|
||||
|
||||
// enum lzma_action
|
||||
exports["RUN"] = Number::New(env, LZMA_RUN);
|
||||
exports["SYNC_FLUSH"] = Number::New(env, LZMA_SYNC_FLUSH);
|
||||
exports["FULL_FLUSH"] = Number::New(env, LZMA_FULL_FLUSH);
|
||||
exports["FINISH"] = Number::New(env, LZMA_FINISH);
|
||||
|
||||
// enum lzma_check
|
||||
exports["CHECK_NONE"] = Number::New(env, LZMA_CHECK_NONE);
|
||||
exports["CHECK_CRC32"] = Number::New(env, LZMA_CHECK_CRC32);
|
||||
exports["CHECK_CRC64"] = Number::New(env, LZMA_CHECK_CRC64);
|
||||
exports["CHECK_SHA256"] = Number::New(env, LZMA_CHECK_SHA256);
|
||||
|
||||
// lzma_match_finder
|
||||
exports["MF_HC3"] = Number::New(env, LZMA_MF_HC3);
|
||||
exports["MF_HC4"] = Number::New(env, LZMA_MF_HC4);
|
||||
exports["MF_BT2"] = Number::New(env, LZMA_MF_BT2);
|
||||
exports["MF_BT3"] = Number::New(env, LZMA_MF_BT3);
|
||||
exports["MF_BT4"] = Number::New(env, LZMA_MF_BT4);
|
||||
|
||||
// lzma_mode
|
||||
exports["MODE_FAST"] = Number::New(env, LZMA_MODE_FAST);
|
||||
exports["MODE_NORMAL"] = Number::New(env, LZMA_MODE_NORMAL);
|
||||
|
||||
// defines
|
||||
exports["FILTER_X86"] = String::New(env, "LZMA_FILTER_X86");
|
||||
exports["FILTER_POWERPC"] = String::New(env, "LZMA_FILTER_POWERPC");
|
||||
exports["FILTER_IA64"] = String::New(env, "LZMA_FILTER_IA64");
|
||||
exports["FILTER_ARM"] = String::New(env, "LZMA_FILTER_ARM");
|
||||
exports["FILTER_ARMTHUMB"] = String::New(env, "LZMA_FILTER_ARMTHUMB");
|
||||
exports["FILTER_SPARC"] = String::New(env, "LZMA_FILTER_SPARC");
|
||||
exports["FILTER_DELTA"] = String::New(env, "LZMA_FILTER_DELTA");
|
||||
exports["FILTERS_MAX"] = String::New(env, "LZMA_FILTERS_MAX");
|
||||
exports["FILTER_LZMA1"] = String::New(env, "LZMA_FILTER_LZMA1");
|
||||
exports["FILTER_LZMA2"] = String::New(env, "LZMA_FILTER_LZMA2");
|
||||
exports["VLI_UNKNOWN"] = String::New(env, "LZMA_VLI_UNKNOWN");
|
||||
|
||||
exports["VLI_BYTES_MAX"] = Number::New(env, LZMA_VLI_BYTES_MAX);
|
||||
exports["CHECK_ID_MAX"] = Number::New(env, LZMA_CHECK_ID_MAX);
|
||||
exports["CHECK_SIZE_MAX"] = Number::New(env, LZMA_CHECK_SIZE_MAX);
|
||||
exports["PRESET_DEFAULT"] = Number::New(env, LZMA_PRESET_DEFAULT);
|
||||
exports["PRESET_LEVEL_MASK"] = Number::New(env, LZMA_PRESET_LEVEL_MASK);
|
||||
exports["PRESET_EXTREME"] = Number::New(env, LZMA_PRESET_EXTREME);
|
||||
exports["TELL_NO_CHECK"] = Number::New(env, LZMA_TELL_NO_CHECK);
|
||||
exports["TELL_UNSUPPORTED_CHECK"] = Number::New(env, LZMA_TELL_UNSUPPORTED_CHECK);
|
||||
exports["TELL_ANY_CHECK"] = Number::New(env, LZMA_TELL_ANY_CHECK);
|
||||
exports["CONCATENATED"] = Number::New(env, LZMA_CONCATENATED);
|
||||
exports["STREAM_HEADER_SIZE"] = Number::New(env, LZMA_STREAM_HEADER_SIZE);
|
||||
exports["VERSION_MAJOR"] = Number::New(env, LZMA_VERSION_MAJOR);
|
||||
exports["VERSION_MINOR"] = Number::New(env, LZMA_VERSION_MINOR);
|
||||
exports["VERSION_PATCH"] = Number::New(env, LZMA_VERSION_PATCH);
|
||||
exports["VERSION_STABILITY"] = Number::New(env, LZMA_VERSION_STABILITY);
|
||||
exports["VERSION_STABILITY_ALPHA"] = Number::New(env, LZMA_VERSION_STABILITY_ALPHA);
|
||||
exports["VERSION_STABILITY_BETA"] = Number::New(env, LZMA_VERSION_STABILITY_BETA);
|
||||
exports["VERSION_STABILITY_STABLE"] = Number::New(env, LZMA_VERSION_STABILITY_STABLE);
|
||||
exports["VERSION"] = Number::New(env, LZMA_VERSION);
|
||||
exports["VERSION_STRING"] = String::New(env, LZMA_VERSION_STRING);
|
||||
|
||||
exports["asyncCodeAvailable"] = Boolean::New(env, true);
|
||||
return exports;
|
||||
}
|
||||
|
||||
NODE_API_MODULE(lzma_native, moduleInit)
|
||||
|
||||
28
electron/node_modules/lzma-native/src/mt-options.cpp
generated
vendored
Normal file
28
electron/node_modules/lzma-native/src/mt-options.cpp
generated
vendored
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
#include "liblzma-node.hpp"
|
||||
|
||||
namespace lzma {
|
||||
|
||||
MTOptions::MTOptions(Value val) {
|
||||
Object opt = val.IsUndefined() || val.IsNull() ?
|
||||
Object::New(val.Env()) : val.ToObject();
|
||||
opts_.flags = 0;
|
||||
opts_.filters = nullptr;
|
||||
|
||||
opts_.block_size = Value(opt["blockSize"]).ToNumber().Int64Value();
|
||||
opts_.timeout = Value(opt["timeout"]).ToNumber().Uint32Value();
|
||||
opts_.preset = Value(opt["preset"]).ToNumber().Uint32Value();
|
||||
opts_.check = (lzma_check)Value(opt["check"]).ToNumber().Int32Value();
|
||||
opts_.threads = Value(opt["threads"]).ToNumber().Uint32Value();
|
||||
|
||||
if (opts_.threads == 0) {
|
||||
opts_.threads = lzma_cputhreads();
|
||||
}
|
||||
|
||||
Value filters = opt["filters"];
|
||||
if (filters.IsArray()) {
|
||||
filters_.reset(new FilterArray(filters));
|
||||
opts_.filters = filters_->array();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
140
electron/node_modules/lzma-native/src/util.cpp
generated
vendored
Normal file
140
electron/node_modules/lzma-native/src/util.cpp
generated
vendored
Normal file
|
|
@ -0,0 +1,140 @@
|
|||
#include "liblzma-node.hpp"
|
||||
#include <cstring>
|
||||
|
||||
namespace lzma {
|
||||
|
||||
lzma_vli FilterByName(Value name) {
|
||||
std::string cpp_string(name.ToString());
|
||||
|
||||
struct SearchEntry {
|
||||
const char* str;
|
||||
lzma_vli value;
|
||||
};
|
||||
|
||||
static const struct SearchEntry search[] = {
|
||||
{ "LZMA_FILTER_X86", LZMA_FILTER_X86 },
|
||||
{ "LZMA_FILTER_POWERPC", LZMA_FILTER_POWERPC },
|
||||
{ "LZMA_FILTER_IA64", LZMA_FILTER_IA64 },
|
||||
{ "LZMA_FILTER_ARM", LZMA_FILTER_ARM },
|
||||
{ "LZMA_FILTER_ARMTHUMB", LZMA_FILTER_ARMTHUMB },
|
||||
{ "LZMA_FILTER_SPARC", LZMA_FILTER_SPARC },
|
||||
{ "LZMA_FILTER_DELTA", LZMA_FILTER_DELTA },
|
||||
{ "LZMA_FILTER_LZMA1", LZMA_FILTER_LZMA1 },
|
||||
{ "LZMA_FILTER_LZMA2", LZMA_FILTER_LZMA2 },
|
||||
{ "LZMA_FILTERS_MAX", LZMA_FILTERS_MAX },
|
||||
{ "LZMA_VLI_UNKNOWN", LZMA_VLI_UNKNOWN }
|
||||
};
|
||||
|
||||
for (const struct SearchEntry* p = search; ; ++p)
|
||||
if (p->value == LZMA_VLI_UNKNOWN || cpp_string == p->str)
|
||||
return p->value;
|
||||
}
|
||||
|
||||
Error lzmaRetError(Env env, lzma_ret rv) {
|
||||
struct ErrorInfo {
|
||||
lzma_ret code;
|
||||
const char* name;
|
||||
const char* desc;
|
||||
};
|
||||
|
||||
/* description strings taken from liblzma/…/api/base.h */
|
||||
static const struct ErrorInfo searchErrorInfo[] = {
|
||||
{ LZMA_OK, "LZMA_OK", "Operation completed successfully" },
|
||||
{ LZMA_STREAM_END, "LZMA_STREAM_END", "End of stream was reached" },
|
||||
{ LZMA_NO_CHECK, "LZMA_NO_CHECK", "Input stream has no integrity check" },
|
||||
{ LZMA_UNSUPPORTED_CHECK, "LZMA_UNSUPPORTED_CHECK", "Cannot calculate the integrity check" },
|
||||
{ LZMA_GET_CHECK, "LZMA_GET_CHECK", "Integrity check type is now available" },
|
||||
{ LZMA_MEM_ERROR, "LZMA_MEM_ERROR", "Cannot allocate memory" },
|
||||
{ LZMA_MEMLIMIT_ERROR, "LZMA_MEMLIMIT_ERROR", "Memory usage limit was reached" },
|
||||
{ LZMA_FORMAT_ERROR, "LZMA_FORMAT_ERROR", "File format not recognized" },
|
||||
{ LZMA_OPTIONS_ERROR, "LZMA_OPTIONS_ERROR", "Invalid or unsupported options" },
|
||||
{ LZMA_DATA_ERROR, "LZMA_DATA_ERROR", "Data is corrupt" },
|
||||
{ LZMA_PROG_ERROR, "LZMA_PROG_ERROR", "Programming error" },
|
||||
{ LZMA_BUF_ERROR, "LZMA_BUF_ERROR", "No progress is possible" },
|
||||
{ (lzma_ret)-1, "LZMA_UNKNOWN_ERROR", "Unknown error code" }
|
||||
};
|
||||
|
||||
const struct ErrorInfo* p = searchErrorInfo;
|
||||
while (p->code != rv && p->code != (lzma_ret)-1)
|
||||
++p;
|
||||
|
||||
Error err = Error::New(env, p->desc);
|
||||
err.Set("code", Number::New(env, rv));
|
||||
err.Set("name", String::New(env, p->name));
|
||||
err.Set("desc", String::New(env, p->desc));
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
Number lzmaRet(Env env, lzma_ret rv) {
|
||||
if (rv != LZMA_OK && rv != LZMA_STREAM_END)
|
||||
throw lzmaRetError(env, rv);
|
||||
|
||||
return Number::New(env, rv);
|
||||
}
|
||||
|
||||
bool readBufferFromObj(Value buf_, std::vector<uint8_t>* data) {
|
||||
if (!buf_.IsTypedArray()) {
|
||||
throw TypeError::New(buf_.Env(), "Expected Buffer as input");
|
||||
return false;
|
||||
}
|
||||
|
||||
TypedArray buf = buf_.As<TypedArray>();
|
||||
size_t len = buf.ByteLength();
|
||||
const uint8_t* ptr = len > 0 ?
|
||||
static_cast<const uint8_t*>(buf.ArrayBuffer().Data()) + buf.ByteOffset() :
|
||||
reinterpret_cast<const uint8_t*>("");
|
||||
|
||||
*data = std::vector<uint8_t>(ptr, ptr + len);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
lzma_options_lzma parseOptionsLZMA (Value val) {
|
||||
HandleScope scope(val.Env());
|
||||
Object obj = val.IsUndefined() || val.IsNull() ?
|
||||
Object::New(val.Env()) : val.ToObject();
|
||||
|
||||
lzma_options_lzma r;
|
||||
r.dict_size = GetIntegerProperty(obj, "dictSize", LZMA_DICT_SIZE_DEFAULT);
|
||||
r.lp = GetIntegerProperty(obj, "lp", LZMA_LP_DEFAULT);
|
||||
r.lc = GetIntegerProperty(obj, "lc", LZMA_LC_DEFAULT);
|
||||
r.pb = GetIntegerProperty(obj, "pb", LZMA_PB_DEFAULT);
|
||||
r.mode = (lzma_mode)GetIntegerProperty(obj, "mode", (int64_t)LZMA_MODE_FAST);
|
||||
r.nice_len = GetIntegerProperty(obj, "niceLen", 64);
|
||||
r.mf = (lzma_match_finder)GetIntegerProperty(obj, "mf", (int64_t)LZMA_MF_HC4);
|
||||
r.depth = GetIntegerProperty(obj, "depth", 0);
|
||||
uint64_t preset_ = GetIntegerProperty(obj, "preset", UINT64_MAX);
|
||||
|
||||
r.preset_dict = nullptr;
|
||||
|
||||
if (preset_ != UINT64_MAX)
|
||||
lzma_lzma_preset(&r, preset_);
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
Value Uint64ToNumberMaxNull(Env env, uint64_t in) {
|
||||
if (in == UINT64_MAX)
|
||||
return env.Null();
|
||||
else
|
||||
return Number::New(env, in);
|
||||
}
|
||||
|
||||
Value Uint64ToNumber0Null(Env env, uint64_t in) {
|
||||
if (in == 0)
|
||||
return env.Null();
|
||||
else
|
||||
return Number::New(env, in);
|
||||
}
|
||||
|
||||
uint64_t NumberToUint64ClampNullMax(Value in) {
|
||||
if (in.IsNull() || in.IsUndefined())
|
||||
return UINT64_MAX;
|
||||
|
||||
Number n = in.ToNumber();
|
||||
|
||||
return n.Int64Value();
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue