]> git.draconx.ca Git - liblbx.git/blob - src/lbx.c
liblbx: Remove stale include directives.
[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         const 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 struct lbx_state *lbx_open(void *f, const struct lbx_file_ops *fops,
83                            int (*destructor)(void *), const char *name)
84 {
85         unsigned char hdr_buf[LBX_HDR_SIZE];
86         struct lbx_state *lbx;
87
88         if (fops->read(hdr_buf, sizeof hdr_buf, f) != sizeof hdr_buf) {
89                 lbx_errno = -errno;
90                 return NULL;
91         }
92
93         lbx = lbx_init(hdr_buf);
94         if (!lbx)
95                 return NULL;
96
97         lbx->dtor = destructor;
98         lbx->name = name;
99         lbx->fops = fops;
100         lbx->f    = f;
101
102         for (unsigned i = 0; i <= lbx->nfiles; i++) {
103                 unsigned char buf[4];
104
105                 if (fops->read(buf, sizeof buf, f) != sizeof buf) {
106                         lbx_errno = -errno;
107                         if (fops->eof(f))
108                                 lbx_errno = LBX_EEOF;
109                         free(lbx);
110                         return NULL;
111                 }
112
113                 lbx->offsets[i] = unpack_32_le(buf);
114         }
115
116         return lbx;
117 }
118
119 struct lbx_state *lbx_fopen(FILE *f, const char *name)
120 {
121         return lbx_open(f, &lbx_default_fops, NULL, name);
122 }
123
124 size_t lbx_numfiles(struct lbx_state *lbx)
125 {
126         return lbx->nfiles;
127 }
128
129 int lbx_stat(struct lbx_state *lbx, size_t index, struct lbx_statbuf *buf)
130 {
131         static char str[256]; /* FIXME */
132
133         if (index >= lbx->nfiles) {
134                 buf->name = NULL;
135                 lbx_errno = LBX_ERANGE;
136                 return -1;
137         }
138
139         snprintf(str, sizeof str, "%s.%03zu", lbx->name, index);
140         buf->name = str;
141         buf->size = lbx->offsets[index+1] - lbx->offsets[index];
142         return 0;
143 }
144
145 int lbx_close(struct lbx_state *lbx)
146 {
147         int rc = 0;
148
149         if (lbx && lbx->dtor)
150                 rc = lbx->dtor(lbx->f);
151         free(lbx);
152
153         return rc;
154 }
155
156 struct lbx_file_state *lbx_file_open(struct lbx_state *lbx, unsigned fileno)
157 {
158         struct lbx_file_state *state;
159
160         if (fileno >= lbx->nfiles) {
161                 lbx_errno = LBX_ERANGE;
162                 return NULL;
163         }
164
165         lbx->last_file = NULL;
166         if (lbx->fops->seek(lbx->f, lbx->offsets[fileno], SEEK_SET) != 0) {
167                 lbx_errno = -errno;
168                 return NULL;
169         }
170
171         state = malloc(sizeof *state);
172         if (!state) {
173                 lbx_errno = -errno;
174                 return NULL;
175         }
176
177         *state = (struct lbx_file_state) {
178                 .base  = lbx->offsets[fileno],
179                 .limit = lbx->offsets[fileno+1] - lbx->offsets[fileno],
180                 .lbx   = lbx,
181         };
182
183         lbx->last_file = state;
184         return state;
185 }
186
187 size_t lbx_file_read(struct lbx_file_state *f, void *buf, size_t n)
188 {
189         const struct lbx_file_ops *fops = f->lbx->fops;
190         size_t want = MIN(n, f->limit - f->offset);
191         size_t rc;
192
193         if (f != f->lbx->last_file) {
194                 f->lbx->last_file = NULL;
195                 if (fops->seek(f->lbx->f, f->base + f->limit, SEEK_SET) != 0)
196                         return 0;
197                 f->lbx->last_file = f;
198         }
199
200         rc = fops->read(buf, want, f->lbx->f);
201         f->offset += rc;
202
203         if (want < n || (rc < want && fops->eof(f->lbx->f)))
204                 f->eof = 1;
205         return rc;
206 }
207
208 int lbx_file_seek(struct lbx_file_state *f, long offset, int whence)
209 {
210         const struct lbx_file_ops *fops = f->lbx->fops;
211         unsigned long pos;
212
213         switch (whence) {
214         case SEEK_CUR:
215                 pos = f->offset + offset;
216                 break;
217         case SEEK_SET:
218                 pos = offset;
219                 break;
220         case SEEK_END:
221                 pos = f->limit + offset;
222                 break;
223         }
224
225         if (pos > f->limit)
226                 return -1;
227
228         f->lbx->last_file = NULL;
229         if (fops->seek(f->lbx->f, f->base + pos, SEEK_SET) != 0)
230                 return -1;
231         f->lbx->last_file = f;
232         f->eof = 0;
233
234         return 0;
235 }
236
237 long lbx_file_tell(struct lbx_file_state *f)
238 {
239         return f->offset;
240 }
241
242 int lbx_file_eof(struct lbx_file_state *f)
243 {
244         return f->eof;
245 }
246
247 void lbx_file_close(struct lbx_file_state *f)
248 {
249         if (f->lbx->last_file == f)
250                 f->lbx->last_file = NULL;
251         free(f);
252 }
253
254 const char *lbx_strerror(void)
255 {
256         if (lbx_errno < 0)
257                 return strerror(-lbx_errno);
258
259         switch (lbx_errno) {
260         case LBX_ESUCCESS: return "Success";
261         case LBX_EMAGIC:   return "Bad magic number";
262         case LBX_EEOF:     return "Unexpected end-of-file";
263         case LBX_ERANGE:   return "Index out of range";
264         case LBX_EFORMAT:  return "Invalid file format";
265         }
266
267         return "Unknown error";
268 }