]> git.draconx.ca Git - liblbx.git/blob - src/image.c
Fix segfault on EOF in lbximg_fopen().
[liblbx.git] / src / image.c
1 #ifdef HAVE_CONFIG_H
2 #       include "config.h"
3 #endif
4
5 #include <stdlib.h>
6 #include <stdint.h>
7 #include <assert.h>
8 #include <errno.h>
9
10 #include "byteorder.h"
11 #include "misc.h"
12 #include "lbx.h"
13 #include "image.h"
14
15 struct lbx_image {
16         uint16_t width, height;
17         uint16_t wtf1, wtf2;
18         uint16_t offs, frames;
19
20         FILE *f;
21         long foff;
22
23         int currentframe;
24         int currentx, currenty;
25         unsigned char **framedata;
26
27         uint32_t offsets[];
28 };
29
30 struct lbx_image *lbximg_fopen(FILE *f)
31 {
32         struct lbx_image tmp = {.f = f}, *new = NULL;
33
34         if (fread(&tmp.width,  sizeof tmp.width,   1, f) != 1) goto readerr;
35         if (fread(&tmp.height, sizeof tmp.height,  1, f) != 1) goto readerr;
36         if (fread(&tmp.wtf1,   sizeof tmp.wtf1,    1, f) != 1) goto readerr;
37         if (fread(&tmp.offs,   sizeof tmp.offs,    1, f) != 1) goto readerr;
38         if (fread(&tmp.frames, sizeof tmp.frames,  1, f) != 1) goto readerr;
39         if (fread(&tmp.wtf2,   sizeof tmp.wtf2,    1, f) != 1) goto readerr;
40
41         tmp.width  = letohs(tmp.width);  tmp.foff += sizeof tmp.width;
42         tmp.height = letohs(tmp.height); tmp.foff += sizeof tmp.height;
43         tmp.wtf1   = letohs(tmp.wtf1);   tmp.foff += sizeof tmp.wtf1;
44         tmp.offs   = letohs(tmp.offs);   tmp.foff += sizeof tmp.offs;
45         tmp.frames = letohs(tmp.frames); tmp.foff += sizeof tmp.frames;
46         tmp.wtf2   = letohs(tmp.wtf2);   tmp.foff += sizeof tmp.wtf2;
47
48         /*
49          * DEBUG ONLY.  These assertions exist to catch otherwise valid image
50          * files which differ from what I believe to be true of all LBX images.
51          * If we never find any exceptions, we can replace the assertions with
52          * assumptions.
53          */
54         _lbx_assert(tmp.wtf1 == 0);
55         _lbx_assert(tmp.offs == tmp.frames + 1);
56
57         new = malloc(sizeof *new + tmp.offs * sizeof *new->offsets);
58         if (!new) {
59                 lbx_errno = -errno;
60                 return NULL;
61         }
62
63         *new = tmp;
64         new->currentframe = -1;
65
66         if (fread(new->offsets, sizeof *new->offsets, new->offs, f)!= new->offs)
67                 goto readerr;
68         new->foff += sizeof *new->offsets * new->offs;
69
70         return new;
71 readerr:
72         if (feof(f)) {
73                 lbx_errno = LBX_EEOF;
74         } else {
75                 lbx_errno = -errno;
76         }
77
78         free(new);
79         return NULL;
80 }
81
82 static int _lbx_drawrow(int first, struct lbx_image *img)
83 {
84         uint16_t type, yval, count, xval;
85         unsigned char *pos;
86         unsigned char abyss;
87         size_t rc;
88
89         assert(img->framedata);
90
91         if (fread(&type, sizeof type, 1, img->f) != 1) goto readerr;
92         type = letohs(type); img->foff += sizeof type;
93
94         if (first) {
95                 img->currentx = 0;
96                 img->currenty = 0;
97                 type = 0;
98         }
99
100         if (type == 0) {
101                 if (fread(&yval,  sizeof yval,  1, img->f) != 1) goto readerr;
102                 yval = letohs(yval); img->foff += sizeof yval;
103
104                 if (yval == 1000)
105                         return 1;
106
107                 if (fread(&count, sizeof count, 1, img->f) != 1) goto readerr;
108                 count = letohs(count); img->foff += sizeof count;
109                 if (fread(&xval,  sizeof xval,  1, img->f) != 1) goto readerr;
110                 xval = letohs(xval); img->foff += sizeof xval;
111
112                 /* Ensure that the row fits in the image. */
113                 if (img->height - img->currenty <= yval || xval >= img->width) {
114                         lbx_errno = LBX_EFORMAT;
115                         return -1;
116                 }
117
118                 img->currenty += yval;
119                 img->currentx  = xval;
120         } else {
121                 if (fread(&xval,  sizeof xval,  1, img->f) != 1) goto readerr;
122                 xval = letohs(xval); img->foff += sizeof xval;
123
124                 if (img->width - img->currentx <= xval) {
125                         lbx_errno = LBX_EFORMAT;
126                         return -1;
127                 }
128                 img->currentx += xval;
129
130                 count = type;
131
132         }
133
134         if (count > img->width - img->currentx) {
135                 lbx_errno = LBX_EFORMAT;
136                 return -1;
137         }
138
139         pos = &img->framedata[img->currenty][img->currentx];
140         rc  = fread(pos, 1, count, img->f);
141         img->currentx += rc;
142         img->foff     += rc;
143
144         if (rc < count)
145                 goto readerr;
146
147         if (count % 2) {
148                 if (fread(&abyss, 1, 1, img->f) != 1)
149                         goto readerr;
150                 img->foff += 1;
151         }
152
153         return 0;
154 readerr:
155         if (feof(img->f)) {
156                 lbx_errno = LBX_EEOF;
157         } else {
158                 lbx_errno = -errno;
159         }
160         return -1;
161 }
162
163 unsigned char **lbximg_getframe(struct lbx_image *img, int frame)
164 {
165         if (frame >= img->frames || frame < 0) {
166                 lbx_errno = LBX_ERANGE;
167                 return NULL;
168         }
169
170         if (!img->framedata) {
171                 unsigned char *tmp;
172                 int i;
173
174                 tmp = malloc(img->width * img->height);
175                 if (!tmp) {
176                         lbx_errno = -errno;
177                         return NULL;
178                 }
179
180                 img->framedata = malloc(img->height * sizeof *img->framedata);
181                 if (!img->framedata) {
182                         lbx_errno = -errno;
183                         free(tmp);
184                         return NULL;
185                 }
186
187                 for (i = 0; i < img->height; i++) {
188                         img->framedata[i] = tmp + i * img->width;
189                 }
190         }
191
192         /* Start over if we are backtracking. */
193         if (img->currentframe > frame)
194                 img->currentframe == -1;
195
196         /* We must have previous frame decoded to continue. */
197         if (frame > img->currentframe + 1) {
198                 if (!lbximg_getframe(img, frame-1))
199                         return NULL;
200         }
201
202         if (img->currentframe != frame) {
203                 int rc, first = 1;
204
205                 if (_lbx_fseek(img->f, &img->foff, img->offsets[frame]) == -1)
206                         return NULL;
207
208                 do {
209                         rc = _lbx_drawrow(first, img);
210                         if (rc == -1)
211                                 return NULL;
212                         first = 0;
213
214                         if (img->foff > img->offsets[frame+1]) {
215                                 lbx_errno = LBX_EFORMAT;
216                                 return NULL;
217                         }
218                 } while (!rc);
219         }
220
221         img->currentframe = frame;
222         return img->framedata;
223 }
224
225 int lbximg_loadpalette(FILE *f, struct lbx_colour palette[static 256])
226 {
227         uint8_t entry[4];
228         int i;
229
230         for (i = 0; i < 256; i++) {
231                 if (fread(entry, sizeof entry, 1, f) != 1) {
232                         lbx_errno = (feof(f)) ? LBX_EEOF : -errno;
233                         return -1;
234                 }
235
236                 if (entry[0] != 1) {
237                         lbx_errno = LBX_EFORMAT;
238                         return -1;
239                 }
240
241                 palette[i] = (struct lbx_colour){
242                         .red   = entry[1] << 2,
243                         .green = entry[2] << 2,
244                         .blue  = entry[3] << 2,
245                 };
246         }
247
248         return 0;
249 }
250
251 int
252 lbximg_getpalette(struct lbx_image *img, struct lbx_colour palette[static 256])
253 {
254         int index = 0;
255         size_t rc;
256         size_t hdrlen = 6*(sizeof img->width)
257                         + (img->offs)*(sizeof *img->offsets);
258         
259         uint8_t entry[4];
260
261         /* Palette data is located right after the header. */
262         if (_lbx_fseek(img->f, &img->foff, hdrlen) == -1)
263                 return -1;
264         
265         while (img->foff + sizeof entry <= img->offsets[0]) {
266                 rc = fread(entry, 1, sizeof entry, img->f);
267                 img->foff += rc;
268
269                 if (rc < sizeof entry) {
270                         goto readerr;
271                 }
272
273                 if (entry[0] == 0) {
274                         index++;
275                 } else {
276                         index = entry[0];
277                 }
278
279                 palette[index] = (struct lbx_colour){
280                         .red   = entry[1] << 2,
281                         .green = entry[2] << 2,
282                         .blue  = entry[3] << 2,
283                 };
284         }
285
286         return 0;
287 readerr:
288         if (feof(img->f))
289                 lbx_errno = LBX_EEOF;
290         else
291                 lbx_errno = -errno;
292         return -1;
293 }
294
295 void lbximg_close(struct lbx_image *img)
296 {
297         if (!img) return;
298
299         if (img->framedata) {
300                 free(img->framedata[0]);
301                 free(img->framedata);
302         }
303
304         if (img->f) {
305                 fclose(img->f);
306         }
307
308         free(img);
309 }