]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/pnm_parser.c
avdevice/avdevice: Constify avdevice_list_input_sources/output_sinks
[ffmpeg] / libavcodec / pnm_parser.c
index 210a8a048e722b9cca3d2b574960a3d057895437..a822c17a2e932e107e8487cc2083e6df782c0fb2 100644 (file)
@@ -19,6 +19,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include "libavutil/avassert.h"
 #include "libavutil/imgutils.h"
 
 #include "parser.h" //for ParseContext
@@ -27,6 +28,7 @@
 typedef struct PNMParseContext {
     ParseContext pc;
     int remaining_bytes;
+    int ascii_scan;
 }PNMParseContext;
 
 static int pnm_parse(AVCodecParserContext *s, AVCodecContext *avctx,
@@ -39,8 +41,11 @@ static int pnm_parse(AVCodecParserContext *s, AVCodecContext *avctx,
     int next = END_NOT_FOUND;
     int skip = 0;
 
-    for (; pc->overread > 0; pc->overread--) {
-        pc->buffer[pc->index++]= pc->buffer[pc->overread_index++];
+    if (pc->overread > 0) {
+        memmove(pc->buffer + pc->index, pc->buffer + pc->overread_index, pc->overread);
+        pc->index          += pc->overread;
+        pc->overread_index += pc->overread;
+        pc->overread = 0;
     }
 
     if (pnmpc->remaining_bytes) {
@@ -67,6 +72,7 @@ retry:
         if (pnmctx.bytestream < pnmctx.bytestream_end) {
             if (pc->index) {
                 pc->index = 0;
+                pnmpc->ascii_scan = 0;
             } else {
                 unsigned step = FFMAX(1, pnmctx.bytestream - pnmctx.bytestream_start);
 
@@ -77,20 +83,36 @@ retry:
     } else if (pnmctx.type < 4) {
               uint8_t *bs  = pnmctx.bytestream;
         const uint8_t *end = pnmctx.bytestream_end;
+        uint8_t *sync      = bs;
+
+        if (pc->index) {
+            av_assert0(pnmpc->ascii_scan <= end - bs);
+            bs += pnmpc->ascii_scan;
+        }
 
         while (bs < end) {
-            int c = *bs++;
+            int c;
+            sync = bs;
+            c = *bs++;
             if (c == '#')  {
-                while (c != '\n' && bs < end)
-                    c = *bs++;
+                uint8_t *match = memchr(bs, '\n', end-bs);
+                if (match)
+                    bs = match + 1;
+                else
+                    break;
             } else if (c == 'P') {
                 next = bs - pnmctx.bytestream_start + skip - 1;
+                pnmpc->ascii_scan = 0;
                 break;
             }
         }
+        if (next == END_NOT_FOUND)
+            pnmpc->ascii_scan = sync - pnmctx.bytestream + skip;
     } else {
-        next = pnmctx.bytestream - pnmctx.bytestream_start + skip
-               + av_image_get_buffer_size(avctx->pix_fmt, avctx->width, avctx->height, 1);
+        int ret = av_image_get_buffer_size(avctx->pix_fmt, avctx->width, avctx->height, 1);
+        next = pnmctx.bytestream - pnmctx.bytestream_start + skip;
+        if (ret >= 0 && next + (uint64_t)ret <= INT_MAX)
+            next += ret;
     }
     if (next != END_NOT_FOUND && pnmctx.bytestream_start != buf + skip)
         next -= pc->index;