static int qcow_read(BlockDriverState *bs, int64_t sector_num,

                     uint8_t *buf, int nb_sectors)

{

    BDRVQcowState *s = bs->opaque;

    int ret, index_in_cluster, n, n1;

    uint64_t cluster_offset;



    while (nb_sectors > 0) {

        n = nb_sectors;

        cluster_offset = qcow2_get_cluster_offset(bs, sector_num << 9, &n);

        index_in_cluster = sector_num & (s->cluster_sectors - 1);

        if (!cluster_offset) {

            if (bs->backing_hd) {

                /* read from the base image */

                n1 = qcow2_backing_read1(bs->backing_hd, sector_num, buf, n);

                if (n1 > 0) {

                    BLKDBG_EVENT(bs->file, BLKDBG_READ_BACKING);

                    ret = bdrv_read(bs->backing_hd, sector_num, buf, n1);

                    if (ret < 0)

                        return -1;

                }

            } else {

                memset(buf, 0, 512 * n);

            }

        } else if (cluster_offset & QCOW_OFLAG_COMPRESSED) {

            if (qcow2_decompress_cluster(bs, cluster_offset) < 0)

                return -1;

            memcpy(buf, s->cluster_cache + index_in_cluster * 512, 512 * n);

        } else {

            BLKDBG_EVENT(bs->file, BLKDBG_READ);

            ret = bdrv_pread(bs->file, cluster_offset + index_in_cluster * 512, buf, n * 512);

            if (ret != n * 512)

                return -1;

            if (s->crypt_method) {

                qcow2_encrypt_sectors(s, sector_num, buf, buf, n, 0,

                                &s->aes_decrypt_key);

            }

        }

        nb_sectors -= n;

        sector_num += n;

        buf += n * 512;

    }

    return 0;

}

static int qcow_read(BlockDriverState *bs, int64_t sector_num,

                     uint8_t *buf, int nb_sectors)

{

    BDRVQcowState *s = bs->opaque;

    int ret, index_in_cluster, n, n1;

    uint64_t cluster_offset;



    while (nb_sectors > 0) {

        n = nb_sectors;

        cluster_offset = qcow2_get_cluster_offset(bs, sector_num << 9, &n);

        index_in_cluster = sector_num & (s->cluster_sectors - 1);

        if (!cluster_offset) {

            if (bs->backing_hd) {

                

                n1 = qcow2_backing_read1(bs->backing_hd, sector_num, buf, n);

                if (n1 > 0) {

                    BLKDBG_EVENT(bs->file, BLKDBG_READ_BACKING);

                    ret = bdrv_read(bs->backing_hd, sector_num, buf, n1);

                    if (ret < 0)

                        return -1;

                }

            } else {

                memset(buf, 0, 512 * n);

            }

        } else if (cluster_offset & QCOW_OFLAG_COMPRESSED) {

            if (qcow2_decompress_cluster(bs, cluster_offset) < 0)

                return -1;

            memcpy(buf, s->cluster_cache + index_in_cluster * 512, 512 * n);

        } else {

            BLKDBG_EVENT(bs->file, BLKDBG_READ);

            ret = bdrv_pread(bs->file, cluster_offset + index_in_cluster * 512, buf, n * 512);

            if (ret != n * 512)

                return -1;

            if (s->crypt_method) {

                qcow2_encrypt_sectors(s, sector_num, buf, buf, n, 0,

                                &s->aes_decrypt_key);

            }

        }

        nb_sectors -= n;

        sector_num += n;

        buf += n * 512;

    }

    return 0;

}

static int FUNC_0(BlockDriverState *VAR_0, int64_t VAR_1,

                     uint8_t *VAR_2, int VAR_3)

{

    BDRVQcowState *s = VAR_0->opaque;

    int VAR_4, VAR_5, VAR_6, VAR_7;

    uint64_t cluster_offset;



    while (VAR_3 > 0) {

        VAR_6 = VAR_3;

        cluster_offset = qcow2_get_cluster_offset(VAR_0, VAR_1 << 9, &VAR_6);

        VAR_5 = VAR_1 & (s->cluster_sectors - 1);

        if (!cluster_offset) {

            if (VAR_0->backing_hd) {

                

                VAR_7 = qcow2_backing_read1(VAR_0->backing_hd, VAR_1, VAR_2, VAR_6);

                if (VAR_7 > 0) {

                    BLKDBG_EVENT(VAR_0->file, BLKDBG_READ_BACKING);

                    VAR_4 = bdrv_read(VAR_0->backing_hd, VAR_1, VAR_2, VAR_7);

                    if (VAR_4 < 0)

                        return -1;

                }

            } else {

                memset(VAR_2, 0, 512 * VAR_6);

            }

        } else if (cluster_offset & QCOW_OFLAG_COMPRESSED) {

            if (qcow2_decompress_cluster(VAR_0, cluster_offset) < 0)

                return -1;

            memcpy(VAR_2, s->cluster_cache + VAR_5 * 512, 512 * VAR_6);

        } else {

            BLKDBG_EVENT(VAR_0->file, BLKDBG_READ);

            VAR_4 = bdrv_pread(VAR_0->file, cluster_offset + VAR_5 * 512, VAR_2, VAR_6 * 512);

            if (VAR_4 != VAR_6 * 512)

                return -1;

            if (s->crypt_method) {

                qcow2_encrypt_sectors(s, VAR_1, VAR_2, VAR_2, VAR_6, 0,

                                &s->aes_decrypt_key);

            }

        }

        VAR_3 -= VAR_6;

        VAR_1 += VAR_6;

        VAR_2 += VAR_6 * 512;

    }

    return 0;

}

static int FUNC_0(BlockDriverState *VAR_0, int64_t VAR_1,
uint8_t *VAR_2, int VAR_3)
{ BDRVQcowState *s = VAR_0->opaque; int VAR_4, VAR_5, VAR_6, VAR_7; uint64_t cluster_offset; while (VAR_3 > 0) { VAR_6 = VAR_3; cluster_offset = qcow2_get_cluster_offset(VAR_0, VAR_1 << 9, &VAR_6); VAR_5 = VAR_1 & (s->cluster_sectors - 1); if (!cluster_offset) { if (VAR_0->backing_hd) { VAR_7 = qcow2_backing_read1(VAR_0->backing_hd, VAR_1, VAR_2, VAR_6); if (VAR_7 > 0) { BLKDBG_EVENT(VAR_0->file, BLKDBG_READ_BACKING); VAR_4 = bdrv_read(VAR_0->backing_hd, VAR_1, VAR_2, VAR_7); if (VAR_4 < 0)
return -1; } } else { memset(VAR_2, 0, 512 * VAR_6); } } else if (cluster_offset & QCOW_OFLAG_COMPRESSED) { if (qcow2_decompress_cluster(VAR_0, cluster_offset) < 0)
return -1; memcpy(VAR_2, s->cluster_cache + VAR_5 * 512, 512 * VAR_6); } else { BLKDBG_EVENT(VAR_0->file, BLKDBG_READ); VAR_4 = bdrv_pread(VAR_0->file, cluster_offset + VAR_5 * 512, VAR_2, VAR_6 * 512); if (VAR_4 != VAR_6 * 512)
return -1; if (s->crypt_method) { qcow2_encrypt_sectors(s, VAR_1, VAR_2, VAR_2, VAR_6, 0,
&s->aes_decrypt_key); } } VAR_3 -= VAR_6; VAR_1 += VAR_6; VAR_2 += VAR_6 * 512; } return 0; }