| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
#include <lzma.h> |
| 13 |
#include <stdlib.h> |
| 14 |
#include <stdio.h> |
| 15 |
#include <string.h> |
| 16 |
#include "lzmadecode.c" |
| 17 |
|
| 18 |
unsigned long ulzman(const unsigned char *src, unsigned long srcn, |
| 19 |
unsigned char *dst, unsigned long dstn) |
| 20 |
{ |
| 21 |
unsigned char properties[LZMA_PROPERTIES_SIZE]; |
| 22 |
const int data_offset = LZMA_PROPERTIES_SIZE + 8; |
| 23 |
UInt32 outSize; |
| 24 |
SizeT inProcessed; |
| 25 |
SizeT outProcessed; |
| 26 |
int res; |
| 27 |
CLzmaDecoderState state; |
| 28 |
SizeT mallocneeds; |
| 29 |
unsigned char *scratchpad; |
| 30 |
|
| 31 |
if (srcn < data_offset) { |
| 32 |
printf("lzma: Input too small.\n"); |
| 33 |
return 0; |
| 34 |
} |
| 35 |
|
| 36 |
memcpy(properties, src, LZMA_PROPERTIES_SIZE); |
| 37 |
memcpy(&outSize, src + LZMA_PROPERTIES_SIZE, sizeof(outSize)); |
| 38 |
if (outSize > dstn) |
| 39 |
outSize = dstn; |
| 40 |
if (LzmaDecodeProperties(&state.Properties, properties, |
| 41 |
LZMA_PROPERTIES_SIZE) != LZMA_RESULT_OK) { |
| 42 |
printf("lzma: Incorrect stream properties.\n"); |
| 43 |
return 0; |
| 44 |
} |
| 45 |
mallocneeds = (LzmaGetNumProbs(&state.Properties) * sizeof(CProb)); |
| 46 |
scratchpad = malloc(mallocneeds); |
| 47 |
if (!scratchpad) { |
| 48 |
printf("lzma: Cannot allocate %u bytes for scratchpad!\n", |
| 49 |
mallocneeds); |
| 50 |
return 0; |
| 51 |
} |
| 52 |
state.Probs = (CProb *)scratchpad; |
| 53 |
res = LzmaDecode(&state, src + data_offset, srcn - data_offset, |
| 54 |
&inProcessed, dst, outSize, &outProcessed); |
| 55 |
free(scratchpad); |
| 56 |
if (res != 0) { |
| 57 |
printf("lzma: Decoding error = %d\n", res); |
| 58 |
return 0; |
| 59 |
} |
| 60 |
return outProcessed; |
| 61 |
} |
| 62 |
|
| 63 |
unsigned long ulzma(const unsigned char *src, unsigned char *dst) |
| 64 |
{ |
| 65 |
return ulzman(src, (unsigned long)(-1), dst, (unsigned long)(-1)); |
| 66 |
} |