libtsfuncs is a library for mpeg PSI parsing and generation. https://georgi.unixsol.org/programs/libtsfuncs/

tsfuncs_cat.c 6.3KB

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