This commit is contained in:
Joshua Granick
2015-06-25 14:13:02 -07:00
parent 15fac4d6bd
commit f80adfae95
6 changed files with 187 additions and 34 deletions

View File

@@ -80,13 +80,14 @@ https://github.com/haxenme/nme
legacy/ legacy/
lime/utils/ByteArray.hx lime/utils/ByteArray.hx
lime/utils/JNI.hx lime/utils/JNI.hx
project/include/utils/ByteArray.h project/include/utils/ByteArray.h
project/include/utils/QuickVec.h project/include/utils/QuickVec.h
project/src/graphics/format/ project/src/graphics/format/
project/src/graphics/opengl/ project/src/graphics/opengl/
project/src/text/Font.cpp project/src/text/Font.cpp
project/src/utils/ByteArray.cpp project/src/utils/ByteArray.cpp
project/src/utils/LZMA.cpp
tools/utils/JavaExternGenerator.hx tools/utils/JavaExternGenerator.hx
The following files were developed in collaboration with Sven Bergström The following files were developed in collaboration with Sven Bergström
@@ -95,8 +96,8 @@ details, see https://github.com/underscorediscovery/snow
project/src/audio/openal/OpenALBindings.cpp project/src/audio/openal/OpenALBindings.cpp
The typed array implementation was also developed in collaboration with Sven The typed array implementation was developed in collaboration with Sven Bergström
Bergström and the hxtypedarray project, which is available under an "MIT" license. and the hxtypedarray project, which is available under an "MIT" license.
For details, see https://github.com/underscorediscovery/hxtypedarray For details, see https://github.com/underscorediscovery/hxtypedarray
------- -------

48
lime/utils/LZMA.hx Normal file
View File

@@ -0,0 +1,48 @@
package lime.utils;
import haxe.io.Bytes;
import lime.system.System;
class LZMA {
public static function decode (bytes:ByteArray):ByteArray {
#if (cpp || neko || nodejs)
var data = lime_lzma_decode (bytes);
return ByteArray.fromBytes (@:privateAccess new Bytes (data.length, data.b));
#else
return null;
#end
}
public static function encode (bytes:ByteArray):ByteArray {
#if (cpp || neko || nodejs)
var data = lime_lzma_encode (bytes);
return ByteArray.fromBytes (@:privateAccess new Bytes (data.length, data.b));
#else
return null;
#end
}
// Native Methods
#if (cpp || neko || nodejs)
private static var lime_lzma_decode = System.load ("lime", "lime_lzma_decode", 1);
private static var lime_lzma_encode = System.load ("lime", "lime_lzma_encode", 1);
#end
}

View File

@@ -14,7 +14,7 @@
<set name="LIME_JPEG" value="1" /> <set name="LIME_JPEG" value="1" />
<set name="LIME_FREETYPE" value="1" /> <set name="LIME_FREETYPE" value="1" />
<set name="LIME_HARFBUZZ" value="1" /> <set name="LIME_HARFBUZZ" value="1" />
<!-- <set name="LIME_LZMA" value="1" /> --> <set name="LIME_LZMA" value="1" />
<!-- <set name="LIME_NEKO" value="1" if="linux" /> --> <!-- <set name="LIME_NEKO" value="1" if="linux" /> -->
<set name="LIME_OGG" value="1" /> <set name="LIME_OGG" value="1" />
<set name="LIME_OPENAL" value="1" /> <set name="LIME_OPENAL" value="1" />
@@ -94,8 +94,11 @@
<section if="LIME_LZMA"> <section if="LIME_LZMA">
<compilerflag value="-I${NATIVE_TOOLKIT_PATH}/lzma/src" />
<compilerflag value="-DLIME_LZMA" /> <compilerflag value="-DLIME_LZMA" />
<file name="src/utils/LZMA.cpp" />
</section> </section>
<section if="LIME_NEKO"> <section if="LIME_NEKO">

View File

@@ -0,0 +1,26 @@
#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

@@ -34,12 +34,9 @@
#include <ui/Window.h> #include <ui/Window.h>
#include <ui/WindowEvent.h> #include <ui/WindowEvent.h>
#include <utils/JNI.h> #include <utils/JNI.h>
#include <utils/LZMA.h>
#include <vm/NekoVM.h> #include <vm/NekoVM.h>
#ifdef LIME_LZMA
#include <lzma.h>
#endif
namespace lime { namespace lime {
@@ -675,42 +672,26 @@ namespace lime {
} }
value lime_lzma_decode (value input_value) { value lime_lzma_decode (value buffer) {
#ifdef LIME_LZMA Bytes data = Bytes (buffer);
Bytes result;
buffer input_buffer = val_to_buffer(input_value); LZMA::Decode (&data, &result);
buffer output_buffer = alloc_buffer_len(0);
native_toolkit_lzma::Lzma::Decode (input_buffer, output_buffer); return result.Value ();
return buffer_val (output_buffer);
#else
return alloc_null ();
#endif
} }
value lime_lzma_encode (value input_value) { value lime_lzma_encode (value buffer) {
#ifdef LIME_LZMA Bytes data = Bytes (buffer);
Bytes result;
buffer input_buffer = val_to_buffer(input_value); LZMA::Encode (&data, &result);
buffer output_buffer = alloc_buffer_len(0);
native_toolkit_lzma::Lzma::Encode (input_buffer, output_buffer); return result.Value ();
return buffer_val (output_buffer);
#else
return alloc_null ();
#endif
} }

View File

@@ -0,0 +1,94 @@
#include <utils/LZMA.h>
#include "LzmaEnc.h"
#include "LzmaDec.h"
namespace lime {
inline void WRITE_LE8 (unsigned char *ptr, char value) { *ptr = value; }
inline void WRITE_LE16 (unsigned char *ptr, short value) { WRITE_LE8 ((ptr) + 0, ((value) >> 0)); WRITE_LE8 ((ptr) + 1, ((value) >> 8)); }
inline void WRITE_LE32 (unsigned char *ptr, int value) { WRITE_LE16 ((ptr) + 0, ((value) >> 0)); WRITE_LE16 ((ptr) + 2, ((value) >> 16)); }
inline void WRITE_LE64 (unsigned char *ptr, Int64 value) { WRITE_LE32 ((ptr) + 0, ((value) >> 0)); WRITE_LE32 ((ptr) + 4, ((value) >> 32)); }
inline unsigned char READ_LE8 (unsigned char *ptr) { return *ptr; }
inline unsigned short READ_LE16 (unsigned char *ptr) { return ((unsigned short)READ_LE8 (ptr + 0) << 0) | ((unsigned short)READ_LE8 (ptr + 1) << 8); }
inline unsigned int READ_LE32 (unsigned char *ptr) { return ((unsigned int)READ_LE16 (ptr + 0) << 0) | ((unsigned int)READ_LE16 (ptr + 2) << 16); }
inline UInt64 READ_LE64 (unsigned char *ptr) { return ((UInt64)READ_LE32 (ptr + 0) << 0) | ((UInt64)READ_LE32 (ptr + 4) << 32); }
extern "C" {
SRes LZMA_progress (void *p, UInt64 inSize, UInt64 outSize) { return SZ_OK; }
void *LZMA_alloc (void *p, size_t size) { return malloc (size); }
void LZMA_free(void *p, void *address) {
if (address == NULL) return;
free (address);
}
}
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) {
SizeT inputBufferSize = data->Length ();
Byte* inputBufferData = data->Data ();
result->Resize (inputBufferSize + 1024);
SizeT outputBufferSize = result->Length ();
Byte* outputBufferData = result->Data ();
SizeT propsSize = 100;
Byte* propsData = (Byte *)malloc (propsSize);
Int64 uncompressedLength = inputBufferSize;
CLzmaEncProps props = { 0 };
LzmaEncProps_Init (&props);
props.dictSize = (1 << 20);
props.writeEndMark = 0;
props.numThreads = 1;
ICompressProgress progress = { LZMA_progress };
ISzAlloc allocSmall = { LZMA_alloc, LZMA_free };
ISzAlloc allocBig = { LZMA_alloc, LZMA_free };
LzmaEncode (outputBufferData + propsSize + 8, &outputBufferSize, inputBufferData, inputBufferSize, &props, propsData, &propsSize, props.writeEndMark, &progress, &allocSmall, &allocBig);
memcpy (outputBufferData, propsData, propsSize);
WRITE_LE64 (outputBufferData + propsSize, uncompressedLength);
free(propsData);
}
}