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.

tsfuncs_cat.c 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <netdb.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include "tsfuncs.h"
  7. struct ts_cat *ts_cat_alloc() {
  8. struct ts_cat *cat = calloc(1, sizeof(struct ts_cat));
  9. cat->section_header = ts_section_data_alloc();
  10. return cat;
  11. }
  12. void ts_cat_free(struct ts_cat **pcat) {
  13. struct ts_cat *cat = *pcat;
  14. if (cat) {
  15. ts_section_data_free(&cat->section_header);
  16. FREE(cat->program_info);
  17. FREE(*pcat);
  18. }
  19. }
  20. static struct ts_cat *ts_cat_reset(struct ts_cat *cat) {
  21. struct ts_cat *newcat = ts_cat_alloc();
  22. ts_cat_free(&cat);
  23. return newcat;
  24. }
  25. struct ts_cat *ts_cat_push_packet(struct ts_cat *cat, uint8_t *ts_packet) {
  26. struct ts_header ts_header;
  27. memset(&ts_header, 0, sizeof(struct ts_header));
  28. if (ts_packet_header_parse(ts_packet, &ts_header)) {
  29. if (!cat->ts_header.pusi)
  30. cat->ts_header = ts_header;
  31. }
  32. if (ts_header.pusi) {
  33. struct ts_section_header section_header;
  34. memset(&section_header, 0, sizeof(struct ts_section_header));
  35. uint8_t *section_data = ts_section_header_parse(ts_packet, &cat->ts_header, &section_header);
  36. if (!section_data) {
  37. memset(&cat->ts_header, 0, sizeof(struct ts_header));
  38. goto OUT;
  39. }
  40. // table_id should be 0x01 (ca_map_section)
  41. if (section_header.table_id != 0x01) {
  42. memset(&cat->ts_header, 0, sizeof(struct ts_header));
  43. goto OUT;
  44. }
  45. // Set correct section_header
  46. ts_section_header_parse(ts_packet, &cat->ts_header, cat->section_header);
  47. }
  48. if (!cat->initialized) {
  49. ts_section_add_packet(cat->section_header, &ts_header, ts_packet);
  50. if (cat->section_header->initialized) {
  51. if (!ts_cat_parse(cat))
  52. goto ERROR;
  53. }
  54. }
  55. OUT:
  56. return cat;
  57. ERROR:
  58. return ts_cat_reset(cat);
  59. }
  60. int ts_cat_parse(struct ts_cat *cat) {
  61. uint8_t *section_data = cat->section_header->data;
  62. int section_len = cat->section_header->data_len;
  63. /* Handle streams */
  64. uint8_t *stream_data = section_data;
  65. cat->program_info_size = section_len;
  66. cat->program_info = malloc(cat->program_info_size);
  67. memcpy(cat->program_info, stream_data, cat->program_info_size);
  68. stream_data += cat->program_info_size;
  69. if (!ts_crc32_section_check(cat->section_header, "CAT"))
  70. return 0;
  71. cat->initialized = 1;
  72. return 1;
  73. }
  74. void ts_cat_generate(struct ts_cat *cat, uint8_t **ts_packets, int *num_packets) {
  75. uint8_t *secdata = ts_section_data_alloc_section();
  76. ts_section_header_generate(secdata, cat->section_header, 0);
  77. int curpos = 8; // Compensate for the section header, frist data byte is at offset 8
  78. memcpy(secdata + curpos, cat->program_info, cat->program_info_size);
  79. curpos += cat->program_info_size;
  80. cat->section_header->CRC = ts_section_data_calculate_crc(secdata, curpos);
  81. curpos += 4; // CRC
  82. ts_section_data_gen_ts_packets(&cat->ts_header, secdata, curpos, cat->section_header->pointer_field, ts_packets, num_packets);
  83. FREE(secdata);
  84. }
  85. void ts_cat_regenerate_packets(struct ts_cat *cat) {
  86. uint8_t *ts_packets;
  87. int num_packets;
  88. ts_cat_generate(cat, &ts_packets, &num_packets);
  89. FREE(cat->section_header->packet_data);
  90. cat->section_header->packet_data = ts_packets;
  91. cat->section_header->num_packets = num_packets;
  92. }
  93. struct ts_cat *ts_cat_copy(struct ts_cat *cat) {
  94. struct ts_cat *newcat = ts_cat_alloc();
  95. int i;
  96. for (i=0;i<cat->section_header->num_packets; i++) {
  97. newcat = ts_cat_push_packet(newcat, cat->section_header->packet_data + (i * TS_PACKET_SIZE));
  98. }
  99. if (newcat->initialized) {
  100. return newcat;
  101. } else {
  102. ts_LOGf("Error copying cat!\n");
  103. ts_cat_free(&newcat);
  104. return NULL;
  105. }
  106. }
  107. void ts_cat_check_generator(struct ts_cat *cat) {
  108. struct ts_cat *cat1 = ts_cat_copy(cat);
  109. if (cat1) {
  110. ts_compare_data("CAT (tspacket->struct)",
  111. cat1->section_header->packet_data,
  112. cat->section_header->packet_data,
  113. cat->section_header->num_packets * TS_PACKET_SIZE);
  114. ts_cat_free(&cat1);
  115. }
  116. uint8_t *ts_packets;
  117. int num_packets;
  118. ts_cat_generate(cat, &ts_packets, &num_packets);
  119. if (num_packets != cat->section_header->num_packets) {
  120. ts_LOGf("ERROR: num_packets:%d != sec->num_packets:%d\n", num_packets, cat->section_header->num_packets);
  121. }
  122. ts_compare_data("CAT (struct->tspacket)", cat->section_header->packet_data, ts_packets, num_packets * TS_PACKET_SIZE);
  123. free(ts_packets);
  124. }
  125. void ts_cat_dump(struct ts_cat *cat) {
  126. struct ts_section_header *sec = cat->section_header;
  127. ts_section_dump(sec);
  128. if (cat->program_info_size > 0) {
  129. ts_LOGf(" * Descriptor dump:\n");
  130. ts_descriptor_dump(cat->program_info, cat->program_info_size);
  131. }
  132. ts_cat_check_generator(cat);
  133. }
  134. int ts_cat_is_same(struct ts_cat *cat1, struct ts_cat *cat2) {
  135. return ts_section_is_same(cat1->section_header, cat2->section_header);
  136. }
  137. enum CA_system ts_get_CA_sys(uint16_t CA_id) {
  138. if (CA_id >= 0x0600 && CA_id <= 0x06FF) return CA_IRDETO;
  139. if (CA_id >= 0x0B00 && CA_id <= 0x0BFF) return CA_CONNAX;
  140. if (CA_id >= 0x0D00 && CA_id <= 0x0DFF) return CA_CRYPTOWORKS;
  141. return CA_UNKNOWN;
  142. }
  143. char * ts_get_CA_sys_txt(enum CA_system CA_sys) {
  144. switch (CA_sys) {
  145. case CA_IRDETO: return "IRDETO CA";
  146. case CA_CONNAX: return "CONNAX CA";
  147. case CA_CRYPTOWORKS: return "CRYPTOWORKS CA";
  148. case CA_UNKNOWN:
  149. default: return "UNKNOWN CA";
  150. }
  151. }
  152. 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) {
  153. while (data_len >= 2) {
  154. uint8_t tag = data[0];
  155. uint8_t this_length = data[1];
  156. data += 2;
  157. data_len -= 2;
  158. if (tag == 9 && this_length == 4) {
  159. uint16_t CA_ID = (data[0] << 8) | data[1];
  160. uint16_t CA_PID = ((data[2] & 0x1F) << 8) | data[3];
  161. if (ts_get_CA_sys(CA_ID) == req_CA_type) {
  162. *CA_id = CA_ID;
  163. *CA_pid = CA_PID;
  164. return 1;
  165. }
  166. }
  167. data_len -= this_length;
  168. data += this_length;
  169. }
  170. return 0;
  171. }
  172. int ts_get_emm_info(struct ts_cat *cat, enum CA_system req_CA_type, uint16_t *CA_id, uint16_t *CA_pid) {
  173. return find_CA_descriptor(cat->program_info, cat->program_info_size, req_CA_type, CA_id, CA_pid);
  174. }
  175. int ts_get_ecm_info(struct ts_pmt *pmt, enum CA_system req_CA_type, uint16_t *CA_id, uint16_t *CA_pid) {
  176. return find_CA_descriptor(pmt->program_info, pmt->program_info_size, req_CA_type, CA_id, CA_pid);
  177. }