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