wok-next view qemu/stuff/cloop.u @ rev 18152
alsaplayer: remove too big pixmaps
author | Xander Ziiryanoff <psychomaniak@xakep.ru> |
---|---|
date | Sat Jun 20 19:58:11 2015 +0200 (2015-06-20) |
parents | 9c8ef3fd3dcf |
children | 8fe10eb4f215 |
line source
1 --- block/cloop.c
2 +++ block/cloop.c
3 @@ -29,11 +29,85 @@
4 /* Maximum compressed block size */
5 #define MAX_BLOCK_SIZE (64 * 1024 * 1024)
7 +typedef struct cloop_tail {
8 + uint32_t table_size;
9 + uint32_t index_size;
10 + uint32_t num_blocks;
11 +} cloop_tail;
12 +
13 +typedef struct block_info {
14 + uint64_t offset; /* 64-bit offsets of compressed block */
15 + uint32_t size; /* 32-bit compressed block size */
16 + uint32_t optidx; /* 32-bit index number */
17 +} block_info;
18 +
19 +static inline int build_index(block_info *offsets, unsigned long n)
20 +{
21 + uint32_t *ofs32 = (uint32_t *) offsets;
22 + uint64_t *ofs64 = (uint64_t *) offsets;
23 +
24 + if (ofs32[0] == 0) {
25 + if (ofs32[2]) { /* ACCELERATED KNOPPIX V1.0 */
26 + while (n--) {
27 + offsets[n].offset = be64_to_cpu(offsets[n].offset);
28 + offsets[n].size = ntohl(offsets[n].size);
29 + if (offsets[n].size > 2 * MAX_BLOCK_SIZE)
30 + return n+1;
31 + }
32 + }
33 + else { /* V2.0 */
34 + uint64_t last = be64_to_cpu(ofs64[n - 1]);
35 + while (n--) {
36 + offsets[n].size = last -
37 + (offsets[n].offset = be64_to_cpu(ofs64[n]));
38 + if (offsets[n].size > 2 * MAX_BLOCK_SIZE)
39 + return n+1;
40 + last = offsets[n].offset;
41 + }
42 + }
43 + }
44 + else if (ofs32[1] == 0) { /* V1.0 */
45 + uint64_t last = le64_to_cpu(ofs64[n - 1]);
46 + while (n--) {
47 + offsets[n].size = last -
48 + (offsets[n].offset = le64_to_cpu(ofs64[n]));
49 + if (offsets[n].size > 2 * MAX_BLOCK_SIZE)
50 + return n+1;
51 + last = offsets[n].offset;
52 + }
53 + }
54 + else if (ntohl(ofs32[0]) == (4*n) + 0x8C) { /* V0.68 */
55 + uint64_t last = ntohl(ofs32[n - 1]);
56 + while (n--) {
57 + offsets[n].size = last -
58 + (offsets[n].offset = ntohl(ofs32[n]));
59 + if (offsets[n].size > 2 * MAX_BLOCK_SIZE)
60 + return n+1;
61 + last = offsets[n].offset;
62 + }
63 + }
64 + else { /* V3.0 */
65 + unsigned long i;
66 + uint64_t j;
67 +
68 + for (i = n; i-- > 0; ) {
69 + offsets[i].size = ntohl(ofs32[i]);
70 + if (offsets[i].size > 2 * MAX_BLOCK_SIZE)
71 + return i+1;
72 + }
73 + for (i = 0, j = 128 + 4 + 4; i < n; i++) {
74 + offsets[i].offset = j;
75 + j += offsets[i].size;
76 + }
77 + }
78 + return 0;
79 +}
80 +
81 typedef struct BDRVCloopState {
82 CoMutex lock;
83 uint32_t block_size;
84 uint32_t n_blocks;
85 - uint64_t *offsets;
86 + block_info *offsets;
87 uint32_t sectors_per_block;
88 uint32_t current_block;
89 uint8_t *compressed_block;
90 @@ -43,17 +117,21 @@
92 static int cloop_probe(const uint8_t *buf, int buf_size, const char *filename)
93 {
94 - const char *magic_version_2_0 = "#!/bin/sh\n"
95 - "#V2.0 Format\n"
96 + static const uint8_t magic[] =
97 "modprobe cloop file=$0 && mount -r -t iso9660 /dev/cloop $1\n";
98 - int length = strlen(magic_version_2_0);
99 - if (length > buf_size) {
100 - length = buf_size;
101 + int i, ret = 0, length = buf_size;
102 + uint8_t c;
103 +
104 + if (length > 127) {
105 + length = 127;
106 }
107 - if (!memcmp(magic_version_2_0, buf, length)) {
108 - return 2;
109 + for (i = 0; i < length - sizeof(magic) + 1; i++) {
110 + if (buf[i] != magic[0]) continue;
111 + if (strncmp(buf + i, magic, sizeof(magic) - 1)) continue;
112 + ret = 2;
113 + break;
114 }
115 - return 0;
116 + return ret;
117 }
119 static int cloop_open(BlockDriverState *bs, QDict *options, int flags,
120 @@ -91,79 +169,97 @@
121 MAX_BLOCK_SIZE / (1024 * 1024));
122 return -EINVAL;
123 }
124 -
125 ret = bdrv_pread(bs->file, 128 + 4, &s->n_blocks, 4);
126 if (ret < 0) {
127 return ret;
128 }
129 s->n_blocks = be32_to_cpu(s->n_blocks);
131 - /* read offsets */
132 - if (s->n_blocks > (UINT32_MAX - 1) / sizeof(uint64_t)) {
133 - /* Prevent integer overflow */
134 - error_setg(errp, "n_blocks %u must be %zu or less",
135 - s->n_blocks,
136 - (UINT32_MAX - 1) / sizeof(uint64_t));
137 - return -EINVAL;
138 - }
139 - offsets_size = (s->n_blocks + 1) * sizeof(uint64_t);
140 - if (offsets_size > 512 * 1024 * 1024) {
141 - /* Prevent ridiculous offsets_size which causes memory allocation to
142 - * fail or overflows bdrv_pread() size. In practice the 512 MB
143 - * offsets[] limit supports 16 TB images at 256 KB block size.
144 - */
145 - error_setg(errp, "image requires too many offsets, "
146 - "try increasing block size");
147 - return -EINVAL;
148 - }
149 - s->offsets = g_malloc(offsets_size);
150 + /* initialize zlib engine */
151 + max_compressed_block_size = s->block_size + s->block_size/1000 + 12 + 4;
152 + s->compressed_block = g_malloc(max_compressed_block_size + 1);
153 + s->uncompressed_block = g_malloc(s->block_size);
155 - ret = bdrv_pread(bs->file, 128 + 4 + 4, s->offsets, offsets_size);
156 - if (ret < 0) {
157 + if (inflateInit(&s->zstream) != Z_OK) {
158 + ret = -EINVAL;
159 goto fail;
160 }
162 - for (i = 0; i < s->n_blocks + 1; i++) {
163 - uint64_t size;
164 + /* read offsets */
165 + if (s->n_blocks + 1 == 0) {
166 + cloop_tail tail;
167 + int64_t end = bdrv_getlength(bs->file);
168 + void *p;
169 + uint32_t toclen, len;
171 - s->offsets[i] = be64_to_cpu(s->offsets[i]);
172 - if (i == 0) {
173 - continue;
174 + ret = bdrv_pread(bs->file, end - sizeof(tail), &tail, sizeof(tail));
175 + if (ret < 0) {
176 + goto fail;
177 }
179 - if (s->offsets[i] < s->offsets[i - 1]) {
180 - error_setg(errp, "offsets not monotonically increasing at "
181 - "index %u, image file is corrupt", i);
182 - ret = -EINVAL;
183 - goto fail;
184 + s->n_blocks = be32_to_cpu(tail.num_blocks);
185 + offsets_size = s->n_blocks * sizeof(block_info);
186 + if (offsets_size > 512 * 1024 * 1024) {
187 + /* Prevent ridiculous offsets_size which causes memory allocation to
188 + * fail or overflows bdrv_pread() size. In practice the 512 MB
189 + * offsets[] limit supports 16 TB images at 256 KB block size.
190 + */
191 + error_setg(errp, "image requires too many offsets, "
192 + "try increasing block size");
193 + return -EINVAL;
194 }
195 + len = be32_to_cpu(tail.table_size);
196 + toclen = (be32_to_cpu(tail.index_size) & 255) * s->n_blocks;
198 - size = s->offsets[i] - s->offsets[i - 1];
199 + s->offsets = g_malloc(offsets_size);
200 + p = g_malloc(len);
202 - /* Compressed blocks should be smaller than the uncompressed block size
203 - * but maybe compression performed poorly so the compressed block is
204 - * actually bigger. Clamp down on unrealistic values to prevent
205 - * ridiculous s->compressed_block allocation.
206 - */
207 - if (size > 2 * MAX_BLOCK_SIZE) {
208 - error_setg(errp, "invalid compressed block size at index %u, "
209 - "image file is corrupt", i);
210 + ret = bdrv_pread(bs->file, end - sizeof(tail) - len, p, len);
211 + if (ret < 0) {
212 + goto fail;
213 + }
214 + s->zstream.next_in = p;
215 + s->zstream.avail_in = len;
216 + s->zstream.next_out = s->offsets;
217 + s->zstream.avail_out = toclen;
218 + ret = inflateReset(&s->zstream);
219 + if (ret != Z_OK) {
220 ret = -EINVAL;
221 goto fail;
222 }
223 -
224 - if (size > max_compressed_block_size) {
225 - max_compressed_block_size = size;
226 + ret = inflate(&s->zstream, Z_FINISH);
227 + if (ret != Z_STREAM_END || s->zstream.total_out != toclen) {
228 + ret = -EINVAL;
229 + goto fail;
230 }
231 + g_free(p);
232 }
233 + else {
234 + offsets_size = s->n_blocks * sizeof(block_info);
235 + if (offsets_size > 512 * 1024 * 1024) {
236 + /* Prevent ridiculous offsets_size which causes memory allocation to
237 + * fail or overflows bdrv_pread() size. In practice the 512 MB
238 + * offsets[] limit supports 16 TB images at 256 KB block size.
239 + */
240 + error_setg(errp, "image requires too many offsets, "
241 + "try increasing block size");
242 + return -EINVAL;
243 + }
244 + s->offsets = g_malloc(offsets_size);
246 - /* initialize zlib engine */
247 - s->compressed_block = g_malloc(max_compressed_block_size + 1);
248 - s->uncompressed_block = g_malloc(s->block_size);
249 - if (inflateInit(&s->zstream) != Z_OK) {
250 + ret = bdrv_pread(bs->file, 128 + 4 + 4, s->offsets, offsets_size);
251 + if (ret < 0) {
252 + goto fail;
253 + }
254 + }
255 + ret = build_index(s->offsets, s->n_blocks);
256 + if (ret) {
257 + error_setg(errp, "invalid compressed block size at index %u, "
258 + "image file is corrupt", ret-1);
259 ret = -EINVAL;
260 goto fail;
261 }
262 +
263 s->current_block = s->n_blocks;
265 s->sectors_per_block = s->block_size/512;
266 @@ -184,10 +280,10 @@
268 if (s->current_block != block_num) {
269 int ret;
270 - uint32_t bytes = s->offsets[block_num + 1] - s->offsets[block_num];
271 + uint32_t bytes = s->offsets[block_num].size;
273 - ret = bdrv_pread(bs->file, s->offsets[block_num], s->compressed_block,
274 - bytes);
275 + ret = bdrv_pread(bs->file, s->offsets[block_num].offset,
276 + s->compressed_block, bytes);
277 if (ret != bytes) {
278 return -1;
279 }