static int tiff_unpack_strip(TiffContext *s, uint8_t* dst, int stride, const uint8_t *src, int size, int lines){

    int c, line, pixels, code;

    const uint8_t *ssrc = src;

    int width = ((s->width * s->bpp) + 7) >> 3;

#if CONFIG_ZLIB

    uint8_t *zbuf; unsigned long outlen;



    if(s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE){

        int ret;

        outlen = width * lines;

        zbuf = av_malloc(outlen);

        ret = tiff_uncompress(zbuf, &outlen, src, size);

        if(ret != Z_OK){

            av_log(s->avctx, AV_LOG_ERROR, "Uncompressing failed (%lu of %lu) with error %d\n", outlen, (unsigned long)width * lines, ret);

            av_free(zbuf);

            return -1;

        }

        src = zbuf;

        for(line = 0; line < lines; line++){

            memcpy(dst, src, width);

            dst += stride;

            src += width;

        }

        av_free(zbuf);

        return 0;

    }

#endif

    if(s->compr == TIFF_LZW){

        if(ff_lzw_decode_init(s->lzw, 8, src, size, FF_LZW_TIFF) < 0){

            av_log(s->avctx, AV_LOG_ERROR, "Error initializing LZW decoder\n");

            return -1;

        }

    }

    if(s->compr == TIFF_CCITT_RLE || s->compr == TIFF_G3 || s->compr == TIFF_G4){

        int i, ret = 0;

        uint8_t *src2 = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);



        if(!src2 || (unsigned)size + FF_INPUT_BUFFER_PADDING_SIZE < (unsigned)size){

            av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");

            return -1;

        }

        if(s->fax_opts & 2){

            av_log(s->avctx, AV_LOG_ERROR, "Uncompressed fax mode is not supported (yet)\n");

            av_free(src2);

            return -1;

        }

        if(!s->fill_order){

            memcpy(src2, src, size);

        }else{

            for(i = 0; i < size; i++)

                src2[i] = av_reverse[src[i]];

        }

        memset(src2+size, 0, FF_INPUT_BUFFER_PADDING_SIZE);

        switch(s->compr){

        case TIFF_CCITT_RLE:

        case TIFF_G3:

        case TIFF_G4:

            ret = ff_ccitt_unpack(s->avctx, src2, size, dst, lines, stride, s->compr, s->fax_opts);

            break;

        }

        av_free(src2);

        return ret;

    }

    for(line = 0; line < lines; line++){

        if(src - ssrc > size){

            av_log(s->avctx, AV_LOG_ERROR, "Source data overread\n");

            return -1;

        }

        switch(s->compr){

        case TIFF_RAW:

            if (ssrc + size - src < width)

                return AVERROR_INVALIDDATA;

            if (!s->fill_order) {

                memcpy(dst, src, width);

            } else {

                int i;

                for (i = 0; i < width; i++)

                    dst[i] = av_reverse[src[i]];

            }

            src += width;

            break;

        case TIFF_PACKBITS:

            for(pixels = 0; pixels < width;){

                code = (int8_t)*src++;

                if(code >= 0){

                    code++;

                    if(pixels + code > width){

                        av_log(s->avctx, AV_LOG_ERROR, "Copy went out of bounds\n");

                        return -1;

                    }

                    memcpy(dst + pixels, src, code);

                    src += code;

                    pixels += code;

                }else if(code != -128){ // -127..-1

                    code = (-code) + 1;

                    if(pixels + code > width){

                        av_log(s->avctx, AV_LOG_ERROR, "Run went out of bounds\n");

                        return -1;

                    }

                    c = *src++;

                    memset(dst + pixels, c, code);

                    pixels += code;

                }

            }

            break;

        case TIFF_LZW:

            pixels = ff_lzw_decode(s->lzw, dst, width);

            if(pixels < width){

                av_log(s->avctx, AV_LOG_ERROR, "Decoded only %i bytes of %i\n", pixels, width);

                return -1;

            }

            break;

        }

        dst += stride;

    }

    return 0;

}

static int tiff_unpack_strip(TiffContext *s, uint8_t* dst, int stride, const uint8_t *src, int size, int lines){

    int c, line, pixels, code;

    const uint8_t *ssrc = src;

    int width = ((s->width * s->bpp) + 7) >> 3;

#if CONFIG_ZLIB

    uint8_t *zbuf; unsigned long outlen;



    if(s->compr == TIFF_DEFLATE || s->compr == TIFF_ADOBE_DEFLATE){

        int ret;

        outlen = width * lines;

        zbuf = av_malloc(outlen);

        ret = tiff_uncompress(zbuf, &outlen, src, size);

        if(ret != Z_OK){

            av_log(s->avctx, AV_LOG_ERROR, "Uncompressing failed (%lu of %lu) with error %d\n", outlen, (unsigned long)width * lines, ret);

            av_free(zbuf);

            return -1;

        }

        src = zbuf;

        for(line = 0; line < lines; line++){

            memcpy(dst, src, width);

            dst += stride;

            src += width;

        }

        av_free(zbuf);

        return 0;

    }

#endif

    if(s->compr == TIFF_LZW){

        if(ff_lzw_decode_init(s->lzw, 8, src, size, FF_LZW_TIFF) < 0){

            av_log(s->avctx, AV_LOG_ERROR, "Error initializing LZW decoder\n");

            return -1;

        }

    }

    if(s->compr == TIFF_CCITT_RLE || s->compr == TIFF_G3 || s->compr == TIFF_G4){

        int i, ret = 0;

        uint8_t *src2 = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);



        if(!src2 || (unsigned)size + FF_INPUT_BUFFER_PADDING_SIZE < (unsigned)size){

            av_log(s->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");

            return -1;

        }

        if(s->fax_opts & 2){

            av_log(s->avctx, AV_LOG_ERROR, "Uncompressed fax mode is not supported (yet)\n");

            av_free(src2);

            return -1;

        }

        if(!s->fill_order){

            memcpy(src2, src, size);

        }else{

            for(i = 0; i < size; i++)

                src2[i] = av_reverse[src[i]];

        }

        memset(src2+size, 0, FF_INPUT_BUFFER_PADDING_SIZE);

        switch(s->compr){

        case TIFF_CCITT_RLE:

        case TIFF_G3:

        case TIFF_G4:

            ret = ff_ccitt_unpack(s->avctx, src2, size, dst, lines, stride, s->compr, s->fax_opts);

            break;

        }

        av_free(src2);

        return ret;

    }

    for(line = 0; line < lines; line++){

        if(src - ssrc > size){

            av_log(s->avctx, AV_LOG_ERROR, "Source data overread\n");

            return -1;

        }

        switch(s->compr){

        case TIFF_RAW:

            if (ssrc + size - src < width)

                return AVERROR_INVALIDDATA;

            if (!s->fill_order) {

                memcpy(dst, src, width);

            } else {

                int i;

                for (i = 0; i < width; i++)

                    dst[i] = av_reverse[src[i]];

            }

            src += width;

            break;

        case TIFF_PACKBITS:

            for(pixels = 0; pixels < width;){

                code = (int8_t)*src++;

                if(code >= 0){

                    code++;

                    if(pixels + code > width){

                        av_log(s->avctx, AV_LOG_ERROR, "Copy went out of bounds\n");

                        return -1;

                    }

                    memcpy(dst + pixels, src, code);

                    src += code;

                    pixels += code;

                }else if(code != -128){ 

                    code = (-code) + 1;

                    if(pixels + code > width){

                        av_log(s->avctx, AV_LOG_ERROR, "Run went out of bounds\n");

                        return -1;

                    }

                    c = *src++;

                    memset(dst + pixels, c, code);

                    pixels += code;

                }

            }

            break;

        case TIFF_LZW:

            pixels = ff_lzw_decode(s->lzw, dst, width);

            if(pixels < width){

                av_log(s->avctx, AV_LOG_ERROR, "Decoded only %i bytes of %i\n", pixels, width);

                return -1;

            }

            break;

        }

        dst += stride;

    }

    return 0;

}

static int FUNC_0(TiffContext *VAR_0, uint8_t* VAR_1, int VAR_2, const uint8_t *VAR_3, int VAR_4, int VAR_5){

    int VAR_6, VAR_7, VAR_8, VAR_9;

    const uint8_t *VAR_10 = VAR_3;

    int VAR_11 = ((VAR_0->VAR_11 * VAR_0->bpp) + 7) >> 3;

#if CONFIG_ZLIB

    uint8_t *zbuf; unsigned long outlen;



    if(VAR_0->compr == TIFF_DEFLATE || VAR_0->compr == TIFF_ADOBE_DEFLATE){

        int VAR_13;

        outlen = VAR_11 * VAR_5;

        zbuf = av_malloc(outlen);

        VAR_13 = tiff_uncompress(zbuf, &outlen, VAR_3, VAR_4);

        if(VAR_13 != Z_OK){

            av_log(VAR_0->avctx, AV_LOG_ERROR, "Uncompressing failed (%lu of %lu) with error %d\n", outlen, (unsigned long)VAR_11 * VAR_5, VAR_13);

            av_free(zbuf);

            return -1;

        }

        VAR_3 = zbuf;

        for(VAR_7 = 0; VAR_7 < VAR_5; VAR_7++){

            memcpy(VAR_1, VAR_3, VAR_11);

            VAR_1 += VAR_2;

            VAR_3 += VAR_11;

        }

        av_free(zbuf);

        return 0;

    }

#endif

    if(VAR_0->compr == TIFF_LZW){

        if(ff_lzw_decode_init(VAR_0->lzw, 8, VAR_3, VAR_4, FF_LZW_TIFF) < 0){

            av_log(VAR_0->avctx, AV_LOG_ERROR, "Error initializing LZW decoder\n");

            return -1;

        }

    }

    if(VAR_0->compr == TIFF_CCITT_RLE || VAR_0->compr == TIFF_G3 || VAR_0->compr == TIFF_G4){

        int VAR_14, VAR_13 = 0;

        uint8_t *src2 = av_malloc(VAR_4 + FF_INPUT_BUFFER_PADDING_SIZE);



        if(!src2 || (unsigned)VAR_4 + FF_INPUT_BUFFER_PADDING_SIZE < (unsigned)VAR_4){

            av_log(VAR_0->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n");

            return -1;

        }

        if(VAR_0->fax_opts & 2){

            av_log(VAR_0->avctx, AV_LOG_ERROR, "Uncompressed fax mode is not supported (yet)\n");

            av_free(src2);

            return -1;

        }

        if(!VAR_0->fill_order){

            memcpy(src2, VAR_3, VAR_4);

        }else{

            for(VAR_14 = 0; VAR_14 < VAR_4; VAR_14++)

                src2[VAR_14] = av_reverse[VAR_3[VAR_14]];

        }

        memset(src2+VAR_4, 0, FF_INPUT_BUFFER_PADDING_SIZE);

        switch(VAR_0->compr){

        case TIFF_CCITT_RLE:

        case TIFF_G3:

        case TIFF_G4:

            VAR_13 = ff_ccitt_unpack(VAR_0->avctx, src2, VAR_4, VAR_1, VAR_5, VAR_2, VAR_0->compr, VAR_0->fax_opts);

            break;

        }

        av_free(src2);

        return VAR_13;

    }

    for(VAR_7 = 0; VAR_7 < VAR_5; VAR_7++){

        if(VAR_3 - VAR_10 > VAR_4){

            av_log(VAR_0->avctx, AV_LOG_ERROR, "Source data overread\n");

            return -1;

        }

        switch(VAR_0->compr){

        case TIFF_RAW:

            if (VAR_10 + VAR_4 - VAR_3 < VAR_11)

                return AVERROR_INVALIDDATA;

            if (!VAR_0->fill_order) {

                memcpy(VAR_1, VAR_3, VAR_11);

            } else {

                int VAR_14;

                for (VAR_14 = 0; VAR_14 < VAR_11; VAR_14++)

                    VAR_1[VAR_14] = av_reverse[VAR_3[VAR_14]];

            }

            VAR_3 += VAR_11;

            break;

        case TIFF_PACKBITS:

            for(VAR_8 = 0; VAR_8 < VAR_11;){

                VAR_9 = (int8_t)*VAR_3++;

                if(VAR_9 >= 0){

                    VAR_9++;

                    if(VAR_8 + VAR_9 > VAR_11){

                        av_log(VAR_0->avctx, AV_LOG_ERROR, "Copy went out of bounds\n");

                        return -1;

                    }

                    memcpy(VAR_1 + VAR_8, VAR_3, VAR_9);

                    VAR_3 += VAR_9;

                    VAR_8 += VAR_9;

                }else if(VAR_9 != -128){ 

                    VAR_9 = (-VAR_9) + 1;

                    if(VAR_8 + VAR_9 > VAR_11){

                        av_log(VAR_0->avctx, AV_LOG_ERROR, "Run went out of bounds\n");

                        return -1;

                    }

                    VAR_6 = *VAR_3++;

                    memset(VAR_1 + VAR_8, VAR_6, VAR_9);

                    VAR_8 += VAR_9;

                }

            }

            break;

        case TIFF_LZW:

            VAR_8 = ff_lzw_decode(VAR_0->lzw, VAR_1, VAR_11);

            if(VAR_8 < VAR_11){

                av_log(VAR_0->avctx, AV_LOG_ERROR, "Decoded only %VAR_14 bytes of %VAR_14\n", VAR_8, VAR_11);

                return -1;

            }

            break;

        }

        VAR_1 += VAR_2;

    }

    return 0;

}

static int FUNC_0(TiffContext *VAR_0, uint8_t* VAR_1, int VAR_2, const uint8_t *VAR_3, int VAR_4, int VAR_5){ int VAR_6, VAR_7, VAR_8, VAR_9; const uint8_t *VAR_10 = VAR_3; int VAR_11 = ((VAR_0->VAR_11 * VAR_0->bpp) + 7) >> 3; #if CONFIG_ZLIB
uint8_t *zbuf; unsigned long outlen; if(VAR_0->compr == TIFF_DEFLATE || VAR_0->compr == TIFF_ADOBE_DEFLATE){ int VAR_13; outlen = VAR_11 * VAR_5; zbuf = av_malloc(outlen); VAR_13 = tiff_uncompress(zbuf, &outlen, VAR_3, VAR_4); if(VAR_13 != Z_OK){ av_log(VAR_0->avctx, AV_LOG_ERROR, "Uncompressing failed (%lu of %lu) with error %d\n", outlen, (unsigned long)VAR_11 * VAR_5, VAR_13); av_free(zbuf); return -1; } VAR_3 = zbuf; for(VAR_7 = 0; VAR_7 < VAR_5; VAR_7++){ memcpy(VAR_1, VAR_3, VAR_11); VAR_1 += VAR_2; VAR_3 += VAR_11; } av_free(zbuf); return 0; } #endif
if(VAR_0->compr == TIFF_LZW){ if(ff_lzw_decode_init(VAR_0->lzw, 8, VAR_3, VAR_4, FF_LZW_TIFF) < 0){ av_log(VAR_0->avctx, AV_LOG_ERROR, "Error initializing LZW decoder\n"); return -1; } } if(VAR_0->compr == TIFF_CCITT_RLE || VAR_0->compr == TIFF_G3 || VAR_0->compr == TIFF_G4){ int VAR_14, VAR_13 = 0; uint8_t *src2 = av_malloc(VAR_4 + FF_INPUT_BUFFER_PADDING_SIZE); if(!src2 || (unsigned)VAR_4 + FF_INPUT_BUFFER_PADDING_SIZE < (unsigned)VAR_4){ av_log(VAR_0->avctx, AV_LOG_ERROR, "Error allocating temporary buffer\n"); return -1; } if(VAR_0->fax_opts & 2){ av_log(VAR_0->avctx, AV_LOG_ERROR, "Uncompressed fax mode is not supported (yet)\n"); av_free(src2); return -1; } if(!VAR_0->fill_order){ memcpy(src2, VAR_3, VAR_4); }else{ for(VAR_14 = 0; VAR_14 < VAR_4; VAR_14++) src2[VAR_14] = av_reverse[VAR_3[VAR_14]]; } memset(src2+VAR_4, 0, FF_INPUT_BUFFER_PADDING_SIZE); switch(VAR_0->compr){ case TIFF_CCITT_RLE:
case TIFF_G3:
case TIFF_G4:
VAR_13 = ff_ccitt_unpack(VAR_0->avctx, src2, VAR_4, VAR_1, VAR_5, VAR_2, VAR_0->compr, VAR_0->fax_opts); break; } av_free(src2); return VAR_13; } for(VAR_7 = 0; VAR_7 < VAR_5; VAR_7++){ if(VAR_3 - VAR_10 > VAR_4){ av_log(VAR_0->avctx, AV_LOG_ERROR, "Source data overread\n"); return -1; } switch(VAR_0->compr){ case TIFF_RAW:
if (VAR_10 + VAR_4 - VAR_3 < VAR_11)
return AVERROR_INVALIDDATA; if (!VAR_0->fill_order) { memcpy(VAR_1, VAR_3, VAR_11); } else { int VAR_14; for (VAR_14 = 0; VAR_14 < VAR_11; VAR_14++) VAR_1[VAR_14] = av_reverse[VAR_3[VAR_14]]; } VAR_3 += VAR_11; break; case TIFF_PACKBITS:
for(VAR_8 = 0; VAR_8 < VAR_11;){ VAR_9 = (int8_t)*VAR_3++; if(VAR_9 >= 0){ VAR_9++; if(VAR_8 + VAR_9 > VAR_11){ av_log(VAR_0->avctx, AV_LOG_ERROR, "Copy went out of bounds\n"); return -1; } memcpy(VAR_1 + VAR_8, VAR_3, VAR_9); VAR_3 += VAR_9; VAR_8 += VAR_9; }else if(VAR_9 != -128){ VAR_9 = (-VAR_9) + 1; if(VAR_8 + VAR_9 > VAR_11){ av_log(VAR_0->avctx, AV_LOG_ERROR, "Run went out of bounds\n"); return -1; } VAR_6 = *VAR_3++; memset(VAR_1 + VAR_8, VAR_6, VAR_9); VAR_8 += VAR_9; } } break; case TIFF_LZW:
VAR_8 = ff_lzw_decode(VAR_0->lzw, VAR_1, VAR_11); if(VAR_8 < VAR_11){ av_log(VAR_0->avctx, AV_LOG_ERROR, "Decoded only %VAR_14 bytes of %VAR_14\n", VAR_8, VAR_11); return -1; } break; } VAR_1 += VAR_2; } return 0; }