Code formatting changes

This commit is contained in:
shermp
2018-11-26 16:57:29 +13:00
parent 8747019ef3
commit 6d68912678

185
minivhd.c
View File

@@ -27,8 +27,8 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#include "minivhd.h"
uint8_t VFT_CONECTIX_COOKIE[] = {'c', 'o', 'n', 'e', 'c', 't', 'i', 'x'};
uint8_t VFT_CREATOR[] = {'p','c', 'e', 'm'};
uint8_t VFT_CREATOR_HOST_OS[] = {'W', 'i', '2','k'};
uint8_t VFT_CREATOR[] = {'p', 'c', 'e', 'm'};
uint8_t VFT_CREATOR_HOST_OS[] = {'W', 'i', '2', 'k'};
uint8_t VHD_CXSPARSE_COOKIE[] = {'c', 'x', 's', 'p', 'a', 'r', 's', 'e'};
/* Internal functions */
@@ -48,21 +48,21 @@ static void vhd_create_blk(VHDMeta *vhdm, FILE *f, int blk_num);
static void mk_guid(uint8_t *guid)
{
#if defined(HAVE_UUID_H)
uuid_generate(guid);
uuid_generate(guid);
//#elif defined(HAVE_OBJBASE_H)
// CoCreateGuid( (GUID *)guid);
#else
int n;
int n;
srand(time(NULL));
for (n = 0; n < 16; n++)
{
guid[n] = rand();
}
guid[6] &= 0x0F;
guid[6] |= 0x40; /* Type 4 */
guid[8] &= 0x3F;
guid[8] |= 0x80; /* Variant 1 */
srand(time(NULL));
for (n = 0; n < 16; n++)
{
guid[n] = rand();
}
guid[6] &= 0x0F;
guid[6] |= 0x40; /* Type 4 */
guid[8] &= 0x3F;
guid[8] |= 0x80; /* Variant 1 */
#endif
}
/* Calculate the current timestamp. */
@@ -91,8 +91,10 @@ int vhd_file_is_vhd(FILE *f)
fread(buffer, 1, VHD_FOOTER_SZ, f);
int valid_vhd = 0;
// Check for valid cookie
if (strncmp((char*)VFT_CONECTIX_COOKIE, (char*)buffer, 8) == 0)
{valid_vhd = 1;}
if (strncmp((char *)VFT_CONECTIX_COOKIE, (char *)buffer, 8) == 0)
{
valid_vhd = 1;
}
return valid_vhd;
}
/* Perform a basic integrity check of the VHD footer and sparse header. */
@@ -101,17 +103,29 @@ VHDError vhd_check_validity(VHDMeta *vhdm)
VHDError status, chksum_status;
chksum_status = vhd_validate_checksum(vhdm);
if (vhdm->type != VHD_FIXED && vhdm->type != VHD_DYNAMIC)
{return status = VHD_ERR_TYPE_UNSUPPORTED;}
{
return status = VHD_ERR_TYPE_UNSUPPORTED;
}
else if (vhdm->curr_size < ((uint64_t)vhdm->geom.cyl * vhdm->geom.heads * vhdm->geom.spt * VHD_SECTOR_SZ))
{return status = VHD_ERR_GEOM_SIZE_MISMATCH;}
{
return status = VHD_ERR_GEOM_SIZE_MISMATCH;
}
else if (vhdm->geom.spt > 63)
{return status = VHD_WARN_SPT_SZ;}
{
return status = VHD_WARN_SPT_SZ;
}
else if (chksum_status == VHD_ERR_BAD_DYN_CHECKSUM)
{return status = VHD_ERR_BAD_DYN_CHECKSUM;}
{
return status = VHD_ERR_BAD_DYN_CHECKSUM;
}
else if (chksum_status == VHD_WARN_BAD_CHECKSUM)
{return status = VHD_WARN_BAD_CHECKSUM;}
{
return status = VHD_WARN_BAD_CHECKSUM;
}
else
{return status = VHD_VALID;}
{
return status = VHD_VALID;
}
}
VHDError vhd_read_file(FILE *f, VHDMeta *vhdm)
{
@@ -119,7 +133,7 @@ VHDError vhd_read_file(FILE *f, VHDMeta *vhdm)
fseeko64(f, -VHD_FOOTER_SZ, SEEK_END);
fread(&vhdm->raw_footer, 1, VHD_FOOTER_SZ, f);
// Check for valid cookie
if (strncmp((char*)VFT_CONECTIX_COOKIE, (char*)vhdm->raw_footer.cookie, 8) == 0)
if (strncmp((char *)VFT_CONECTIX_COOKIE, (char *)vhdm->raw_footer.cookie, 8) == 0)
{
/* Don't want a pointer to who knows where... */
vhdm->sparse_bat_arr = NULL;
@@ -130,11 +144,15 @@ VHDError vhd_read_file(FILE *f, VHDMeta *vhdm)
fread(&vhdm->raw_sparse_header, 1, VHD_SPARSE_HEAD_SZ, f);
vhd_sparse_head_to_meta(vhdm);
if (!vhd_bat_from_file(vhdm, f))
{ret = VHD_RET_MALLOC_ERROR;}
{
ret = VHD_RET_MALLOC_ERROR;
}
}
}
else
{ret = VHD_RET_NOT_VHD;}
{
ret = VHD_RET_NOT_VHD;
}
return ret;
}
/* Convenience function to create VHD file by specifiying size in MB */
@@ -157,7 +175,9 @@ void vhd_create_file(FILE *f, VHDMeta *vhdm, int cyl, int heads, int spt, VHDTyp
vhdm->sparse_block_sz = VHD_DEF_BLOCK_SZ;
vhdm->sparse_max_bat = vhdm->curr_size / vhdm->sparse_block_sz;
if (vhdm->curr_size % vhdm->sparse_block_sz != 0)
{vhdm->sparse_max_bat += 1;}
{
vhdm->sparse_max_bat += 1;
}
vhd_new_raw(vhdm);
uint8_t zero_buff[VHD_SECTOR_SZ];
memset(zero_buff, 0, sizeof zero_buff);
@@ -210,7 +230,9 @@ static void vhd_sparse_head_to_meta(VHDMeta *vhdm)
vhdm->sparse_spb = vhdm->sparse_block_sz / VHD_SECTOR_SZ;
vhdm->sparse_sb_sz = vhdm->sparse_spb / 8;
if (vhdm->sparse_sb_sz % VHD_SECTOR_SZ != 0)
{vhdm->sparse_sb_sz += (vhdm->sparse_sb_sz % VHD_SECTOR_SZ);}
{
vhdm->sparse_sb_sz += (vhdm->sparse_sb_sz % VHD_SECTOR_SZ);
}
}
static void vhd_new_raw(VHDMeta *vhdm)
{
@@ -218,17 +240,21 @@ static void vhd_new_raw(VHDMeta *vhdm)
memset(&vhdm->raw_footer, 0, VHD_FOOTER_SZ);
memset(&vhdm->raw_sparse_header, 0, VHD_SPARSE_HEAD_SZ);
/* Write to footer buffer. */
strncpy((char*)vhdm->raw_footer.cookie, (char*)VFT_CONECTIX_COOKIE, sizeof(VFT_CONECTIX_COOKIE));
strncpy((char *)vhdm->raw_footer.cookie, (char *)VFT_CONECTIX_COOKIE, sizeof(VFT_CONECTIX_COOKIE));
vhdm->raw_footer.features = cpu_to_be32(0x00000002);
vhdm->raw_footer.fi_fmt_vers = cpu_to_be32(0x00010000);
if (vhdm->type == VHD_DYNAMIC)
{vhdm->raw_footer.data_offset = cpu_to_be64(vhdm->sparse_header_offset);}
{
vhdm->raw_footer.data_offset = cpu_to_be64(vhdm->sparse_header_offset);
}
else
{vhdm->raw_footer.data_offset = 0xffffffffffffffff;}
{
vhdm->raw_footer.data_offset = 0xffffffffffffffff;
}
vhdm->raw_footer.timestamp = cpu_to_be32(vhd_calc_timestamp());
strncpy((char*)vhdm->raw_footer.cr_app, (char*)VFT_CREATOR, sizeof(VFT_CREATOR));
strncpy((char *)vhdm->raw_footer.cr_app, (char *)VFT_CREATOR, sizeof(VFT_CREATOR));
vhdm->raw_footer.cr_vers = cpu_to_be32(0x000e0000);
strncpy((char*)vhdm->raw_footer.cr_host_os, (char*)VFT_CREATOR_HOST_OS, sizeof(VFT_CREATOR_HOST_OS));
strncpy((char *)vhdm->raw_footer.cr_host_os, (char *)VFT_CREATOR_HOST_OS, sizeof(VFT_CREATOR_HOST_OS));
vhdm->raw_footer.orig_sz = cpu_to_be64(vhdm->curr_size);
vhdm->raw_footer.curr_sz = cpu_to_be64(vhdm->curr_size);
vhdm->raw_footer.geom.cyl = cpu_to_be16(vhdm->geom.cyl);
@@ -240,7 +266,7 @@ static void vhd_new_raw(VHDMeta *vhdm)
memcpy(vhdm->raw_footer.uuid, uuid, sizeof(uuid));
vhdm->raw_footer.checksum = vhd_generate_be_checksum(vhdm, VHD_FIXED);
/* Write to sparse header buffer */
strncpy((char*)vhdm->raw_sparse_header.cookie, (char*)VHD_CXSPARSE_COOKIE, sizeof(VHD_CXSPARSE_COOKIE));
strncpy((char *)vhdm->raw_sparse_header.cookie, (char *)VHD_CXSPARSE_COOKIE, sizeof(VHD_CXSPARSE_COOKIE));
vhdm->raw_sparse_header.dat_offset = 0xffffffffffffffff;
vhdm->raw_sparse_header.table_offset = cpu_to_be64(vhdm->sparse_bat_offset);
vhdm->raw_sparse_header.head_vers = cpu_to_be32(0x00010000);
@@ -256,9 +282,13 @@ static int vhd_bat_from_file(VHDMeta *vhdm, FILE *f)
int ba_sz = sizeof(uint32_t) * vhdm->sparse_max_bat;
vhdm->sparse_bat_arr = malloc(ba_sz);
if (vhdm->sparse_bat_arr)
{memset(vhdm->sparse_bat_arr, 255, ba_sz);}
{
memset(vhdm->sparse_bat_arr, 255, ba_sz);
}
else
{return 0;}
{
return 0;
}
}
int b;
for (b = 0; b < vhdm->sparse_max_bat; b++)
@@ -285,22 +315,26 @@ static uint32_t vhd_generate_be_checksum(VHDMeta *vhdm, uint32_t type)
uint32_t chk = 0;
if (type == VHD_DYNAMIC)
{
uint8_t *vhd_ptr = (uint8_t*)&vhdm->raw_sparse_header;
uint8_t *vhd_ptr = (uint8_t *)&vhdm->raw_sparse_header;
int i;
for (i = 0; i < VHD_SPARSE_HEAD_SZ; i++)
{
if (i < offsetof(VHDSparseStruct, checksum) || i >= offsetof(VHDSparseStruct, par_uuid))
{chk += vhd_ptr[i];}
{
chk += vhd_ptr[i];
}
}
}
else
{
uint8_t *vft_ptr = (uint8_t*)&vhdm->raw_footer;
uint8_t *vft_ptr = (uint8_t *)&vhdm->raw_footer;
int i;
for (i = 0; i < VHD_FOOTER_SZ; i++)
{
if (i < offsetof(VHDFooterStruct, checksum) || i >= offsetof(VHDFooterStruct, uuid))
{chk += vft_ptr[i];}
{
chk += vft_ptr[i];
}
}
}
chk = ~chk;
@@ -324,7 +358,9 @@ static VHDError vhd_validate_checksum(VHDMeta *vhdm)
stored_chksum = vhdm->raw_footer.checksum;
calc_chksum = vhd_generate_be_checksum(vhdm, VHD_FIXED);
if (stored_chksum != calc_chksum)
{ret = VHD_WARN_BAD_CHECKSUM;}
{
ret = VHD_WARN_BAD_CHECKSUM;
}
return ret;
}
/* Calculate the geometry from size (in MB), using the algorithm provided in
@@ -335,7 +371,9 @@ VHDGeom vhd_calc_chs(uint32_t sz_mb)
uint32_t ts = ((uint64_t)sz_mb * 1024 * 1024) / VHD_SECTOR_SZ;
uint32_t spt, heads, cyl, cth;
if (ts > 65535 * 16 * 255)
{ts = 65535 * 16 * 255;}
{
ts = 65535 * 16 * 255;
}
if (ts >= 65535 * 16 * 63)
{
ts = 65535 * 16 * 63;
@@ -347,9 +385,11 @@ VHDGeom vhd_calc_chs(uint32_t sz_mb)
{
spt = 17;
cth = ts / spt;
heads = (cth +1023) / 1024;
heads = (cth + 1023) / 1024;
if (heads < 4)
{heads = 4;}
{
heads = 4;
}
if (cth >= (heads * 1024) || heads > 16)
{
spt = 31;
@@ -384,7 +424,7 @@ static void vhd_create_blk(VHDMeta *vhdm, FILE *f, int blk_num)
fread(ftr, 1, 512, f);
fseeko64(f, -512, SEEK_END);
/* Let's be sure we are not potentially overwriting a data block for some reason. */
if (strncmp((char*)VFT_CONECTIX_COOKIE, (char*)ftr, 8) == 0)
if (strncmp((char *)VFT_CONECTIX_COOKIE, (char *)ftr, 8) == 0)
{
uint32_t sb_sz = vhdm->sparse_sb_sz / VHD_SECTOR_SZ;
uint32_t sect_to_write = sb_sz + vhdm->sparse_spb;
@@ -392,11 +432,16 @@ static void vhd_create_blk(VHDMeta *vhdm, FILE *f, int blk_num)
for (s = 0; s < sect_to_write; s++)
{
if (s < sb_sz)
{fwrite(full_sect, VHD_SECTOR_SZ, 1, f);}
{
fwrite(full_sect, VHD_SECTOR_SZ, 1, f);
}
else
{fwrite(zero_sect, VHD_SECTOR_SZ, 1, f);}
{
fwrite(zero_sect, VHD_SECTOR_SZ, 1, f);
}
}
for (s = 0; s < VHD_BLK_PADDING_SECT; s++) {
for (s = 0; s < VHD_BLK_PADDING_SECT; s++)
{
fwrite(zero_sect, sizeof zero_sect, 1, f);
}
fwrite(ftr, VHD_FOOTER_SZ, 1, f);
@@ -411,7 +456,9 @@ int vhd_read_sectors(VHDMeta *vhdm, FILE *f, int offset, int nr_sectors, void *b
uint32_t total_sectors = vhdm->geom.cyl * vhdm->geom.heads * vhdm->geom.spt;
/* This check comes from PCem */
if ((total_sectors - offset) < transfer_sectors)
{transfer_sectors = total_sectors - offset;}
{
transfer_sectors = total_sectors - offset;
}
if (vhdm->type == VHD_DYNAMIC)
{
int start_blk = offset / vhdm->sparse_spb;
@@ -423,7 +470,9 @@ int vhd_read_sectors(VHDMeta *vhdm, FILE *f, int offset, int nr_sectors, void *b
uint32_t sib = offset % vhdm->sparse_spb;
/* If the data block doesn't yet exist, fill the buffer with zero data */
if (vhdm->sparse_bat_arr[start_blk] == VHD_SPARSE_BLK)
{memset(buffer, 0, (transfer_sectors * VHD_SECTOR_SZ));}
{
memset(buffer, 0, (transfer_sectors * VHD_SECTOR_SZ));
}
else
{
uint32_t file_sect_offs = vhdm->sparse_bat_arr[start_blk] + sbsz + sib;
@@ -442,14 +491,16 @@ int vhd_read_sectors(VHDMeta *vhdm, FILE *f, int offset, int nr_sectors, void *b
uint32_t sib = s % vhdm->sparse_spb;
/* If the data block doesn't yet exist, fill the buffer with zero data */
if (vhdm->sparse_bat_arr[blk] == VHD_SPARSE_BLK)
{memset(buffer, 0, VHD_SECTOR_SZ);}
{
memset(buffer, 0, VHD_SECTOR_SZ);
}
else
{
uint32_t file_sect_offs = vhdm->sparse_bat_arr[blk] + sbsz + sib;
fseeko64(f, (uint64_t)file_sect_offs * VHD_SECTOR_SZ, SEEK_SET);
fread(buffer, VHD_SECTOR_SZ, 1, f);
}
buffer = (uint8_t*)buffer + VHD_SECTOR_SZ;
buffer = (uint8_t *)buffer + VHD_SECTOR_SZ;
}
}
}
@@ -461,7 +512,9 @@ int vhd_read_sectors(VHDMeta *vhdm, FILE *f, int offset, int nr_sectors, void *b
fread(buffer, transfer_sectors * VHD_SECTOR_SZ, 1, f);
}
if (nr_sectors != transfer_sectors)
{ return 1;}
{
return 1;
}
return 0;
}
int vhd_write_sectors(VHDMeta *vhdm, FILE *f, int offset, int nr_sectors, void *buffer)
@@ -470,7 +523,9 @@ int vhd_write_sectors(VHDMeta *vhdm, FILE *f, int offset, int nr_sectors, void *
uint32_t total_sectors = vhdm->geom.cyl * vhdm->geom.heads * vhdm->geom.spt;
/* This check comes from PCem */
if ((total_sectors - offset) < transfer_sectors)
{transfer_sectors = total_sectors - offset;}
{
transfer_sectors = total_sectors - offset;
}
if (vhdm->type == VHD_DYNAMIC)
{
int start_blk = offset / vhdm->sparse_spb;
@@ -482,7 +537,9 @@ int vhd_write_sectors(VHDMeta *vhdm, FILE *f, int offset, int nr_sectors, void *
uint32_t sib = offset % vhdm->sparse_spb;
/* We need to create a data block if it does not yet exist. */
if (vhdm->sparse_bat_arr[start_blk] == VHD_SPARSE_BLK)
{vhd_create_blk(vhdm, f, start_blk);}
{
vhd_create_blk(vhdm, f, start_blk);
}
uint32_t file_sect_offs = vhdm->sparse_bat_arr[start_blk] + sbsz + sib;
fseeko64(f, (uint64_t)file_sect_offs * VHD_SECTOR_SZ, SEEK_SET);
fwrite(buffer, transfer_sectors * VHD_SECTOR_SZ, 1, f);
@@ -498,12 +555,14 @@ int vhd_write_sectors(VHDMeta *vhdm, FILE *f, int offset, int nr_sectors, void *
uint32_t sib = s % vhdm->sparse_spb;
/* We need to create a data block if it does not yet exist. */
if (vhdm->sparse_bat_arr[blk] == VHD_SPARSE_BLK)
{vhd_create_blk(vhdm, f, blk);}
{
vhd_create_blk(vhdm, f, blk);
}
uint32_t file_sect_offs = vhdm->sparse_bat_arr[blk] + sbsz + sib;
fseeko64(f, (uint64_t)file_sect_offs * VHD_SECTOR_SZ, SEEK_SET);
fwrite(buffer, VHD_SECTOR_SZ, 1, f);
buffer = (uint8_t*)buffer + VHD_SECTOR_SZ;
buffer = (uint8_t *)buffer + VHD_SECTOR_SZ;
}
}
}
@@ -515,7 +574,9 @@ int vhd_write_sectors(VHDMeta *vhdm, FILE *f, int offset, int nr_sectors, void *
fwrite(buffer, transfer_sectors * VHD_SECTOR_SZ, 1, f);
}
if (nr_sectors != transfer_sectors)
{return 1;}
{
return 1;
}
return 0;
}
int vhd_format_sectors(VHDMeta *vhdm, FILE *f, int offset, int nr_sectors)
@@ -526,7 +587,9 @@ int vhd_format_sectors(VHDMeta *vhdm, FILE *f, int offset, int nr_sectors)
uint32_t total_sectors = vhdm->geom.cyl * vhdm->geom.heads * vhdm->geom.spt;
/* This check comes from PCem */
if ((total_sectors - offset) < transfer_sectors)
{transfer_sectors = total_sectors - offset;}
{
transfer_sectors = total_sectors - offset;
}
if (vhdm->type == VHD_DYNAMIC)
{
@@ -571,10 +634,14 @@ int vhd_format_sectors(VHDMeta *vhdm, FILE *f, int offset, int nr_sectors)
addr = (uint64_t)offset * VHD_SECTOR_SZ;
fseeko64(f, addr, SEEK_SET);
for (c = 0; c < transfer_sectors; c++)
{fwrite(zero_buffer, VHD_SECTOR_SZ, 1, f);}
{
fwrite(zero_buffer, VHD_SECTOR_SZ, 1, f);
}
}
if (nr_sectors != transfer_sectors)
{return 1;}
{
return 1;
}
return 0;
}