oggzdump.c 16.6 KB
Newer Older
andre's avatar
andre committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/*
   Copyright (C) 2003 Commonwealth Scientific and Industrial Research
   Organisation (CSIRO) Australia

   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions
   are met:

   - Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.

   - Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.

   - Neither the name of CSIRO Australia nor the names of its
   contributors may be used to endorse or promote products derived from
   this software without specific prior written permission.

   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
   PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE ORGANISATION OR
   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

33
#include "config.h"
andre's avatar
andre committed
34 35 36

#include <stdio.h>
#include <stdlib.h>
conrad's avatar
conrad committed
37
#include <string.h>
andre's avatar
andre committed
38
#include <ctype.h>
39 40 41 42 43

#ifdef HAVE_INTTYPES_H
#  include <inttypes.h>
#else
#  define PRId64 "I64d"
44
#endif
45

conrad's avatar
conrad committed
46 47
#include <getopt.h>
#include <errno.h>
andre's avatar
andre committed
48 49

#include <oggz/oggz.h>
conrad's avatar
conrad committed
50
#include "oggz_tools.h"
andre's avatar
andre committed
51

52
/*#define DEBUG*/
conrad's avatar
conrad committed
53

andre's avatar
andre committed
54 55 56
#undef MIN
#define MIN(a,b) (((a)<(b))?(a):(b))

57
typedef struct {
58
  OggzTable * serialno_table;
59 60
  OggzTable * content_types_table;
  OggzReadPacket read_packet;
61 62 63 64 65 66
  int dump_bits;
  int dump_char;
  int hide_offset;
  int hide_serialno;
  int hide_granulepos;
  int hide_packetno;
67 68
} ODData;

conrad's avatar
conrad committed
69 70 71 72 73 74 75 76
static char * progname;
static FILE * outfile = NULL;
static int truth = 1;

static void
usage (char * progname)
{
  printf ("Usage: %s [options] filename\n", progname);
77 78 79 80 81 82
  printf ("Hexdump packets of an Ogg file, or revert an Ogg file from such a hexdump\n");
  printf ("\nDump format options\n");
  printf ("  -b, --binary           Generate a binary dump of each packet\n");
  printf ("  -x, --hexadecimal      Generate a hexadecimal dump of each packet\n");
  printf ("\nFiltering options\n");
  printf ("  -n, --new              Only dump the first packet of each logical bitstream\n");
83 84 85 86
  printf ("  -c content-type, --content-type content-type\n");
  printf ("                         Dump only the logical bitstreams for a specified\n");
  printf ("                         content-type. The following codecs are currently\n");
  printf ("                         detected: theora, vorbis, speex, cmml\n");
87 88 89 90 91 92 93 94 95 96 97 98 99
  printf ("  -s serialno, --serialno serialno\n");
  printf ("                         Dump only the logical bitstream with specified serialno\n");
  printf ("  -O, --hide-offset      Hide the byte offset of each packet\n");
  printf ("  -S, --hide-serialno    Hide the serialno field of each packet\n");
  printf ("  -G, --hide-granulepos  Hide the granulepos field of each packet\n");
  printf ("  -P, --hide-packetno    Hide the packetno field of each packet\n");
  printf ("\nMode options\n");
  printf ("  -r, --revert           Revert an oggzdump. Generates an Ogg bitstream\n");
  printf ("                         as prescribed in the input oggzdump\n");
  printf ("\nMiscellaneous options\n");
  printf ("  -o filename, --output filename\n");
  printf ("                         Specify output filename\n");
  printf ("  -h, --help             Display this help and exit\n");
conrad's avatar
conrad committed
100
  printf ("  -v, --version          Output version information and exit\n");
101
  printf ("\n");
conrad's avatar
conrad committed
102
  printf ("Please report bugs to <ogg-dev@xiph.org>\n");
conrad's avatar
conrad committed
103 104
}

105 106 107 108 109 110
static ODData *
oddata_new ()
{
  ODData * oddata = malloc (sizeof (ODData));
  memset (oddata, 0, sizeof (ODData));

111
  oddata->serialno_table = oggz_table_new ();
112 113
  oddata->content_types_table = oggz_table_new ();

114 115 116 117 118 119 120 121
  oddata->dump_bits = 0;
  oddata->dump_char = 1;

  oddata->hide_offset = 0;
  oddata->hide_serialno = 0;
  oddata->hide_granulepos = 0;
  oddata->hide_packetno = 0;

122 123 124 125 126 127
  return oddata;
}

static void
oddata_delete (ODData * oddata)
{
128
  oggz_table_delete (oddata->serialno_table);
129 130 131 132 133
  oggz_table_delete (oddata->content_types_table);

  free (oddata);
}

conrad's avatar
conrad committed
134 135 136 137 138 139
static void
dump_char_line (unsigned char * buf, long n)
{
  int i;

  fprintf (outfile, "  ");
140

conrad's avatar
conrad committed
141 142 143 144 145 146 147 148
  for (i = 0; i < n; i++) {
    if (isgraph(buf[i])) fprintf (outfile, "%c", buf[i]);
    else if (isspace(buf[i])) fprintf (outfile, " ");
    else fprintf (outfile, ".");
  }
}

static void
149
hex_dump (unsigned char * buf, long n, int dump_char)
conrad's avatar
conrad committed
150
{
andre's avatar
andre committed
151 152 153 154 155 156 157 158
  int i;
  long remaining = n, count = 0;
  long rowlen;

  while (remaining > 0) {
    rowlen = MIN (remaining, 16);

    if (n > 0xffffff)
conrad's avatar
conrad committed
159
      fprintf (outfile, "%08lx:", count);
andre's avatar
andre committed
160
    else if (n > 0xffff)
conrad's avatar
conrad committed
161
      fprintf (outfile, "  %06lx:", count);
andre's avatar
andre committed
162
    else
conrad's avatar
conrad committed
163
      fprintf (outfile, "    %04lx:", count);
andre's avatar
andre committed
164 165

    for (i = 0; i < rowlen; i++) {
conrad's avatar
conrad committed
166 167
      if (!(i%2)) fprintf (outfile, " ");
      fprintf (outfile, "%02x", buf[i]);
andre's avatar
andre committed
168 169 170
    }

    for (; i < 16; i++) {
conrad's avatar
conrad committed
171 172
      if (!(i%2)) fprintf (outfile, " ");
      fprintf (outfile, "  ");
andre's avatar
andre committed
173 174
    }

conrad's avatar
conrad committed
175 176
    if (dump_char)
      dump_char_line (buf, rowlen);
andre's avatar
andre committed
177

conrad's avatar
conrad committed
178
    fprintf(outfile, "\n");
andre's avatar
andre committed
179 180 181 182 183 184 185

    remaining -= rowlen;
    buf += rowlen;
    count += rowlen;
  }
}

conrad's avatar
conrad committed
186
static void
187
bin_dump (unsigned char * buf, long n, int dump_char)
conrad's avatar
conrad committed
188
{
andre's avatar
andre committed
189 190 191 192 193 194 195 196
  int i, j;
  long remaining = n, count = 0;
  long rowlen;

  while (remaining > 0) {
    rowlen = MIN (remaining, 6);

    if (n > 0xffffff)
conrad's avatar
conrad committed
197
      fprintf (outfile, "%08lx:", count);
andre's avatar
andre committed
198
    else if (n > 0xffff)
conrad's avatar
conrad committed
199
      fprintf (outfile, "  %06lx:", count);
andre's avatar
andre committed
200
    else
conrad's avatar
conrad committed
201
      fprintf (outfile, "    %04lx:", count);
andre's avatar
andre committed
202 203

    for (i = 0; i < rowlen; i++) {
conrad's avatar
conrad committed
204
      fprintf (outfile, " ");
andre's avatar
andre committed
205 206 207 208 209
#ifdef WORDS_BIGENDIAN
      for (j = 0; j < 8; j++)
#else
      for (j = 7; j >= 0; j--)
#endif
conrad's avatar
conrad committed
210
	fprintf (outfile, "%c", (buf[i]&(1<<j)) ? '1' : '0');
andre's avatar
andre committed
211 212 213
    }

    for (; i < 6; i++) {
conrad's avatar
conrad committed
214 215
      if (!(i%2)) fprintf (outfile, " ");
      fprintf (outfile, "         ");
andre's avatar
andre committed
216 217
    }

conrad's avatar
conrad committed
218 219
    if (dump_char)
      dump_char_line (buf, rowlen);
andre's avatar
andre committed
220 221 222 223 224 225 226 227 228

    printf("\n");

    remaining -= rowlen;
    buf += rowlen;
    count += rowlen;
  }
}

229 230 231 232 233 234 235 236
/* FIXME: on Mac OS X, off_t is 64-bits.  Obviously we want a nicer
 * way to do it than this, but a quick fix is a good fix */
#ifdef __APPLE__
#  define PRI_off_t "q"
#else
#  define PRI_off_t "l"
#endif

andre's avatar
andre committed
237 238 239
static int
read_packet (OGGZ * oggz, ogg_packet * op, long serialno, void * user_data)
{
240
  ODData * oddata = (ODData *) user_data;
241 242
  ogg_int64_t units;
  double time_offset;
243
  int granuleshift;
244

245
  if (oddata->hide_offset) {
246 247
    fprintf (outfile, "oOo");
  } else {
248 249 250 251 252 253 254
    units = oggz_tell_units (oggz);
    if (units == -1) {
      fprintf (outfile, "%08" PRI_off_t "x", oggz_tell (oggz));
    } else {
      time_offset = (double)units / 1000.0;
      ot_fprint_time (outfile, time_offset);
    }
255 256
  }

257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
  fprintf (outfile, ": serialno %010ld, ",
	   oddata->hide_serialno ? -1 : serialno);

  if (oddata->hide_granulepos) {
    fprintf (outfile, "granulepos gGg,");
  } else {
    granuleshift = oggz_get_granuleshift (oggz, serialno);
    if (granuleshift < 1) {
      fprintf (outfile, "granulepos %" PRId64 ",", op->granulepos);
    } else {
      ogg_int64_t iframe, pframe;
      iframe = op->granulepos >> granuleshift;
      pframe = op->granulepos - (iframe << granuleshift);

      fprintf (outfile, "granulepos %" PRId64 "|%" PRId64 ",",
	       iframe, pframe);
    }
  }

  fprintf (outfile, " packetno %" PRId64,
277
	   oddata->hide_packetno ? -1 : op->packetno);
andre's avatar
andre committed
278 279

  if (op->b_o_s) {
conrad's avatar
conrad committed
280
    fprintf (outfile, " *** bos");
andre's avatar
andre committed
281 282 283
  }

  if (op->e_o_s) {
conrad's avatar
conrad committed
284
    fprintf (outfile, " *** eos");
andre's avatar
andre committed
285 286
  }

287 288 289
  fprintf (outfile, ": ");
  ot_fprint_bytes (outfile, op->bytes);
  fputc ('\n', outfile);
andre's avatar
andre committed
290

291 292
  if (oddata->dump_bits) {
    bin_dump (op->packet, op->bytes, oddata->dump_char);
conrad's avatar
conrad committed
293
  } else {
294
    hex_dump (op->packet, op->bytes, oddata->dump_char);
conrad's avatar
conrad committed
295
  }
andre's avatar
andre committed
296

conrad's avatar
conrad committed
297
  fprintf (outfile, "\n");
andre's avatar
andre committed
298 299 300 301

  return 0;
}

302 303 304 305 306 307 308 309 310 311 312 313 314
static int
filter_page (OGGZ * oggz, const ogg_page * og, long serialno, void * user_data)
{
  ODData * oddata = (ODData *) user_data;
  const char * ident;
  int i, n;

  if (ogg_page_bos ((ogg_page *)og)) {
    ident = ot_page_identify (og, NULL);
    n = oggz_table_size (oddata->content_types_table);
    for (i = 0; i < n; i++) {
      char * c = oggz_table_nth (oddata->content_types_table, i, NULL);
      if (strcasecmp (c, ident) == 0) {
315
	oggz_set_read_callback (oggz, serialno, oddata->read_packet, oddata);
316 317 318 319 320 321 322
      }
    }
  }

  return 0;
}

andre's avatar
andre committed
323
static int
conrad's avatar
conrad committed
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
ignore_packet (OGGZ * oggz, ogg_packet * op, long serialno, void * user_data)
{
  return -1;
}

static int
read_new_packet (OGGZ * oggz, ogg_packet * op, long serialno, void * user_data)
{
  oggz_set_read_callback (oggz, serialno, ignore_packet, NULL);
  read_packet (oggz, op, serialno, user_data);

  return -1;
}

static void
revert_file (char * infilename)
andre's avatar
andre committed
340
{
conrad's avatar
conrad committed
341 342 343
  OGGZ * oggz;
  FILE * infile;
  char line[80];
344
  int hh, mm, ss;
conrad's avatar
conrad committed
345 346
  unsigned int offset;
  long current_serialno = -1, serialno;
347 348
  ogg_int64_t granulepos, iframe, pframe, packetno;
  int is_packetinfo = 0;
conrad's avatar
conrad committed
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
  int bos = 0, eos = 0;

  int line_offset = 0, consumed = 0;

  unsigned char * packet = NULL;
  long max_bytes = 0;

  unsigned char buf[1024];
  ogg_packet op;
  int flush = 1;
  long n;
  char c;

  if (strcmp (infilename, "-") == 0) {
    infile = stdin;
  } else {
365
    infile = fopen (infilename, "rb");
conrad's avatar
conrad committed
366 367
  }

368
  oggz = oggz_new (OGGZ_WRITE|OGGZ_NONSTRICT|OGGZ_AUTO);
conrad's avatar
conrad committed
369 370

  while (fgets (line, 80, infile)) {
371 372 373 374 375 376
    line_offset = 0;

    /* Skip time offsets, OR ensure line_offset is 0 */
    if (sscanf (line, "%2d:%2d:%2d.%n", &hh, &mm, &ss, &line_offset) < 3)
      line_offset = 0;

377
    is_packetinfo = 0;
378
    if (sscanf (&line[line_offset], "%x: serialno %ld, granulepos %lld, packetno %lld%n",
conrad's avatar
conrad committed
379 380
		&offset, &serialno, &granulepos, &packetno,
		&line_offset) >= 4) {
381 382 383 384 385 386 387 388 389 390
      is_packetinfo = 1;
    } else {
      if (sscanf (&line[line_offset], "%x: serialno %ld, granulepos %lld|%lld, packetno %lld%n",
		  &offset, &serialno, &iframe, &pframe, &packetno,
		  &line_offset) >= 5) {
	int granuleshift = oggz_get_granuleshift (oggz, serialno);
	is_packetinfo = 1;
	granulepos = (iframe<<granuleshift)+pframe;
      }
    }
conrad's avatar
conrad committed
391

392
    if (is_packetinfo) {
conrad's avatar
conrad committed
393 394 395 396
      /* flush any existing packets */
      if (current_serialno != -1) {
	int ret;

397
#ifdef DEBUG
398 399 400
	printf ("feeding packet (%010ld) %ld bytes %s\n",
		current_serialno, op.bytes,
		op.b_o_s ? "bos" : "not bos");
conrad's avatar
conrad committed
401 402 403 404 405 406
#endif
	if ((ret = oggz_write_feed (oggz, &op, current_serialno, flush, NULL)) != 0) {
	  fprintf (stderr, "%s: oggz_write_feed error %d\n", progname, ret);
	}

	while ((n = oggz_write_output (oggz, buf, 1024)) > 0) {
conrad's avatar
conrad committed
407
	  fwrite (buf, 1, n, outfile);
conrad's avatar
conrad committed
408 409
	}
      }
410

conrad's avatar
conrad committed
411 412
      /* Start new packet */
      bos = 0; eos = 0;
413 414 415 416 417 418 419 420 421 422
      while ((c=line[line_offset++]) && c != '*');
      if (c == '*') {
	if (!strncmp (&line[line_offset], "** bos", 6)) {
	  bos = 1;
	  line_offset += 6;
	}
	if (!strncmp (&line[line_offset], "** eos", 6)) {
	  eos = 1;
	  line_offset += 6;
	}
conrad's avatar
conrad committed
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
      }

      current_serialno = serialno;

      op.packet = packet;
      op.bytes = 0;
      op.b_o_s = bos;
      op.e_o_s = eos;
      op.granulepos = granulepos;
      op.packetno = packetno;

    } else {
      int nread = 0;
      unsigned int val = 0;
      unsigned int offset;

      if (current_serialno != -1 &&
	  sscanf (line, "%x:%n", &offset, &line_offset) >= 1) {
	while (nread < 16 &&
	       (sscanf (&line[line_offset], "%2x%n", &val, &consumed) > 0)) {
	  op.bytes++;
	  if (op.bytes > max_bytes) {
	    unsigned char * new_packet;
	    size_t new_size;

	    if (max_bytes == 0) {
	      new_size = 128;
	    } else {
	      new_size = max_bytes * 2;
	    }

	    new_packet =
	      (unsigned char *) realloc ((void *)packet, new_size);
	    if (new_packet == NULL) {
	      fprintf (stderr,
		       "%s: error allocating memory for packet data\n",
		       progname);
	      exit (1);
	    } else {
462
	      max_bytes = (long)new_size;
conrad's avatar
conrad committed
463 464 465 466
	      packet = new_packet;
	      op.packet = packet;
	    }
	  }
467

conrad's avatar
conrad committed
468 469 470 471 472 473 474
	  packet[op.bytes-1] = (unsigned char) val;

	  line_offset += consumed;
	  nread++;
	}
      }
    }
andre's avatar
andre committed
475 476
  }

conrad's avatar
conrad committed
477
  fclose (infile);
andre's avatar
andre committed
478 479 480 481 482
}

int
main (int argc, char ** argv)
{
conrad's avatar
conrad committed
483 484 485
  int show_version = 0;
  int show_help = 0;

486
  ODData * oddata;
andre's avatar
andre committed
487
  OGGZ * oggz;
conrad's avatar
conrad committed
488 489 490 491
  char * infilename = NULL, * outfilename = NULL;
  int revert = 0;
  long serialno;
  int i, size;
andre's avatar
andre committed
492 493
  long n;

494 495 496
  int filter_serialnos = 0;
  int filter_content_types = 0;

conrad's avatar
conrad committed
497 498
  ot_init ();

conrad's avatar
conrad committed
499 500
  progname = argv[0];

andre's avatar
andre committed
501
  if (argc < 2) {
502 503
    usage (progname);
    return (1);
conrad's avatar
conrad committed
504 505
  }

506 507 508 509
  oddata = oddata_new ();

  oddata->read_packet = read_packet;

conrad's avatar
conrad committed
510
  while (1) {
511
    char * optstring = "hvbxnro:s:c:OSGP";
conrad's avatar
conrad committed
512 513 514 515

#ifdef HAVE_GETOPT_LONG
    static struct option long_options[] = {
      {"help", no_argument, 0, 'h'},
conrad's avatar
conrad committed
516
      {"version", no_argument, 0, 'v'},
conrad's avatar
conrad committed
517 518 519 520 521 522
      {"binary", no_argument, 0, 'b'},
      {"hexadecimal", no_argument, 0, 'x'},
      {"new", no_argument, 0, 'n'},
      {"revert", no_argument, 0, 'r'},
      {"output", required_argument, 0, 'o'},
      {"serialno", required_argument, 0, 's'},
523
      {"content-type", required_argument, 0, 'c'},
conrad's avatar
conrad committed
524 525 526 527 528 529 530 531 532 533 534 535 536 537
      {"hide-offset", no_argument, 0, 'O'},
      {"hide-serialno", no_argument, 0, 'S'},
      {"hide-granulepos", no_argument, 0, 'G'},
      {"hide-packetno", no_argument, 0, 'P'},
      {0,0,0,0}
    };

    i = getopt_long(argc, argv, optstring, long_options, NULL);
#else
    i = getopt (argc, argv, optstring);
#endif
    if (i == -1) break;
    if (i == ':') {
      usage (progname);
conrad's avatar
conrad committed
538
      goto exit_err;
conrad's avatar
conrad committed
539 540 541 542
    }

    switch (i) {
    case 'h': /* help */
conrad's avatar
conrad committed
543 544 545 546
      show_help = 1;
      break;
    case 'v': /* version */
      show_version = 1;
conrad's avatar
conrad committed
547 548
      break;
    case 'b': /* binary */
549
      oddata->dump_bits = 1;
conrad's avatar
conrad committed
550 551
      break;
    case 'n': /* new */
552
      oddata->read_packet = read_new_packet;
conrad's avatar
conrad committed
553 554 555 556 557 558 559 560
      break;
    case 'o': /* output */
      outfilename = optarg;
      break;
    case 'r': /* revert */
      revert = 1;
      break;
    case 's': /* serialno */
561
      filter_serialnos = 1;
conrad's avatar
conrad committed
562
      serialno = atol (optarg);
563
      oggz_table_insert (oddata->serialno_table, serialno, &truth);
conrad's avatar
conrad committed
564
      break;
565 566 567 568 569
    case 'c': /* content-type */
      filter_content_types = 1;
      n = (long)oggz_table_size (oddata->content_types_table);
      oggz_table_insert (oddata->content_types_table, (long)n, optarg);
      break;
conrad's avatar
conrad committed
570
    case 'O': /* hide offset */
571
      oddata->hide_offset = 1;
conrad's avatar
conrad committed
572 573
      break;
    case 'S': /* hide serialno */
574
      oddata->hide_serialno = 1;
conrad's avatar
conrad committed
575 576
      break;
    case 'G': /* hide granulepos */
577
      oddata->hide_granulepos = 1;
conrad's avatar
conrad committed
578 579
      break;
    case 'P': /* hide packetno */
580
      oddata->hide_packetno = 1;
conrad's avatar
conrad committed
581 582 583 584
      break;
    default:
      break;
    }
andre's avatar
andre committed
585 586
  }

conrad's avatar
conrad committed
587 588 589 590 591 592 593 594 595 596 597 598
  if (show_version) {
    printf ("%s version " VERSION "\n", progname);
  }

  if (show_help) {
    usage (progname);
  }

  if (show_version || show_help) {
    goto exit_ok;
  }

conrad's avatar
conrad committed
599 600
  if (optind >= argc) {
    usage (progname);
conrad's avatar
conrad committed
601
    goto exit_err;
andre's avatar
andre committed
602 603
  }

conrad's avatar
conrad committed
604 605 606 607 608
  infilename = argv[optind++];

  if (outfilename == NULL) {
    outfile = stdout;
  } else {
609
    outfile = fopen (outfilename, "wb");
conrad's avatar
conrad committed
610 611 612
    if (outfile == NULL) {
      fprintf (stderr, "%s: unable to open output file %s\n",
	       progname, outfilename);
conrad's avatar
conrad committed
613
      goto exit_err;
conrad's avatar
conrad committed
614 615 616 617
    }
  }

  if (revert) {
618
    if (oddata->dump_bits) {
conrad's avatar
conrad committed
619
      fprintf (stderr, "%s: Revert of binary dump not supported\n", progname);
conrad's avatar
conrad committed
620
      goto exit_err;
conrad's avatar
conrad committed
621 622 623 624 625 626 627
    }

    revert_file (infilename);
  } else {
    errno = 0;

    if (strcmp (infilename, "-") == 0) {
628
      oggz = oggz_open_stdio (stdin, OGGZ_READ|OGGZ_AUTO);
conrad's avatar
conrad committed
629
    } else {
630
      oggz = oggz_open (infilename, OGGZ_READ|OGGZ_AUTO);
conrad's avatar
conrad committed
631
    }
andre's avatar
andre committed
632

conrad's avatar
conrad committed
633 634 635 636 637 638 639 640
    if (oggz == NULL) {
      if (errno == 0) {
	fprintf (stderr, "%s: %s: OGGZ error opening input file\n",
		 progname, infilename);
      } else {
	fprintf (stderr, "%s: %s: %s\n",
		 progname, infilename, strerror (errno));
      }
conrad's avatar
conrad committed
641
      goto exit_err;
conrad's avatar
conrad committed
642 643
    }

644
    if (!filter_serialnos && !filter_content_types) {
645
      oggz_set_read_callback (oggz, -1, oddata->read_packet, oddata);
conrad's avatar
conrad committed
646
    } else {
647
      if (filter_serialnos) {
648
	size = oggz_table_size (oddata->serialno_table);
649
	for (i = 0; i < size; i++) {
650 651
	  oggz_table_nth (oddata->serialno_table, i, &serialno);
	  oggz_set_read_callback (oggz, serialno, oddata->read_packet, oddata);
652 653 654 655 656
	}
      }
      
      if (filter_content_types) {
	oggz_set_read_page (oggz, -1, filter_page, oddata);
conrad's avatar
conrad committed
657 658
      }
    }
659

conrad's avatar
conrad committed
660
    while ((n = oggz_read (oggz, 1024)) > 0);
661

conrad's avatar
conrad committed
662 663
    oggz_close (oggz);
  }
664

conrad's avatar
conrad committed
665
 exit_ok:
666
  oddata_delete (oddata);
conrad's avatar
conrad committed
667 668 669
  exit (0);

 exit_err:
670
  oddata_delete (oddata);
conrad's avatar
conrad committed
671
  exit (1);
andre's avatar
andre committed
672
}