summaryrefslogtreecommitdiffstats
path: root/PICA_msgproc.c
blob: 27629d9c212f624e0117f8a510687578f77b868e (plain) (blame)
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
/*
	(c) Copyright  2012 - 2018 Anton Sviridenko
	https://picapica.im

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, version 3.

	This program 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 General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/
#include <limits.h>
#include <string.h>
#include "PICA_proto.h"
#include "PICA_msgproc.h"


//возвращает размер сообщения либо UINT_MAX,если количество прочитанных байт недостаточно для определения размера
//либо 0 при ошибочных данных
unsigned int PICA_msggetsize(unsigned char *buf, unsigned int nb, struct PICA_msginfo *msgs, unsigned int nmsg)
{
	unsigned int i;

	if (nb < 2)
		return UINT_MAX;

	if (buf[0] != buf[1])
		return 0;


	for (i = 0; i < nmsg; i++)
		if (buf[0] == (unsigned char)msgs[i].msgid)
		{
			if (msgs[i].fixedsz == PICA_MSG_FIXED_SIZE)
				return msgs[i].sz;
			else if (msgs[i].fixedsz == PICA_MSG_VAR_SIZE)
			{
				if (nb >= (msgs[i].sz + 2))
				{
					unsigned int tail_len = 0;
					int j;
#if defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
					for (j = 0; j < msgs[i].sz; j++)
						((char*)&tail_len)[sizeof(int) - 1 - j] = buf[2 + j];
#else
					for (j = 0; j < msgs[i].sz; j++)
						((char*)&tail_len)[j] = buf[2 + j];
#endif
					return 2 + msgs[i].sz + tail_len;
				}
				else
					return UINT_MAX;
			}
		}

	return 0;
}

//Определяет тип сообщения размера size, находящегося в буфере buf, вызывает соответствующий обработчик и передает обработчику аргумент arg. Возвращает значение, которое вернула функция-обработчик сообщения. Если тип сообщения неизвестен, возвращает 0
unsigned int PICA_processmsg(unsigned char *buf, unsigned int size, void* arg, struct PICA_msginfo *msgs, unsigned int nmsg)
{
	int i;

	if (buf[0] != buf[1])
		return 0;

	for (i = 0; i < nmsg; i++)
		if (buf[0] == (unsigned char)msgs[i].msgid)
			return msgs[i].msgprocfn(buf, size, arg);

	return 0;
}

unsigned int PICA_processdatastream(unsigned char *buf, unsigned int *read_pos, void* arg, const struct PICA_msginfo *msgs, unsigned int nmsg)
{
	while(*read_pos)
	{
		unsigned int uret;
		uret = PICA_msggetsize(buf, *read_pos, msgs, nmsg);

		if (!uret)
			return 0;

		if (uret > *read_pos)
			break;

		if (!PICA_processmsg(buf, uret, arg, msgs, nmsg))
			return 0;


		*read_pos -= uret;

		memmove(buf, buf + uret, *read_pos); //сдвиг содержимого буфера к началу
	}

	return 1;
}