summaryrefslogtreecommitdiff
path: root/mpq-file.c
diff options
context:
space:
mode:
authorpixel <pixel>2007-07-11 14:23:29 +0000
committerpixel <pixel>2007-07-11 14:23:29 +0000
commit26fcbbdd26a389b6979a966ae8dd49a40a6a1d05 (patch)
treebbf63c4080bcba14aa7d02176dc23b920a70514c /mpq-file.c
parent2b81447acc91685dbfb6419282a0f53b6ac3eaa0 (diff)
Adding comments.
Diffstat (limited to 'mpq-file.c')
-rw-r--r--mpq-file.c15
1 files changed, 15 insertions, 0 deletions
diff --git a/mpq-file.c b/mpq-file.c
index 7bafd2f..f6a26d8 100644
--- a/mpq-file.c
+++ b/mpq-file.c
@@ -33,7 +33,10 @@ struct mpq_file_t * mpqlib_open_file(struct mpq_archive_t * mpq_a, int entry) {
__mpqlib_errno = MPQLIB_ERROR_NO_ERROR;
+ /* Basic checks of the entry. */
+
if (mpqlib_ioctl(mpq_a, MPQLIB_IOCTL_ENTRY_EXISTS, entry)) {
+ __mpqlib_errno = MPQLIB_ERROR_FILE_NOT_FOUND;
return NULL;
}
@@ -49,6 +52,7 @@ struct mpq_file_t * mpqlib_open_file(struct mpq_archive_t * mpq_a, int entry) {
return NULL;
}
+ // We won't support anything else than compressed files yet.
if (!(flags & MPQ_FLAGS_COMPRESSED)) {
__mpqlib_errno = MPQLIB_ERROR_NOT_COMPRESSED;
return NULL;
@@ -61,6 +65,7 @@ struct mpq_file_t * mpqlib_open_file(struct mpq_archive_t * mpq_a, int entry) {
r->sector_size = mpqlib_ioctl(mpq_a, MPQLIB_IOCTL_GET_SECTOR_SIZE, 0);
+ /* Allocating and filling up the mpq_file_t structure. */
if (!(r->buffer = malloc(r->sector_size))) {
free(r);
__mpqlib_errno = MPQLIB_ERROR_MEMORY;
@@ -95,6 +100,7 @@ struct mpq_file_t * mpqlib_open_file(struct mpq_archive_t * mpq_a, int entry) {
return NULL;
}
+ /* Reading the offset table and building the size table */
__mpqlib_seek(mpq_a, r->offset);
for (i = 0; i <= r->number_of_sectors; i++) {
__mpqlib_read(mpq_a, &o, 4);
@@ -128,6 +134,7 @@ static int cache_sector(struct mpq_file_t * mpq_f, int sector) {
static char in_buf[MPQ_BUFSIZ];
int r = 1;
+ /* current_sector should be initialized to -1 */
if (mpq_f->current_sector != sector) {
int il, ol;
__mpqlib_seek(mpq_f->mpq_a, mpq_f->offsets[sector]);
@@ -145,6 +152,10 @@ static int cache_sector(struct mpq_file_t * mpq_f, int sector) {
return r;
}
+/*
+ * The read function will call itself recursively, in order to split the calls across the sectors.
+ */
+
uint32_t mpqlib_read(struct mpq_file_t * mpq_f, void * _buffer, uint32_t size) {
char * buffer = (char *) _buffer;
int sector_begin, sector_end;
@@ -155,6 +166,7 @@ uint32_t mpqlib_read(struct mpq_file_t * mpq_f, void * _buffer, uint32_t size) {
uint32_t first_sector_begins, last_sector_ends;
+ /* Computing various cursors and stuff. */
if ((size + mpq_f->cursor) >= mpq_f->file_size)
size = mpq_f->file_size - mpq_f->cursor;
@@ -171,17 +183,20 @@ uint32_t mpqlib_read(struct mpq_file_t * mpq_f, void * _buffer, uint32_t size) {
last_sector_ends = mpq_f->sector_size;
}
+ /* Let's ask for the first sector */
if (!cache_sector(mpq_f, sector_begin)) {
__mpqlib_errno = MPQLIB_ERROR_COMPRESSION;
return 0;
}
+ /* If we've hit the end sector, let's just copy that over and exit. */
if (sector_begin == sector_end) {
memcpy(buffer, mpq_f->buffer + first_sector_begins, size);
mpq_f->cursor += size;
return size;
}
+ /* Else, let's compute the cluster size, copy that chunk, and recurse. */
cl_size = mpq_f->sector_size - first_sector_begins;
memcpy(buffer, mpq_f->buffer + first_sector_begins, cl_size);
mpq_f->cursor += cl_size;