blob: cd792154daa88b3c2c7469fb678adb3479544208 [file] [log] [blame]
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001/* Copyright (C) 2005 TightVNC Team. All Rights Reserved.
2 *
3 * Developed by Dennis Syrovatsky.
4 *
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 *
Peter Åstrand7877cd62009-02-25 16:15:48 +000020 *
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000021 *
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,
41 unsigned int *pFlags)
42{
43 *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 }
55}
56
57bool
58SFTMsgReader::readFileDownloadRqst(unsigned int *pFilenameSize, char *pFilename,
59 unsigned int *pPosition)
60{
61 unsigned char compressedLevel = 0;
62 return readU8U16U32StringMsg(&compressedLevel, pFilenameSize, pPosition, pFilename);
63}
64
65bool
66SFTMsgReader::readFileUploadRqst(unsigned int *pFilenameSize, char *pFilename,
67 unsigned int *pPosition)
68{
69 unsigned char compressedLevel = 0;
70 return readU8U16U32StringMsg(&compressedLevel, pFilenameSize, pPosition, pFilename);
71}
72
73char *
74SFTMsgReader::readFileUploadData(unsigned int *pDataSize, unsigned int *pModTime)
75{
Adam Tkacfee32e32008-10-10 15:48:22 +000076 /*
77 * Compressed level is not used now so we have to skip one byte
78 *
79 * unsigned char compressedLevel = m_pIS->readU8();
80 */
81 (void) m_pIS->readU8();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000082 unsigned int realSize = m_pIS->readU16();
83 unsigned int compressedSize = m_pIS->readU16();
84
85 if ((realSize == 0) && (compressedSize == 0)) {
86 *pDataSize = 0;
87 *pModTime = m_pIS->readU32();
88 return NULL;
89 } else {
90 char *pData = new char [compressedSize];
91 m_pIS->readBytes(pData, compressedSize);
92 *pDataSize = compressedSize;
93 *pModTime = 0;
94 return pData;
95 }
96}
97
98bool
99SFTMsgReader::readFileCreateDirRqst(unsigned int *pDirNameSize, char *pDirName)
100{
101 return readU8U16StringMsg(pDirNameSize, pDirName);
102}
103
104bool
105SFTMsgReader::readFileDirSizeRqst(unsigned int *pDirNameSize, char *pDirName)
106{
107 return readU8U16StringMsg(pDirNameSize, pDirName);
108}
109
110bool
111SFTMsgReader::readFileDeleteRqst(unsigned int *pNameSize, char *pName)
112{
113 return readU8U16StringMsg(pNameSize, pName);
114}
115
116bool
117SFTMsgReader::readFileRenameRqst(unsigned int *pOldNameSize,
118 unsigned int *pNewNameSize,
119 char *pOldName, char *pNewName)
120{
121 m_pIS->skip(1);
122
123 unsigned int oldNameSize = m_pIS->readU16();
124 unsigned int newNameSize = m_pIS->readU16();
125
126 if ((oldNameSize >= *pOldNameSize) || (newNameSize >= *pNewNameSize)) {
127 m_pIS->skip(oldNameSize);
128 m_pIS->skip(newNameSize);
129 return false;
130 }
131
132 if (oldNameSize != 0) {
133 m_pIS->readBytes(pOldName, oldNameSize);
134 pOldName[oldNameSize] = '\0';
135 *pOldNameSize = oldNameSize;
136 } else {
137 *pOldNameSize = 0;
138 pOldName[0] = '\0';
139 }
140
141 if (newNameSize != 0) {
142 m_pIS->readBytes(pNewName, newNameSize);
143 pNewName[newNameSize] = '\0';
144 } else {
145 *pNewNameSize = 0;
146 pNewName[0] = '\0';
147 }
148
149 return true;
150}
151
152bool
153SFTMsgReader::readFileDownloadCancel(unsigned int *pReasonSize, char *pReason)
154{
155 return readU8U16StringMsg(pReasonSize, pReason);
156}
157
158bool
159SFTMsgReader::readFileUploadFailed(unsigned int *pReasonSize, char *pReason)
160{
161 return readU8U16StringMsg(pReasonSize, pReason);
162}
163
164bool
165SFTMsgReader::readU8U16StringMsg(unsigned int *pReasonSize, char *pReason)
166{
167 m_pIS->skip(1);
168 unsigned int reasonSize = m_pIS->readU16();
169
170 if (reasonSize >= FT_FILENAME_SIZE) {
171 m_pIS->skip(reasonSize);
172 return false;
173 } else {
174 if (reasonSize == 0) {
175 pReason[0] = '\0';
176 } else {
177 m_pIS->readBytes(pReason, reasonSize);
178 pReason[reasonSize] = '\0';
179 }
180 *pReasonSize = reasonSize;
181 return true;
182 }
183}
184
185bool
186SFTMsgReader::readU8U16U32StringMsg(unsigned char *pU8, unsigned int *pU16,
187 unsigned int *pU32, char *pString)
188{
189 *pU8 = m_pIS->readU8();
190 unsigned int strSize = m_pIS->readU16();
191 *pU32 = m_pIS->readU32();
192
193 if (strSize >= FT_FILENAME_SIZE) {
194 m_pIS->skip(strSize);
195 return false;
196 } else {
197 *pU16 = strSize;
198 if (strSize == 0) {
199 pString[0] = '\0';
200 } else {
201 m_pIS->readBytes(pString, strSize);
202 pString[strSize] = '\0';
203 }
204 return true;
205 }
206}