Coverage Report

Created: 2026-11-10 06:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/FreeRDP/winpr/libwinpr/ndr/ndr.c
Line
Count
Source
1
/**
2
 * FreeRDP: A Remote Desktop Protocol Implementation
3
 * NDR decoder / encoder
4
 *
5
 * Copyright 2024 David Fort <contact@hardening-consulting.com>
6
 *
7
 * Licensed under the Apache License, Version 2.0 (the "License");
8
 * you may not use this file except in compliance with the License.
9
 * You may obtain a copy of the License at
10
 *
11
 *     http://www.apache.org/licenses/LICENSE-2.0
12
 *
13
 * Unless required by applicable law or agreed to in writing, software
14
 * distributed under the License is distributed on an "AS IS" BASIS,
15
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
 * See the License for the specific language governing permissions and
17
 * limitations under the License.
18
 */
19
#include <stddef.h>
20
21
#include <winpr/assert.h>
22
#include <winpr/collections.h>
23
#include <winpr/interlocked.h>
24
#include <winpr/wlog.h>
25
#include <winpr/string.h>
26
#include <winpr/ndr.h>
27
28
#include "../log.h"
29
30
#define TAG WINPR_TAG("ndr")
31
32
0
#define NDR_MAX_DEFERRED 128
33
34
/** @brief pending deferred pointers
35
 *
36
 * NDR writes the referents of embedded pointers after the construct (structure, array or
37
 * referent) that contains the pointers, in the order of the pointers, each referent being
38
 * followed by its own referents.
39
 *
40
 * While a construct is walked the deferred pointers are collected in a block. When the
41
 * outermost construct is done the block is pushed on a stack in reverse order: popping the stack
42
 * treats the entries in order, and the entries pushed while treating one are treated before the
43
 * next one.
44
 */
45
typedef struct
46
{
47
  size_t nstack;
48
  WinPrNdrDeferredEntry stack[NDR_MAX_DEFERRED];
49
50
  size_t depth;
51
  size_t nblock;
52
  WinPrNdrDeferredEntry block[NDR_MAX_DEFERRED];
53
} NdrDeferreds;
54
55
struct WinPrNdrEncoder_s
56
{
57
  BYTE version;
58
  BOOL bigEndianDrep;
59
60
  size_t offset; /** bytes written in the current object, for the alignments */
61
  BOOL inObject;
62
  size_t objectStart; /** stream position of the private header of the current object */
63
64
  UINT32 refIdCounter;
65
  NdrDeferreds deferreds;
66
};
67
68
struct WinPrNdrDecoder_s
69
{
70
  BYTE version;
71
  BOOL bigEndianDrep;
72
73
  size_t offset; /** bytes read in the current object, for the alignments */
74
75
  wHashTable* referents; /** referent id => NdrReferent */
76
  NdrDeferreds deferreds;
77
};
78
79
0
#define NDR_REFERENT_MAGIC 0x46455244 /* "DREF" */
80
81
/** @brief a reference counted referent, the referent data follows the header
82
 *
83
 * All the pointers returned by the decoder are allocated this way, they are released with
84
 * winpr_ndr_pointer_release().
85
 */
86
typedef struct
87
{
88
  UINT32 magic;
89
  winpr_ndr_refid refId;
90
  BOOL full; /** referent of full pointers, that can be aliased */
91
  volatile LONG refCount;
92
  WinPrNdrMessageType typeDescr;
93
  size_t size; /** size of the referent */
94
  BOOL hasHints;
95
  union
96
  {
97
    WinPrNdrArrayHints array;
98
    WinPrNdrVaryingArrayHints varying;
99
  } hints; /** copy of the hints, the destroy function of the type needs them */
100
  union
101
  {
102
    /* max_align_t is not available with all the compilers (MSVC C) */
103
    UINT64 u64;
104
    double d;
105
    long double ld;
106
    void* ptr;
107
    BYTE data[1];
108
  } payload;
109
} NdrReferent;
110
111
static NdrReferent* ndr_referent(void* ptr)
112
0
{
113
0
  WINPR_ASSERT(ptr);
114
115
  /* computed on the address: the referent header precedes the payload */
116
0
  NdrReferent* ref = (NdrReferent*)((uintptr_t)ptr - offsetof(NdrReferent, payload));
117
0
  WINPR_ASSERT(ref->magic == NDR_REFERENT_MAGIC);
118
0
  return ref;
119
0
}
120
121
static void ndr_deferreds_enter(NdrDeferreds* d)
122
0
{
123
0
  WINPR_ASSERT(d);
124
0
  d->depth++;
125
0
}
126
127
static BOOL ndr_deferreds_add(NdrDeferreds* d, const WinPrNdrDeferredEntry* entry)
128
0
{
129
0
  WINPR_ASSERT(d);
130
0
  WINPR_ASSERT(d->depth);
131
0
  WINPR_ASSERT(entry);
132
133
0
  if (d->nblock == NDR_MAX_DEFERRED)
134
0
  {
135
0
    WLog_ERR(TAG, "too many deferred pointers when adding %s", entry->name);
136
0
    return FALSE;
137
0
  }
138
139
0
  d->block[d->nblock++] = *entry;
140
0
  return TRUE;
141
0
}
142
143
static BOOL ndr_deferreds_leave(NdrDeferreds* d, BOOL success)
144
0
{
145
0
  WINPR_ASSERT(d);
146
0
  WINPR_ASSERT(d->depth);
147
148
0
  d->depth--;
149
0
  if (d->depth)
150
0
    return success;
151
152
0
  const size_t nblock = d->nblock;
153
0
  d->nblock = 0;
154
0
  if (!success)
155
0
    return FALSE;
156
157
0
  if (d->nstack + nblock > NDR_MAX_DEFERRED)
158
0
  {
159
0
    WLog_ERR(TAG, "too many deferred pointers");
160
0
    return FALSE;
161
0
  }
162
163
0
  for (size_t i = nblock; i > 0; i--)
164
0
    d->stack[d->nstack++] = d->block[i - 1];
165
0
  return TRUE;
166
0
}
167
168
static BOOL ndr_deferreds_pop(NdrDeferreds* d, WinPrNdrDeferredEntry* entry)
169
0
{
170
0
  WINPR_ASSERT(d);
171
0
  WINPR_ASSERT(entry);
172
173
0
  if (!d->nstack)
174
0
    return FALSE;
175
176
0
  d->nstack--;
177
0
  *entry = d->stack[d->nstack];
178
0
  return TRUE;
179
0
}
180
181
/** @brief computes the address of the hints of a field, if any */
182
static void* ndr_field_hints(const WinPrNdrStructDescr* descr, const WinPrNdrFieldStruct* field,
183
                             const void* obj)
184
0
{
185
0
  if (field->hintsField < 0)
186
0
    return nullptr;
187
188
0
  WINPR_ASSERT((size_t)field->hintsField < descr->nfields);
189
0
  const WinPrNdrFieldStruct* hintsField = &descr->fields[field->hintsField];
190
0
  const BYTE* hints = (const BYTE*)obj + hintsField->structOffset;
191
0
  return WINPR_CAST_CONST_PTR_AWAY(hints, void*);
192
0
}
193
194
WinPrNdrEncoder* winpr_ndr_encoder_new(BOOL bigEndianDrep, BYTE version)
195
0
{
196
0
  WinPrNdrEncoder* ret = calloc(1, sizeof(*ret));
197
0
  if (!ret)
198
0
    return nullptr;
199
200
0
  ret->version = version;
201
0
  ret->bigEndianDrep = bigEndianDrep;
202
0
  ret->refIdCounter = 0x20000;
203
0
  return ret;
204
0
}
205
206
WinPrNdrEncoder* winpr_ndr_encoder_from_decoder(const WinPrNdrDecoder* src)
207
0
{
208
0
  WINPR_ASSERT(src);
209
210
0
  return winpr_ndr_encoder_new(src->bigEndianDrep, src->version);
211
0
}
212
213
void winpr_ndr_encoder_free(WinPrNdrEncoder* context)
214
0
{
215
0
  free(context);
216
0
}
217
218
void winpr_ndr_encoder_bytes_written(WinPrNdrEncoder* context, size_t len)
219
0
{
220
0
  WINPR_ASSERT(context);
221
0
  context->offset += len;
222
0
}
223
224
BOOL winpr_ndr_encoder_write_header(WinPrNdrEncoder* context, wStream* s)
225
0
{
226
0
  WINPR_ASSERT(context);
227
228
0
  if (!Stream_EnsureRemainingCapacity(s, 8))
229
0
    return FALSE;
230
231
0
  Stream_Write_UINT8(s, context->version);
232
0
  Stream_Write_UINT8(s, context->bigEndianDrep ? 0x00 : 0x10);
233
0
  Stream_Write_UINT16(s, 0x8); /* header len */
234
235
0
  BYTE filler[] = { 0xcc, 0xcc, 0xcc, 0xcc };
236
0
  Stream_Write(s, filler, sizeof(filler));
237
0
  return TRUE;
238
0
}
239
240
BOOL winpr_ndr_encoder_write_align(WinPrNdrEncoder* context, wStream* s, size_t sz)
241
0
{
242
0
  WINPR_ASSERT(context);
243
244
0
  size_t rest = context->offset % sz;
245
0
  if (rest)
246
0
  {
247
0
    size_t padding = (sz - rest);
248
249
0
    if (!Stream_EnsureRemainingCapacity(s, padding))
250
0
      return FALSE;
251
252
0
    Stream_Zero(s, padding);
253
0
    context->offset += padding;
254
0
  }
255
256
0
  return TRUE;
257
0
}
258
259
BOOL winpr_ndr_encoder_start_object(WinPrNdrEncoder* context, wStream* s)
260
0
{
261
0
  WINPR_ASSERT(context);
262
0
  WINPR_ASSERT(s);
263
264
0
  if (context->inObject)
265
0
  {
266
0
    WLog_ERR(TAG, "an object is already being serialized");
267
0
    return FALSE;
268
0
  }
269
270
0
  if (!Stream_EnsureRemainingCapacity(s, 8))
271
0
    return FALSE;
272
273
  /* ObjectBufferLength, filled by winpr_ndr_encoder_end_object, and Filler */
274
0
  context->objectStart = Stream_GetPosition(s);
275
0
  Stream_Zero(s, 8);
276
277
0
  context->inObject = TRUE;
278
0
  context->offset = 0;
279
0
  return TRUE;
280
0
}
281
282
BOOL winpr_ndr_encoder_end_object(WinPrNdrEncoder* context, wStream* s)
283
0
{
284
0
  WINPR_ASSERT(context);
285
0
  WINPR_ASSERT(s);
286
287
0
  if (!context->inObject)
288
0
  {
289
0
    WLog_ERR(TAG, "no object being serialized");
290
0
    return FALSE;
291
0
  }
292
293
0
  const size_t start = context->objectStart + 8;
294
0
  WINPR_ASSERT(Stream_GetPosition(s) >= start);
295
296
  /* the length of the object buffer must be a multiple of 8 */
297
0
  const size_t padding = (8 - ((Stream_GetPosition(s) - start) % 8)) % 8;
298
0
  if (!Stream_EnsureRemainingCapacity(s, padding))
299
0
    return FALSE;
300
0
  Stream_Zero(s, padding);
301
0
  context->offset += padding;
302
303
0
  const size_t len = Stream_GetPosition(s) - start;
304
0
  if (len > UINT32_MAX)
305
0
    return FALSE;
306
307
0
  BYTE* header = Stream_Buffer(s) + context->objectStart;
308
0
  if (context->bigEndianDrep)
309
0
    winpr_Data_Write_UINT32_BE(header, (UINT32)len);
310
0
  else
311
0
    winpr_Data_Write_UINT32(header, (UINT32)len);
312
313
0
  context->inObject = FALSE;
314
0
  return TRUE;
315
0
}
316
317
BOOL winpr_ndr_encoder_write_toplevel_refid(WinPrNdrEncoder* context, wStream* s)
318
0
{
319
0
  WINPR_ASSERT(context);
320
321
0
  const winpr_ndr_refid refId = context->refIdCounter;
322
0
  context->refIdCounter += 4;
323
0
  return winpr_ndr_encoder_write_uint32(context, s, refId);
324
0
}
325
326
static size_t ndr_hintsCount(WinPrNdrMessageType msgType, const void* hints)
327
0
{
328
0
  WINPR_ASSERT(msgType);
329
330
0
  switch (msgType->arity)
331
0
  {
332
0
    case WINPR_NDR_ARITY_SIMPLE:
333
0
      return 1;
334
0
    case WINPR_NDR_ARITY_ARRAYOF:
335
0
      WINPR_ASSERT(hints);
336
0
      return ((const WinPrNdrArrayHints*)hints)->count;
337
0
    case WINPR_NDR_ARITY_VARYING_ARRAYOF:
338
0
      WINPR_ASSERT(hints);
339
0
      return ((const WinPrNdrVaryingArrayHints*)hints)->maxLength;
340
0
    case WINPR_NDR_ARITY_SELF_ALLOCATED:
341
0
      return 1;
342
0
    default:
343
0
      WINPR_ASSERT(0 && "unknown arity");
344
0
      return 0;
345
0
  }
346
0
}
347
348
/** @brief checks that the array described by hints fits in the inline storage of a fixed array */
349
static BOOL ndr_check_fixed_array(const WinPrNdrStructDescr* descr,
350
                                  const WinPrNdrFieldStruct* field, const void* hints)
351
0
{
352
0
  if (!hints || !field->fixedArraySize)
353
0
  {
354
0
    WLog_ERR(TAG, "%s.%s: fixed array without hints or size", descr->name, field->name);
355
0
    return FALSE;
356
0
  }
357
358
0
  const UINT64 needed =
359
0
      1ull * ndr_hintsCount(field->typeDescr, hints) * field->typeDescr->itemSize;
360
0
  if (needed > field->fixedArraySize)
361
0
  {
362
0
    WLog_ERR(TAG, "%s.%s: %" PRIu64 " bytes don't fit in a fixed array of %" PRIuz " bytes",
363
0
             descr->name, field->name, needed, field->fixedArraySize);
364
0
    return FALSE;
365
0
  }
366
0
  return TRUE;
367
0
}
368
369
/** @brief checks the [range(0, rangeMax)] attribute of an integer field */
370
static BOOL ndr_check_range(const WinPrNdrStructDescr* descr, const WinPrNdrFieldStruct* field,
371
                            const void* ptr)
372
0
{
373
0
  if (!field->rangeMax)
374
0
    return TRUE;
375
376
0
  UINT64 v = 0;
377
0
  switch (field->typeDescr->itemSize)
378
0
  {
379
0
    case 1:
380
0
      v = *(const UINT8*)ptr;
381
0
      break;
382
0
    case 2:
383
0
    {
384
0
      UINT16 v16 = 0;
385
0
      memcpy(&v16, ptr, sizeof(v16));
386
0
      v = v16;
387
0
    }
388
0
    break;
389
0
    case 4:
390
0
    {
391
0
      UINT32 v32 = 0;
392
0
      memcpy(&v32, ptr, sizeof(v32));
393
0
      v = v32;
394
0
    }
395
0
    break;
396
0
    case 8:
397
0
      memcpy(&v, ptr, sizeof(v));
398
0
      break;
399
0
    default:
400
0
      WLog_ERR(TAG, "%s.%s: range on a field of %" PRIuz " bytes", descr->name, field->name,
401
0
               field->typeDescr->itemSize);
402
0
      return FALSE;
403
0
  }
404
405
0
  if (v > field->rangeMax)
406
0
  {
407
0
    WLog_ERR(TAG, "%s.%s: %" PRIu64 " is out of range(0, %" PRIu32 ")", descr->name,
408
0
             field->name, v, field->rangeMax);
409
0
    return FALSE;
410
0
  }
411
0
  return TRUE;
412
0
}
413
414
/** @brief number of items of an inline array field */
415
static size_t ndr_inline_count(const WinPrNdrFieldStruct* field)
416
0
{
417
0
  WINPR_ASSERT(field->typeDescr->itemSize);
418
0
  return field->fixedArraySize / field->typeDescr->itemSize;
419
0
}
420
421
BOOL winpr_ndr_encoder_write_uint8(WinPrNdrEncoder* context, wStream* s, BYTE v)
422
0
{
423
0
  if (!Stream_EnsureRemainingCapacity(s, 1))
424
0
    return FALSE;
425
426
0
  Stream_Write_UINT8(s, v);
427
0
  winpr_ndr_encoder_bytes_written(context, 1);
428
0
  return TRUE;
429
0
}
430
431
BOOL winpr_ndr_encoder_write_uint8_(WinPrNdrEncoder* context, wStream* s, const void* hints,
432
                                    const void* v)
433
0
{
434
0
  WINPR_ASSERT(context);
435
0
  WINPR_ASSERT(s);
436
0
  WINPR_ASSERT(v);
437
0
  WINPR_UNUSED(hints);
438
439
0
  return winpr_ndr_encoder_write_uint8(context, s, *(const BYTE*)v);
440
0
}
441
442
const WinPrNdrMessageDescr winpr_ndr_uint8_descr_s = {
443
  WINPR_NDR_ARITY_SIMPLE,         1,       winpr_ndr_decoder_read_uint8_,
444
  winpr_ndr_encoder_write_uint8_, nullptr, nullptr
445
};
446
447
WinPrNdrMessageType winpr_ndr_uint8_descr(void)
448
0
{
449
0
  return &winpr_ndr_uint8_descr_s;
450
0
}
451
452
BOOL winpr_ndr_encoder_write_int8(WinPrNdrEncoder* context, wStream* s, INT8 v)
453
0
{
454
0
  return winpr_ndr_encoder_write_uint8(context, s, (BYTE)v);
455
0
}
456
457
BOOL winpr_ndr_encoder_write_int8_(WinPrNdrEncoder* context, wStream* s, const void* hints,
458
                                   const void* v)
459
0
{
460
0
  WINPR_ASSERT(context);
461
0
  WINPR_ASSERT(s);
462
0
  WINPR_ASSERT(v);
463
0
  WINPR_UNUSED(hints);
464
465
0
  return winpr_ndr_encoder_write_int8(context, s, *(const INT8*)v);
466
0
}
467
468
const WinPrNdrMessageDescr winpr_ndr_int8_descr_s = {
469
  WINPR_NDR_ARITY_SIMPLE,        1,       winpr_ndr_decoder_read_int8_,
470
  winpr_ndr_encoder_write_int8_, nullptr, nullptr
471
};
472
473
WinPrNdrMessageType winpr_ndr_int8_descr(void)
474
0
{
475
0
  return &winpr_ndr_int8_descr_s;
476
0
}
477
478
#define SIMPLE_TYPE_IMPL(PREFIX, UPPERTYPE, LOWERTYPE)                                         \
479
  BOOL PREFIX##_decoder_read_##LOWERTYPE(WinPrNdrDecoder* context, wStream* s, UPPERTYPE* v) \
480
0
  {                                                                                          \
481
0
    WINPR_ASSERT(context);                                                                 \
482
0
                                                                                               \
483
0
    if (!winpr_ndr_decoder_read_align(context, s, sizeof(UPPERTYPE)))                      \
484
0
      return FALSE;                                                                      \
485
0
    if (!Stream_CheckAndLogRequiredLength(TAG, s, sizeof(UPPERTYPE)))                      \
486
0
      return FALSE;                                                                      \
487
0
                                                                                               \
488
0
    if (context->bigEndianDrep)                                                            \
489
0
      Stream_Read_##UPPERTYPE##_BE(s, *v);                                               \
490
0
    else                                                                                   \
491
0
      Stream_Read_##UPPERTYPE(s, *v);                                                    \
492
0
                                                                                               \
493
0
    winpr_ndr_decoder_bytes_read(context, sizeof(UPPERTYPE));                              \
494
0
    return TRUE;                                                                           \
495
0
  }                                                                                          \
Unexecuted instantiation: winpr_ndr_decoder_read_uint32
Unexecuted instantiation: winpr_ndr_decoder_read_int32
Unexecuted instantiation: winpr_ndr_decoder_read_uint16
Unexecuted instantiation: winpr_ndr_decoder_read_int16
Unexecuted instantiation: winpr_ndr_decoder_read_uint64
Unexecuted instantiation: winpr_ndr_decoder_read_int64
496
                                                                                               \
497
  BOOL PREFIX##_decoder_read_##LOWERTYPE##_(WinPrNdrDecoder* context, wStream* s,            \
498
                                            const void* hints, void* v)                      \
499
0
  {                                                                                          \
500
0
    WINPR_UNUSED(hints);                                                                   \
501
0
    return PREFIX##_decoder_read_##LOWERTYPE(context, s, (UPPERTYPE*)v);                   \
502
0
  }                                                                                          \
Unexecuted instantiation: winpr_ndr_decoder_read_uint32_
Unexecuted instantiation: winpr_ndr_decoder_read_int32_
Unexecuted instantiation: winpr_ndr_decoder_read_uint16_
Unexecuted instantiation: winpr_ndr_decoder_read_int16_
Unexecuted instantiation: winpr_ndr_decoder_read_uint64_
Unexecuted instantiation: winpr_ndr_decoder_read_int64_
503
                                                                                               \
504
  BOOL PREFIX##_encoder_write_##LOWERTYPE(WinPrNdrEncoder* context, wStream* s, UPPERTYPE v) \
505
0
  {                                                                                          \
506
0
    if (!winpr_ndr_encoder_write_align(context, s, sizeof(UPPERTYPE)) ||                   \
507
0
        !Stream_EnsureRemainingCapacity(s, sizeof(UPPERTYPE)))                             \
508
0
      return FALSE;                                                                      \
509
0
                                                                                               \
510
0
    if (context->bigEndianDrep)                                                            \
511
0
      Stream_Write_##UPPERTYPE##_BE(s, v);                                               \
512
0
    else                                                                                   \
513
0
      Stream_Write_##UPPERTYPE(s, v);                                                    \
514
0
                                                                                               \
515
0
    winpr_ndr_encoder_bytes_written(context, sizeof(UPPERTYPE));                           \
516
0
    return TRUE;                                                                           \
517
0
  }                                                                                          \
Unexecuted instantiation: winpr_ndr_encoder_write_uint32
Unexecuted instantiation: winpr_ndr_encoder_write_int32
Unexecuted instantiation: winpr_ndr_encoder_write_uint16
Unexecuted instantiation: winpr_ndr_encoder_write_int16
Unexecuted instantiation: winpr_ndr_encoder_write_uint64
Unexecuted instantiation: winpr_ndr_encoder_write_int64
518
                                                                                               \
519
  BOOL PREFIX##_encoder_write_##LOWERTYPE##_(WinPrNdrEncoder* context, wStream* s,           \
520
                                             const void* hints, const void* v)               \
521
0
  {                                                                                          \
522
0
    WINPR_ASSERT(context);                                                                 \
523
0
    WINPR_ASSERT(s);                                                                       \
524
0
    WINPR_ASSERT(v);                                                                       \
525
0
    WINPR_UNUSED(hints);                                                                   \
526
0
                                                                                               \
527
0
    return PREFIX##_encoder_write_##LOWERTYPE(context, s, *(const UPPERTYPE*)v);           \
528
0
  }                                                                                          \
Unexecuted instantiation: winpr_ndr_encoder_write_uint32_
Unexecuted instantiation: winpr_ndr_encoder_write_int32_
Unexecuted instantiation: winpr_ndr_encoder_write_uint16_
Unexecuted instantiation: winpr_ndr_encoder_write_int16_
Unexecuted instantiation: winpr_ndr_encoder_write_uint64_
Unexecuted instantiation: winpr_ndr_encoder_write_int64_
529
                                                                                               \
530
  const WinPrNdrMessageDescr PREFIX##_##LOWERTYPE##_descr_s = {                              \
531
    WINPR_NDR_ARITY_SIMPLE,                                                                \
532
    sizeof(UPPERTYPE),                                                                     \
533
    PREFIX##_decoder_read_##LOWERTYPE##_,                                                  \
534
    PREFIX##_encoder_write_##LOWERTYPE##_,                                                 \
535
    nullptr,                                                                               \
536
    nullptr                                                                                \
537
  };                                                                                         \
538
                                                                                               \
539
  WinPrNdrMessageType PREFIX##_##LOWERTYPE##_descr(void)                                     \
540
0
  {                                                                                          \
541
0
    return &PREFIX##_##LOWERTYPE##_descr_s;                                                \
542
0
  }
Unexecuted instantiation: winpr_ndr_uint32_descr
Unexecuted instantiation: winpr_ndr_int32_descr
Unexecuted instantiation: winpr_ndr_uint16_descr
Unexecuted instantiation: winpr_ndr_int16_descr
Unexecuted instantiation: winpr_ndr_uint64_descr
Unexecuted instantiation: winpr_ndr_int64_descr
543
544
SIMPLE_TYPE_IMPL(winpr_ndr, UINT32, uint32)
545
SIMPLE_TYPE_IMPL(winpr_ndr, INT32, int32)
546
SIMPLE_TYPE_IMPL(winpr_ndr, UINT16, uint16)
547
SIMPLE_TYPE_IMPL(winpr_ndr, INT16, int16)
548
SIMPLE_TYPE_IMPL(winpr_ndr, UINT64, uint64)
549
SIMPLE_TYPE_IMPL(winpr_ndr, INT64, int64)
550
551
#define ARRAY_OF_TYPE_IMPL(TYPE, UPPERTYPE)                                                      \
552
  BOOL winpr_ndr_decoder_read_##TYPE##Array(WinPrNdrDecoder* context, wStream* s,              \
553
                                            const void* hints, void* v)                        \
554
0
  {                                                                                            \
555
0
    WINPR_ASSERT(context);                                                                   \
556
0
    WINPR_ASSERT(s);                                                                         \
557
0
    return winpr_ndr_decoder_read_uconformant_array(context, s, hints,                       \
558
0
                                                    winpr_ndr_##TYPE##_descr(), v);          \
559
0
  }                                                                                            \
Unexecuted instantiation: winpr_ndr_decoder_read_uint8Array
Unexecuted instantiation: winpr_ndr_decoder_read_uint16Array
560
                                                                                                 \
561
  BOOL winpr_ndr_encoder_write_##TYPE##Array(WinPrNdrEncoder* context, wStream* s,             \
562
                                             const void* hints, const void* v)                 \
563
0
  {                                                                                            \
564
0
    WINPR_ASSERT(context);                                                                   \
565
0
    WINPR_ASSERT(s);                                                                         \
566
0
    const WinPrNdrArrayHints* ahints = (const WinPrNdrArrayHints*)hints;                     \
567
0
    return winpr_ndr_encoder_write_uconformant_array(context, s, ahints->count,              \
568
0
                                                     winpr_ndr_##TYPE##_descr(), v);         \
569
0
  }                                                                                            \
Unexecuted instantiation: winpr_ndr_encoder_write_uint8Array
Unexecuted instantiation: winpr_ndr_encoder_write_uint16Array
570
  void winpr_ndr_destroy_##TYPE##Array(const void* hints, void* obj)                           \
571
0
  {                                                                                            \
572
0
    WINPR_ASSERT(obj);                                                                       \
573
0
    WINPR_ASSERT(hints);                                                                     \
574
0
    const WinPrNdrArrayHints* ahints = (const WinPrNdrArrayHints*)hints;                     \
575
0
    WinPrNdrMessageType descr = winpr_ndr_##TYPE##_descr();                                  \
576
0
    if (descr->destroyFn)                                                                    \
577
0
    {                                                                                        \
578
0
      UPPERTYPE* ptr = (UPPERTYPE*)obj;                                                    \
579
0
      for (UINT32 i = 0; i < ahints->count; i++, ptr++)                                    \
580
0
        descr->destroyFn(nullptr, ptr);                                                  \
581
0
    }                                                                                        \
582
0
  }                                                                                            \
Unexecuted instantiation: winpr_ndr_destroy_uint8Array
Unexecuted instantiation: winpr_ndr_destroy_uint16Array
583
                                                                                                 \
584
  const WinPrNdrMessageDescr winpr_ndr_##TYPE##Array_descr_s = {                               \
585
    WINPR_NDR_ARITY_ARRAYOF,                                                                 \
586
    sizeof(UPPERTYPE),                                                                       \
587
    winpr_ndr_decoder_read_##TYPE##Array,                                                    \
588
    winpr_ndr_encoder_write_##TYPE##Array,                                                   \
589
    winpr_ndr_destroy_##TYPE##Array,                                                         \
590
    nullptr                                                                                  \
591
  };                                                                                           \
592
                                                                                                 \
593
  WinPrNdrMessageType winpr_ndr_##TYPE##Array_descr(void)                                      \
594
0
  {                                                                                            \
595
0
    return &winpr_ndr_##TYPE##Array_descr_s;                                                 \
596
0
  }                                                                                            \
Unexecuted instantiation: winpr_ndr_uint8Array_descr
Unexecuted instantiation: winpr_ndr_uint16Array_descr
597
                                                                                                 \
598
  BOOL winpr_ndr_decoder_read_##TYPE##VaryingArray(WinPrNdrDecoder* context, wStream* s,       \
599
                                                   const void* hints, void* v)                 \
600
0
  {                                                                                            \
601
0
    WINPR_ASSERT(context);                                                                   \
602
0
    WINPR_ASSERT(s);                                                                         \
603
0
    return winpr_ndr_decoder_read_uconformant_varying_array(                                 \
604
0
        context, s, (const WinPrNdrVaryingArrayHints*)hints, winpr_ndr_##TYPE##_descr(), v); \
605
0
  }                                                                                            \
Unexecuted instantiation: winpr_ndr_decoder_read_uint8VaryingArray
Unexecuted instantiation: winpr_ndr_decoder_read_uint16VaryingArray
606
  BOOL winpr_ndr_encoder_write_##TYPE##VaryingArray(WinPrNdrEncoder* context, wStream* s,      \
607
                                                    const void* hints, const void* v)          \
608
0
  {                                                                                            \
609
0
    WINPR_ASSERT(context);                                                                   \
610
0
    WINPR_ASSERT(s);                                                                         \
611
0
    return winpr_ndr_encoder_write_uconformant_varying_array(                                \
612
0
        context, s, (const WinPrNdrVaryingArrayHints*)hints, winpr_ndr_##TYPE##_descr(), v); \
613
0
  }                                                                                            \
Unexecuted instantiation: winpr_ndr_encoder_write_uint8VaryingArray
Unexecuted instantiation: winpr_ndr_encoder_write_uint16VaryingArray
614
                                                                                                 \
615
  const WinPrNdrMessageDescr winpr_ndr_##TYPE##VaryingArray_descr_s = {                        \
616
    WINPR_NDR_ARITY_VARYING_ARRAYOF,                                                         \
617
    sizeof(UPPERTYPE),                                                                       \
618
    winpr_ndr_decoder_read_##TYPE##VaryingArray,                                             \
619
    winpr_ndr_encoder_write_##TYPE##VaryingArray,                                            \
620
    nullptr,                                                                                 \
621
    nullptr                                                                                  \
622
  };                                                                                           \
623
                                                                                                 \
624
  WinPrNdrMessageType winpr_ndr_##TYPE##VaryingArray_descr(void)                               \
625
0
  {                                                                                            \
626
0
    return &winpr_ndr_##TYPE##VaryingArray_descr_s;                                          \
627
0
  }
Unexecuted instantiation: winpr_ndr_uint8VaryingArray_descr
Unexecuted instantiation: winpr_ndr_uint16VaryingArray_descr
628
629
ARRAY_OF_TYPE_IMPL(uint8, BYTE)
630
ARRAY_OF_TYPE_IMPL(uint16, UINT16)
631
632
static BOOL winpr_ndr_decoder_read_string_conformant(WinPrNdrDecoder* context, wStream* s,
633
                                                     const void* hints, void* v)
634
0
{
635
0
  WINPR_ASSERT(context);
636
0
  WINPR_ASSERT(s);
637
0
  WINPR_ASSERT(v);
638
0
  WINPR_UNUSED(hints);
639
640
0
  UINT32 sz = 0;
641
0
  if (!winpr_ndr_decoder_read_uint32(context, s, &sz))
642
0
    return FALSE;
643
644
0
  if (!Stream_CheckAndLogRequiredLength(TAG, s, sz))
645
0
    return FALSE;
646
647
0
  char* ret = winpr_ndr_pointer_new(&winpr_ndr_string_conformant_descr_s, nullptr, 1ull * sz + 1);
648
0
  if (!ret)
649
0
    return FALSE;
650
651
0
  Stream_Read(s, ret, sz);
652
0
  ret[sz] = 0;
653
654
0
  winpr_ndr_decoder_bytes_read(context, sz);
655
0
  char** pstr = (char**)v;
656
0
  *pstr = ret;
657
0
  return TRUE;
658
0
}
659
660
static BOOL winpr_ndr_encoder_write_string_conformant(WinPrNdrEncoder* context, wStream* s,
661
                                                      const void* hints, const void* v)
662
0
{
663
0
  WINPR_ASSERT(context);
664
0
  WINPR_ASSERT(s);
665
0
  WINPR_ASSERT(v);
666
0
  WINPR_UNUSED(hints);
667
668
0
  const char* str = *(const char* const*)v;
669
0
  const size_t l = str ? strlen(str) + 1 : 0;
670
0
  if (l > UINT32_MAX)
671
0
    return FALSE;
672
673
0
  if (!winpr_ndr_encoder_write_uint32(context, s, (UINT32)l) ||
674
0
      !Stream_EnsureRemainingCapacity(s, l))
675
0
    return FALSE;
676
0
  Stream_Write(s, str, l);
677
0
  winpr_ndr_encoder_bytes_written(context, l);
678
0
  return TRUE;
679
0
}
680
681
const WinPrNdrMessageDescr winpr_ndr_string_conformant_descr_s = {
682
  WINPR_NDR_ARITY_SELF_ALLOCATED,
683
  sizeof(char),
684
  winpr_ndr_decoder_read_string_conformant,
685
  winpr_ndr_encoder_write_string_conformant,
686
  nullptr /* destroy */,
687
  nullptr /* dump */
688
};
689
690
WinPrNdrMessageType winpr_ndr_string_conformant_descr(void)
691
0
{
692
0
  return &winpr_ndr_string_conformant_descr_s;
693
0
}
694
695
/** @brief reads the header of a conformant varying string, returns the number of characters */
696
static BOOL ndr_read_string_varying_header(WinPrNdrDecoder* context, wStream* s, size_t charSize,
697
                                           UINT32* plength)
698
0
{
699
0
  UINT32 maxCount = 0;
700
0
  UINT32 offset = 0;
701
0
  UINT32 length = 0;
702
0
  if (!winpr_ndr_decoder_read_uint32(context, s, &maxCount) ||
703
0
      !winpr_ndr_decoder_read_uint32(context, s, &offset) ||
704
0
      !winpr_ndr_decoder_read_uint32(context, s, &length))
705
0
    return FALSE;
706
707
0
  if (offset != 0)
708
0
  {
709
0
    WLog_ERR(TAG, "offset != 0 not supported yet");
710
0
    return FALSE;
711
0
  }
712
713
0
  if (length > maxCount)
714
0
  {
715
0
    WLog_ERR(TAG, "varying string length %" PRIu32 " > maximum count %" PRIu32, length,
716
0
             maxCount);
717
0
    return FALSE;
718
0
  }
719
720
0
  if (!Stream_CheckAndLogRequiredLengthOfSize(TAG, s, length, charSize))
721
0
    return FALSE;
722
723
0
  *plength = length;
724
0
  return TRUE;
725
0
}
726
727
static BOOL winpr_ndr_decoder_read_string_varying(WinPrNdrDecoder* context, wStream* s,
728
                                                  const void* hints, void* v)
729
0
{
730
0
  WINPR_ASSERT(context);
731
0
  WINPR_ASSERT(s);
732
0
  WINPR_ASSERT(v);
733
0
  WINPR_UNUSED(hints);
734
735
0
  UINT32 length = 0;
736
0
  if (!ndr_read_string_varying_header(context, s, sizeof(char), &length))
737
0
    return FALSE;
738
739
0
  char* ret =
740
0
      winpr_ndr_pointer_new(&winpr_ndr_string_varying_descr_s, nullptr, 1ull * length + 1);
741
0
  if (!ret)
742
0
    return FALSE;
743
744
0
  Stream_Read(s, ret, length);
745
0
  ret[length] = 0;
746
747
0
  winpr_ndr_decoder_bytes_read(context, length);
748
0
  char** pstr = (char**)v;
749
0
  *pstr = ret;
750
0
  return TRUE;
751
0
}
752
753
static BOOL winpr_ndr_encoder_write_string_varying(WinPrNdrEncoder* context, wStream* s,
754
                                                   const void* hints, const void* v)
755
0
{
756
0
  WINPR_ASSERT(context);
757
0
  WINPR_ASSERT(s);
758
0
  WINPR_ASSERT(v);
759
0
  WINPR_UNUSED(hints);
760
761
0
  const char* str = *(const char* const*)v;
762
0
  const size_t l = str ? strlen(str) + 1 : 0;
763
0
  if (l > UINT32_MAX)
764
0
    return FALSE;
765
766
0
  if (!winpr_ndr_encoder_write_uint32(context, s, (UINT32)l) ||
767
0
      !winpr_ndr_encoder_write_uint32(context, s, 0) ||
768
0
      !winpr_ndr_encoder_write_uint32(context, s, (UINT32)l) ||
769
0
      !Stream_EnsureRemainingCapacity(s, l))
770
0
    return FALSE;
771
772
0
  Stream_Write(s, str, l);
773
0
  winpr_ndr_encoder_bytes_written(context, l);
774
0
  return TRUE;
775
0
}
776
777
const WinPrNdrMessageDescr winpr_ndr_string_varying_descr_s = {
778
  WINPR_NDR_ARITY_SELF_ALLOCATED,
779
  sizeof(char),
780
  winpr_ndr_decoder_read_string_varying,
781
  winpr_ndr_encoder_write_string_varying,
782
  nullptr /* destroy */,
783
  nullptr /* dump */
784
};
785
786
WinPrNdrMessageType winpr_ndr_string_varying_descr(void)
787
0
{
788
0
  return &winpr_ndr_string_varying_descr_s;
789
0
}
790
791
static BOOL ndr_decoder_read_wstring_varying(WinPrNdrDecoder* context, wStream* s,
792
                                             const void* hints, void* v)
793
0
{
794
0
  WINPR_ASSERT(context);
795
0
  WINPR_ASSERT(s);
796
0
  WINPR_ASSERT(v);
797
0
  WINPR_UNUSED(hints);
798
799
0
  UINT32 length = 0;
800
0
  if (!ndr_read_string_varying_header(context, s, sizeof(WCHAR), &length))
801
0
    return FALSE;
802
803
0
  WCHAR* ret = winpr_ndr_pointer_new(&winpr_ndr_wstring_varying_descr_s, nullptr,
804
0
                                     (1ull * length + 1) * sizeof(WCHAR));
805
0
  if (!ret)
806
0
    return FALSE;
807
808
0
  for (UINT32 i = 0; i < length; i++)
809
0
  {
810
0
    if (!winpr_ndr_decoder_read_wchar(context, s, &ret[i]))
811
0
    {
812
0
      winpr_ndr_pointer_release(ret);
813
0
      return FALSE;
814
0
    }
815
0
  }
816
817
0
  WCHAR** pstr = (WCHAR**)v;
818
0
  *pstr = ret;
819
0
  return TRUE;
820
0
}
821
822
static BOOL ndr_encoder_write_wstring_varying(WinPrNdrEncoder* context, wStream* s,
823
                                              const void* hints, const void* v)
824
0
{
825
0
  WINPR_ASSERT(context);
826
0
  WINPR_ASSERT(s);
827
0
  WINPR_ASSERT(v);
828
0
  WINPR_UNUSED(hints);
829
830
0
  const WCHAR* str = *(const WCHAR* const*)v;
831
0
  const size_t l = str ? _wcslen(str) + 1 : 0;
832
0
  if (l > UINT32_MAX)
833
0
    return FALSE;
834
835
0
  if (!winpr_ndr_encoder_write_uint32(context, s, (UINT32)l) ||
836
0
      !winpr_ndr_encoder_write_uint32(context, s, 0) ||
837
0
      !winpr_ndr_encoder_write_uint32(context, s, (UINT32)l))
838
0
    return FALSE;
839
840
0
  for (size_t i = 0; i < l; i++)
841
0
  {
842
0
    if (!winpr_ndr_encoder_write_uint16(context, s, str[i]))
843
0
      return FALSE;
844
0
  }
845
0
  return TRUE;
846
0
}
847
848
const WinPrNdrMessageDescr winpr_ndr_wstring_varying_descr_s = {
849
  WINPR_NDR_ARITY_SELF_ALLOCATED,    sizeof(WCHAR),         ndr_decoder_read_wstring_varying,
850
  ndr_encoder_write_wstring_varying, nullptr /* destroy */, nullptr /* dump */
851
};
852
853
WinPrNdrMessageType winpr_ndr_wstring_varying_descr(void)
854
0
{
855
0
  return &winpr_ndr_wstring_varying_descr_s;
856
0
}
857
858
/** @brief writes the items of an array, their deferred pointers are treated after the array */
859
static BOOL ndr_encoder_write_items(WinPrNdrEncoder* context, wStream* s, UINT32 count,
860
                                    WinPrNdrMessageType itemType, const BYTE* src)
861
0
{
862
0
  BOOL ok = TRUE;
863
864
0
  ndr_deferreds_enter(&context->deferreds);
865
0
  for (UINT32 i = 0; ok && (i < count); i++, src += itemType->itemSize)
866
0
    ok = itemType->writeFn(context, s, nullptr, src);
867
868
0
  return ndr_deferreds_leave(&context->deferreds, ok);
869
0
}
870
871
BOOL winpr_ndr_encoder_write_uconformant_varying_array(WinPrNdrEncoder* context, wStream* s,
872
                                                       const WinPrNdrVaryingArrayHints* hints,
873
                                                       WinPrNdrMessageType itemType,
874
                                                       const void* psrc)
875
0
{
876
0
  WINPR_ASSERT(context);
877
0
  WINPR_ASSERT(s);
878
0
  WINPR_ASSERT(hints);
879
0
  WINPR_ASSERT(itemType);
880
0
  WINPR_ASSERT(psrc);
881
882
0
  if (!winpr_ndr_encoder_write_uint32(context, s, hints->maxLength) ||
883
0
      !winpr_ndr_encoder_write_uint32(context, s, 0) ||
884
0
      !winpr_ndr_encoder_write_uint32(context, s, hints->length))
885
0
    return FALSE;
886
887
0
  return ndr_encoder_write_items(context, s, hints->length, itemType, psrc);
888
0
}
889
890
BOOL winpr_ndr_encoder_write_uconformant_array(WinPrNdrEncoder* context, wStream* s, UINT32 len,
891
                                               WinPrNdrMessageType itemType, const BYTE* ptr)
892
0
{
893
0
  WINPR_ASSERT(context);
894
0
  WINPR_ASSERT(s);
895
0
  WINPR_ASSERT(itemType);
896
0
  WINPR_ASSERT(ptr);
897
898
0
  if (!winpr_ndr_encoder_write_uint32(context, s, len))
899
0
    return FALSE;
900
901
0
  return ndr_encoder_write_items(context, s, len, itemType, ptr);
902
0
}
903
904
static BOOL ndr_encoder_write_field(WinPrNdrEncoder* encoder, wStream* s,
905
                                    const WinPrNdrStructDescr* descr,
906
                                    const WinPrNdrFieldStruct* field, const void* src)
907
0
{
908
0
  const BYTE* ptr = (const BYTE*)src + field->structOffset;
909
0
  void* hints = ndr_field_hints(descr, field, src);
910
911
0
  switch (field->pointerType)
912
0
  {
913
0
    case WINPR_NDR_NOT_POINTER:
914
0
    {
915
0
      BOOL ok = FALSE;
916
0
      if (field->fixedArraySize)
917
0
        ok = ndr_encoder_write_items(encoder, s, (UINT32)ndr_inline_count(field),
918
0
                                     field->typeDescr, ptr);
919
0
      else
920
0
        ok = ndr_check_range(descr, field, ptr) &&
921
0
             field->typeDescr->writeFn(encoder, s, hints, ptr);
922
923
0
      if (!ok)
924
0
      {
925
0
        WLog_ERR(TAG, "error when writing %s.%s", descr->name, field->name);
926
0
        return FALSE;
927
0
      }
928
0
      return TRUE;
929
0
    }
930
931
0
    case WINPR_NDR_POINTER:
932
0
    case WINPR_NDR_POINTER_NON_NULL:
933
0
    case WINPR_NDR_POINTER_FULL: /* each pointer has its own referent */
934
0
    case WINPR_NDR_FIXED_ARRAY:
935
0
      break;
936
937
0
    default:
938
0
      WLog_ERR(TAG, "%s.%s unknown pointer type 0x%x", descr->name, field->name,
939
0
               field->pointerType);
940
0
      return FALSE;
941
0
  }
942
943
  /* the target of the deferred: the referent, or the pointer itself for a self allocated type */
944
0
  const void* target = ptr;
945
0
  if (field->pointerType == WINPR_NDR_FIXED_ARRAY)
946
0
  {
947
0
    if (!ndr_check_fixed_array(descr, field, hints))
948
0
      return FALSE;
949
950
    /* an empty array is sent as a null pointer */
951
0
    if (ndr_hintsCount(field->typeDescr, hints) == 0)
952
0
      ptr = nullptr;
953
0
  }
954
0
  else
955
0
  {
956
0
    ptr = *(WINPR_CAST_CONST_PTR_AWAY(ptr, const void**));
957
0
    if (field->typeDescr->arity != WINPR_NDR_ARITY_SELF_ALLOCATED)
958
0
      target = ptr;
959
960
0
    if (!ptr && field->pointerType == WINPR_NDR_POINTER_NON_NULL)
961
0
    {
962
0
      WLog_ERR(TAG, "%s.%s can't be null", descr->name, field->name);
963
0
      return FALSE;
964
0
    }
965
0
  }
966
967
0
  winpr_ndr_refid ptrId = WINPR_NDR_PTR_NULL;
968
0
  BOOL isNew = FALSE;
969
0
  if (ptr && !winpr_ndr_encoder_allocatePtr(encoder, ptr, &ptrId, &isNew))
970
0
  {
971
0
    WLog_ERR(TAG, "error allocating pointer for %s.%s", descr->name, field->name);
972
0
    return FALSE;
973
0
  }
974
975
0
  if (isNew)
976
0
  {
977
0
    const WinPrNdrDeferredEntry deferred = { ptrId, field->name,
978
0
                                           hints, WINPR_CAST_CONST_PTR_AWAY(target, void*),
979
0
                                           FALSE, field->typeDescr };
980
0
    if (!ndr_deferreds_add(&encoder->deferreds, &deferred))
981
0
      return FALSE;
982
0
  }
983
984
0
  return winpr_ndr_encoder_write_uint32(encoder, s, ptrId);
985
0
}
986
987
BOOL winpr_ndr_encoder_write_fromDescr(WinPrNdrEncoder* context, wStream* s,
988
                                       const WinPrNdrStructDescr* descr, const void* src)
989
0
{
990
0
  WINPR_ASSERT(context);
991
0
  WINPR_ASSERT(s);
992
0
  WINPR_ASSERT(descr);
993
0
  WINPR_ASSERT(src);
994
995
0
  if ((descr->alignment > 1) && !winpr_ndr_encoder_write_align(context, s, descr->alignment))
996
0
    return FALSE;
997
998
0
  BOOL ok = TRUE;
999
0
  ndr_deferreds_enter(&context->deferreds);
1000
0
  for (size_t i = 0; ok && (i < descr->nfields); i++)
1001
0
    ok = ndr_encoder_write_field(context, s, descr, &descr->fields[i], src);
1002
1003
0
  return ndr_deferreds_leave(&context->deferreds, ok);
1004
0
}
1005
1006
winpr_ndr_refid winpr_ndr_pointer_refid(const void* ptr)
1007
0
{
1008
0
  return (winpr_ndr_refid)((ULONG_PTR)ptr);
1009
0
}
1010
1011
BOOL winpr_ndr_encoder_allocatePtr(WinPrNdrEncoder* context, const void* ptr,
1012
                                   winpr_ndr_refid* prefId, BOOL* pnewPtr)
1013
0
{
1014
0
  WINPR_ASSERT(context);
1015
0
  WINPR_ASSERT(prefId);
1016
0
  WINPR_ASSERT(pnewPtr);
1017
0
  WINPR_UNUSED(ptr);
1018
1019
  /* pointers are unique pointers: each non-null pointer has its own referent, the decoder
1020
   * rejects aliased pointers */
1021
0
  if (context->refIdCounter > UINT32_MAX - 4)
1022
0
    return FALSE;
1023
1024
0
  *pnewPtr = TRUE;
1025
0
  *prefId = context->refIdCounter;
1026
0
  context->refIdCounter += 4;
1027
0
  return TRUE;
1028
0
}
1029
1030
BOOL winpr_ndr_encoder_treat_deferreds(WinPrNdrEncoder* context, wStream* s)
1031
0
{
1032
0
  WINPR_ASSERT(context);
1033
0
  WINPR_ASSERT(s);
1034
1035
0
  WinPrNdrDeferredEntry current = { 0 };
1036
0
  while (ndr_deferreds_pop(&context->deferreds, &current))
1037
0
  {
1038
0
    WLog_VRB(TAG, "treating write deferred for %s", current.name);
1039
0
    if (!current.msg->writeFn(context, s, current.hints, current.target))
1040
0
    {
1041
0
      WLog_ERR(TAG, "error writing deferred %s", current.name);
1042
0
      return FALSE;
1043
0
    }
1044
0
  }
1045
1046
0
  return TRUE;
1047
0
}
1048
1049
BOOL winpr_ndr_encoder_write_data(WinPrNdrEncoder* context, wStream* s, const void* data, size_t sz)
1050
0
{
1051
0
  if (!Stream_EnsureRemainingCapacity(s, sz))
1052
0
    return FALSE;
1053
1054
0
  Stream_Write(s, data, sz);
1055
0
  winpr_ndr_encoder_bytes_written(context, sz);
1056
0
  return TRUE;
1057
0
}
1058
1059
/* ====================================================================================== */
1060
1061
WinPrNdrDecoder* winpr_ndr_decoder_new(BOOL bigEndianDrep, BYTE version)
1062
0
{
1063
0
  WinPrNdrDecoder* ret = calloc(1, sizeof(*ret));
1064
0
  if (!ret)
1065
0
    return nullptr;
1066
1067
0
  ret->version = version;
1068
0
  ret->bigEndianDrep = bigEndianDrep;
1069
0
  ret->referents = HashTable_New(FALSE);
1070
0
  if (!ret->referents)
1071
0
  {
1072
0
    winpr_ndr_decoder_free(ret);
1073
0
    return nullptr;
1074
0
  }
1075
1076
0
  return ret;
1077
0
}
1078
1079
WinPrNdrDecoder* winpr_ndr_decoder_new_fromStream(wStream* s)
1080
0
{
1081
0
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
1082
0
    return nullptr;
1083
1084
0
  BYTE version = Stream_Get_UINT8(s);
1085
0
  BYTE drep = Stream_Get_UINT8(s);
1086
0
  UINT16 headerLen = Stream_Get_UINT16(s);
1087
1088
0
  if (version != 1)
1089
0
  {
1090
0
    WLog_ERR(TAG, "unsupported type serialization version %" PRIu8, version);
1091
0
    return nullptr;
1092
0
  }
1093
1094
0
  if (headerLen < 4 || !Stream_CheckAndLogRequiredLength(TAG, s, headerLen - 4))
1095
0
    return nullptr;
1096
1097
  /* skip filler */
1098
0
  Stream_Seek(s, headerLen - 4);
1099
1100
0
  return winpr_ndr_decoder_new((drep != 0x10), version);
1101
0
}
1102
1103
void winpr_ndr_decoder_free(WinPrNdrDecoder* context)
1104
0
{
1105
0
  if (context)
1106
0
  {
1107
    /* the referents belong to the pointers that reference them */
1108
0
    HashTable_Free(context->referents);
1109
0
    free(context);
1110
0
  }
1111
0
}
1112
1113
void winpr_ndr_decoder_bytes_read(WinPrNdrDecoder* context, size_t len)
1114
0
{
1115
0
  WINPR_ASSERT(context);
1116
0
  context->offset += len;
1117
0
}
1118
1119
BOOL winpr_ndr_decoder_skip_bytes(WinPrNdrDecoder* context, wStream* s, size_t nbytes)
1120
0
{
1121
0
  WINPR_ASSERT(context);
1122
1123
0
  if (!Stream_CheckAndLogRequiredLength(TAG, s, nbytes))
1124
0
    return FALSE;
1125
1126
0
  context->offset += nbytes;
1127
0
  Stream_Seek(s, nbytes);
1128
0
  return TRUE;
1129
0
}
1130
1131
BOOL winpr_ndr_decoder_read_align(WinPrNdrDecoder* context, wStream* s, size_t sz)
1132
0
{
1133
0
  WINPR_ASSERT(context);
1134
1135
0
  size_t rest = context->offset % sz;
1136
0
  if (rest)
1137
0
  {
1138
0
    size_t padding = (sz - rest);
1139
0
    if (!Stream_CheckAndLogRequiredLength(TAG, s, padding))
1140
0
      return FALSE;
1141
1142
0
    Stream_Seek(s, padding);
1143
0
    context->offset += padding;
1144
0
  }
1145
1146
0
  return TRUE;
1147
0
}
1148
1149
BOOL winpr_ndr_decoder_read_refpointer(WinPrNdrDecoder* context, wStream* s, winpr_ndr_refid* refId)
1150
0
{
1151
0
  return winpr_ndr_decoder_read_uint32(context, s, refId);
1152
0
}
1153
1154
BOOL winpr_ndr_decoder_read_object(WinPrNdrDecoder* context, wStream* s, wStream* object)
1155
0
{
1156
0
  WINPR_ASSERT(context);
1157
0
  WINPR_ASSERT(s);
1158
0
  WINPR_ASSERT(object);
1159
1160
0
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
1161
0
    return FALSE;
1162
1163
0
  UINT32 len = 0;
1164
0
  if (context->bigEndianDrep)
1165
0
    Stream_Read_UINT32_BE(s, len);
1166
0
  else
1167
0
    Stream_Read_UINT32(s, len);
1168
0
  Stream_Seek(s, 4); /* Filler */
1169
1170
0
  if (!Stream_CheckAndLogRequiredLength(TAG, s, len))
1171
0
    return FALSE;
1172
1173
0
  Stream_StaticInit(object, Stream_PointerAs(s, BYTE), len);
1174
0
  Stream_Seek(s, len);
1175
1176
  /* alignments are relative to the start of the object data */
1177
0
  context->offset = 0;
1178
0
  return TRUE;
1179
0
}
1180
1181
BOOL winpr_ndr_decoder_read_uint8(WinPrNdrDecoder* context, wStream* s, BYTE* v)
1182
0
{
1183
0
  WINPR_ASSERT(context);
1184
1185
0
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
1186
0
    return FALSE;
1187
1188
0
  Stream_Read_UINT8(s, *v);
1189
1190
0
  winpr_ndr_decoder_bytes_read(context, 1);
1191
0
  return TRUE;
1192
0
}
1193
1194
BOOL winpr_ndr_decoder_read_uint8_(WinPrNdrDecoder* context, wStream* s, const void* hints, void* v)
1195
0
{
1196
0
  WINPR_UNUSED(hints);
1197
0
  return winpr_ndr_decoder_read_uint8(context, s, (BYTE*)v);
1198
0
}
1199
1200
BOOL winpr_ndr_decoder_read_int8(WinPrNdrDecoder* context, wStream* s, INT8* v)
1201
0
{
1202
0
  WINPR_ASSERT(v);
1203
1204
0
  BYTE b = 0;
1205
0
  if (!winpr_ndr_decoder_read_uint8(context, s, &b))
1206
0
    return FALSE;
1207
0
  *v = (INT8)b;
1208
0
  return TRUE;
1209
0
}
1210
1211
BOOL winpr_ndr_decoder_read_int8_(WinPrNdrDecoder* context, wStream* s, const void* hints, void* v)
1212
0
{
1213
0
  WINPR_UNUSED(hints);
1214
0
  return winpr_ndr_decoder_read_int8(context, s, (INT8*)v);
1215
0
}
1216
1217
BOOL winpr_ndr_decoder_read_wchar(WinPrNdrDecoder* context, wStream* s, WCHAR* ptr)
1218
0
{
1219
0
  return winpr_ndr_decoder_read_uint16(context, s, (UINT16*)ptr);
1220
0
}
1221
1222
/** @brief reads the items of an array, their deferred pointers are treated after the array */
1223
static BOOL ndr_decoder_read_items(WinPrNdrDecoder* context, wStream* s, UINT32 count,
1224
                                   WinPrNdrMessageType itemType, BYTE* target)
1225
0
{
1226
0
  BOOL ok = TRUE;
1227
1228
0
  ndr_deferreds_enter(&context->deferreds);
1229
0
  for (UINT32 i = 0; ok && (i < count); i++, target += itemType->itemSize)
1230
0
    ok = itemType->readFn(context, s, nullptr, target);
1231
1232
0
  return ndr_deferreds_leave(&context->deferreds, ok);
1233
0
}
1234
1235
/** @brief checks the count of a conformant array against the hints
1236
 *
1237
 * For an array of simple items the hint is the size_is() of the array: the conformance must
1238
 * match it. Otherwise it's a number of bytes the items must fit in.
1239
 */
1240
static BOOL ndr_check_array_bound(const char* what, UINT32 count, WinPrNdrMessageType itemType,
1241
                                  UINT32 limit)
1242
0
{
1243
0
  if (itemType->arity == WINPR_NDR_ARITY_SIMPLE)
1244
0
  {
1245
0
    if (count != limit)
1246
0
    {
1247
0
      WLog_ERR(TAG, "%s: conformance %" PRIu32 " doesn't match the expected count %" PRIu32,
1248
0
               what, count, limit);
1249
0
      return FALSE;
1250
0
    }
1251
0
    return TRUE;
1252
0
  }
1253
1254
0
  if ((1ull * count * itemType->itemSize) > limit)
1255
0
  {
1256
0
    WLog_ERR(TAG, "%s: %" PRIu32 " items don't fit in %" PRIu32, what, count, limit);
1257
0
    return FALSE;
1258
0
  }
1259
0
  return TRUE;
1260
0
}
1261
1262
/** @brief checks the header of a conformant varying array, returns the number of items to read */
1263
static BOOL ndr_read_varying_array_header(WinPrNdrDecoder* context, wStream* s,
1264
                                          const WinPrNdrVaryingArrayHints* hints,
1265
                                          WinPrNdrMessageType itemType, UINT32* pmaxCount,
1266
                                          UINT32* plength)
1267
0
{
1268
0
  if (itemType->itemSize == 0)
1269
0
    return FALSE;
1270
1271
0
  UINT32 maxCount = 0;
1272
0
  UINT32 offset = 0;
1273
0
  UINT32 length = 0;
1274
1275
0
  if (!winpr_ndr_decoder_read_uint32(context, s, &maxCount) ||
1276
0
      !winpr_ndr_decoder_read_uint32(context, s, &offset) ||
1277
0
      !winpr_ndr_decoder_read_uint32(context, s, &length))
1278
0
    return FALSE;
1279
1280
0
  if (length > maxCount)
1281
0
  {
1282
0
    WLog_ERR(TAG, "varying array length %" PRIu32 " > maximum count %" PRIu32, length,
1283
0
             maxCount);
1284
0
    return FALSE;
1285
0
  }
1286
1287
0
  if (hints)
1288
0
  {
1289
0
    if ((1ull * length * itemType->itemSize) > hints->length)
1290
0
      return FALSE;
1291
1292
0
    if ((1ull * maxCount * itemType->itemSize) > hints->maxLength)
1293
0
      return FALSE;
1294
0
  }
1295
1296
0
  *pmaxCount = maxCount;
1297
0
  *plength = length;
1298
0
  return TRUE;
1299
0
}
1300
1301
BOOL winpr_ndr_decoder_read_uconformant_varying_array(WinPrNdrDecoder* context, wStream* s,
1302
                                                      const WinPrNdrVaryingArrayHints* hints,
1303
                                                      WinPrNdrMessageType itemType, void* target)
1304
0
{
1305
0
  WINPR_ASSERT(context);
1306
0
  WINPR_ASSERT(s);
1307
0
  WINPR_ASSERT(itemType);
1308
0
  WINPR_ASSERT(target);
1309
1310
0
  if (!hints)
1311
0
    return FALSE;
1312
1313
0
  UINT32 maxCount = 0;
1314
0
  UINT32 length = 0;
1315
0
  if (!ndr_read_varying_array_header(context, s, hints, itemType, &maxCount, &length))
1316
0
    return FALSE;
1317
1318
0
  return ndr_decoder_read_items(context, s, length, itemType, target);
1319
0
}
1320
1321
BOOL winpr_ndr_decoder_read_uconformant_array(WinPrNdrDecoder* context, wStream* s,
1322
                                              const WinPrNdrArrayHints* hints,
1323
                                              WinPrNdrMessageType itemType, void* vtarget)
1324
0
{
1325
0
  WINPR_ASSERT(context);
1326
0
  WINPR_ASSERT(s);
1327
0
  WINPR_ASSERT(itemType);
1328
0
  WINPR_ASSERT(vtarget);
1329
1330
0
  if (!hints)
1331
0
    return FALSE;
1332
1333
0
  if (itemType->itemSize == 0)
1334
0
    return FALSE;
1335
1336
0
  UINT32 count = 0;
1337
0
  if (!winpr_ndr_decoder_read_uint32(context, s, &count))
1338
0
    return FALSE;
1339
1340
0
  if (!ndr_check_array_bound("conformant array", count, itemType, hints->count))
1341
0
    return FALSE;
1342
1343
0
  return ndr_decoder_read_items(context, s, count, itemType, vtarget);
1344
0
}
1345
1346
/** @brief size of the referent of a pointer */
1347
static BOOL ndr_referent_size(const char* name, WinPrNdrMessageType descr, const void* hints,
1348
                              size_t* psize)
1349
0
{
1350
0
  if (descr->arity == WINPR_NDR_ARITY_SELF_ALLOCATED)
1351
0
  {
1352
0
    WLog_ERR(TAG, "%s: the size of a self allocated type isn't known", name);
1353
0
    return FALSE;
1354
0
  }
1355
1356
0
  if ((descr->arity != WINPR_NDR_ARITY_SIMPLE) && !hints)
1357
0
  {
1358
0
    WLog_ERR(TAG, "%s: no hints for an array", name);
1359
0
    return FALSE;
1360
0
  }
1361
1362
0
  if (descr->itemSize == 0)
1363
0
    return FALSE;
1364
1365
0
  const size_t count = ndr_hintsCount(descr, hints);
1366
0
  if (count > SIZE_MAX / descr->itemSize)
1367
0
    return FALSE;
1368
1369
  /* an empty array is still a valid referent */
1370
0
  *psize = (count ? count : 1) * descr->itemSize;
1371
0
  return TRUE;
1372
0
}
1373
1374
static void ndr_referent_copy_hints(NdrReferent* ref, const void* hints)
1375
0
{
1376
0
  ref->hasHints = FALSE;
1377
0
  if (!ref->typeDescr || !hints)
1378
0
    return;
1379
1380
0
  switch (ref->typeDescr->arity)
1381
0
  {
1382
0
    case WINPR_NDR_ARITY_ARRAYOF:
1383
0
      ref->hints.array = *(const WinPrNdrArrayHints*)hints;
1384
0
      ref->hasHints = TRUE;
1385
0
      break;
1386
0
    case WINPR_NDR_ARITY_VARYING_ARRAYOF:
1387
0
      ref->hints.varying = *(const WinPrNdrVaryingArrayHints*)hints;
1388
0
      ref->hasHints = TRUE;
1389
0
      break;
1390
0
    default:
1391
0
      break;
1392
0
  }
1393
0
}
1394
1395
static NdrReferent* ndr_referent_new(WinPrNdrMessageType descr, const void* hints, size_t size)
1396
0
{
1397
0
  const size_t headerSize = offsetof(NdrReferent, payload);
1398
0
  const size_t payloadSize = sizeof(((NdrReferent*)nullptr)->payload);
1399
0
  if (size > SIZE_MAX - headerSize - payloadSize)
1400
0
    return nullptr;
1401
1402
0
  NdrReferent* ref = calloc(1, headerSize + ((size > payloadSize) ? size : payloadSize));
1403
0
  if (!ref)
1404
0
    return nullptr;
1405
1406
0
  ref->magic = NDR_REFERENT_MAGIC;
1407
0
  ref->refCount = 1;
1408
0
  ref->typeDescr = descr;
1409
0
  ref->size = size;
1410
0
  ndr_referent_copy_hints(ref, hints);
1411
0
  return ref;
1412
0
}
1413
1414
void* winpr_ndr_pointer_new(WinPrNdrMessageType descr, const void* hints, size_t size)
1415
0
{
1416
0
  NdrReferent* ref = ndr_referent_new(descr, hints, size);
1417
0
  WINPR_PRAGMA_DIAG_PUSH
1418
0
  WINPR_DO_COVERITY_PRAGMA(
1419
0
      coverity compliance block
1420
0
          deviate 'RESOURCE_LEAK' 'returning a constant offset pointer, winpr_ndr_pointer_release does the inverse')
1421
0
  return ref ? ref->payload.data : nullptr;
1422
0
  WINPR_PRAGMA_DIAG_POP
1423
0
}
1424
1425
size_t winpr_ndr_pointer_size(const void* ptr)
1426
0
{
1427
0
  if (!ptr)
1428
0
    return 0;
1429
1430
0
  return ndr_referent(WINPR_CAST_CONST_PTR_AWAY(ptr, void*))->size;
1431
0
}
1432
1433
void* winpr_ndr_pointer_ref(void* ptr)
1434
0
{
1435
0
  if (!ptr)
1436
0
    return nullptr;
1437
1438
0
  NdrReferent* ref = ndr_referent(ptr);
1439
0
  InterlockedIncrement(&ref->refCount);
1440
0
  return ptr;
1441
0
}
1442
1443
void winpr_ndr_pointer_release(void* ptr)
1444
0
{
1445
0
  if (!ptr)
1446
0
    return;
1447
1448
0
  NdrReferent* ref = ndr_referent(ptr);
1449
0
  if (InterlockedDecrement(&ref->refCount) > 0)
1450
0
    return;
1451
1452
0
  if (ref->typeDescr && ref->typeDescr->destroyFn)
1453
0
    ref->typeDescr->destroyFn(ref->hasHints ? &ref->hints : nullptr, ref->payload.data);
1454
0
  ref->magic = 0;
1455
0
  free(ref);
1456
0
}
1457
1458
/** @brief records the referent of a referent id */
1459
static BOOL ndr_decoder_register(WinPrNdrDecoder* decoder, NdrReferent* ref, winpr_ndr_refid refId,
1460
                                 BOOL full)
1461
0
{
1462
0
  ref->refId = refId;
1463
0
  ref->full = full;
1464
0
  return HashTable_Insert(decoder->referents, (void*)(UINT_PTR)refId, ref);
1465
0
}
1466
1467
/** @brief checks that another full pointer to a referent has the same type and size */
1468
static BOOL ndr_referent_check(const char* name, const NdrReferent* ref, WinPrNdrMessageType descr,
1469
                               const void* hints, size_t size)
1470
0
{
1471
0
  if (!ref->full)
1472
0
  {
1473
0
    WLog_ERR(TAG, "%s: referent 0x%" PRIx32 " is the referent of a unique pointer", name,
1474
0
             ref->refId);
1475
0
    return FALSE;
1476
0
  }
1477
1478
0
  if ((ref->typeDescr != descr) || (ref->size != size))
1479
0
  {
1480
0
    WLog_ERR(TAG,
1481
0
             "%s: referent 0x%" PRIx32 " is aliased with another type or size (%" PRIuz
1482
0
             " vs %" PRIuz " bytes)",
1483
0
             name, ref->refId, size, ref->size);
1484
0
    return FALSE;
1485
0
  }
1486
1487
0
  BOOL same = TRUE;
1488
0
  switch (descr->arity)
1489
0
  {
1490
0
    case WINPR_NDR_ARITY_ARRAYOF:
1491
0
      same = memcmp(&ref->hints.array, hints, sizeof(ref->hints.array)) == 0;
1492
0
      break;
1493
0
    case WINPR_NDR_ARITY_VARYING_ARRAYOF:
1494
0
      same = memcmp(&ref->hints.varying, hints, sizeof(ref->hints.varying)) == 0;
1495
0
      break;
1496
0
    default:
1497
0
      break;
1498
0
  }
1499
1500
0
  if (!same)
1501
0
    WLog_ERR(TAG, "%s: referent 0x%" PRIx32 " is aliased with other hints", name, ref->refId);
1502
0
  return same;
1503
0
}
1504
1505
/** @brief reads a full pointer, the caller has entered a deferred block */
1506
static BOOL ndr_decoder_read_full_pointer(WinPrNdrDecoder* decoder, wStream* s, const char* name,
1507
                                          WinPrNdrMessageType descr, void* hints, void** target)
1508
0
{
1509
0
  WINPR_ASSERT(decoder);
1510
0
  WINPR_ASSERT(descr);
1511
0
  WINPR_ASSERT(target);
1512
1513
0
  *target = nullptr;
1514
1515
0
  winpr_ndr_refid refId = 0;
1516
0
  if (!winpr_ndr_decoder_read_refpointer(decoder, s, &refId))
1517
0
    return FALSE;
1518
0
  if (!refId)
1519
0
    return TRUE;
1520
1521
0
  size_t size = 0;
1522
0
  if (!ndr_referent_size(name, descr, hints, &size))
1523
0
    return FALSE;
1524
1525
0
  NdrReferent* ref = HashTable_GetItemValue(decoder->referents, (void*)(UINT_PTR)refId);
1526
0
  if (ref)
1527
0
  {
1528
0
    if (!ndr_referent_check(name, ref, descr, hints, size))
1529
0
      return FALSE;
1530
1531
0
    *target = winpr_ndr_pointer_ref(ref->payload.data);
1532
0
    return TRUE;
1533
0
  }
1534
1535
  /* each item of an array takes at least one byte on the wire */
1536
0
  if ((descr->arity == WINPR_NDR_ARITY_ARRAYOF) &&
1537
0
      !Stream_CheckAndLogRequiredLength(TAG, s, ndr_hintsCount(descr, hints)))
1538
0
    return FALSE;
1539
1540
0
  ref = ndr_referent_new(descr, hints, size);
1541
0
  if (!ref)
1542
0
    return FALSE;
1543
1544
0
  if (!ndr_decoder_register(decoder, ref, refId, TRUE))
1545
0
  {
1546
0
    free(ref);
1547
0
    return FALSE;
1548
0
  }
1549
1550
  /* the referent is read with the deferred pointers */
1551
0
  *target = ref->payload.data;
1552
0
  const WinPrNdrDeferredEntry deferred = { refId, name, hints, ref->payload.data, FALSE, descr };
1553
0
  return ndr_deferreds_add(&decoder->deferreds, &deferred);
1554
0
}
1555
1556
BOOL winpr_ndr_decoder_read_pointer_ex(WinPrNdrDecoder* context, wStream* s, const char* name,
1557
                                       WinPrNdrMessageType descr, void* hints, void** target)
1558
0
{
1559
0
  WINPR_ASSERT(context);
1560
1561
0
  ndr_deferreds_enter(&context->deferreds);
1562
0
  const BOOL ok = ndr_decoder_read_full_pointer(context, s, name, descr, hints, target);
1563
0
  return ndr_deferreds_leave(&context->deferreds, ok);
1564
0
}
1565
1566
static BOOL ndr_decoder_read_field(WinPrNdrDecoder* decoder, wStream* s,
1567
                                   const WinPrNdrStructDescr* descr,
1568
                                   const WinPrNdrFieldStruct* field, void* target)
1569
0
{
1570
0
  BYTE* ptr = (BYTE*)target + field->structOffset;
1571
0
  void* hints = ndr_field_hints(descr, field, target);
1572
1573
0
  switch (field->pointerType)
1574
0
  {
1575
0
    case WINPR_NDR_NOT_POINTER:
1576
0
    {
1577
0
      BOOL ok = FALSE;
1578
0
      if (field->fixedArraySize)
1579
0
        ok = ndr_decoder_read_items(decoder, s, (UINT32)ndr_inline_count(field),
1580
0
                                    field->typeDescr, ptr);
1581
0
      else
1582
0
        ok = field->typeDescr->readFn(decoder, s, hints, ptr) &&
1583
0
             ndr_check_range(descr, field, ptr);
1584
1585
0
      if (!ok)
1586
0
      {
1587
0
        WLog_ERR(TAG, "error when reading %s.%s", descr->name, field->name);
1588
0
        return FALSE;
1589
0
      }
1590
0
      return TRUE;
1591
0
    }
1592
1593
0
    case WINPR_NDR_POINTER_FULL:
1594
0
    {
1595
      /* the field is a pointer, possibly not aligned in the byte view of the struct */
1596
0
      void* referent = nullptr;
1597
0
      const BOOL ok = ndr_decoder_read_full_pointer(decoder, s, field->name, field->typeDescr,
1598
0
                                                    hints, &referent);
1599
1600
      /* stored even on failure, the referent is released with the structure */
1601
0
      memcpy(ptr, (const void*)&referent, sizeof(referent));
1602
0
      if (!ok)
1603
0
      {
1604
0
        WLog_ERR(TAG, "error when reading %s.%s", descr->name, field->name);
1605
0
        return FALSE;
1606
0
      }
1607
0
      return TRUE;
1608
0
    }
1609
1610
0
    case WINPR_NDR_POINTER:
1611
0
    case WINPR_NDR_POINTER_NON_NULL:
1612
0
    case WINPR_NDR_FIXED_ARRAY:
1613
0
      break;
1614
1615
0
    default:
1616
0
      WLog_ERR(TAG, "%s.%s unknown pointer type 0x%x", descr->name, field->name,
1617
0
               field->pointerType);
1618
0
      return FALSE;
1619
0
  }
1620
1621
0
  WinPrNdrDeferredEntry deferred = {
1622
0
    WINPR_NDR_PTR_NULL, field->name, hints, ptr, field->pointerType != WINPR_NDR_FIXED_ARRAY,
1623
0
    field->typeDescr
1624
0
  };
1625
0
  if (!winpr_ndr_decoder_read_refpointer(decoder, s, &deferred.ptrId))
1626
0
  {
1627
0
    WLog_ERR(TAG, "error when reading %s.%s", descr->name, field->name);
1628
0
    return FALSE;
1629
0
  }
1630
1631
0
  if (!deferred.ptrId)
1632
0
  {
1633
0
    if (field->pointerType == WINPR_NDR_POINTER_NON_NULL)
1634
0
    {
1635
0
      WLog_ERR(TAG, "%s.%s can't be null", descr->name, field->name);
1636
0
      return FALSE;
1637
0
    }
1638
1639
    /* the inline storage of a fixed array can't hold a null pointer to a non empty array */
1640
0
    if ((field->pointerType == WINPR_NDR_FIXED_ARRAY) &&
1641
0
        (!hints || (ndr_hintsCount(field->typeDescr, hints) != 0)))
1642
0
    {
1643
0
      WLog_ERR(TAG, "%s.%s is null but the array is not empty", descr->name, field->name);
1644
0
      return FALSE;
1645
0
    }
1646
0
    return TRUE;
1647
0
  }
1648
1649
0
  if (!deferred.allocate && !ndr_check_fixed_array(descr, field, hints))
1650
0
    return FALSE;
1651
1652
0
  return ndr_deferreds_add(&decoder->deferreds, &deferred);
1653
0
}
1654
1655
BOOL winpr_ndr_decoder_read_fromDescr(WinPrNdrDecoder* context, wStream* s,
1656
                                      const WinPrNdrStructDescr* descr, void* target)
1657
0
{
1658
0
  WINPR_ASSERT(context);
1659
0
  WINPR_ASSERT(s);
1660
0
  WINPR_ASSERT(descr);
1661
0
  WINPR_ASSERT(target);
1662
1663
0
  if ((descr->alignment > 1) && !winpr_ndr_decoder_read_align(context, s, descr->alignment))
1664
0
    return FALSE;
1665
1666
0
  BOOL ok = TRUE;
1667
0
  ndr_deferreds_enter(&context->deferreds);
1668
0
  for (size_t i = 0; ok && (i < descr->nfields); i++)
1669
0
    ok = ndr_decoder_read_field(context, s, descr, &descr->fields[i], target);
1670
1671
0
  return ndr_deferreds_leave(&context->deferreds, ok);
1672
0
}
1673
1674
BOOL winpr_ndr_decoder_treat_deferreds(WinPrNdrDecoder* context, wStream* s)
1675
0
{
1676
0
  WINPR_ASSERT(context);
1677
0
  WINPR_ASSERT(s);
1678
1679
0
  WinPrNdrDeferredEntry current = { 0 };
1680
0
  while (ndr_deferreds_pop(&context->deferreds, &current))
1681
0
  {
1682
0
    WLog_VRB(TAG, "treating read deferred 0x%x for %s", current.ptrId, current.name);
1683
1684
0
    BOOL ok = FALSE;
1685
0
    if (current.allocate)
1686
0
      ok = winpr_ndr_decoder_read_pointedMessageEx(context, s, current.ptrId, current.msg,
1687
0
                                                   current.hints, (void**)current.target);
1688
0
    else
1689
0
      ok = current.msg->readFn(context, s, current.hints, current.target);
1690
1691
0
    if (!ok)
1692
0
    {
1693
0
      WLog_ERR(TAG, "error parsing deferred %s", current.name);
1694
0
      return FALSE;
1695
0
    }
1696
0
  }
1697
1698
0
  return TRUE;
1699
0
}
1700
1701
BOOL winpr_ndr_decoder_read_pointedMessageEx(WinPrNdrDecoder* context, wStream* s,
1702
                                             winpr_ndr_refid ptrId, WinPrNdrMessageType descr,
1703
                                             void* hints, void** target)
1704
0
{
1705
0
  WINPR_ASSERT(context);
1706
0
  WINPR_ASSERT(s);
1707
0
  WINPR_ASSERT(descr);
1708
0
  WINPR_ASSERT(target);
1709
1710
0
  *target = nullptr;
1711
0
  if (!ptrId)
1712
0
    return TRUE;
1713
1714
  /* a unique pointer can't share its referent */
1715
0
  if (HashTable_Contains(context->referents, (void*)(UINT_PTR)ptrId))
1716
0
  {
1717
0
    WLog_ERR(TAG, "unique pointer 0x%" PRIx32 " is aliased", ptrId);
1718
0
    return FALSE;
1719
0
  }
1720
1721
0
  void* ret = nullptr;
1722
0
  if (descr->arity == WINPR_NDR_ARITY_SELF_ALLOCATED)
1723
0
  {
1724
    /* the reader allocates the referent with winpr_ndr_pointer_new() */
1725
0
    if (!descr->readFn(context, s, hints, (void*)&ret))
1726
0
      return FALSE;
1727
0
    if (!ret)
1728
0
      return FALSE;
1729
0
  }
1730
0
  else
1731
0
  {
1732
0
    size_t size = 0;
1733
0
    if (!ndr_referent_size("unique pointer", descr, hints, &size))
1734
0
      return FALSE;
1735
1736
    /* each item takes at least one byte on the wire, don't allocate for data that isn't
1737
     * there */
1738
0
    if ((descr->arity == WINPR_NDR_ARITY_ARRAYOF) &&
1739
0
        !Stream_CheckAndLogRequiredLength(TAG, s, ndr_hintsCount(descr, hints)))
1740
0
      return FALSE;
1741
1742
0
    ret = winpr_ndr_pointer_new(descr, hints, size);
1743
0
    if (!ret)
1744
0
      return FALSE;
1745
1746
0
    if (!descr->readFn(context, s, hints, ret))
1747
0
    {
1748
0
      winpr_ndr_pointer_release(ret);
1749
0
      return FALSE;
1750
0
    }
1751
0
  }
1752
1753
0
  if (!ndr_decoder_register(context, ndr_referent(ret), ptrId, FALSE))
1754
0
  {
1755
0
    winpr_ndr_pointer_release(ret);
1756
0
    return FALSE;
1757
0
  }
1758
1759
0
  *target = ret;
1760
0
  return TRUE;
1761
0
}
1762
1763
BOOL winpr_ndr_decoder_push_deferreds(WinPrNdrDecoder* context, WinPrNdrDeferredEntry* deferreds,
1764
                                      size_t ndeferred)
1765
0
{
1766
0
  WINPR_ASSERT(context);
1767
0
  WINPR_ASSERT(deferreds);
1768
1769
0
  BOOL ok = TRUE;
1770
0
  ndr_deferreds_enter(&context->deferreds);
1771
0
  for (size_t i = 0; ok && (i < ndeferred); i++)
1772
0
    ok = ndr_deferreds_add(&context->deferreds, &deferreds[i]);
1773
1774
0
  return ndr_deferreds_leave(&context->deferreds, ok);
1775
0
}
1776
1777
/* ========================================================================================================
1778
 */
1779
1780
/* ====================================== unions ========================================== */
1781
1782
/** @brief size of the discriminant of a union */
1783
static size_t ndr_union_switch_size(const WinPrNdrUnionDescr* descr)
1784
0
{
1785
0
  WINPR_ASSERT(descr);
1786
0
  WINPR_ASSERT((descr->switchSize == 0) || (descr->switchSize == 2) || (descr->switchSize == 4));
1787
0
  return descr->switchSize ? descr->switchSize : 4;
1788
0
}
1789
1790
UINT32 winpr_ndr_union_switch_value(const WinPrNdrUnionDescr* descr, const void* switchField)
1791
0
{
1792
0
  WINPR_ASSERT(switchField);
1793
1794
0
  if (ndr_union_switch_size(descr) == 2)
1795
0
    return *(const UINT16*)switchField;
1796
0
  return *(const UINT32*)switchField;
1797
0
}
1798
1799
/** @brief the arm of a union for a discriminant, the default arm if there's none */
1800
static const WinPrNdrUnionArm* ndr_union_find_arm(const WinPrNdrUnionDescr* descr,
1801
                                                  UINT32 switchValue)
1802
0
{
1803
0
  WINPR_ASSERT(descr);
1804
1805
0
  for (size_t i = 0; i < descr->narms; i++)
1806
0
  {
1807
0
    const WinPrNdrUnionArm* arm = &descr->arms[i];
1808
0
    if (arm->discriminant == switchValue)
1809
0
      return arm;
1810
0
  }
1811
0
  return descr->defaultArm;
1812
0
}
1813
1814
/** @brief the arm of a union for a discriminant, as a structure of one field */
1815
static const WinPrNdrUnionArm* ndr_union_arm(const WinPrNdrUnionDescr* descr, UINT32 switchValue,
1816
                                             WinPrNdrStructDescr* armStruct)
1817
0
{
1818
0
  WINPR_ASSERT(armStruct);
1819
1820
0
  const WinPrNdrUnionArm* arm = ndr_union_find_arm(descr, switchValue);
1821
0
  if (!arm)
1822
0
  {
1823
0
    WLog_ERR(TAG, "%s: no arm for the discriminant 0x%08" PRIx32, descr->name, switchValue);
1824
0
    return nullptr;
1825
0
  }
1826
1827
0
  WINPR_ASSERT(arm->field.hintsField < 0);
1828
0
  armStruct->name = descr->name;
1829
0
  armStruct->nfields = 1;
1830
0
  armStruct->fields = &arm->field;
1831
0
  armStruct->alignment = 0;
1832
0
  return arm;
1833
0
}
1834
1835
static BOOL ndr_encoder_write_discriminant(WinPrNdrEncoder* context, wStream* s,
1836
                                           const WinPrNdrUnionDescr* descr, UINT32 switchValue)
1837
0
{
1838
0
  if (ndr_union_switch_size(descr) == 4)
1839
0
    return winpr_ndr_encoder_write_uint32(context, s, switchValue);
1840
1841
0
  if (switchValue > UINT16_MAX)
1842
0
  {
1843
0
    WLog_ERR(TAG, "%s: discriminant 0x%08" PRIx32 " doesn't fit in 16 bits", descr->name,
1844
0
             switchValue);
1845
0
    return FALSE;
1846
0
  }
1847
0
  return winpr_ndr_encoder_write_uint16(context, s, (UINT16)switchValue);
1848
0
}
1849
1850
static BOOL ndr_decoder_read_discriminant(WinPrNdrDecoder* context, wStream* s,
1851
                                          const WinPrNdrUnionDescr* descr, UINT32* discriminant)
1852
0
{
1853
0
  if (ndr_union_switch_size(descr) == 4)
1854
0
    return winpr_ndr_decoder_read_uint32(context, s, discriminant);
1855
1856
0
  UINT16 v = 0;
1857
0
  if (!winpr_ndr_decoder_read_uint16(context, s, &v))
1858
0
    return FALSE;
1859
0
  *discriminant = v;
1860
0
  return TRUE;
1861
0
}
1862
1863
BOOL winpr_ndr_encoder_write_union(WinPrNdrEncoder* context, wStream* s,
1864
                                   const WinPrNdrUnionDescr* descr, UINT32 switchValue,
1865
                                   const void* src)
1866
0
{
1867
0
  WINPR_ASSERT(context);
1868
0
  WINPR_ASSERT(src);
1869
1870
0
  WinPrNdrStructDescr armStruct = WINPR_C_ARRAY_INIT;
1871
0
  const WinPrNdrUnionArm* arm = ndr_union_arm(descr, switchValue, &armStruct);
1872
0
  if (!arm || !ndr_encoder_write_discriminant(context, s, descr, switchValue))
1873
0
    return FALSE;
1874
1875
0
  BOOL ok = TRUE;
1876
0
  ndr_deferreds_enter(&context->deferreds);
1877
0
  if (arm->field.typeDescr)
1878
0
    ok = ndr_encoder_write_field(context, s, &armStruct, &arm->field, src);
1879
0
  return ndr_deferreds_leave(&context->deferreds, ok);
1880
0
}
1881
1882
BOOL winpr_ndr_decoder_read_union(WinPrNdrDecoder* context, wStream* s,
1883
                                  const WinPrNdrUnionDescr* descr, UINT32 switchValue, void* target)
1884
0
{
1885
0
  WINPR_ASSERT(context);
1886
0
  WINPR_ASSERT(target);
1887
1888
0
  UINT32 discriminant = 0;
1889
0
  if (!ndr_decoder_read_discriminant(context, s, descr, &discriminant))
1890
0
    return FALSE;
1891
1892
0
  if (discriminant != switchValue)
1893
0
  {
1894
0
    WLog_ERR(TAG,
1895
0
             "%s: discriminant 0x%08" PRIx32 " doesn't match the switch value 0x%08" PRIx32,
1896
0
             descr->name, discriminant, switchValue);
1897
0
    return FALSE;
1898
0
  }
1899
1900
0
  WinPrNdrStructDescr armStruct = WINPR_C_ARRAY_INIT;
1901
0
  const WinPrNdrUnionArm* arm = ndr_union_arm(descr, switchValue, &armStruct);
1902
0
  if (!arm)
1903
0
    return FALSE;
1904
1905
0
  BOOL ok = TRUE;
1906
0
  ndr_deferreds_enter(&context->deferreds);
1907
0
  if (arm->field.typeDescr)
1908
0
    ok = ndr_decoder_read_field(context, s, &armStruct, &arm->field, target);
1909
0
  return ndr_deferreds_leave(&context->deferreds, ok);
1910
0
}
1911
1912
void winpr_ndr_union_destroy(const WinPrNdrUnionDescr* descr, UINT32 switchValue, void* obj)
1913
0
{
1914
0
  WINPR_ASSERT(descr);
1915
0
  WINPR_ASSERT(obj);
1916
1917
0
  const WinPrNdrUnionArm* arm = ndr_union_find_arm(descr, switchValue);
1918
0
  if (!arm || !arm->field.typeDescr)
1919
0
    return;
1920
1921
0
  const WinPrNdrStructDescr armStruct = { descr->name, 1, &arm->field, 0 };
1922
0
  winpr_ndr_struct_destroy(&armStruct, obj);
1923
0
}
1924
1925
/* ======================================================================================== */
1926
1927
void winpr_ndr_struct_dump_fromDescr(wLog* logger, UINT32 lvl, size_t identLevel,
1928
                                     const WinPrNdrStructDescr* descr, const void* obj)
1929
0
{
1930
0
  char tabArray[30 + 1];
1931
0
  size_t ntabs = (identLevel <= 30) ? identLevel : 30;
1932
1933
0
  memset(tabArray, '\t', ntabs);
1934
0
  tabArray[ntabs] = 0;
1935
1936
0
  WLog_Print(logger, lvl, "%s%s", tabArray, descr->name);
1937
0
  for (size_t i = 0; i < descr->nfields; i++)
1938
0
  {
1939
0
    const WinPrNdrFieldStruct* field = &descr->fields[i];
1940
0
    const BYTE* ptr = (const BYTE*)obj + field->structOffset;
1941
1942
0
    switch (field->pointerType)
1943
0
    {
1944
0
      case WINPR_NDR_POINTER:
1945
0
      case WINPR_NDR_POINTER_NON_NULL:
1946
0
      case WINPR_NDR_POINTER_FULL:
1947
0
        ptr = *(WINPR_CAST_CONST_PTR_AWAY(ptr, const void**));
1948
0
        break;
1949
0
      case WINPR_NDR_NOT_POINTER:
1950
0
      case WINPR_NDR_FIXED_ARRAY:
1951
0
        break;
1952
0
      default:
1953
0
        WLog_ERR(TAG, "invalid field->pointerType");
1954
0
        break;
1955
0
    }
1956
1957
0
    WLog_Print(logger, lvl, "%s*%s:", tabArray, field->name);
1958
0
    if (field->typeDescr->dumpFn)
1959
0
      field->typeDescr->dumpFn(logger, lvl, identLevel + 1, ptr);
1960
0
    else
1961
0
      WLog_Print(logger, lvl, "%s\t<no dump function>", tabArray);
1962
0
  }
1963
0
}
1964
1965
void winpr_ndr_struct_destroy(const WinPrNdrStructDescr* descr, void* pptr)
1966
0
{
1967
0
  WINPR_ASSERT(descr);
1968
0
  WINPR_ASSERT(pptr);
1969
1970
0
  for (size_t i = 0; i < descr->nfields; i++)
1971
0
  {
1972
0
    const WinPrNdrFieldStruct* field = &descr->fields[i];
1973
0
    void* ptr = (BYTE*)pptr + field->structOffset;
1974
0
    void* hints = nullptr;
1975
1976
0
    if (field->hintsField >= 0)
1977
0
    {
1978
      /* computes the address of the hints field if any */
1979
0
      WINPR_ASSERT((size_t)field->hintsField < descr->nfields);
1980
0
      const WinPrNdrFieldStruct* hintsField = &descr->fields[field->hintsField];
1981
1982
0
      hints = (BYTE*)pptr + hintsField->structOffset;
1983
0
    }
1984
1985
0
    switch (field->pointerType)
1986
0
    {
1987
0
      case WINPR_NDR_POINTER:
1988
0
      case WINPR_NDR_POINTER_NON_NULL:
1989
0
      case WINPR_NDR_POINTER_FULL:
1990
0
      {
1991
        /* the referent is destroyed with its last reference */
1992
0
        void** pptrField = (void**)ptr;
1993
0
        winpr_ndr_pointer_release(*pptrField);
1994
0
        *pptrField = nullptr;
1995
0
      }
1996
0
      break;
1997
1998
0
      default:
1999
        /* NOT_POINTER and FIXED_ARRAY fields are stored inline in the struct */
2000
0
        if (field->typeDescr->destroyFn)
2001
0
          field->typeDescr->destroyFn(hints, ptr);
2002
0
        break;
2003
0
    }
2004
0
  }
2005
0
}