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