Remove Zefie's chunking and async stuff. Will revisit later. Fixing alg first.

This commit is contained in:
Eric MacDonald
2021-08-07 15:11:18 -04:00
parent 6a286d6851
commit 7a69d3e8da
2 changed files with 19 additions and 47 deletions

View File

@@ -9,17 +9,15 @@ var EventEmitter = require('events').EventEmitter;
* By: Eric MacDonald (eMac) * By: Eric MacDonald (eMac)
*/ */
class WTVLzpf extends EventEmitter { class WTVLzpf {
// Note: currentlty doesn't offer optimal streaming support but this is good enough to meet perf demands at the scale we're at. // Note: currentlty doesn't offer optimal streaming support but this is good enough to meet perf demands at the scale we're at.
current_length = 0 current_length = 0
current_literal = 0 current_literal = 0
compressed_offset = 0
total_compressed = 0;
chunk_size = 65535; chunk_size = 65535;
flag_table = new Uint16Array(0x1000) flag_table = new Uint16Array(0x1000)
ring_buffer = new Uint8Array(0x2000) ring_buffer = new Uint8Array(0x2000)
compressed_data = null; compressed_data = [];
nomatchEncode = [ nomatchEncode = [
[0x0000, 0x10], [0x0001, 0x10], [0x0002, 0x10], [0x0000, 0x10], [0x0001, 0x10], [0x0002, 0x10],
@@ -266,29 +264,25 @@ class WTVLzpf extends EventEmitter {
/** /**
* Initialize the Lzpf class. * Initialize the Lzpf class.
* *
* @param chunk_size {Number} Set the size of the compressed data chunks to fire off with 'data' event. (suggested betweet 2048 and 65535);
* @returns {undefined} * @returns {undefined}
*/ */
constructor(chunk_size = 0) { constructor() {
super(); // for extended class
if (chunk_size > 0) this.chunk_size = chunk_size;
this.clear(); this.clear();
} }
/** /**
* Sets starting values for the compression algorithm. * Sets starting values for the compression algorithm.
* *
* @param length {Number} size of compressed data Buffer to allocate
* @returns {undefined} * @returns {undefined}
*/ */
clear(length = 0) { clear() {
this.current_length = 0; this.current_length = 0;
this.current_literal = 0; this.current_literal = 0;
this.total_compressed = 0; this.total_compressed = 0;
this.compressed_offset = 0; this.compressed_offset = 0;
this.ring_buffer.fill(0x00, 0, 0x2000) this.ring_buffer.fill(0x00, 0, 0x2000)
this.flag_table.fill(0xFFFF, 0, 0x1000); this.flag_table.fill(0xFFFF, 0, 0x1000);
this.compressed_data = (length > 0) ? null : Buffer.alloc(length); this.compressed_data = [];
} }
/** /**
@@ -306,8 +300,7 @@ class WTVLzpf extends EventEmitter {
this.current_length += code_length; this.current_length += code_length;
while (this.current_length > 7) { while (this.current_length > 7) {
//console.log("add", this.current_literal >>> 0, code >>> 0, byte, type) this.compressed_data.push((this.current_literal >>> 0x18) & 0xFF);
this.compressed_offset = this.compressed_data.writeUInt8((this.current_literal >>> 0x18) & 0xFF, this.compressed_offset);
this.current_length -= 8; this.current_length -= 8;
this.current_literal = (this.current_literal << 8) & 0xFFFFFFFF; this.current_literal = (this.current_literal << 8) & 0xFFFFFFFF;
@@ -321,23 +314,17 @@ class WTVLzpf extends EventEmitter {
* *
* @returns {Buffer} Lzpf compression data * @returns {Buffer} Lzpf compression data
*/ */
async Compress(uncompressed_data) { Compress(uncompressed_data) {
this.clear(); this.clear();
if (uncompressed_data.words) {
// if its a wordArray convert it to a Buffer
// Barrow function from WTVSec class if (uncompressed_data.words) {
const WTVSec = require("./WTVSec.js");
var wtvsec = new WTVSec();
uncompressed_data = new Buffer.from(wtvsec.wordArrayToUint8Array(uncompressed_data)); uncompressed_data = new Buffer.from(wtvsec.wordArrayToUint8Array(uncompressed_data));
} else if (!uncompressed_data.byteLength) { } else if (!uncompressed_data.byteLength) {
// otherwise if its not already a Buffer, convert it to one // otherwise if its not already a Buffer, convert it to one
uncompressed_data = new Buffer.from(uncompressed_data); uncompressed_data = new Buffer.from(uncompressed_data);
} }
var uncompressed_len = uncompressed_data.byteLength; var uncompressed_len = uncompressed_data.length;
this.clear(uncompressed_len);
this.compressed_data = new Buffer.alloc((uncompressed_len >= this.chunk_size) ? this.chunk_size : uncompressed_len);
var i = 0; var i = 0;
var sum = 0; var sum = 0;
@@ -397,11 +384,6 @@ class WTVLzpf extends EventEmitter {
if (code_length > 0) { if (code_length > 0) {
this.EncodeLiteral(code_length, code); this.EncodeLiteral(code_length, code);
} }
if (this.compressed_offset == this.chunk_size) {
this.total_compressed += this.compressed_offset;
this.emit('data', this.compressed_data, this.compressed_offset, (this.total_compressed - this.compressed_offset), false);
this.compressed_offset = 0;
}
} }
// Finish up. This would normally be in an Lzpf_Finish method. // Finish up. This would normally be in an Lzpf_Finish method.
@@ -429,12 +411,10 @@ class WTVLzpf extends EventEmitter {
this.EncodeLiteral(0x08, (sum << 0x18) & 0xFFFFFFFF); this.EncodeLiteral(0x08, (sum << 0x18) & 0xFFFFFFFF);
// End // End
this.compressed_offset = this.compressed_data.writeUInt8(this.current_literal >>> 0x18, this.compressed_offset); this.compressed_data.push(this.current_literal >>> 0x18);
this.compressed_offset = this.compressed_data.writeUInt8(0x20, this.compressed_offset); this.compressed_data.push(0x20);
this.total_compressed += this.compressed_offset; return Buffer.from(this.compressed_data);
this.emit('data', this.compressed_data, this.compressed_offset, (this.total_compressed - this.compressed_offset), this.total_compressed);
} }
} }

View File

@@ -484,23 +484,15 @@ async function sendToClient(socket, headers_obj, data, compress_data = false) {
} }
// compress if needed // compress if needed
if (compress_data && clen > 0 && !headers_obj['minisrv-already-compressed']) { if (compress_data && clen > 0) {
if (zdebug) console.log(" # Uncompressed data length:", clen);
headers_obj["wtv-lzpf"] = 0; headers_obj["wtv-lzpf"] = 0;
var wtvcomp = new WTVLzpf(); var wtvcomp = new WTVLzpf();
var compressed_data = new Buffer.alloc(clen); data = wtvcomp.Compress(data);
wtvcomp.on('data', (data, length, offset, complete) => {
data.copy(compressed_data, offset, 0, length); console.log("data", data)
if (complete !== false) {
data = new Buffer.alloc(complete); wtvcomp = null; // Makes the garbage gods happy so it cleans up our mess
compressed_data.copy(data, 0, 0, compressed_data.byteLength);
compressed_data, wtvcomp = null;
headers_obj['minisrv-already-compressed'] = true;
sendToClient(socket, headers_obj, data);
}
});
wtvcomp.Compress(data);
return;
} }
if (headers_obj['minisrv-already-compressed']) delete headers_obj['minisrv-already-compressed']; if (headers_obj['minisrv-already-compressed']) delete headers_obj['minisrv-already-compressed'];