Pillow/src/libImaging/ZipDecode.c

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

300 lines
10 KiB
C
Raw Normal View History

2010-07-31 06:52:47 +04:00
/*
* The Python Imaging Library.
* $Id$
*
* decoder for ZIP (deflated) image data.
*
* history:
* 1996-12-14 fl Created (for PNG)
* 1997-01-15 fl Prepared to read TIFF/ZIP
* 2001-11-19 fl PNG incomplete read patch (from Bernhard Herzog)
*
* Copyright (c) Fredrik Lundh 1996.
* Copyright (c) Secret Labs AB 1997-2001.
*
* See the README file for information on usage and redistribution.
*/
#include "Imaging.h"
2016-03-16 14:23:45 +03:00
#ifdef HAVE_LIBZ
2010-07-31 06:52:47 +04:00
2020-08-31 15:44:51 +03:00
#include "ZipCodecs.h"
2010-07-31 06:52:47 +04:00
static const int OFFSET[] = {7, 3, 3, 1, 1, 0, 0};
static const int STARTING_COL[] = {0, 4, 0, 2, 0, 1, 0};
static const int STARTING_ROW[] = {0, 0, 4, 0, 2, 0, 1};
static const int COL_INCREMENT[] = {8, 8, 4, 4, 2, 2, 1};
static const int ROW_INCREMENT[] = {8, 8, 8, 4, 4, 2, 2};
/* Get the length in bytes of a scanline in the pass specified,
* for interlaced images */
static int
get_row_len(ImagingCodecState state, int pass) {
int row_len = (state->xsize + OFFSET[pass]) / COL_INCREMENT[pass];
2010-07-31 06:52:47 +04:00
return ((row_len * state->bits) + 7) / 8;
}
/* -------------------------------------------------------------------- */
2016-03-16 14:23:45 +03:00
/* Decoder */
2010-07-31 06:52:47 +04:00
/* -------------------------------------------------------------------- */
int
ImagingZipDecode(Imaging im, ImagingCodecState state, UINT8 *buf, Py_ssize_t bytes) {
2010-07-31 06:52:47 +04:00
ZIPSTATE *context = (ZIPSTATE *)state->context;
int err;
int n;
UINT8 *ptr;
int i, bpp;
int row_len;
if (!state->state) {
2016-03-16 14:23:45 +03:00
/* Initialization */
2020-05-10 12:56:36 +03:00
if (context->mode == ZIP_PNG || context->mode == ZIP_PNG_PALETTE) {
2016-03-16 14:23:45 +03:00
context->prefix = 1; /* PNG */
2020-05-10 12:56:36 +03:00
}
2010-07-31 06:52:47 +04:00
2016-03-16 14:47:18 +03:00
/* overflow check for malloc */
if (state->bytes > INT_MAX - 1) {
2016-03-16 14:47:18 +03:00
state->errcode = IMAGING_CODEC_MEMORY;
return -1;
}
2016-03-16 14:23:45 +03:00
/* Expand standard buffer to make room for the (optional) filter
prefix, and allocate a buffer to hold the previous line */
free(state->buffer);
2016-03-16 14:47:18 +03:00
/* malloc check ok, overflow checked above */
2016-03-16 14:23:45 +03:00
state->buffer = (UINT8 *)malloc(state->bytes + 1);
context->previous = (UINT8 *)malloc(state->bytes + 1);
if (!state->buffer || !context->previous) {
state->errcode = IMAGING_CODEC_MEMORY;
return -1;
}
2010-07-31 06:52:47 +04:00
context->last_output = 0;
2016-03-16 14:23:45 +03:00
/* Initialize to black */
memset(context->previous, 0, state->bytes + 1);
2010-07-31 06:52:47 +04:00
2016-03-16 14:23:45 +03:00
/* Setup decompression context */
context->z_stream.zalloc = (alloc_func)NULL;
context->z_stream.zfree = (free_func)NULL;
context->z_stream.opaque = (voidpf)NULL;
2010-07-31 06:52:47 +04:00
2016-03-16 14:23:45 +03:00
err = inflateInit(&context->z_stream);
if (err < 0) {
state->errcode = IMAGING_CODEC_CONFIG;
2017-05-22 19:38:57 +03:00
free(context->previous);
2017-05-23 17:26:13 +03:00
context->previous = NULL;
2016-03-16 14:23:45 +03:00
return -1;
}
2010-07-31 06:52:47 +04:00
2016-03-16 14:23:45 +03:00
if (context->interlaced) {
context->pass = 0;
state->y = STARTING_ROW[context->pass];
}
2010-07-31 06:52:47 +04:00
2016-03-16 14:23:45 +03:00
/* Ready to decode */
state->state = 1;
2010-07-31 06:52:47 +04:00
}
if (context->interlaced) {
2016-03-16 14:23:45 +03:00
row_len = get_row_len(state, context->pass);
2010-07-31 06:52:47 +04:00
} else {
2016-03-16 14:23:45 +03:00
row_len = state->bytes;
2010-07-31 06:52:47 +04:00
}
/* Setup the source buffer */
context->z_stream.next_in = buf;
context->z_stream.avail_in = bytes;
/* Decompress what we've got this far */
while (context->z_stream.avail_in > 0) {
2016-03-16 14:23:45 +03:00
context->z_stream.next_out = state->buffer + context->last_output;
context->z_stream.avail_out = row_len + context->prefix - context->last_output;
err = inflate(&context->z_stream, Z_NO_FLUSH);
if (err < 0) {
/* Something went wrong inside the compression library */
2020-05-10 12:56:36 +03:00
if (err == Z_DATA_ERROR) {
2016-03-16 14:23:45 +03:00
state->errcode = IMAGING_CODEC_BROKEN;
2020-05-10 12:56:36 +03:00
} else if (err == Z_MEM_ERROR) {
2016-03-16 14:23:45 +03:00
state->errcode = IMAGING_CODEC_MEMORY;
2020-05-10 12:56:36 +03:00
} else {
2016-03-16 14:23:45 +03:00
state->errcode = IMAGING_CODEC_CONFIG;
2020-05-10 12:56:36 +03:00
}
2016-03-16 14:23:45 +03:00
free(context->previous);
2017-05-23 17:26:13 +03:00
context->previous = NULL;
2016-03-16 14:23:45 +03:00
inflateEnd(&context->z_stream);
return -1;
}
n = row_len + context->prefix - context->z_stream.avail_out;
if (n < row_len + context->prefix) {
context->last_output = n;
break; /* need more input data */
}
/* Apply predictor */
switch (context->mode) {
case ZIP_PNG:
switch (state->buffer[0]) {
case 0:
break;
case 1:
/* prior */
bpp = (state->bits + 7) / 8;
2020-05-10 12:56:36 +03:00
for (i = bpp + 1; i <= row_len; i++) {
2016-03-16 14:23:45 +03:00
state->buffer[i] += state->buffer[i - bpp];
2021-01-03 06:17:51 +03:00
}
break;
case 2:
/* up */
2020-05-10 12:56:36 +03:00
for (i = 1; i <= row_len; i++) {
2016-03-16 14:23:45 +03:00
state->buffer[i] += context->previous[i];
2021-01-03 06:17:51 +03:00
}
break;
case 3:
2016-03-16 14:23:45 +03:00
/* average */
bpp = (state->bits + 7) / 8;
2020-05-10 12:56:36 +03:00
for (i = 1; i <= bpp; i++) {
2016-03-16 14:23:45 +03:00
state->buffer[i] += context->previous[i] / 2;
2021-01-03 06:17:51 +03:00
}
2020-05-10 12:56:36 +03:00
for (; i <= row_len; i++) {
2016-03-16 14:23:45 +03:00
state->buffer[i] +=
(state->buffer[i - bpp] + context->previous[i]) / 2;
2021-01-03 06:17:51 +03:00
}
break;
case 4:
2016-03-16 14:23:45 +03:00
/* paeth filtering */
bpp = (state->bits + 7) / 8;
2020-05-10 12:56:36 +03:00
for (i = 1; i <= bpp; i++) {
2016-03-16 14:23:45 +03:00
state->buffer[i] += context->previous[i];
2021-01-03 06:17:51 +03:00
}
2020-05-10 12:56:36 +03:00
for (; i <= row_len; i++) {
2016-03-16 14:23:45 +03:00
int a, b, c;
int pa, pb, pc;
2021-01-03 06:17:51 +03:00
2016-03-16 14:23:45 +03:00
/* fetch pixels */
a = state->buffer[i - bpp];
b = context->previous[i];
c = context->previous[i - bpp];
2021-01-03 06:17:51 +03:00
2016-03-16 14:23:45 +03:00
/* distances to surrounding pixels */
pa = abs(b - c);
pb = abs(a - c);
pc = abs(a + b - 2 * c);
2021-01-03 06:17:51 +03:00
2016-03-16 14:23:45 +03:00
/* pick predictor with the shortest distance */
state->buffer[i] += (pa <= pb && pa <= pc) ? a
: (pb <= pc) ? b
2021-01-03 06:17:51 +03:00
: c;
}
break;
default:
2016-03-16 14:23:45 +03:00
state->errcode = IMAGING_CODEC_UNKNOWN;
free(context->previous);
2017-05-23 17:26:13 +03:00
context->previous = NULL;
2016-03-16 14:23:45 +03:00
inflateEnd(&context->z_stream);
return -1;
2020-05-10 12:56:36 +03:00
}
2016-03-16 14:23:45 +03:00
break;
case ZIP_TIFF_PREDICTOR:
bpp = (state->bits + 7) / 8;
2020-05-10 12:56:36 +03:00
for (i = bpp + 1; i <= row_len; i++) {
2016-03-16 14:23:45 +03:00
state->buffer[i] += state->buffer[i - bpp];
2020-05-10 12:56:36 +03:00
}
2016-03-16 14:23:45 +03:00
break;
}
/* Stuff data into the image */
if (context->interlaced) {
int col = STARTING_COL[context->pass];
if (state->bits >= 8) {
/* Stuff pixels in their correct location, one by one */
for (i = 0; i < row_len; i += ((state->bits + 7) / 8)) {
state->shuffle(
(UINT8 *)im->image[state->y] + col * im->pixelsize,
state->buffer + context->prefix + i,
1);
col += COL_INCREMENT[context->pass];
}
} else {
/* Handle case with more than a pixel in each byte */
int row_bits = ((state->xsize + OFFSET[context->pass]) /
COL_INCREMENT[context->pass]) *
state->bits;
for (i = 0; i < row_bits; i += state->bits) {
UINT8 byte = *(state->buffer + context->prefix + (i / 8));
byte <<= (i % 8);
state->shuffle(
(UINT8 *)im->image[state->y] + col * im->pixelsize, &byte, 1);
col += COL_INCREMENT[context->pass];
}
}
/* Find next valid scanline */
state->y += ROW_INCREMENT[context->pass];
while (state->y >= state->ysize || row_len <= 0) {
context->pass++;
if (context->pass == 7) {
/* Force exit below */
state->y = state->ysize;
break;
}
state->y = STARTING_ROW[context->pass];
row_len = get_row_len(state, context->pass);
/* Since we're moving to the "first" line, the previous line
2016-08-31 13:12:07 +03:00
* should be black to make filters work correctly */
2016-03-16 14:23:45 +03:00
memset(state->buffer, 0, state->bytes + 1);
}
} else {
state->shuffle(
(UINT8 *)im->image[state->y + state->yoff] +
state->xoff * im->pixelsize,
state->buffer + context->prefix,
state->xsize);
state->y++;
}
2010-07-31 06:52:47 +04:00
/* all inflate output has been consumed */
context->last_output = 0;
2016-03-16 14:23:45 +03:00
if (state->y >= state->ysize || err == Z_STREAM_END) {
/* The image and the data should end simultaneously */
/* if (state->y < state->ysize || err != Z_STREAM_END)
state->errcode = IMAGING_CODEC_BROKEN; */
2010-07-31 06:52:47 +04:00
2016-03-16 14:23:45 +03:00
free(context->previous);
2017-05-23 17:26:13 +03:00
context->previous = NULL;
2016-03-16 14:23:45 +03:00
inflateEnd(&context->z_stream);
return -1; /* end of file (errcode=0) */
}
2010-07-31 06:52:47 +04:00
2016-03-16 14:23:45 +03:00
/* Swap buffer pointers */
ptr = state->buffer;
state->buffer = context->previous;
context->previous = ptr;
2010-07-31 06:52:47 +04:00
}
return bytes; /* consumed all of it */
}
2017-05-23 17:26:13 +03:00
int
ImagingZipDecodeCleanup(ImagingCodecState state) {
2017-05-25 13:59:11 +03:00
/* called to free the decompression engine when the decode terminates
2017-05-23 17:26:13 +03:00
due to a corrupt or truncated image
*/
ZIPSTATE *context = (ZIPSTATE *)state->context;
/* Clean up */
if (context->previous) {
inflateEnd(&context->z_stream);
free(context->previous);
context->previous = NULL;
}
return -1;
}
2010-07-31 06:52:47 +04:00
#endif