libtsfuncs is a library for mpeg PSI parsing and generation. https://georgi.unixsol.org/programs/libtsfuncs/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cat.c 10KB

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