Reworked the loop that finds the first ogg page in the input stream.
authorTilman Sauerbeck <tilman@code-monkey.de>
Thu, 17 Aug 2006 16:30:51 +0000 (18:30 +0200)
committerTilman Sauerbeck <tilman@code-monkey.de>
Wed, 23 Aug 2006 17:39:15 +0000 (19:39 +0200)
The loop also handles short reads gracefully now.

ext/vcedit.c

index fd89008587c452b23db169e16c5435cc1a88593f..46d68ee578231999db4d9f0b236fe0a234d44579 100644 (file)
@@ -264,8 +264,8 @@ vcedit_open (vcedit_state *state)
 {
        vcedit_error ret;
        char *buffer;
 {
        vcedit_error ret;
        char *buffer;
-       int bytes, i;
-       int chunks = 0;
+       size_t bytes, total = 0;
+       int i;
        ogg_packet *header;
        ogg_packet header_main, header_comments, header_codebooks;
        ogg_page og;
        ogg_packet *header;
        ogg_packet header_main, header_comments, header_codebooks;
        ogg_page og;
@@ -276,22 +276,21 @@ vcedit_open (vcedit_state *state)
 
        ogg_sync_init (&state->oy);
 
 
        ogg_sync_init (&state->oy);
 
-       while (1) {
-               buffer = ogg_sync_buffer (&state->oy, CHUNKSIZE);
-               bytes = fread (buffer, 1, CHUNKSIZE, state->in);
-
-               ogg_sync_wrote (&state->oy, bytes);
-
-               if (ogg_sync_pageout (&state->oy, &og) == 1)
-                       break;
-
+       do {
                /* Bail if we don't find data in the first 40 kB */
                /* Bail if we don't find data in the first 40 kB */
-               if (chunks++ >= 10) {
+               if (feof (state->in) || total >= (CHUNKSIZE * 10)) {
                        ogg_sync_clear (&state->oy);
 
                        return VCEDIT_ERR_INVAL;
                }
                        ogg_sync_clear (&state->oy);
 
                        return VCEDIT_ERR_INVAL;
                }
-       }
+
+               buffer = ogg_sync_buffer (&state->oy, CHUNKSIZE);
+
+               bytes = fread (buffer, 1, CHUNKSIZE, state->in);
+               total += bytes;
+
+               ogg_sync_wrote (&state->oy, bytes);
+       } while (ogg_sync_pageout (&state->oy, &og) != 1);
 
        state->serial = ogg_page_serialno (&og);
 
 
        state->serial = ogg_page_serialno (&og);