1
2
3
4
5
6
7
8#include "elf.h"
9#include "page.h"
10#include "string.h"
11#include "stdio.h"
12#include "ops.h"
13#include "reg.h"
14#include "types.h"
15
16
17
18
19
20
21
22#define STATIC static
23#define INIT
24
25
26
27
28
29
30
31#ifdef CONFIG_KERNEL_GZIP
32# include "decompress_inflate.c"
33#endif
34
35#ifdef CONFIG_KERNEL_XZ
36# include "xz_config.h"
37# include "../../../lib/decompress_unxz.c"
38#endif
39
40
41static unsigned long decompressed_bytes;
42static unsigned long limit;
43static unsigned long skip;
44static char *output_buffer;
45
46
47
48
49
50static long flush(void *v, unsigned long buffer_size)
51{
52 unsigned long end = decompressed_bytes + buffer_size;
53 unsigned long size = buffer_size;
54 unsigned long offset = 0;
55 char *in = v;
56 char *out;
57
58
59
60
61
62 if (decompressed_bytes >= limit)
63 return -1;
64
65
66 if (end <= skip) {
67 decompressed_bytes += buffer_size;
68 return buffer_size;
69 }
70
71
72 if (decompressed_bytes < skip && end > skip) {
73 offset = skip - decompressed_bytes;
74
75 in += offset;
76 size -= offset;
77 decompressed_bytes += offset;
78 }
79
80 out = &output_buffer[decompressed_bytes - skip];
81 size = min(decompressed_bytes + size, limit) - decompressed_bytes;
82
83 memcpy(out, in, size);
84 decompressed_bytes += size;
85
86 return buffer_size;
87}
88
89static void print_err(char *s)
90{
91
92 if (decompressed_bytes >= limit)
93 return;
94
95 printf("Decompression error: '%s'\n\r", s);
96}
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116long partial_decompress(void *inbuf, unsigned long input_size,
117 void *outbuf, unsigned long output_size, unsigned long _skip)
118{
119 int ret;
120
121
122
123
124
125 output_size += _skip;
126
127 decompressed_bytes = 0;
128 output_buffer = outbuf;
129 limit = output_size;
130 skip = _skip;
131
132 ret = __decompress(inbuf, input_size, NULL, flush, outbuf,
133 output_size, NULL, print_err);
134
135
136
137
138
139 if (decompressed_bytes < limit)
140 return ret;
141
142 return decompressed_bytes - skip;
143}
144