2 * 2ooM: The Master of Orion II Reverse Engineering Project
3 * Library for working with LBX archive files.
4 * Copyright (C) 2006-2008 Nick Bowler
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
37 #define LBX_MAGIC 0x0000fead
38 #define LBX_HDR_SIZE 8
54 static struct lbx_state *lbx_init(unsigned char hdr[static LBX_HDR_SIZE])
56 unsigned short nfiles = unpack_16_le(hdr+0);
57 unsigned long magic = unpack_32_le(hdr+2);
58 unsigned short version = unpack_16_le(hdr+6);
59 struct lbx_state *lbx;
61 if (magic != LBX_MAGIC) {
62 lbx_errno = -LBX_EMAGIC;
66 lbx = malloc(sizeof *lbx + sizeof lbx->offsets[0] * (nfiles+1));
72 *lbx = (struct lbx_state) {
79 struct lbx_state *lbx_fopen(FILE *f, const char *name)
81 unsigned char hdr_buf[LBX_HDR_SIZE];
82 struct lbx_state *lbx;
84 if (fread(hdr_buf, 1, sizeof hdr_buf, f) != sizeof hdr_buf) {
89 lbx = lbx_init(hdr_buf);
95 lbx->foff = sizeof hdr_buf;
97 for (unsigned i = 0; i <= lbx->nfiles; i++) {
100 if (fread(buf, 1, sizeof buf, f) != sizeof buf) {
103 lbx_errno = LBX_EEOF;
108 lbx->offsets[i] = unpack_32_le(buf);
109 lbx->foff += sizeof buf;
115 static int _lbx_memcpy(void *dest, struct lbx_state *src, size_t size)
117 if (src->foff + size > src->memsize)
119 memcpy(dest, src->mem + src->foff, size);
124 struct lbx_state *lbx_mopen(void *_mem, size_t size, const char *name)
126 struct lbx_state *new = NULL;
127 struct lbx_state tmp = { .mem = _mem, .memsize = size };
128 uint16_t nfiles, version;
131 if (_lbx_memcpy(&nfiles, &tmp, sizeof nfiles) == -1) goto eof;
132 if (_lbx_memcpy(&magic, &tmp, sizeof magic) == -1) goto eof;
133 if (_lbx_memcpy(&version, &tmp, sizeof version) == -1) goto eof;
135 nfiles = letohs(nfiles);
136 magic = letohl(magic);
137 version = letohs(version);
139 if (magic != LBX_MAGIC) {
140 lbx_errno = LBX_EMAGIC;
144 new = malloc(sizeof *new + (nfiles+1)*(sizeof *new->offsets));
150 *new = (struct lbx_state){
158 if (_lbx_memcpy(new->offsets, new, (nfiles+1)*(sizeof *new->offsets)))
164 lbx_errno = LBX_EEOF;
168 struct lbx_state *lbx_open(const char *path)
170 struct lbx_state *new = NULL;
173 if ((f = fopen(path, "rb"))) {
174 const char *name = strrchr(path, '/');
175 new = lbx_fopen(f, name ? name+1 : path);
183 size_t lbx_numfiles(struct lbx_state *lbx)
188 int lbx_stat(struct lbx_state *lbx, size_t index, struct lbx_statbuf *buf)
190 static char str[256]; /* FIXME */
192 if (index >= lbx->nfiles) {
194 lbx_errno = LBX_ERANGE;
198 snprintf(str, sizeof str, "%s.%03zu", lbx->name, index);
200 buf->size = lbx->offsets[index+1] - lbx->offsets[index];
205 _lbx_mextract(struct lbx_state *lbx, size_t base, size_t len, FILE *of)
210 assert(base + len <= lbx->memsize);
212 rc = fwrite(lbx->mem + base, 1, len, of);
220 _lbx_fextract(struct lbx_state *lbx, size_t base, size_t len, FILE *of)
222 unsigned char buf[1024];
223 size_t rc, written = 0;
227 if (_lbx_fseek(lbx->f, &lbx->foff, base) == -1)
231 size_t amt = MIN(len, sizeof buf);
233 rc = fread(buf, 1, amt, lbx->f);
237 if (feof(lbx->f)) lbx_errno = LBX_EEOF;
238 else lbx_errno = -errno;
242 rc = fwrite(buf, 1, amt, of);
253 size_t lbx_extract(struct lbx_state *lbx, size_t index, FILE *of)
257 if (index >= lbx->nfiles) {
258 lbx_errno = LBX_ERANGE;
262 base = lbx->offsets[index];
263 len = lbx->offsets[index+1] - lbx->offsets[index];
266 return _lbx_mextract(lbx, base, len, of);
267 return _lbx_fextract(lbx, base, len, of);
270 void *lbx_mmap(struct lbx_state *lbx, size_t index, size_t *len)
272 unsigned char *mapping;
276 if (index >= lbx->nfiles) {
277 lbx_errno = LBX_ERANGE;
281 base = lbx->offsets[index];
282 *len = lbx->offsets[index+1] - lbx->offsets[index];
285 return lbx->mem + base;
287 if (fstat(fileno(lbx->f), &statbuf) == -1) {
292 mapping = mmap(NULL, statbuf.st_size, PROT_READ, 0, fileno(lbx->f), 0);
293 if (mapping == MAP_FAILED) {
299 lbx->memsize = statbuf.st_size;
300 return mapping + base;
303 void lbx_close(struct lbx_state *lbx)
310 munmap(lbx->mem, lbx->memsize);
317 const char *lbx_strerror(void)
320 return strerror(-lbx_errno);
323 case LBX_ESUCCESS: return "Success";
324 case LBX_EMAGIC: return "Bad magic number";
325 case LBX_EEOF: return "Unexpected end-of-file";
326 case LBX_ERANGE: return "Index out of range";
327 case LBX_EFORMAT: return "Invalid file format";
330 return "Unknown error";