static int dmg_read_mish_block(BDRVDMGState *s, DmgHeaderState *ds,

                               uint8_t *buffer, uint32_t count)

{

    uint32_t type, i;

    int ret;

    size_t new_size;

    uint32_t chunk_count;

    int64_t offset = 0;



    type = buff_read_uint32(buffer, offset);

    /* skip data that is not a valid MISH block (invalid magic or too small) */

    if (type != 0x6d697368 || count < 244) {

        /* assume success for now */

        return 0;

    }



    offset += 4;

    offset += 200;



    chunk_count = (count - 204) / 40;

    new_size = sizeof(uint64_t) * (s->n_chunks + chunk_count);

    s->types = g_realloc(s->types, new_size / 2);

    s->offsets = g_realloc(s->offsets, new_size);

    s->lengths = g_realloc(s->lengths, new_size);

    s->sectors = g_realloc(s->sectors, new_size);

    s->sectorcounts = g_realloc(s->sectorcounts, new_size);



    for (i = s->n_chunks; i < s->n_chunks + chunk_count; i++) {

        s->types[i] = buff_read_uint32(buffer, offset);

        offset += 4;

        if (s->types[i] != 0x80000005 && s->types[i] != 1 &&

            s->types[i] != 2) {

            if (s->types[i] == 0xffffffff && i > 0) {

                ds->last_in_offset = s->offsets[i - 1] + s->lengths[i - 1];

                ds->last_out_offset = s->sectors[i - 1] +

                                      s->sectorcounts[i - 1];

            }

            chunk_count--;

            i--;

            offset += 36;

            continue;

        }

        offset += 4;



        s->sectors[i] = buff_read_uint64(buffer, offset);

        s->sectors[i] += ds->last_out_offset;

        offset += 8;



        s->sectorcounts[i] = buff_read_uint64(buffer, offset);

        offset += 8;



        if (s->sectorcounts[i] > DMG_SECTORCOUNTS_MAX) {

            error_report("sector count %" PRIu64 " for chunk %" PRIu32

                         " is larger than max (%u)",

                         s->sectorcounts[i], i, DMG_SECTORCOUNTS_MAX);

            ret = -EINVAL;

            goto fail;

        }



        s->offsets[i] = buff_read_uint64(buffer, offset);

        s->offsets[i] += ds->last_in_offset;

        offset += 8;



        s->lengths[i] = buff_read_uint64(buffer, offset);

        offset += 8;



        if (s->lengths[i] > DMG_LENGTHS_MAX) {

            error_report("length %" PRIu64 " for chunk %" PRIu32

                         " is larger than max (%u)",

                         s->lengths[i], i, DMG_LENGTHS_MAX);

            ret = -EINVAL;

            goto fail;

        }



        update_max_chunk_size(s, i, &ds->max_compressed_size,

                              &ds->max_sectors_per_chunk);

    }

    s->n_chunks += chunk_count;

    return 0;



fail:

    return ret;

}

static int dmg_read_mish_block(BDRVDMGState *s, DmgHeaderState *ds,

                               uint8_t *buffer, uint32_t count)

{

    uint32_t type, i;

    int ret;

    size_t new_size;

    uint32_t chunk_count;

    int64_t offset = 0;



    type = buff_read_uint32(buffer, offset);

    

    if (type != 0x6d697368 || count < 244) {

        

        return 0;

    }



    offset += 4;

    offset += 200;



    chunk_count = (count - 204) / 40;

    new_size = sizeof(uint64_t) * (s->n_chunks + chunk_count);

    s->types = g_realloc(s->types, new_size / 2);

    s->offsets = g_realloc(s->offsets, new_size);

    s->lengths = g_realloc(s->lengths, new_size);

    s->sectors = g_realloc(s->sectors, new_size);

    s->sectorcounts = g_realloc(s->sectorcounts, new_size);



    for (i = s->n_chunks; i < s->n_chunks + chunk_count; i++) {

        s->types[i] = buff_read_uint32(buffer, offset);

        offset += 4;

        if (s->types[i] != 0x80000005 && s->types[i] != 1 &&

            s->types[i] != 2) {

            if (s->types[i] == 0xffffffff && i > 0) {

                ds->last_in_offset = s->offsets[i - 1] + s->lengths[i - 1];

                ds->last_out_offset = s->sectors[i - 1] +

                                      s->sectorcounts[i - 1];

            }

            chunk_count--;

            i--;

            offset += 36;

            continue;

        }

        offset += 4;



        s->sectors[i] = buff_read_uint64(buffer, offset);

        s->sectors[i] += ds->last_out_offset;

        offset += 8;



        s->sectorcounts[i] = buff_read_uint64(buffer, offset);

        offset += 8;



        if (s->sectorcounts[i] > DMG_SECTORCOUNTS_MAX) {

            error_report("sector count %" PRIu64 " for chunk %" PRIu32

                         " is larger than max (%u)",

                         s->sectorcounts[i], i, DMG_SECTORCOUNTS_MAX);

            ret = -EINVAL;

            goto fail;

        }



        s->offsets[i] = buff_read_uint64(buffer, offset);

        s->offsets[i] += ds->last_in_offset;

        offset += 8;



        s->lengths[i] = buff_read_uint64(buffer, offset);

        offset += 8;



        if (s->lengths[i] > DMG_LENGTHS_MAX) {

            error_report("length %" PRIu64 " for chunk %" PRIu32

                         " is larger than max (%u)",

                         s->lengths[i], i, DMG_LENGTHS_MAX);

            ret = -EINVAL;

            goto fail;

        }



        update_max_chunk_size(s, i, &ds->max_compressed_size,

                              &ds->max_sectors_per_chunk);

    }

    s->n_chunks += chunk_count;

    return 0;



fail:

    return ret;

}

    offset += 4;     offset += 200;                 ds->last_in_offset = s->offsets[i - 1] + s->lengths[i - 1];         s->offsets[i] += ds->last_in_offset;
static int FUNC_0(BDRVDMGState *VAR_0, DmgHeaderState *VAR_1,

                               uint8_t *VAR_2, uint32_t VAR_3)

{

    uint32_t type, i;

    int VAR_4;

    size_t new_size;

    uint32_t chunk_count;

    int64_t offset = 0;



    type = buff_read_uint32(VAR_2, offset);

    

    if (type != 0x6d697368 || VAR_3 < 244) {

        

        return 0;

    }



    offset += 4;

    offset += 200;



    chunk_count = (VAR_3 - 204) / 40;

    new_size = sizeof(uint64_t) * (VAR_0->n_chunks + chunk_count);

    VAR_0->types = g_realloc(VAR_0->types, new_size / 2);

    VAR_0->offsets = g_realloc(VAR_0->offsets, new_size);

    VAR_0->lengths = g_realloc(VAR_0->lengths, new_size);

    VAR_0->sectors = g_realloc(VAR_0->sectors, new_size);

    VAR_0->sectorcounts = g_realloc(VAR_0->sectorcounts, new_size);



    for (i = VAR_0->n_chunks; i < VAR_0->n_chunks + chunk_count; i++) {

        VAR_0->types[i] = buff_read_uint32(VAR_2, offset);

        offset += 4;

        if (VAR_0->types[i] != 0x80000005 && VAR_0->types[i] != 1 &&

            VAR_0->types[i] != 2) {

            if (VAR_0->types[i] == 0xffffffff && i > 0) {

                VAR_1->last_in_offset = VAR_0->offsets[i - 1] + VAR_0->lengths[i - 1];

                VAR_1->last_out_offset = VAR_0->sectors[i - 1] +

                                      VAR_0->sectorcounts[i - 1];

            }

            chunk_count--;

            i--;

            offset += 36;

            continue;

        }

        offset += 4;



        VAR_0->sectors[i] = buff_read_uint64(VAR_2, offset);

        VAR_0->sectors[i] += VAR_1->last_out_offset;

        offset += 8;



        VAR_0->sectorcounts[i] = buff_read_uint64(VAR_2, offset);

        offset += 8;



        if (VAR_0->sectorcounts[i] > DMG_SECTORCOUNTS_MAX) {

            error_report("sector VAR_3 %" PRIu64 " for chunk %" PRIu32

                         " is larger than max (%u)",

                         VAR_0->sectorcounts[i], i, DMG_SECTORCOUNTS_MAX);

            VAR_4 = -EINVAL;

            goto fail;

        }



        VAR_0->offsets[i] = buff_read_uint64(VAR_2, offset);

        VAR_0->offsets[i] += VAR_1->last_in_offset;

        offset += 8;



        VAR_0->lengths[i] = buff_read_uint64(VAR_2, offset);

        offset += 8;



        if (VAR_0->lengths[i] > DMG_LENGTHS_MAX) {

            error_report("length %" PRIu64 " for chunk %" PRIu32

                         " is larger than max (%u)",

                         VAR_0->lengths[i], i, DMG_LENGTHS_MAX);

            VAR_4 = -EINVAL;

            goto fail;

        }



        update_max_chunk_size(VAR_0, i, &VAR_1->max_compressed_size,

                              &VAR_1->max_sectors_per_chunk);

    }

    VAR_0->n_chunks += chunk_count;

    return 0;



fail:

    return VAR_4;

}

static int FUNC_0(BDRVDMGState *VAR_0, DmgHeaderState *VAR_1,
uint8_t *VAR_2, uint32_t VAR_3)
{ uint32_t type, i; int VAR_4; size_t new_size; uint32_t chunk_count; int64_t offset = 0; type = buff_read_uint32(VAR_2, offset); if (type != 0x6d697368 || VAR_3 < 244) { return 0; } offset += 4; offset += 200; chunk_count = (VAR_3 - 204) / 40; new_size = sizeof(uint64_t) * (VAR_0->n_chunks + chunk_count); VAR_0->types = g_realloc(VAR_0->types, new_size / 2); VAR_0->offsets = g_realloc(VAR_0->offsets, new_size); VAR_0->lengths = g_realloc(VAR_0->lengths, new_size); VAR_0->sectors = g_realloc(VAR_0->sectors, new_size); VAR_0->sectorcounts = g_realloc(VAR_0->sectorcounts, new_size); for (i = VAR_0->n_chunks; i < VAR_0->n_chunks + chunk_count; i++) { VAR_0->types[i] = buff_read_uint32(VAR_2, offset); offset += 4; if (VAR_0->types[i] != 0x80000005 && VAR_0->types[i] != 1 &&
VAR_0->types[i] != 2) { if (VAR_0->types[i] == 0xffffffff && i > 0) { VAR_1->last_in_offset = VAR_0->offsets[i - 1] + VAR_0->lengths[i - 1]; VAR_1->last_out_offset = VAR_0->sectors[i - 1] +
VAR_0->sectorcounts[i - 1]; } chunk_count--; i--; offset += 36; continue; } offset += 4; VAR_0->sectors[i] = buff_read_uint64(VAR_2, offset); VAR_0->sectors[i] += VAR_1->last_out_offset; offset += 8; VAR_0->sectorcounts[i] = buff_read_uint64(VAR_2, offset); offset += 8; if (VAR_0->sectorcounts[i] > DMG_SECTORCOUNTS_MAX) { error_report("sector VAR_3 %" PRIu64 " for chunk %" PRIu32
" is larger than max (%u)",
VAR_0->sectorcounts[i], i, DMG_SECTORCOUNTS_MAX); VAR_4 = -EINVAL; goto fail; } VAR_0->offsets[i] = buff_read_uint64(VAR_2, offset); VAR_0->offsets[i] += VAR_1->last_in_offset; offset += 8; VAR_0->lengths[i] = buff_read_uint64(VAR_2, offset); offset += 8; if (VAR_0->lengths[i] > DMG_LENGTHS_MAX) { error_report("length %" PRIu64 " for chunk %" PRIu32
" is larger than max (%u)",
VAR_0->lengths[i], i, DMG_LENGTHS_MAX); VAR_4 = -EINVAL; goto fail; } update_max_chunk_size(VAR_0, i, &VAR_1->max_compressed_size,
&VAR_1->max_sectors_per_chunk); } VAR_0->n_chunks += chunk_count; return 0; fail:
return VAR_4; }