1 /*
2  * Copyright (c) 2011-2012 - Mauro Carvalho Chehab
3  * Copyright (c) 2012-2014 - Andre Roth <neolynx@gmail.com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation version 2.1 of the License.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  * Or, point your browser to http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
18  *
19  */
20 
21 #include <libdvbv5/sdt.h>
22 #include <libdvbv5/descriptors.h>
23 #include <libdvbv5/dvb-fe.h>
24 
25 #if __GNUC__ >= 9
26 #pragma GCC diagnostic ignored "-Waddress-of-packed-member"
27 #endif
28 
dvb_table_sdt_init(struct dvb_v5_fe_parms * parms,const uint8_t * buf,ssize_t buflen,struct dvb_table_sdt ** table)29 ssize_t dvb_table_sdt_init(struct dvb_v5_fe_parms *parms, const uint8_t *buf,
30 			ssize_t buflen, struct dvb_table_sdt **table)
31 {
32 	const uint8_t *p = buf, *endbuf = buf + buflen;
33 	struct dvb_table_sdt *sdt;
34 	struct dvb_table_sdt_service **head;
35 	size_t size;
36 
37 	size = offsetof(struct dvb_table_sdt, service);
38 	if (p + size > endbuf) {
39 		dvb_logerr("%s: short read %zd/%zd bytes", __func__,
40 			   endbuf - p, size);
41 		return -1;
42 	}
43 
44 	if (buf[0] != DVB_TABLE_SDT && buf[0] != DVB_TABLE_SDT2) {
45 		dvb_logerr("%s: invalid marker 0x%02x, should be 0x%02x or 0x%02x",
46 				__func__, buf[0], DVB_TABLE_SDT, DVB_TABLE_SDT2);
47 		return -2;
48 	}
49 
50 	if (!*table) {
51 		*table = calloc(sizeof(struct dvb_table_sdt), 1);
52 		if (!*table) {
53 			dvb_logerr("%s: out of memory", __func__);
54 			return -3;
55 		}
56 	}
57 	sdt = *table;
58 	memcpy(sdt, p, size);
59 	p += size;
60 	dvb_table_header_init(&sdt->header);
61 	bswap16(sdt->network_id);
62 
63 	/* find end of curent list */
64 	head = &sdt->service;
65 	while (*head != NULL)
66 		head = &(*head)->next;
67 
68 	size = sdt->header.section_length + 3 - DVB_CRC_SIZE; /* plus header, minus CRC */
69 	if (buf + size > endbuf) {
70 		dvb_logerr("%s: short read %zd/%zd bytes", __func__,
71 			   endbuf - buf, size);
72 		return -4;
73 	}
74 	endbuf = buf + size;
75 
76 	/* get the event entries */
77 	size = offsetof(struct dvb_table_sdt_service, descriptor);
78 	while (p + size <= endbuf) {
79 		struct dvb_table_sdt_service *service;
80 
81 		service = malloc(sizeof(struct dvb_table_sdt_service));
82 		if (!service) {
83 			dvb_logerr("%s: out of memory", __func__);
84 			return -5;
85 		}
86 		memcpy(service, p, size);
87 		p += size;
88 
89 		bswap16(service->service_id);
90 		bswap16(service->bitfield);
91 		service->descriptor = NULL;
92 		service->next = NULL;
93 
94 		*head = service;
95 		head = &(*head)->next;
96 
97 		/* parse the descriptors */
98 		if (service->desc_length > 0) {
99 			uint16_t desc_length = service->desc_length;
100 			if (p + desc_length > endbuf) {
101 				dvb_logwarn("%s: descriptors short read %zd/%d bytes", __func__,
102 					   endbuf - p, desc_length);
103 				desc_length = endbuf - p;
104 			}
105 			if (dvb_desc_parse(parms, p, desc_length,
106 					      &service->descriptor) != 0) {
107 				return -6;
108 			}
109 			p += desc_length;
110 		}
111 
112 	}
113 	if (endbuf - p)
114 		dvb_logwarn("%s: %zu spurious bytes at the end",
115 			   __func__, endbuf - p);
116 
117 	return p - buf;
118 }
119 
dvb_table_sdt_free(struct dvb_table_sdt * sdt)120 void dvb_table_sdt_free(struct dvb_table_sdt *sdt)
121 {
122 	struct dvb_table_sdt_service *service = sdt->service;
123 	while(service) {
124 		dvb_desc_free((struct dvb_desc **) &service->descriptor);
125 		struct dvb_table_sdt_service *tmp = service;
126 		service = service->next;
127 		free(tmp);
128 	}
129 	free(sdt);
130 }
131 
dvb_table_sdt_print(struct dvb_v5_fe_parms * parms,struct dvb_table_sdt * sdt)132 void dvb_table_sdt_print(struct dvb_v5_fe_parms *parms, struct dvb_table_sdt *sdt)
133 {
134 	dvb_loginfo("SDT");
135 	dvb_table_header_print(parms, &sdt->header);
136 	dvb_loginfo("| network_id          %d", sdt->network_id);
137 	dvb_loginfo("| reserved            %d", sdt->reserved);
138 	dvb_loginfo("|\\");
139 	const struct dvb_table_sdt_service *service = sdt->service;
140 	uint16_t services = 0;
141 	while (service) {
142 		dvb_loginfo("|- service 0x%04x", service->service_id);
143 		dvb_loginfo("|   EIT schedule          %d", service->EIT_schedule);
144 		dvb_loginfo("|   EIT present following %d", service->EIT_present_following);
145 		dvb_loginfo("|   free CA mode          %d", service->free_CA_mode);
146 		dvb_loginfo("|   running status        %d", service->running_status);
147 		dvb_loginfo("|   descriptor length     %d", service->desc_length);
148 		dvb_desc_print(parms, service->descriptor);
149 		service = service->next;
150 		services++;
151 	}
152 	dvb_loginfo("|_  %d services", services);
153 }
154 
155