forked from FernetMenta/vdr-plugin-vnsiserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
requestpacket.c
143 lines (128 loc) · 3.51 KB
/
requestpacket.c
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
/*
* vdr-plugin-vnsi - KODI server plugin for VDR
*
* Copyright (C) 2007 Chris Tallon
* Copyright (C) 2010 Alwin Esch (Team XBMC)
* Copyright (C) 2010, 2011 Alexander Pipelka
* Copyright (C) 2015 Team KODI
*
* http://kodi.tv
*
* 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; either version 2, or (at your option)
* any later version.
*
* 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 KODI; see the file COPYING. If not, see
* <http://www.gnu.org/licenses/>.
*
*/
#include "requestpacket.h"
#include "vnsicommand.h"
#include "config.h"
#include <stdlib.h>
#include <string.h>
#ifndef __FreeBSD__
#include <asm/byteorder.h>
#else
#include <sys/endian.h>
#define __be64_to_cpu be64toh
#define __cpu_to_be64 htobe64
#endif
cRequestPacket::cRequestPacket(uint32_t requestID, uint32_t opcode, uint8_t* data, size_t dataLength)
: userData(data), userDataLength(dataLength), opCode(opcode), requestID(requestID)
{
packetPos = 0;
channelID = 0;
streamID = 0;
flag = 0;
}
cRequestPacket::~cRequestPacket()
{
delete[] userData;
}
bool cRequestPacket::end() const
{
return (packetPos >= userDataLength);
}
char* cRequestPacket::extract_String()
{
char *p = (char *)&userData[packetPos];
const char *end = (const char *)memchr(p, '\0', userDataLength - packetPos);
if (end == NULL)
/* string is not terminated - fail */
throw MalformedVNSIPacket();
int length = end - p;
packetPos += length + 1;
return p;
}
uint8_t cRequestPacket::extract_U8()
{
if ((packetPos + sizeof(uint8_t)) > userDataLength)
throw MalformedVNSIPacket();
uint8_t uc = userData[packetPos];
packetPos += sizeof(uint8_t);
return uc;
}
uint32_t cRequestPacket::extract_U32()
{
if ((packetPos + sizeof(uint32_t)) > userDataLength)
throw MalformedVNSIPacket();
uint32_t ul;
memcpy(&ul, &userData[packetPos], sizeof(uint32_t));
ul = ntohl(ul);
packetPos += sizeof(uint32_t);
return ul;
}
uint64_t cRequestPacket::extract_U64()
{
if ((packetPos + sizeof(uint64_t)) > userDataLength)
throw MalformedVNSIPacket();
uint64_t ull;
memcpy(&ull, &userData[packetPos], sizeof(uint64_t));
ull = __be64_to_cpu(ull);
packetPos += sizeof(uint64_t);
return ull;
}
int64_t cRequestPacket::extract_S64()
{
if ((packetPos + sizeof(int64_t)) > userDataLength)
throw MalformedVNSIPacket();
int64_t ll;
memcpy(&ll, &userData[packetPos], sizeof(int64_t));
ll = __be64_to_cpu(ll);
packetPos += sizeof(int64_t);
return ll;
}
double cRequestPacket::extract_Double()
{
if ((packetPos + sizeof(uint64_t)) > userDataLength)
throw MalformedVNSIPacket();
uint64_t ull;
memcpy(&ull, &userData[packetPos], sizeof(uint64_t));
ull = __be64_to_cpu(ull);
double d;
memcpy(&d, &ull, sizeof(double));
packetPos += sizeof(uint64_t);
return d;
}
int32_t cRequestPacket::extract_S32()
{
if ((packetPos + sizeof(int32_t)) > userDataLength)
throw MalformedVNSIPacket();
int32_t l;
memcpy(&l, &userData[packetPos], sizeof(int32_t));
l = ntohl(l);
packetPos += sizeof(int32_t);
return l;
}
uint8_t* cRequestPacket::getData()
{
return userData;
}