Coverage Report

Created: 2026-05-30 06:46

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/FreeRDP/channels/cliprdr/client/cliprdr_format.c
Line
Count
Source
1
/**
2
 * FreeRDP: A Remote Desktop Protocol Implementation
3
 * Clipboard Virtual Channel
4
 *
5
 * Copyright 2009-2011 Jay Sorg
6
 * Copyright 2010-2011 Vic Lee
7
 * Copyright 2015 Thincast Technologies GmbH
8
 * Copyright 2015 DI (FH) Martin Haimberger <martin.haimberger@thincast.com>
9
 *
10
 * Licensed under the Apache License, Version 2.0 (the "License");
11
 * you may not use this file except in compliance with the License.
12
 * You may obtain a copy of the License at
13
 *
14
 *     http://www.apache.org/licenses/LICENSE-2.0
15
 *
16
 * Unless required by applicable law or agreed to in writing, software
17
 * distributed under the License is distributed on an "AS IS" BASIS,
18
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
 * See the License for the specific language governing permissions and
20
 * limitations under the License.
21
 */
22
23
#include <freerdp/config.h>
24
25
#include <winpr/crt.h>
26
#include <winpr/print.h>
27
#include <winpr/clipboard.h>
28
29
#include <freerdp/types.h>
30
#include <freerdp/freerdp.h>
31
#include <freerdp/settings.h>
32
#include <freerdp/constants.h>
33
#include <freerdp/client/cliprdr.h>
34
35
#include "cliprdr_main.h"
36
#include "cliprdr_format.h"
37
#include "../cliprdr_common.h"
38
39
CLIPRDR_FORMAT_LIST cliprdr_filter_format_list(const CLIPRDR_FORMAT_LIST* list, UINT32 mask,
40
                                               UINT32 checkMask)
41
0
{
42
0
  const UINT32 maskData =
43
0
      checkMask & (CLIPRDR_FLAG_LOCAL_TO_REMOTE | CLIPRDR_FLAG_REMOTE_TO_LOCAL);
44
0
  const UINT32 maskFiles =
45
0
      checkMask & (CLIPRDR_FLAG_LOCAL_TO_REMOTE_FILES | CLIPRDR_FLAG_REMOTE_TO_LOCAL_FILES);
46
0
  WINPR_ASSERT(list);
47
48
0
  CLIPRDR_FORMAT_LIST filtered = WINPR_C_ARRAY_INIT;
49
0
  filtered.common.msgType = CB_FORMAT_LIST;
50
0
  filtered.numFormats = list->numFormats;
51
0
  if (filtered.numFormats > 0)
52
0
  {
53
0
    filtered.formats = calloc(filtered.numFormats, sizeof(CLIPRDR_FORMAT));
54
0
    if (!filtered.formats)
55
0
    {
56
0
      const CLIPRDR_FORMAT_LIST empty = WINPR_C_ARRAY_INIT;
57
0
      return empty;
58
0
    }
59
0
  }
60
61
0
  size_t wpos = 0;
62
0
  if ((mask & checkMask) == checkMask)
63
0
  {
64
0
    for (size_t x = 0; x < list->numFormats; x++)
65
0
    {
66
0
      const CLIPRDR_FORMAT* format = &list->formats[x];
67
0
      CLIPRDR_FORMAT* cur = &filtered.formats[x];
68
0
      cur->formatId = format->formatId;
69
0
      if (format->formatName)
70
0
        cur->formatName = _strdup(format->formatName);
71
0
      wpos++;
72
0
    }
73
0
  }
74
0
  else if ((mask & maskFiles) != 0)
75
0
  {
76
0
    for (size_t x = 0; x < list->numFormats; x++)
77
0
    {
78
0
      const CLIPRDR_FORMAT* format = &list->formats[x];
79
0
      CLIPRDR_FORMAT* cur = &filtered.formats[wpos];
80
81
0
      if (!format->formatName)
82
0
        continue;
83
0
      if (strcmp(format->formatName, type_FileGroupDescriptorW) == 0 ||
84
0
          strcmp(format->formatName, type_FileContents) == 0)
85
0
      {
86
0
        cur->formatId = format->formatId;
87
0
        cur->formatName = _strdup(format->formatName);
88
0
        wpos++;
89
0
      }
90
0
    }
91
0
  }
92
0
  else if ((mask & maskData) != 0)
93
0
  {
94
0
    for (size_t x = 0; x < list->numFormats; x++)
95
0
    {
96
0
      const CLIPRDR_FORMAT* format = &list->formats[x];
97
0
      CLIPRDR_FORMAT* cur = &filtered.formats[wpos];
98
99
0
      if (!format->formatName ||
100
0
          (strcmp(format->formatName, type_FileGroupDescriptorW) != 0 &&
101
0
           strcmp(format->formatName, type_FileContents) != 0))
102
0
      {
103
0
        cur->formatId = format->formatId;
104
0
        if (format->formatName)
105
0
          cur->formatName = _strdup(format->formatName);
106
0
        wpos++;
107
0
      }
108
0
    }
109
0
  }
110
0
  WINPR_ASSERT(wpos <= UINT32_MAX);
111
0
  filtered.numFormats = (UINT32)wpos;
112
0
  return filtered;
113
0
}
114
115
/**
116
 * Function description
117
 *
118
 * @return 0 on success, otherwise a Win32 error code
119
 */
120
UINT cliprdr_process_format_list(cliprdrPlugin* cliprdr, wStream* s, UINT32 dataLen,
121
                                 UINT16 msgFlags)
122
0
{
123
0
  CLIPRDR_FORMAT_LIST formatList = WINPR_C_ARRAY_INIT;
124
0
  CLIPRDR_FORMAT_LIST filteredFormatList = WINPR_C_ARRAY_INIT;
125
0
  CliprdrClientContext* context = cliprdr_get_client_interface(cliprdr);
126
0
  UINT error = CHANNEL_RC_OK;
127
128
0
  formatList.common.msgType = CB_FORMAT_LIST;
129
0
  formatList.common.msgFlags = msgFlags;
130
0
  formatList.common.dataLen = dataLen;
131
132
0
  if ((error =
133
0
           cliprdr_read_format_list(cliprdr->log, s, &formatList, cliprdr->useLongFormatNames)))
134
0
    goto error_out;
135
136
0
  {
137
0
    const UINT32 mask = freerdp_settings_get_uint32(context->rdpcontext->settings,
138
0
                                                    FreeRDP_ClipboardFeatureMask);
139
0
    filteredFormatList = cliprdr_filter_format_list(
140
0
        &formatList, mask, CLIPRDR_FLAG_REMOTE_TO_LOCAL | CLIPRDR_FLAG_REMOTE_TO_LOCAL_FILES);
141
0
  }
142
143
0
  if (filteredFormatList.numFormats == 0)
144
0
    goto error_out;
145
146
0
  {
147
0
    const DWORD level = WLOG_DEBUG;
148
0
    if (WLog_IsLevelActive(cliprdr->log, level))
149
0
    {
150
0
      WLog_Print(cliprdr->log, level, "ServerFormatList: numFormats: %" PRIu32 "",
151
0
                 formatList.numFormats);
152
0
      for (size_t x = 0; x < formatList.numFormats; x++)
153
0
      {
154
0
        const CLIPRDR_FORMAT* format = &formatList.formats[x];
155
0
        WLog_Print(cliprdr->log, level, "[%" PRIuz "]: id=0x%08" PRIx32 " [%s|%s]", x,
156
0
                   format->formatId, ClipboardGetFormatIdString(format->formatId),
157
0
                   format->formatName);
158
0
      }
159
160
0
      WLog_Print(cliprdr->log, level, "ServerFormatList [filtered]: numFormats: %" PRIu32 "",
161
0
                 filteredFormatList.numFormats);
162
0
      for (size_t x = 0; x < filteredFormatList.numFormats; x++)
163
0
      {
164
0
        const CLIPRDR_FORMAT* format = &filteredFormatList.formats[x];
165
0
        WLog_Print(cliprdr->log, level, "[%" PRIuz "]: id=0x%08" PRIx32 " [%s|%s]", x,
166
0
                   format->formatId, ClipboardGetFormatIdString(format->formatId),
167
0
                   format->formatName);
168
0
      }
169
0
    }
170
0
  }
171
172
0
  if (context->ServerFormatList)
173
0
  {
174
0
    if ((error = context->ServerFormatList(context, &filteredFormatList)))
175
0
      WLog_Print(cliprdr->log, WLOG_ERROR, "ServerFormatList failed with error %" PRIu32 "",
176
0
                 error);
177
0
  }
178
179
0
error_out:
180
0
  cliprdr_free_format_list(&filteredFormatList);
181
0
  cliprdr_free_format_list(&formatList);
182
0
  return error;
183
0
}
184
185
/**
186
 * Function description
187
 *
188
 * @return 0 on success, otherwise a Win32 error code
189
 */
190
UINT cliprdr_process_format_list_response(cliprdrPlugin* cliprdr, WINPR_ATTR_UNUSED wStream* s,
191
                                          UINT32 dataLen, UINT16 msgFlags)
192
0
{
193
0
  CLIPRDR_FORMAT_LIST_RESPONSE formatListResponse = WINPR_C_ARRAY_INIT;
194
0
  CliprdrClientContext* context = cliprdr_get_client_interface(cliprdr);
195
0
  UINT error = CHANNEL_RC_OK;
196
197
0
  WLog_Print(cliprdr->log, WLOG_DEBUG, "ServerFormatListResponse");
198
199
0
  formatListResponse.common.msgType = CB_FORMAT_LIST_RESPONSE;
200
0
  formatListResponse.common.msgFlags = msgFlags;
201
0
  formatListResponse.common.dataLen = dataLen;
202
203
0
  IFCALLRET(context->ServerFormatListResponse, error, context, &formatListResponse);
204
0
  if (error)
205
0
    WLog_Print(cliprdr->log, WLOG_ERROR,
206
0
               "ServerFormatListResponse failed with error %" PRIu32 "!", error);
207
208
0
  return error;
209
0
}
210
211
/**
212
 * Function description
213
 *
214
 * @return 0 on success, otherwise a Win32 error code
215
 */
216
UINT cliprdr_process_format_data_request(cliprdrPlugin* cliprdr, wStream* s, UINT32 dataLen,
217
                                         UINT16 msgFlags)
218
0
{
219
0
  CLIPRDR_FORMAT_DATA_REQUEST formatDataRequest = WINPR_C_ARRAY_INIT;
220
0
  CliprdrClientContext* context = cliprdr_get_client_interface(cliprdr);
221
0
  UINT error = CHANNEL_RC_OK;
222
223
0
  formatDataRequest.common.msgType = CB_FORMAT_DATA_REQUEST;
224
0
  formatDataRequest.common.msgFlags = msgFlags;
225
0
  formatDataRequest.common.dataLen = dataLen;
226
227
0
  if ((error = cliprdr_read_format_data_request(s, &formatDataRequest)))
228
0
    return error;
229
230
0
  WLog_Print(cliprdr->log, WLOG_DEBUG, "ServerFormatDataRequest (0x%08" PRIx32 " [%s])",
231
0
             formatDataRequest.requestedFormatId,
232
0
             ClipboardGetFormatIdString(formatDataRequest.requestedFormatId));
233
234
0
  const UINT32 mask =
235
0
      freerdp_settings_get_uint32(context->rdpcontext->settings, FreeRDP_ClipboardFeatureMask);
236
0
  if ((mask & (CLIPRDR_FLAG_LOCAL_TO_REMOTE | CLIPRDR_FLAG_LOCAL_TO_REMOTE_FILES)) == 0)
237
0
  {
238
0
    return cliprdr_send_error_response(cliprdr, CB_FORMAT_DATA_RESPONSE);
239
0
  }
240
241
0
  context->lastRequestedFormatId = formatDataRequest.requestedFormatId;
242
0
  IFCALLRET(context->ServerFormatDataRequest, error, context, &formatDataRequest);
243
0
  if (error)
244
0
    WLog_Print(cliprdr->log, WLOG_ERROR,
245
0
               "ServerFormatDataRequest failed with error %" PRIu32 "!", error);
246
247
0
  return error;
248
0
}
249
250
/**
251
 * Function description
252
 *
253
 * @return 0 on success, otherwise a Win32 error code
254
 */
255
UINT cliprdr_process_format_data_response(cliprdrPlugin* cliprdr, wStream* s, UINT32 dataLen,
256
                                          UINT16 msgFlags)
257
0
{
258
0
  CLIPRDR_FORMAT_DATA_RESPONSE formatDataResponse = WINPR_C_ARRAY_INIT;
259
0
  CliprdrClientContext* context = cliprdr_get_client_interface(cliprdr);
260
0
  UINT error = CHANNEL_RC_OK;
261
262
0
  WLog_Print(cliprdr->log, WLOG_DEBUG,
263
0
             "ServerFormatDataResponse: msgFlags=0x%08" PRIx32 ", dataLen=%" PRIu32, msgFlags,
264
0
             dataLen);
265
266
0
  formatDataResponse.common.msgType = CB_FORMAT_DATA_RESPONSE;
267
0
  formatDataResponse.common.msgFlags = msgFlags;
268
0
  formatDataResponse.common.dataLen = dataLen;
269
270
0
  if ((error = cliprdr_read_format_data_response(s, &formatDataResponse)))
271
0
    return error;
272
273
0
  const UINT32 mask =
274
0
      freerdp_settings_get_uint32(context->rdpcontext->settings, FreeRDP_ClipboardFeatureMask);
275
0
  if ((mask & (CLIPRDR_FLAG_REMOTE_TO_LOCAL | CLIPRDR_FLAG_REMOTE_TO_LOCAL_FILES)) == 0)
276
0
  {
277
0
    WLog_Print(cliprdr->log, WLOG_WARN,
278
0
               "Received ServerFormatDataResponse but remote -> local clipboard is disabled");
279
0
    return CHANNEL_RC_OK;
280
0
  }
281
282
0
  IFCALLRET(context->ServerFormatDataResponse, error, context, &formatDataResponse);
283
0
  if (error)
284
0
    WLog_Print(cliprdr->log, WLOG_ERROR,
285
0
               "ServerFormatDataResponse failed with error %" PRIu32 "!", error);
286
287
0
  return error;
288
0
}