summaryrefslogtreecommitdiff
path: root/qstd/pack.c
blob: 3006a7df5f0a6ade55014ace2dd2574bd1c7452d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include "memory.h"
#include "pack.h"
#include "plat.h"
#include "str.h"

#include <stddef.h>
#include <stdint.h>
#include <stdio.h>

#define pack_max_files 4

typedef struct Pack_File {
	FILE* handle;
	Pack_Entry* entry;
	int offset;
} Pack_File;

Pack_Entry* get_pack_table(Pack* p) {
	return (Pack_Entry*)(
		(char*)&p[1] + pack_max_files * sizeof(Pack_File)
	);
}

int get_pack_size(int entry_count) {
	return sizeof(Pack) +
		pack_max_files * sizeof(Pack_File) +
		entry_count * sizeof(Pack_Entry);
}

Pack_Entry* pack_find_entry(Pack* p, const char* name) {
	Pack_Entry* tab = get_pack_table(p);
	uint32_t hash = hash_string(name);
	int idx = (int)(hash % p->file_count), i;
	int e = (int)p->file_count;
	for (i = 0; i < e; i++) {
		Pack_Entry* e = &tab[idx];
		if (!e->name[0] || string_equal(name, e->name))
			return e;
		idx = (int)((idx + 1) % p->file_count);
	}
	return 0;
}

static void insert_entry(Pack* p, const Pack_Entry* e) {
	Pack_Entry* d = pack_find_entry(p, e->name);
	assert(d != 0);
	if (d->name[0])
		print_war("Duplicate entry in %s: %s\n", p->filename, e->name);
	*d = *e;
}

static void read_table(Pack* p, FILE* f) {
	uint32_t i, e = p->file_count;
	Pack_Entry* tab = get_pack_table(p);
	for (i = 0; i < e; i++)
		tab[i].name[0] = 0;
	for (i = 0; i < e; i++) {
		Pack_Entry e;
		fread(&e, sizeof e, 1, f);
		insert_entry(p, &e);
	}
}

static void init_files(Pack* p) {
	int i;
	Pack_File* files = (Pack_File*)&p[1];
	for (i = 0; i < pack_max_files; i++) {
		files[i].handle = 0;
	}
}

Pack* pack_open(const char* filename, Arena* a) {
	Pack* p;
	char magic[4];
	uint32_t entry_count;
	FILE* f = fopen(filename, "rb");
	if (!f) return 0;
	fread(magic, 1, 4, f);
	if (
		magic[0] != 'P' ||
		magic[1] != 'A' ||
		magic[2] != 'C' ||
		magic[3] != 'K'
	) {
		fclose(f);
		return 0;
	}
	fread(&entry_count, 4, 1, f);
	p = arena_alloc(a, get_pack_size(entry_count));
	p->filename = dup_string(a, filename);
	p->file_count = entry_count;
	init_files(p);
	read_table(p, f);
	fclose(f);
	return p;
}

void pack_close(Pack* p) {
	(void)p;
}

Pack_Entry* pack_get_entry(Pack* p, const char* name) {
	Pack_Entry* e = pack_find_entry(p, name);
	if (!e || e->name[0] == 0) return 0;
	return e;
}

static Pack_File* init_handle(
	Pack* p,
	Pack_File* h,
	const char* name
) {
	FILE* f;
	Pack_Entry* e = pack_find_entry(p, name);
	if (!e || !e->name[0]) return 0;
	f = fopen(p->filename, "rb");
	if (!f) return 0;
	h->offset = 0;
	h->handle = f;
	h->entry = e;
	return h;
}

Pack_File* pack_open_file(Pack* p, const char* name) {
	int i;
	Pack_File* files = (Pack_File*)&p[1];
	for (i = 0; i < pack_max_files; i++) {
		Pack_File* file = &files[i];
		if (!file->handle)
			return init_handle(p, file, name);
	}
	return 0;
}

void pack_close_file(Pack_File* f) {
	fclose(f->handle);
	f->handle = 0;
}

int pack_read(Pack_File* f, void* buf, int size) {
	int diff = (f->offset + size) - f->entry->size;
	int read;
	if (diff > 0) size -= diff;
	fseek(f->handle, f->entry->offset + f->offset, SEEK_SET);
	read = fread(buf, 1, size, f->handle);
	f->offset += read;
	return read;
}

void pack_seek(Pack_File* f, int offset, Seek_Rel rel) {
	switch (rel) {
		case seek_rel_cur:
			f->offset += offset;
			break;
		case seek_rel_start:
			f->offset = offset;
			break;
		case seek_rel_end:
			f->offset = f->entry->size - offset;
			break;
	}
}

int pack_tell(Pack_File* f) {
	return f->offset;
}