--- qt-faststart.c 2008-12-05 11:33:16 -0500 +++ qt-faststart.c 2008-12-05 11:45:19 -0500 @@ -1,9 +1,16 @@ /* - * qt-faststart.c, v0.1 + * qt-faststart.c, v0.2 + * * by Mike Melanson (melanson@pcisys.net) * This file is placed in the public domain. Use the program however you * see fit. * + * ChangLog + * 20081205 - Andrew Andkjar + * qt-faststart.c v0.2 - fixes for infinite loops caused by various malformed input files + * Pre 20081205 - Mike Melanson + * qt-faststart.c v0.1 - original version + * * This utility rearranges a Quicktime file such that the moov atom * is in front of the data, thus facilitating network streaming. * @@ -70,32 +77,32 @@ int main(int argc, char *argv[]) { - FILE *infile; - FILE *outfile; + FILE *infile = NULL; + FILE *outfile = NULL; unsigned char atom_bytes[ATOM_PREAMBLE_SIZE]; uint32_t atom_type = 0; uint64_t atom_size = 0; - uint64_t last_offset; - unsigned char *moov_atom; - unsigned char *ftyp_atom = 0; - uint64_t moov_atom_size; + uint64_t last_offset = 0; + unsigned char *moov_atom = NULL; + unsigned char *ftyp_atom = NULL; + uint64_t moov_atom_size = 0; uint64_t ftyp_atom_size = 0; - uint64_t i, j; - uint32_t offset_count; - uint64_t current_offset; + uint64_t i = 0, j = 0; + uint32_t offset_count = 0; + uint64_t current_offset = 0; uint64_t start_offset = 0; unsigned char copy_buffer[COPY_BUFFER_SIZE]; - int bytes_to_copy; + int bytes_to_copy = 0; if (argc != 3) { - printf ("Usage: qt-faststart \n"); + fprintf(stderr, "Usage: qt-faststart \n"); return 0; } infile = fopen(argv[1], "rb"); if (!infile) { perror(argv[1]); - return 1; + goto error_out; } /* traverse through the atoms in the file to make sure that 'moov' is @@ -107,6 +114,11 @@ atom_size = (uint32_t)BE_32(&atom_bytes[0]); atom_type = BE_32(&atom_bytes[4]); + if(atom_size < ATOM_PREAMBLE_SIZE) { + fprintf(stderr, "fatal error: bad atom size\n"); + goto error_out; + } + if ((atom_type != FREE_ATOM) && (atom_type != JUNK_ATOM) && (atom_type != MDAT_ATOM) && @@ -116,7 +128,7 @@ (atom_type != WIDE_ATOM) && (atom_type != PICT_ATOM) && (atom_type != FTYP_ATOM)) { - printf ("encountered non-QT top-level atom (is this a Quicktime file?)\n"); + fprintf(stderr, "encountered non-QT top-level atom (is this a Quicktime file?)\n"); break; } @@ -125,70 +137,82 @@ ftyp_atom_size = atom_size; ftyp_atom = malloc(ftyp_atom_size); if (!ftyp_atom) { - printf ("could not allocate 0x%llX byte for ftyp atom\n", - atom_size); - fclose(infile); - return 1; + fprintf(stderr, "could not allocate 0x%llX byte for ftyp atom\n", atom_size); + goto error_out; } - fseeko(infile, -ATOM_PREAMBLE_SIZE, SEEK_CUR); + + if(0 != fseeko(infile, -ATOM_PREAMBLE_SIZE, SEEK_CUR)) { + perror(argv[1]); + goto error_out; + } + if (fread(ftyp_atom, atom_size, 1, infile) != 1) { perror(argv[1]); - free(ftyp_atom); - fclose(infile); - return 1; + goto error_out; } start_offset = ftello(infile); + if(start_offset < 0) { + perror(argv[1]); + goto error_out; + } continue; } /* 64-bit special case */ if (atom_size == 1) { if (fread(atom_bytes, ATOM_PREAMBLE_SIZE, 1, infile) != 1) { - break; + break; } atom_size = BE_64(&atom_bytes[0]); - fseeko(infile, atom_size - ATOM_PREAMBLE_SIZE * 2, SEEK_CUR); + if(0 != fseeko(infile, atom_size - ATOM_PREAMBLE_SIZE * 2, SEEK_CUR)) { + perror(argv[1]); + goto error_out; + } } else { - fseeko(infile, atom_size - ATOM_PREAMBLE_SIZE, SEEK_CUR); + if(0 != fseeko(infile, atom_size - ATOM_PREAMBLE_SIZE, SEEK_CUR)) { + perror(argv[1]); + goto error_out; + } } } if (atom_type != MOOV_ATOM) { - printf ("last atom in file was not a moov atom\n"); - fclose(infile); - return 0; + fprintf(stderr, "last atom in file was not a moov atom\n"); + goto error_out; } /* moov atom was, in fact, the last atom in the chunk; load the whole * moov atom */ - fseeko(infile, -atom_size, SEEK_END); + if(0 != fseeko(infile, -atom_size, SEEK_END)) { + perror(argv[1]); + 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) { - printf ("could not allocate 0x%llX byte for moov atom\n", - atom_size); - fclose(infile); - return 1; + fprintf(stderr, "could not allocate 0x%llX byte for moov atom\n", atom_size); + goto error_out; } if (fread(moov_atom, atom_size, 1, infile) != 1) { perror(argv[1]); - free(moov_atom); - fclose(infile); - return 1; + goto error_out; } /* this utility does not support compressed atoms yet, so disqualify * files with compressed QT atoms */ if (BE_32(&moov_atom[12]) == CMOV_ATOM) { - printf ("this utility does not support compressed moov atoms yet\n"); - free(moov_atom); - fclose(infile); - return 1; + fprintf(stderr, "this utility does not support compressed moov atoms yet\n"); + goto error_out; } /* close; will be re-opened later */ fclose(infile); + infile = NULL; /* crawl through the moov chunk in search of stco or co64 atoms */ for (i = 4; i < moov_atom_size - 4; i++) { @@ -198,8 +222,7 @@ atom_size = BE_32(&moov_atom[i - 4]); if (i + atom_size - 4 > moov_atom_size) { printf (" bad atom size\n"); - free(moov_atom); - return 1; + goto error_out; } offset_count = BE_32(&moov_atom[i + 8]); for (j = 0; j < offset_count; j++) { @@ -216,8 +239,7 @@ atom_size = BE_32(&moov_atom[i - 4]); if (i + atom_size - 4 > moov_atom_size) { printf (" bad atom size\n"); - free(moov_atom); - return 1; + goto error_out; } offset_count = BE_32(&moov_atom[i + 8]); for (j = 0; j < offset_count; j++) { @@ -240,21 +262,21 @@ infile = fopen(argv[1], "rb"); if (!infile) { perror(argv[1]); - free(moov_atom); - return 1; + goto error_out; } if (start_offset > 0) { /* seek after ftyp atom */ - fseeko(infile, start_offset, SEEK_SET); + if(0 != fseeko(infile, start_offset, SEEK_SET)) { + perror(argv[1]); + goto error_out; + } last_offset -= start_offset; } outfile = fopen(argv[2], "wb"); if (!outfile) { perror(argv[2]); - fclose(outfile); - free(moov_atom); - return 1; + goto error_out; } /* dump the same ftyp atom */ @@ -295,17 +317,16 @@ fclose(infile); fclose(outfile); - free(moov_atom); - if (ftyp_atom_size > 0) - free(ftyp_atom); - + free(moov_atom); + free(ftyp_atom); return 0; error_out: - fclose(infile); - fclose(outfile); + if(infile) + fclose(infile); + if(outfile) + fclose(outfile); free(moov_atom); - if (ftyp_atom_size > 0) - free(ftyp_atom); + free(ftyp_atom); return 1; }