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

tsfuncs_cat.c 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 || !section_header.section_syntax_indicator) {
  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. if (cat->section_header->section_syntax_indicator) {
  50. ts_section_add_packet(cat->section_header, &ts_header, ts_packet);
  51. if (cat->section_header->initialized) {
  52. if (!ts_cat_parse(cat))
  53. goto ERROR;
  54. }
  55. }
  56. }
  57. OUT:
  58. return cat;
  59. ERROR:
  60. return ts_cat_reset(cat);
  61. }
  62. int ts_cat_parse(struct ts_cat *cat) {
  63. uint8_t *section_data = cat->section_header->data;
  64. int section_len = cat->section_header->data_len;
  65. /* Handle streams */
  66. uint8_t *stream_data = section_data;
  67. cat->program_info_size = section_len;
  68. cat->program_info = malloc(cat->program_info_size);
  69. memcpy(cat->program_info, stream_data, cat->program_info_size);
  70. stream_data += cat->program_info_size;
  71. cat->CRC = (cat->CRC << 8) | stream_data[3];
  72. cat->CRC = (cat->CRC << 8) | stream_data[2];
  73. cat->CRC = (cat->CRC << 8) | stream_data[1];
  74. cat->CRC = (cat->CRC << 8) | stream_data[0];
  75. u_int32_t check_crc = ts_crc32_section(cat->section_header);
  76. if (check_crc != 0) {
  77. ts_LOGf("!!! Wrong cat CRC! It should be 0 but it is %08x (CRC in data is 0x%08x)\n", check_crc, cat->CRC);
  78. return 0;
  79. }
  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->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. int i;
  136. ts_LOGf("CAT table\n");
  137. for(i=0;i<cat->section_header->num_packets;i++) {
  138. struct ts_header tshdr;
  139. ts_packet_header_parse(cat->section_header->packet_data + (i * TS_PACKET_SIZE), &tshdr);
  140. ts_packet_header_dump(&tshdr);
  141. }
  142. ts_section_header_dump(cat->section_header);
  143. if (cat->program_info_size > 0) {
  144. ts_LOGf(" * Descriptor dump:\n");
  145. ts_descriptor_dump(cat->program_info, cat->program_info_size);
  146. }
  147. ts_LOGf(" * CRC 0x%04x\n", cat->CRC);
  148. ts_cat_check_generator(cat);
  149. }
  150. int ts_cat_is_same(struct ts_cat *cat1, struct ts_cat *cat2) {
  151. if (cat1->CRC == cat2->CRC) // Same
  152. return 1;
  153. // If some version is not current, just claim the structures are the same
  154. if (!cat1->section_header->current_next_indicator || cat2->section_header->version_number)
  155. return 1;
  156. if (cat1->section_header->version_number != cat2->section_header->version_number) // Different
  157. return 0;
  158. return 1; // Same
  159. }
  160. enum CA_system ts_get_CA_sys(uint16_t CA_id) {
  161. if (CA_id >= 0x0600 && CA_id <= 0x06FF) return CA_IRDETO;
  162. if (CA_id >= 0x0B00 && CA_id <= 0x0BFF) return CA_CONNAX;
  163. if (CA_id >= 0x0D00 && CA_id <= 0x0DFF) return CA_CRYPTOWORKS;
  164. return CA_UNKNOWN;
  165. }
  166. char * ts_get_CA_sys_txt(enum CA_system CA_sys) {
  167. switch (CA_sys) {
  168. case CA_IRDETO: return "IRDETO CA";
  169. case CA_CONNAX: return "CONNAX CA";
  170. case CA_CRYPTOWORKS: return "CRYPTOWORKS CA";
  171. case CA_UNKNOWN:
  172. default: return "UNKNOWN CA";
  173. }
  174. }
  175. 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) {
  176. while (data_len >= 2) {
  177. uint8_t tag = data[0];
  178. uint8_t this_length = data[1];
  179. data += 2;
  180. data_len -= 2;
  181. if (tag == 9 && this_length == 4) {
  182. uint16_t CA_ID = (data[0] << 8) | data[1];
  183. uint16_t CA_PID = ((data[2] & 0x1F) << 8) | data[3];
  184. if (ts_get_CA_sys(CA_ID) == req_CA_type) {
  185. *CA_id = CA_ID;
  186. *CA_pid = CA_PID;
  187. return 1;
  188. }
  189. }
  190. data_len -= this_length;
  191. data += this_length;
  192. }
  193. return 0;
  194. }
  195. int ts_get_emm_info(struct ts_cat *cat, enum CA_system req_CA_type, uint16_t *CA_id, uint16_t *CA_pid) {
  196. return find_CA_descriptor(cat->program_info, cat->program_info_size, req_CA_type, CA_id, CA_pid);
  197. }
  198. int ts_get_ecm_info(struct ts_pmt *pmt, enum CA_system req_CA_type, uint16_t *CA_id, uint16_t *CA_pid) {
  199. return find_CA_descriptor(pmt->program_info, pmt->program_info_size, req_CA_type, CA_id, CA_pid);
  200. }