]> git.draconx.ca Git - liblbx.git/blob - src/lbx.c
05da0b4775fb74a858846d068e9b228b52bbff87
[liblbx.git] / src / lbx.c
1 /*
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
5  *
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.
10  *
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.
15  *
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/>.
18  */
19 #ifdef HAVE_CONFIG_H
20 #       include "config.h"
21 #endif
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <errno.h>
27 #include <assert.h>
28
29 #include "pack.h"
30 #include "misc.h"
31 #include "lbx.h"
32
33 #define LBX_MAGIC    0x0000fead
34 #define LBX_HDR_SIZE 8
35
36 int lbx_errno = 0;
37
38 struct lbx_state {
39         char *name;
40
41         const struct lbx_file_ops *fops;
42         int (*dtor)(void *handle);
43         void *f;
44
45         struct lbx_file_state *last_file;
46
47         unsigned short nfiles;
48         unsigned long offsets[];
49 };
50
51 struct lbx_file_state {
52         unsigned long base, limit, offset;
53         struct lbx_state *lbx;
54         int eof;
55 };
56
57 static struct lbx_state *lbx_init(unsigned char hdr[static LBX_HDR_SIZE])
58 {
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;
63
64         if (magic != LBX_MAGIC) {
65                 lbx_errno = -LBX_EMAGIC;
66                 return NULL;
67         }
68
69         lbx = malloc(sizeof *lbx + sizeof lbx->offsets[0] * (nfiles+1));
70         if (!lbx) {
71                 lbx_errno = -errno;
72                 return NULL;
73         }
74
75         *lbx = (struct lbx_state) {
76                 .nfiles = nfiles,
77         };
78
79         return lbx;
80 }
81
82 static char *str_dup(const char *s)
83 {
84         char *buf;
85
86         buf = malloc(strlen(s)+1);
87         if (buf)
88                 strcpy(buf, s);
89         return buf;
90 }
91
92 struct lbx_state *lbx_open(void *f, const struct lbx_file_ops *fops,
93                            int (*destructor)(void *), const char *name)
94 {
95         unsigned char hdr_buf[LBX_HDR_SIZE];
96         struct lbx_state *lbx = NULL;
97         char *dupname = NULL;
98
99         dupname = str_dup(name);
100         if (!dupname) {
101                 lbx_errno = -errno;
102                 goto err;
103         }
104
105         if (fops->read(hdr_buf, sizeof hdr_buf, f) != sizeof hdr_buf) {
106                 lbx_errno = -errno;
107                 goto err;
108         }
109
110         lbx = lbx_init(hdr_buf);
111         if (!lbx)
112                 goto err;
113
114         lbx->name = dupname;
115         lbx->dtor = destructor;
116         lbx->fops = fops;
117         lbx->f    = f;
118
119         for (unsigned i = 0; i <= lbx->nfiles; i++) {
120                 unsigned char buf[4];
121
122                 if (fops->read(buf, sizeof buf, f) != sizeof buf) {
123                         lbx_errno = -errno;
124                         if (fops->eof(f))
125                                 lbx_errno = LBX_EEOF;
126                         goto err;
127                 }
128
129                 lbx->offsets[i] = unpack_32_le(buf);
130         }
131
132         return lbx;
133 err:
134         free(dupname);
135         free(lbx);
136         return NULL;
137 }
138
139 struct lbx_state *lbx_fopen(FILE *f, const char *name)
140 {
141         return lbx_open(f, &lbx_default_fops, NULL, name);
142 }
143
144 size_t lbx_numfiles(struct lbx_state *lbx)
145 {
146         return lbx->nfiles;
147 }
148
149 int lbx_stat(struct lbx_state *lbx, size_t index, struct lbx_statbuf *buf)
150 {
151         static char str[256]; /* FIXME */
152
153         if (index >= lbx->nfiles) {
154                 buf->name = NULL;
155                 lbx_errno = LBX_ERANGE;
156                 return -1;
157         }
158
159         snprintf(str, sizeof str, "%s.%03zu", lbx->name, index);
160         buf->name = str;
161         buf->size = lbx->offsets[index+1] - lbx->offsets[index];
162         return 0;
163 }
164
165 int lbx_close(struct lbx_state *lbx)
166 {
167         int rc = 0;
168
169         if (lbx && lbx->dtor)
170                 rc = lbx->dtor(lbx->f);
171         free(lbx->name);
172         free(lbx);
173
174         return rc;
175 }
176
177 struct lbx_file_state *lbx_file_open(struct lbx_state *lbx, unsigned fileno)
178 {
179         struct lbx_file_state *state;
180
181         if (fileno >= lbx->nfiles) {
182                 lbx_errno = LBX_ERANGE;
183                 return NULL;
184         }
185
186         lbx->last_file = NULL;
187         if (lbx->fops->seek(lbx->f, lbx->offsets[fileno], SEEK_SET) != 0) {
188                 lbx_errno = -errno;
189                 return NULL;
190         }
191
192         state = malloc(sizeof *state);
193         if (!state) {
194                 lbx_errno = -errno;
195                 return NULL;
196         }
197
198         *state = (struct lbx_file_state) {
199                 .base  = lbx->offsets[fileno],
200                 .limit = lbx->offsets[fileno+1] - lbx->offsets[fileno],
201                 .lbx   = lbx,
202         };
203
204         lbx->last_file = state;
205         return state;
206 }
207
208 size_t lbx_file_read(struct lbx_file_state *f, void *buf, size_t n)
209 {
210         const struct lbx_file_ops *fops = f->lbx->fops;
211         size_t want = MIN(n, f->limit - f->offset);
212         size_t rc;
213
214         if (f != f->lbx->last_file) {
215                 f->lbx->last_file = NULL;
216                 if (fops->seek(f->lbx->f, f->base + f->limit, SEEK_SET) != 0)
217                         return 0;
218                 f->lbx->last_file = f;
219         }
220
221         rc = fops->read(buf, want, f->lbx->f);
222         f->offset += rc;
223
224         if (want < n || (rc < want && fops->eof(f->lbx->f)))
225                 f->eof = 1;
226         return rc;
227 }
228
229 int lbx_file_seek(struct lbx_file_state *f, long offset, int whence)
230 {
231         const struct lbx_file_ops *fops = f->lbx->fops;
232         unsigned long pos;
233
234         switch (whence) {
235         case SEEK_CUR:
236                 pos = f->offset + offset;
237                 break;
238         case SEEK_SET:
239                 pos = offset;
240                 break;
241         case SEEK_END:
242                 pos = f->limit + offset;
243                 break;
244         }
245
246         if (pos > f->limit)
247                 return -1;
248
249         f->lbx->last_file = NULL;
250         if (fops->seek(f->lbx->f, f->base + pos, SEEK_SET) != 0)
251                 return -1;
252         f->lbx->last_file = f;
253         f->eof = 0;
254
255         return 0;
256 }
257
258 long lbx_file_tell(struct lbx_file_state *f)
259 {
260         return f->offset;
261 }
262
263 int lbx_file_eof(struct lbx_file_state *f)
264 {
265         return f->eof;
266 }
267
268 void lbx_file_close(struct lbx_file_state *f)
269 {
270         if (f->lbx->last_file == f)
271                 f->lbx->last_file = NULL;
272         free(f);
273 }
274
275 const char *lbx_strerror(void)
276 {
277         if (lbx_errno < 0)
278                 return strerror(-lbx_errno);
279
280         switch (lbx_errno) {
281         case LBX_ESUCCESS: return "Success";
282         case LBX_EMAGIC:   return "Bad magic number";
283         case LBX_EEOF:     return "Unexpected end-of-file";
284         case LBX_ERANGE:   return "Index out of range";
285         case LBX_EFORMAT:  return "Invalid file format";
286         }
287
288         return "Unknown error";
289 }