2 Copyright 2007 Chris Tallon
4 This file is part of VOMP.
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.
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.
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.
21 #include "vdrresponsepacket.h"
25 VDR_ResponsePacket::VDR_ResponsePacket()
40 VDR_ResponsePacket::~VDR_ResponsePacket()
42 if (!ownBlock) return; // don't free if it's a getblock
44 if (userData) free(userData);
47 void VDR_ResponsePacket::setResponse(ULONG trequestID, UCHAR* tuserData, ULONG tuserDataLength)
49 channelID = VDR::CHANNEL_REQUEST_RESPONSE;
50 requestID = trequestID;
52 userDataLength = tuserDataLength;
55 void VDR_ResponsePacket::setStream(ULONG tstreamID, ULONG tflag, UCHAR* tuserData, ULONG tuserDataLength, ULONG tchannelID)
57 channelID = tchannelID;
61 userDataLength = tuserDataLength;
64 bool VDR_ResponsePacket::end()
66 return (packetPos >= userDataLength);
69 void VDR_ResponsePacket::dumpUD()
71 // FIXME TODO - use generic stdout hex printer for userData, userDataLength
74 int VDR_ResponsePacket::serverError()
76 if ((packetPos == 0) && (userDataLength == 4) && !ntohl(*(ULONG*)userData)) return 1;
80 std::string VDR_ResponsePacket::extractStdString()
82 if (serverError()) return NULL;
84 int length = strlen((char*)&userData[packetPos]);
85 if ((packetPos + length) > userDataLength) return std::string("");
86 const char * dest_str=(char*)&userData[packetPos];
87 packetPos += length + 1;
91 char* VDR_ResponsePacket::extractString()
93 if (serverError()) return NULL;
95 int length = strlen((char*)&userData[packetPos]);
96 if ((packetPos + length) > userDataLength) return NULL;
97 char* str = new char[length + 1];
98 strcpy(str, (char*)&userData[packetPos]);
99 packetPos += length + 1;
103 UCHAR VDR_ResponsePacket::extractUCHAR()
105 if ((packetPos + sizeof(UCHAR)) > userDataLength) return 0;
106 UCHAR uc = userData[packetPos];
107 packetPos += sizeof(UCHAR);
111 ULONG VDR_ResponsePacket::extractULONG()
113 if ((packetPos + sizeof(ULONG)) > userDataLength) return 0;
114 ULONG ul = userData[packetPos++]<<24;
115 ul|= userData[packetPos++]<<16;
116 ul|= userData[packetPos++]<<8;
117 ul|= userData[packetPos++];
118 //packetPos += sizeof(ULONG);
122 ULLONG VDR_ResponsePacket::extractULLONG()
124 if ((packetPos + sizeof(ULLONG)) > userDataLength) return 0;
125 ULLONG ull= ((ULLONG)userData[packetPos++])<<56;
126 ull|= ((ULLONG)userData[packetPos++])<<48;
127 ull|= ((ULLONG)userData[packetPos++])<<40;
128 ull|= ((ULLONG)userData[packetPos++])<<32;
129 ull|= ((ULLONG)userData[packetPos++])<<24;
130 ull|= ((ULLONG)userData[packetPos++])<<16;
131 ull|= ((ULLONG)userData[packetPos++])<<8;
132 ull|= ((ULLONG)userData[packetPos++]);
136 double VDR_ResponsePacket::extractdouble()
138 if ((packetPos + sizeof(ULLONG)) > userDataLength) return 0;
139 ULLONG ull = extractULLONG();
141 memcpy(&d,&ull,sizeof(double));
145 long VDR_ResponsePacket::extractLONG()
147 if ((packetPos + sizeof(long)) > userDataLength) return 0;
148 long l = userData[packetPos++]<<24;
149 l|= userData[packetPos++]<<16;
150 l|= userData[packetPos++]<<8;
151 l|= userData[packetPos++];
155 UCHAR* VDR_ResponsePacket::getUserData()