wok view qemu/stuff/cloop.u @ rev 20074

syslinux: shrink i18n.cfg (again)
author Pascal Bellard <pascal.bellard@slitaz.org>
date Sun Sep 24 21:46:04 2017 +0200 (2017-09-24)
parents 8fe10eb4f215
children 5f55920ffb7e
line source
1 --- block/cloop.c
2 +++ block/cloop.c
3 @@ -29,11 +29,90 @@
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 + if (offsets[i].size & 0x80000000) {
76 + unsigned long k = offsets[i].size & 0x7FFFFFFF;
77 + offsets[i].offset = offsets[k].offset;
78 + offsets[i].size = offsets[k].size;
79 + }
80 + else j += offsets[i].size;
81 + }
82 + }
83 + return 0;
84 +}
85 +
86 typedef struct BDRVCloopState {
87 CoMutex lock;
88 uint32_t block_size;
89 uint32_t n_blocks;
90 - uint64_t *offsets;
91 + block_info *offsets;
92 uint32_t sectors_per_block;
93 uint32_t current_block;
94 uint8_t *compressed_block;
95 @@ -43,17 +117,21 @@
97 static int cloop_probe(const uint8_t *buf, int buf_size, const char *filename)
98 {
99 - const char *magic_version_2_0 = "#!/bin/sh\n"
100 - "#V2.0 Format\n"
101 + static const uint8_t magic[] =
102 "modprobe cloop file=$0 && mount -r -t iso9660 /dev/cloop $1\n";
103 - int length = strlen(magic_version_2_0);
104 - if (length > buf_size) {
105 - length = buf_size;
106 + int i, ret = 0, length = buf_size;
107 + uint8_t c;
108 +
109 + if (length > 127) {
110 + length = 127;
111 }
112 - if (!memcmp(magic_version_2_0, buf, length)) {
113 - return 2;
114 + for (i = 0; i < length - sizeof(magic) + 1; i++) {
115 + if (buf[i] != magic[0]) continue;
116 + if (strncmp(buf + i, magic, sizeof(magic) - 1)) continue;
117 + ret = 2;
118 + break;
119 }
120 - return 0;
121 + return ret;
122 }
124 static int cloop_open(BlockDriverState *bs, QDict *options, int flags,
125 @@ -91,79 +169,97 @@
126 MAX_BLOCK_SIZE / (1024 * 1024));
127 return -EINVAL;
128 }
129 -
130 ret = bdrv_pread(bs->file, 128 + 4, &s->n_blocks, 4);
131 if (ret < 0) {
132 return ret;
133 }
134 s->n_blocks = be32_to_cpu(s->n_blocks);
136 - /* read offsets */
137 - if (s->n_blocks > (UINT32_MAX - 1) / sizeof(uint64_t)) {
138 - /* Prevent integer overflow */
139 - error_setg(errp, "n_blocks %u must be %zu or less",
140 - s->n_blocks,
141 - (UINT32_MAX - 1) / sizeof(uint64_t));
142 - return -EINVAL;
143 - }
144 - offsets_size = (s->n_blocks + 1) * sizeof(uint64_t);
145 - if (offsets_size > 512 * 1024 * 1024) {
146 - /* Prevent ridiculous offsets_size which causes memory allocation to
147 - * fail or overflows bdrv_pread() size. In practice the 512 MB
148 - * offsets[] limit supports 16 TB images at 256 KB block size.
149 - */
150 - error_setg(errp, "image requires too many offsets, "
151 - "try increasing block size");
152 - return -EINVAL;
153 - }
154 - s->offsets = g_malloc(offsets_size);
155 + /* initialize zlib engine */
156 + max_compressed_block_size = s->block_size + s->block_size/1000 + 12 + 4;
157 + s->compressed_block = g_malloc(max_compressed_block_size + 1);
158 + s->uncompressed_block = g_malloc(s->block_size);
160 - ret = bdrv_pread(bs->file, 128 + 4 + 4, s->offsets, offsets_size);
161 - if (ret < 0) {
162 + if (inflateInit(&s->zstream) != Z_OK) {
163 + ret = -EINVAL;
164 goto fail;
165 }
167 - for (i = 0; i < s->n_blocks + 1; i++) {
168 - uint64_t size;
169 + /* read offsets */
170 + if (s->n_blocks + 1 == 0) {
171 + cloop_tail tail;
172 + int64_t end = bdrv_getlength(bs->file);
173 + void *p;
174 + uint32_t toclen, len;
176 - s->offsets[i] = be64_to_cpu(s->offsets[i]);
177 - if (i == 0) {
178 - continue;
179 + ret = bdrv_pread(bs->file, end - sizeof(tail), &tail, sizeof(tail));
180 + if (ret < 0) {
181 + goto fail;
182 }
184 - if (s->offsets[i] < s->offsets[i - 1]) {
185 - error_setg(errp, "offsets not monotonically increasing at "
186 - "index %u, image file is corrupt", i);
187 - ret = -EINVAL;
188 - goto fail;
189 + s->n_blocks = be32_to_cpu(tail.num_blocks);
190 + offsets_size = s->n_blocks * sizeof(block_info);
191 + if (offsets_size > 512 * 1024 * 1024) {
192 + /* Prevent ridiculous offsets_size which causes memory allocation to
193 + * fail or overflows bdrv_pread() size. In practice the 512 MB
194 + * offsets[] limit supports 16 TB images at 256 KB block size.
195 + */
196 + error_setg(errp, "image requires too many offsets, "
197 + "try increasing block size");
198 + return -EINVAL;
199 }
200 + len = be32_to_cpu(tail.table_size);
201 + toclen = (be32_to_cpu(tail.index_size) & 255) * s->n_blocks;
203 - size = s->offsets[i] - s->offsets[i - 1];
204 + s->offsets = g_malloc(offsets_size);
205 + p = g_malloc(len);
207 - /* Compressed blocks should be smaller than the uncompressed block size
208 - * but maybe compression performed poorly so the compressed block is
209 - * actually bigger. Clamp down on unrealistic values to prevent
210 - * ridiculous s->compressed_block allocation.
211 - */
212 - if (size > 2 * MAX_BLOCK_SIZE) {
213 - error_setg(errp, "invalid compressed block size at index %u, "
214 - "image file is corrupt", i);
215 + ret = bdrv_pread(bs->file, end - sizeof(tail) - len, p, len);
216 + if (ret < 0) {
217 + goto fail;
218 + }
219 + s->zstream.next_in = p;
220 + s->zstream.avail_in = len;
221 + s->zstream.next_out = s->offsets;
222 + s->zstream.avail_out = toclen;
223 + ret = inflateReset(&s->zstream);
224 + if (ret != Z_OK) {
225 ret = -EINVAL;
226 goto fail;
227 }
228 -
229 - if (size > max_compressed_block_size) {
230 - max_compressed_block_size = size;
231 + ret = inflate(&s->zstream, Z_FINISH);
232 + if (ret != Z_STREAM_END || s->zstream.total_out != toclen) {
233 + ret = -EINVAL;
234 + goto fail;
235 }
236 + g_free(p);
237 }
238 + else {
239 + offsets_size = s->n_blocks * sizeof(block_info);
240 + if (offsets_size > 512 * 1024 * 1024) {
241 + /* Prevent ridiculous offsets_size which causes memory allocation to
242 + * fail or overflows bdrv_pread() size. In practice the 512 MB
243 + * offsets[] limit supports 16 TB images at 256 KB block size.
244 + */
245 + error_setg(errp, "image requires too many offsets, "
246 + "try increasing block size");
247 + return -EINVAL;
248 + }
249 + s->offsets = g_malloc(offsets_size);
251 - /* initialize zlib engine */
252 - s->compressed_block = g_malloc(max_compressed_block_size + 1);
253 - s->uncompressed_block = g_malloc(s->block_size);
254 - if (inflateInit(&s->zstream) != Z_OK) {
255 + ret = bdrv_pread(bs->file, 128 + 4 + 4, s->offsets, offsets_size);
256 + if (ret < 0) {
257 + goto fail;
258 + }
259 + }
260 + ret = build_index(s->offsets, s->n_blocks);
261 + if (ret) {
262 + error_setg(errp, "invalid compressed block size at index %u, "
263 + "image file is corrupt", ret-1);
264 ret = -EINVAL;
265 goto fail;
266 }
267 +
268 s->current_block = s->n_blocks;
270 s->sectors_per_block = s->block_size/512;
271 @@ -184,10 +280,10 @@
273 if (s->current_block != block_num) {
274 int ret;
275 - uint32_t bytes = s->offsets[block_num + 1] - s->offsets[block_num];
276 + uint32_t bytes = s->offsets[block_num].size;
278 - ret = bdrv_pread(bs->file, s->offsets[block_num], s->compressed_block,
279 - bytes);
280 + ret = bdrv_pread(bs->file, s->offsets[block_num].offset,
281 + s->compressed_block, bytes);
282 if (ret != bytes) {
283 return -1;
284 }