diff --git a/docs/ImportAll.hx b/docs/ImportAll.hx index b2a4ab5a1..f94966deb 100644 --- a/docs/ImportAll.hx +++ b/docs/ImportAll.hx @@ -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; diff --git a/lime/utils/LZMA.hx b/lime/utils/LZMA.hx index cbe54d145..a0b810b34 100644 --- a/lime/utils/LZMA.hx +++ b/lime/utils/LZMA.hx @@ -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 - - } \ No newline at end of file diff --git a/lime/utils/compress/Deflate.hx b/lime/utils/compress/Deflate.hx new file mode 100644 index 000000000..134649fb0 --- /dev/null +++ b/lime/utils/compress/Deflate.hx @@ -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 + + +} \ No newline at end of file diff --git a/lime/utils/compress/GZip.hx b/lime/utils/compress/GZip.hx new file mode 100644 index 000000000..6c7c9389e --- /dev/null +++ b/lime/utils/compress/GZip.hx @@ -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 + + +} \ No newline at end of file diff --git a/lime/utils/compress/LZMA.hx b/lime/utils/compress/LZMA.hx new file mode 100644 index 000000000..cc99ea7e4 --- /dev/null +++ b/lime/utils/compress/LZMA.hx @@ -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 + + +} \ No newline at end of file diff --git a/lime/utils/compress/Zlib.hx b/lime/utils/compress/Zlib.hx new file mode 100644 index 000000000..811922d20 --- /dev/null +++ b/lime/utils/compress/Zlib.hx @@ -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 + + +} \ No newline at end of file diff --git a/project/Build.xml b/project/Build.xml index 4e3a02c0c..2ee45b480 100644 --- a/project/Build.xml +++ b/project/Build.xml @@ -103,7 +103,7 @@ - + @@ -206,8 +206,10 @@
+ +
diff --git a/project/include/utils/LZMA.h b/project/include/utils/LZMA.h deleted file mode 100644 index 301ac2b34..000000000 --- a/project/include/utils/LZMA.h +++ /dev/null @@ -1,26 +0,0 @@ -#ifndef LIME_UTILS_LZMA_H -#define LIME_UTILS_LZMA_H - - -#include - - -namespace lime { - - - class LZMA { - - - public: - - static void Decode (Bytes* data, Bytes* result); - static void Encode (Bytes* data, Bytes* result); - - - }; - - -} - - -#endif \ No newline at end of file diff --git a/project/include/utils/compress/LZMA.h b/project/include/utils/compress/LZMA.h new file mode 100644 index 000000000..11bb11b48 --- /dev/null +++ b/project/include/utils/compress/LZMA.h @@ -0,0 +1,26 @@ +#ifndef LIME_UTILS_COMPRESS_LZMA_H +#define LIME_UTILS_COMPRESS_LZMA_H + + +#include + + +namespace lime { + + + class LZMA { + + + public: + + static void Compress (Bytes* data, Bytes* result); + static void Decompress (Bytes* data, Bytes* result); + + + }; + + +} + + +#endif \ No newline at end of file diff --git a/project/include/utils/compress/Zlib.h b/project/include/utils/compress/Zlib.h new file mode 100644 index 000000000..0d95818f7 --- /dev/null +++ b/project/include/utils/compress/Zlib.h @@ -0,0 +1,35 @@ +#ifndef LIME_UTILS_COMPRESS_ZLIB_H +#define LIME_UTILS_COMPRESS_ZLIB_H + + +#include + + +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 \ No newline at end of file diff --git a/project/src/ExternalInterface.cpp b/project/src/ExternalInterface.cpp index aaeabaa24..388c92292 100644 --- a/project/src/ExternalInterface.cpp +++ b/project/src/ExternalInterface.cpp @@ -42,7 +42,8 @@ #include #include #include -#include +#include +#include #include 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); } diff --git a/project/src/utils/LZMA.cpp b/project/src/utils/compress/LZMA.cpp similarity index 96% rename from project/src/utils/LZMA.cpp rename to project/src/utils/compress/LZMA.cpp index c0933ba96..661d56c05 100644 --- a/project/src/utils/LZMA.cpp +++ b/project/src/utils/compress/LZMA.cpp @@ -1,4 +1,4 @@ -#include +#include #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); + + } + + } \ No newline at end of file diff --git a/project/src/utils/compress/Zlib.cpp b/project/src/utils/compress/Zlib.cpp new file mode 100644 index 000000000..b7f3ad801 --- /dev/null +++ b/project/src/utils/compress/Zlib.cpp @@ -0,0 +1,146 @@ +#include +#include + + +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; + + } + + +} \ No newline at end of file diff --git a/svg.n b/svg.n index bb88b8a21..6337b0f25 100644 Binary files a/svg.n and b/svg.n differ