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