summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--mdxtest.c134
1 files changed, 70 insertions, 64 deletions
diff --git a/mdxtest.c b/mdxtest.c
index 97e29dd..b8f57a2 100644
--- a/mdxtest.c
+++ b/mdxtest.c
@@ -3,6 +3,8 @@
#define _GNU_SOURCE /* for strnlen */
#define HAS_READLINE /* remove if you don't have readline */
+//#define DEBUG
+
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
@@ -14,6 +16,12 @@
#endif
#include "viewhtml.h"
+#ifdef DEBUG
+# define debug(...) fprintf(stderr, __VA_ARGS__)
+#else
+# define debug(...) do {} while (0)
+#endif
+
typedef enum {
MDE_Success = 0,
MDE_BadSignature,
@@ -198,9 +206,9 @@ static int mdstrcmp(const char *a, const char *b) {
int i = strcasecmp(a, b);
int j = _mdstrcmp(a, b);
- fprintf(stderr, "strcasecmp %3d, _mdstrcmp %3d\n", i, j);
+ debug("strcasecmp %3d, _mdstrcmp %3d\n", i, j);
if (sign(i) != sign(j)) {
- fprintf(stderr, " %s\n %s\n", a, b);
+ debug(" %s\n %s\n", a, b);
abort();
}
return i;
@@ -244,7 +252,7 @@ MDError parse_index_entry(const MDFile *file, uint8_t *data, bool last,
check(get16(data, len, offset, &namelen));
check(getstring(data, len, offset, namelen, &entry->last_word));
- //fprintf(stderr, "from: %s\n", entry->last_word);
+ //debug("from: %s\n", entry->last_word);
check(get32(data, len, offset, &entry->unknown1));
check(get32(data, len, offset, &entry->compressed_size));
@@ -263,7 +271,7 @@ MDError parse_index_entry(const MDFile *file, uint8_t *data, bool last,
check(get16(data, len, offset, &namelen));
check(getstring(data, len, offset, namelen, &entry->next_word));
- //fprintf(stderr, "from: %s to: %s\n", entry->last_word, entry->next_word);
+ //debug("from: %s to: %s\n", entry->last_word, entry->next_word);
return MDE_Success;
}
@@ -286,8 +294,8 @@ MDError read_index(FILE *input, MDFile *file) {
free(index);
return MDE_TruncatedFile;
}
- //fprintf(stderr, "compr crc32: %lx\n", crc32(0, compressed, csize));
- //for (int i = 0; i < csize; i++) fprintf(stderr, "%hx ", compressed[i]);
+ //debug("compr crc32: %lx\n", crc32(0, compressed, csize));
+ //for (int i = 0; i < csize; i++) debug("%hx ", compressed[i]);
int zstatus = uncompress(index, &usize,
compressed, csize);
free(compressed);
@@ -298,9 +306,9 @@ MDError read_index(FILE *input, MDFile *file) {
//file->index_count = byteswap32(*(uint32_t*)(index+4));
- fprintf(stderr, "unknown value: %d\n", byteswap32(*(uint32_t*)(index+4)));
- fprintf(stderr, "uncompr crc32: %lx\n", crc32(0, index, usize));
- fprintf(stderr, " adler32: %lx\n", adler32(0, index, usize));
+ debug("unknown value: %d\n", byteswap32(*(uint32_t*)(index+4)));
+ debug("uncompr crc32: %lx\n", crc32(0, index, usize));
+ debug(" adler32: %lx\n", adler32(0, index, usize));
file->index = malloc((file->header.index_entries)*sizeof(MDIndexEntry));
if (!file->index) {
@@ -310,7 +318,7 @@ MDError read_index(FILE *input, MDFile *file) {
size_t offset = 11;
off_t dataptr = ftello(input);
for (size_t i = 0; i < file->header.index_entries; i++) {
- //fprintf(stderr, "parsing entry %d. dataptr = %qd\n", i, dataptr);
+ //debug("parsing entry %d. dataptr = %qd\n", i, dataptr);
check(parse_index_entry(file, index, (i == file->header.index_entries-1),
&offset, &file->index[i]));
file->index[i].offset = dataptr+8;
@@ -329,14 +337,14 @@ MDError get_block_number(const MDFile *file, const char *word, size_t *number) {
mdstrcmp(word, file->index[i].last_word),
mdstrcmp(word, file->index[i].next_word));*/
-// fprintf(stderr, "compare %20s < %20s < %20s\n", first_word, word, file->index[i].last_word);
+// debug("compare %20s < %20s < %20s\n", first_word, word, file->index[i].last_word);
if (mdstrcmp(word, first_word) < 0) {
fprintf(stderr, "can't get block number\n");
return MDE_EntryNotFound;
}
if (mdstrcmp(word, file->index[i].last_word) <= 0) {
- fprintf(stderr, "matching block %d\n", i);
+ debug("matching block %d\n", i);
*number = i;
return MDE_Success;
}
@@ -352,19 +360,18 @@ MDError get_block(FILE *input, const MDIndexEntry *entry,
fseeko(input, entry->offset, SEEK_SET);
- fprintf(stderr,
- " unkn1: %8d\n"
- " compressed: %8d\n"
- " unkn3: %8d\n"
- " uncompressed: %8d\n"
- " unkn5: %8d\n"
- " unkn6: %8d\n", // maybe the difference of the first characters between the first/last word
- entry->unknown1,
- entry->compressed_size,
- entry->unknown3,
- entry->uncompressed_size,
- entry->unknown5,
- entry->unknown6);
+ debug(" unkn1: %8d\n"
+ " compressed: %8d\n"
+ " unkn3: %8d\n"
+ " uncompressed: %8d\n"
+ " unkn5: %8d\n"
+ " unkn6: %8d\n", // maybe the difference of the first characters between the first/last word
+ entry->unknown1,
+ entry->compressed_size,
+ entry->unknown3,
+ entry->uncompressed_size,
+ entry->unknown5,
+ entry->unknown6);
uint8_t *compressed = malloc(entry->compressed_size);
*block = malloc(usize);
@@ -373,7 +380,7 @@ MDError get_block(FILE *input, const MDIndexEntry *entry,
free(compressed);
return MDE_OutOfMemory;
}
- fprintf(stderr, "reading from %qd (%d bytes)\n", entry->offset, entry->compressed_size);
+ debug("reading from %qd (%d bytes)\n", entry->offset, entry->compressed_size);
if (fread(compressed, entry->compressed_size, 1, input) != 1) {
free(compressed);
return MDE_TruncatedFile;
@@ -406,14 +413,14 @@ MDError find_offset_in_block(const MDFile *file, const uint8_t *block, size_t bl
//printf("compare %s %s\n", name, (const char*)(block + blockpos + 8));
if (!mdstrcmp((const char*)(block + blockpos + 8), name)) return MDE_Success;
/*if (mdstrcmp((const char*)(block + blockpos + 8), name) > 0) {
- for (const char *c = (char*)(block + blockpos + 8); *c; c++) fprintf(stderr, "%hhx[%c] ", *c, *c);
- fprintf(stderr, "\n");
+ for (const char *c = (char*)(block + blockpos + 8); *c; c++) debug("%hhx[%c] ", *c, *c);
+ debug("\n");
return MDE_EntryNotFound;
}*/
blockpos += 8 + strlen((const char*)(block + blockpos + 8)) + 1;
}
- //fprintf(stderr, "can't get offset in block\n");
+ //debug("can't get offset in block\n");
return MDE_EntryNotFound;
}
@@ -509,7 +516,7 @@ MDError get_article(FILE *input, const MDFile *file, const char *name,
/* Locate index block */
check(get_block_number(file, name, &blocknum));
- fprintf(stderr, "blocknum = %d\n", blocknum);
+ debug("blocknum = %d\n", blocknum);
check(get_block(input, &file->index[blocknum], &block, &blocklen));
//fwrite(block, file->index[0].uncompressed_size, 1, stdout);
@@ -521,8 +528,8 @@ MDError get_article(FILE *input, const MDFile *file, const char *name,
lengths = &file->lengths[length_entry_number];
offsets = &file->offsets[length_entry_number];
- fprintf(stderr, "whole_mem_offset = %qd, disk_offset[%d] = %qd (articles_start is %qd)\n", article_offs, length_entry_number, offsets->disk, file->articles_start);
- fprintf(stderr, "lengths: mem %d disk: %d\n", lengths->mem_size, lengths->disk_size);
+ debug("whole_mem_offset = %qd, disk_offset[%d] = %qd (articles_start is %qd)\n", article_offs, length_entry_number, offsets->disk, file->articles_start);
+ debug("lengths: mem %d disk: %d\n", lengths->mem_size, lengths->disk_size);
/*printf("article_offs: %1$qx = %1$qd (articles_start is %2$qd)\n", article_offs + file->articles_start, file->articles_start);
printf(" + header: %1$qx = %1$qd\n", article_offs + file->header_end);
printf(" + index: %1$qx = %1$qd\n", article_offs + file->header_end + file->header.index_containersize);
@@ -541,13 +548,13 @@ MDError get_article(FILE *input, const MDFile *file, const char *name,
free(compressed);
return MDE_OutOfMemory;
}
- fprintf(stderr, "reading articles (%d bytes)\n", lengths->disk_size);
+ debug("reading articles (%d bytes)\n", lengths->disk_size);
if (fread(compressed, lengths->disk_size, 1, input) != 1) {
free(compressed);
return MDE_TruncatedFile;
}
- //fprintf(stderr, "disksize = %d memsize = %d\n", lengths->disk_size, lengths->mem_size);
+ //debug("disksize = %d memsize = %d\n", lengths->disk_size, lengths->mem_size);
uLongf usize = lengths->mem_size;
int zstatus = uncompress(articles, &usize,
@@ -558,13 +565,13 @@ MDError get_article(FILE *input, const MDFile *file, const char *name,
free(articles);
return MDE_DecompressionFailed;
}
- fprintf(stderr, "lengths: mem %d\n", usize);
+ debug("lengths: mem %d\n", usize);
/* Extract the right article */
//article_offs
size_t internal_offset = article_offs - offsets->mem;
const char *article_start = (char*)&articles[internal_offset];
- fprintf(stderr, "articles = %p + offs (%d) = %p\n", articles, internal_offset, &articles[internal_offset]);
+ debug("articles = %p + offs (%d) = %p\n", articles, internal_offset, &articles[internal_offset]);
size_t article_length = strnlen(article_start, /*lengths->mem_size*/ usize - internal_offset);
*article = malloc(article_length+1);
@@ -582,32 +589,31 @@ MDError init_file(FILE *input, MDFile *file) {
check(read_header(input, &file->header));
file->header_end = ftello(input);
- fprintf(stderr,
- " meta_csum?: %.8x\n"
- " unkn2: %8d\n"
- "index entries: %8d\n"
- " unkn4: %8d\n"
- " num entries: %8d\n"
- " unkn6: %8d\n"
- " index_msize: %8d\n"
- " unkn8: %8d\n"
- " index_csize: %8d\n"
- " unkn10: %8d\n"
- " unkn11: %8d\n"
- " unkn12: %.8x\n",
- file->header.metadata_checksum,
- file->header.unknown2,
- file->header.index_entries,
- file->header.unknown4,
- file->header.num_entries,
- file->header.unknown6,
- file->header.index_memsize,
- file->header.unknown8,
- file->header.index_containersize,
- file->header.unknown10,
- file->header.unknown11,
- file->header.unknown12);
- fprintf(stderr, " header end: %8qd\n", file->header_end);
+ debug(" meta_csum?: %.8x\n"
+ " unkn2: %8d\n"
+ "index entries: %8d\n"
+ " unkn4: %8d\n"
+ " num entries: %8d\n"
+ " unkn6: %8d\n"
+ " index_msize: %8d\n"
+ " unkn8: %8d\n"
+ " index_csize: %8d\n"
+ " unkn10: %8d\n"
+ " unkn11: %8d\n"
+ " unkn12: %.8x\n",
+ file->header.metadata_checksum,
+ file->header.unknown2,
+ file->header.index_entries,
+ file->header.unknown4,
+ file->header.num_entries,
+ file->header.unknown6,
+ file->header.index_memsize,
+ file->header.unknown8,
+ file->header.index_containersize,
+ file->header.unknown10,
+ file->header.unknown11,
+ file->header.unknown12);
+ debug(" header end: %8qd\n", file->header_end);
check(read_index(input, file));
check(read_lengthtables(input, file));
@@ -661,8 +667,8 @@ MDError list_words(FILE *input, const MDFile *file, const char *beginning) {
printf(" %s\n", current_word);
}
/*if (mdstrcmp((const char*)(block + blockpos + 8), beginning) > 0) {
- for (const char *c = (char*)(block + blockpos + 8); *c; c++) fprintf(stderr, "%hhx[%c] ", *c, *c);
- fprintf(stderr, "\n");
+ for (const char *c = (char*)(block + blockpos + 8); *c; c++) debug("%hhx[%c] ", *c, *c);
+ debug("\n");
return MDE_EntryNotFound;
}*/
previous_word = current_word;