epg/epg.c

161 lines
3.5 KiB
C
Raw Normal View History

2011-06-01 23:34:35 +00:00
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <time.h>
#include <strings.h>
#include "lint.h"
2011-06-02 01:32:55 +00:00
#define _swap16(v) ((((v) >> 8) & 0xff) | (((v) & 0xff) << 8))
2011-06-01 23:34:35 +00:00
struct section_header *
read_section_header(struct epg *epg)
{
static struct section_header h;
if (epg->binsize - epg->offset < sizeof(h))
return NULL;
memcpy(&h, epg->bin + epg->offset, sizeof(h));
if (memcmp(h.magic, SECTION_HEADER_MAGIC, sizeof(h.magic)))
{
printf("Section header magic mismatch.\n");
return NULL;
}
2011-06-02 01:32:55 +00:00
h.total_length = _swap16(h.total_length);
h.service_id = _swap16(h.service_id);
h.transport_stream_id = _swap16(h.transport_stream_id);
h.original_network_id = _swap16(h.original_network_id);
h.u1.comp = _swap16(h.u1.comp);
2011-06-01 23:34:35 +00:00
epg->offset += sizeof(h);
return &h;
}
2011-06-02 01:32:55 +00:00
struct data *
read_data(struct epg *epg)
{
static struct data d;
if (epg->binsize - epg->offset < sizeof(d))
return NULL;
memcpy(&d, epg->bin + epg->offset, sizeof(d));
d.event_id = _swap16(d.event_id);
d.start_date = _swap16(d.start_date);
d.u1.comp = _swap16(d.u1.comp);
epg->offset += sizeof(d);
return &d;
}
struct descriptor_header *
read_descriptor_header(struct epg *epg)
{
static struct descriptor_header d;
if (epg->binsize - epg->offset < sizeof(d))
return NULL;
memcpy(&d, epg->bin + epg->offset, sizeof(d));
epg->offset += sizeof(d);
return &d;
}
2011-06-01 23:34:35 +00:00
#define DUMPINT(ss,xx) printf(" %30s: %d\n", #xx, ss->xx)
#define DUMPSTR(ss,xx) printf(" %30s: %s\n", #xx, ss->xx)
#define DUMPHEX(ss,xx) printf(" %30s: %s\n", #xx, \
hexstr(ss->xx, sizeof(ss->xx)))
2011-06-02 01:32:55 +00:00
char *
ctime_nl(time_t *tm)
{
static char buf[26];
strcpy(buf, ctime(tm));
*strchr(buf, '\n') = '\0';
return buf;
}
2011-06-01 23:34:35 +00:00
void
dump_section_header(struct section_header *h)
{
printf("Section header:\n");
DUMPINT(h, total_length);
DUMPINT(h, table_id);
2011-06-02 01:32:55 +00:00
DUMPINT(h, u1.u.syntax_indicator);
DUMPINT(h, u1.u.reserved);
DUMPINT(h, u1.u.length);
2011-06-01 23:34:35 +00:00
DUMPINT(h, service_id);
DUMPINT(h, reserved2);
DUMPINT(h, version_number);
DUMPINT(h, current_next_indicator);
DUMPINT(h, section_number);
DUMPINT(h, last_section_number);
DUMPINT(h, transport_stream_id);
DUMPINT(h, original_network_id);
DUMPINT(h, segment_last_section_number);
DUMPINT(h, last_table_id);
}
void
dump_data(struct data *d)
{
time_t tm;
printf("Data:\n");
DUMPINT(d, event_id);
tm = MJD_TO_UNIX(d->start_date);
2011-06-02 01:32:55 +00:00
printf(" %30s: %#x (%s)\n", "start_date",
d->start_date, ctime_nl(&tm));
printf(" %30s: %d:%02d:%02d\n", "start_time",
d->start_hour, d->start_min, d->start_sec);
printf(" %30s: %d:%02d:%02d\n", "duration",
d->dur_hour, d->dur_min, d->dur_sec);
DUMPINT(d, u1.u.running_status);
DUMPINT(d, u1.u.free_CA_mode);
DUMPINT(d, u1.u.descriptors_loop_length);
}
2011-06-01 23:34:35 +00:00
2011-06-02 01:32:55 +00:00
char *
descriptor_name(struct descriptor_header *d)
{
switch (d->tag)
{
case 77: return "short event";
case 80: return "component";
case 137: return "user defined";
case 118: return "content identifier";
case 74: return "linkage";
case 84: return "content";
case 95: return "private data specifier";
case 126: return "FTA content management";
default: return "Unknown";
}
return "Unknown";
}
2011-06-01 23:34:35 +00:00
2011-06-02 01:32:55 +00:00
void
dump_descriptor_header(struct descriptor_header *d)
{
printf("Descriptor header:\n");
printf(" %30s: %#x [%d] (%s)\n", "descriptor", d->tag, d->tag,
descriptor_name(d));
DUMPINT(d, len);
2011-06-01 23:34:35 +00:00
}