static int targa_decode_rle(AVCodecContext *avctx, TargaContext *s, const uint8_t *src, int src_size, uint8_t *dst, int w, int h, int stride, int bpp)

{

    int i, x, y;

    int depth = (bpp + 1) >> 3;

    int type, count;

    int diff;

    const uint8_t *src_end = src + src_size;



    diff = stride - w * depth;

    x = y = 0;

    while(y < h){

        CHECK_BUFFER_SIZE(src, src_end, 1, "image type");

        type = *src++;

        count = (type & 0x7F) + 1;

        type &= 0x80;

        if((x + count > w) && (x + count + 1 > (h - y) * w)){

            av_log(avctx, AV_LOG_ERROR, "Packet went out of bounds: position (%i,%i) size %i\n", x, y, count);

            return -1;

        }

        if(type){

            CHECK_BUFFER_SIZE(src, src_end, depth, "image data");

        }else{

            CHECK_BUFFER_SIZE(src, src_end, count * depth, "image data");

        }

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

            switch(depth){

            case 1:

                *dst = *src;

                break;

            case 2:

                AV_WN16A(dst, AV_RN16A(src));

                break;

            case 3:

                dst[0] = src[0];

                dst[1] = src[1];

                dst[2] = src[2];

                break;

            case 4:

                AV_WN32A(dst, AV_RN32A(src));

                break;

            }

            dst += depth;

            if(!type)

                src += depth;



            x++;

            if(x == w){

                x = 0;

                y++;

                dst += diff;

            }

        }

        if(type)

            src += depth;

    }

    return src_size;

}

static int targa_decode_rle(AVCodecContext *avctx, TargaContext *s, const uint8_t *src, int src_size, uint8_t *dst, int w, int h, int stride, int bpp)

{

    int i, x, y;

    int depth = (bpp + 1) >> 3;

    int type, count;

    int diff;

    const uint8_t *src_end = src + src_size;



    diff = stride - w * depth;

    x = y = 0;

    while(y < h){

        CHECK_BUFFER_SIZE(src, src_end, 1, "image type");

        type = *src++;

        count = (type & 0x7F) + 1;

        type &= 0x80;

        if((x + count > w) && (x + count + 1 > (h - y) * w)){

            av_log(avctx, AV_LOG_ERROR, "Packet went out of bounds: position (%i,%i) size %i\n", x, y, count);

            return -1;

        }

        if(type){

            CHECK_BUFFER_SIZE(src, src_end, depth, "image data");

        }else{

            CHECK_BUFFER_SIZE(src, src_end, count * depth, "image data");

        }

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

            switch(depth){

            case 1:

                *dst = *src;

                break;

            case 2:

                AV_WN16A(dst, AV_RN16A(src));

                break;

            case 3:

                dst[0] = src[0];

                dst[1] = src[1];

                dst[2] = src[2];

                break;

            case 4:

                AV_WN32A(dst, AV_RN32A(src));

                break;

            }

            dst += depth;

            if(!type)

                src += depth;



            x++;

            if(x == w){

                x = 0;

                y++;

                dst += diff;

            }

        }

        if(type)

            src += depth;

    }

    return src_size;

}

static int FUNC_0(AVCodecContext *VAR_0, TargaContext *VAR_1, const uint8_t *VAR_2, int VAR_3, uint8_t *VAR_4, int VAR_5, int VAR_6, int VAR_7, int VAR_8)

{

    int VAR_9, VAR_10, VAR_11;

    int VAR_12 = (VAR_8 + 1) >> 3;

    int VAR_13, VAR_14;

    int VAR_15;

    const uint8_t *VAR_16 = VAR_2 + VAR_3;



    VAR_15 = VAR_7 - VAR_5 * VAR_12;

    VAR_10 = VAR_11 = 0;

    while(VAR_11 < VAR_6){

        CHECK_BUFFER_SIZE(VAR_2, VAR_16, 1, "image VAR_13");

        VAR_13 = *VAR_2++;

        VAR_14 = (VAR_13 & 0x7F) + 1;

        VAR_13 &= 0x80;

        if((VAR_10 + VAR_14 > VAR_5) && (VAR_10 + VAR_14 + 1 > (VAR_6 - VAR_11) * VAR_5)){

            av_log(VAR_0, AV_LOG_ERROR, "Packet went out of bounds: position (%VAR_9,%VAR_9) size %VAR_9\n", VAR_10, VAR_11, VAR_14);

            return -1;

        }

        if(VAR_13){

            CHECK_BUFFER_SIZE(VAR_2, VAR_16, VAR_12, "image data");

        }else{

            CHECK_BUFFER_SIZE(VAR_2, VAR_16, VAR_14 * VAR_12, "image data");

        }

        for(VAR_9 = 0; VAR_9 < VAR_14; VAR_9++){

            switch(VAR_12){

            case 1:

                *VAR_4 = *VAR_2;

                break;

            case 2:

                AV_WN16A(VAR_4, AV_RN16A(VAR_2));

                break;

            case 3:

                VAR_4[0] = VAR_2[0];

                VAR_4[1] = VAR_2[1];

                VAR_4[2] = VAR_2[2];

                break;

            case 4:

                AV_WN32A(VAR_4, AV_RN32A(VAR_2));

                break;

            }

            VAR_4 += VAR_12;

            if(!VAR_13)

                VAR_2 += VAR_12;



            VAR_10++;

            if(VAR_10 == VAR_5){

                VAR_10 = 0;

                VAR_11++;

                VAR_4 += VAR_15;

            }

        }

        if(VAR_13)

            VAR_2 += VAR_12;

    }

    return VAR_3;

}

static int FUNC_0(AVCodecContext *VAR_0, TargaContext *VAR_1, const uint8_t *VAR_2, int VAR_3, uint8_t *VAR_4, int VAR_5, int VAR_6, int VAR_7, int VAR_8)
{ int VAR_9, VAR_10, VAR_11; int VAR_12 = (VAR_8 + 1) >> 3; int VAR_13, VAR_14; int VAR_15; const uint8_t *VAR_16 = VAR_2 + VAR_3; VAR_15 = VAR_7 - VAR_5 * VAR_12; VAR_10 = VAR_11 = 0; while(VAR_11 < VAR_6){ CHECK_BUFFER_SIZE(VAR_2, VAR_16, 1, "image VAR_13"); VAR_13 = *VAR_2++; VAR_14 = (VAR_13 & 0x7F) + 1; VAR_13 &= 0x80; if((VAR_10 + VAR_14 > VAR_5) && (VAR_10 + VAR_14 + 1 > (VAR_6 - VAR_11) * VAR_5)){ av_log(VAR_0, AV_LOG_ERROR, "Packet went out of bounds: position (%VAR_9,%VAR_9) size %VAR_9\n", VAR_10, VAR_11, VAR_14); return -1; } if(VAR_13){ CHECK_BUFFER_SIZE(VAR_2, VAR_16, VAR_12, "image data"); }else{ CHECK_BUFFER_SIZE(VAR_2, VAR_16, VAR_14 * VAR_12, "image data"); } for(VAR_9 = 0; VAR_9 < VAR_14; VAR_9++){ switch(VAR_12){ case 1:
*VAR_4 = *VAR_2; break; case 2:
AV_WN16A(VAR_4, AV_RN16A(VAR_2)); break; case 3:
VAR_4[0] = VAR_2[0]; VAR_4[1] = VAR_2[1]; VAR_4[2] = VAR_2[2]; break; case 4:
AV_WN32A(VAR_4, AV_RN32A(VAR_2)); break; } VAR_4 += VAR_12; if(!VAR_13)
VAR_2 += VAR_12; VAR_10++; if(VAR_10 == VAR_5){ VAR_10 = 0; VAR_11++; VAR_4 += VAR_15; } } if(VAR_13)
VAR_2 += VAR_12; } return VAR_3; }