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/>.
33 #define LBX_MAGIC 0x0000fead
34 #define LBX_HDR_SIZE 8
41 const struct lbx_file_ops *fops;
42 int (*dtor)(void *handle);
45 struct lbx_file_state *last_file;
47 unsigned short nfiles;
48 unsigned long offsets[];
51 struct lbx_file_state {
52 unsigned long base, limit, offset;
53 struct lbx_state *lbx;
57 static struct lbx_state *lbx_init(unsigned char hdr[static LBX_HDR_SIZE])
59 unsigned short nfiles = unpack_16_le(hdr+0);
60 unsigned long magic = unpack_32_le(hdr+2);
61 unsigned short version = unpack_16_le(hdr+6);
62 struct lbx_state *lbx;
64 if (magic != LBX_MAGIC) {
65 lbx_errno = -LBX_EMAGIC;
69 lbx = malloc(sizeof *lbx + sizeof lbx->offsets[0] * (nfiles+1));
75 *lbx = (struct lbx_state) {
82 static char *str_dup(const char *s)
86 buf = malloc(strlen(s)+1);
92 struct lbx_state *lbx_open(void *f, const struct lbx_file_ops *fops,
93 int (*destructor)(void *), const char *name)
95 unsigned char hdr_buf[LBX_HDR_SIZE];
96 struct lbx_state *lbx = NULL;
99 dupname = str_dup(name);
105 if (fops->read(hdr_buf, sizeof hdr_buf, f) != sizeof hdr_buf) {
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) {
125 lbx_errno = LBX_EEOF;
129 lbx->offsets[i] = unpack_32_le(buf);
139 static int file_close(void *f)
141 return fclose((FILE *)f);
144 static int pipe_close(void *f)
146 struct lbx_pipe_state *p = f;
154 static char *last_component(const char *name)
158 /* TODO: Handle other path separators. */
159 c = strrchr(name, '/');
165 struct lbx_state *lbx_fopen(const char *file)
167 const char *name = last_component(file);
168 struct lbx_pipe_state *p;
171 f = fopen(file, "rb");
175 if (fseek(f, 0, SEEK_CUR) == 0)
176 return lbx_open(f, &lbx_default_fops, file_close, name);
178 p = malloc(sizeof *p);
184 *p = (struct lbx_pipe_state) { .f = f };
185 return lbx_open(p, &lbx_pipe_fops, pipe_close, name);
188 size_t lbx_numfiles(struct lbx_state *lbx)
193 int lbx_stat(struct lbx_state *lbx, size_t index, struct lbx_statbuf *buf)
195 static char str[256]; /* FIXME */
197 if (index >= lbx->nfiles) {
199 lbx_errno = LBX_ERANGE;
203 snprintf(str, sizeof str, "%s.%03zu", lbx->name, index);
205 buf->size = lbx->offsets[index+1] - lbx->offsets[index];
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_errno = LBX_ERANGE;
230 lbx->last_file = NULL;
231 if (lbx->fops->seek(lbx->f, lbx->offsets[fileno], SEEK_SET) != 0) {
236 state = malloc(sizeof *state);
242 *state = (struct lbx_file_state) {
243 .base = lbx->offsets[fileno],
244 .limit = lbx->offsets[fileno+1] - lbx->offsets[fileno],
248 lbx->last_file = state;
252 size_t lbx_file_read(struct lbx_file_state *f, void *buf, size_t n)
254 const struct lbx_file_ops *fops = f->lbx->fops;
255 size_t want = MIN(n, f->limit - f->offset);
258 if (f != f->lbx->last_file) {
259 f->lbx->last_file = NULL;
260 if (fops->seek(f->lbx->f, f->base + f->limit, SEEK_SET) != 0)
262 f->lbx->last_file = f;
265 rc = fops->read(buf, want, f->lbx->f);
268 if (want < n || (rc < want && fops->eof(f->lbx->f)))
273 int lbx_file_seek(struct lbx_file_state *f, long offset, int whence)
275 const struct lbx_file_ops *fops = f->lbx->fops;
280 pos = f->offset + offset;
286 pos = f->limit + offset;
293 f->lbx->last_file = NULL;
294 if (fops->seek(f->lbx->f, f->base + pos, SEEK_SET) != 0)
298 f->lbx->last_file = f;
304 long lbx_file_tell(struct lbx_file_state *f)
309 int lbx_file_eof(struct lbx_file_state *f)
314 void lbx_file_close(struct lbx_file_state *f)
316 if (f->lbx->last_file == f)
317 f->lbx->last_file = NULL;
321 const char *lbx_strerror(void)
324 return strerror(-lbx_errno);
327 case LBX_ESUCCESS: return "Success";
328 case LBX_EMAGIC: return "Bad magic number";
329 case LBX_EEOF: return "Unexpected end-of-file";
330 case LBX_ERANGE: return "Index out of range";
331 case LBX_EFORMAT: return "Invalid file format";
334 return "Unknown error";