1 /*
2 * Wrapper for decompressing LZ4-compressed kernel, initramfs, and initrd
3 *
4 * Copyright (C) 2013, LG Electronics, Kyungsik Lee <kyungsik.lee@lge.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
11 #include "decompress.h"
12 #include <xen/lz4.h>
13 #include "lz4/decompress.c"
14
15 /*
16 * Note: Uncompressed chunk size is used in the compressor side
17 * (userspace side for compression).
18 * It is hardcoded because there is not proper way to extract it
19 * from the binary stream which is generated by the preliminary
20 * version of LZ4 tool so far.
21 */
22 #define LZ4_DEFAULT_UNCOMPRESSED_CHUNK_SIZE (8 << 20)
23 #define ARCHIVE_MAGICNUMBER 0x184C2102
24
unlz4(unsigned char * input,unsigned int in_len,int (* fill)(void *,unsigned int),int (* flush)(void *,unsigned int),unsigned char * output,unsigned int * posp,void (* error)(const char * x))25 STATIC int INIT unlz4(unsigned char *input, unsigned int in_len,
26 int (*fill)(void *, unsigned int),
27 int (*flush)(void *, unsigned int),
28 unsigned char *output,
29 unsigned int *posp,
30 void (*error)(const char *x))
31 {
32 int ret = -1;
33 size_t chunksize = 0;
34 size_t uncomp_chunksize = LZ4_DEFAULT_UNCOMPRESSED_CHUNK_SIZE;
35 u8 *inp;
36 u8 *inp_start;
37 u8 *outp;
38 int size = in_len -= 4;
39 #if defined(__XEN__) || defined(__MINIOS__)
40 size_t out_len = get_unaligned_le32(input + in_len);
41 #endif
42 size_t dest_len;
43
44
45 if (output) {
46 outp = output;
47 } else if (!flush) {
48 error("NULL output pointer and no flush function provided");
49 goto exit_0;
50 } else {
51 outp = large_malloc(uncomp_chunksize);
52 if (!outp) {
53 error("Could not allocate output buffer");
54 goto exit_0;
55 }
56 }
57
58 if (input && fill) {
59 error("Both input pointer and fill function provided,");
60 goto exit_1;
61 } else if (input) {
62 inp = input;
63 } else if (!fill) {
64 error("NULL input pointer and missing fill function");
65 goto exit_1;
66 } else {
67 inp = large_malloc(lz4_compressbound(uncomp_chunksize));
68 if (!inp) {
69 error("Could not allocate input buffer");
70 goto exit_1;
71 }
72 }
73 inp_start = inp;
74
75 if (posp)
76 *posp = 0;
77
78 if (fill)
79 fill(inp, 4);
80
81 chunksize = get_unaligned_le32(inp);
82 if (chunksize == ARCHIVE_MAGICNUMBER) {
83 inp += 4;
84 size -= 4;
85 } else {
86 error("invalid header");
87 goto exit_2;
88 }
89
90 if (posp)
91 *posp += 4;
92
93 for (;;) {
94
95 if (fill)
96 fill(inp, 4);
97
98 chunksize = get_unaligned_le32(inp);
99 if (chunksize == ARCHIVE_MAGICNUMBER) {
100 inp += 4;
101 size -= 4;
102 if (posp)
103 *posp += 4;
104 continue;
105 }
106 inp += 4;
107 size -= 4;
108
109 if (posp)
110 *posp += 4;
111
112 if (fill) {
113 if (chunksize > lz4_compressbound(uncomp_chunksize)) {
114 error("chunk length is longer than allocated");
115 goto exit_2;
116 }
117 fill(inp, chunksize);
118 }
119 #if defined(__XEN__) || defined(__MINIOS__)
120 if (out_len >= uncomp_chunksize) {
121 dest_len = uncomp_chunksize;
122 out_len -= dest_len;
123 } else
124 dest_len = out_len;
125 ret = lz4_decompress(inp, &chunksize, outp, dest_len);
126 #else
127 dest_len = uncomp_chunksize;
128 ret = lz4_decompress_unknownoutputsize(inp, chunksize, outp,
129 &dest_len);
130 #endif
131 if (ret < 0) {
132 error("Decoding failed");
133 goto exit_2;
134 }
135
136 ret = -1;
137 if (flush && flush(outp, dest_len) != dest_len)
138 goto exit_2;
139 if (output)
140 outp += dest_len;
141 if (posp)
142 *posp += chunksize;
143
144 size -= chunksize;
145
146 if (size == 0)
147 break;
148 else if (size < 0) {
149 error("data corrupted");
150 goto exit_2;
151 }
152
153 inp += chunksize;
154 if (fill)
155 inp = inp_start;
156 }
157
158 ret = 0;
159 exit_2:
160 if (!input)
161 large_free(inp_start);
162 exit_1:
163 if (!output)
164 large_free(outp);
165 exit_0:
166 return ret;
167 }
168