blob: 2a5d283e8e6eaecd8c372104832d72a1843514d1 [file] [log] [blame]
Dennis Syrovatsky613f7c82005-12-18 14:24:06 +00001/* Copyright (C) 2005 TightVNC Team. All Rights Reserved.
2 *
3 * This is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This software is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16 * USA.
17 *
18 * TightVNC distribution homepage on the Web: http://www.tightvnc.com/
19 *
20 */
21
22// -=- SFTMsgReader.cxx
23
24#include <rfb/SFTMsgReader.h>
25
26using namespace rfb;
27
28SFTMsgReader::SFTMsgReader(rdr::InStream *pIS)
29{
30 m_pIS = pIS;
31}
32
33SFTMsgReader::~SFTMsgReader()
34{
35}
36
37bool
38SFTMsgReader::readFileListRqst(unsigned int *pDirNameSize, char *pDirName,
Dennis Syrovatskya10c2762005-12-18 15:58:33 +000039 unsigned int *pFlags)
Dennis Syrovatsky613f7c82005-12-18 14:24:06 +000040{
Dennis Syrovatskya10c2762005-12-18 15:58:33 +000041 *pFlags = m_pIS->readU8();
42 unsigned int dirNameSize = m_pIS->readU16();
43
44 if (dirNameSize >= FT_FILENAME_SIZE) {
45 m_pIS->skip(dirNameSize);
46 return false;
47 } else {
48 m_pIS->readBytes(pDirName, dirNameSize);
49 *pDirNameSize = dirNameSize;
50 pDirName[dirNameSize] = '\0';
51 return true;
52 }
Dennis Syrovatsky613f7c82005-12-18 14:24:06 +000053}
54
55bool
56SFTMsgReader::readFileDownloadRqst(unsigned int *pFilenameSize, char *pFilename,
57 unsigned int *pPosition)
58{
Dennis Syrovatskya10c2762005-12-18 15:58:33 +000059 unsigned char compressedLevel = 0;
60 return readU8U16U32StringMsg(&compressedLevel, pFilenameSize, pPosition, pFilename);
Dennis Syrovatsky613f7c82005-12-18 14:24:06 +000061}
62
63bool
64SFTMsgReader::readFileUploadRqst(unsigned int *pFilenameSize, char *pFilename,
65 unsigned int *pPosition)
66{
Dennis Syrovatskya10c2762005-12-18 15:58:33 +000067 unsigned char compressedLevel = 0;
68 return readU8U16U32StringMsg(&compressedLevel, pFilenameSize, pPosition, pFilename);
Dennis Syrovatsky613f7c82005-12-18 14:24:06 +000069}
70
71void *
72SFTMsgReader::readFileUploadData(unsigned int *pDataSize, unsigned int *pModTime)
73{
Dennis Syrovatskya10c2762005-12-18 15:58:33 +000074 unsigned char compressedLevel = m_pIS->readU8();
75 unsigned int realSize = m_pIS->readU16();
76 unsigned int compressedSize = m_pIS->readU16();
77
78 if ((realSize == 0) && (compressedSize == 0)) {
79 *pDataSize = 0;
80 *pModTime = m_pIS->readU32();
81 return NULL;
82 } else {
83 char *pData = new char [compressedSize];
84 m_pIS->readBytes(pData, compressedSize);
85 *pDataSize = compressedSize;
86 *pModTime = 0;
87 return pData;
88 }
Dennis Syrovatsky613f7c82005-12-18 14:24:06 +000089}
90
91bool
92SFTMsgReader::readFileCreateDirRqst(unsigned int *pDirNameSize, char *pDirName)
93{
Dennis Syrovatskya10c2762005-12-18 15:58:33 +000094 return readU8U16StringMsg(pDirNameSize, pDirName);
Dennis Syrovatsky613f7c82005-12-18 14:24:06 +000095}
96
97bool
98SFTMsgReader::readFileDirSizeRqst(unsigned int *pDirNameSize, char *pDirName)
99{
Dennis Syrovatskya10c2762005-12-18 15:58:33 +0000100 return readU8U16StringMsg(pDirNameSize, pDirName);
Dennis Syrovatsky613f7c82005-12-18 14:24:06 +0000101}
102
103bool
104SFTMsgReader::readFileDeleteRqst(unsigned int *pNameSize, char *pName)
105{
Dennis Syrovatskya10c2762005-12-18 15:58:33 +0000106 return readU8U16StringMsg(pNameSize, pName);
Dennis Syrovatsky613f7c82005-12-18 14:24:06 +0000107}
108
Dennis Syrovatskyf72f9fd2006-04-17 08:56:48 +0000109bool
110SFTMsgReader::readFileRenameRqst(unsigned int *pOldNameSize,
111 unsigned int *pNewNameSize,
112 char *pOldName, char *pNewName)
Dennis Syrovatsky613f7c82005-12-18 14:24:06 +0000113{
Dennis Syrovatskyf72f9fd2006-04-17 08:56:48 +0000114 m_pIS->skip(1);
115
116 unsigned int oldNameSize = m_pIS->readU16();
117 unsigned int newNameSize = m_pIS->readU16();
118
119 if ((oldNameSize >= *pOldNameSize) || (newNameSize >= *pNewNameSize)) {
120 m_pIS->skip(oldNameSize);
121 m_pIS->skip(newNameSize);
122 return false;
123 }
124
125 if (oldNameSize != 0) {
126 m_pIS->readBytes(pOldName, oldNameSize);
127 pOldName[oldNameSize] = '\0';
128 *pOldNameSize = oldNameSize;
129 } else {
130 *pOldNameSize = 0;
131 pOldName[0] = '\0';
132 }
133
134 if (newNameSize != 0) {
135 m_pIS->readBytes(pNewName, newNameSize);
136 pNewName[newNameSize] = '\0';
137 } else {
138 *pNewNameSize = 0;
139 pNewName[0] = '\0';
140 }
141
142 return true;
Dennis Syrovatsky613f7c82005-12-18 14:24:06 +0000143}
144
Dennis Syrovatskya10c2762005-12-18 15:58:33 +0000145bool
146SFTMsgReader::readFileDownloadCancel(unsigned int *pReasonSize, char *pReason)
Dennis Syrovatsky613f7c82005-12-18 14:24:06 +0000147{
Dennis Syrovatskya10c2762005-12-18 15:58:33 +0000148 return readU8U16StringMsg(pReasonSize, pReason);
Dennis Syrovatsky613f7c82005-12-18 14:24:06 +0000149}
150
Dennis Syrovatskya10c2762005-12-18 15:58:33 +0000151bool
152SFTMsgReader::readFileUploadFailed(unsigned int *pReasonSize, char *pReason)
Dennis Syrovatsky613f7c82005-12-18 14:24:06 +0000153{
Dennis Syrovatskya10c2762005-12-18 15:58:33 +0000154 return readU8U16StringMsg(pReasonSize, pReason);
Dennis Syrovatsky613f7c82005-12-18 14:24:06 +0000155}
156
157bool
158SFTMsgReader::readU8U16StringMsg(unsigned int *pReasonSize, char *pReason)
159{
Dennis Syrovatskya10c2762005-12-18 15:58:33 +0000160 m_pIS->skip(1);
161 unsigned int reasonSize = m_pIS->readU16();
162
163 if (reasonSize >= FT_FILENAME_SIZE) {
164 m_pIS->skip(reasonSize);
165 return false;
166 } else {
167 if (reasonSize == 0) {
168 pReason[0] = '\0';
169 } else {
170 m_pIS->readBytes(pReason, reasonSize);
171 pReason[reasonSize] = '\0';
172 }
173 *pReasonSize = reasonSize;
174 return true;
175 }
176}
177
178bool
179SFTMsgReader::readU8U16U32StringMsg(unsigned char *pU8, unsigned int *pU16,
180 unsigned int *pU32, char *pString)
181{
182 *pU8 = m_pIS->readU8();
183 unsigned int strSize = m_pIS->readU16();
184 *pU32 = m_pIS->readU32();
185
186 if (strSize >= FT_FILENAME_SIZE) {
187 m_pIS->skip(strSize);
188 return false;
189 } else {
190 *pU16 = strSize;
191 if (strSize == 0) {
192 pString[0] = '\0';
193 } else {
194 m_pIS->readBytes(pString, strSize);
195 pString[strSize] = '\0';
196 }
197 return true;
198 }
Dennis Syrovatsky613f7c82005-12-18 14:24:06 +0000199}