#include "tiffiop.h"
#ifdef PACKBITS_SUPPORT
#include <stdio.h>
static int PackBitsPreEncode(TIFF *tif, uint16_t s)
{
(void)s;
tif->tif_data = (uint8_t *)_TIFFmallocExt(tif, sizeof(tmsize_t));
if (tif->tif_data == NULL)
return (0);
if (isTiled(tif))
*(tmsize_t *)tif->tif_data = TIFFTileRowSize(tif);
else
*(tmsize_t *)tif->tif_data = TIFFScanlineSize(tif);
return (1);
}
static int PackBitsPostEncode(TIFF *tif)
{
if (tif->tif_data)
_TIFFfreeExt(tif, tif->tif_data);
return (1);
}
static int PackBitsEncode(TIFF *tif, uint8_t *buf, tmsize_t cc, uint16_t s)
{
unsigned char *bp = (unsigned char *)buf;
uint8_t *op;
uint8_t *ep;
uint8_t *lastliteral;
long n, slop;
int b;
enum
{
BASE,
LITERAL,
RUN,
LITERAL_RUN
} state;
(void)s;
op = tif->tif_rawcp;
ep = tif->tif_rawdata + tif->tif_rawdatasize;
state = BASE;
lastliteral = 0;
while (cc > 0)
{
b = *bp++;
cc--;
n = 1;
for (; cc > 0 && b == *bp; cc--, bp++)
n++;
again:
if (op + 2 >= ep)
{
if (state == LITERAL || state == LITERAL_RUN)
{
slop = (long)(op - lastliteral);
tif->tif_rawcc += (tmsize_t)(lastliteral - tif->tif_rawcp);
if (!TIFFFlushData1(tif))
return (0);
op = tif->tif_rawcp;
while (slop-- > 0)
*op++ = *lastliteral++;
lastliteral = tif->tif_rawcp;
}
else
{
tif->tif_rawcc += (tmsize_t)(op - tif->tif_rawcp);
if (!TIFFFlushData1(tif))
return (0);
op = tif->tif_rawcp;
}
}
switch (state)
{
case BASE:
if (n > 1)
{
state = RUN;
if (n > 128)
{
*op++ = (uint8_t)-127;
*op++ = (uint8_t)b;
n -= 128;
goto again;
}
*op++ = (uint8_t)(-(n - 1));
*op++ = (uint8_t)b;
}
else
{
lastliteral = op;
*op++ = 0;
*op++ = (uint8_t)b;
state = LITERAL;
}
break;
case LITERAL:
if (n > 1)
{
state = LITERAL_RUN;
if (n > 128)
{
*op++ = (uint8_t)-127;
*op++ = (uint8_t)b;
n -= 128;
goto again;
}
*op++ = (uint8_t)(-(n - 1));
*op++ = (uint8_t)b;
}
else
{
if (++(*lastliteral) == 127)
state = BASE;
*op++ = (uint8_t)b;
}
break;
case RUN:
if (n > 1)
{
if (n > 128)
{
*op++ = (uint8_t)-127;
*op++ = (uint8_t)b;
n -= 128;
goto again;
}
*op++ = (uint8_t)(-(n - 1));
*op++ = (uint8_t)b;
}
else
{
lastliteral = op;
*op++ = 0;
*op++ = (uint8_t)b;
state = LITERAL;
}
break;
case LITERAL_RUN:
if (n == 1 && op[-2] == (uint8_t)-1 && *lastliteral < 126)
{
state = (((*lastliteral) += 2) == 127 ? BASE : LITERAL);
op[-2] = op[-1];
}
else
state = RUN;
goto again;
}
}
tif->tif_rawcc += (tmsize_t)(op - tif->tif_rawcp);
tif->tif_rawcp = op;
return (1);
}
static int PackBitsEncodeChunk(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s)
{
tmsize_t rowsize = *(tmsize_t *)tif->tif_data;
while (cc > 0)
{
tmsize_t chunk = rowsize;
if (cc < chunk)
chunk = cc;
if (PackBitsEncode(tif, bp, chunk, s) < 0)
return (-1);
bp += chunk;
cc -= chunk;
}
return (1);
}
static int PackBitsDecode(TIFF *tif, uint8_t *op, tmsize_t occ, uint16_t s)
{
static const char module[] = "PackBitsDecode";
int8_t *bp;
tmsize_t cc;
long n;
int b;
(void)s;
bp = (int8_t *)tif->tif_rawcp;
cc = tif->tif_rawcc;
while (cc > 0 && occ > 0)
{
n = (long)*bp++;
cc--;
if (n < 0)
{
if (n == -128)
continue;
n = -n + 1;
if (occ < (tmsize_t)n)
{
TIFFWarningExtR(tif, module,
"Discarding %" TIFF_SSIZE_FORMAT
" bytes to avoid buffer overrun",
(tmsize_t)n - occ);
n = (long)occ;
}
if (cc == 0)
{
TIFFWarningExtR(
tif, module,
"Terminating PackBitsDecode due to lack of data.");
break;
}
occ -= n;
b = *bp++;
cc--;
while (n-- > 0)
*op++ = (uint8_t)b;
}
else
{
if (occ < (tmsize_t)(n + 1))
{
TIFFWarningExtR(tif, module,
"Discarding %" TIFF_SSIZE_FORMAT
" bytes to avoid buffer overrun",
(tmsize_t)n - occ + 1);
n = (long)occ - 1;
}
if (cc < (tmsize_t)(n + 1))
{
TIFFWarningExtR(
tif, module,
"Terminating PackBitsDecode due to lack of data.");
break;
}
_TIFFmemcpy(op, bp, ++n);
op += n;
occ -= n;
bp += n;
cc -= n;
}
}
tif->tif_rawcp = (uint8_t *)bp;
tif->tif_rawcc = cc;
if (occ > 0)
{
memset(op, 0, (size_t)occ);
TIFFErrorExtR(tif, module, "Not enough data for scanline %" PRIu32,
tif->tif_row);
return (0);
}
return (1);
}
int TIFFInitPackBits(TIFF *tif, int scheme)
{
(void)scheme;
tif->tif_decoderow = PackBitsDecode;
tif->tif_decodestrip = PackBitsDecode;
tif->tif_decodetile = PackBitsDecode;
tif->tif_preencode = PackBitsPreEncode;
tif->tif_postencode = PackBitsPostEncode;
tif->tif_encoderow = PackBitsEncode;
tif->tif_encodestrip = PackBitsEncodeChunk;
tif->tif_encodetile = PackBitsEncodeChunk;
return (1);
}
#endif