blob: dfd0ad5949a78204345507631e1441e52a1ff98c [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// -=- SFTMsgWriter.cxx
23
24#include <rfb/SFTMsgWriter.h>
25
26using namespace rfb;
27
28SFTMsgWriter::SFTMsgWriter(rdr::OutStream *pOS)
29{
30 m_pOS = pOS;
31}
32
33SFTMsgWriter::~SFTMsgWriter()
34{
35}
36
37bool
38SFTMsgWriter::writeFileListData(unsigned char flags, rfb::FileInfo *pFileInfo)
39{
Dennis Syrovatsky3db3c482005-12-18 15:22:13 +000040 unsigned int numFiles = pFileInfo->getNumEntries();
41
42 m_pOS->writeU8(msgTypeFileListData);
43 m_pOS->writeU8(flags);
44 m_pOS->writeU16(numFiles);
45
46 if (numFiles == 0) {
47 m_pOS->writeU16(0);
48 m_pOS->writeU16(0);
49 } else {
50 unsigned int filenamesSize = pFileInfo->getFilenamesSize() + numFiles;
51
52 m_pOS->writeU16(filenamesSize);
53 m_pOS->writeU16(filenamesSize);
54
55 char *pFilenames = new char [filenamesSize];
56 unsigned int pos = 0;
57
58 for (unsigned int i = 0; i < numFiles; i++) {
59 char *pName = pFileInfo->getNameAt(i);
60 unsigned int len = strlen(pName);
61
62 memcpy((void *)&pFilenames[pos], pName, len + 1);
Dennis Syrovatskyf72f9fd2006-04-17 08:56:48 +000063 pos += (len + 1);
Dennis Syrovatsky3db3c482005-12-18 15:22:13 +000064
Dennis Syrovatskyf72f9fd2006-04-17 08:56:48 +000065 if (pFileInfo->getFlagsAt(i) & FT_ATTR_DIR) {
66 m_pOS->writeU32(FT_NET_ATTR_DIR);
67 } else {
68 m_pOS->writeU32(pFileInfo->getSizeAt(i));
69 }
Dennis Syrovatsky3db3c482005-12-18 15:22:13 +000070 m_pOS->writeU32(pFileInfo->getDataAt(i));
71 }
72
73 m_pOS->writeBytes(pFilenames, filenamesSize);
74
75 delete [] pFilenames;
76 }
77
78 m_pOS->flush();
79
80 return true;
Dennis Syrovatsky613f7c82005-12-18 14:24:06 +000081}
82
83bool
84SFTMsgWriter::writeFileDownloadData(unsigned int dataSize, void *pData)
85{
Dennis Syrovatskyed521352005-12-18 14:49:59 +000086 m_pOS->writeU8(msgTypeFileDownloadData);
87 m_pOS->writeU8(0);
88 m_pOS->writeU16(dataSize);
89 m_pOS->writeU16(dataSize);
90 m_pOS->writeBytes(pData, dataSize);
91 m_pOS->flush();
92 return true;
Dennis Syrovatsky613f7c82005-12-18 14:24:06 +000093}
94
95bool
96SFTMsgWriter::writeFileDownloadData(unsigned int modTime)
97{
Dennis Syrovatskyed521352005-12-18 14:49:59 +000098 m_pOS->writeU8(msgTypeFileDownloadData);
99 m_pOS->writeU8(0);
100 m_pOS->writeU16(0);
101 m_pOS->writeU16(0);
102 m_pOS->writeU32(modTime);
103 m_pOS->flush();
104 return true;
Dennis Syrovatsky613f7c82005-12-18 14:24:06 +0000105}
106
107bool
108SFTMsgWriter::writeFileUploadCancel(unsigned int reasonLen, char *pReason)
109{
Dennis Syrovatskyed521352005-12-18 14:49:59 +0000110 m_pOS->writeU8(msgTypeFileUploadCancel);
111 return writeU8U16StringMsg(0, reasonLen, pReason);
Dennis Syrovatsky613f7c82005-12-18 14:24:06 +0000112}
113
114bool
115SFTMsgWriter::writeFileDownloadFailed(unsigned int reasonLen, char *pReason)
116{
Dennis Syrovatskyed521352005-12-18 14:49:59 +0000117 m_pOS->writeU8(msgTypeFileDownloadFailed);
118 return writeU8U16StringMsg(0, reasonLen, pReason);
Dennis Syrovatsky613f7c82005-12-18 14:24:06 +0000119}
120
121bool
122SFTMsgWriter::writeFileDirSizeData(unsigned int dirSizeLow,
123 unsigned short dirSizeHigh)
124{
Dennis Syrovatskyed521352005-12-18 14:49:59 +0000125 m_pOS->writeU8(msgTypeFileDirSizeData);
126 m_pOS->writeU8(0);
127 m_pOS->writeU16(dirSizeHigh);
128 m_pOS->writeU32(dirSizeLow);
129 m_pOS->flush();
130 return true;
Dennis Syrovatsky613f7c82005-12-18 14:24:06 +0000131}
132
133bool
134SFTMsgWriter::writeFileLastRqstFailed(unsigned char lastRequest,
135 unsigned short reasonLen,
136 char *pReason)
137{
Dennis Syrovatskyed521352005-12-18 14:49:59 +0000138 m_pOS->writeU8(msgTypeFileLastRequestFailed);
139 return writeU8U16StringMsg(lastRequest, reasonLen, pReason);
Dennis Syrovatsky613f7c82005-12-18 14:24:06 +0000140}
141
142bool
143SFTMsgWriter::writeU8U16StringMsg(unsigned char p1, unsigned short p2, char *pP3)
144{
Dennis Syrovatskyed521352005-12-18 14:49:59 +0000145 m_pOS->writeU8(p1);
146 m_pOS->writeU16(p2);
147 m_pOS->writeBytes(pP3, p2);
148 m_pOS->flush();
149 return true;
Dennis Syrovatsky613f7c82005-12-18 14:24:06 +0000150}