2 * 2ooM: The Master of Orion II Reverse Engineering Project
3 * Library for working with LBX archive files.
4 * Copyright (C) 2006-2010 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/>.
34 #define LBX_MAGIC 0x0000fead
35 #define LBX_HDR_SIZE 8
40 const struct lbx_file_ops *fops;
41 int (*dtor)(void *handle);
44 struct lbx_file_state *last_file;
46 unsigned short nfiles;
47 unsigned long offsets[];
50 struct lbx_file_state {
51 unsigned long base, limit, offset;
52 struct lbx_state *lbx;
56 static struct lbx_state *lbx_init(unsigned char hdr[static LBX_HDR_SIZE])
58 unsigned short nfiles = unpack_16_le(hdr+0);
59 unsigned long magic = unpack_32_le(hdr+2);
60 unsigned short version = unpack_16_le(hdr+6);
61 struct lbx_state *lbx;
63 if (magic != LBX_MAGIC) {
64 lbx_error_raise(LBX_EMAGIC);
68 lbx = malloc(sizeof *lbx + sizeof lbx->offsets[0] * (nfiles+1));
70 lbx_error_raise(LBX_ENOMEM);
74 *lbx = (struct lbx_state) {
81 static char *str_dup(const char *s)
85 buf = malloc(strlen(s)+1);
91 struct lbx_state *lbx_open(void *f, const struct lbx_file_ops *fops,
92 int (*destructor)(void *), const char *name)
94 unsigned char hdr_buf[LBX_HDR_SIZE];
95 struct lbx_state *lbx = NULL;
98 dupname = str_dup(name);
100 lbx_error_raise(LBX_ENOMEM);
104 if (fops->read(hdr_buf, sizeof hdr_buf, f) != sizeof hdr_buf) {
106 lbx_error_raise(LBX_EEOF);
110 lbx = lbx_init(hdr_buf);
115 lbx->dtor = destructor;
119 for (unsigned i = 0; i <= lbx->nfiles; i++) {
120 unsigned char buf[4];
122 if (fops->read(buf, sizeof buf, f) != sizeof buf) {
124 lbx_error_raise(LBX_EEOF);
128 lbx->offsets[i] = unpack_32_le(buf);
138 static int file_close(void *f)
140 return fclose((FILE *)f);
143 static int pipe_close(void *f)
145 struct lbx_pipe_state *p = f;
153 static char *last_component(const char *name)
157 /* TODO: Handle other path separators. */
158 c = strrchr(name, '/');
164 struct lbx_state *lbx_fopen(const char *file)
166 const char *name = last_component(file);
167 struct lbx_pipe_state *p;
170 f = fopen(file, "rb");
174 if (fseek(f, 0, SEEK_CUR) == 0)
175 return lbx_open(f, &lbx_default_fops, file_close, name);
177 p = malloc(sizeof *p);
183 *p = (struct lbx_pipe_state) { .f = f };
184 return lbx_open(p, &lbx_pipe_fops, pipe_close, name);
187 size_t lbx_numfiles(struct lbx_state *lbx)
193 lbx_file_stat(struct lbx_state *lbx, unsigned fileno, struct lbx_statbuf *buf)
195 static char str[256]; /* FIXME */
197 if (fileno >= lbx->nfiles) {
198 lbx_error_raise(LBX_ENOENT);
203 snprintf(str, sizeof str, "%s.%03u", lbx->name, fileno);
205 buf->size = lbx->offsets[fileno+1] - lbx->offsets[fileno];
209 int lbx_close(struct lbx_state *lbx)
213 if (lbx && lbx->dtor)
214 rc = lbx->dtor(lbx->f);
221 struct lbx_file_state *lbx_file_open(struct lbx_state *lbx, unsigned fileno)
223 struct lbx_file_state *state;
225 if (fileno >= lbx->nfiles) {
226 lbx_error_raise(LBX_ENOENT);
230 lbx->last_file = NULL;
231 if (lbx->fops->seek(lbx->f, lbx->offsets[fileno], SEEK_SET) != 0) {
235 state = malloc(sizeof *state);
237 lbx_error_raise(LBX_ENOMEM);
241 *state = (struct lbx_file_state) {
242 .base = lbx->offsets[fileno],
243 .limit = lbx->offsets[fileno+1] - lbx->offsets[fileno],
247 lbx->last_file = state;
251 size_t lbx_file_read(struct lbx_file_state *f, void *buf, size_t n)
253 const struct lbx_file_ops *fops = f->lbx->fops;
254 size_t want = MIN(n, f->limit - f->offset);
257 if (f != f->lbx->last_file) {
258 f->lbx->last_file = NULL;
259 if (fops->seek(f->lbx->f, f->base + f->limit, SEEK_SET) != 0)
261 f->lbx->last_file = f;
264 rc = fops->read(buf, want, f->lbx->f);
267 if (want < n || (rc < want && fops->eof(f->lbx->f)))
272 int lbx_file_seek(struct lbx_file_state *f, long offset, int whence)
274 const struct lbx_file_ops *fops = f->lbx->fops;
279 pos = f->offset + offset;
285 pos = f->limit + offset;
292 f->lbx->last_file = NULL;
293 if (fops->seek(f->lbx->f, f->base + pos, SEEK_SET) != 0)
297 f->lbx->last_file = f;
303 long lbx_file_tell(struct lbx_file_state *f)
308 int lbx_file_eof(struct lbx_file_state *f)
313 void lbx_file_close(struct lbx_file_state *f)
315 if (f->lbx->last_file == f)
316 f->lbx->last_file = NULL;