summaryrefslogtreecommitdiff
path: root/src/rtsp_decoder.c
blob: 68f58662f8431c9671105076f18f8e493e5b38e6 (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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
/*
 * OpenWFD - Open-Source Wifi-Display Implementation
 *
 * Copyright (c) 2013 David Herrmann <dh.herrmann@gmail.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files
 * (the "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include <errno.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include "shared.h"
#include "shl_ring.h"
#include "rtsp.h"

enum state {
	STATE_NEW,
	STATE_HEADER,
	STATE_HEADER_NL,
	STATE_BODY,
};

struct owfd_rtsp_decoder {
	void *data;
	owfd_rtsp_decoder_cb cb;

	struct shl_ring ring;
	unsigned int state;
	char last_chr;
	size_t remaining_body;

	size_t header_size;
	struct owfd_rtsp_msg msg;
};

int owfd_rtsp_decoder_new(struct owfd_rtsp_decoder **out,
			  owfd_rtsp_decoder_cb cb)
{
	struct owfd_rtsp_decoder *dec;
	int r;

	dec = calloc(1, sizeof(*dec));
	if (!dec)
		return -ENOMEM;
	dec->cb = cb;

	dec->header_size = 8;
	dec->msg.header = calloc(1, sizeof(char*) * dec->header_size);
	if (!dec->msg.header) {
		r = -ENOMEM;
		goto err_dec;
	}

	dec->msg.header_len = calloc(1, sizeof(size_t) * dec->header_size);
	if (!dec->msg.header_len) {
		r = -ENOMEM;
		goto err_header;
	}

	*out = dec;
	return 0;

err_header:
	free(dec->msg.header);
err_dec:
	free(dec);
	return r;
}

void owfd_rtsp_decoder_free(struct owfd_rtsp_decoder *dec)
{
	size_t i;

	if (!dec)
		return;

	for (i = 0; i < dec->msg.header_num; ++i)
		free(dec->msg.header[i]);

	free(dec->msg.header);
	free(dec->msg.header_len);
	free(dec->msg.body);

	shl_ring_clear(&dec->ring);
	free(dec);
}

void owfd_rtsp_decoder_set_data(struct owfd_rtsp_decoder *dec, void *data)
{
	dec->data = data;
}

void *owfd_rtsp_decoder_get_data(struct owfd_rtsp_decoder *dec)
{
	return dec->data;
}

void owfd_rtsp_decoder_flush(struct owfd_rtsp_decoder *dec)
{
	shl_ring_flush(&dec->ring);
	dec->state = STATE_NEW;
	dec->last_chr = 0;
	dec->remaining_body = 0;
}

static void msg_done(struct owfd_rtsp_decoder *dec)
{
	size_t i;

	if (dec->cb)
		dec->cb(dec, &dec->msg, dec->data);

	for (i = 0; i < dec->msg.header_num; ++i) {
		free(dec->msg.header[i]);
		dec->msg.header[i] = NULL;
		dec->msg.header_len[i] = 0;
	}

	dec->msg.header_num = 0;

	free(dec->msg.body);
	dec->msg.body = NULL;
	dec->msg.body_len = 0;
}

static int push_header_line(struct owfd_rtsp_decoder *dec, char *line,
			    size_t len)
{
	char **h;
	size_t *l;
	size_t n;

	/* keep 1 entry for terminating NULL/0 */
	if (dec->header_size == dec->msg.header_num + 2) {
		n = dec->header_size * 2;
		if (n < dec->header_size)
			return -ENOMEM;

		h = realloc(dec->msg.header, sizeof(char*) * n);
		if (!h)
			return -ENOMEM;
		dec->msg.header = h;

		l = realloc(dec->msg.header_len, sizeof(size_t) * n);
		if (!l)
			return -ENOMEM;
		dec->msg.header_len = l;

		dec->header_size = n;
	}

	dec->msg.header[dec->msg.header_num] = line;
	dec->msg.header_len[dec->msg.header_num] = len;
	++dec->msg.header_num;
	dec->msg.header[dec->msg.header_num] = NULL;
	dec->msg.header_len[dec->msg.header_num] = 0;

	return 0;
}

static size_t sanitize_header_line(struct owfd_rtsp_decoder *dec,
				   char *line, size_t len)
{
	char *src, *dst, c, prev, last_c;
	size_t i;

	src = line;
	dst = line;
	last_c = 0;

	for (i = 0; i < len; ++i) {
		c = *src++;
		prev = last_c;
		last_c = c;

		/* ignore any binary 0 */
		if (c == '\0')
			continue;

		/* turn new-lines/tabs into white-space */
		if (c == '\r' || c == '\n' || c == '\t')
			c = ' ';

		/* trim whitespace */
		if (c == ' ' && prev == ' ')
			continue;

		*dst++ = c;
	}

	/* terminate string with binary zero */
	*dst = 0;

	return dst - line;
}

static int parse_header_line(struct owfd_rtsp_decoder *dec,
			     char *line)
{
	unsigned long l;
	char *e;

	if (!strncasecmp(line, "content-length:", 15)) {
		l = strtoul(&line[15], &e, 10);
		if (!line[15] || *e)
			return -EINVAL;

		if (dec->remaining_body && dec->remaining_body != l)
			return -EINVAL;

		dec->remaining_body = l;
	}

	return 0;
}

static int finish_header_line(struct owfd_rtsp_decoder *dec, size_t rlen)
{
	char *line;
	size_t l;
	int r;

	l = rlen;
	line = shl_ring_copy(&dec->ring, &l);
	if (!line)
		return -ENOMEM;

	shl_ring_pull(&dec->ring, rlen);

	l = sanitize_header_line(dec, line, l);
	r = parse_header_line(dec, line);
	if (r < 0) {
		free(line);
		return r;
	}

	r = push_header_line(dec, line, l);
	if (r < 0) {
		free(line);
		return r;
	}

	return 0;
}

static ssize_t feed_char_new(struct owfd_rtsp_decoder *dec,
			     char ch, size_t rlen)
{
	switch (ch) {
	case '\r':
	case '\n':
	case '\t':
	case ' ':
		/* If no msg has been started, yet, we ignore LWS for
		 * compatibility reasons. Note that they're actually not
		 * allowed, but should be ignored by implementations. */
		++rlen;
		break;
	default:
		/* Clear any pending data in the ring-buffer and then just
		 * push the data into the buffer. Any data except LWS is fine
		 * here. */
		dec->state = STATE_HEADER;
		dec->remaining_body = 0;

		shl_ring_pull(&dec->ring, rlen);
		rlen = 1;
		break;
	}

	return rlen;
}

static ssize_t feed_char_header(struct owfd_rtsp_decoder *dec,
				char ch, size_t rlen)
{
	int r;

	switch (ch) {
	case '\r':
		if (dec->last_chr == '\r' || dec->last_chr == '\n') {
			/* \r\r means empty new-line. We actually allow \r\r\n,
			 * too. \n\r means empty new-line, too, but might also
			 * be finished off as \n\r\n so go to STATE_HEADER_NL
			 * to optionally complete the new-line.
			 * However, if the body is empty, we need to finish the
			 * msg early as there might be no \n coming.. */
			dec->state = STATE_HEADER_NL;

			/* First finish the last header line if any. Don't
			 * include the current \r as it is already part of the
			 * empty following line. */
			r = finish_header_line(dec, rlen);
			if (r < 0)
				return r;
			rlen = 0;

			/* No remaining body. Finish message! */
			if (!dec->remaining_body)
				msg_done(dec);

			++rlen;
		} else {
			/* '\r' following any character just means newline
			 * (optionally followed by \n). We don't do anything as
			 * it might be a continuation line. */
			++rlen;
		}
		break;
	case '\n':
		if (dec->last_chr == '\n') {
			/* We got \n\n, which means we need to finish the
			 * current header-line. If there's no remaining body,
			 * we immediately finish the message and got to
			 * STATE_NEW. Otherwise, we go to STATE_BODY
			 * straight. */

			/* don't include second \n in header-line */
			r = finish_header_line(dec, rlen);
			if (r < 0)
				return r;
			rlen = 0;

			dec->state = STATE_BODY;
			if (!dec->remaining_body) {
				dec->state = STATE_NEW;
				msg_done(dec);
			}

			/* discard \n */
			shl_ring_pull(&dec->ring, 1);
		} else if (dec->last_chr == '\r') {
			/* We got an \r\n. We cannot finish the header line as
			 * it might be a continuation line. Next character
			 * decides what to do. Don't do anything here.
			 * \r\n\r cannot happen here as it is handled by
			 * STATE_HEADER_NL. */
			++rlen;
		} else {
			/* Same as above, we cannot finish the line as it
			 * might be a continuation line. Do nothing. */
			++rlen;
		}
		break;
	case '\t':
	case ' ':
		/* Whitespace. Simply push into buffer and don't do anything.
		 * In case of a continuation line, nothing has to be done,
		 * either. */
		++rlen;
		break;
	default:
		/* Last line was already completed and this is no whitespace,
		 * thus it's not a continuation line. Finish the line. */
		if (dec->last_chr == '\r' || dec->last_chr == '\n') {
			/* don't include new char in line */
			r = finish_header_line(dec, rlen);
			if (r < 0)
				return r;
			rlen = 0;
		}

		/* Push character into new line. Nothing to be done. */
		++rlen;
		break;
	}

	return rlen;
}

static ssize_t feed_char_body(struct owfd_rtsp_decoder *dec,
			      char ch, size_t rlen)
{
	char *line;
	size_t l;

	/* If remaining_body was already 0, the message had no body. Note that
	 * messages without body are finished early, so no need to call
	 * msg_done() here. Simply forward @ch to STATE_NEW.
	 * @rlen is usually 0. We don't care and forward it, too. */
	if (!dec->remaining_body) {
		dec->state = STATE_NEW;
		return feed_char_new(dec, ch, rlen);
	}

	/* *any* character is allowed as body */
	++rlen;
	if (!--dec->remaining_body) {
		/* full body received, copy it and go to STATE_NEW */
		l = (size_t)rlen;
		line = shl_ring_copy(&dec->ring, &l);
		if (!line)
			return -ENOMEM;

		dec->msg.body = line;
		dec->msg.body_len = l;
		msg_done(dec);

		dec->state = STATE_NEW;
		shl_ring_pull(&dec->ring, rlen);
		rlen = 0;
	}

	return rlen;
}

static ssize_t feed_char_header_nl(struct owfd_rtsp_decoder *dec,
				   char ch, size_t rlen)
{
	/* STATE_HEADER_NL means we received an empty line ending with \r. The
	 * standard requires a following \n but advises implementations to
	 * accept \r on itself, too.
	 * What we do is to parse a \n as end-of-header and any character as
	 * end-of-header plus start-of-body. Note that we discard anything in
	 * the ring-buffer that has already been parsed (which normally can
	 * only be a single \r or \r\n). */

	if (ch == '\n') {
		/* discard parsed \r plus new \n */
		shl_ring_pull(&dec->ring, rlen + 1);
		rlen = 0;

		dec->state = STATE_BODY;
		if (!dec->remaining_body)
			dec->state = STATE_NEW;

		return rlen;
	} else {
		/* discard parsed \r and push @ch into body */
		shl_ring_pull(&dec->ring, rlen);
		rlen = 0;

		dec->state = STATE_BODY;
		return feed_char_body(dec, ch, rlen);
	}
}

static ssize_t feed_char(struct owfd_rtsp_decoder *dec, char ch, size_t rlen)
{
	ssize_t r = 0;

	switch (dec->state) {
	case STATE_NEW:
		r = feed_char_new(dec, ch, rlen);
		break;
	case STATE_HEADER:
		r = feed_char_header(dec, ch, rlen);
		break;
	case STATE_HEADER_NL:
		r = feed_char_header_nl(dec, ch, rlen);
		break;
	case STATE_BODY:
		r = feed_char_body(dec, ch, rlen);
		break;
	}

	return r;
}

int owfd_rtsp_decoder_feed(struct owfd_rtsp_decoder *dec,
			   const char *buf, size_t len)
{
	size_t rlen, i;
	ssize_t l;
	int r;

	rlen = shl_ring_length(&dec->ring);
	r = shl_ring_push(&dec->ring, buf, len);
	if (r < 0)
		return -ENOMEM;

	for (i = 0; i < len; ++i) {
		l = feed_char(dec, buf[i], rlen);
		if (l < 0) {
			r = l;
			break;
		}

		rlen = l;
		dec->last_chr = buf[i];
	}

	if (r < 0) {
		/* ring buffer may be corrupted, flush it */
		owfd_rtsp_decoder_flush(dec);
	}

	return r;
}