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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
|
/*
* CAT table parser and generator
* Copyright (C) 2010-2011 Unix Solutions Ltd.
*
* Released under MIT license.
* See LICENSE-MIT.txt for license terms.
*/
#include <stdio.h>
#include <unistd.h>
#include <netdb.h>
#include <stdlib.h>
#include <string.h>
#include "tsfuncs.h"
struct ts_cat *ts_cat_alloc() {
struct ts_cat *cat = calloc(1, sizeof(struct ts_cat));
cat->section_header = ts_section_data_alloc();
return cat;
}
void ts_cat_clear(struct ts_cat *cat) {
if (!cat)
return;
// save
struct ts_section_header *section_header = cat->section_header;
// free
FREE(cat->program_info);
// clear
ts_section_data_clear(section_header);
memset(cat, 0, sizeof(struct ts_cat));
// restore
cat->section_header = section_header;
}
void ts_cat_free(struct ts_cat **pcat) {
struct ts_cat *cat = *pcat;
if (cat) {
ts_section_data_free(&cat->section_header);
FREE(cat->program_info);
FREE(*pcat);
}
}
struct ts_cat *ts_cat_push_packet(struct ts_cat *cat, uint8_t *ts_packet) {
struct ts_header ts_header;
memset(&ts_header, 0, sizeof(struct ts_header));
if (ts_packet_header_parse(ts_packet, &ts_header)) {
// Received PUSI packet before table END, clear the table to start gathering new one
if (ts_header.pusi && cat->ts_header.pusi)
ts_cat_clear(cat);
if (!cat->ts_header.pusi)
cat->ts_header = ts_header;
}
if (ts_header.pusi) {
struct ts_section_header section_header;
memset(§ion_header, 0, sizeof(struct ts_section_header));
uint8_t *section_data = ts_section_header_parse(ts_packet, &cat->ts_header, §ion_header);
if (!section_data) {
memset(&cat->ts_header, 0, sizeof(struct ts_header));
goto OUT;
}
// table_id should be 0x01 (ca_map_section)
if (section_header.table_id != 0x01) {
memset(&cat->ts_header, 0, sizeof(struct ts_header));
goto OUT;
}
// Set correct section_header
ts_section_header_parse(ts_packet, &cat->ts_header, cat->section_header);
}
if (!cat->initialized) {
ts_section_add_packet(cat->section_header, &ts_header, ts_packet);
if (cat->section_header->initialized) {
if (!ts_cat_parse(cat))
goto ERROR;
}
}
OUT:
return cat;
ERROR:
ts_cat_clear(cat);
return cat;
}
int ts_cat_parse(struct ts_cat *cat) {
uint8_t *section_data = cat->section_header->data;
int section_len = cat->section_header->data_len;
/* Handle streams */
uint8_t *stream_data = section_data;
cat->program_info_size = section_len;
cat->program_info = malloc(cat->program_info_size);
memcpy(cat->program_info, stream_data, cat->program_info_size);
stream_data += cat->program_info_size;
if (!ts_crc32_section_check(cat->section_header, "CAT"))
return 0;
cat->initialized = 1;
return 1;
}
void ts_cat_generate(struct ts_cat *cat, uint8_t **ts_packets, int *num_packets) {
uint8_t *secdata = ts_section_data_alloc_section();
ts_section_header_generate(secdata, cat->section_header, 0);
int curpos = 8; // Compensate for the section header, frist data byte is at offset 8
memcpy(secdata + curpos, cat->program_info, cat->program_info_size);
curpos += cat->program_info_size;
cat->section_header->CRC = ts_section_data_calculate_crc(secdata, curpos);
curpos += 4; // CRC
ts_section_data_gen_ts_packets(&cat->ts_header, secdata, curpos, cat->section_header->pointer_field, ts_packets, num_packets);
FREE(secdata);
}
void ts_cat_regenerate_packets(struct ts_cat *cat) {
uint8_t *ts_packets;
int num_packets;
ts_cat_generate(cat, &ts_packets, &num_packets);
FREE(cat->section_header->packet_data);
cat->section_header->packet_data = ts_packets;
cat->section_header->num_packets = num_packets;
}
struct ts_cat *ts_cat_copy(struct ts_cat *cat) {
struct ts_cat *newcat = ts_cat_alloc();
int i;
for (i=0;i<cat->section_header->num_packets; i++) {
newcat = ts_cat_push_packet(newcat, cat->section_header->packet_data + (i * TS_PACKET_SIZE));
}
if (newcat->initialized) {
return newcat;
} else {
ts_LOGf("Error copying cat!\n");
ts_cat_free(&newcat);
return NULL;
}
}
void ts_cat_check_generator(struct ts_cat *cat) {
struct ts_cat *cat1 = ts_cat_copy(cat);
if (cat1) {
ts_compare_data("CAT (tspacket->struct)",
cat1->section_header->packet_data,
cat->section_header->packet_data,
cat->section_header->num_packets * TS_PACKET_SIZE);
ts_cat_free(&cat1);
}
uint8_t *ts_packets;
int num_packets;
ts_cat_generate(cat, &ts_packets, &num_packets);
if (num_packets != cat->section_header->num_packets) {
ts_LOGf("ERROR: num_packets:%d != sec->num_packets:%d\n", num_packets, cat->section_header->num_packets);
}
ts_compare_data("CAT (struct->tspacket)", cat->section_header->packet_data, ts_packets, num_packets * TS_PACKET_SIZE);
free(ts_packets);
}
void ts_cat_dump(struct ts_cat *cat) {
struct ts_section_header *sec = cat->section_header;
ts_section_dump(sec);
if (cat->program_info_size > 0) {
ts_LOGf(" * Descriptor dump:\n");
ts_descriptor_dump(cat->program_info, cat->program_info_size);
}
ts_cat_check_generator(cat);
}
int ts_cat_is_same(struct ts_cat *cat1, struct ts_cat *cat2) {
if (cat1 == cat2) return 1; // Same
if ((!cat1 && cat2) || (cat1 && !cat2)) return 0; // Not same (one is NULL)
return ts_section_is_same(cat1->section_header, cat2->section_header);
}
enum CA_system ts_get_CA_sys(uint16_t CA_id) {
if (CA_id >= 0x0100 && CA_id <= 0x01FF) return CA_SECA;
if (CA_id >= 0x0500 && CA_id <= 0x05FF) return CA_VIACCESS;
if (CA_id >= 0x0600 && CA_id <= 0x06FF) return CA_IRDETO;
if (CA_id >= 0x0900 && CA_id <= 0x09FF) return CA_VIDEOGUARD;
if (CA_id >= 0x0B00 && CA_id <= 0x0BFF) return CA_CONAX;
if (CA_id >= 0x0D00 && CA_id <= 0x0DFF) return CA_CRYPTOWORKS;
if (CA_id >= 0x1800 && CA_id <= 0x18FF) return CA_NAGRA;
switch (CA_id) {
case 0x4ABF: return CA_DGCRYPT;
case 0x4AE0: return CA_DRECRYPT;
case 0x4AE1: return CA_DRECRYPT;
case 0x5581: return CA_BULCRYPT;
case 0x4AEE: return CA_BULCRYPT;
case 0x5501: return CA_GRIFFIN;
case 0x5504: return CA_GRIFFIN;
case 0x5506: return CA_GRIFFIN;
case 0x5508: return CA_GRIFFIN;
case 0x5509: return CA_GRIFFIN;
case 0x550E: return CA_GRIFFIN;
case 0x5511: return CA_GRIFFIN;
}
return CA_UNKNOWN;
}
char * ts_get_CA_sys_txt(enum CA_system CA_sys) {
switch (CA_sys) {
case CA_SECA: return "SECA";
case CA_VIACCESS: return "VIACCESS";
case CA_IRDETO: return "IRDETO";
case CA_VIDEOGUARD: return "VIDEOGUARD";
case CA_CONAX: return "CONAX";
case CA_CRYPTOWORKS: return "CRYPTOWORKS";
case CA_NAGRA: return "NAGRA";
case CA_DRECRYPT: return "DRE-CRYPT";
case CA_BULCRYPT: return "BULCRYPT";
case CA_GRIFFIN: return "GRIFFIN";
case CA_DGCRYPT: return "DGCRYPT";
case CA_UNKNOWN: return "UNKNOWN";
}
return "UNKNOWN";
}
static int find_CA_descriptor(uint8_t *data, int data_len, enum CA_system req_CA_type, uint16_t *CA_id, uint16_t *CA_pid) {
while (data_len >= 2) {
uint8_t tag = data[0];
uint8_t this_length = data[1];
data += 2;
data_len -= 2;
if (tag == 9 && this_length >= 4) {
uint16_t CA_ID = (data[0] << 8) | data[1];
uint16_t CA_PID = ((data[2] & 0x1F) << 8) | data[3];
if (ts_get_CA_sys(CA_ID) == req_CA_type) {
*CA_id = CA_ID;
*CA_pid = CA_PID;
return 1;
}
}
data_len -= this_length;
data += this_length;
}
return 0;
}
int ts_get_emm_info(struct ts_cat *cat, enum CA_system req_CA_type, uint16_t *CA_id, uint16_t *CA_pid) {
return find_CA_descriptor(cat->program_info, cat->program_info_size, req_CA_type, CA_id, CA_pid);
}
int ts_get_ecm_info(struct ts_pmt *pmt, enum CA_system req_CA_type, uint16_t *CA_id, uint16_t *CA_pid) {
int i, result = find_CA_descriptor(pmt->program_info, pmt->program_info_size, req_CA_type, CA_id, CA_pid);
if (!result) {
for(i=0;i<pmt->streams_num;i++) {
struct ts_pmt_stream *stream = pmt->streams[i];
if (stream->ES_info) {
result = find_CA_descriptor(stream->ES_info, stream->ES_info_size, req_CA_type, CA_id, CA_pid);
if (result)
break;
}
}
}
return result;
}
static int find_CA_descriptor_by_caid(uint8_t *data, int data_len, uint16_t caid, uint16_t *CA_pid) {
while (data_len >= 2) {
uint8_t tag = data[0];
uint8_t this_length = data[1];
data += 2;
data_len -= 2;
if (tag == 9 && this_length >= 4) {
uint16_t CA_ID = (data[0] << 8) | data[1];
uint16_t CA_PID = ((data[2] & 0x1F) << 8) | data[3];
if (CA_ID == caid) {
*CA_pid = CA_PID;
return 1;
}
}
data_len -= this_length;
data += this_length;
}
return 0;
}
int ts_get_emm_info_by_caid(struct ts_cat *cat, uint16_t caid, uint16_t *ca_pid) {
return find_CA_descriptor_by_caid(cat->program_info, cat->program_info_size, caid, ca_pid);
}
int ts_get_ecm_info_by_caid(struct ts_pmt *pmt, uint16_t caid, uint16_t *ca_pid) {
int i, result = find_CA_descriptor_by_caid(pmt->program_info, pmt->program_info_size, caid, ca_pid);
if (!result) {
for(i=0;i<pmt->streams_num;i++) {
struct ts_pmt_stream *stream = pmt->streams[i];
if (stream->ES_info) {
result = find_CA_descriptor_by_caid(stream->ES_info, stream->ES_info_size, caid, ca_pid);
if (result)
break;
}
}
}
return result;
}
static int find_CA_descriptor_by_pid(uint8_t *data, int data_len, uint16_t *caid, uint16_t pid) {
while (data_len >= 2) {
uint8_t tag = data[0];
uint8_t this_length = data[1];
data += 2;
data_len -= 2;
if (tag == 9 && this_length >= 4) {
uint16_t CA_ID = (data[0] << 8) | data[1];
uint16_t CA_PID = ((data[2] & 0x1F) << 8) | data[3];
if (CA_PID == pid) {
*caid = CA_ID;
return 1;
}
}
data_len -= this_length;
data += this_length;
}
return 0;
}
int ts_get_emm_info_by_pid(struct ts_cat *cat, uint16_t *caid, uint16_t ca_pid) {
return find_CA_descriptor_by_pid(cat->program_info, cat->program_info_size, caid, ca_pid);
}
int ts_get_ecm_info_by_pid(struct ts_pmt *pmt, uint16_t *caid, uint16_t ca_pid) {
int i, result = find_CA_descriptor_by_pid(pmt->program_info, pmt->program_info_size, caid, ca_pid);
if (!result) {
for(i=0;i<pmt->streams_num;i++) {
struct ts_pmt_stream *stream = pmt->streams[i];
if (stream->ES_info) {
result = find_CA_descriptor_by_pid(stream->ES_info, stream->ES_info_size, caid, ca_pid);
if (result)
break;
}
}
}
return result;
}
|