Coverage Report

Created: 2026-10-15 06:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/FreeRDP/channels/cliprdr/cliprdr_common.c
Line
Count
Source
1
/**
2
 * FreeRDP: A Remote Desktop Protocol Implementation
3
 * Cliprdr common
4
 *
5
 * Copyright 2013 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6
 * Copyright 2015 Thincast Technologies GmbH
7
 * Copyright 2015 DI (FH) Martin Haimberger <martin.haimberger@thincast.com>
8
 * Copyright 2019 Kobi Mizrachi <kmizrachi18@gmail.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 <winpr/crt.h>
24
#include <winpr/stream.h>
25
#include <freerdp/channels/log.h>
26
27
#define TAG CHANNELS_TAG("cliprdr.common")
28
29
#include "cliprdr_common.h"
30
31
static const char* CB_MSG_TYPE_STR(UINT32 type)
32
0
{
33
0
  switch (type)
34
0
  {
35
0
    case CB_TYPE_NONE:
36
0
      return "CB_TYPE_NONE";
37
0
    case CB_MONITOR_READY:
38
0
      return "CB_MONITOR_READY";
39
0
    case CB_FORMAT_LIST:
40
0
      return "CB_FORMAT_LIST";
41
0
    case CB_FORMAT_LIST_RESPONSE:
42
0
      return "CB_FORMAT_LIST_RESPONSE";
43
0
    case CB_FORMAT_DATA_REQUEST:
44
0
      return "CB_FORMAT_DATA_REQUEST";
45
0
    case CB_FORMAT_DATA_RESPONSE:
46
0
      return "CB_FORMAT_DATA_RESPONSE";
47
0
    case CB_TEMP_DIRECTORY:
48
0
      return "CB_TEMP_DIRECTORY";
49
0
    case CB_CLIP_CAPS:
50
0
      return "CB_CLIP_CAPS";
51
0
    case CB_FILECONTENTS_REQUEST:
52
0
      return "CB_FILECONTENTS_REQUEST";
53
0
    case CB_FILECONTENTS_RESPONSE:
54
0
      return "CB_FILECONTENTS_RESPONSE";
55
0
    case CB_LOCK_CLIPDATA:
56
0
      return "CB_LOCK_CLIPDATA";
57
0
    case CB_UNLOCK_CLIPDATA:
58
0
      return "CB_UNLOCK_CLIPDATA";
59
0
    default:
60
0
      return "UNKNOWN";
61
0
  }
62
0
}
63
64
const char* CB_MSG_TYPE_STRING(UINT16 type, char* buffer, size_t size)
65
0
{
66
0
  (void)_snprintf(buffer, size, "%s [0x%04" PRIx16 "]", CB_MSG_TYPE_STR(type), type);
67
0
  return buffer;
68
0
}
69
70
const char* CB_MSG_FLAGS_STRING(UINT16 msgFlags, char* buffer, size_t size)
71
0
{
72
0
  if ((msgFlags & CB_RESPONSE_OK) != 0)
73
0
    winpr_str_append("CB_RESPONSE_OK", buffer, size, "|");
74
0
  if ((msgFlags & CB_RESPONSE_FAIL) != 0)
75
0
    winpr_str_append("CB_RESPONSE_FAIL", buffer, size, "|");
76
0
  if ((msgFlags & CB_ASCII_NAMES) != 0)
77
0
    winpr_str_append("CB_ASCII_NAMES", buffer, size, "|");
78
79
0
  const size_t len = strnlen(buffer, size);
80
0
  if (!len)
81
0
    winpr_str_append("NONE", buffer, size, "");
82
83
0
  char val[32] = WINPR_C_ARRAY_INIT;
84
0
  (void)_snprintf(val, sizeof(val), "[0x%04" PRIx16 "]", msgFlags);
85
0
  winpr_str_append(val, buffer, size, "|");
86
0
  return buffer;
87
0
}
88
89
static BOOL cliprdr_validate_file_contents_request(const CLIPRDR_FILE_CONTENTS_REQUEST* request)
90
333
{
91
  /*
92
   * [MS-RDPECLIP] 2.2.5.3 File Contents Request PDU (CLIPRDR_FILECONTENTS_REQUEST).
93
   *
94
   * A request for the size of the file identified by the lindex field. The size MUST be
95
   * returned as a 64-bit, unsigned integer. The cbRequested field MUST be set to
96
   * 0x00000008 and both the nPositionLow and nPositionHigh fields MUST be
97
   * set to 0x00000000.
98
   */
99
100
333
  if (request->dwFlags & FILECONTENTS_SIZE)
101
265
  {
102
265
    if (request->cbRequested != sizeof(UINT64))
103
158
    {
104
158
      WLog_ERR(TAG, "cbRequested must be %" PRIuz ", got %" PRIu32 "", sizeof(UINT64),
105
158
               request->cbRequested);
106
158
      return FALSE;
107
158
    }
108
109
107
    if (request->nPositionHigh != 0 || request->nPositionLow != 0)
110
105
    {
111
105
      WLog_ERR(TAG, "nPositionHigh and nPositionLow must be set to 0");
112
105
      return FALSE;
113
105
    }
114
107
  }
115
116
70
  return TRUE;
117
333
}
118
119
wStream* cliprdr_packet_new(UINT16 msgType, UINT16 msgFlags, size_t dataLen)
120
0
{
121
0
  WINPR_ASSERT(dataLen < UINT32_MAX);
122
0
  wStream* s = Stream_New(nullptr, dataLen + 8ULL);
123
124
0
  if (!s)
125
0
  {
126
0
    WLog_ERR(TAG, "Stream_New failed!");
127
0
    return nullptr;
128
0
  }
129
130
0
  Stream_Write_UINT16(s, msgType);
131
0
  Stream_Write_UINT16(s, msgFlags);
132
  /* Write actual length after the entire packet has been constructed. */
133
0
  Stream_Write_UINT32(s, 0);
134
0
  return s;
135
0
}
136
137
static void cliprdr_write_file_contents_request(wStream* s,
138
                                                const CLIPRDR_FILE_CONTENTS_REQUEST* request)
139
0
{
140
0
  Stream_Write_UINT32(s, request->streamId);      /* streamId (4 bytes) */
141
0
  Stream_Write_UINT32(s, request->listIndex);     /* listIndex (4 bytes) */
142
0
  Stream_Write_UINT32(s, request->dwFlags);       /* dwFlags (4 bytes) */
143
0
  Stream_Write_UINT32(s, request->nPositionLow);  /* nPositionLow (4 bytes) */
144
0
  Stream_Write_UINT32(s, request->nPositionHigh); /* nPositionHigh (4 bytes) */
145
0
  Stream_Write_UINT32(s, request->cbRequested);   /* cbRequested (4 bytes) */
146
147
0
  if (request->haveClipDataId)
148
0
    Stream_Write_UINT32(s, request->clipDataId); /* clipDataId (4 bytes) */
149
0
}
150
151
static inline void cliprdr_write_lock_unlock_clipdata(wStream* s, UINT32 clipDataId)
152
0
{
153
0
  Stream_Write_UINT32(s, clipDataId);
154
0
}
155
156
static void cliprdr_write_lock_clipdata(wStream* s,
157
                                        const CLIPRDR_LOCK_CLIPBOARD_DATA* lockClipboardData)
158
0
{
159
0
  cliprdr_write_lock_unlock_clipdata(s, lockClipboardData->clipDataId);
160
0
}
161
162
static void cliprdr_write_unlock_clipdata(wStream* s,
163
                                          const CLIPRDR_UNLOCK_CLIPBOARD_DATA* unlockClipboardData)
164
0
{
165
0
  cliprdr_write_lock_unlock_clipdata(s, unlockClipboardData->clipDataId);
166
0
}
167
168
static void cliprdr_write_file_contents_response(wStream* s,
169
                                                 const CLIPRDR_FILE_CONTENTS_RESPONSE* response)
170
0
{
171
0
  Stream_Write_UINT32(s, response->streamId); /* streamId (4 bytes) */
172
0
  Stream_Write(s, response->requestedData, response->cbRequested);
173
0
}
174
175
wStream* cliprdr_packet_lock_clipdata_new(const CLIPRDR_LOCK_CLIPBOARD_DATA* lockClipboardData)
176
0
{
177
0
  wStream* s = nullptr;
178
179
0
  if (!lockClipboardData)
180
0
    return nullptr;
181
182
0
  s = cliprdr_packet_new(CB_LOCK_CLIPDATA, 0, 4);
183
184
0
  if (!s)
185
0
    return nullptr;
186
187
0
  cliprdr_write_lock_clipdata(s, lockClipboardData);
188
0
  return s;
189
0
}
190
191
wStream*
192
cliprdr_packet_unlock_clipdata_new(const CLIPRDR_UNLOCK_CLIPBOARD_DATA* unlockClipboardData)
193
0
{
194
0
  wStream* s = nullptr;
195
196
0
  if (!unlockClipboardData)
197
0
    return nullptr;
198
199
0
  s = cliprdr_packet_new(CB_UNLOCK_CLIPDATA, 0, 4);
200
201
0
  if (!s)
202
0
    return nullptr;
203
204
0
  cliprdr_write_unlock_clipdata(s, unlockClipboardData);
205
0
  return s;
206
0
}
207
208
wStream* cliprdr_packet_file_contents_request_new(const CLIPRDR_FILE_CONTENTS_REQUEST* request)
209
0
{
210
0
  wStream* s = nullptr;
211
212
0
  if (!request)
213
0
    return nullptr;
214
215
0
  s = cliprdr_packet_new(CB_FILECONTENTS_REQUEST, 0, 28);
216
217
0
  if (!s)
218
0
    return nullptr;
219
220
0
  cliprdr_write_file_contents_request(s, request);
221
0
  return s;
222
0
}
223
224
wStream* cliprdr_packet_file_contents_response_new(const CLIPRDR_FILE_CONTENTS_RESPONSE* response)
225
0
{
226
0
  wStream* s = nullptr;
227
228
0
  if (!response)
229
0
    return nullptr;
230
231
0
  s = cliprdr_packet_new(CB_FILECONTENTS_RESPONSE, response->common.msgFlags,
232
0
                         4 + response->cbRequested);
233
234
0
  if (!s)
235
0
    return nullptr;
236
237
0
  cliprdr_write_file_contents_response(s, response);
238
0
  return s;
239
0
}
240
241
wStream* cliprdr_packet_format_list_new(const CLIPRDR_FORMAT_LIST* formatList,
242
                                        BOOL useLongFormatNames, BOOL useAsciiNames)
243
0
{
244
0
  WINPR_ASSERT(formatList);
245
246
0
  if (formatList->common.msgType != CB_FORMAT_LIST)
247
0
    WLog_WARN(TAG, "called with invalid type %08" PRIx32, formatList->common.msgType);
248
249
0
  if (useLongFormatNames && useAsciiNames)
250
0
    WLog_WARN(TAG, "called with invalid arguments useLongFormatNames=true && "
251
0
                   "useAsciiNames=true. useAsciiNames requires "
252
0
                   "useLongFormatNames=false, ignoring argument.");
253
254
0
  const UINT32 length = formatList->numFormats * 36;
255
0
  const size_t formatNameCharSize =
256
0
      (useLongFormatNames || !useAsciiNames) ? sizeof(WCHAR) : sizeof(CHAR);
257
258
0
  wStream* s = cliprdr_packet_new(CB_FORMAT_LIST, 0, length);
259
0
  if (!s)
260
0
  {
261
0
    WLog_ERR(TAG, "cliprdr_packet_new failed!");
262
0
    return nullptr;
263
0
  }
264
265
0
  for (UINT32 index = 0; index < formatList->numFormats; index++)
266
0
  {
267
0
    const CLIPRDR_FORMAT* format = &(formatList->formats[index]);
268
269
0
    const char* szFormatName = format->formatName;
270
0
    size_t formatNameLength = 0;
271
0
    size_t formatNameStrLength = 0;
272
0
    if (szFormatName)
273
0
    {
274
0
      formatNameStrLength = strlen(szFormatName);
275
0
      const SSIZE_T wlen = ConvertUtf8ToWChar(szFormatName, nullptr, 0);
276
0
      if (wlen < 0)
277
0
        goto fail;
278
0
      formatNameLength = WINPR_ASSERTING_INT_CAST(size_t, wlen);
279
0
    }
280
281
0
    size_t formatNameMaxLength = formatNameLength + 1; /* Ensure '\0' termination in output */
282
0
    if (!Stream_EnsureRemainingCapacity(s,
283
0
                                        4 + MAX(32, formatNameMaxLength * formatNameCharSize)))
284
0
      goto fail;
285
286
0
    Stream_Write_UINT32(s, format->formatId); /* formatId (4 bytes) */
287
288
0
    if (!useLongFormatNames)
289
0
    {
290
0
      formatNameMaxLength = useAsciiNames ? 32 : 16;
291
0
      formatNameLength = MIN(formatNameMaxLength - 1, formatNameLength);
292
0
    }
293
294
0
    if (szFormatName && (formatNameLength > 0))
295
0
    {
296
0
      if (useAsciiNames)
297
0
      {
298
0
        Stream_Write(s, szFormatName, formatNameLength);
299
0
        Stream_Zero(s, formatNameMaxLength - formatNameLength);
300
0
      }
301
0
      else
302
0
      {
303
0
        const size_t formatNameWriteLength =
304
0
            MIN(formatNameStrLength, formatNameMaxLength - 1);
305
0
        if (Stream_Write_UTF16_String_From_UTF8(s, formatNameMaxLength - 1, szFormatName,
306
0
                                                formatNameWriteLength, TRUE) < 0)
307
0
          goto fail;
308
0
        Stream_Write_UINT16(s, 0);
309
0
      }
310
0
    }
311
0
    else
312
0
      Stream_Zero(s, formatNameMaxLength * formatNameCharSize);
313
0
  }
314
315
0
  return s;
316
317
0
fail:
318
0
  Stream_Free(s, TRUE);
319
0
  return nullptr;
320
0
}
321
322
UINT cliprdr_read_unlock_clipdata(wStream* s, CLIPRDR_UNLOCK_CLIPBOARD_DATA* unlockClipboardData)
323
42
{
324
42
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
325
3
    return ERROR_INVALID_DATA;
326
327
39
  Stream_Read_UINT32(s, unlockClipboardData->clipDataId); /* clipDataId (4 bytes) */
328
39
  return CHANNEL_RC_OK;
329
42
}
330
331
UINT cliprdr_read_format_data_request(wStream* s, CLIPRDR_FORMAT_DATA_REQUEST* request)
332
54
{
333
54
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
334
4
    return ERROR_INVALID_DATA;
335
336
50
  Stream_Read_UINT32(s, request->requestedFormatId); /* requestedFormatId (4 bytes) */
337
50
  return CHANNEL_RC_OK;
338
54
}
339
340
UINT cliprdr_read_format_data_response(wStream* s, CLIPRDR_FORMAT_DATA_RESPONSE* response)
341
23
{
342
23
  response->requestedFormatData = nullptr;
343
344
23
  if (!Stream_CheckAndLogRequiredLength(TAG, s, response->common.dataLen))
345
0
    return ERROR_INVALID_DATA;
346
347
23
  if (response->common.dataLen > 0)
348
22
  {
349
22
    response->requestedFormatData = Stream_ConstPointer(s);
350
22
    if (!Stream_SafeSeek(s, response->common.dataLen))
351
0
      return ERROR_INVALID_DATA;
352
22
  }
353
23
  return CHANNEL_RC_OK;
354
23
}
355
356
UINT cliprdr_read_file_contents_request(wStream* s, CLIPRDR_FILE_CONTENTS_REQUEST* request)
357
342
{
358
342
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 24))
359
9
    return ERROR_INVALID_DATA;
360
361
333
  request->haveClipDataId = FALSE;
362
333
  Stream_Read_UINT32(s, request->streamId);      /* streamId (4 bytes) */
363
333
  Stream_Read_UINT32(s, request->listIndex);     /* listIndex (4 bytes) */
364
333
  Stream_Read_UINT32(s, request->dwFlags);       /* dwFlags (4 bytes) */
365
333
  Stream_Read_UINT32(s, request->nPositionLow);  /* nPositionLow (4 bytes) */
366
333
  Stream_Read_UINT32(s, request->nPositionHigh); /* nPositionHigh (4 bytes) */
367
333
  Stream_Read_UINT32(s, request->cbRequested);   /* cbRequested (4 bytes) */
368
369
333
  if (Stream_GetRemainingLength(s) >= 4)
370
205
  {
371
205
    Stream_Read_UINT32(s, request->clipDataId); /* clipDataId (4 bytes) */
372
205
    request->haveClipDataId = TRUE;
373
205
  }
374
375
333
  if (!cliprdr_validate_file_contents_request(request))
376
263
    return ERROR_BAD_ARGUMENTS;
377
378
70
  return CHANNEL_RC_OK;
379
333
}
380
381
UINT cliprdr_read_file_contents_response(wStream* s, CLIPRDR_FILE_CONTENTS_RESPONSE* response)
382
62
{
383
62
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
384
5
    return ERROR_INVALID_DATA;
385
386
57
  Stream_Read_UINT32(s, response->streamId);   /* streamId (4 bytes) */
387
57
  response->requestedData = Stream_ConstPointer(s); /* requestedFileContentsData */
388
389
57
  if (response->common.dataLen < 4)
390
0
  {
391
0
    WLog_WARN(TAG, "dataLen=%" PRIu32 " but expected >= 4", response->common.dataLen);
392
0
    return ERROR_INVALID_DATA;
393
0
  }
394
395
57
  response->cbRequested = response->common.dataLen - 4;
396
57
  if (!Stream_CheckAndLogRequiredLength(TAG, s, response->cbRequested))
397
0
    return ERROR_INVALID_DATA;
398
57
  Stream_Seek(s, response->cbRequested);
399
57
  return CHANNEL_RC_OK;
400
57
}
401
402
UINT cliprdr_read_format_list(wLog* log, wStream* s, CLIPRDR_FORMAT_LIST* formatList,
403
                              BOOL useLongFormatNames)
404
548
{
405
548
  UINT32 index = 0;
406
548
  size_t formatNameLength = 0;
407
548
  const char* szFormatName = nullptr;
408
548
  const WCHAR* wszFormatName = nullptr;
409
548
  wStream sub1buffer = WINPR_C_ARRAY_INIT;
410
548
  CLIPRDR_FORMAT* formats = nullptr;
411
548
  UINT error = ERROR_INTERNAL_ERROR;
412
413
548
  const BOOL asciiNames = (formatList->common.msgFlags & CB_ASCII_NAMES) != 0;
414
415
548
  index = 0;
416
  /* empty format list */
417
548
  formatList->formats = nullptr;
418
548
  formatList->numFormats = 0;
419
420
548
  wStream* sub1 =
421
548
      Stream_StaticConstInit(&sub1buffer, Stream_ConstPointer(s), formatList->common.dataLen);
422
548
  if (!Stream_SafeSeek(s, formatList->common.dataLen))
423
0
    return ERROR_INVALID_DATA;
424
425
548
  if (!formatList->common.dataLen)
426
1
  {
427
1
  }
428
547
  else if (!useLongFormatNames)
429
133
  {
430
133
    const size_t cap = Stream_Capacity(sub1) / 36ULL;
431
133
    if (cap > UINT32_MAX)
432
0
    {
433
0
      WLog_Print(log, WLOG_ERROR, "Invalid short format list length: %" PRIuz "", cap);
434
0
      return ERROR_INTERNAL_ERROR;
435
0
    }
436
133
    formatList->numFormats = (UINT32)cap;
437
438
133
    if (formatList->numFormats)
439
121
      formats = (CLIPRDR_FORMAT*)calloc(formatList->numFormats, sizeof(CLIPRDR_FORMAT));
440
441
133
    if (!formats)
442
12
    {
443
12
      WLog_Print(log, WLOG_ERROR, "calloc failed!");
444
12
      return CHANNEL_RC_NO_MEMORY;
445
12
    }
446
447
121
    formatList->formats = formats;
448
449
222k
    while (Stream_GetRemainingLength(sub1) >= 4)
450
222k
    {
451
222k
      if (index >= formatList->numFormats)
452
35
        goto error_out;
453
454
222k
      CLIPRDR_FORMAT* format = &formats[index];
455
456
222k
      Stream_Read_UINT32(sub1, format->formatId); /* formatId (4 bytes) */
457
458
      /* According to MS-RDPECLIP 2.2.3.1.1.1 formatName is "a 32-byte block containing
459
       * the *null-terminated* name assigned to the Clipboard Format: (32 ASCII 8 characters
460
       * or 16 Unicode characters)"
461
       * However, both Windows RDSH and mstsc violate this specs as seen in the following
462
       * example of a transferred short format name string: [R.i.c.h. .T.e.x.t. .F.o.r.m.a.t.]
463
       * These are 16 unicode characters - *without* terminating null !
464
       */
465
466
222k
      szFormatName = Stream_ConstPointer(sub1);
467
222k
      wszFormatName = Stream_ConstPointer(sub1);
468
222k
      if (!Stream_SafeSeek(sub1, 32))
469
0
        goto error_out;
470
471
222k
      free(format->formatName);
472
222k
      format->formatName = nullptr;
473
474
222k
      if (asciiNames)
475
0
      {
476
0
        if (szFormatName[0])
477
0
        {
478
          /* ensure null termination */
479
0
          format->formatName = strndup(szFormatName, 31);
480
0
          if (!format->formatName)
481
0
          {
482
0
            WLog_Print(log, WLOG_ERROR, "malloc failed!");
483
0
            error = CHANNEL_RC_NO_MEMORY;
484
0
            goto error_out;
485
0
          }
486
0
        }
487
0
      }
488
222k
      else
489
222k
      {
490
222k
        if (wszFormatName[0])
491
128k
        {
492
128k
          format->formatName = ConvertWCharNToUtf8Alloc(wszFormatName, 16, nullptr);
493
128k
          if (!format->formatName)
494
38
            goto error_out;
495
128k
        }
496
222k
      }
497
498
221k
      index++;
499
221k
    }
500
121
  }
501
414
  else
502
414
  {
503
414
    wStream sub2buffer = sub1buffer;
504
414
    wStream* sub2 = &sub2buffer;
505
506
    /* Bound must match the read pass below: some clients pad the format list with
507
     * trailing bytes that do not start another entry. */
508
1.37M
    while (Stream_GetRemainingLength(sub1) >= 4)
509
1.37M
    {
510
1.37M
      size_t rest = 0;
511
1.37M
      if (!Stream_SafeSeek(sub1, 4)) /* formatId (4 bytes) */
512
0
        goto error_out;
513
514
1.37M
      wszFormatName = Stream_ConstPointer(sub1);
515
1.37M
      rest = Stream_GetRemainingLength(sub1);
516
1.37M
      formatNameLength = _wcsnlen(wszFormatName, rest / sizeof(WCHAR));
517
518
1.37M
      if (!Stream_SafeSeek(sub1, (formatNameLength + 1) * sizeof(WCHAR)))
519
48
        goto error_out;
520
1.37M
      formatList->numFormats++;
521
1.37M
    }
522
523
366
    if (formatList->numFormats)
524
363
      formats = (CLIPRDR_FORMAT*)calloc(formatList->numFormats, sizeof(CLIPRDR_FORMAT));
525
526
366
    if (!formats)
527
3
    {
528
3
      WLog_Print(log, WLOG_ERROR, "calloc failed!");
529
3
      return CHANNEL_RC_NO_MEMORY;
530
3
    }
531
532
363
    formatList->formats = formats;
533
534
878k
    while (Stream_GetRemainingLength(sub2) >= 4)
535
878k
    {
536
878k
      if (index >= formatList->numFormats)
537
0
        goto error_out;
538
539
878k
      size_t rest = 0;
540
878k
      CLIPRDR_FORMAT* format = &formats[index];
541
542
878k
      Stream_Read_UINT32(sub2, format->formatId); /* formatId (4 bytes) */
543
544
878k
      free(format->formatName);
545
878k
      format->formatName = nullptr;
546
547
878k
      wszFormatName = Stream_ConstPointer(sub2);
548
878k
      rest = Stream_GetRemainingLength(sub2);
549
878k
      formatNameLength = _wcsnlen(wszFormatName, rest / sizeof(WCHAR));
550
878k
      if (!Stream_SafeSeek(sub2, (formatNameLength + 1) * sizeof(WCHAR)))
551
0
        goto error_out;
552
553
878k
      if (formatNameLength)
554
8.64k
      {
555
8.64k
        format->formatName =
556
8.64k
            ConvertWCharNToUtf8Alloc(wszFormatName, formatNameLength, nullptr);
557
8.64k
        if (!format->formatName)
558
83
          goto error_out;
559
8.64k
      }
560
561
878k
      index++;
562
878k
    }
563
363
  }
564
565
329
  return CHANNEL_RC_OK;
566
567
204
error_out:
568
204
  cliprdr_free_format_list(formatList);
569
204
  return error;
570
548
}
571
572
void cliprdr_free_format_list(CLIPRDR_FORMAT_LIST* formatList)
573
533
{
574
533
  if (formatList == nullptr)
575
0
    return;
576
577
533
  if (formatList->formats)
578
484
  {
579
1.76M
    for (UINT32 index = 0; index < formatList->numFormats; index++)
580
1.76M
    {
581
1.76M
      free(formatList->formats[index].formatName);
582
1.76M
    }
583
584
484
    free(formatList->formats);
585
484
    formatList->formats = nullptr;
586
484
    formatList->numFormats = 0;
587
484
  }
588
533
}