From 18ceb4dedfd48580a5649f8c31aaf72614790e72 Mon Sep 17 00:00:00 2001 From: pixel Date: Mon, 9 Jul 2007 19:06:25 +0000 Subject: Simplified writing. --- int-bios.h | 6 +- mpq-bios.c | 186 +++++++++++++++++++++++++++++-------------------------------- mpq-bios.h | 18 +++--- mpq-file.c | 4 +- mpq-file.h | 16 ++---- test-it.c | 2 +- 6 files changed, 106 insertions(+), 126 deletions(-) diff --git a/int-bios.h b/int-bios.h index 1c05d29..10a4a44 100644 --- a/int-bios.h +++ b/int-bios.h @@ -19,8 +19,8 @@ typedef struct { #define FLAGS_FILE_ENCRYPTED 0x00010000 #define FLAGS_FILE_EXISTS 0x80000000 -const block_t * __mpqlib_get_block_entry(mpq_archive_t *, int); -int __mpqlib_seek(mpq_archive_t *, off_t); -int __mpqlib_read(mpq_archive_t *, void *, size_t); +const block_t * __mpqlib_get_block_entry(struct mpq_archive_t *, int); +int __mpqlib_seek(struct mpq_archive_t *, off_t); +int __mpqlib_read(struct mpq_archive_t *, void *, size_t); #endif diff --git a/mpq-bios.c b/mpq-bios.c index 055fa48..d8be0bd 100644 --- a/mpq-bios.c +++ b/mpq-bios.c @@ -67,7 +67,7 @@ typedef struct { */ -struct mpq_internals_t { +struct mpq_archive_t { int fd; int closeit; @@ -91,9 +91,9 @@ struct mpq_internals_t { #define O_BINARY 0 #endif -mpq_archive_t * mpqlib_open_archive(const char * fname) { +struct mpq_archive_t * mpqlib_open_archive(const char * fname) { int fd; - mpq_archive_t * r; + struct mpq_archive_t * r; if ((fd = open(fname, O_RDONLY | O_LARGEFILE | O_BINARY)) == -1) { __mpqlib_errno = MPQLIB_ERROR_OPEN; @@ -102,7 +102,7 @@ mpq_archive_t * mpqlib_open_archive(const char * fname) { r = mpqlib_reopen_archive(fd); if (r) { - r->mpq_i->closeit = 1; + r->closeit = 1; } else { close(fd); } @@ -115,7 +115,6 @@ mpq_archive_t * mpqlib_open_archive(const char * fname) { #define no_mpq() { \ __mpqlib_errno = MPQLIB_ERROR_NOT_MPQ_ARCHIVE; \ - free(mpq_i); \ free(mpq_a); \ return NULL; \ } @@ -128,15 +127,14 @@ mpq_archive_t * mpqlib_open_archive(const char * fname) { return NULL; \ } -static void free_archive(mpq_archive_t * mpq_a) { - free(mpq_a->mpq_i->hashs); - free(mpq_a->mpq_i->blocks); - free(mpq_a->mpq_i); +static void free_archive(struct mpq_archive_t * mpq_a) { + free(mpq_a->hashs); + free(mpq_a->blocks); free(mpq_a); } -static int read_data(mpq_archive_t * mpq_a, void * buf, size_t l) { - if (read(mpq_a->mpq_i->fd, buf, l) != l) +static int read_data(struct mpq_archive_t * mpq_a, void * buf, size_t l) { + if (read(mpq_a->fd, buf, l) != l) return 0; return 1; } @@ -145,18 +143,17 @@ void mpqlib_init() { __mpqlib_init_cryptography(); } -void mpqlib_close_archive(mpq_archive_t * mpq_a) { +void mpqlib_close_archive(struct mpq_archive_t * mpq_a) { if (mpq_a) { - if (mpq_a->mpq_i->closeit) { - close(mpq_a->mpq_i->fd); + if (mpq_a->closeit) { + close(mpq_a->fd); } free_archive(mpq_a); } } -mpq_archive_t * mpqlib_reopen_archive(int fd) { - struct mpq_internals_t * mpq_i; - mpq_archive_t * mpq_a; +struct mpq_archive_t * mpqlib_reopen_archive(int fd) { + struct mpq_archive_t * mpq_a; mpq_header_t mpq_h; mpq_hash_t * mpq_hashs; mpq_block_t * mpq_blocks; @@ -166,20 +163,13 @@ mpq_archive_t * mpqlib_reopen_archive(int fd) { /****TODO****/ /* Implement endianess a bit everywhere */ - if (!(mpq_i = (struct mpq_internals_t *) malloc(sizeof(struct mpq_internals_t)))) { + if (!(mpq_a = (struct mpq_archive_t *) malloc(sizeof(struct mpq_archive_t)))) { __mpqlib_errno = MPQLIB_ERROR_MEMORY; return NULL; } - if (!(mpq_a = (mpq_archive_t *) malloc(sizeof(mpq_archive_t)))) { - __mpqlib_errno = MPQLIB_ERROR_MEMORY; - free(mpq_i); - return NULL; - } - - mpq_i->closeit = 0; - mpq_i->fd = fd; - mpq_a->mpq_i = mpq_i; + mpq_a->closeit = 0; + mpq_a->fd = fd; if (read(fd, &mpq_h, STD_HEADER_SIZE) != STD_HEADER_SIZE) no_mpq(); @@ -199,60 +189,58 @@ mpq_archive_t * mpqlib_reopen_archive(int fd) { mpq_h.block_table_offset_high = 0; } - mpq_i->header_size = mpq_h.header_size; - mpq_i->archive_size = mpq_h.archive_size; - mpq_i->format_version = mpq_h.format_version; - mpq_i->sector_size = 0x200 << mpq_h.sector_size; + mpq_a->header_size = mpq_h.header_size; + mpq_a->archive_size = mpq_h.archive_size; + mpq_a->format_version = mpq_h.format_version; + mpq_a->sector_size = 0x200 << mpq_h.sector_size; - mpq_i->hash_table_offset = (uint64_t) mpq_h.hash_table_offset + (((uint64_t) mpq_h.hash_table_offset_high) << 32); - mpq_i->block_table_offset = (uint64_t) mpq_h.block_table_offset + (((uint64_t) mpq_h.block_table_offset_high) << 32); - mpq_i->hash_table_entries = mpq_h.hash_table_entries; - mpq_i->block_table_entries = mpq_h.block_table_entries; - mpq_i->extended_block_table_offset = mpq_h.extended_block_table_offset; + mpq_a->hash_table_offset = (uint64_t) mpq_h.hash_table_offset + (((uint64_t) mpq_h.hash_table_offset_high) << 32); + mpq_a->block_table_offset = (uint64_t) mpq_h.block_table_offset + (((uint64_t) mpq_h.block_table_offset_high) << 32); + mpq_a->hash_table_entries = mpq_h.hash_table_entries; + mpq_a->block_table_entries = mpq_h.block_table_entries; + mpq_a->extended_block_table_offset = mpq_h.extended_block_table_offset; - if (!(mpq_i->hashs = (hash_t *) malloc(mpq_i->hash_table_entries * sizeof(hash_t)))) { + if (!(mpq_a->hashs = (hash_t *) malloc(mpq_a->hash_table_entries * sizeof(hash_t)))) { __mpqlib_errno = MPQLIB_ERROR_MEMORY; - free(mpq_i); free(mpq_a); return NULL; } - if (!(mpq_i->blocks = (block_t *) malloc(mpq_i->block_table_entries * sizeof(block_t)))) { + if (!(mpq_a->blocks = (block_t *) malloc(mpq_a->block_table_entries * sizeof(block_t)))) { __mpqlib_errno = MPQLIB_ERROR_MEMORY; - free(mpq_i->hashs); - free(mpq_i); + free(mpq_a->hashs); free(mpq_a); return NULL; } - if (!(mpq_hashs = (mpq_hash_t *) malloc(mpq_i->hash_table_entries * sizeof(mpq_hash_t)))) { + if (!(mpq_hashs = (mpq_hash_t *) malloc(mpq_a->hash_table_entries * sizeof(mpq_hash_t)))) { __mpqlib_errno = MPQLIB_ERROR_MEMORY; free_archive(mpq_a); return NULL; } - if (!(mpq_blocks = (mpq_block_t *) malloc(mpq_i->block_table_entries * sizeof(mpq_block_t)))) { + if (!(mpq_blocks = (mpq_block_t *) malloc(mpq_a->block_table_entries * sizeof(mpq_block_t)))) { __mpqlib_errno = MPQLIB_ERROR_MEMORY; free(mpq_hashs); free_archive(mpq_a); return NULL; } - if (lseek64(fd, mpq_i->hash_table_offset, SEEK_SET) != mpq_i->hash_table_offset) + if (lseek64(fd, mpq_a->hash_table_offset, SEEK_SET) != mpq_a->hash_table_offset) read_error(); - if (!read_data(mpq_a, mpq_hashs, mpq_i->hash_table_entries * sizeof(mpq_hash_t))) + if (!read_data(mpq_a, mpq_hashs, mpq_a->hash_table_entries * sizeof(mpq_hash_t))) read_error(); - if (lseek64(fd, mpq_i->block_table_offset, SEEK_SET) != mpq_i->block_table_offset) + if (lseek64(fd, mpq_a->block_table_offset, SEEK_SET) != mpq_a->block_table_offset) read_error(); - if (!read_data(mpq_a, mpq_blocks, mpq_i->block_table_entries * sizeof(mpq_block_t))) + if (!read_data(mpq_a, mpq_blocks, mpq_a->block_table_entries * sizeof(mpq_block_t))) read_error(); - if (mpq_i->extended_block_table_offset) { - if (lseek64(fd, mpq_i->block_table_offset, SEEK_SET)) + if (mpq_a->extended_block_table_offset) { + if (lseek64(fd, mpq_a->block_table_offset, SEEK_SET)) read_error(); - if (!(mpq_extblocks = (uint16_t *) malloc(mpq_i->block_table_entries * sizeof(uint16_t)))) { + if (!(mpq_extblocks = (uint16_t *) malloc(mpq_a->block_table_entries * sizeof(uint16_t)))) { __mpqlib_errno = MPQLIB_ERROR_MEMORY; free_archive(mpq_a); free(mpq_hashs); @@ -260,32 +248,32 @@ mpq_archive_t * mpqlib_reopen_archive(int fd) { return NULL; } - if (!read_data(mpq_a, mpq_blocks, mpq_i->block_table_entries * sizeof(mpq_block_t))) { + if (!read_data(mpq_a, mpq_blocks, mpq_a->block_table_entries * sizeof(mpq_block_t))) { free(mpq_extblocks); read_error(); } } - __mpqlib_decrypt(mpq_hashs, mpq_i->hash_table_entries * sizeof(mpq_block_t), __mpqlib_hash_cstring("(hash table)", 3), 1); - __mpqlib_decrypt(mpq_blocks, mpq_i->block_table_entries * sizeof(mpq_block_t), __mpqlib_hash_cstring("(block table)", 3), 1); + __mpqlib_decrypt(mpq_hashs, mpq_a->hash_table_entries * sizeof(mpq_block_t), __mpqlib_hash_cstring("(hash table)", 3), 1); + __mpqlib_decrypt(mpq_blocks, mpq_a->block_table_entries * sizeof(mpq_block_t), __mpqlib_hash_cstring("(block table)", 3), 1); - for (i = 0; i < mpq_i->hash_table_entries; i++) { + for (i = 0; i < mpq_a->hash_table_entries; i++) { /****TODO****/ /* Implement various checks of the hash table. */ - mpq_i->hashs[i].file_path_hasha = mpq_hashs[i].file_path_hasha; - mpq_i->hashs[i].file_path_hashb = mpq_hashs[i].file_path_hashb; - mpq_i->hashs[i].language = mpq_hashs[i].language; - mpq_i->hashs[i].platform = mpq_hashs[i].platform; - mpq_i->hashs[i].file_block_index = mpq_hashs[i].file_block_index; + mpq_a->hashs[i].file_path_hasha = mpq_hashs[i].file_path_hasha; + mpq_a->hashs[i].file_path_hashb = mpq_hashs[i].file_path_hashb; + mpq_a->hashs[i].language = mpq_hashs[i].language; + mpq_a->hashs[i].platform = mpq_hashs[i].platform; + mpq_a->hashs[i].file_block_index = mpq_hashs[i].file_block_index; } - for (i = 0; i < mpq_i->block_table_entries; i++) { + for (i = 0; i < mpq_a->block_table_entries; i++) { /****TODO****/ /* Implement various checks of the block table. */ - mpq_i->blocks[i].block_offset = mpq_blocks[i].block_offset + (mpq_extblocks ? (((uint64_t) mpq_extblocks[i]) << 32): 0); - mpq_i->blocks[i].block_size = mpq_blocks[i].block_size; - mpq_i->blocks[i].file_size = mpq_blocks[i].file_size; - mpq_i->blocks[i].flags = mpq_blocks[i].flags; + mpq_a->blocks[i].block_offset = mpq_blocks[i].block_offset + (mpq_extblocks ? (((uint64_t) mpq_extblocks[i]) << 32): 0); + mpq_a->blocks[i].block_size = mpq_blocks[i].block_size; + mpq_a->blocks[i].file_size = mpq_blocks[i].file_size; + mpq_a->blocks[i].flags = mpq_blocks[i].flags; } if (mpq_extblocks) @@ -297,98 +285,98 @@ mpq_archive_t * mpqlib_reopen_archive(int fd) { return mpq_a; } -void mpqlib_printtables(mpq_archive_t * mpq_a) { +void mpqlib_printtables(struct mpq_archive_t * mpq_a) { int i; printf("Archive details:\n"); - printf("Header size: %8d\n", mpq_a->mpq_i->header_size); - printf("Archive size: %8d\n", mpq_a->mpq_i->archive_size); - printf("Format version: %8d\n", mpq_a->mpq_i->format_version); - printf("Sector size: %8d\n\n", mpq_a->mpq_i->sector_size); + printf("Header size: %8d\n", mpq_a->header_size); + printf("Archive size: %8d\n", mpq_a->archive_size); + printf("Format version: %8d\n", mpq_a->format_version); + printf("Sector size: %8d\n\n", mpq_a->sector_size); - printf("Hash table offset: %016llX\n", mpq_a->mpq_i->hash_table_offset); - printf("Block table offset: %016llX\n", mpq_a->mpq_i->block_table_offset); - printf("Hash table entries: %8d\n", mpq_a->mpq_i->hash_table_entries); - printf("Block table entries: %8d\n", mpq_a->mpq_i->block_table_entries); + printf("Hash table offset: %016llX\n", mpq_a->hash_table_offset); + printf("Block table offset: %016llX\n", mpq_a->block_table_offset); + printf("Hash table entries: %8d\n", mpq_a->hash_table_entries); + printf("Block table entries: %8d\n", mpq_a->block_table_entries); - printf("Extended block table offset: %016llX\n\n\n", mpq_a->mpq_i->extended_block_table_offset); + printf("Extended block table offset: %016llX\n\n\n", mpq_a->extended_block_table_offset); printf("Hash table dump.\n"); printf("HashA HashB language platform index\n"); printf("-------- -------- -------- -------- -----\n"); - for (i = 0; i < mpq_a->mpq_i->hash_table_entries; i++) { - printf("%08X %08X %08X %08X %d\n", mpq_a->mpq_i->hashs[i].file_path_hasha, mpq_a->mpq_i->hashs[i].file_path_hashb, mpq_a->mpq_i->hashs[i].language, mpq_a->mpq_i->hashs[i].platform, mpq_a->mpq_i->hashs[i].file_block_index); + for (i = 0; i < mpq_a->hash_table_entries; i++) { + printf("%08X %08X %08X %08X %d\n", mpq_a->hashs[i].file_path_hasha, mpq_a->hashs[i].file_path_hashb, mpq_a->hashs[i].language, mpq_a->hashs[i].platform, mpq_a->hashs[i].file_block_index); } printf("\n\nBlock table dump.\n"); printf("Entry Block offset size filesize flags\n"); printf("-------- ---------------- -------- -------- --------\n"); - for (i = 0; i < mpq_a->mpq_i->block_table_entries; i++) { - printf("%8d %016llX %8d %8d %08X\n", i, mpq_a->mpq_i->blocks[i].block_offset, mpq_a->mpq_i->blocks[i].block_size, mpq_a->mpq_i->blocks[i].file_size, mpq_a->mpq_i->blocks[i].flags); + for (i = 0; i < mpq_a->block_table_entries; i++) { + printf("%8d %016llX %8d %8d %08X\n", i, mpq_a->blocks[i].block_offset, mpq_a->blocks[i].block_size, mpq_a->blocks[i].file_size, mpq_a->blocks[i].flags); } } -int mpqlib_find_hash_entry(mpq_archive_t * mpq_a, const char * name, uint32_t language, uint32_t platform) { +int mpqlib_find_hash_entry(struct mpq_archive_t * mpq_a, const char * name, uint32_t language, uint32_t platform) { int i; uint32_t hA, hB; hA = mpqlib_hashA_filename(name); hB = mpqlib_hashB_filename(name); - for (i = 0; i < mpq_a->mpq_i->hash_table_entries; i++) { - if ((mpq_a->mpq_i->hashs[i].file_path_hasha == hA) && - (mpq_a->mpq_i->hashs[i].file_path_hashb == hB) && - (mpq_a->mpq_i->hashs[i].language == language) && - (mpq_a->mpq_i->hashs[i].platform == platform)) { - return mpq_a->mpq_i->hashs[i].file_block_index; + for (i = 0; i < mpq_a->hash_table_entries; i++) { + if ((mpq_a->hashs[i].file_path_hasha == hA) && + (mpq_a->hashs[i].file_path_hashb == hB) && + (mpq_a->hashs[i].language == language) && + (mpq_a->hashs[i].platform == platform)) { + return mpq_a->hashs[i].file_block_index; } } return -1; } -const block_t * __mpqlib_get_block_entry(mpq_archive_t * mpq_a, int entry) { - if ((entry >= mpq_a->mpq_i->block_table_entries) || (entry < 0)) +const block_t * __mpqlib_get_block_entry(struct mpq_archive_t * mpq_a, int entry) { + if ((entry >= mpq_a->block_table_entries) || (entry < 0)) return NULL; - return mpq_a->mpq_i->blocks + entry; + return mpq_a->blocks + entry; } -int __mpqlib_seek(mpq_archive_t * mpq_a, off_t off) { - if (lseek64(mpq_a->mpq_i->fd, off, SEEK_SET) != off) +int __mpqlib_seek(struct mpq_archive_t * mpq_a, off_t off) { + if (lseek64(mpq_a->fd, off, SEEK_SET) != off) return 0; return 1; } -int __mpqlib_read(mpq_archive_t * mpq_a, void * buffer, size_t size) { +int __mpqlib_read(struct mpq_archive_t * mpq_a, void * buffer, size_t size) { return read_data(mpq_a, buffer, size); } -uint64_t mpqlib_ioctl(mpq_archive_t * mpq_a, enum mpqlib_ioctl_t command, int entry) { +uint64_t mpqlib_ioctl(struct mpq_archive_t * mpq_a, enum mpqlib_ioctl_t command, int entry) { switch(command) { case MPQLIB_IOCTL_NO_ACTION: return 0; case MPQLIB_IOCTL_GET_FORMAT_VERSION: - return mpq_a->mpq_i->format_version; + return mpq_a->format_version; case MPQLIB_IOCTL_GET_SECTOR_SIZE: - return mpq_a->mpq_i->sector_size; + return mpq_a->sector_size; case MPQLIB_IOCTL_GET_BLOCK_OFFSET: case MPQLIB_IOCTL_GET_BLOCK_SIZE: case MPQLIB_IOCTL_GET_FILE_SIZE: case MPQLIB_IOCTL_GET_FLAGS: - if (entry >= mpq_a->mpq_i->block_table_entries) { + if (entry >= mpq_a->block_table_entries) { __mpqlib_errno = MPQLIB_ERROR_IOCTL_INVALID_ENTRY; return -1; } switch (command) { case MPQLIB_IOCTL_GET_BLOCK_OFFSET: - return mpq_a->mpq_i->blocks[entry].block_offset; + return mpq_a->blocks[entry].block_offset; case MPQLIB_IOCTL_GET_BLOCK_SIZE: - return mpq_a->mpq_i->blocks[entry].block_size; + return mpq_a->blocks[entry].block_size; case MPQLIB_IOCTL_GET_FILE_SIZE: - return mpq_a->mpq_i->blocks[entry].file_size; + return mpq_a->blocks[entry].file_size; case MPQLIB_IOCTL_GET_FLAGS: - return mpq_a->mpq_i->blocks[entry].flags; + return mpq_a->blocks[entry].flags; default: // should never get there. __mpqlib_errno = MPQLIB_ERROR_UNKNOWN; diff --git a/mpq-bios.h b/mpq-bios.h index 138367d..a30ef16 100644 --- a/mpq-bios.h +++ b/mpq-bios.h @@ -3,11 +3,7 @@ #include "inttypes.h" -struct mpq_internals_t; - -typedef struct { - struct mpq_internals_t * mpq_i; -} mpq_archive_t; +struct mpq_archive_t; #define MPQ_FLAGS_ISFILE 0x80000000 #define MPQ_FLAGS_HAS_META 0x04000000 @@ -32,12 +28,12 @@ extern "C" { #endif void mpqlib_init(); -mpq_archive_t * mpqlib_open_archive(const char * fname); -mpq_archive_t * mpqlib_reopen_archive(int fd); -void mpqlib_printtables(mpq_archive_t *); -void mpqlib_close_archive(mpq_archive_t *); -int mpqlib_find_hash_entry(mpq_archive_t *, const char * name, uint32_t language, uint32_t platform); -uint64_t mpqlib_ioctl(mpq_archive_t *, enum mpqlib_ioctl_t command, int entry); +struct mpq_archive_t * mpqlib_open_archive(const char * fname); +struct mpq_archive_t * mpqlib_reopen_archive(int fd); +void mpqlib_printtables(struct mpq_archive_t *); +void mpqlib_close_archive(struct mpq_archive_t *); +int mpqlib_find_hash_entry(struct mpq_archive_t *, const char * name, uint32_t language, uint32_t platform); +uint64_t mpqlib_ioctl(struct mpq_archive_t *, enum mpqlib_ioctl_t command, int entry); #ifdef __cplusplus } diff --git a/mpq-file.c b/mpq-file.c index 27cc9f2..2fc372b 100644 --- a/mpq-file.c +++ b/mpq-file.c @@ -1,8 +1,8 @@ #include "mpq-bios.h" #include "mpq-file.h" -struct mpq_file_internals_t { - mpq_archive_t * mpq_a; +struct mpq_file_t { + struct mpq_archive_t * mpq_a; int entry; int number_of_sectors; int remaining_bytes; diff --git a/mpq-file.h b/mpq-file.h index d94a2cf..53b514e 100644 --- a/mpq-file.h +++ b/mpq-file.h @@ -3,21 +3,17 @@ #include "inttypes.h" -struct mpq_file_internals_t; +struct mpq_file_t; -typedef struct { - struct mpq_file_internals_t * mpq_i; -} mpq_file_t; - -enum mpqfile_seek_t { +enum mpqlib_file_seek_t { MPQLIB_SEEK_SET, MPQLIB_SEEK_CUR, MPQLIB_SEEK_END, }; -mpq_file_t * mpqlib_open_file(mpq_archive_t * mpq_a, int entry); -mpq_file_t * mpqlib_open_filename(mpq_archive_t * mpq_a, const char * fname); -uint32_t mpqlib_read(mpq_file_t * mpq_f, void * buffer, uint32_t size); -uint32_t mpqlib_seek(mpq_file_t, int32_t offset, enum mpqfile_seek_t); +struct mpq_file_t * mpqlib_open_file(struct mpq_archive_t * mpq_a, int entry); +struct mpq_file_t * mpqlib_open_filename(struct mpq_archive_t * mpq_a, const char * fname); +uint32_t mpqlib_read(struct mpq_file_t * mpq_f, void * buffer, uint32_t size); +uint32_t mpqlib_seek(struct mpq_file_t * mpq_f, int32_t offset, enum mpqlib_file_seek_t); #endif diff --git a/test-it.c b/test-it.c index b6331d9..5a0b76b 100644 --- a/test-it.c +++ b/test-it.c @@ -4,7 +4,7 @@ #include "mpq-errors.h" int main(int argc, char ** argv) { - mpq_archive_t * t1; + struct mpq_archive_t * t1; char * fname = "test.mpq"; mpqlib_init(); -- cgit v1.2.3