]> git.sesse.net Git - ffmpeg/blobdiff - tools/qt-faststart.c
Ignore all generated example binaries
[ffmpeg] / tools / qt-faststart.c
index 0abcd2b67cce1a1cff83e8250253aac821e3332c..0db5ca21384c5eb3f8fc3117b9c7dd7016b79853 100644 (file)
 #include <string.h>
 
 #ifdef __MINGW32__
+#undef fseeko
 #define fseeko(x, y, z) fseeko64(x, y, z)
+#undef ftello
 #define ftello(x)       ftello64(x)
 #elif defined(_WIN32)
+#undef fseeko
 #define fseeko(x, y, z) _fseeki64(x, y, z)
+#undef ftello
 #define ftello(x)       _ftelli64(x)
 #endif
 
 
 #define BE_16(x) ((((uint8_t*)(x))[0] <<  8) | ((uint8_t*)(x))[1])
 
-#define BE_32(x) ((((uint8_t*)(x))[0] << 24) |  \
-                  (((uint8_t*)(x))[1] << 16) |  \
-                  (((uint8_t*)(x))[2] <<  8) |  \
-                   ((uint8_t*)(x))[3])
+#define BE_32(x) (((uint32_t)(((uint8_t*)(x))[0]) << 24) |  \
+                             (((uint8_t*)(x))[1]  << 16) |  \
+                             (((uint8_t*)(x))[2]  <<  8) |  \
+                              ((uint8_t*)(x))[3])
 
 #define BE_64(x) (((uint64_t)(((uint8_t*)(x))[0]) << 56) |  \
                   ((uint64_t)(((uint8_t*)(x))[1]) << 48) |  \
@@ -89,7 +93,7 @@ int main(int argc, char *argv[])
     uint32_t atom_type   = 0;
     uint64_t atom_size   = 0;
     uint64_t atom_offset = 0;
-    uint64_t last_offset;
+    int64_t last_offset;
     unsigned char *moov_atom = NULL;
     unsigned char *ftyp_atom = NULL;
     uint64_t moov_atom_size;
@@ -97,12 +101,13 @@ int main(int argc, char *argv[])
     uint64_t i, j;
     uint32_t offset_count;
     uint64_t current_offset;
-    uint64_t start_offset = 0;
+    int64_t start_offset = 0;
     unsigned char copy_buffer[COPY_BUFFER_SIZE];
     int bytes_to_copy;
 
     if (argc != 3) {
-        printf("Usage: qt-faststart <infile.mov> <outfile.mov>\n");
+        printf("Usage: qt-faststart <infile.mov> <outfile.mov>\n"
+               "Note: alternatively you can use -movflags +faststart in avconv\n");
         return 0;
     }
 
@@ -123,7 +128,7 @@ int main(int argc, char *argv[])
         if (fread(atom_bytes, ATOM_PREAMBLE_SIZE, 1, infile) != 1) {
             break;
         }
-        atom_size = (uint32_t) BE_32(&atom_bytes[0]);
+        atom_size = BE_32(&atom_bytes[0]);
         atom_type = BE_32(&atom_bytes[4]);
 
         /* keep ftyp atom */
@@ -137,11 +142,11 @@ int main(int argc, char *argv[])
                 goto error_out;
             }
             if (fseeko(infile, -ATOM_PREAMBLE_SIZE, SEEK_CUR) ||
-                fread(ftyp_atom, atom_size, 1, infile) != 1) {
+                fread(ftyp_atom, atom_size, 1, infile) != 1 ||
+                (start_offset = ftello(infile)) < 0) {
                 perror(argv[1]);
                 goto error_out;
             }
-            start_offset = ftello(infile);
         } else {
             int ret;
             /* 64-bit special case */
@@ -202,6 +207,10 @@ int main(int argc, char *argv[])
         goto error_out;
     }
     last_offset    = ftello(infile);
+    if (last_offset < 0) {
+        perror(argv[1]);
+        goto error_out;
+    }
     moov_atom_size = atom_size;
     moov_atom      = malloc(moov_atom_size);
     if (!moov_atom) {
@@ -235,6 +244,10 @@ int main(int argc, char *argv[])
                 goto error_out;
             }
             offset_count = BE_32(&moov_atom[i + 8]);
+            if (i + 12 + offset_count * UINT64_C(4) > moov_atom_size) {
+                printf(" bad atom size/element count\n");
+                goto error_out;
+            }
             for (j = 0; j < offset_count; j++) {
                 current_offset  = BE_32(&moov_atom[i + 12 + j * 4]);
                 current_offset += moov_atom_size;
@@ -252,6 +265,10 @@ int main(int argc, char *argv[])
                 goto error_out;
             }
             offset_count = BE_32(&moov_atom[i + 8]);
+            if (i + 12 + offset_count * UINT64_C(8) > moov_atom_size) {
+                printf(" bad atom size/element count\n");
+                goto error_out;
+            }
             for (j = 0; j < offset_count; j++) {
                 current_offset  = BE_64(&moov_atom[i + 12 + j * 8]);
                 current_offset += moov_atom_size;