summaryrefslogtreecommitdiffstats
path: root/common/rfb/SFTMsgWriter.cxx
blob: fa6a82f9c38bd0b21cac963b3dbdb5f1fe09c2fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/* Copyright (C) 2005 TightVNC Team.  All Rights Reserved.
 *
 * Developed by Dennis Syrovatsky.
 *    
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this software; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
 * USA.
 *
 * TightVNC distribution homepage on the Web: http://www.tightvnc.com/
 *
 */

// -=- SFTMsgWriter.cxx

#include <rfb/SFTMsgWriter.h>

using namespace rfb;

SFTMsgWriter::SFTMsgWriter(rdr::OutStream *pOS)
{
  m_pOS = pOS;
}

SFTMsgWriter::~SFTMsgWriter()
{
}
    
bool 
SFTMsgWriter::writeFileListData(unsigned char flags, rfb::FileInfo *pFileInfo)
{
  unsigned int numFiles = pFileInfo->getNumEntries();

  m_pOS->writeU8(msgTypeFileListData);
  m_pOS->writeU8(flags);
  m_pOS->writeU16(numFiles);
  
  if (numFiles == 0) {
    m_pOS->writeU16(0);
    m_pOS->writeU16(0);
  } else {
    unsigned int filenamesSize = pFileInfo->getFilenamesSize() + numFiles;

    m_pOS->writeU16(filenamesSize);
    m_pOS->writeU16(filenamesSize);

    char *pFilenames = new char [filenamesSize];
    unsigned int pos = 0;

    for (unsigned int i = 0; i < numFiles; i++) {
      char *pName = pFileInfo->getNameAt(i);
      unsigned int len = strlen(pName);

      memcpy((void *)&pFilenames[pos], pName, len + 1);
      pos += (len + 1);

      if (pFileInfo->getFlagsAt(i) & FT_ATTR_DIR) {
        m_pOS->writeU32(FT_NET_ATTR_DIR);
      } else {
        m_pOS->writeU32(pFileInfo->getSizeAt(i));
      }
      m_pOS->writeU32(pFileInfo->getDataAt(i));
    }

    m_pOS->writeBytes(pFilenames, filenamesSize);

    delete [] pFilenames;
  }

  m_pOS->flush();

  return true;
}

bool 
SFTMsgWriter::writeFileDownloadData(unsigned int dataSize, void *pData)
{
  m_pOS->writeU8(msgTypeFileDownloadData);
  m_pOS->writeU8(0);
  m_pOS->writeU16(dataSize);
  m_pOS->writeU16(dataSize);
  m_pOS->writeBytes(pData, dataSize);
  m_pOS->flush();
  return true;
}

bool 
SFTMsgWriter::writeFileDownloadData(unsigned int modTime)
{
  m_pOS->writeU8(msgTypeFileDownloadData);
  m_pOS->writeU8(0);
  m_pOS->writeU16(0);
  m_pOS->writeU16(0);
  m_pOS->writeU32(modTime);
  m_pOS->flush();
  return true;
}

bool
SFTMsgWriter::writeFileUploadCancel(unsigned int reasonLen, char *pReason)
{
  m_pOS->writeU8(msgTypeFileUploadCancel);
  return writeU8U16StringMsg(0, reasonLen, pReason);
}

bool 
SFTMsgWriter::writeFileDownloadFailed(unsigned int reasonLen, char *pReason)
{
  m_pOS->writeU8(msgTypeFileDownloadFailed);
  return writeU8U16StringMsg(0, reasonLen, pReason);
}

bool 
SFTMsgWriter::writeFileDirSizeData(unsigned int dirSizeLow, 
                                   unsigned short dirSizeHigh)
{
  m_pOS->writeU8(msgTypeFileDirSizeData);
  m_pOS->writeU8(0);
  m_pOS->writeU16(dirSizeHigh);
  m_pOS->writeU32(dirSizeLow);
  m_pOS->flush();
  return true;
}

bool 
SFTMsgWriter::writeFileLastRqstFailed(unsigned char lastRequest, 
                                      unsigned short reasonLen,
                                      char *pReason)
{
  m_pOS->writeU8(msgTypeFileLastRequestFailed);
  return writeU8U16StringMsg(lastRequest, reasonLen, pReason);
}

bool
SFTMsgWriter::writeU8U16StringMsg(unsigned char p1, unsigned short p2, char *pP3)
{
  m_pOS->writeU8(p1);
  m_pOS->writeU16(p2);
  m_pOS->writeBytes(pP3, p2);
  m_pOS->flush();
  return true;
}