struct lbx_image *lbximg_fopen(FILE *f)
{
- struct lbx_image tmp = {.f = f}, *new;
+ struct lbx_image tmp = {.f = f}, *new = NULL;
if (fread(&tmp.width, sizeof tmp.width, 1, f) != 1) goto readerr;
if (fread(&tmp.height, sizeof tmp.height, 1, f) != 1) goto readerr;
tmp.frames = letohs(tmp.frames); tmp.foff += sizeof tmp.frames;
tmp.wtf2 = letohs(tmp.wtf2); tmp.foff += sizeof tmp.wtf2;
+ /* For some reason, the format seems to need this. */
+ tmp.offs++;
+ tmp.frames++;
+
+ if (tmp.offs <= tmp.frames) {
+ lbx_errno = LBX_EFORMAT;
+ return NULL;
+ }
+
/*
* DEBUG ONLY. These assertions exist to catch otherwise valid image
* files which differ from what I believe to be true of all LBX images.
* If we never find any exceptions, we can replace the assertions with
* assumptions.
*/
- assert(tmp.wtf1 == 0);
- assert(tmp.offs == tmp.frames + 1);
+ _lbx_assert(tmp.wtf1 == 0);
+ _lbx_assert(tmp.offs == tmp.frames + 1);
new = malloc(sizeof *new + tmp.offs * sizeof *new->offsets);
if (!new) {
if (type == 0) {
if (fread(&yval, sizeof yval, 1, img->f) != 1) goto readerr;
yval = letohs(yval); img->foff += sizeof yval;
-
if (yval == 1000)
return 1;
-
if (fread(&count, sizeof count, 1, img->f) != 1) goto readerr;
count = letohs(count); img->foff += sizeof count;
if (fread(&xval, sizeof xval, 1, img->f) != 1) goto readerr;
xval = letohs(xval); img->foff += sizeof xval;
+ if (xval == 1000)
+ return 1;
/* Ensure that the row fits in the image. */
- if (img->height - img->currenty <= yval
- || xval >= img->width || count > img->width - xval) {
+ if (img->height - img->currenty <= yval || xval >= img->width) {
lbx_errno = LBX_EFORMAT;
return -1;
}
img->currenty += yval;
img->currentx = xval;
} else {
- if (fread(&yval, sizeof yval, 1, img->f) != 1) goto readerr;
- yval = letohs(yval); img->foff += sizeof yval;
-
- /* FIXME Still have to figure out what to do here. */
-
- count = type;
+ if (fread(&xval, sizeof xval, 1, img->f) != 1) goto readerr;
+ xval = letohs(xval); img->foff += sizeof xval;
- if (count > img->width - img->currentx) {
+ if (img->width - img->currentx <= xval) {
lbx_errno = LBX_EFORMAT;
return -1;
}
+ img->currentx += xval;
+
+ count = type;
+
+ }
+
+ if (count > img->width - img->currentx) {
+ lbx_errno = LBX_EFORMAT;
+ return -1;
}
pos = &img->framedata[img->currenty][img->currentx];
return NULL;
first = 0;
- if (img->foff > img->offsets[frame+1]) {
+ if (!rc && img->foff > img->offsets[frame+1]) {
lbx_errno = LBX_EFORMAT;
return NULL;
}
if (entry[0] == 0) {
index++;
+ if (index >= 256) {
+ lbx_errno = LBX_EFORMAT;
+ return -1;
+ }
} else {
index = entry[0];
}
return -1;
}
+void lbximg_getinfo(struct lbx_image *img, struct lbx_imginfo *info)
+{
+ *info = (struct lbx_imginfo) {
+ .width = img->width,
+ .height = img->height,
+ .nframes = img->frames,
+ };
+}
+
void lbximg_close(struct lbx_image *img)
{
if (!img) return;