Lines Matching +full:fail +full:- +full:safe
26 #include "block/block-io.h"
29 #include "qemu/error-report.h"
39 * or truncating when converting to 32-bit types
67 if (len > 4 && !strcmp(filename + len - 4, ".dmg")) { in dmg_probe()
79 ret = bdrv_pread(bs->file, offset, 8, &buffer, 0); in read_uint64()
94 ret = bdrv_pread(bs->file, offset, 4, &buffer, 0); in read_uint32()
123 switch (s->types[chunk]) { in update_max_chunk_size()
127 compressed_size = s->lengths[chunk]; in update_max_chunk_size()
128 uncompressed_sectors = s->sectorcounts[chunk]; in update_max_chunk_size()
131 uncompressed_sectors = DIV_ROUND_UP(s->lengths[chunk], 512); in update_max_chunk_size()
135 /* as the all-zeroes block may be large, it is treated specially: the in update_max_chunk_size()
151 BlockDriverState *file_bs = file->bs; in dmg_find_koly_offset()
160 * in the last 511 bytes of the second-last sector or the first 4 bytes of in dmg_find_koly_offset()
164 error_setg_errno(errp, -length, in dmg_find_koly_offset()
169 return -EINVAL; in dmg_find_koly_offset()
172 offset = length - 511 - 512; in dmg_find_koly_offset()
177 error_setg_errno(errp, -ret, "Failed while reading UDIF trailer"); in dmg_find_koly_offset()
180 for (i = 0; i < length - 3; i++) { in dmg_find_koly_offset()
187 return -EINVAL; in dmg_find_koly_offset()
226 uint64_t in_offset = ds->data_fork_offset; in dmg_read_mish_block()
246 chunk_count = (count - 204) / 40; in dmg_read_mish_block()
247 new_size = sizeof(uint64_t) * (s->n_chunks + chunk_count); in dmg_read_mish_block()
248 s->types = g_realloc(s->types, new_size / 2); in dmg_read_mish_block()
249 s->offsets = g_realloc(s->offsets, new_size); in dmg_read_mish_block()
250 s->lengths = g_realloc(s->lengths, new_size); in dmg_read_mish_block()
251 s->sectors = g_realloc(s->sectors, new_size); in dmg_read_mish_block()
252 s->sectorcounts = g_realloc(s->sectorcounts, new_size); in dmg_read_mish_block()
254 for (i = s->n_chunks; i < s->n_chunks + chunk_count; i++) { in dmg_read_mish_block()
255 s->types[i] = buff_read_uint32(buffer, offset); in dmg_read_mish_block()
256 if (!dmg_is_known_block_type(s->types[i])) { in dmg_read_mish_block()
257 switch (s->types[i]) { in dmg_read_mish_block()
259 warn_report_once("dmg-bzip2 module is missing, accessing bzip2 " in dmg_read_mish_block()
263 warn_report_once("dmg-lzfse module is missing, accessing lzfse " in dmg_read_mish_block()
273 s->types[i]); in dmg_read_mish_block()
276 chunk_count--; in dmg_read_mish_block()
277 i--; in dmg_read_mish_block()
283 s->sectors[i] = buff_read_uint64(buffer, offset + 8); in dmg_read_mish_block()
284 s->sectors[i] += out_offset; in dmg_read_mish_block()
287 s->sectorcounts[i] = buff_read_uint64(buffer, offset + 0x10); in dmg_read_mish_block()
289 /* all-zeroes sector (type UDZE and UDIG) does not need to be in dmg_read_mish_block()
291 if (s->types[i] != UDZE && s->types[i] != UDIG in dmg_read_mish_block()
292 && s->sectorcounts[i] > DMG_SECTORCOUNTS_MAX) { in dmg_read_mish_block()
295 s->sectorcounts[i], i, DMG_SECTORCOUNTS_MAX); in dmg_read_mish_block()
296 ret = -EINVAL; in dmg_read_mish_block()
297 goto fail; in dmg_read_mish_block()
301 s->offsets[i] = buff_read_uint64(buffer, offset + 0x18); in dmg_read_mish_block()
302 s->offsets[i] += in_offset; in dmg_read_mish_block()
305 s->lengths[i] = buff_read_uint64(buffer, offset + 0x20); in dmg_read_mish_block()
307 if (s->lengths[i] > DMG_LENGTHS_MAX) { in dmg_read_mish_block()
310 s->lengths[i], i, DMG_LENGTHS_MAX); in dmg_read_mish_block()
311 ret = -EINVAL; in dmg_read_mish_block()
312 goto fail; in dmg_read_mish_block()
315 update_max_chunk_size(s, i, &ds->max_compressed_size, in dmg_read_mish_block()
316 &ds->max_sectors_per_chunk); in dmg_read_mish_block()
319 s->n_chunks += chunk_count; in dmg_read_mish_block()
322 fail: in dmg_read_mish_block()
330 BDRVDMGState *s = bs->opaque; in dmg_read_resource_fork()
340 goto fail; in dmg_read_resource_fork()
342 ret = -EINVAL; in dmg_read_resource_fork()
343 goto fail; in dmg_read_resource_fork()
349 goto fail; in dmg_read_resource_fork()
351 ret = -EINVAL; in dmg_read_resource_fork()
352 goto fail; in dmg_read_resource_fork()
367 goto fail; in dmg_read_resource_fork()
368 } else if (count == 0 || count > info_end - offset) { in dmg_read_resource_fork()
369 ret = -EINVAL; in dmg_read_resource_fork()
370 goto fail; in dmg_read_resource_fork()
375 ret = bdrv_pread(bs->file, offset, count, buffer, 0); in dmg_read_resource_fork()
377 goto fail; in dmg_read_resource_fork()
382 goto fail; in dmg_read_resource_fork()
389 fail: in dmg_read_resource_fork()
398 BDRVDMGState *s = bs->opaque; in dmg_read_plist_xml()
404 * safe upper cap on the data length. A test sample had a XML length of in dmg_read_plist_xml()
407 ret = -EINVAL; in dmg_read_plist_xml()
408 goto fail; in dmg_read_plist_xml()
413 ret = bdrv_pread(bs->file, info_begin, info_length, buffer, 0); in dmg_read_plist_xml()
415 ret = -EINVAL; in dmg_read_plist_xml()
416 goto fail; in dmg_read_plist_xml()
431 ret = -EINVAL; in dmg_read_plist_xml()
432 goto fail; in dmg_read_plist_xml()
439 goto fail; in dmg_read_plist_xml()
444 fail: in dmg_read_plist_xml()
452 BDRVDMGState *s = bs->opaque; in dmg_open()
480 if (block_module_load("dmg-bz2", errp) < 0) { in dmg_open()
481 return -EINVAL; in dmg_open()
483 if (block_module_load("dmg-lzfse", errp) < 0) { in dmg_open()
484 return -EINVAL; in dmg_open()
487 s->n_chunks = 0; in dmg_open()
488 s->offsets = s->lengths = s->sectors = s->sectorcounts = NULL; in dmg_open()
495 offset = dmg_find_koly_offset(bs->file, errp); in dmg_open()
498 goto fail; in dmg_open()
504 goto fail; in dmg_open()
506 ret = -EINVAL; in dmg_open()
507 goto fail; in dmg_open()
513 goto fail; in dmg_open()
517 goto fail; in dmg_open()
520 rsrc_fork_length > offset - rsrc_fork_offset) { in dmg_open()
521 ret = -EINVAL; in dmg_open()
522 goto fail; in dmg_open()
527 goto fail; in dmg_open()
531 goto fail; in dmg_open()
534 plist_xml_length > offset - plist_xml_offset) { in dmg_open()
535 ret = -EINVAL; in dmg_open()
536 goto fail; in dmg_open()
538 ret = read_uint64(bs, offset + 0x1ec, (uint64_t *)&bs->total_sectors); in dmg_open()
540 goto fail; in dmg_open()
542 if (bs->total_sectors < 0) { in dmg_open()
543 ret = -EINVAL; in dmg_open()
544 goto fail; in dmg_open()
550 goto fail; in dmg_open()
555 goto fail; in dmg_open()
558 ret = -EINVAL; in dmg_open()
559 goto fail; in dmg_open()
563 s->compressed_chunk = qemu_try_blockalign(bs->file->bs, in dmg_open()
565 s->uncompressed_chunk = qemu_try_blockalign(bs->file->bs, in dmg_open()
567 if (s->compressed_chunk == NULL || s->uncompressed_chunk == NULL) { in dmg_open()
568 ret = -ENOMEM; in dmg_open()
569 goto fail; in dmg_open()
572 if (inflateInit(&s->zstream) != Z_OK) { in dmg_open()
573 ret = -EINVAL; in dmg_open()
574 goto fail; in dmg_open()
577 s->current_chunk = s->n_chunks; in dmg_open()
579 qemu_co_mutex_init(&s->lock); in dmg_open()
582 fail: in dmg_open()
583 g_free(s->types); in dmg_open()
584 g_free(s->offsets); in dmg_open()
585 g_free(s->lengths); in dmg_open()
586 g_free(s->sectors); in dmg_open()
587 g_free(s->sectorcounts); in dmg_open()
588 qemu_vfree(s->compressed_chunk); in dmg_open()
589 qemu_vfree(s->uncompressed_chunk); in dmg_open()
595 bs->bl.request_alignment = BDRV_SECTOR_SIZE; /* No sub-sector I/O */ in dmg_refresh_limits()
601 if (chunk_num >= s->n_chunks || s->sectors[chunk_num] > sector_num || in is_sector_in_chunk()
602 s->sectors[chunk_num] + s->sectorcounts[chunk_num] <= sector_num) { in is_sector_in_chunk()
605 return -1; in is_sector_in_chunk()
612 uint32_t chunk1 = 0, chunk2 = s->n_chunks, chunk3; in search_chunk()
615 if (s->sectors[chunk3] > sector_num) { in search_chunk()
619 chunk2 = chunk3 - 1; in search_chunk()
620 } else if (s->sectors[chunk3] + s->sectorcounts[chunk3] > sector_num) { in search_chunk()
627 return s->n_chunks; /* error */ in search_chunk()
633 BDRVDMGState *s = bs->opaque; in dmg_read_chunk()
635 if (!is_sector_in_chunk(s, s->current_chunk, sector_num)) { in dmg_read_chunk()
639 if (chunk >= s->n_chunks) { in dmg_read_chunk()
640 return -1; in dmg_read_chunk()
643 s->current_chunk = s->n_chunks; in dmg_read_chunk()
644 switch (s->types[chunk]) { /* block entry type */ in dmg_read_chunk()
648 ret = bdrv_co_pread(bs->file, s->offsets[chunk], s->lengths[chunk], in dmg_read_chunk()
649 s->compressed_chunk, 0); in dmg_read_chunk()
651 return -1; in dmg_read_chunk()
654 s->zstream.next_in = s->compressed_chunk; in dmg_read_chunk()
655 s->zstream.avail_in = s->lengths[chunk]; in dmg_read_chunk()
656 s->zstream.next_out = s->uncompressed_chunk; in dmg_read_chunk()
657 s->zstream.avail_out = 512 * s->sectorcounts[chunk]; in dmg_read_chunk()
658 ret = inflateReset(&s->zstream); in dmg_read_chunk()
660 return -1; in dmg_read_chunk()
662 ret = inflate(&s->zstream, Z_FINISH); in dmg_read_chunk()
664 s->zstream.total_out != 512 * s->sectorcounts[chunk]) { in dmg_read_chunk()
665 return -1; in dmg_read_chunk()
674 ret = bdrv_co_pread(bs->file, s->offsets[chunk], s->lengths[chunk], in dmg_read_chunk()
675 s->compressed_chunk, 0); in dmg_read_chunk()
677 return -1; in dmg_read_chunk()
680 ret = dmg_uncompress_bz2((char *)s->compressed_chunk, in dmg_read_chunk()
681 (unsigned int) s->lengths[chunk], in dmg_read_chunk()
682 (char *)s->uncompressed_chunk, in dmg_read_chunk()
684 (512 * s->sectorcounts[chunk])); in dmg_read_chunk()
695 ret = bdrv_co_pread(bs->file, s->offsets[chunk], s->lengths[chunk], in dmg_read_chunk()
696 s->compressed_chunk, 0); in dmg_read_chunk()
698 return -1; in dmg_read_chunk()
701 ret = dmg_uncompress_lzfse((char *)s->compressed_chunk, in dmg_read_chunk()
702 (unsigned int) s->lengths[chunk], in dmg_read_chunk()
703 (char *)s->uncompressed_chunk, in dmg_read_chunk()
705 (512 * s->sectorcounts[chunk])); in dmg_read_chunk()
711 ret = bdrv_co_pread(bs->file, s->offsets[chunk], s->lengths[chunk], in dmg_read_chunk()
712 s->uncompressed_chunk, 0); in dmg_read_chunk()
714 return -1; in dmg_read_chunk()
720 * pre-filled, the zeroes can be set directly. */ in dmg_read_chunk()
723 s->current_chunk = chunk; in dmg_read_chunk()
732 BDRVDMGState *s = bs->opaque; in dmg_co_preadv()
740 qemu_co_mutex_lock(&s->lock); in dmg_co_preadv()
747 ret = -EIO; in dmg_co_preadv()
748 goto fail; in dmg_co_preadv()
751 * s->uncompressed_chunk may be too small to cover the large all-zeroes in dmg_co_preadv()
752 * section. dmg_read_chunk is called to find s->current_chunk */ in dmg_co_preadv()
753 if (s->types[s->current_chunk] == UDZE in dmg_co_preadv()
754 || s->types[s->current_chunk] == UDIG) { /* all zeroes block entry */ in dmg_co_preadv()
758 sector_offset_in_chunk = sector_num + i - s->sectors[s->current_chunk]; in dmg_co_preadv()
759 data = s->uncompressed_chunk + sector_offset_in_chunk * 512; in dmg_co_preadv()
764 fail: in dmg_co_preadv()
765 qemu_co_mutex_unlock(&s->lock); in dmg_co_preadv()
771 BDRVDMGState *s = bs->opaque; in dmg_close()
773 g_free(s->types); in dmg_close()
774 g_free(s->offsets); in dmg_close()
775 g_free(s->lengths); in dmg_close()
776 g_free(s->sectors); in dmg_close()
777 g_free(s->sectorcounts); in dmg_close()
778 qemu_vfree(s->compressed_chunk); in dmg_close()
779 qemu_vfree(s->uncompressed_chunk); in dmg_close()
781 inflateEnd(&s->zstream); in dmg_close()