Reworked the loop that finds the first ogg page in the input stream.
[ruby-vorbistagger.git] / ext / vcedit.c
index 832f417b2b30519cba6661b3dfb063c52a43f925..46d68ee578231999db4d9f0b236fe0a234d44579 100644 (file)
@@ -40,8 +40,6 @@ struct vcedit_state_St {
        vorbis_comment vc;
        vorbis_info vi;
 
-       char filename[PATH_MAX];
-
        FILE *in;
        bool opened;
        long serial;
@@ -53,6 +51,8 @@ struct vcedit_state_St {
        int prevW;
        int extrapage;
        int eosin;
+
+       char filename[0];
 };
 
 static void
@@ -71,10 +71,12 @@ vcedit_state_free (vcedit_state *state)
 }
 
 static bool
-vcedit_state_init (vcedit_state *state)
+vcedit_state_init (vcedit_state *state, const char *filename)
 {
        state->refcount = 1;
 
+       strcpy (state->filename, filename);
+
        return true;
 }
 
@@ -82,21 +84,23 @@ vcedit_state *
 vcedit_state_new (const char *filename)
 {
        vcedit_state *state;
+       size_t len;
 
-       state = malloc (sizeof (vcedit_state));
+       len = strlen (filename);
+       if (len > PATH_MAX)
+               return NULL;
+
+       state = malloc (sizeof (vcedit_state) + len + 1);
        if (!state)
                return NULL;
 
        memset (state, 0, sizeof (vcedit_state));
 
-       if (!vcedit_state_init (state)) {
+       if (!vcedit_state_init (state, filename)) {
                vcedit_state_free (state);
                return NULL;
        }
 
-       snprintf (state->filename, sizeof (state->filename),
-                 "%s", filename);
-
        return state;
 }
 
@@ -260,8 +264,8 @@ vcedit_open (vcedit_state *state)
 {
        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;
@@ -272,22 +276,21 @@ vcedit_open (vcedit_state *state)
 
        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 */
-               if (chunks++ >= 10) {
+               if (feof (state->in) || total >= (CHUNKSIZE * 10)) {
                        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);