]> git.vomp.tv Git - vompclient.git/blob - vdrresponsepacket.cc
Fix text corruption in channel number display on live tv
[vompclient.git] / vdrresponsepacket.cc
1 /*
2     Copyright 2007 Chris Tallon
3
4     This file is part of VOMP.
5
6     VOMP is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     VOMP is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with VOMP; if not, write to the Free Software
18     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19 */
20
21 #include "vdrresponsepacket.h"
22
23 #include "vdr.h"
24 #include "tcp.h"
25
26 VDR_ResponsePacket::VDR_ResponsePacket()
27 {
28   userDataLength = 0;
29   packetPos = 0;
30   userData = NULL;
31   ownBlock = true;
32   
33   channelID = 0;
34   
35   requestID = 0;
36   streamID = 0;
37   
38   flag = 0;
39 }
40
41 VDR_ResponsePacket::~VDR_ResponsePacket()
42 {
43   if (!ownBlock) return; // don't free if it's a getblock
44   
45   if (userData) free(userData);
46 }
47
48 void VDR_ResponsePacket::setResponse(ULONG trequestID, UCHAR* tuserData, ULONG tuserDataLength)
49 {
50   channelID = VDR::CHANNEL_REQUEST_RESPONSE;
51   requestID = trequestID;
52   userData = tuserData;
53   userDataLength = tuserDataLength;
54 }
55
56 void VDR_ResponsePacket::setStream(ULONG tstreamID, ULONG tflag, UCHAR* tuserData, ULONG tuserDataLength, ULONG tchannelID)
57 {
58   channelID = tchannelID;
59   streamID = tstreamID;
60   flag = tflag;
61   userData = tuserData;
62   userDataLength = tuserDataLength;
63 }
64
65 bool VDR_ResponsePacket::end()
66 {
67   return (packetPos >= userDataLength);
68 }
69
70 void VDR_ResponsePacket::dumpUD()
71 {
72   TCP::dump(userData, userDataLength);
73 }
74
75 int VDR_ResponsePacket::serverError()
76 {
77   if ((packetPos == 0) && (userDataLength == 4) && !ntohl(*(ULONG*)userData)) return 1;
78   else return 0;
79 }
80
81 std::string  VDR_ResponsePacket::extractStdString()
82 {
83   if (serverError()) return NULL;
84
85   int length = strlen((char*)&userData[packetPos]);
86   if ((packetPos + length) > userDataLength) return std::string("");
87   const char * dest_str=(char*)&userData[packetPos];
88   packetPos += length + 1;
89   return dest_str;
90 }
91
92 char* VDR_ResponsePacket::extractString()
93 {
94   if (serverError()) return NULL;
95
96   int length = strlen((char*)&userData[packetPos]);
97   if ((packetPos + length) > userDataLength) return NULL;
98   char* str = new char[length + 1];
99   strcpy(str, (char*)&userData[packetPos]);
100   packetPos += length + 1;
101   return str;
102 }
103
104 UCHAR VDR_ResponsePacket::extractUCHAR()
105 {
106   if ((packetPos + sizeof(UCHAR)) > userDataLength) return 0;
107   UCHAR uc = userData[packetPos];
108   packetPos += sizeof(UCHAR);
109   return uc;
110 }
111
112 ULONG VDR_ResponsePacket::extractULONG()
113 {
114   if ((packetPos + sizeof(ULONG)) > userDataLength) return 0;
115   ULONG ul = userData[packetPos++]<<24;
116   ul|= userData[packetPos++]<<16;
117   ul|= userData[packetPos++]<<8;
118   ul|= userData[packetPos++];
119   //packetPos += sizeof(ULONG);
120   return ul;
121 }
122
123 ULLONG VDR_ResponsePacket::extractULLONG()
124 {
125   if ((packetPos + sizeof(ULLONG)) > userDataLength) return 0;
126   ULLONG ull= ((ULLONG)userData[packetPos++])<<56;
127   ull|= ((ULLONG)userData[packetPos++])<<48;
128   ull|= ((ULLONG)userData[packetPos++])<<40;
129   ull|= ((ULLONG)userData[packetPos++])<<32;
130   ull|= ((ULLONG)userData[packetPos++])<<24;
131   ull|= ((ULLONG)userData[packetPos++])<<16;
132   ull|= ((ULLONG)userData[packetPos++])<<8;
133   ull|= ((ULLONG)userData[packetPos++]);
134   return ull;
135 }
136
137 double VDR_ResponsePacket::extractdouble()
138 {
139   if ((packetPos + sizeof(ULLONG)) > userDataLength) return 0;
140   ULLONG ull = extractULLONG();
141   double d;
142   memcpy(&d,&ull,sizeof(double));
143   return d;
144 }
145
146 long VDR_ResponsePacket::extractLONG()
147 {
148   if ((packetPos + sizeof(long)) > userDataLength) return 0;
149   long l = userData[packetPos++]<<24;
150   l|= userData[packetPos++]<<16;
151   l|= userData[packetPos++]<<8;
152   l|= userData[packetPos++];
153   return l;
154 }
155
156 UCHAR* VDR_ResponsePacket::getUserData()
157 {
158   ownBlock = false;
159   return userData;
160 }
161