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"
//returns the message size, or UINT_MAX if not enough bytes have been read to determine the size,
//or 0 if the data is invalid
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;
}
//Determines the type of the message of size size located in buffer buf, calls the corresponding handler and passes it the argument arg. Returns the value returned by the message handler. Returns 0 if the message type is unknown
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); //move the buffer contents to the beginning
}
return 1;
}
|