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
37 const struct lbx_file_ops *fops;
38 int (*dtor)(void *handle);
41 struct lbx_file_state *last_file;
43 unsigned short nfiles;
44 unsigned long offsets[];
47 struct lbx_file_state {
48 unsigned long base, limit, offset;
49 struct lbx_state *lbx;
53 static struct lbx_state *lbx_init(unsigned char hdr[static LBX_HDR_SIZE])
55 unsigned short nfiles = unpack_16_le(hdr+0);
56 unsigned long magic = unpack_32_le(hdr+2);
57 unsigned short version = unpack_16_le(hdr+6);
58 struct lbx_state *lbx;
60 if (magic != LBX_MAGIC) {
61 lbx_error_raise(LBX_EMAGIC);
65 lbx = malloc(sizeof *lbx + sizeof lbx->offsets[0] * (nfiles+1));
67 lbx_error_raise(LBX_ENOMEM);
71 *lbx = (struct lbx_state) {
78 static char *str_dup(const char *s)
82 buf = malloc(strlen(s)+1);
88 struct lbx_state *lbx_open(void *f, const struct lbx_file_ops *fops,
89 int (*destructor)(void *), const char *name)
91 unsigned char hdr_buf[LBX_HDR_SIZE];
92 struct lbx_state *lbx = NULL;
95 dupname = str_dup(name);
97 lbx_error_raise(LBX_ENOMEM);
101 if (fops->read(hdr_buf, sizeof hdr_buf, f) != sizeof hdr_buf) {
103 lbx_error_raise(LBX_EEOF);
107 lbx = lbx_init(hdr_buf);
112 lbx->dtor = destructor;
116 for (unsigned i = 0; i <= lbx->nfiles; i++) {
117 unsigned char buf[4];
119 if (fops->read(buf, sizeof buf, f) != sizeof buf) {
121 lbx_error_raise(LBX_EEOF);
125 lbx->offsets[i] = unpack_32_le(buf);
135 static int file_close(void *f)
137 return fclose((FILE *)f);
140 static int pipe_close(void *f)
142 struct lbx_pipe_state *p = f;
150 static char *last_component(const char *name)
154 /* TODO: Handle other path separators. */
155 c = strrchr(name, '/');
161 struct lbx_state *lbx_fopen(const char *file)
163 const char *name = last_component(file);
164 struct lbx_pipe_state *p;
167 f = fopen(file, "rb");
171 if (fseek(f, 0, SEEK_CUR) == 0)
172 return lbx_open(f, &lbx_default_fops, file_close, name);
174 p = malloc(sizeof *p);
180 *p = (struct lbx_pipe_state) { .f = f };
181 return lbx_open(p, &lbx_pipe_fops, pipe_close, name);
184 size_t lbx_numfiles(struct lbx_state *lbx)
190 lbx_file_stat(struct lbx_state *lbx, unsigned fileno, struct lbx_statbuf *buf)
192 static char str[256]; /* FIXME */
194 if (fileno >= lbx->nfiles) {
195 lbx_error_raise(LBX_ENOENT);
200 snprintf(str, sizeof str, "%s.%03u", lbx->name, fileno);
202 buf->size = lbx->offsets[fileno+1] - lbx->offsets[fileno];
206 int lbx_close(struct lbx_state *lbx)
210 if (lbx && lbx->dtor)
211 rc = lbx->dtor(lbx->f);
218 struct lbx_file_state *lbx_file_open(struct lbx_state *lbx, unsigned fileno)
220 struct lbx_file_state *state;
222 if (fileno >= lbx->nfiles) {
223 lbx_error_raise(LBX_ENOENT);
227 lbx->last_file = NULL;
228 if (lbx->fops->seek(lbx->f, lbx->offsets[fileno], SEEK_SET) != 0) {
232 state = malloc(sizeof *state);
234 lbx_error_raise(LBX_ENOMEM);
238 *state = (struct lbx_file_state) {
239 .base = lbx->offsets[fileno],
240 .limit = lbx->offsets[fileno+1] - lbx->offsets[fileno],
244 lbx->last_file = state;
248 size_t lbx_file_read(struct lbx_file_state *f, void *buf, size_t n)
250 const struct lbx_file_ops *fops = f->lbx->fops;
251 size_t want = MIN(n, f->limit - f->offset);
254 if (f != f->lbx->last_file) {
255 f->lbx->last_file = NULL;
256 if (fops->seek(f->lbx->f, f->base + f->limit, SEEK_SET) != 0)
258 f->lbx->last_file = f;
261 rc = fops->read(buf, want, f->lbx->f);
264 if (want < n || (rc < want && fops->eof(f->lbx->f)))
269 int lbx_file_seek(struct lbx_file_state *f, long offset, int whence)
271 const struct lbx_file_ops *fops = f->lbx->fops;
276 pos = f->offset + offset;
282 pos = f->limit + offset;
289 f->lbx->last_file = NULL;
290 if (fops->seek(f->lbx->f, f->base + pos, SEEK_SET) != 0)
294 f->lbx->last_file = f;
300 long lbx_file_tell(struct lbx_file_state *f)
305 int lbx_file_eof(struct lbx_file_state *f)
310 void lbx_file_close(struct lbx_file_state *f)
312 if (f->lbx->last_file == f)
313 f->lbx->last_file = NULL;