diff --git a/src/minivhd.h b/src/minivhd.h index 5e3a3a1..9bbd428 100644 --- a/src/minivhd.h +++ b/src/minivhd.h @@ -11,7 +11,7 @@ typedef enum MVHDType { } MVHDType; typedef enum MVHDError { - MVHD_ERR_MEM = -1, + MVHD_ERR_MEM, MVHD_ERR_FILE, MVHD_ERR_NOT_VHD, MVHD_ERR_TYPE, @@ -22,9 +22,10 @@ typedef enum MVHDError { typedef struct MVHDMeta { FILE* f; char* filename; + struct MVHDMeta* parent; MVHDFooter footer; MVHDSparseHeader sparse; - struct MVHDMeta* parent; + MVHDBlock* block; } MVHDMeta; MVHDMeta* mvhd_open(const char* path, int* err); diff --git a/src/minivhd_internal.h b/src/minivhd_internal.h index d815aba..6e5ead0 100644 --- a/src/minivhd_internal.h +++ b/src/minivhd_internal.h @@ -6,53 +6,59 @@ #define MVHD_FOOTER_SIZE 512 #define MVHD_SPARSE_SIZE 1024 -typedef struct MVHDFooter -{ - uint8_t cookie[8]; - uint32_t features; - uint32_t fi_fmt_vers; - uint64_t data_offset; - uint32_t timestamp; - uint8_t cr_app[4]; - uint32_t cr_vers; - uint8_t cr_host_os[4]; - uint64_t orig_sz; - uint64_t curr_sz; - struct { - uint16_t cyl; - uint8_t heads; - uint8_t spt; - } geom; - uint32_t disk_type; - uint32_t checksum; - uint8_t uuid[16]; - uint8_t saved_st; - uint8_t reserved[427]; +#define MVHD_SECTOR_SIZE 512 +#define MVHD_MAX_BLOCK_SIZE 0x00200000 + +typedef struct MVHDFooter { + uint8_t cookie[8]; + uint32_t features; + uint32_t fi_fmt_vers; + uint64_t data_offset; + uint32_t timestamp; + uint8_t cr_app[4]; + uint32_t cr_vers; + uint8_t cr_host_os[4]; + uint64_t orig_sz; + uint64_t curr_sz; + struct { + uint16_t cyl; + uint8_t heads; + uint8_t spt; + } geom; + uint32_t disk_type; + uint32_t checksum; + uint8_t uuid[16]; + uint8_t saved_st; + uint8_t reserved[427]; } MVHDFooter; -typedef struct MVHDSparseHeader -{ - uint8_t cookie[8]; - uint64_t data_offset; - uint64_t bat_offset; - uint32_t head_vers; - uint32_t max_bat_ent; - uint32_t block_sz; - uint32_t checksum; - uint8_t par_uuid[16]; - uint32_t par_timestamp; - uint32_t reserved_1; - uint8_t par_utf16_name[512]; - struct { - uint32_t plat_code; - uint32_t plat_data_space; - uint32_t plat_data_len; - uint32_t reserved; - uint64_t plat_data_offset; - } par_loc_entry[8]; - uint8_t reserved_2[256]; +typedef struct MVHDSparseHeader { + uint8_t cookie[8]; + uint64_t data_offset; + uint64_t bat_offset; + uint32_t head_vers; + uint32_t max_bat_ent; + uint32_t block_sz; + uint32_t checksum; + uint8_t par_uuid[16]; + uint32_t par_timestamp; + uint32_t reserved_1; + uint8_t par_utf16_name[512]; + struct { + uint32_t plat_code; + uint32_t plat_data_space; + uint32_t plat_data_len; + uint32_t reserved; + uint64_t plat_data_offset; + } par_loc_entry[8]; + uint8_t reserved_2[256]; } MVHDSparseHeader; +typedef struct MVHDBlock { + uint8_t* bitmap; + uint32_t offset; +} MVHDBlock; + bool mvhd_is_conectix_str(const void* buffer); void mvhd_buffer_to_footer(MVHDFooter* footer, uint8_t* buffer); void mvhd_buffer_to_header(MVHDSparseHeader* header, uint8_t* buffer); diff --git a/src/minivhd_manage.c b/src/minivhd_manage.c index b85cb79..4423fbe 100644 --- a/src/minivhd_manage.c +++ b/src/minivhd_manage.c @@ -114,5 +114,18 @@ end: } void mvhd_close(MVHDMeta* vhdm) { + if (vhdm->parent != NULL) { + mvhd_close(vhdm->parent); + } fclose(vhdm->f); + if (vhdm->block != NULL) { + for (uint32_t i = 0; i < vhdm->sparse.max_bat_ent; i++) { + free(vhdm->block[i].bitmap); + vhdm->block[i].bitmap = NULL; + } + free(vhdm->block); + vhdm->block = NULL; + } + free(vhdm); + vhdm = NULL; } \ No newline at end of file