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

privsec.c 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. if (!privsec->ts_header.pusi)
  50. privsec->ts_header = ts_header;
  51. }
  52. if (ts_header.pusi) {
  53. struct ts_section_header section_header;
  54. memset(&section_header, 0, sizeof(struct ts_section_header));
  55. uint8_t *section_data = ts_section_header_parse(ts_packet, &privsec->ts_header, &section_header);
  56. if (!section_data) {
  57. memset(&privsec->ts_header, 0, sizeof(struct ts_header));
  58. goto OUT;
  59. }
  60. // Set correct section_header
  61. ts_section_header_parse(ts_packet, &privsec->ts_header, privsec->section_header);
  62. }
  63. if (!privsec->initialized) {
  64. ts_section_add_packet(privsec->section_header, &ts_header, ts_packet);
  65. if (privsec->section_header->initialized) {
  66. privsec->initialized = 1;
  67. }
  68. }
  69. OUT:
  70. return privsec;
  71. }
  72. int ts_privsec_is_same(struct ts_privsec *p1, struct ts_privsec *p2) {
  73. if (p1 == p2) return 1; // Same
  74. if ((!p1 && p2) || (p1 && !p2)) return 0; // Not same (one is NULL)
  75. if (p1->section_header->section_length != p1->section_header->section_length) return 0; // Not same
  76. return memcmp(p1->section_header->section_data, p2->section_header->section_data, p1->section_header->section_length) == 0;
  77. }
  78. void ts_privsec_dump(struct ts_privsec *privsec) {
  79. struct ts_section_header *sec = privsec->section_header;
  80. ts_section_dump(sec);
  81. char *data = ts_hex_dump(sec->data, sec->data_len, 16);
  82. ts_LOGf(" * Section data:\n%s\n", data);
  83. FREE(data);
  84. }