Move LZMA to utils.compress, add Deflate, GZip and Zlib compress/decompress support

This commit is contained in:
Joshua Granick
2016-07-20 20:50:35 -07:00
parent 5945147b8a
commit 683094599a
14 changed files with 544 additions and 94 deletions

View File

@@ -196,6 +196,10 @@ import lime.ui.Mouse;
import lime.ui.MouseCursor;
import lime.ui.Touch;
import lime.ui.Window;
import lime.utils.compress.Deflate;
import lime.utils.compress.GZip;
import lime.utils.compress.LZMA;
import lime.utils.compress.Zlib;
import lime.utils.ArrayBuffer;
import lime.utils.ArrayBufferView;
import lime.utils.Bytes;
@@ -207,7 +211,6 @@ import lime.utils.Int16Array;
import lime.utils.Int32Array;
import lime.utils.Int8Array;
import lime.utils.Log;
import lime.utils.LZMA;
import lime.utils.UInt16Array;
import lime.utils.UInt32Array;
import lime.utils.UInt8Array;

View File

@@ -1,51 +1,24 @@
package lime.utils;
import haxe.io.Bytes;
#if !macro
@:build(lime.system.CFFI.build())
#end
import haxe.io.Bytes;
class LZMA {
@:deprecated class LZMA {
public static function decode (bytes:Bytes):Bytes {
#if ((cpp || neko || nodejs) && !macro)
var data:Dynamic = lime_lzma_decode (bytes);
return @:privateAccess new Bytes (data.length, data.b);
#else
return null;
#end
return lime.utils.compress.LZMA.decompress (bytes);
}
public static function encode (bytes:Bytes):Bytes {
#if ((cpp || neko || nodejs) && !macro)
var data:Dynamic = lime_lzma_encode (bytes);
return @:privateAccess new Bytes (data.length, data.b);
#else
return null;
#end
return lime.utils.compress.LZMA.compress (bytes);
}
// Native Methods
#if ((cpp || neko || nodejs) && !macro)
@:cffi private static function lime_lzma_decode (data:Dynamic):Dynamic;
@:cffi private static function lime_lzma_encode (data:Dynamic):Dynamic;
#end
}

View File

@@ -0,0 +1,53 @@
package lime.utils.compress;
import haxe.io.Bytes;
#if !macro
@:build(lime.system.CFFI.build())
#end
class Deflate {
public static function compress (bytes:Bytes):Bytes {
#if ((cpp || neko || nodejs) && !macro)
var data:Dynamic = lime_deflate_compress (bytes);
if (data == null) return null;
return @:privateAccess new Bytes (data.length, data.b);
#else
return null;
#end
}
public static function decompress (bytes:Bytes):Bytes {
#if ((cpp || neko || nodejs) && !macro)
var data:Dynamic = lime_deflate_decompress (bytes);
if (data == null) return null;
return @:privateAccess new Bytes (data.length, data.b);
#else
return null;
#end
}
// Native Methods
#if ((cpp || neko || nodejs) && !macro)
@:cffi private static function lime_deflate_compress (data:Dynamic):Dynamic;
@:cffi private static function lime_deflate_decompress (data:Dynamic):Dynamic;
#end
}

View File

@@ -0,0 +1,51 @@
package lime.utils.compress;
import haxe.io.Bytes;
#if !macro
@:build(lime.system.CFFI.build())
#end
class GZip {
public static function compress (bytes:Bytes):Bytes {
#if ((cpp || neko || nodejs) && !macro)
var data:Dynamic = lime_gzip_compress (bytes);
return @:privateAccess new Bytes (data.length, data.b);
#else
return null;
#end
}
public static function decompress (bytes:Bytes):Bytes {
#if ((cpp || neko || nodejs) && !macro)
var data:Dynamic = lime_gzip_decompress (bytes);
return @:privateAccess new Bytes (data.length, data.b);
#else
return null;
#end
}
// Native Methods
#if ((cpp || neko || nodejs) && !macro)
@:cffi private static function lime_gzip_compress (data:Dynamic):Dynamic;
@:cffi private static function lime_gzip_decompress (data:Dynamic):Dynamic;
#end
}

View File

@@ -0,0 +1,51 @@
package lime.utils.compress;
import haxe.io.Bytes;
#if !macro
@:build(lime.system.CFFI.build())
#end
class LZMA {
public static function compress (bytes:Bytes):Bytes {
#if ((cpp || neko || nodejs) && !macro)
var data:Dynamic = lime_lzma_compress (bytes);
return @:privateAccess new Bytes (data.length, data.b);
#else
return null;
#end
}
public static function decompress (bytes:Bytes):Bytes {
#if ((cpp || neko || nodejs) && !macro)
var data:Dynamic = lime_lzma_decompress (bytes);
return @:privateAccess new Bytes (data.length, data.b);
#else
return null;
#end
}
// Native Methods
#if ((cpp || neko || nodejs) && !macro)
@:cffi private static function lime_lzma_compress (data:Dynamic):Dynamic;
@:cffi private static function lime_lzma_decompress (data:Dynamic):Dynamic;
#end
}

View File

@@ -0,0 +1,51 @@
package lime.utils.compress;
import haxe.io.Bytes;
#if !macro
@:build(lime.system.CFFI.build())
#end
class Zlib {
public static function compress (bytes:Bytes):Bytes {
#if ((cpp || neko || nodejs) && !macro)
var data:Dynamic = lime_zlib_compress (bytes);
return @:privateAccess new Bytes (data.length, data.b);
#else
return null;
#end
}
public static function decompress (bytes:Bytes):Bytes {
#if ((cpp || neko || nodejs) && !macro)
var data:Dynamic = lime_zlib_decompress (bytes);
return @:privateAccess new Bytes (data.length, data.b);
#else
return null;
#end
}
// Native Methods
#if ((cpp || neko || nodejs) && !macro)
@:cffi private static function lime_zlib_compress (data:Dynamic):Dynamic;
@:cffi private static function lime_zlib_decompress (data:Dynamic):Dynamic;
#end
}

View File

@@ -103,7 +103,7 @@
<compilerflag value="-I${NATIVE_TOOLKIT_PATH}/lzma/src" />
<compilerflag value="-DLIME_LZMA" />
<file name="src/utils/LZMA.cpp" />
<file name="src/utils/compress/LZMA.cpp" />
</section>
@@ -206,8 +206,10 @@
<section if="LIME_ZLIB">
<compilerflag value="-DSTATIC_LINK" if="emscripten" />
<compilerflag value="-DLIME_ZLIB" />
<file name="${HXCPP}/project/libs/zlib/ZLib.cpp" if="emscripten || ios || static_link || tvos" />
<file name="src/utils/compress/Zlib.cpp" />
</section>

View File

@@ -1,26 +0,0 @@
#ifndef LIME_UTILS_LZMA_H
#define LIME_UTILS_LZMA_H
#include <utils/Bytes.h>
namespace lime {
class LZMA {
public:
static void Decode (Bytes* data, Bytes* result);
static void Encode (Bytes* data, Bytes* result);
};
}
#endif

View File

@@ -0,0 +1,26 @@
#ifndef LIME_UTILS_COMPRESS_LZMA_H
#define LIME_UTILS_COMPRESS_LZMA_H
#include <utils/Bytes.h>
namespace lime {
class LZMA {
public:
static void Compress (Bytes* data, Bytes* result);
static void Decompress (Bytes* data, Bytes* result);
};
}
#endif

View File

@@ -0,0 +1,35 @@
#ifndef LIME_UTILS_COMPRESS_ZLIB_H
#define LIME_UTILS_COMPRESS_ZLIB_H
#include <utils/Bytes.h>
namespace lime {
enum ZlibType {
DEFLATE,
GZIP,
ZLIB
};
class Zlib {
public:
static void Compress (ZlibType type, Bytes* data, Bytes* result);
static void Decompress (ZlibType type, Bytes* data, Bytes* result);
};
}
#endif

View File

@@ -42,7 +42,8 @@
#include <ui/TouchEvent.h>
#include <ui/Window.h>
#include <ui/WindowEvent.h>
#include <utils/LZMA.h>
#include <utils/compress/LZMA.h>
#include <utils/compress/Zlib.h>
#include <vm/NekoVM.h>
DEFINE_KIND (k_finalizer);
@@ -272,6 +273,32 @@ namespace lime {
}
value lime_deflate_compress (value buffer) {
Bytes data;
data.Set (buffer);
Bytes result;
Zlib::Compress (DEFLATE, &data, &result);
return result.Value ();
}
value lime_deflate_decompress (value buffer) {
Bytes data;
data.Set (buffer);
Bytes result;
Zlib::Decompress (DEFLATE, &data, &result);
return result.Value ();
}
void lime_drop_event_manager_register (value callback, value eventObject) {
DropEvent::callback = new AutoGCRoot (callback);
@@ -630,6 +657,32 @@ namespace lime {
}
value lime_gzip_compress (value buffer) {
Bytes data;
data.Set (buffer);
Bytes result;
Zlib::Compress (GZIP, &data, &result);
return result.Value ();
}
value lime_gzip_decompress (value buffer) {
Bytes data;
data.Set (buffer);
Bytes result;
Zlib::Decompress (GZIP, &data, &result);
return result.Value ();
}
value lime_image_encode (value buffer, int type, int quality) {
ImageBuffer imageBuffer = ImageBuffer (buffer);
@@ -961,14 +1014,14 @@ namespace lime {
}
value lime_lzma_decode (value buffer) {
value lime_lzma_compress (value buffer) {
#ifdef LIME_LZMA
Bytes data;
data.Set (buffer);
Bytes result;
LZMA::Decode (&data, &result);
LZMA::Compress (&data, &result);
return result.Value ();
#else
@@ -978,14 +1031,14 @@ namespace lime {
}
value lime_lzma_encode (value buffer) {
value lime_lzma_decompress (value buffer) {
#ifdef LIME_LZMA
Bytes data;
data.Set (buffer);
Bytes result;
LZMA::Encode (&data, &result);
LZMA::Decompress (&data, &result);
return result.Value ();
#else
@@ -1526,6 +1579,32 @@ namespace lime {
}
value lime_zlib_compress (value buffer) {
Bytes data;
data.Set (buffer);
Bytes result;
Zlib::Compress (ZLIB, &data, &result);
return result.Value ();
}
value lime_zlib_decompress (value buffer) {
Bytes data;
data.Set (buffer);
Bytes result;
Zlib::Decompress (ZLIB, &data, &result);
return result.Value ();
}
DEFINE_PRIME1 (lime_application_create);
DEFINE_PRIME2v (lime_application_event_manager_register);
DEFINE_PRIME1 (lime_application_exec);
@@ -1541,6 +1620,8 @@ namespace lime {
DEFINE_PRIME1 (lime_cffi_set_finalizer);
DEFINE_PRIME0 (lime_clipboard_get_text);
DEFINE_PRIME1v (lime_clipboard_set_text);
DEFINE_PRIME1 (lime_deflate_compress);
DEFINE_PRIME1 (lime_deflate_decompress);
DEFINE_PRIME2v (lime_drop_event_manager_register);
DEFINE_PRIME2 (lime_file_dialog_open_directory);
DEFINE_PRIME2 (lime_file_dialog_open_file);
@@ -1566,6 +1647,8 @@ namespace lime {
DEFINE_PRIME2v (lime_gamepad_event_manager_register);
DEFINE_PRIME1 (lime_gamepad_get_device_guid);
DEFINE_PRIME1 (lime_gamepad_get_device_name);
DEFINE_PRIME1 (lime_gzip_compress);
DEFINE_PRIME1 (lime_gzip_decompress);
DEFINE_PRIME3v (lime_image_data_util_color_transform);
DEFINE_PRIME6v (lime_image_data_util_copy_channel);
DEFINE_PRIME7v (lime_image_data_util_copy_pixels);
@@ -1592,8 +1675,8 @@ namespace lime {
DEFINE_PRIME2 (lime_jpeg_decode_bytes);
DEFINE_PRIME2 (lime_jpeg_decode_file);
DEFINE_PRIME2v (lime_key_event_manager_register);
DEFINE_PRIME1 (lime_lzma_decode);
DEFINE_PRIME1 (lime_lzma_encode);
DEFINE_PRIME1 (lime_lzma_compress);
DEFINE_PRIME1 (lime_lzma_decompress);
DEFINE_PRIME2v (lime_mouse_event_manager_register);
DEFINE_PRIME0v (lime_mouse_hide);
DEFINE_PRIME1v (lime_mouse_set_cursor);
@@ -1649,6 +1732,8 @@ namespace lime {
DEFINE_PRIME2 (lime_window_set_minimized);
DEFINE_PRIME2 (lime_window_set_resizable);
DEFINE_PRIME2 (lime_window_set_title);
DEFINE_PRIME1 (lime_zlib_compress);
DEFINE_PRIME1 (lime_zlib_decompress);
}

View File

@@ -1,4 +1,4 @@
#include <utils/LZMA.h>
#include <utils/compress/LZMA.h>
#include "LzmaEnc.h"
#include "LzmaDec.h"
@@ -32,33 +32,7 @@ namespace lime {
}
void LZMA::Decode (Bytes* data, Bytes* result) {
SizeT inputBufferSize = data->Length ();
Byte* inputBufferData = data->Data ();
Int64 uncompressedLength = -1;
ELzmaStatus status = LZMA_STATUS_NOT_SPECIFIED;
ISzAlloc alloc = { LZMA_alloc, LZMA_free };
CLzmaProps props = { 0 };
LzmaProps_Decode (&props, inputBufferData, LZMA_PROPS_SIZE);
uncompressedLength = READ_LE64 (inputBufferData + LZMA_PROPS_SIZE);
result->Resize ((int)uncompressedLength);
SizeT outputBufferSize = result->Length ();
Byte* outputBufferData = result->Data ();
Byte* _inputBufferData = inputBufferData + LZMA_PROPS_SIZE + sizeof (uncompressedLength);
SizeT _inputBufferSize = inputBufferSize - LZMA_PROPS_SIZE - sizeof (uncompressedLength);
LzmaDecode (outputBufferData, &outputBufferSize, _inputBufferData, &_inputBufferSize, inputBufferData, LZMA_PROPS_SIZE, LZMA_FINISH_ANY, &status, &alloc);
}
void LZMA::Encode (Bytes* data, Bytes* result) {
void LZMA::Compress (Bytes* data, Bytes* result) {
SizeT inputBufferSize = data->Length ();
Byte* inputBufferData = data->Data ();
@@ -94,4 +68,30 @@ namespace lime {
}
void LZMA::Decompress (Bytes* data, Bytes* result) {
SizeT inputBufferSize = data->Length ();
Byte* inputBufferData = data->Data ();
Int64 uncompressedLength = -1;
ELzmaStatus status = LZMA_STATUS_NOT_SPECIFIED;
ISzAlloc alloc = { LZMA_alloc, LZMA_free };
CLzmaProps props = { 0 };
LzmaProps_Decode (&props, inputBufferData, LZMA_PROPS_SIZE);
uncompressedLength = READ_LE64 (inputBufferData + LZMA_PROPS_SIZE);
result->Resize ((int)uncompressedLength);
SizeT outputBufferSize = result->Length ();
Byte* outputBufferData = result->Data ();
Byte* _inputBufferData = inputBufferData + LZMA_PROPS_SIZE + sizeof (uncompressedLength);
SizeT _inputBufferSize = inputBufferSize - LZMA_PROPS_SIZE - sizeof (uncompressedLength);
LzmaDecode (outputBufferData, &outputBufferSize, _inputBufferData, &_inputBufferSize, inputBufferData, LZMA_PROPS_SIZE, LZMA_FINISH_ANY, &status, &alloc);
}
}

View File

@@ -0,0 +1,146 @@
#include <utils/compress/Zlib.h>
#include <zlib.h>
namespace lime {
void Zlib::Compress (ZlibType type, Bytes* data, Bytes* result) {
int windowBits = 15;
switch (type) {
case DEFLATE: windowBits = -15; break;
case GZIP: windowBits = 31; break;
}
z_stream* stream = (z_stream*)malloc (sizeof (z_stream));
stream->zalloc = Z_NULL;
stream->zfree = Z_NULL;
stream->opaque = Z_NULL;
int ret = 0;
if ((ret = deflateInit2 (stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, windowBits, 8, Z_DEFAULT_STRATEGY) != Z_OK)) {
//val_throw (stream->msg);
free (stream);
return;
}
int bufferSize = deflateBound (stream, data->Length ());
char* buffer = (char*)malloc (bufferSize);
stream->next_in = (Bytef*)data->Data ();
stream->next_out = (Bytef*)buffer;
stream->avail_in = data->Length ();
stream->avail_out = bufferSize;
if ((ret = deflate (stream, Z_FINISH)) < 0) {
//val_throw (stream->msg);
deflateEnd (stream);
free (buffer);
return;
}
int size = bufferSize - stream->avail_out;
result->Resize (size);
memcpy (result->Data (), buffer, size);
deflateEnd (stream);
free (buffer);
return;
}
void Zlib::Decompress (ZlibType type, Bytes* data, Bytes* result) {
int windowBits = 15;
switch (type) {
case DEFLATE: windowBits = -15; break;
case GZIP: windowBits = 31; break;
}
z_stream* stream = (z_stream*)malloc (sizeof (z_stream));
stream->zalloc = Z_NULL;
stream->zfree = Z_NULL;
stream->opaque = Z_NULL;
int ret = 0;
if ((ret = inflateInit2 (stream, windowBits) != Z_OK)) {
//val_throw (stream->msg);
inflateEnd (stream);
return;
}
int chunkSize = 1 << 16;
int readSize = 0;
Bytef* sourcePosition = data->Data ();
int destSize = 0;
int readTotal = 0;
Bytef* buffer = (Bytef*)malloc (chunkSize);
stream->avail_in = data->Length ();
stream->next_in = data->Data ();
if (stream->avail_in > 0) {
do {
stream->avail_out = chunkSize;
stream->next_out = buffer;
ret = inflate (stream, Z_NO_FLUSH);
if (ret == Z_STREAM_ERROR) {
inflateEnd (stream);
return;
}
switch (ret) {
case Z_NEED_DICT:
ret = Z_DATA_ERROR;
case Z_DATA_ERROR:
case Z_MEM_ERROR:
inflateEnd (stream);
return;
}
readSize = chunkSize - stream->avail_out;
readTotal += readSize;
result->Resize (readTotal);
memcpy (result->Data () - readSize, buffer, readSize);
sourcePosition += readSize;
} while (stream->avail_out == 0);
}
inflateEnd (stream);
free (buffer);
return;
}
}

BIN
svg.n

Binary file not shown.