dmg: use pread
[qemu.git] / block / dmg.c
bloba0ba34fb8991a4c3e59f0c9ac51c7f2cbbc3ddd5
1 /*
2 * QEMU Block driver for DMG images
4 * Copyright (c) 2004 Johannes E. Schindelin
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
24 #include "qemu-common.h"
25 #include "block_int.h"
26 #include "bswap.h"
27 #include "module.h"
28 #include <zlib.h>
30 typedef struct BDRVDMGState {
31 int fd;
33 /* each chunk contains a certain number of sectors,
34 * offsets[i] is the offset in the .dmg file,
35 * lengths[i] is the length of the compressed chunk,
36 * sectors[i] is the sector beginning at offsets[i],
37 * sectorcounts[i] is the number of sectors in that chunk,
38 * the sectors array is ordered
39 * 0<=i<n_chunks */
41 uint32_t n_chunks;
42 uint32_t* types;
43 uint64_t* offsets;
44 uint64_t* lengths;
45 uint64_t* sectors;
46 uint64_t* sectorcounts;
47 uint32_t current_chunk;
48 uint8_t *compressed_chunk;
49 uint8_t *uncompressed_chunk;
50 z_stream zstream;
51 } BDRVDMGState;
53 static int dmg_probe(const uint8_t *buf, int buf_size, const char *filename)
55 int len=strlen(filename);
56 if(len>4 && !strcmp(filename+len-4,".dmg"))
57 return 2;
58 return 0;
61 static off_t read_off(int fd, int64_t offset)
63 uint64_t buffer;
64 if (pread(fd, &buffer, 8, offset) < 8)
65 return 0;
66 return be64_to_cpu(buffer);
69 static off_t read_uint32(int fd, int64_t offset)
71 uint32_t buffer;
72 if (pread(fd, &buffer, 4, offset) < 4)
73 return 0;
74 return be32_to_cpu(buffer);
77 static int dmg_open(BlockDriverState *bs, const char *filename, int flags)
79 BDRVDMGState *s = bs->opaque;
80 off_t info_begin,info_end,last_in_offset,last_out_offset;
81 uint32_t count;
82 uint32_t max_compressed_size=1,max_sectors_per_chunk=1,i;
83 int64_t offset;
85 s->fd = open(filename, O_RDONLY | O_BINARY);
86 if (s->fd < 0)
87 return -errno;
88 bs->read_only = 1;
89 s->n_chunks = 0;
90 s->offsets = s->lengths = s->sectors = s->sectorcounts = NULL;
92 /* read offset of info blocks */
93 offset = lseek(s->fd, -0x1d8, SEEK_END);
94 if (offset < 0) {
95 goto fail;
98 info_begin = read_off(s->fd, offset);
99 if (info_begin == 0) {
100 goto fail;
103 if (read_uint32(s->fd, info_begin) != 0x100) {
104 goto fail;
107 count = read_uint32(s->fd, info_begin + 4);
108 if (count == 0) {
109 goto fail;
111 info_end = info_begin + count;
113 offset = info_begin + 0x100;
115 /* read offsets */
116 last_in_offset = last_out_offset = 0;
117 while (offset < info_end) {
118 uint32_t type;
120 count = read_uint32(s->fd, offset);
121 if(count==0)
122 goto fail;
123 offset += 4;
125 type = read_uint32(s->fd, offset);
126 if (type == 0x6d697368 && count >= 244) {
127 int new_size, chunk_count;
129 offset += 4;
130 offset += 200;
132 chunk_count = (count-204)/40;
133 new_size = sizeof(uint64_t) * (s->n_chunks + chunk_count);
134 s->types = qemu_realloc(s->types, new_size/2);
135 s->offsets = qemu_realloc(s->offsets, new_size);
136 s->lengths = qemu_realloc(s->lengths, new_size);
137 s->sectors = qemu_realloc(s->sectors, new_size);
138 s->sectorcounts = qemu_realloc(s->sectorcounts, new_size);
140 for(i=s->n_chunks;i<s->n_chunks+chunk_count;i++) {
141 s->types[i] = read_uint32(s->fd, offset);
142 offset += 4;
143 if(s->types[i]!=0x80000005 && s->types[i]!=1 && s->types[i]!=2) {
144 if(s->types[i]==0xffffffff) {
145 last_in_offset = s->offsets[i-1]+s->lengths[i-1];
146 last_out_offset = s->sectors[i-1]+s->sectorcounts[i-1];
148 chunk_count--;
149 i--;
150 offset += 36;
151 continue;
153 offset += 4;
155 s->sectors[i] = last_out_offset+read_off(s->fd, offset);
156 offset += 8;
158 s->sectorcounts[i] = read_off(s->fd, offset);
159 offset += 8;
161 s->offsets[i] = last_in_offset+read_off(s->fd, offset);
162 offset += 8;
164 s->lengths[i] = read_off(s->fd, offset);
165 offset += 8;
167 if(s->lengths[i]>max_compressed_size)
168 max_compressed_size = s->lengths[i];
169 if(s->sectorcounts[i]>max_sectors_per_chunk)
170 max_sectors_per_chunk = s->sectorcounts[i];
172 s->n_chunks+=chunk_count;
176 /* initialize zlib engine */
177 s->compressed_chunk = qemu_malloc(max_compressed_size+1);
178 s->uncompressed_chunk = qemu_malloc(512*max_sectors_per_chunk);
179 if(inflateInit(&s->zstream) != Z_OK)
180 goto fail;
182 s->current_chunk = s->n_chunks;
184 return 0;
185 fail:
186 close(s->fd);
187 return -1;
190 static inline int is_sector_in_chunk(BDRVDMGState* s,
191 uint32_t chunk_num,int sector_num)
193 if(chunk_num>=s->n_chunks || s->sectors[chunk_num]>sector_num ||
194 s->sectors[chunk_num]+s->sectorcounts[chunk_num]<=sector_num)
195 return 0;
196 else
197 return -1;
200 static inline uint32_t search_chunk(BDRVDMGState* s,int sector_num)
202 /* binary search */
203 uint32_t chunk1=0,chunk2=s->n_chunks,chunk3;
204 while(chunk1!=chunk2) {
205 chunk3 = (chunk1+chunk2)/2;
206 if(s->sectors[chunk3]>sector_num)
207 chunk2 = chunk3;
208 else if(s->sectors[chunk3]+s->sectorcounts[chunk3]>sector_num)
209 return chunk3;
210 else
211 chunk1 = chunk3;
213 return s->n_chunks; /* error */
216 static inline int dmg_read_chunk(BDRVDMGState *s,int sector_num)
218 if(!is_sector_in_chunk(s,s->current_chunk,sector_num)) {
219 int ret;
220 uint32_t chunk = search_chunk(s,sector_num);
222 if(chunk>=s->n_chunks)
223 return -1;
225 s->current_chunk = s->n_chunks;
226 switch(s->types[chunk]) {
227 case 0x80000005: { /* zlib compressed */
228 int i;
230 /* we need to buffer, because only the chunk as whole can be
231 * inflated. */
232 i=0;
233 do {
234 ret = pread(s->fd, s->compressed_chunk+i, s->lengths[chunk]-i,
235 s->offsets[chunk] + i);
236 if(ret<0 && errno==EINTR)
237 ret=0;
238 i+=ret;
239 } while(ret>=0 && ret+i<s->lengths[chunk]);
241 if (ret != s->lengths[chunk])
242 return -1;
244 s->zstream.next_in = s->compressed_chunk;
245 s->zstream.avail_in = s->lengths[chunk];
246 s->zstream.next_out = s->uncompressed_chunk;
247 s->zstream.avail_out = 512*s->sectorcounts[chunk];
248 ret = inflateReset(&s->zstream);
249 if(ret != Z_OK)
250 return -1;
251 ret = inflate(&s->zstream, Z_FINISH);
252 if(ret != Z_STREAM_END || s->zstream.total_out != 512*s->sectorcounts[chunk])
253 return -1;
254 break; }
255 case 1: /* copy */
256 ret = pread(s->fd, s->uncompressed_chunk, s->lengths[chunk],
257 s->offsets[chunk]);
258 if (ret != s->lengths[chunk])
259 return -1;
260 break;
261 case 2: /* zero */
262 memset(s->uncompressed_chunk, 0, 512*s->sectorcounts[chunk]);
263 break;
265 s->current_chunk = chunk;
267 return 0;
270 static int dmg_read(BlockDriverState *bs, int64_t sector_num,
271 uint8_t *buf, int nb_sectors)
273 BDRVDMGState *s = bs->opaque;
274 int i;
276 for(i=0;i<nb_sectors;i++) {
277 uint32_t sector_offset_in_chunk;
278 if(dmg_read_chunk(s, sector_num+i) != 0)
279 return -1;
280 sector_offset_in_chunk = sector_num+i-s->sectors[s->current_chunk];
281 memcpy(buf+i*512,s->uncompressed_chunk+sector_offset_in_chunk*512,512);
283 return 0;
286 static void dmg_close(BlockDriverState *bs)
288 BDRVDMGState *s = bs->opaque;
289 close(s->fd);
290 if(s->n_chunks>0) {
291 free(s->types);
292 free(s->offsets);
293 free(s->lengths);
294 free(s->sectors);
295 free(s->sectorcounts);
297 free(s->compressed_chunk);
298 free(s->uncompressed_chunk);
299 inflateEnd(&s->zstream);
302 static BlockDriver bdrv_dmg = {
303 .format_name = "dmg",
304 .instance_size = sizeof(BDRVDMGState),
305 .bdrv_probe = dmg_probe,
306 .bdrv_file_open = dmg_open,
307 .bdrv_read = dmg_read,
308 .bdrv_close = dmg_close,
311 static void bdrv_dmg_init(void)
313 bdrv_register(&bdrv_dmg);
316 block_init(bdrv_dmg_init);