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/>.
31 #define LBX_MAGIC 0x0000fead
32 #define LBX_HDR_SIZE 8
39 const struct lbx_file_ops *fops;
40 int (*dtor)(void *handle);
43 struct lbx_file_state *last_file;
45 unsigned long offsets[];
48 struct lbx_file_state {
49 unsigned long base, limit, offset;
54 static struct lbx_priv *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);
61 if (magic != LBX_MAGIC) {
62 lbx_error_raise(LBX_EMAGIC);
66 lbx = malloc(sizeof *lbx + sizeof lbx->offsets[0] * (nfiles+1));
68 lbx_error_raise(LBX_ENOMEM);
72 *lbx = (struct lbx_priv) {
73 .pub = { .nfiles = nfiles, },
79 static char *str_dup(const char *s)
83 buf = malloc(strlen(s)+1);
89 struct lbx *lbx_open(void *f, const struct lbx_file_ops *fops,
90 int (*destructor)(void *), const char *name)
92 unsigned char hdr_buf[LBX_HDR_SIZE];
93 struct lbx_priv *lbx = NULL;
96 dupname = str_dup(name);
98 lbx_error_raise(LBX_ENOMEM);
102 if (fops->read(hdr_buf, sizeof hdr_buf, f) != sizeof hdr_buf) {
104 lbx_error_raise(LBX_EEOF);
108 lbx = lbx_init(hdr_buf);
113 lbx->dtor = destructor;
117 for (unsigned i = 0; i <= lbx->pub.nfiles; i++) {
118 unsigned char buf[4];
120 if (fops->read(buf, sizeof buf, f) != sizeof buf) {
122 lbx_error_raise(LBX_EEOF);
126 lbx->offsets[i] = unpack_32_le(buf);
136 static int file_close(void *f)
138 return fclose((FILE *)f);
141 static int pipe_close(void *f)
143 struct lbx_pipe_state *p = f;
151 static char *last_component(const char *name)
155 /* TODO: Handle other path separators. */
156 c = strrchr(name, '/');
162 struct lbx *lbx_fopen(const char *file)
164 const char *name = last_component(file);
165 struct lbx_pipe_state *p;
168 f = fopen(file, "rb");
172 if (fseek(f, 0, SEEK_CUR) == 0)
173 return lbx_open(f, &lbx_default_fops, file_close, name);
175 p = malloc(sizeof *p);
181 *p = (struct lbx_pipe_state) { .f = f };
182 return lbx_open(p, &lbx_pipe_fops, pipe_close, name);
185 int lbx_file_stat(struct lbx *pub, unsigned fileno, struct lbx_statbuf *buf)
187 struct lbx_priv *lbx = (struct lbx_priv *)pub;
188 static char str[256]; /* FIXME */
190 if (fileno >= lbx->pub.nfiles) {
191 lbx_error_raise(LBX_ENOENT);
196 snprintf(str, sizeof str, "%s.%03u", lbx->name, fileno);
198 buf->size = lbx->offsets[fileno+1] - lbx->offsets[fileno];
202 int lbx_close(struct lbx *pub)
204 struct lbx_priv *lbx = (struct lbx_priv *)pub;
207 if (lbx && lbx->dtor)
208 rc = lbx->dtor(lbx->f);
215 struct lbx_file_state *lbx_file_open(struct lbx *pub, unsigned fileno)
217 struct lbx_priv *lbx = (struct lbx_priv *)pub;
218 struct lbx_file_state *state;
220 if (fileno >= lbx->pub.nfiles) {
221 lbx_error_raise(LBX_ENOENT);
225 lbx->last_file = NULL;
226 if (lbx->fops->seek(lbx->f, lbx->offsets[fileno], SEEK_SET) != 0) {
230 state = malloc(sizeof *state);
232 lbx_error_raise(LBX_ENOMEM);
236 *state = (struct lbx_file_state) {
237 .base = lbx->offsets[fileno],
238 .limit = lbx->offsets[fileno+1] - lbx->offsets[fileno],
242 lbx->last_file = state;
246 size_t lbx_file_read(struct lbx_file_state *f, void *buf, size_t n)
248 const struct lbx_file_ops *fops = f->lbx->fops;
249 size_t want = MIN(n, f->limit - f->offset);
252 if (f != f->lbx->last_file) {
253 f->lbx->last_file = NULL;
254 if (fops->seek(f->lbx->f, f->base + f->limit, SEEK_SET) != 0)
256 f->lbx->last_file = f;
259 rc = fops->read(buf, want, f->lbx->f);
262 if (want < n || (rc < want && fops->eof(f->lbx->f)))
267 int lbx_file_seek(struct lbx_file_state *f, long offset, int whence)
269 const struct lbx_file_ops *fops = f->lbx->fops;
274 pos = f->offset + offset;
280 pos = f->limit + offset;
287 f->lbx->last_file = NULL;
288 if (fops->seek(f->lbx->f, f->base + pos, SEEK_SET) != 0)
292 f->lbx->last_file = f;
298 long lbx_file_tell(struct lbx_file_state *f)
303 int lbx_file_eof(struct lbx_file_state *f)
308 void lbx_file_close(struct lbx_file_state *f)
310 if (f->lbx->last_file == f)
311 f->lbx->last_file = NULL;