Subversion Repositories spk

Rev

Rev 1 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 cycrow 1
#include "7Decoder.h"
2
#include "LzmaDecode.h"
3
 
4
#include <stdlib.h>
5
#include <stdio.h>
6
#include <string.h>
7
 
8
unsigned char *LZMADecode_C ( unsigned char *inBuffer, size_t inSize, size_t *outSizeProcessed, int *result )
9
{
10
	const UInt32 kPropertiesSize = 5;
11
	Byte *properties;
12
 
13
    size_t inProcessed;
14
    CLzmaDecoderState state; 
15
//    int result;
16
	size_t outSizeProcessedLoc;
17
	size_t outSize = 0;
18
	Byte *outBuffer = NULL;
19
	int i = 0;
20
 
21
	properties = (Byte *)malloc(sizeof(Byte *) * kPropertiesSize);
22
	*outSizeProcessed = 0;
23
 
24
	// read properties
25
	memcpy ( properties, inBuffer, 5 );
26
	inSize -= 5;
27
	inBuffer += 5;
28
 
29
	// read size
30
	for ( i = 0; i < 8; i++ )
31
	{
32
		Byte b = inBuffer[0];
33
		inBuffer++;
34
		inSize--;
35
 
36
		outSize |= ((size_t)b) << (8 * i);
37
	}
38
 
39
	outBuffer = (Byte *)malloc ( sizeof(Byte) * outSize );
40
 
41
 
42
    if ( LzmaDecodeProperties ( &state.Properties, properties, kPropertiesSize ) != LZMA_RESULT_OK )
43
	{
44
		if ( result )
45
			*result = SZE_FAIL;
46
		return NULL;
47
	}
48
 
49
    state.Probs = (CProb *)malloc ( LzmaGetNumProbs ( &state.Properties ) * sizeof (CProb) );
50
    if (state.Probs == 0)
51
	{
52
		if ( result )
53
			*result = SZE_OUTOFMEMORY;
54
		return NULL;
55
	}
56
 
57
    int r = LzmaDecode(&state,
58
        inBuffer, inSize, &inProcessed,
59
        outBuffer, outSize, &outSizeProcessedLoc);
60
 
61
    *outSizeProcessed = (size_t)outSizeProcessedLoc;
62
	free ( state.Probs );
63
 
64
    if ( r == LZMA_RESULT_DATA_ERROR )
65
	{
66
		if ( result )
67
			*result = SZE_DATA_ERROR;
68
		return NULL;
69
	}
70
    if ( r != LZMA_RESULT_OK )
71
	{
72
		if ( result )
73
			*result = SZE_FAIL;
74
		return NULL;
75
	}
76
 
77
	if ( result )
78
		*result = SZ_OK;
79
	return outBuffer;
80
}