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/>.
36 #define LBX_MAGIC 0x0000fead
37 #define LBX_HDR_SIZE 8
44 const struct lbx_file_ops *fops;
45 int (*dtor)(void *handle);
48 struct lbx_file_state *last_file;
50 unsigned short nfiles;
51 unsigned long offsets[];
54 struct lbx_file_state {
55 unsigned long base, limit, offset;
56 struct lbx_state *lbx;
60 static struct lbx_state *lbx_init(unsigned char hdr[static LBX_HDR_SIZE])
62 unsigned short nfiles = unpack_16_le(hdr+0);
63 unsigned long magic = unpack_32_le(hdr+2);
64 unsigned short version = unpack_16_le(hdr+6);
65 struct lbx_state *lbx;
67 if (magic != LBX_MAGIC) {
68 lbx_errno = -LBX_EMAGIC;
72 lbx = malloc(sizeof *lbx + sizeof lbx->offsets[0] * (nfiles+1));
78 *lbx = (struct lbx_state) {
85 struct lbx_state *lbx_open(void *f, const struct lbx_file_ops *fops,
86 int (*destructor)(void *), const char *name)
88 unsigned char hdr_buf[LBX_HDR_SIZE];
89 struct lbx_state *lbx;
91 if (fops->read(hdr_buf, sizeof hdr_buf, f) != sizeof hdr_buf) {
96 lbx = lbx_init(hdr_buf);
100 lbx->dtor = destructor;
105 for (unsigned i = 0; i <= lbx->nfiles; i++) {
106 unsigned char buf[4];
108 if (fops->read(buf, sizeof buf, f) != sizeof buf) {
111 lbx_errno = LBX_EEOF;
116 lbx->offsets[i] = unpack_32_le(buf);
122 struct lbx_state *lbx_fopen(FILE *f, const char *name)
124 return lbx_open(f, &lbx_default_fops, NULL, name);
127 size_t lbx_numfiles(struct lbx_state *lbx)
132 int lbx_stat(struct lbx_state *lbx, size_t index, struct lbx_statbuf *buf)
134 static char str[256]; /* FIXME */
136 if (index >= lbx->nfiles) {
138 lbx_errno = LBX_ERANGE;
142 snprintf(str, sizeof str, "%s.%03zu", lbx->name, index);
144 buf->size = lbx->offsets[index+1] - lbx->offsets[index];
148 int lbx_close(struct lbx_state *lbx)
152 if (lbx && lbx->dtor)
153 rc = lbx->dtor(lbx->f);
159 struct lbx_file_state *lbx_file_open(struct lbx_state *lbx, unsigned fileno)
161 struct lbx_file_state *state;
163 if (fileno >= lbx->nfiles) {
164 lbx_errno = LBX_ERANGE;
168 lbx->last_file = NULL;
169 if (lbx->fops->seek(lbx->f, lbx->offsets[fileno], SEEK_SET) != 0) {
174 state = malloc(sizeof *state);
180 *state = (struct lbx_file_state) {
181 .base = lbx->offsets[fileno],
182 .limit = lbx->offsets[fileno+1] - lbx->offsets[fileno],
186 lbx->last_file = state;
190 size_t lbx_file_read(struct lbx_file_state *f, void *buf, size_t n)
192 const struct lbx_file_ops *fops = f->lbx->fops;
193 size_t want = MIN(n, f->limit - f->offset);
196 if (f != f->lbx->last_file) {
197 f->lbx->last_file = NULL;
198 if (fops->seek(f->lbx->f, f->base + f->limit, SEEK_SET) != 0)
200 f->lbx->last_file = f;
203 rc = fops->read(buf, want, f->lbx->f);
206 if (want < n || (rc < want && fops->eof(f->lbx->f)))
211 int lbx_file_seek(struct lbx_file_state *f, long offset, int whence)
213 const struct lbx_file_ops *fops = f->lbx->fops;
218 pos = f->offset + offset;
224 pos = f->limit + offset;
231 f->lbx->last_file = NULL;
232 if (fops->seek(f->lbx->f, f->base + pos, SEEK_SET) != 0)
234 f->lbx->last_file = f;
240 long lbx_file_tell(struct lbx_file_state *f)
245 int lbx_file_eof(struct lbx_file_state *f)
250 void lbx_file_close(struct lbx_file_state *f)
252 if (f->lbx->last_file == f)
253 f->lbx->last_file = NULL;
257 const char *lbx_strerror(void)
260 return strerror(-lbx_errno);
263 case LBX_ESUCCESS: return "Success";
264 case LBX_EMAGIC: return "Bad magic number";
265 case LBX_EEOF: return "Unexpected end-of-file";
266 case LBX_ERANGE: return "Index out of range";
267 case LBX_EFORMAT: return "Invalid file format";
270 return "Unknown error";