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

Up: patch 2.7.4
author Alexander Medvedev <devl547@gmail.com>
date Sun Feb 08 22:12:39 2015 +0000 (2015-02-08)
parents 1f00b27b3afb
children 9c8ef3fd3dcf
line source
1 --- block/cloop.c
2 +++ block/cloop.c
3 @@ -26,11 +26,78 @@
4 #include "qemu/module.h"
5 #include <zlib.h>
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 void 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 + }
30 + // return (char *) "128BE accelerated knoppix 1.0";
31 + }
32 + else { /* V2.0 */
33 + uint64_t last = be64_to_cpu(ofs64[n - 1]);
34 + while (n--) {
35 + offsets[n].size = last -
36 + (offsets[n].offset = be64_to_cpu(ofs64[n]));
37 + last = offsets[n].offset;
38 + }
39 + // return (char *) "64BE v2.0";
40 + }
41 + }
42 + else if (ofs32[1] == 0) { /* V1.0 */
43 + uint64_t last = le64_to_cpu(ofs64[n - 1]);
44 + while (n--) {
45 + offsets[n].size = last -
46 + (offsets[n].offset = le64_to_cpu(ofs64[n]));
47 + last = offsets[n].offset;
48 + }
49 + // return (char *) "64LE v1.0";
50 + }
51 + else if (ntohl(ofs32[0]) == (4*n) + 0x8C) { /* V0.68 */
52 + uint64_t last = ntohl(ofs32[n - 1]);
53 + while (n--) {
54 + offsets[n].size = last -
55 + (offsets[n].offset = ntohl(ofs32[n]));
56 + last = offsets[n].offset;
57 + }
58 + // return (char *) "32BE v0.68";
59 + }
60 + else { /* V3.0 */
61 + unsigned long i;
62 + uint64_t j;
63 +
64 + for (i = n; i-- > 0; )
65 + offsets[i].size = ntohl(ofs32[i]);
66 + for (i = 0, j = 128 + 4 + 4; i < n; i++) {
67 + offsets[i].offset = j;
68 + j += offsets[i].size;
69 + }
70 + // return (char *) "32BE v3.0";
71 + }
72 +}
73 +
74 typedef struct BDRVCloopState {
75 CoMutex lock;
76 uint32_t block_size;
77 uint32_t n_blocks;
78 - uint64_t *offsets;
79 + block_info *offsets;
80 uint32_t sectors_per_block;
81 uint32_t current_block;
82 uint8_t *compressed_block;
83 @@ -40,17 +107,21 @@
85 static int cloop_probe(const uint8_t *buf, int buf_size, const char *filename)
86 {
87 - const char *magic_version_2_0 = "#!/bin/sh\n"
88 - "#V2.0 Format\n"
89 + static const uint8_t magic[] =
90 "modprobe cloop file=$0 && mount -r -t iso9660 /dev/cloop $1\n";
91 - int length = strlen(magic_version_2_0);
92 - if (length > buf_size) {
93 - length = buf_size;
94 + int i, ret = 0, length = buf_size;
95 + uint8_t c;
96 +
97 + if (length > 127) {
98 + length = 127;
99 }
100 - if (!memcmp(magic_version_2_0, buf, length)) {
101 - return 2;
102 + for (i = 0; i < length - sizeof(magic) + 1; i++) {
103 + if (buf[i] != magic[0]) continue;
104 + if (strncmp(buf + i, magic, sizeof(magic) - 1)) continue;
105 + ret = 2;
106 + break;
107 }
108 - return 0;
109 + return ret;
110 }
112 static int cloop_open(BlockDriverState *bs, QDict *options, int flags)
113 @@ -74,32 +145,67 @@
114 }
115 s->n_blocks = be32_to_cpu(s->n_blocks);
117 - /* read offsets */
118 - offsets_size = s->n_blocks * sizeof(uint64_t);
119 - s->offsets = g_malloc(offsets_size);
120 + /* initialize zlib engine */
121 + max_compressed_block_size = s->block_size + s->block_size/1000 + 12 + 4;
122 + s->compressed_block = g_malloc(max_compressed_block_size + 1);
123 + s->uncompressed_block = g_malloc(s->block_size);
125 - ret = bdrv_pread(bs->file, 128 + 4 + 4, s->offsets, offsets_size);
126 - if (ret < 0) {
127 + if (inflateInit(&s->zstream) != Z_OK) {
128 + ret = -EINVAL;
129 goto fail;
130 }
132 - for(i=0;i<s->n_blocks;i++) {
133 - s->offsets[i] = be64_to_cpu(s->offsets[i]);
134 - if (i > 0) {
135 - uint32_t size = s->offsets[i] - s->offsets[i - 1];
136 - if (size > max_compressed_block_size) {
137 - max_compressed_block_size = size;
138 - }
139 + /* read offsets */
140 + if (s->n_blocks + 1 == 0) {
141 + cloop_tail tail;
142 + int64_t end = bdrv_getlength(bs->file);
143 + void *p;
144 + uint32_t toclen, len;
145 +
146 + ret = bdrv_pread(bs->file, end - sizeof(tail), &tail, sizeof(tail));
147 + if (ret < 0) {
148 + goto fail;
149 }
150 +
151 + s->n_blocks = be32_to_cpu(tail.num_blocks);
152 + offsets_size = s->n_blocks * sizeof(block_info);
153 + len = be32_to_cpu(tail.table_size);
154 + toclen = (be32_to_cpu(tail.index_size) & 255) * s->n_blocks;
155 +
156 + s->offsets = g_malloc(offsets_size);
157 + p = g_malloc(len);
158 +
159 + ret = bdrv_pread(bs->file, end - sizeof(tail) - len, p, len);
160 + if (ret < 0) {
161 + goto fail;
162 + }
163 + s->zstream.next_in = p;
164 + s->zstream.avail_in = len;
165 + s->zstream.next_out = s->offsets;
166 + s->zstream.avail_out = toclen;
167 + ret = inflateReset(&s->zstream);
168 + if (ret != Z_OK) {
169 + ret = -EINVAL;
170 + goto fail;
171 + }
172 + ret = inflate(&s->zstream, Z_FINISH);
173 + if (ret != Z_STREAM_END || s->zstream.total_out != toclen) {
174 + ret = -EINVAL;
175 + goto fail;
176 + }
177 + g_free(p);
178 }
179 + else {
180 + offsets_size = s->n_blocks * sizeof(block_info);
181 + s->offsets = g_malloc(offsets_size);
183 - /* initialize zlib engine */
184 - s->compressed_block = g_malloc(max_compressed_block_size + 1);
185 - s->uncompressed_block = g_malloc(s->block_size);
186 - if (inflateInit(&s->zstream) != Z_OK) {
187 - ret = -EINVAL;
188 - goto fail;
189 + ret = bdrv_pread(bs->file, 128 + 4 + 4, s->offsets, offsets_size);
190 + if (ret < 0) {
191 + goto fail;
192 + }
193 }
194 + build_index(s->offsets, s->n_blocks);
195 +
196 s->current_block = s->n_blocks;
198 s->sectors_per_block = s->block_size/512;
199 @@ -120,10 +226,10 @@
201 if (s->current_block != block_num) {
202 int ret;
203 - uint32_t bytes = s->offsets[block_num + 1] - s->offsets[block_num];
204 + uint32_t bytes = s->offsets[block_num].size;
206 - ret = bdrv_pread(bs->file, s->offsets[block_num], s->compressed_block,
207 - bytes);
208 + ret = bdrv_pread(bs->file, s->offsets[block_num].offset,
209 + s->compressed_block, bytes);
210 if (ret != bytes) {
211 return -1;
212 }