blob: 3d9aeb2ba1715dc4b418175f23ae9a94d4ff9863 [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
109bool readFileRenameRqst(unsigned int *pOldNameSize, unsigned int *pNewNameSize,
110 char *pOldName, char *pNewName)
111{
112 return false;
113}
114
Dennis Syrovatskya10c2762005-12-18 15:58:33 +0000115bool
116SFTMsgReader::readFileDownloadCancel(unsigned int *pReasonSize, char *pReason)
Dennis Syrovatsky613f7c82005-12-18 14:24:06 +0000117{
Dennis Syrovatskya10c2762005-12-18 15:58:33 +0000118 return readU8U16StringMsg(pReasonSize, pReason);
Dennis Syrovatsky613f7c82005-12-18 14:24:06 +0000119}
120
Dennis Syrovatskya10c2762005-12-18 15:58:33 +0000121bool
122SFTMsgReader::readFileUploadFailed(unsigned int *pReasonSize, char *pReason)
Dennis Syrovatsky613f7c82005-12-18 14:24:06 +0000123{
Dennis Syrovatskya10c2762005-12-18 15:58:33 +0000124 return readU8U16StringMsg(pReasonSize, pReason);
Dennis Syrovatsky613f7c82005-12-18 14:24:06 +0000125}
126
127bool
128SFTMsgReader::readU8U16StringMsg(unsigned int *pReasonSize, char *pReason)
129{
Dennis Syrovatskya10c2762005-12-18 15:58:33 +0000130 m_pIS->skip(1);
131 unsigned int reasonSize = m_pIS->readU16();
132
133 if (reasonSize >= FT_FILENAME_SIZE) {
134 m_pIS->skip(reasonSize);
135 return false;
136 } else {
137 if (reasonSize == 0) {
138 pReason[0] = '\0';
139 } else {
140 m_pIS->readBytes(pReason, reasonSize);
141 pReason[reasonSize] = '\0';
142 }
143 *pReasonSize = reasonSize;
144 return true;
145 }
146}
147
148bool
149SFTMsgReader::readU8U16U32StringMsg(unsigned char *pU8, unsigned int *pU16,
150 unsigned int *pU32, char *pString)
151{
152 *pU8 = m_pIS->readU8();
153 unsigned int strSize = m_pIS->readU16();
154 *pU32 = m_pIS->readU32();
155
156 if (strSize >= FT_FILENAME_SIZE) {
157 m_pIS->skip(strSize);
158 return false;
159 } else {
160 *pU16 = strSize;
161 if (strSize == 0) {
162 pString[0] = '\0';
163 } else {
164 m_pIS->readBytes(pString, strSize);
165 pString[strSize] = '\0';
166 }
167 return true;
168 }
Dennis Syrovatsky613f7c82005-12-18 14:24:06 +0000169}