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

cat.c 6.7KB

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