Pillow/src/libImaging/SgiRleDecode.c

226 lines
5.9 KiB
C
Raw Normal View History

2017-07-22 10:34:06 +03:00
/*
* The Python Imaging Library.
* $Id$
*
* decoder for Sgi RLE data.
*
* history:
2017-07-28 22:58:17 +03:00
* 2017-07-28 mb fixed for images larger than 64KB
2020-05-01 15:08:57 +03:00
* 2017-07-20 mb created
2017-07-22 10:34:06 +03:00
*
* Copyright (c) Mickael Bonfill 2017.
*
* See the README file for information on usage and redistribution.
*/
#include "Imaging.h"
2017-07-28 01:51:10 +03:00
#include "Sgi.h"
2017-07-22 10:34:06 +03:00
2017-07-28 01:51:10 +03:00
#define SGI_HEADER_SIZE 512
#define RLE_COPY_FLAG 0x80
#define RLE_MAX_RUN 0x7f
2021-01-03 06:17:51 +03:00
static void
read4B(UINT32 *dest, UINT8 *buf) {
*dest = (UINT32)((buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3]);
2017-07-22 10:34:06 +03:00
}
2021-01-03 06:17:51 +03:00
static int
expandrow(UINT8 *dest, UINT8 *src, int n, int z, int xsize) {
2017-07-27 00:01:45 +03:00
UINT8 pixel, count;
int x = 0;
2018-01-27 09:02:56 +03:00
2021-01-03 06:17:51 +03:00
for (; n > 0; n--) {
2017-07-28 01:51:10 +03:00
pixel = *src++;
2020-05-10 12:56:36 +03:00
if (n == 1 && pixel != 0) {
2017-07-28 01:51:10 +03:00
return n;
2020-05-10 12:56:36 +03:00
}
2017-07-28 01:51:10 +03:00
count = pixel & RLE_MAX_RUN;
2020-05-10 12:56:36 +03:00
if (!count) {
2017-07-28 01:51:10 +03:00
return count;
2020-05-10 12:56:36 +03:00
}
if (x + count > xsize) {
2020-01-01 06:16:45 +03:00
return -1;
}
x += count;
2017-07-28 01:51:10 +03:00
if (pixel & RLE_COPY_FLAG) {
2021-01-03 06:17:51 +03:00
while (count--) {
2017-07-28 22:58:17 +03:00
*dest = *src++;
dest += z;
2017-07-28 01:51:10 +03:00
}
2018-01-27 09:02:56 +03:00
2021-01-03 06:17:51 +03:00
} else {
2017-07-28 01:51:10 +03:00
pixel = *src++;
while (count--) {
2017-07-28 22:58:17 +03:00
*dest = pixel;
dest += z;
2017-07-28 01:51:10 +03:00
}
}
2017-07-27 00:01:45 +03:00
}
2017-07-28 01:51:10 +03:00
return 0;
2017-07-22 10:34:06 +03:00
}
2021-01-03 06:17:51 +03:00
static int
expandrow2(UINT8 *dest, const UINT8 *src, int n, int z, int xsize) {
2017-07-28 01:51:10 +03:00
UINT8 pixel, count;
2017-07-29 00:12:46 +03:00
int x = 0;
2018-01-27 09:02:56 +03:00
2021-01-03 06:17:51 +03:00
for (; n > 0; n--) {
pixel = src[1];
2021-01-03 06:17:51 +03:00
src += 2;
2020-05-10 12:56:36 +03:00
if (n == 1 && pixel != 0) {
2017-07-28 01:51:10 +03:00
return n;
2020-05-10 12:56:36 +03:00
}
2017-07-28 01:51:10 +03:00
count = pixel & RLE_MAX_RUN;
2020-05-10 12:56:36 +03:00
if (!count) {
2017-07-28 01:51:10 +03:00
return count;
2020-05-10 12:56:36 +03:00
}
if (x + count > xsize) {
2020-01-01 06:16:45 +03:00
return -1;
}
x += count;
2017-07-28 01:51:10 +03:00
if (pixel & RLE_COPY_FLAG) {
2021-01-03 06:17:51 +03:00
while (count--) {
memcpy(dest, src, 2);
src += 2;
dest += z * 2;
2017-07-28 01:51:10 +03:00
}
2021-01-03 06:17:51 +03:00
} else {
2017-07-28 01:51:10 +03:00
while (count--) {
memcpy(dest, src, 2);
dest += z * 2;
2017-07-28 01:51:10 +03:00
}
2021-01-03 06:17:51 +03:00
src += 2;
}
}
2017-07-28 01:51:10 +03:00
return 0;
}
2017-07-22 10:34:06 +03:00
2017-07-28 01:51:10 +03:00
int
2021-01-03 06:17:51 +03:00
ImagingSgiRleDecode(Imaging im, ImagingCodecState state, UINT8 *buf, Py_ssize_t bytes) {
2017-07-28 01:51:10 +03:00
UINT8 *ptr;
SGISTATE *c;
2017-09-29 14:01:10 +03:00
int err = 0;
2020-01-01 06:16:45 +03:00
int status;
2017-07-22 10:34:06 +03:00
/* size check */
2021-01-03 06:17:51 +03:00
if (im->xsize > INT_MAX / im->bands || im->ysize > INT_MAX / im->bands) {
state->errcode = IMAGING_CODEC_MEMORY;
return -1;
}
2018-01-27 09:02:56 +03:00
/* Get all data from File descriptor */
2021-01-03 06:17:51 +03:00
c = (SGISTATE *)state->context;
2017-07-28 22:58:17 +03:00
_imaging_seek_pyFd(state->fd, 0L, SEEK_END);
c->bufsize = _imaging_tell_pyFd(state->fd);
c->bufsize -= SGI_HEADER_SIZE;
c->tablen = im->bands * im->ysize;
/* below, we populate the starttab and lentab into the bufsize,
each with 4 bytes per element of tablen
Check here before we allocate any memory
*/
2021-01-03 06:17:51 +03:00
if (c->bufsize < 8 * c->tablen) {
state->errcode = IMAGING_CODEC_OVERRUN;
return -1;
}
ptr = malloc(sizeof(UINT8) * c->bufsize);
2017-09-29 14:01:10 +03:00
if (!ptr) {
state->errcode = IMAGING_CODEC_MEMORY;
return -1;
2017-09-29 14:01:10 +03:00
}
2017-07-28 22:58:17 +03:00
_imaging_seek_pyFd(state->fd, SGI_HEADER_SIZE, SEEK_SET);
2021-01-03 06:17:51 +03:00
_imaging_read_pyFd(state->fd, (char *)ptr, c->bufsize);
2017-07-28 22:58:17 +03:00
2017-07-29 00:12:46 +03:00
/* decoder initialization */
state->count = 0;
2017-07-28 22:58:17 +03:00
state->y = 0;
2017-09-29 14:01:10 +03:00
if (state->ystep < 0) {
2017-07-28 22:58:17 +03:00
state->y = im->ysize - 1;
2017-09-29 14:01:10 +03:00
} else {
2017-07-28 22:58:17 +03:00
state->ystep = 1;
2017-09-29 14:01:10 +03:00
}
2017-07-29 00:12:46 +03:00
/* Allocate memory for RLE tables and rows */
free(state->buffer);
2017-09-29 14:01:10 +03:00
state->buffer = NULL;
/* malloc overflow check above */
state->buffer = calloc(im->xsize * im->bands, sizeof(UINT8) * 2);
c->starttab = calloc(c->tablen, sizeof(UINT32));
c->lengthtab = calloc(c->tablen, sizeof(UINT32));
2021-01-03 06:17:51 +03:00
if (!state->buffer || !c->starttab || !c->lengthtab) {
2017-09-29 14:01:10 +03:00
err = IMAGING_CODEC_MEMORY;
goto sgi_finish_decode;
}
2017-07-29 00:12:46 +03:00
/* populate offsets table */
2021-01-03 06:17:51 +03:00
for (c->tabindex = 0, c->bufindex = 0; c->tabindex < c->tablen;
c->tabindex++, c->bufindex += 4) {
read4B(&c->starttab[c->tabindex], &ptr[c->bufindex]);
2020-05-10 12:56:36 +03:00
}
2017-07-29 00:12:46 +03:00
/* populate lengths table */
2021-01-03 06:17:51 +03:00
for (c->tabindex = 0, c->bufindex = c->tablen * sizeof(UINT32);
c->tabindex < c->tablen;
c->tabindex++, c->bufindex += 4) {
read4B(&c->lengthtab[c->tabindex], &ptr[c->bufindex]);
2020-05-10 12:56:36 +03:00
}
2017-07-28 22:58:17 +03:00
state->count += c->tablen * sizeof(UINT32) * 2;
2017-07-28 22:58:17 +03:00
2017-07-29 00:12:46 +03:00
/* read compressed rows */
2021-01-03 06:17:51 +03:00
for (c->rowno = 0; c->rowno < im->ysize; c->rowno++, state->y += state->ystep) {
for (c->channo = 0; c->channo < im->bands; c->channo++) {
c->rleoffset = c->starttab[c->rowno + c->channo * im->ysize];
c->rlelength = c->lengthtab[c->rowno + c->channo * im->ysize];
c->rleoffset -= SGI_HEADER_SIZE;
2018-01-27 09:02:56 +03:00
2019-09-30 11:45:43 +03:00
if (c->rleoffset + c->rlelength > c->bufsize) {
state->errcode = IMAGING_CODEC_OVERRUN;
goto sgi_finish_decode;
2019-09-30 11:45:43 +03:00
}
2017-07-29 00:12:46 +03:00
/* row decompression */
2021-01-03 06:17:51 +03:00
if (c->bpc == 1) {
status = expandrow(
&state->buffer[c->channo],
&ptr[c->rleoffset],
c->rlelength,
im->bands,
im->xsize);
} else {
status = expandrow2(
&state->buffer[c->channo * 2],
&ptr[c->rleoffset],
c->rlelength,
im->bands,
im->xsize);
2020-01-01 06:16:45 +03:00
}
if (status == -1) {
state->errcode = IMAGING_CODEC_OVERRUN;
goto sgi_finish_decode;
2020-01-01 06:16:45 +03:00
} else if (status == 1) {
goto sgi_finish_decode;
2017-07-28 01:51:10 +03:00
}
2018-01-27 09:02:56 +03:00
state->count += c->rlelength;
2017-07-28 22:58:17 +03:00
}
2018-01-27 09:02:56 +03:00
2017-07-29 00:12:46 +03:00
/* store decompressed data in image */
2021-01-03 06:17:51 +03:00
state->shuffle((UINT8 *)im->image[state->y], state->buffer, im->xsize);
2017-07-28 01:51:10 +03:00
}
2017-07-22 10:34:06 +03:00
c->bufsize++;
2017-07-22 10:34:06 +03:00
2021-01-03 06:17:51 +03:00
sgi_finish_decode:;
2018-01-27 09:02:56 +03:00
free(c->starttab);
free(c->lengthtab);
2017-07-28 22:58:17 +03:00
free(ptr);
2021-01-03 06:17:51 +03:00
if (err != 0) {
state->errcode = err;
return -1;
2017-09-29 14:01:10 +03:00
}
return state->count - c->bufsize;
2017-07-28 22:58:17 +03:00
}