- if((compr & (~TGA_RLE)) == TGA_NODATA)
- memset(p->data[0], 0, p->linesize[0] * s->height);
- else{
- if(compr & TGA_RLE)
- targa_decode_rle(avctx, s, buf, dst, avctx->width, avctx->height, stride, bpp);
- else{
- for(y = 0; y < s->height; y++){
-#ifdef WORDS_BIGENDIAN
- if((s->bpp + 1) >> 3 == 2){
- uint16_t *dst16 = (uint16_t*)dst;
- for(x = 0; x < s->width; x++)
- dst16[x] = AV_RL16(buf + x * 2);
- }else if((s->bpp + 1) >> 3 == 4){
- uint32_t *dst32 = (uint32_t*)dst;
- for(x = 0; x < s->width; x++)
- dst32[x] = AV_RL32(buf + x * 4);
- }else
-#endif
- memcpy(dst, buf, s->width * ((s->bpp + 1) >> 3));
-
+ if ((compr & (~TGA_RLE)) == TGA_NODATA) {
+ memset(p->data[0], 0, p->linesize[0] * h);
+ } else {
+ if(compr & TGA_RLE){
+ int res = targa_decode_rle(avctx, s, dst, w, h, stride, bpp);
+ if (res < 0)
+ return res;
+ } else {
+ size_t img_size = w * ((bpp + 1) >> 3);
+ if (bytestream2_get_bytes_left(&s->gb) < img_size * h) {
+ av_log(avctx, AV_LOG_ERROR,
+ "Not enough data available for image\n");
+ return AVERROR_INVALIDDATA;
+ }
+ for (y = 0; y < h; y++) {
+ bytestream2_get_bufferu(&s->gb, dst, img_size);