• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/pat.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_pat_init(struct dvb_v5_fe_parms * parms,const uint8_t * buf,ssize_t buflen,struct dvb_table_pat ** table)29 ssize_t dvb_table_pat_init(struct dvb_v5_fe_parms *parms, const uint8_t *buf,
30 			ssize_t buflen, struct dvb_table_pat **table)
31 {
32 	const uint8_t *p = buf, *endbuf = buf + buflen;
33 	struct dvb_table_pat *pat;
34 	struct dvb_table_pat_program **head;
35 	size_t size;
36 
37 	size = offsetof(struct dvb_table_pat, programs);
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_PAT) {
45 		dvb_logerr("%s: invalid marker 0x%02x, should be 0x%02x",
46 				__func__, buf[0], DVB_TABLE_PAT);
47 		return -2;
48 	}
49 
50 	if (!*table) {
51 		*table = calloc(sizeof(struct dvb_table_pat), 1);
52 		if (!*table) {
53 			dvb_logerr("%s: out of memory", __func__);
54 			return -3;
55 		}
56 	}
57 	pat = *table;
58 	memcpy(pat, buf, size);
59 	p += size;
60 	dvb_table_header_init(&pat->header);
61 
62 	/* find end of current list */
63 	head = &pat->program;
64 	while (*head != NULL)
65 		head = &(*head)->next;
66 
67 	size = pat->header.section_length + 3 - DVB_CRC_SIZE; /* plus header, minus CRC */
68 	if (buf + size > endbuf) {
69 		dvb_logerr("%s: short read %zd/%zd bytes", __func__,
70 			   endbuf - buf, size);
71 		return -4;
72 	}
73 	endbuf = buf + size;
74 
75 	size = offsetof(struct dvb_table_pat_program, next);
76 	while (p + size <= endbuf) {
77 		struct dvb_table_pat_program *prog;
78 
79 		prog = malloc(sizeof(struct dvb_table_pat_program));
80 		if (!prog) {
81 			dvb_logerr("%s: out of memory", __func__);
82 			return -5;
83 		}
84 
85 		memcpy(prog, p, size);
86 		p += size;
87 
88 		bswap16(prog->service_id);
89 
90 		if (prog->pid == 0x1fff) { /* ignore null packets */
91 			free(prog);
92 			break;
93 		}
94 		bswap16(prog->bitfield);
95 		pat->programs++;
96 
97 		prog->next = NULL;
98 
99 		*head = prog;
100 		head = &(*head)->next;
101 	}
102 	if (endbuf - p)
103 		dvb_logwarn("%s: %zu spurious bytes at the end",
104 			   __func__, endbuf - p);
105 	return p - buf;
106 }
107 
dvb_table_pat_free(struct dvb_table_pat * pat)108 void dvb_table_pat_free(struct dvb_table_pat *pat)
109 {
110 	struct dvb_table_pat_program *prog = pat->program;
111 
112 	while (prog) {
113 		struct dvb_table_pat_program *tmp = prog;
114 		prog = prog->next;
115 		free(tmp);
116 	}
117 	free(pat);
118 }
119 
dvb_table_pat_print(struct dvb_v5_fe_parms * parms,struct dvb_table_pat * pat)120 void dvb_table_pat_print(struct dvb_v5_fe_parms *parms, struct dvb_table_pat *pat)
121 {
122 	struct dvb_table_pat_program *prog = pat->program;
123 
124 	dvb_loginfo("PAT");
125 	dvb_table_header_print(parms, &pat->header);
126 	dvb_loginfo("|\\ %d program pid%s", pat->programs, pat->programs != 1 ? "s" : "");
127 
128 	while (prog) {
129 		dvb_loginfo("|  pid 0x%04x: service 0x%04x", prog->pid, prog->service_id);
130 		prog = prog->next;
131 	}
132 }
133 
134