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