2
* section and descriptor parser
4
* Copyright (C) 2005 Andrew de Quincey (adq_dvb@lidskialf.net)
6
* This library is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU Lesser General Public
8
* License as published by the Free Software Foundation; either
9
* version 2.1 of the License, or (at your option) any later version.
11
* This library is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
* Lesser General Public License for more details.
16
* You should have received a copy of the GNU Lesser General Public
17
* License along with this library; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23
#include "section_buf.h"
25
#define SECTION_HDR_SIZE 3
26
#define SECTION_PAD 0xff
28
int section_buf_init(struct section_buf *section, int max)
30
if (max < SECTION_HDR_SIZE)
33
memset(section, 0, sizeof(struct section_buf));
34
section->max = max; /* max size of data */
35
section->len = SECTION_HDR_SIZE;
36
section->wait_pdu = 1;
41
int section_buf_add(struct section_buf *section, uint8_t* frag, int len, int *section_status)
46
uint8_t *pos = (uint8_t*) section + sizeof(struct section_buf) + section->count;
48
/* have we finished? */
49
if (section->header && (section->len == section->count)) {
54
/* skip over section padding bytes */
56
if (section->count == 0) {
57
while(len && (*frag == SECTION_PAD)) {
67
/* grab the header to get the section length */
68
if (!section->header) {
69
/* copy the header frag */
70
copy = SECTION_HDR_SIZE - section->count;
73
memcpy(pos, frag, copy);
74
section->count += copy;
80
/* we need 3 bytes for the section header */
81
if (section->count != SECTION_HDR_SIZE)
84
/* work out the length & check it isn't too big */
85
data = (uint8_t*) section + sizeof(struct section_buf);
86
section->len = SECTION_HDR_SIZE + (((data[1] & 0x0f) << 8) | data[2]);
87
if (section->len > section->max) {
88
*section_status = -ERANGE;
97
copy = section->len - section->count;
100
memcpy(pos, frag, copy);
101
section->count += copy;
104
/* have we finished? */
105
if (section->header && (section->len == section->count))
108
/* return number of bytes used */
112
int section_buf_add_transport_payload(struct section_buf *section,
113
uint8_t* payload, int len,
114
int pdu_start, int *section_status)
119
/* have we finished? */
120
if (section->header && (section->len == section->count)) {
125
/* don't bother if we're waiting for a PDU */
127
if (section->wait_pdu && (!pdu_start))
130
/* if we're at a PDU start, we need extra handling for the extra first
131
* byte giving the offset to the start of the next section. */
133
/* we have received a pdu */
134
section->wait_pdu = 0;
136
/* work out the offset to the _next_ payload */
137
int offset = payload[0];
138
if ((offset+1) > len) {
139
section->wait_pdu = 1;
140
*section_status = -EINVAL;
144
/* accumulate the end if we need to */
145
if (section->count != 0) {
146
/* add the final fragment. */
147
tmp = section_buf_add(section, payload + 1, offset, section_status);
149
/* the stream said this was the final fragment
150
* (PDU START bit) - check that it really was! */
151
if ((tmp != offset) || section_buf_remaining(section) || (*section_status != 1)) {
152
*section_status = -ERANGE;
153
section->wait_pdu = 1;
157
/* it is complete - return the number of bytes we used */
161
/* otherwise, we skip the end of the previous section, and
162
* start accumulating the new data. */
166
/* ok, just accumulate the data as normal */
167
tmp = section_buf_add(section, payload+used, len - used, section_status);
168
if (*section_status < 0) {
169
section->wait_pdu = 1;