static int extract_header(AVCodecContext *const avctx,

                          const AVPacket *const avpkt) {

    const uint8_t *buf;

    unsigned buf_size;

    IffContext *s = avctx->priv_data;

    int palette_size;



    if (avctx->extradata_size < 2) {

        av_log(avctx, AV_LOG_ERROR, "not enough extradata\n");

        return AVERROR_INVALIDDATA;


    palette_size = avctx->extradata_size - AV_RB16(avctx->extradata);



    if (avpkt) {

        int image_size;

        if (avpkt->size < 2)

            return AVERROR_INVALIDDATA;

        image_size = avpkt->size - AV_RB16(avpkt->data);

        buf = avpkt->data;

        buf_size = bytestream_get_be16(&buf);

        if (buf_size <= 1 || image_size <= 1) {

            av_log(avctx, AV_LOG_ERROR,

                   "Invalid image size received: %u -> image data offset: %d\n",

                   buf_size, image_size);

            return AVERROR_INVALIDDATA;


    } else {

        buf = avctx->extradata;

        buf_size = bytestream_get_be16(&buf);

        if (buf_size <= 1 || palette_size < 0) {

            av_log(avctx, AV_LOG_ERROR,

                   "Invalid palette size received: %u -> palette data offset: %d\n",

                   buf_size, palette_size);

            return AVERROR_INVALIDDATA;





    if (buf_size > 8) {

        s->compression  = bytestream_get_byte(&buf);

        s->bpp          = bytestream_get_byte(&buf);

        s->ham          = bytestream_get_byte(&buf);

        s->flags        = bytestream_get_byte(&buf);

        s->transparency = bytestream_get_be16(&buf);

        s->masking      = bytestream_get_byte(&buf);

        if (s->masking == MASK_HAS_MASK) {

            if (s->bpp >= 8) {

                avctx->pix_fmt = PIX_FMT_RGB32;


                av_freep(&s->mask_palbuf);

                s->mask_buf = av_malloc((s->planesize * 32) + FF_INPUT_BUFFER_PADDING_SIZE);

                if (!s->mask_buf)







                s->mask_palbuf = av_malloc((2 << s->bpp) * sizeof(uint32_t) + FF_INPUT_BUFFER_PADDING_SIZE);

                if (!s->mask_palbuf) {





            s->bpp++;

        } else if (s->masking != MASK_NONE && s->masking != MASK_HAS_TRANSPARENT_COLOR) {

            av_log(avctx, AV_LOG_ERROR, "Masking not supported\n");

            return AVERROR_PATCHWELCOME;


        if (!s->bpp || s->bpp > 32) {

            av_log(avctx, AV_LOG_ERROR, "Invalid number of bitplanes: %u\n", s->bpp);

            return AVERROR_INVALIDDATA;

        } else if (s->ham >= 8) {

            av_log(avctx, AV_LOG_ERROR, "Invalid number of hold bits for HAM: %u\n", s->ham);

            return AVERROR_INVALIDDATA;




        av_freep(&s->ham_buf);

        av_freep(&s->ham_palbuf);



        if (s->ham) {

            int i, count = FFMIN(palette_size / 3, 1 << s->ham);

            int ham_count;

            const uint8_t *const palette = avctx->extradata + AV_RB16(avctx->extradata);



            s->ham_buf = av_malloc((s->planesize * 8) + FF_INPUT_BUFFER_PADDING_SIZE);

            if (!s->ham_buf)




            ham_count = 8 * (1 << s->ham);

            s->ham_palbuf = av_malloc((ham_count << !!(s->masking == MASK_HAS_MASK)) * sizeof (uint32_t) + FF_INPUT_BUFFER_PADDING_SIZE);

            if (!s->ham_palbuf) {

                av_freep(&s->ham_buf);





            if (count) { // HAM with color palette attached

                // prefill with black and palette and set HAM take direct value mask to zero

                memset(s->ham_palbuf, 0, (1 << s->ham) * 2 * sizeof (uint32_t));

                for (i=0; i < count; i++) {

                    s->ham_palbuf[i*2+1] = 0xFF000000 | AV_RL24(palette + i*3);


                count = 1 << s->ham;

            } else { // HAM with grayscale color palette

                count = 1 << s->ham;

                for (i=0; i < count; i++) {

                    s->ham_palbuf[i*2]   = 0xFF000000; // take direct color value from palette

                    s->ham_palbuf[i*2+1] = 0xFF000000 | av_le2ne32(gray2rgb((i * 255) >> s->ham));



            for (i=0; i < count; i++) {

                uint32_t tmp = i << (8 - s->ham);

                tmp |= tmp >> s->ham;

                s->ham_palbuf[(i+count)*2]     = 0xFF00FFFF; // just modify blue color component

                s->ham_palbuf[(i+count*2)*2]   = 0xFFFFFF00; // just modify red color component

                s->ham_palbuf[(i+count*3)*2]   = 0xFFFF00FF; // just modify green color component

                s->ham_palbuf[(i+count)*2+1]   = 0xFF000000 | tmp << 16;

                s->ham_palbuf[(i+count*2)*2+1] = 0xFF000000 | tmp;

                s->ham_palbuf[(i+count*3)*2+1] = 0xFF000000 | tmp << 8;


            if (s->masking == MASK_HAS_MASK) {

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

                    s->ham_palbuf[(1 << s->bpp) + i] = s->ham_palbuf[i] | 0xFF000000;






    return 0;

static int extract_header(AVCodecContext *const avctx,

                          const AVPacket *const avpkt) {

    const uint8_t *buf;

    unsigned buf_size;

    IffContext *s = avctx->priv_data;

    int palette_size;



    if (avctx->extradata_size < 2) {

        av_log(avctx, AV_LOG_ERROR, "not enough extradata\n");

        return AVERROR_INVALIDDATA;


    palette_size = avctx->extradata_size - AV_RB16(avctx->extradata);



    if (avpkt) {

        int image_size;

        if (avpkt->size < 2)

            return AVERROR_INVALIDDATA;

        image_size = avpkt->size - AV_RB16(avpkt->data);

        buf = avpkt->data;

        buf_size = bytestream_get_be16(&buf);

        if (buf_size <= 1 || image_size <= 1) {

            av_log(avctx, AV_LOG_ERROR,

                   "Invalid image size received: %u -> image data offset: %d\n",

                   buf_size, image_size);

            return AVERROR_INVALIDDATA;


    } else {

        buf = avctx->extradata;

        buf_size = bytestream_get_be16(&buf);

        if (buf_size <= 1 || palette_size < 0) {

            av_log(avctx, AV_LOG_ERROR,

                   "Invalid palette size received: %u -> palette data offset: %d\n",

                   buf_size, palette_size);

            return AVERROR_INVALIDDATA;





    if (buf_size > 8) {

        s->compression  = bytestream_get_byte(&buf);

        s->bpp          = bytestream_get_byte(&buf);

        s->ham          = bytestream_get_byte(&buf);

        s->flags        = bytestream_get_byte(&buf);

        s->transparency = bytestream_get_be16(&buf);

        s->masking      = bytestream_get_byte(&buf);

        if (s->masking == MASK_HAS_MASK) {

            if (s->bpp >= 8) {

                avctx->pix_fmt = PIX_FMT_RGB32;


                av_freep(&s->mask_palbuf);

                s->mask_buf = av_malloc((s->planesize * 32) + FF_INPUT_BUFFER_PADDING_SIZE);

                if (!s->mask_buf)







                s->mask_palbuf = av_malloc((2 << s->bpp) * sizeof(uint32_t) + FF_INPUT_BUFFER_PADDING_SIZE);

                if (!s->mask_palbuf) {





            s->bpp++;

        } else if (s->masking != MASK_NONE && s->masking != MASK_HAS_TRANSPARENT_COLOR) {

            av_log(avctx, AV_LOG_ERROR, "Masking not supported\n");

            return AVERROR_PATCHWELCOME;


        if (!s->bpp || s->bpp > 32) {

            av_log(avctx, AV_LOG_ERROR, "Invalid number of bitplanes: %u\n", s->bpp);

            return AVERROR_INVALIDDATA;

        } else if (s->ham >= 8) {

            av_log(avctx, AV_LOG_ERROR, "Invalid number of hold bits for HAM: %u\n", s->ham);

            return AVERROR_INVALIDDATA;




        av_freep(&s->ham_buf);

        av_freep(&s->ham_palbuf);



        if (s->ham) {

            int i, count = FFMIN(palette_size / 3, 1 << s->ham);

            int ham_count;

            const uint8_t *const palette = avctx->extradata + AV_RB16(avctx->extradata);



            s->ham_buf = av_malloc((s->planesize * 8) + FF_INPUT_BUFFER_PADDING_SIZE);

            if (!s->ham_buf)




            ham_count = 8 * (1 << s->ham);

            s->ham_palbuf = av_malloc((ham_count << !!(s->masking == MASK_HAS_MASK)) * sizeof (uint32_t) + FF_INPUT_BUFFER_PADDING_SIZE);

            if (!s->ham_palbuf) {

                av_freep(&s->ham_buf);





            if (count) { 

                

                memset(s->ham_palbuf, 0, (1 << s->ham) * 2 * sizeof (uint32_t));

                for (i=0; i < count; i++) {

                    s->ham_palbuf[i*2+1] = 0xFF000000 | AV_RL24(palette + i*3);


                count = 1 << s->ham;

            } else { 

                count = 1 << s->ham;

                for (i=0; i < count; i++) {

                    s->ham_palbuf[i*2]   = 0xFF000000; 

                    s->ham_palbuf[i*2+1] = 0xFF000000 | av_le2ne32(gray2rgb((i * 255) >> s->ham));



            for (i=0; i < count; i++) {

                uint32_t tmp = i << (8 - s->ham);

                tmp |= tmp >> s->ham;

                s->ham_palbuf[(i+count)*2]     = 0xFF00FFFF; 

                s->ham_palbuf[(i+count*2)*2]   = 0xFFFFFF00; 

                s->ham_palbuf[(i+count*3)*2]   = 0xFFFF00FF; 

                s->ham_palbuf[(i+count)*2+1]   = 0xFF000000 | tmp << 16;

                s->ham_palbuf[(i+count*2)*2+1] = 0xFF000000 | tmp;

                s->ham_palbuf[(i+count*3)*2+1] = 0xFF000000 | tmp << 8;


            if (s->masking == MASK_HAS_MASK) {

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

                    s->ham_palbuf[(1 << s->bpp) + i] = s->ham_palbuf[i] | 0xFF000000;






    return 0;

static int FUNC_0(AVCodecContext *const VAR_0,

                          const AVPacket *const VAR_1) {

    const uint8_t *VAR_2;

    unsigned VAR_3;

    IffContext *s = VAR_0->priv_data;

    int VAR_4;



    if (VAR_0->extradata_size < 2) {

        av_log(VAR_0, AV_LOG_ERROR, "not enough extradata\n");

        return AVERROR_INVALIDDATA;


    VAR_4 = VAR_0->extradata_size - AV_RB16(VAR_0->extradata);



    if (VAR_1) {

        int VAR_5;

        if (VAR_1->size < 2)

            return AVERROR_INVALIDDATA;

        VAR_5 = VAR_1->size - AV_RB16(VAR_1->data);

        VAR_2 = VAR_1->data;

        VAR_3 = bytestream_get_be16(&VAR_2);

        if (VAR_3 <= 1 || VAR_5 <= 1) {

            av_log(VAR_0, AV_LOG_ERROR,

                   "Invalid image size received: %u -> image data offset: %d\n",

                   VAR_3, VAR_5);

            return AVERROR_INVALIDDATA;


    } else {

        VAR_2 = VAR_0->extradata;

        VAR_3 = bytestream_get_be16(&VAR_2);

        if (VAR_3 <= 1 || VAR_4 < 0) {

            av_log(VAR_0, AV_LOG_ERROR,

                   "Invalid VAR_9 size received: %u -> VAR_9 data offset: %d\n",

                   VAR_3, VAR_4);

            return AVERROR_INVALIDDATA;





    if (VAR_3 > 8) {

        s->compression  = bytestream_get_byte(&VAR_2);

        s->bpp          = bytestream_get_byte(&VAR_2);

        s->ham          = bytestream_get_byte(&VAR_2);

        s->flags        = bytestream_get_byte(&VAR_2);

        s->transparency = bytestream_get_be16(&VAR_2);

        s->masking      = bytestream_get_byte(&VAR_2);

        if (s->masking == MASK_HAS_MASK) {

            if (s->bpp >= 8) {

                VAR_0->pix_fmt = PIX_FMT_RGB32;


                av_freep(&s->mask_palbuf);

                s->mask_buf = av_malloc((s->planesize * 32) + FF_INPUT_BUFFER_PADDING_SIZE);

                if (!s->mask_buf)







                s->mask_palbuf = av_malloc((2 << s->bpp) * sizeof(uint32_t) + FF_INPUT_BUFFER_PADDING_SIZE);

                if (!s->mask_palbuf) {





            s->bpp++;

        } else if (s->masking != MASK_NONE && s->masking != MASK_HAS_TRANSPARENT_COLOR) {

            av_log(VAR_0, AV_LOG_ERROR, "Masking not supported\n");

            return AVERROR_PATCHWELCOME;


        if (!s->bpp || s->bpp > 32) {

            av_log(VAR_0, AV_LOG_ERROR, "Invalid number of bitplanes: %u\n", s->bpp);

            return AVERROR_INVALIDDATA;

        } else if (s->ham >= 8) {

            av_log(VAR_0, AV_LOG_ERROR, "Invalid number of hold bits for HAM: %u\n", s->ham);

            return AVERROR_INVALIDDATA;




        av_freep(&s->ham_buf);

        av_freep(&s->ham_palbuf);



        if (s->ham) {

            int VAR_6, VAR_7 = FFMIN(VAR_4 / 3, 1 << s->ham);

            int VAR_8;

            const uint8_t *const VAR_9 = VAR_0->extradata + AV_RB16(VAR_0->extradata);



            s->ham_buf = av_malloc((s->planesize * 8) + FF_INPUT_BUFFER_PADDING_SIZE);

            if (!s->ham_buf)




            VAR_8 = 8 * (1 << s->ham);

            s->ham_palbuf = av_malloc((VAR_8 << !!(s->masking == MASK_HAS_MASK)) * sizeof (uint32_t) + FF_INPUT_BUFFER_PADDING_SIZE);

            if (!s->ham_palbuf) {

                av_freep(&s->ham_buf);





            if (VAR_7) { 

                

                memset(s->ham_palbuf, 0, (1 << s->ham) * 2 * sizeof (uint32_t));

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

                    s->ham_palbuf[VAR_6*2+1] = 0xFF000000 | AV_RL24(VAR_9 + VAR_6*3);


                VAR_7 = 1 << s->ham;

            } else { 

                VAR_7 = 1 << s->ham;

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

                    s->ham_palbuf[VAR_6*2]   = 0xFF000000; 

                    s->ham_palbuf[VAR_6*2+1] = 0xFF000000 | av_le2ne32(gray2rgb((VAR_6 * 255) >> s->ham));



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

                uint32_t tmp = VAR_6 << (8 - s->ham);

                tmp |= tmp >> s->ham;

                s->ham_palbuf[(VAR_6+VAR_7)*2]     = 0xFF00FFFF; 

                s->ham_palbuf[(VAR_6+VAR_7*2)*2]   = 0xFFFFFF00; 

                s->ham_palbuf[(VAR_6+VAR_7*3)*2]   = 0xFFFF00FF; 

                s->ham_palbuf[(VAR_6+VAR_7)*2+1]   = 0xFF000000 | tmp << 16;

                s->ham_palbuf[(VAR_6+VAR_7*2)*2+1] = 0xFF000000 | tmp;

                s->ham_palbuf[(VAR_6+VAR_7*3)*2+1] = 0xFF000000 | tmp << 8;


            if (s->masking == MASK_HAS_MASK) {

                for (VAR_6 = 0; VAR_6 < VAR_8; VAR_6++)

                    s->ham_palbuf[(1 << s->bpp) + VAR_6] = s->ham_palbuf[VAR_6] | 0xFF000000;






    return 0;

static int FUNC_0(AVCodecContext *const VAR_0,
const AVPacket *const VAR_1) { const uint8_t *VAR_2; unsigned VAR_3; IffContext *s = VAR_0->priv_data; int VAR_4; if (VAR_0->extradata_size < 2) { av_log(VAR_0, AV_LOG_ERROR, "not enough extradata\n"); return AVERROR_INVALIDDATA; VAR_4 = VAR_0->extradata_size - AV_RB16(VAR_0->extradata); if (VAR_1) { int VAR_5; if (VAR_1->size < 2)
return AVERROR_INVALIDDATA; VAR_5 = VAR_1->size - AV_RB16(VAR_1->data); VAR_2 = VAR_1->data; VAR_3 = bytestream_get_be16(&VAR_2); if (VAR_3 <= 1 || VAR_5 <= 1) { av_log(VAR_0, AV_LOG_ERROR,
"Invalid image size received: %u -> image data offset: %d\n",
VAR_3, VAR_5); return AVERROR_INVALIDDATA; } else { VAR_2 = VAR_0->extradata; VAR_3 = bytestream_get_be16(&VAR_2); if (VAR_3 <= 1 || VAR_4 < 0) { av_log(VAR_0, AV_LOG_ERROR,
"Invalid VAR_9 size received: %u -> VAR_9 data offset: %d\n",
VAR_3, VAR_4); return AVERROR_INVALIDDATA; if (VAR_3 > 8) { s->compression  = bytestream_get_byte(&VAR_2); s->bpp          = bytestream_get_byte(&VAR_2); s->ham          = bytestream_get_byte(&VAR_2); s->flags        = bytestream_get_byte(&VAR_2); s->transparency = bytestream_get_be16(&VAR_2); s->masking      = bytestream_get_byte(&VAR_2); if (s->masking == MASK_HAS_MASK) { if (s->bpp >= 8) { VAR_0->pix_fmt = PIX_FMT_RGB32; av_freep(&s->mask_palbuf); s->mask_buf = av_malloc((s->planesize * 32) + FF_INPUT_BUFFER_PADDING_SIZE); if (!s->mask_buf)
s->mask_palbuf = av_malloc((2 << s->bpp) * sizeof(uint32_t) + FF_INPUT_BUFFER_PADDING_SIZE); if (!s->mask_palbuf) { s->bpp++; } else if (s->masking != MASK_NONE && s->masking != MASK_HAS_TRANSPARENT_COLOR) { av_log(VAR_0, AV_LOG_ERROR, "Masking not supported\n"); return AVERROR_PATCHWELCOME; if (!s->bpp || s->bpp > 32) { av_log(VAR_0, AV_LOG_ERROR, "Invalid number of bitplanes: %u\n", s->bpp); return AVERROR_INVALIDDATA; } else if (s->ham >= 8) { av_log(VAR_0, AV_LOG_ERROR, "Invalid number of hold bits for HAM: %u\n", s->ham); return AVERROR_INVALIDDATA; av_freep(&s->ham_buf); av_freep(&s->ham_palbuf); if (s->ham) { int VAR_6, VAR_7 = FFMIN(VAR_4 / 3, 1 << s->ham); int VAR_8; const uint8_t *const VAR_9 = VAR_0->extradata + AV_RB16(VAR_0->extradata); s->ham_buf = av_malloc((s->planesize * 8) + FF_INPUT_BUFFER_PADDING_SIZE); if (!s->ham_buf)
VAR_8 = 8 * (1 << s->ham); s->ham_palbuf = av_malloc((VAR_8 << !!(s->masking == MASK_HAS_MASK)) * sizeof (uint32_t) + FF_INPUT_BUFFER_PADDING_SIZE); if (!s->ham_palbuf) { av_freep(&s->ham_buf); if (VAR_7) { memset(s->ham_palbuf, 0, (1 << s->ham) * 2 * sizeof (uint32_t)); for (VAR_6=0; VAR_6 < VAR_7; VAR_6++) { s->ham_palbuf[VAR_6*2+1] = 0xFF000000 | AV_RL24(VAR_9 + VAR_6*3); VAR_7 = 1 << s->ham; } else { VAR_7 = 1 << s->ham; for (VAR_6=0; VAR_6 < VAR_7; VAR_6++) { s->ham_palbuf[VAR_6*2]   = 0xFF000000; s->ham_palbuf[VAR_6*2+1] = 0xFF000000 | av_le2ne32(gray2rgb((VAR_6 * 255) >> s->ham)); for (VAR_6=0; VAR_6 < VAR_7; VAR_6++) { uint32_t tmp = VAR_6 << (8 - s->ham); tmp |= tmp >> s->ham; s->ham_palbuf[(VAR_6+VAR_7)*2]     = 0xFF00FFFF; s->ham_palbuf[(VAR_6+VAR_7*2)*2]   = 0xFFFFFF00; s->ham_palbuf[(VAR_6+VAR_7*3)*2]   = 0xFFFF00FF; s->ham_palbuf[(VAR_6+VAR_7)*2+1]   = 0xFF000000 | tmp << 16; s->ham_palbuf[(VAR_6+VAR_7*2)*2+1] = 0xFF000000 | tmp; s->ham_palbuf[(VAR_6+VAR_7*3)*2+1] = 0xFF000000 | tmp << 8; if (s->masking == MASK_HAS_MASK) { for (VAR_6 = 0; VAR_6 < VAR_8; VAR_6++) s->ham_palbuf[(1 << s->bpp) + VAR_6] = s->ham_palbuf[VAR_6] | 0xFF000000; return 0;