]> git.draconx.ca Git - liblbx.git/blob - src/lbx.c
liblbx: Implement improved error reporting mechanism.
[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-2010 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 "error.h"
32 #include "lbx.h"
33
34 #define LBX_MAGIC    0x0000fead
35 #define LBX_HDR_SIZE 8
36
37 struct lbx_state {
38         char *name;
39
40         const struct lbx_file_ops *fops;
41         int (*dtor)(void *handle);
42         void *f;
43
44         struct lbx_file_state *last_file;
45
46         unsigned short nfiles;
47         unsigned long offsets[];
48 };
49
50 struct lbx_file_state {
51         unsigned long base, limit, offset;
52         struct lbx_state *lbx;
53         int eof;
54 };
55
56 static struct lbx_state *lbx_init(unsigned char hdr[static LBX_HDR_SIZE])
57 {
58         unsigned short nfiles  = unpack_16_le(hdr+0);
59         unsigned long  magic   = unpack_32_le(hdr+2);
60         unsigned short version = unpack_16_le(hdr+6);
61         struct lbx_state *lbx;
62
63         if (magic != LBX_MAGIC) {
64                 lbx_error_raise(LBX_EMAGIC);
65                 return NULL;
66         }
67
68         lbx = malloc(sizeof *lbx + sizeof lbx->offsets[0] * (nfiles+1));
69         if (!lbx) {
70                 lbx_error_raise(LBX_ENOMEM);
71                 return NULL;
72         }
73
74         *lbx = (struct lbx_state) {
75                 .nfiles = nfiles,
76         };
77
78         return lbx;
79 }
80
81 static char *str_dup(const char *s)
82 {
83         char *buf;
84
85         buf = malloc(strlen(s)+1);
86         if (buf)
87                 strcpy(buf, s);
88         return buf;
89 }
90
91 struct lbx_state *lbx_open(void *f, const struct lbx_file_ops *fops,
92                            int (*destructor)(void *), const char *name)
93 {
94         unsigned char hdr_buf[LBX_HDR_SIZE];
95         struct lbx_state *lbx = NULL;
96         char *dupname = NULL;
97
98         dupname = str_dup(name);
99         if (!dupname) {
100                 lbx_error_raise(LBX_ENOMEM);
101                 goto err;
102         }
103
104         if (fops->read(hdr_buf, sizeof hdr_buf, f) != sizeof hdr_buf) {
105                 if (fops->eof(f))
106                         lbx_error_raise(LBX_EEOF);
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                         if (fops->eof(f))
124                                 lbx_error_raise(LBX_EEOF);
125                         goto err;
126                 }
127
128                 lbx->offsets[i] = unpack_32_le(buf);
129         }
130
131         return lbx;
132 err:
133         free(dupname);
134         free(lbx);
135         return NULL;
136 }
137
138 static int file_close(void *f)
139 {
140         return fclose((FILE *)f);
141 }
142
143 static int pipe_close(void *f)
144 {
145         struct lbx_pipe_state *p = f;
146         int rc;
147
148         rc = fclose(p->f);
149         free(p);
150         return rc;
151 }
152
153 static char *last_component(const char *name)
154 {
155         char *c;
156
157         /* TODO: Handle other path separators. */
158         c = strrchr(name, '/');
159         if (!c)
160                 return (char *)name;
161         return c+1;
162 }
163
164 struct lbx_state *lbx_fopen(const char *file)
165 {
166         const char *name = last_component(file);
167         struct lbx_pipe_state *p;
168         FILE *f;
169
170         f = fopen(file, "rb");
171         if (!f)
172                 return NULL;
173
174         if (fseek(f, 0, SEEK_CUR) == 0)
175                 return lbx_open(f, &lbx_default_fops, file_close, name);
176
177         p = malloc(sizeof *p);
178         if (!p) {
179                 fclose(f);
180                 return NULL;
181         }
182
183         *p = (struct lbx_pipe_state) { .f = f };
184         return lbx_open(p, &lbx_pipe_fops, pipe_close, name);
185 }
186
187 size_t lbx_numfiles(struct lbx_state *lbx)
188 {
189         return lbx->nfiles;
190 }
191
192 int
193 lbx_file_stat(struct lbx_state *lbx, unsigned fileno, struct lbx_statbuf *buf)
194 {
195         static char str[256]; /* FIXME */
196
197         if (fileno >= lbx->nfiles) {
198                 lbx_error_raise(LBX_ENOENT);
199                 buf->name = NULL;
200                 return -1;
201         }
202
203         snprintf(str, sizeof str, "%s.%03u", lbx->name, fileno);
204         buf->name = str;
205         buf->size = lbx->offsets[fileno+1] - lbx->offsets[fileno];
206         return 0;
207 }
208
209 int lbx_close(struct lbx_state *lbx)
210 {
211         int rc = 0;
212
213         if (lbx && lbx->dtor)
214                 rc = lbx->dtor(lbx->f);
215         free(lbx->name);
216         free(lbx);
217
218         return rc;
219 }
220
221 struct lbx_file_state *lbx_file_open(struct lbx_state *lbx, unsigned fileno)
222 {
223         struct lbx_file_state *state;
224
225         if (fileno >= lbx->nfiles) {
226                 lbx_error_raise(LBX_ENOENT);
227                 return NULL;
228         }
229
230         lbx->last_file = NULL;
231         if (lbx->fops->seek(lbx->f, lbx->offsets[fileno], SEEK_SET) != 0) {
232                 return NULL;
233         }
234
235         state = malloc(sizeof *state);
236         if (!state) {
237                 lbx_error_raise(LBX_ENOMEM);
238                 return NULL;
239         }
240
241         *state = (struct lbx_file_state) {
242                 .base  = lbx->offsets[fileno],
243                 .limit = lbx->offsets[fileno+1] - lbx->offsets[fileno],
244                 .lbx   = lbx,
245         };
246
247         lbx->last_file = state;
248         return state;
249 }
250
251 size_t lbx_file_read(struct lbx_file_state *f, void *buf, size_t n)
252 {
253         const struct lbx_file_ops *fops = f->lbx->fops;
254         size_t want = MIN(n, f->limit - f->offset);
255         size_t rc;
256
257         if (f != f->lbx->last_file) {
258                 f->lbx->last_file = NULL;
259                 if (fops->seek(f->lbx->f, f->base + f->limit, SEEK_SET) != 0)
260                         return 0;
261                 f->lbx->last_file = f;
262         }
263
264         rc = fops->read(buf, want, f->lbx->f);
265         f->offset += rc;
266
267         if (want < n || (rc < want && fops->eof(f->lbx->f)))
268                 f->eof = 1;
269         return rc;
270 }
271
272 int lbx_file_seek(struct lbx_file_state *f, long offset, int whence)
273 {
274         const struct lbx_file_ops *fops = f->lbx->fops;
275         unsigned long pos;
276
277         switch (whence) {
278         case SEEK_CUR:
279                 pos = f->offset + offset;
280                 break;
281         case SEEK_SET:
282                 pos = offset;
283                 break;
284         case SEEK_END:
285                 pos = f->limit + offset;
286                 break;
287         }
288
289         if (pos > f->limit)
290                 return -1;
291
292         f->lbx->last_file = NULL;
293         if (fops->seek(f->lbx->f, f->base + pos, SEEK_SET) != 0)
294                 return -1;
295
296         f->offset = pos;
297         f->lbx->last_file = f;
298         f->eof = 0;
299
300         return 0;
301 }
302
303 long lbx_file_tell(struct lbx_file_state *f)
304 {
305         return f->offset;
306 }
307
308 int lbx_file_eof(struct lbx_file_state *f)
309 {
310         return f->eof;
311 }
312
313 void lbx_file_close(struct lbx_file_state *f)
314 {
315         if (f->lbx->last_file == f)
316                 f->lbx->last_file = NULL;
317         free(f);
318 }