Commit 7292ebc6 authored by conrad's avatar conrad

oggz-basetime: handle out-of-memory, check fwrite

git-svn-id: http://svn.annodex.net/liboggz/trunk@3858 8158c8cd-e7e1-0310-9fa4-c5954c97daef
parent 8c57e2f2
...@@ -89,6 +89,8 @@ or_track_data_new (void) ...@@ -89,6 +89,8 @@ or_track_data_new (void)
OBTrackData * ort; OBTrackData * ort;
ort = malloc (sizeof (OBTrackData)); ort = malloc (sizeof (OBTrackData));
if (ort == NULL) return NULL;
ort->delta = -1; ort->delta = -1;
ort->nr_packets = 0; ort->nr_packets = 0;
...@@ -109,8 +111,14 @@ or_data_new (void) ...@@ -109,8 +111,14 @@ or_data_new (void)
OBData * ord; OBData * ord;
ord = malloc (sizeof (OBData)); ord = malloc (sizeof (OBData));
if (ord == NULL) return NULL;
ord->base_units = -1; ord->base_units = -1;
ord->tracks = oggz_table_new (); ord->tracks = oggz_table_new ();
if (ord->tracks == NULL) {
free (ord);
return NULL;
}
return ord; return ord;
} }
...@@ -129,6 +137,18 @@ or_data_delete (OBData * ord) ...@@ -129,6 +137,18 @@ or_data_delete (OBData * ord)
oggz_table_delete (ord->tracks); oggz_table_delete (ord->tracks);
} }
/********** checked_fwrite **********/
static void
checked_fwrite (const void *data, size_t size, size_t count, FILE *stream)
{
int n = fwrite (data, size, count, stream);
if ((size_t)n != count) {
perror ("write failed");
exit (1);
}
}
/********** Filter **********/ /********** Filter **********/
static int static int
...@@ -177,7 +197,8 @@ read_page (OGGZ * oggz, const ogg_page * og, long serialno, void * user_data) ...@@ -177,7 +197,8 @@ read_page (OGGZ * oggz, const ogg_page * og, long serialno, void * user_data)
int numheaders; int numheaders;
if (ogg_page_bos ((ogg_page *)og)) { if (ogg_page_bos ((ogg_page *)og)) {
ort = or_track_data_new (); if ((ort = or_track_data_new ()) == NULL)
return OGGZ_STOP_ERR;
oggz_table_insert (ord->tracks, serialno, ort); oggz_table_insert (ord->tracks, serialno, ort);
} else { } else {
ort = oggz_table_lookup (ord->tracks, serialno); ort = oggz_table_lookup (ord->tracks, serialno);
...@@ -224,8 +245,8 @@ read_page (OGGZ * oggz, const ogg_page * og, long serialno, void * user_data) ...@@ -224,8 +245,8 @@ read_page (OGGZ * oggz, const ogg_page * og, long serialno, void * user_data)
serialno, ort->nr_packets, ogg_page_granulepos ((ogg_page *)og)); serialno, ort->nr_packets, ogg_page_granulepos ((ogg_page *)og));
#endif #endif
fwrite (og->header, 1, og->header_len, stdout); checked_fwrite (og->header, 1, og->header_len, stdout);
fwrite (og->body, 1, og->body_len, stdout); checked_fwrite (og->body, 1, og->body_len, stdout);
return 0; return 0;
} }
...@@ -245,6 +266,7 @@ main (int argc, char ** argv) ...@@ -245,6 +266,7 @@ main (int argc, char ** argv)
char * progname = argv[0]; char * progname = argv[0];
OGGZ * oggz; OGGZ * oggz;
OBData * ord; OBData * ord;
int ret;
if (argc < 2) { if (argc < 2) {
usage (progname); usage (progname);
...@@ -262,6 +284,7 @@ main (int argc, char ** argv) ...@@ -262,6 +284,7 @@ main (int argc, char ** argv)
} }
ord = or_data_new (); ord = or_data_new ();
if (ord == NULL) goto oom;
if ((oggz = oggz_open ((char *)argv[1], OGGZ_READ | OGGZ_AUTO)) == NULL) { if ((oggz = oggz_open ((char *)argv[1], OGGZ_READ | OGGZ_AUTO)) == NULL) {
printf ("unable to open file %s\n", argv[1]); printf ("unable to open file %s\n", argv[1]);
...@@ -270,11 +293,17 @@ main (int argc, char ** argv) ...@@ -270,11 +293,17 @@ main (int argc, char ** argv)
oggz_set_read_page (oggz, -1, read_page, ord); oggz_set_read_page (oggz, -1, read_page, ord);
oggz_run (oggz); ret = oggz_run (oggz);
oggz_close (oggz); oggz_close (oggz);
or_data_delete (ord); or_data_delete (ord);
if (ret == OGGZ_ERR_STOP_ERR) goto oom;
exit (0); exit (0);
oom:
fprintf (stderr, "%s: out of memory\n", progname);
exit (1);
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment