This repository has been archived by the owner on Jan 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pak.h
175 lines (142 loc) · 3.08 KB
/
pak.h
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
167
168
169
170
171
172
173
174
175
/* SPDX-License-Identifier: ISC */
#ifndef PAK_H
#define PAK_H
#include <stddef.h>
struct pak {
unsigned int size;
void *data;
unsigned int flag;
size_t count;
struct pak_item *items;
#ifndef PAK_NO_STDIO
FILE *file;
#endif
};
enum {
PAK_FROM_FILE = 1,
PAK_FROM_MMAP = 2,
};
struct pak_file {
const char *name;
unsigned int index;
unsigned int size;
const void *data;
};
struct pak_head {
char magic[4];
uint32_t off;
uint32_t len;
};
struct pak_item {
char name[56];
uint32_t off;
uint32_t len;
};
#ifndef PAK_NO_STDIO
#include <stdio.h>
#include <stdlib.h>
int pak_read(const char *filename, struct pak *pak);
int pak_from_file(FILE *file, struct pak *pak);
#endif
int pak_from_memory(unsigned int size, void *data, struct pak *pak);
struct pak_file *pak_file_at(const struct pak *pak, int index, struct pak_file *file);
struct pak_file *pak_find_file(const struct pak *pak, const char *name, struct pak_file *file);
#define pak_for_each_file(p, f) for ((f)->index = 0; pak_file_at(p, (f)->index, f); (f)->index++)
#endif /* PAK_H */
#ifdef PAK_IMPLEMENTATION
#include <stdint.h>
#include <string.h>
static int pak_err(const char *msg)
{
#ifndef PAK_NO_STDIO
fprintf(stderr, "%s\n", msg);
#endif
return -1;
}
struct pak_file *pak_file_at(const struct pak *pak, int index, struct pak_file *file)
{
const struct pak_head *hdr = pak->data;
const struct pak_item *it = pak->data + hdr->off;
if (index < 0 || (index * sizeof(*it)) >= hdr->len)
return NULL;
file->name = (const char *)&it[index].name;
file->size = it[index].len;
file->data = pak->data + it[index].off;
return file;
}
struct pak_file *pak_find_file(const struct pak *pak, const char *name, struct pak_file *file)
{
struct pak_file i = {};
pak_for_each_file(pak, &i) {
if (strcmp(i.name, name) == 0) {
*file = i;
return file;
}
}
return NULL;
}
int pak_check_header(struct pak pak)
{
const struct pak_head *hdr = pak.data;
if (pak.size < sizeof(hdr))
return pak_err("no header");
if (memcmp("PACK", hdr->magic, sizeof(hdr->magic)) != 0)
return pak_err("bad magic");
if (pak.size < (hdr->off + hdr->len))
return pak_err("not enough data");
return 0;
}
int pak_from_memory(unsigned int size, void *data, struct pak *pak)
{
struct pak p = {
.size = size,
.data = data,
};
if (pak_check_header(p))
return -1;
*pak = p;
return 0;
}
#ifndef PAK_NO_STDIO
int pak_from_file(FILE *f, struct pak *pak)
{
struct pak p = {
.flag = PAK_FROM_FILE,
};
int size;
size_t n;
fseek(f, 0, SEEK_END);
size = ftell(f);
if (size < 0)
return pak_err("ftell");
fseek(f, 0, SEEK_SET);
p.size = size;
p.data = malloc(p.size);
if (!p.data)
return pak_err("outofmem");
n = fread(p.data, p.size, 1, f);
fclose(f);
if (n != 1) {
free(p.data);
return pak_err("fread");
}
if (pak_check_header(p)) {
free(p.data);
return -1;
}
*pak = p;
return 0;
}
int pak_read(const char *name, struct pak *pak)
{
FILE *f;
int ret;
f = fopen(name, "rb");
if (!f)
return pak_err("fopen");
ret = pak_from_file(f, pak);
fclose(f);
return ret;
}
#endif /* PAK_NO_STDIO */
#endif /* PAK_IMPLEMENTATION */