2016-11-29 16:53:39 +00:00
|
|
|
/* --- Code for inlining --- */
|
|
|
|
|
2017-07-02 13:23:33 +00:00
|
|
|
#ifndef UNCOMPRESSED
|
2016-11-29 16:53:39 +00:00
|
|
|
#include "LzmaDec.h"
|
|
|
|
|
|
|
|
static void *_lzalloc(void *p, size_t size) { p = p; return malloc(size); }
|
|
|
|
static void _lzfree(void *p, void *address) { p = p; free(address); }
|
|
|
|
static ISzAlloc _lzallocator = { _lzalloc, _lzfree };
|
2017-07-02 13:23:33 +00:00
|
|
|
#define lzmafree(x) free(x)
|
|
|
|
#else
|
|
|
|
#define lzmafree(x) do {} while (0)
|
|
|
|
#endif
|
2016-11-29 16:53:39 +00:00
|
|
|
|
|
|
|
static void *lzmaunpack(const char *data, size_t size, size_t *puncompressed_size) {
|
2017-04-03 16:40:20 +00:00
|
|
|
unsigned char *uncompressed = NULL;
|
2016-11-29 16:53:39 +00:00
|
|
|
size_t uncompressed_size = 0;
|
|
|
|
|
2017-07-02 13:23:33 +00:00
|
|
|
#ifndef UNCOMPRESSED
|
2017-04-03 16:40:20 +00:00
|
|
|
const Byte *wheader = (Byte *) data + sizeof(unsigned int);
|
|
|
|
const Byte *woheader = (Byte *) wheader + LZMA_PROPS_SIZE;
|
2016-11-29 16:53:39 +00:00
|
|
|
|
|
|
|
ELzmaStatus status;
|
2017-07-02 13:23:33 +00:00
|
|
|
|
2016-11-29 16:53:39 +00:00
|
|
|
size_t srcLen;
|
|
|
|
int res;
|
2017-07-02 13:23:33 +00:00
|
|
|
#endif
|
2016-11-29 16:53:39 +00:00
|
|
|
|
|
|
|
union {
|
|
|
|
unsigned int l;
|
|
|
|
unsigned char c[4];
|
|
|
|
} x;
|
|
|
|
|
|
|
|
x.c[3] = data[0];
|
|
|
|
x.c[2] = data[1];
|
|
|
|
x.c[1] = data[2];
|
|
|
|
x.c[0] = data[3];
|
|
|
|
|
|
|
|
uncompressed_size = x.l;
|
|
|
|
|
2017-07-02 13:23:33 +00:00
|
|
|
#ifndef UNCOMPRESSED
|
2016-11-29 16:53:39 +00:00
|
|
|
uncompressed = malloc(uncompressed_size);
|
|
|
|
if (!uncompressed) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
srcLen = size - sizeof(unsigned int) - LZMA_PROPS_SIZE;
|
|
|
|
|
|
|
|
res = LzmaDecode(
|
|
|
|
uncompressed, &uncompressed_size, woheader, &srcLen, wheader,
|
|
|
|
LZMA_PROPS_SIZE, LZMA_FINISH_ANY, &status, &_lzallocator
|
|
|
|
);
|
|
|
|
|
|
|
|
if (res != SZ_OK) {
|
|
|
|
free(uncompressed);
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-07-02 13:23:33 +00:00
|
|
|
#else
|
|
|
|
uncompressed = data + sizeof(unsigned int);
|
|
|
|
#endif
|
2016-11-29 16:53:39 +00:00
|
|
|
|
|
|
|
if (puncompressed_size) {
|
|
|
|
*puncompressed_size = uncompressed_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
return uncompressed;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *PyObject_lzmaunpack(const char *data, size_t size) {
|
|
|
|
PyObject * object;
|
|
|
|
size_t uncompressed_size = 0;
|
|
|
|
void *uncompressed = lzmaunpack(data, size, &uncompressed_size);
|
|
|
|
if (!uncompressed) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
object = PyMarshal_ReadObjectFromString(
|
|
|
|
uncompressed, uncompressed_size);
|
|
|
|
|
2017-07-02 13:23:33 +00:00
|
|
|
lzmafree(uncompressed);
|
2016-11-29 16:53:39 +00:00
|
|
|
return object;
|
|
|
|
}
|