-
Notifications
You must be signed in to change notification settings - Fork 5
/
decode.c
144 lines (127 loc) · 3.72 KB
/
decode.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/****************************************************************************
*
* rg/pkg/lzma/SRC/7zip/Compress/LZMA_C/decode.c
*
* Copyright (C) Jungo LTD 2004
*
* This program is free software; you can redistribute it
* and/or modify it under the terms of the GNU General
* Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
* MA 02111-1307, USA.
*
* Developed by Jungo LTD.
* Residential Gateway Software Division
* www.jungo.com
* info@jungo.com
*/
/* Note: This file is included by the kernel and receives its __OS_*__
* definition from the kernel CFLAGS using -D. This is why we don't
* include rg_os.h.
*/
//#include <linux/types.h>
#include <asm/types.h>
#define u32 __u32
#define u8 __u8
#if defined(__KERNEL__) && !defined(__OS_VXWORKS__)
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/mm.h>
#else
#ifdef __OS_VXWORKS__
#include <kos/kos.h>
#endif
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#endif
#include "LzmaDecode.h"
#include "decode.h"
static u32 lzma_internal_size = 0;
static void *lzma_internal_data = NULL;
#ifdef __LZMA_UNCOMPRESS_KERNEL__
#define LZMA_ERR(args...) puts("LZMA Decode error\n");
#define LZMA_ALLOC(size) malloc(size)
#define LZMA_FREE free
#elif defined(__KERNEL__) && !defined(__OS_VXWORKS__)
#define LZMA_ERR(args...) printk("LZMA decode error: " args)
#define LZMA_ALLOC(size) kmalloc(size, GFP_KERNEL)
#define LZMA_FREE kfree
#else
#define LZMA_ERR(args...) fprintf(stderr, "LZMA decode error: " args)
#define LZMA_ALLOC(size) malloc(size)
#define LZMA_FREE free
#endif
int lzma_decode(void *dst, int dstlen, void *src, int srclen)
{
u8 properties[5], prop0;
u32 out_len = 0;
int i, lc, lp, pb;
int calc_internal_size, res;
/* Compressed buffer's structure is:
* 5 bytes properties
* 4 bytes uncompressed length
* rest is compressed data. */
/* Read properties */
memcpy(properties, src, 5);
src += 5;
/* Read length */
for (i = 0; i < 4; i++)
out_len += (u32)(((u8 *)src)[i]) << (i * 8);
if (out_len > dstlen)
{
LZMA_ERR("Out buffer too small - have %d and need %d\n", dstlen,
out_len);
return -1;
}
src += 4;
srclen -= 9;
prop0 = properties[0];
if (prop0 >= 9 * 5 * 5)
{
LZMA_ERR("Properties Error\n");
return -1;
}
for (pb = 0; prop0 >= 9 * 5; pb++, prop0 -= 9 * 5);
for (lp = 0; prop0 >= 9; lp++, prop0 -= 9);
lc = prop0;
calc_internal_size = (LZMA_BASE_SIZE + (LZMA_LIT_SIZE << (lc + lp))) *
sizeof(CProb);
if (calc_internal_size > lzma_internal_size)
{
if (lzma_internal_data)
LZMA_FREE(lzma_internal_data);
if (!(lzma_internal_data = LZMA_ALLOC(calc_internal_size)))
{
lzma_internal_size = 0;
LZMA_ERR("Error allocating internal data\n");
return -1;
}
lzma_internal_size = calc_internal_size;
}
res = LzmaDecode((u8 *)lzma_internal_data, lzma_internal_size, lc, lp, pb,
(u8 *)src, srclen, (u8 *)dst, out_len, &dstlen);
if (res)
{
LZMA_ERR("Decoder internal error (%d)\n", res);
return -1;
}
return dstlen;
}
void lzma_decode_uninit(void)
{
if (lzma_internal_data)
LZMA_FREE(lzma_internal_data);
lzma_internal_data = NULL;
lzma_internal_size = 0;
}