+/* ---------------------------- lzma reader -------------------------- */
+typedef struct
+{
+ reader_t*input;
+ CLzmaDecoderState state;
+ unsigned char*mem;
+ int pos;
+ int len;
+ int lzmapos;
+ int available;
+} lzma_t;
+
+static void reader_lzma_dealloc(reader_t*reader)
+{
+ lzma_t*i = (lzma_t*)reader->internal;
+ free(i->state.Probs);i->state.Probs = 0;
+ free(i->state.Dictionary);i->state.Dictionary = 0;
+ free(reader->internal);reader->internal=0;
+}
+
+static int reader_lzma_read(reader_t*reader, void*data, int len)
+{
+ lzma_t*i = (lzma_t*)reader->internal;
+
+ SizeT processed = 0;
+ if(len>i->available)
+ len = i->available;
+ int ret = LzmaDecode(&i->state,
+ &i->mem[i->pos], i->len-i->pos, &i->lzmapos,
+ data, len, &processed);
+ i->available -= processed;
+ i->pos += i->lzmapos;
+ return processed;
+}
+
+reader_t* reader_init_lzma(void*mem, int len)
+{
+ reader_t*r = malloc(sizeof(reader_t));
+ memset(r, 0, sizeof(reader_t));
+
+ lzma_t*i = (lzma_t*)malloc(sizeof(lzma_t));
+ memset(i, 0, sizeof(lzma_t));
+ r->internal = i;
+ r->read = reader_lzma_read;
+ r->dealloc = reader_lzma_dealloc;
+ r->pos = 0;
+
+ i->mem = mem;
+ i->len = len;
+ i->lzmapos = 0;
+
+ if(LzmaDecodeProperties(&i->state.Properties, mem, LZMA_PROPERTIES_SIZE)) {
+ printf("Couldn't decode properties\n");
+ return 0;
+ }
+ i->pos += LZMA_PROPERTIES_SIZE;
+
+ unsigned char*l = &i->mem[i->pos];
+ i->available = (long long)l[0] | (long long)l[1]<<8 | (long long)l[2]<<16 | (long long)l[3]<<24 |
+ (long long)l[4]<<32 | (long long)l[5]<<40 | (long long)l[6]<<48 | (long long)l[7]<<56;
+ i->pos += 8; //uncompressed size