summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author= <jakob@memeware.net>2017-05-15 16:41:12 -0400
committer= <jakob@memeware.net>2017-05-15 16:41:12 -0400
commitba4fad8c5a409dd0738bc09a95cbdebf360aa496 (patch)
tree989dfc5e3594a74ce2e4fcd4014b8db7b33b4629
parent2ba0b505ce1ee748f42a85f7e27906737fa47fc8 (diff)
Began finalization of the table dumping functionality
-rw-r--r--src/compress.c18
-rw-r--r--src/main.c45
-rw-r--r--src/table.c24
-rw-r--r--src/table.h5
4 files changed, 54 insertions, 38 deletions
diff --git a/src/compress.c b/src/compress.c
index 47acb74..9b6607d 100644
--- a/src/compress.c
+++ b/src/compress.c
@@ -65,7 +65,7 @@ struct stream *stream_inflate(struct stream *s, size_t len,
/* Deflates `s` into a newly allocated stream structure. */
struct stream *stream_deflate(struct stream *s, size_t len) {
struct stream *new = stream_new(len);
- z_stream strm;
+ z_stream strm;
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
@@ -73,15 +73,15 @@ struct stream *stream_deflate(struct stream *s, size_t len) {
if (deflateInit(&strm, LEVEL) != Z_OK)
return NULL;
+ strm.avail_in = len;
+ strm.next_in = (unsigned char *) s->_cur;
do {
- strm.avail_in = len;
- strm.next_in = (unsigned char *) s->_cur;
- do {
- strm.avail_out = len;
- strm.next_out = (unsigned char *) new->_cur;
- deflate(&strm, Z_NO_FLUSH); // Flush param may cause issues.
- } while (strm.avail_out == 0);
- } while (0);
+ strm.avail_out = len;
+ strm.next_out = (unsigned char *) new->_cur;
+ deflate(&strm, Z_NO_FLUSH); // Flush param may cause issues.
+ } while (strm.avail_out == 0);
+
+ new->len = len - strm.avail_out;
deflateEnd(&strm);
return new;
diff --git a/src/main.c b/src/main.c
index 8949272..49fb944 100644
--- a/src/main.c
+++ b/src/main.c
@@ -208,16 +208,16 @@ static void create_archive(char **paths, int argc, struct params p) {
return;
}
- struct table_entry *root = calloc(sizeof(struct table_entry), 1);
+ struct table_entry *root = calloc(sizeof(struct table_entry), 1);
struct table_entry *cur;
- struct header *h = create_header();
- struct stream *data = stream_new(1);
- struct stream *table = stream_new(1);
+ struct header *h = create_header();
+ struct stream *table_compressed, *data_compressed;
+ struct stream *data = stream_new(1);
+ struct stream *table = stream_new(1);
struct stream *file;
- uint64_t data_size = 0;
- uint64_t table_size;
+ uint64_t data_size = 0;
+ uint64_t table_size = 0;
- dump_header(fp, h);
for (int i = 1; i < argc - p.vararg_index; i++) {
file = stream_from_file(paths[i]);
if (file == NULL) {
@@ -228,11 +228,34 @@ static void create_archive(char **paths, int argc, struct params p) {
data_size += file->len;
stream_concat(data, file, file->len);
cur = add_file(root, paths[i]);
+ /* TODO: Separate into own function. */
+ table_size += 20 + strlen(cur->filename) * 2;
+ table_size += 28 * cur->segment_count + 48;
}
- stream_seek(data, 0, SEEK_SET);
- stream_dump(fp, data, data_size);
- table_size = dump_table(table, root);
- stream_dump(fp, table, table_size);
+
+ h->table_size = table_size;
+ h->table_offset = 40 + data_size;
+
+ dump_table(table, root);
+
+ data_compressed = stream_deflate(data, data_size);
+ table_compressed = stream_deflate(table, table_size);
+ stream_free(data);
+ stream_free(table);
+
+ dump_header(fp, h);
+ /* FIXME: Formatting and this initial seek might not be necessary. */
+ stream_seek(data_compressed, 0, SEEK_SET);
+ stream_dump(fp, data_compressed, data_size);
+
+ /* FIXME: May be an incompatible value. Also, move this shitty hack. */
+ uint8_t compressed = 0x80;
+ uint64_t len = table_compressed->len, decompressed_len = table_size;
+ fwrite(&compressed, sizeof(uint8_t), 1, fp);
+ fwrite(&len, sizeof(uint64_t), 1, fp);
+ fwrite(&decompressed_len, sizeof(uint64_t), 1, fp);
+
+ stream_dump(fp, table_compressed, table_size);
}
diff --git a/src/table.c b/src/table.c
index 6a88d56..1bb14cc 100644
--- a/src/table.c
+++ b/src/table.c
@@ -144,9 +144,8 @@ void read_file(struct stream *s, struct table_entry *root) {
}
-/* Dumps the File entry for `cur` into the table at `s` and returns the
- number of bytes written. */
-static uint64_t dump_file(struct stream *s, struct table_entry *cur) {
+/* Dumps the File entry for `cur` into the table at `s`. */
+static void dump_file(struct stream *s, struct table_entry *cur) {
uint32_t magic = FILE_MAGIC;
uint64_t bytes_written = 28 * cur->segment_count + 48;
stream_write(s, &magic, sizeof(uint32_t));
@@ -159,7 +158,6 @@ static uint64_t dump_file(struct stream *s, struct table_entry *cur) {
stream_seek(s, -bytes_written, SEEK_CUR);
stream_write(s, &bytes_written, sizeof(uint64_t));
stream_seek(s, bytes_written, SEEK_CUR);
- return bytes_written;
}
@@ -211,9 +209,8 @@ void read_elif(struct stream *s, struct table_entry *root) {
}
-/* Dumps the eliF entry for `cur` into the table at `s` and returns the
- number of bytes written. */
-static uint64_t dump_elif(struct stream *s, struct table_entry *cur) {
+/* Dumps the eliF entry for `cur` into the table at `s`. */
+static void dump_elif(struct stream *s, struct table_entry *cur) {
uint32_t magic = ELIF_MAGIC;
uint16_t name_len = strlen(cur->filename);
uint64_t entry_size = name_len * 2 + 8;
@@ -225,7 +222,6 @@ static uint64_t dump_elif(struct stream *s, struct table_entry *cur) {
stream_write(s, &cur->key, sizeof(uint32_t));
stream_write(s, &name_len, sizeof(uint16_t));
stream_write(s, encoded, name_len * 2 + 2);
- return 18 + name_len * 2 + 2;
}
@@ -260,17 +256,15 @@ struct table_entry *read_table(struct stream *s) {
}
-/* Dumps the XP3 table specified by `root` into `s` and returns the
- number of bytes written. */
-uint64_t dump_table(struct stream *s, struct table_entry *root) {
- uint64_t bytes_written = 0;
+/* Dumps the XP3 table specified by `root` into `s`. */
+void dump_table(struct stream *s, struct table_entry *root) {
struct table_entry *cur;
for (cur = root->next; cur != NULL; cur = cur->next) {
- bytes_written += dump_elif(s, cur);
- bytes_written += dump_file(s, cur);
+ dump_elif(s, cur);
+ dump_file(s, cur);
}
+ /* FIXME: This should be moved. */
stream_seek(s, 0, SEEK_SET);
- return bytes_written;
}
diff --git a/src/table.h b/src/table.h
index 74de5b0..90c9b1a 100644
--- a/src/table.h
+++ b/src/table.h
@@ -60,9 +60,8 @@ void read_elif(struct stream *s, struct table_entry *root);
in the archive's table section. */
struct table_entry *read_table(struct stream *s);
-/* Dumps the XP3 table specified by `root` into `s` and returns the
- number of bytes written. */
-uint64_t dump_table(struct stream *s, struct table_entry *root);
+/* Dumps the XP3 table specified by `root` into `s`. */
+void dump_table(struct stream *s, struct table_entry *root);
/* Inserts the file specified by `path` into the table linked list
specified by `root`. */