]> git.draconx.ca Git - liblbx.git/blob - src/lbx.c
Update license information.
[liblbx.git] / src / lbx.c
1 /* 2ooM: The Master of Orion II Reverse Engineering Project
2  * Library for working with LBX archive files.
3  * Copyright (C) 2006-2008 Nick Bowler
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
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 <stdint.h>
27 #include <errno.h>
28 #include <assert.h>
29
30 #include <sys/stat.h>
31 #include <sys/mman.h>
32
33 #include "byteorder.h"
34 #include "misc.h"
35 #include "lbx.h"
36
37 #define LBX_MAGIC 0x0000fead
38
39 int lbx_errno = 0;
40
41 struct lbx_state {
42         const char *name;
43
44         unsigned char *mem;
45         size_t memsize;
46         FILE *f;
47         long foff;
48
49         uint16_t nfiles;
50         uint32_t offsets[];
51 };
52
53 struct lbx_state *lbx_fopen(FILE *f, const char *name)
54 {
55         struct lbx_state *new = NULL;
56         uint16_t nfiles, version;
57         uint32_t magic;
58
59         if (fread(&nfiles,  sizeof nfiles,  1, f) != 1) goto readerr;
60         if (fread(&magic,   sizeof magic,   1, f) != 1) goto readerr;
61         if (fread(&version, sizeof version, 1, f) != 1) goto readerr;
62
63         nfiles  = letohs(nfiles);
64         magic   = letohl(magic);
65         version = letohs(version);
66
67         if (magic != LBX_MAGIC) {
68                 lbx_errno = LBX_EMAGIC;
69                 return NULL;
70         }
71         
72         new = malloc(sizeof *new + (nfiles+1)*(sizeof *new->offsets));
73         if (!new) {
74                 lbx_errno = -errno;
75                 return NULL;
76         }
77         
78         *new = (struct lbx_state){
79                 .name   = name,
80                 .nfiles = nfiles,
81                 .f      = f,
82                 .foff   = sizeof nfiles + sizeof magic + sizeof version,
83         };
84         
85         if (fread(new->offsets, sizeof *new->offsets, nfiles+1, f) != nfiles+1)
86                 goto readerr;
87         new->foff += sizeof *new->offsets * (nfiles+1);
88
89         return new;
90 readerr:
91         if (feof(f)) {
92                 lbx_errno = LBX_EEOF;
93         } else {
94                 lbx_errno = -errno;
95         }
96
97         free(new);
98         return NULL;
99 }
100
101 static int _lbx_memcpy(void *dest, struct lbx_state *src, size_t size)
102 {
103         if (src->foff + size > src->memsize)
104                 return -1;
105         memcpy(dest, src->mem + src->foff, size);
106         src->foff += size;
107         return 0;
108 }
109
110 struct lbx_state *lbx_mopen(void *_mem, size_t size, const char *name)
111 {
112         struct lbx_state *new = NULL;
113         struct lbx_state tmp = { .mem = _mem, .memsize = size };
114         uint16_t nfiles, version;
115         uint32_t magic;
116
117         if (_lbx_memcpy(&nfiles,  &tmp, sizeof nfiles)  == -1) goto eof;
118         if (_lbx_memcpy(&magic,   &tmp, sizeof magic)   == -1) goto eof;
119         if (_lbx_memcpy(&version, &tmp, sizeof version) == -1) goto eof;
120
121         nfiles  = letohs(nfiles);
122         magic   = letohl(magic);
123         version = letohs(version);
124
125         if (magic != LBX_MAGIC) {
126                 lbx_errno = LBX_EMAGIC;
127                 return NULL;
128         }
129         
130         new = malloc(sizeof *new + (nfiles+1)*(sizeof *new->offsets));
131         if (!new) {
132                 lbx_errno = -errno;
133                 return NULL;
134         }
135         
136         *new = (struct lbx_state){
137                 .name    = name,
138                 .nfiles  = nfiles,
139                 .mem     = _mem,
140                 .memsize = size,
141                 .foff    = tmp.foff,
142         };
143         
144         if (_lbx_memcpy(new->offsets, new, (nfiles+1)*(sizeof *new->offsets)))
145                 goto eof;
146
147         return new;
148 eof:
149         free(new);
150         lbx_errno = LBX_EEOF;
151         return NULL;
152 }
153
154 struct lbx_state *lbx_open(const char *path)
155 {
156         struct lbx_state *new = NULL;
157         FILE *f;
158         
159         if ((f = fopen(path, "rb"))) {
160                 const char *name = strrchr(path, '/');
161                 new = lbx_fopen(f, name ? name+1 : path);
162         } else {
163                 lbx_errno = -errno;
164         }
165
166         return new;
167 }
168
169 size_t lbx_numfiles(struct lbx_state *lbx)
170 {
171         return lbx->nfiles;
172 }
173
174 int lbx_stat(struct lbx_state *lbx, size_t index, struct lbx_statbuf *buf)
175 {
176         static char str[256]; /* FIXME */
177
178         if (index >= lbx->nfiles) {
179                 buf->name = NULL;
180                 lbx_errno = LBX_ERANGE;
181                 return -1;
182         }
183
184         snprintf(str, sizeof str, "%s.%03d", lbx->name, index);
185         buf->name = str;
186         buf->size = lbx->offsets[index+1] - lbx->offsets[index];
187         return 0;
188 }
189
190 static size_t
191 _lbx_mextract(struct lbx_state *lbx, size_t base, size_t len, FILE *of)
192 {
193         size_t rc;
194
195         assert(lbx->mem);
196         assert(base + len <= lbx->memsize);
197
198         rc = fwrite(lbx->mem + base, 1, len, of);
199         if (rc < len)
200                 lbx_errno = -errno;
201
202         return rc;
203 }
204
205 static size_t
206 _lbx_fextract(struct lbx_state *lbx, size_t base, size_t len, FILE *of)
207 {
208         unsigned char buf[1024];
209         size_t rc, written = 0;
210
211         assert(lbx->f);
212
213         if (_lbx_fseek(lbx->f, &lbx->foff, base) == -1)
214                 return 0;
215         
216         while (len) {
217                 size_t amt = MIN(len, sizeof buf);
218
219                 rc = fread(buf, 1, amt, lbx->f);
220                 lbx->foff += rc;
221                 len -= rc;
222                 if (rc < amt) {
223                         if (feof(lbx->f)) lbx_errno = LBX_EEOF;
224                         else lbx_errno = -errno;
225                         break;
226                 }
227
228                 rc = fwrite(buf, 1, amt, of);
229                 written += rc;
230                 if (rc < amt) {
231                         lbx_errno = -errno;
232                         break;
233                 }
234         }
235
236         return written;
237 }
238
239 size_t lbx_extract(struct lbx_state *lbx, size_t index, FILE *of)
240 {
241         size_t base, len;
242
243         if (index >= lbx->nfiles) {
244                 lbx_errno = LBX_ERANGE;
245                 return 0;
246         }
247         
248         base = lbx->offsets[index];
249         len  = lbx->offsets[index+1] - lbx->offsets[index];
250
251         if (lbx->mem)
252                 return _lbx_mextract(lbx, base, len, of);
253         return _lbx_fextract(lbx, base, len, of);
254 }
255
256 void *lbx_mmap(struct lbx_state *lbx, size_t index, size_t *len)
257 {
258         unsigned char *mapping;
259         struct stat statbuf;
260         size_t base;
261
262         if (index >= lbx->nfiles) {
263                 lbx_errno = LBX_ERANGE;
264                 return NULL;
265         }
266         
267         base = lbx->offsets[index];
268         *len = lbx->offsets[index+1] - lbx->offsets[index];
269
270         if (lbx->mem)
271                 return lbx->mem + base;
272         
273         if (fstat(fileno(lbx->f), &statbuf) == -1) {
274                 lbx_errno = -errno;
275                 return NULL;
276         }
277
278         mapping = mmap(NULL, statbuf.st_size, PROT_READ, 0, fileno(lbx->f), 0);
279         if (mapping == MAP_FAILED) {
280                 lbx_errno = -errno;
281                 return NULL;
282         }
283
284         lbx->mem     = mapping;
285         lbx->memsize = statbuf.st_size;
286         return mapping + base;
287 }
288
289 void lbx_close(struct lbx_state *lbx)
290 {
291         if (!lbx) return;
292
293         if (lbx->f) {
294                 fclose(lbx->f);
295                 if (lbx->mem) {
296                         munmap(lbx->mem, lbx->memsize);
297                 }
298         }
299
300         free(lbx);
301 }
302
303 const char *lbx_strerror(void)
304 {
305         if (lbx_errno < 0)
306                 return strerror(-lbx_errno);
307
308         switch (lbx_errno) {
309         case LBX_ESUCCESS: return "Success";
310         case LBX_EMAGIC:   return "Bad magic number";
311         case LBX_EEOF:     return "Unexpected end-of-file";
312         case LBX_ERANGE:   return "Index out of range";
313         case LBX_EFORMAT:  return "Invalid file format";
314         }
315
316         return "Unknown error";
317 }