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/>.
37 #define LBX_MAGIC 0x0000fead
38 #define LBX_HDR_SIZE 8
54 static struct lbx_state *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);
59 struct lbx_state *lbx;
61 if (magic != LBX_MAGIC) {
62 lbx_errno = -LBX_EMAGIC;
66 lbx = malloc(sizeof *lbx + sizeof lbx->offsets[0] * (nfiles+1));
72 *lbx = (struct lbx_state) {
79 struct lbx_state *lbx_fopen(FILE *f, const char *name)
81 unsigned char hdr_buf[LBX_HDR_SIZE];
82 struct lbx_state *lbx;
84 if (fread(hdr_buf, 1, sizeof hdr_buf, f) != sizeof hdr_buf) {
89 lbx = lbx_init(hdr_buf);
95 lbx->foff = sizeof hdr_buf;
97 for (unsigned i = 0; i <= lbx->nfiles; i++) {
100 if (fread(buf, 1, sizeof buf, f) != sizeof buf) {
103 lbx_errno = LBX_EEOF;
108 lbx->offsets[i] = unpack_32_le(buf);
109 lbx->foff += sizeof buf;
115 static int _lbx_memcpy(void *dest, struct lbx_state *src, size_t size)
117 if (src->foff + size > src->memsize)
119 memcpy(dest, src->mem + src->foff, size);
124 struct lbx_state *lbx_mopen(void *_mem, size_t size, const char *name)
126 struct lbx_state *new = NULL;
127 struct lbx_state tmp = { .mem = _mem, .memsize = size };
128 uint16_t nfiles, version;
131 if (_lbx_memcpy(&nfiles, &tmp, sizeof nfiles) == -1) goto eof;
132 if (_lbx_memcpy(&magic, &tmp, sizeof magic) == -1) goto eof;
133 if (_lbx_memcpy(&version, &tmp, sizeof version) == -1) goto eof;
135 nfiles = letohs(nfiles);
136 magic = letohl(magic);
137 version = letohs(version);
139 if (magic != LBX_MAGIC) {
140 lbx_errno = LBX_EMAGIC;
144 new = malloc(sizeof *new + (nfiles+1)*(sizeof *new->offsets));
150 *new = (struct lbx_state){
158 if (_lbx_memcpy(new->offsets, new, (nfiles+1)*(sizeof *new->offsets)))
164 lbx_errno = LBX_EEOF;
168 size_t lbx_numfiles(struct lbx_state *lbx)
173 int lbx_stat(struct lbx_state *lbx, size_t index, struct lbx_statbuf *buf)
175 static char str[256]; /* FIXME */
177 if (index >= lbx->nfiles) {
179 lbx_errno = LBX_ERANGE;
183 snprintf(str, sizeof str, "%s.%03zu", lbx->name, index);
185 buf->size = lbx->offsets[index+1] - lbx->offsets[index];
190 _lbx_mextract(struct lbx_state *lbx, size_t base, size_t len, FILE *of)
195 assert(base + len <= lbx->memsize);
197 rc = fwrite(lbx->mem + base, 1, len, of);
205 _lbx_fextract(struct lbx_state *lbx, size_t base, size_t len, FILE *of)
207 unsigned char buf[1024];
208 size_t rc, written = 0;
212 if (_lbx_fseek(lbx->f, &lbx->foff, base) == -1)
216 size_t amt = MIN(len, sizeof buf);
218 rc = fread(buf, 1, amt, lbx->f);
222 if (feof(lbx->f)) lbx_errno = LBX_EEOF;
223 else lbx_errno = -errno;
227 rc = fwrite(buf, 1, amt, of);
238 size_t lbx_extract(struct lbx_state *lbx, size_t index, FILE *of)
242 if (index >= lbx->nfiles) {
243 lbx_errno = LBX_ERANGE;
247 base = lbx->offsets[index];
248 len = lbx->offsets[index+1] - lbx->offsets[index];
251 return _lbx_mextract(lbx, base, len, of);
252 return _lbx_fextract(lbx, base, len, of);
255 int lbx_close(struct lbx_state *lbx)
269 const char *lbx_strerror(void)
272 return strerror(-lbx_errno);
275 case LBX_ESUCCESS: return "Success";
276 case LBX_EMAGIC: return "Bad magic number";
277 case LBX_EEOF: return "Unexpected end-of-file";
278 case LBX_ERANGE: return "Index out of range";
279 case LBX_EFORMAT: return "Invalid file format";
282 return "Unknown error";