aboutsummaryrefslogtreecommitdiffstats
path: root/lib/libucsi/section_buf.c
blob: 35d465e0c904b23bcddd679079c6bc570a7f18f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
 * section and descriptor parser
 *
 * Copyright (C) 2005 Andrew de Quincey (adq_dvb@lidskialf.net)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */

#include <errno.h>
#include <string.h>
#include "section_buf.h"

#define SECTION_HDR_SIZE 3
#define SECTION_PAD 0xff

int section_buf_init(struct section_buf *section, int max)
{
	if (max < SECTION_HDR_SIZE)
		return -EINVAL;

	memset(section, 0, sizeof(struct section_buf));
	section->max = max; /* max size of data */
	section->len = SECTION_HDR_SIZE;
	section->wait_pdu = 1;

	return 0;
}

int section_buf_add(struct section_buf *section, uint8_t* frag, int len, int *section_status)
{
	int copy;
	int used = 0;
	uint8_t *data;
	uint8_t *pos = (uint8_t*) section + sizeof(struct section_buf) + section->count;

	/* have we finished? */
	if (section->header && (section->len == section->count)) {
		*section_status = 1;
		return 0;
	}

	/* skip over section padding bytes */
	*section_status = 0;
	if (section->count == 0) {
		while(len && (*frag == SECTION_PAD)) {
			frag++;
			len--;
			used++;
		}

		if (len == 0)
			return used;
	}

	/* grab the header to get the section length */
	if (!section->header) {
		/* copy the header frag */
		copy = SECTION_HDR_SIZE - section->count;
		if (copy > len)
			copy = len;
		memcpy(pos, frag, copy);
		section->count += copy;
		pos += copy;
		frag += copy;
		used += copy;
		len -= copy;

		/* we need 3 bytes for the section header */
		if (section->count != SECTION_HDR_SIZE)
			return used;

		/* work out the length & check it isn't too big */
		data = (uint8_t*) section + sizeof(struct section_buf);
		section->len = SECTION_HDR_SIZE + (((data[1] & 0x0f) << 8) | data[2]);
		if (section->len > section->max) {
			*section_status = -ERANGE;
			return len + used;
		}

		/* update fields */
		section->header = 1;
	}

	/* accumulate frag */
	copy = section->len - section->count;
	if (copy > len)
		copy = len;
	memcpy(pos, frag, copy);
	section->count += copy;
	used += copy;

	/* have we finished? */
	if (section->header && (section->len == section->count))
		*section_status = 1;

	/* return number of bytes used */
	return used;
}

int section_buf_add_transport_payload(struct section_buf *section,
				      uint8_t* payload, int len,
				      int pdu_start, int *section_status)
{
	int used = 0;
	int tmp;

	/* have we finished? */
	if (section->header && (section->len == section->count)) {
		*section_status = 1;
		return 0;
	}

	/* don't bother if we're waiting for a PDU */
	*section_status = 0;
	if (section->wait_pdu && (!pdu_start))
		return len;

	/* if we're at a PDU start, we need extra handling for the extra first
	 * byte giving the offset to the start of the next section. */
	if (pdu_start) {
		/* we have received a pdu */
		section->wait_pdu = 0;

		/* work out the offset to the _next_ payload */
		int offset = payload[0];
		if ((offset+1) > len) {
			section->wait_pdu = 1;
			*section_status = -EINVAL;
			return len;
		}

		/* accumulate the end if we need to */
		if (section->count != 0) {
			/* add the final fragment. */
			tmp = section_buf_add(section, payload + 1, offset, section_status);

			/* the stream said this was the final fragment
			 * (PDU START bit) - check that it really was! */
			if ((tmp != offset) || section_buf_remaining(section) || (*section_status != 1)) {
				*section_status = -ERANGE;
				section->wait_pdu = 1;
				return 1 + tmp;
			}

			/* it is complete - return the number of bytes we used */
			return 1 + tmp;
		}

		/* otherwise, we skip the end of the previous section, and
		 * start accumulating the new data. */
		used = 1 + offset;
	}

	/* ok, just accumulate the data as normal */
	tmp = section_buf_add(section, payload+used, len - used, section_status);
	if (*section_status < 0) {
		section->wait_pdu = 1;
	}

	return used + tmp;
}