static int qcow_write(BlockDriverState *bs, int64_t sector_num,

                     const uint8_t *buf, int nb_sectors)

{

    BDRVQcowState *s = bs->opaque;

    int ret, index_in_cluster, n;

    uint64_t cluster_offset;

    int n_end;



    while (nb_sectors > 0) {

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

        n_end = index_in_cluster + nb_sectors;

        if (s->crypt_method &&

            n_end > QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors)

            n_end = QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors;

        cluster_offset = alloc_cluster_offset(bs, sector_num << 9,

                                              index_in_cluster,

                                              n_end, &n);

        if (!cluster_offset)

            return -1;

        if (s->crypt_method) {

            encrypt_sectors(s, sector_num, s->cluster_data, buf, n, 1,

                            &s->aes_encrypt_key);

            ret = bdrv_pwrite(s->hd, cluster_offset + index_in_cluster * 512,

                              s->cluster_data, n * 512);

        } else {

            ret = bdrv_pwrite(s->hd, cluster_offset + index_in_cluster * 512, buf, n * 512);

        }

        if (ret != n * 512)

            return -1;

        nb_sectors -= n;

        sector_num += n;

        buf += n * 512;

    }

    s->cluster_cache_offset = -1; /* disable compressed cache */

    return 0;

}

static int qcow_write(BlockDriverState *bs, int64_t sector_num,

                     const uint8_t *buf, int nb_sectors)

{

    BDRVQcowState *s = bs->opaque;

    int ret, index_in_cluster, n;

    uint64_t cluster_offset;

    int n_end;



    while (nb_sectors > 0) {

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

        n_end = index_in_cluster + nb_sectors;

        if (s->crypt_method &&

            n_end > QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors)

            n_end = QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors;

        cluster_offset = alloc_cluster_offset(bs, sector_num << 9,

                                              index_in_cluster,

                                              n_end, &n);

        if (!cluster_offset)

            return -1;

        if (s->crypt_method) {

            encrypt_sectors(s, sector_num, s->cluster_data, buf, n, 1,

                            &s->aes_encrypt_key);

            ret = bdrv_pwrite(s->hd, cluster_offset + index_in_cluster * 512,

                              s->cluster_data, n * 512);

        } else {

            ret = bdrv_pwrite(s->hd, cluster_offset + index_in_cluster * 512, buf, n * 512);

        }

        if (ret != n * 512)

            return -1;

        nb_sectors -= n;

        sector_num += n;

        buf += n * 512;

    }

    s->cluster_cache_offset = -1; 

    return 0;

}

                                              n_end, &n);         if (ret != n * 512)     uint64_t cluster_offset;
static int FUNC_0(BlockDriverState *VAR_0, int64_t VAR_1,

                     const uint8_t *VAR_2, int VAR_3)

{

    BDRVQcowState *s = VAR_0->opaque;

    int VAR_4, VAR_5, VAR_6;

    uint64_t cluster_offset;

    int VAR_7;



    while (VAR_3 > 0) {

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

        VAR_7 = VAR_5 + VAR_3;

        if (s->crypt_method &&

            VAR_7 > QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors)

            VAR_7 = QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors;

        cluster_offset = alloc_cluster_offset(VAR_0, VAR_1 << 9,

                                              VAR_5,

                                              VAR_7, &VAR_6);

        if (!cluster_offset)

            return -1;

        if (s->crypt_method) {

            encrypt_sectors(s, VAR_1, s->cluster_data, VAR_2, VAR_6, 1,

                            &s->aes_encrypt_key);

            VAR_4 = bdrv_pwrite(s->hd, cluster_offset + VAR_5 * 512,

                              s->cluster_data, VAR_6 * 512);

        } else {

            VAR_4 = bdrv_pwrite(s->hd, cluster_offset + VAR_5 * 512, VAR_2, VAR_6 * 512);

        }

        if (VAR_4 != VAR_6 * 512)

            return -1;

        VAR_3 -= VAR_6;

        VAR_1 += VAR_6;

        VAR_2 += VAR_6 * 512;

    }

    s->cluster_cache_offset = -1; 

    return 0;

}

static int FUNC_0(BlockDriverState *VAR_0, int64_t VAR_1,
const uint8_t *VAR_2, int VAR_3)
{ BDRVQcowState *s = VAR_0->opaque; int VAR_4, VAR_5, VAR_6; uint64_t cluster_offset; int VAR_7; while (VAR_3 > 0) { VAR_5 = VAR_1 & (s->cluster_sectors - 1); VAR_7 = VAR_5 + VAR_3; if (s->crypt_method &&
VAR_7 > QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors)
VAR_7 = QCOW_MAX_CRYPT_CLUSTERS * s->cluster_sectors; cluster_offset = alloc_cluster_offset(VAR_0, VAR_1 << 9,
VAR_5,
VAR_7, &VAR_6); if (!cluster_offset)
return -1; if (s->crypt_method) { encrypt_sectors(s, VAR_1, s->cluster_data, VAR_2, VAR_6, 1,
&s->aes_encrypt_key); VAR_4 = bdrv_pwrite(s->hd, cluster_offset + VAR_5 * 512,
s->cluster_data, VAR_6 * 512); } else { VAR_4 = bdrv_pwrite(s->hd, cluster_offset + VAR_5 * 512, VAR_2, VAR_6 * 512); } if (VAR_4 != VAR_6 * 512)
return -1; VAR_3 -= VAR_6; VAR_1 += VAR_6; VAR_2 += VAR_6 * 512; } s->cluster_cache_offset = -1; return 0; }