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
45 const struct lbx_file_ops *fops;
46 int (*dtor)(void *handle);
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_errno = -LBX_EMAGIC;
65 lbx = malloc(sizeof *lbx + sizeof lbx->offsets[0] * (nfiles+1));
71 *lbx = (struct lbx_state) {
78 struct lbx_state *lbx_open(void *f, const struct lbx_file_ops *fops,
79 int (*destructor)(void *), const char *name)
81 unsigned char hdr_buf[LBX_HDR_SIZE];
82 struct lbx_state *lbx;
84 if (fops->read(hdr_buf, sizeof hdr_buf, f) != sizeof hdr_buf) {
89 lbx = lbx_init(hdr_buf);
93 lbx->dtor = destructor;
98 for (unsigned i = 0; i <= lbx->nfiles; i++) {
101 if (fops->read(buf, sizeof buf, f) != sizeof buf) {
104 lbx_errno = LBX_EEOF;
109 lbx->offsets[i] = unpack_32_le(buf);
115 struct lbx_state *lbx_fopen(FILE *f, const char *name)
117 return lbx_open(f, &lbx_default_fops, NULL, name);
120 size_t lbx_numfiles(struct lbx_state *lbx)
125 int lbx_stat(struct lbx_state *lbx, size_t index, struct lbx_statbuf *buf)
127 static char str[256]; /* FIXME */
129 if (index >= lbx->nfiles) {
131 lbx_errno = LBX_ERANGE;
135 snprintf(str, sizeof str, "%s.%03zu", lbx->name, index);
137 buf->size = lbx->offsets[index+1] - lbx->offsets[index];
142 _lbx_fextract(struct lbx_state *lbx, size_t base, size_t len, FILE *of)
144 unsigned char buf[1024];
145 size_t rc, written = 0;
149 if (lbx->fops->seek(lbx->f, base, SEEK_SET) != 0) {
155 size_t amt = MIN(len, sizeof buf);
157 rc = lbx->fops->read(buf, amt, lbx->f);
160 if (lbx->fops->eof(lbx->f)) lbx_errno = LBX_EEOF;
161 else lbx_errno = -errno;
165 rc = fwrite(buf, 1, amt, of);
176 size_t lbx_extract(struct lbx_state *lbx, size_t index, FILE *of)
180 if (index >= lbx->nfiles) {
181 lbx_errno = LBX_ERANGE;
185 base = lbx->offsets[index];
186 len = lbx->offsets[index+1] - lbx->offsets[index];
187 return _lbx_fextract(lbx, base, len, of);
190 int lbx_close(struct lbx_state *lbx)
194 if (lbx && lbx->dtor)
195 rc = lbx->dtor(lbx->f);
201 const char *lbx_strerror(void)
204 return strerror(-lbx_errno);
207 case LBX_ESUCCESS: return "Success";
208 case LBX_EMAGIC: return "Bad magic number";
209 case LBX_EEOF: return "Unexpected end-of-file";
210 case LBX_ERANGE: return "Index out of range";
211 case LBX_EFORMAT: return "Invalid file format";
214 return "Unknown error";