static int vmdk_write_extent(VmdkExtent *extent, int64_t cluster_offset,

                            int64_t offset_in_cluster, const uint8_t *buf,

                            int nb_sectors, int64_t sector_num)

{

    int ret;

    VmdkGrainMarker *data = NULL;

    uLongf buf_len;

    const uint8_t *write_buf = buf;

    int write_len = nb_sectors * 512;



    if (extent->compressed) {

        if (!extent->has_marker) {

            ret = -EINVAL;

            goto out;

        }

        buf_len = (extent->cluster_sectors << 9) * 2;

        data = g_malloc(buf_len + sizeof(VmdkGrainMarker));

        if (compress(data->data, &buf_len, buf, nb_sectors << 9) != Z_OK ||

                buf_len == 0) {

            ret = -EINVAL;

            goto out;

        }

        data->lba = sector_num;

        data->size = buf_len;

        write_buf = (uint8_t *)data;

        write_len = buf_len + sizeof(VmdkGrainMarker);

    }

    ret = bdrv_pwrite(extent->file,

                        cluster_offset + offset_in_cluster,

                        write_buf,

                        write_len);

    if (ret != write_len) {

        ret = ret < 0 ? ret : -EIO;

        goto out;

    }

    ret = 0;

 out:

    g_free(data);

    return ret;

}

static int vmdk_write_extent(VmdkExtent *extent, int64_t cluster_offset,

                            int64_t offset_in_cluster, const uint8_t *buf,

                            int nb_sectors, int64_t sector_num)

{

    int ret;

    VmdkGrainMarker *data = NULL;

    uLongf buf_len;

    const uint8_t *write_buf = buf;

    int write_len = nb_sectors * 512;



    if (extent->compressed) {

        if (!extent->has_marker) {

            ret = -EINVAL;

            goto out;

        }

        buf_len = (extent->cluster_sectors << 9) * 2;

        data = g_malloc(buf_len + sizeof(VmdkGrainMarker));

        if (compress(data->data, &buf_len, buf, nb_sectors << 9) != Z_OK ||

                buf_len == 0) {

            ret = -EINVAL;

            goto out;

        }

        data->lba = sector_num;

        data->size = buf_len;

        write_buf = (uint8_t *)data;

        write_len = buf_len + sizeof(VmdkGrainMarker);

    }

    ret = bdrv_pwrite(extent->file,

                        cluster_offset + offset_in_cluster,

                        write_buf,

                        write_len);

    if (ret != write_len) {

        ret = ret < 0 ? ret : -EIO;

        goto out;

    }

    ret = 0;

 out:

    g_free(data);

    return ret;

}

    ret = bdrv_pwrite(extent->file,                         cluster_offset + offset_in_cluster,                         write_buf,                         write_len);
static int FUNC_0(VmdkExtent *VAR_0, int64_t VAR_1,

                            int64_t VAR_2, const uint8_t *VAR_3,

                            int VAR_4, int64_t VAR_5)

{

    int VAR_6;

    VmdkGrainMarker *data = NULL;

    uLongf buf_len;

    const uint8_t *VAR_7 = VAR_3;

    int VAR_8 = VAR_4 * 512;



    if (VAR_0->compressed) {

        if (!VAR_0->has_marker) {

            VAR_6 = -EINVAL;

            goto out;

        }

        buf_len = (VAR_0->cluster_sectors << 9) * 2;

        data = g_malloc(buf_len + sizeof(VmdkGrainMarker));

        if (compress(data->data, &buf_len, VAR_3, VAR_4 << 9) != Z_OK ||

                buf_len == 0) {

            VAR_6 = -EINVAL;

            goto out;

        }

        data->lba = VAR_5;

        data->size = buf_len;

        VAR_7 = (uint8_t *)data;

        VAR_8 = buf_len + sizeof(VmdkGrainMarker);

    }

    VAR_6 = bdrv_pwrite(VAR_0->file,

                        VAR_1 + VAR_2,

                        VAR_7,

                        VAR_8);

    if (VAR_6 != VAR_8) {

        VAR_6 = VAR_6 < 0 ? VAR_6 : -EIO;

        goto out;

    }

    VAR_6 = 0;

 out:

    g_free(data);

    return VAR_6;

}

static int FUNC_0(VmdkExtent *VAR_0, int64_t VAR_1,
int64_t VAR_2, const uint8_t *VAR_3,
int VAR_4, int64_t VAR_5)
{ int VAR_6; VmdkGrainMarker *data = NULL; uLongf buf_len; const uint8_t *VAR_7 = VAR_3; int VAR_8 = VAR_4 * 512; if (VAR_0->compressed) { if (!VAR_0->has_marker) { VAR_6 = -EINVAL; goto out; } buf_len = (VAR_0->cluster_sectors << 9) * 2; data = g_malloc(buf_len + sizeof(VmdkGrainMarker)); if (compress(data->data, &buf_len, VAR_3, VAR_4 << 9) != Z_OK ||
buf_len == 0) { VAR_6 = -EINVAL; goto out; } data->lba = VAR_5; data->size = buf_len; VAR_7 = (uint8_t *)data; VAR_8 = buf_len + sizeof(VmdkGrainMarker); } VAR_6 = bdrv_pwrite(VAR_0->file,
VAR_1 + VAR_2,
VAR_7,
VAR_8); if (VAR_6 != VAR_8) { VAR_6 = VAR_6 < 0 ? VAR_6 : -EIO; goto out; } VAR_6 = 0; out:
g_free(data); return VAR_6; }