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.

privsec.c 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Private sections parser
  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 <time.h>
  14. #include "tsfuncs.h"
  15. struct ts_privsec *ts_privsec_alloc() {
  16. struct ts_privsec *privsec = calloc(1, sizeof(struct ts_privsec));
  17. privsec->section_header = ts_section_data_alloc();
  18. return privsec;
  19. }
  20. void ts_privsec_clear(struct ts_privsec *privsec) {
  21. if (!privsec)
  22. return;
  23. // save
  24. struct ts_section_header *section_header = privsec->section_header;
  25. // clear
  26. ts_section_data_clear(section_header);
  27. memset(privsec, 0, sizeof(struct ts_privsec));
  28. // restore
  29. privsec->section_header = section_header;
  30. }
  31. void ts_privsec_free(struct ts_privsec **pprivsec) {
  32. struct ts_privsec *privsec = *pprivsec;
  33. if (privsec) {
  34. ts_section_data_free(&privsec->section_header);
  35. FREE(*pprivsec);
  36. }
  37. }
  38. void ts_privsec_copy(struct ts_privsec *src, struct ts_privsec *dst) {
  39. if (!src || !dst)
  40. return;
  41. dst->ts_header = src->ts_header;
  42. dst->initialized = src->initialized;
  43. ts_section_data_copy(src->section_header, dst->section_header);
  44. }
  45. struct ts_privsec *ts_privsec_push_packet(struct ts_privsec *privsec, uint8_t *ts_packet) {
  46. struct ts_header ts_header;
  47. memset(&ts_header, 0, sizeof(struct ts_header));
  48. if (ts_packet_header_parse(ts_packet, &ts_header)) {
  49. // Received PUSI packet before table END, clear the table to start gathering new one
  50. if (ts_header.pusi && privsec->ts_header.pusi)
  51. ts_privsec_clear(privsec);
  52. if (!privsec->ts_header.pusi)
  53. privsec->ts_header = ts_header;
  54. }
  55. if (ts_header.pusi) {
  56. struct ts_section_header section_header;
  57. memset(&section_header, 0, sizeof(struct ts_section_header));
  58. uint8_t *section_data = ts_section_header_parse(ts_packet, &privsec->ts_header, &section_header);
  59. if (!section_data) {
  60. memset(&privsec->ts_header, 0, sizeof(struct ts_header));
  61. goto OUT;
  62. }
  63. // Set correct section_header
  64. ts_section_header_parse(ts_packet, &privsec->ts_header, privsec->section_header);
  65. }
  66. if (!privsec->initialized) {
  67. ts_section_add_packet(privsec->section_header, &ts_header, ts_packet);
  68. if (privsec->section_header->initialized) {
  69. privsec->initialized = 1;
  70. }
  71. }
  72. OUT:
  73. return privsec;
  74. }
  75. int ts_privsec_is_same(struct ts_privsec *p1, struct ts_privsec *p2) {
  76. if (p1 == p2) return 1; // Same
  77. if ((!p1 && p2) || (p1 && !p2)) return 0; // Not same (one is NULL)
  78. if (p1->section_header->section_length != p1->section_header->section_length) return 0; // Not same
  79. return memcmp(p1->section_header->section_data, p2->section_header->section_data, p1->section_header->section_length) == 0;
  80. }
  81. void ts_privsec_dump(struct ts_privsec *privsec) {
  82. struct ts_section_header *sec = privsec->section_header;
  83. ts_section_dump(sec);
  84. char *data = ts_hex_dump(sec->data, sec->data_len, 16);
  85. ts_LOGf(" * Section data:\n%s\n", data);
  86. FREE(data);
  87. }