| 1 |
/** |
| 2 |
* FreeRDP: A Remote Desktop Protocol Implementation |
| 3 |
* Authentication redirection virtual channel |
| 4 |
* |
| 5 |
* Copyright 2024 David Fort <[email protected]> |
| 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 |
|
| 20 |
#ifndef CHANNELS_RDPEAR_NDR_H_ |
| 21 |
#define CHANNELS_RDPEAR_NDR_H_ |
| 22 |
|
| 23 |
#include <winpr/stream.h> |
| 24 |
#include <freerdp/api.h> |
| 25 |
|
| 26 |
#define NDR_PTR_NULL (0UL) |
| 27 |
|
| 28 |
#define NDR_SIMPLE_TYPE_DECL(LOWER, UPPER) \ |
| 29 |
WINPR_ATTR_NODISCARD FREERDP_LOCAL BOOL ndr_read_##LOWER(NdrContext* context, wStream* s, \ |
| 30 |
UPPER* v); \ |
| 31 |
WINPR_ATTR_NODISCARD FREERDP_LOCAL BOOL ndr_read_##LOWER##_(NdrContext* context, wStream* s, \ |
| 32 |
const void* hints, void* v); \ |
| 33 |
WINPR_ATTR_NODISCARD FREERDP_LOCAL BOOL ndr_write_##LOWER(NdrContext* context, wStream* s, \ |
| 34 |
UPPER v); \ |
| 35 |
WINPR_ATTR_NODISCARD FREERDP_LOCAL BOOL ndr_write_##LOWER##_( \ |
| 36 |
NdrContext* context, wStream* s, const void* hints, const void* v); \ |
| 37 |
FREERDP_LOCAL \ |
| 38 |
extern const NdrMessageDescr ndr_##LOWER##_descr_s; \ |
| 39 |
WINPR_ATTR_NODISCARD FREERDP_LOCAL NdrMessageType ndr_##LOWER##_descr(void) |
| 40 |
|
| 41 |
#define NDR_ARRAY_OF_TYPE_DECL(TYPE, UPPERTYPE) \ |
| 42 |
WINPR_ATTR_NODISCARD FREERDP_LOCAL BOOL ndr_read_##TYPE##Array( \ |
| 43 |
NdrContext* context, wStream* s, const void* hints, void* v); \ |
| 44 |
WINPR_ATTR_NODISCARD FREERDP_LOCAL BOOL ndr_write_##TYPE##Array( \ |
| 45 |
NdrContext* context, wStream* s, const void* hints, const void* v); \ |
| 46 |
FREERDP_LOCAL \ |
| 47 |
void ndr_destroy_##TYPE##Array(NdrContext* context, const void* hints, void* obj); \ |
| 48 |
FREERDP_LOCAL \ |
| 49 |
extern const NdrMessageDescr ndr_##TYPE##Array_descr_s; \ |
| 50 |
WINPR_ATTR_NODISCARD FREERDP_LOCAL NdrMessageType ndr_##TYPE##Array_descr(void); \ |
| 51 |
\ |
| 52 |
WINPR_ATTR_NODISCARD FREERDP_LOCAL BOOL ndr_read_##TYPE##VaryingArray( \ |
| 53 |
NdrContext* context, wStream* s, const void* hints, void* v); \ |
| 54 |
WINPR_ATTR_NODISCARD FREERDP_LOCAL BOOL ndr_write_##TYPE##VaryingArray( \ |
| 55 |
NdrContext* context, wStream* s, const void* hints, const void* v); \ |
| 56 |
extern const NdrMessageDescr ndr_##TYPE##VaryingArray_descr_s; \ |
| 57 |
NdrMessageType ndr_##TYPE##VaryingArray_descr(void) |
| 58 |
|
| 59 |
#ifdef __cplusplus |
| 60 |
extern "C" |
| 61 |
{ |
| 62 |
#endif |
| 63 |
|
| 64 |
typedef struct NdrContext_s NdrContext; |
| 65 |
|
| 66 |
typedef UINT32 ndr_refid; |
| 67 |
|
| 68 |
typedef BOOL (*NDR_READER_FN)(NdrContext* context, wStream* s, const void* hints, void* target); |
| 69 |
typedef BOOL (*NDR_WRITER_FN)(NdrContext* context, wStream* s, const void* hints, |
| 70 |
const void* obj); |
| 71 |
typedef void (*NDR_DESTROY_FN)(NdrContext* context, const void* hints, void* obj); |
| 72 |
typedef void (*NDR_DUMP_FN)(wLog* logger, UINT32 lvl, size_t indentLevel, const void* obj); |
| 73 |
|
| 74 |
/** @brief arity of a message */ |
| 75 |
typedef enum |
| 76 |
{ |
| 77 |
NDR_ARITY_SIMPLE, |
| 78 |
NDR_ARITY_ARRAYOF, |
| 79 |
NDR_ARITY_VARYING_ARRAYOF, |
| 80 |
} NdrTypeArity; |
| 81 |
|
| 82 |
/** @brief message descriptor */ |
| 83 |
typedef struct |
| 84 |
{ |
| 85 |
NdrTypeArity arity; |
| 86 |
size_t itemSize; |
| 87 |
WINPR_ATTR_NODISCARD NDR_READER_FN readFn; |
| 88 |
WINPR_ATTR_NODISCARD NDR_WRITER_FN writeFn; |
| 89 |
NDR_DESTROY_FN destroyFn; |
| 90 |
NDR_DUMP_FN dumpFn; |
| 91 |
} NdrMessageDescr; |
| 92 |
|
| 93 |
typedef const NdrMessageDescr* NdrMessageType; |
| 94 |
|
| 95 |
/** @brief pointer or not and if null is accepted */ |
| 96 |
typedef enum |
| 97 |
{ |
| 98 |
NDR_NOT_POINTER, |
| 99 |
NDR_POINTER_NON_NULL, |
| 100 |
NDR_POINTER |
| 101 |
} NdrPointerType; |
| 102 |
|
| 103 |
/** @brief descriptor of a field in a structure */ |
| 104 |
typedef struct |
| 105 |
{ |
| 106 |
const char* name; |
| 107 |
size_t structOffset; |
| 108 |
NdrPointerType pointerType; |
| 109 |
ssize_t hintsField; |
| 110 |
NdrMessageType typeDescr; |
| 111 |
} NdrFieldStruct; |
| 112 |
|
| 113 |
/** @brief structure descriptor */ |
| 114 |
typedef struct |
| 115 |
{ |
| 116 |
const char* name; |
| 117 |
size_t nfields; |
| 118 |
const NdrFieldStruct* fields; |
| 119 |
} NdrStructDescr; |
| 120 |
|
| 121 |
/** @brief a deferred pointer */ |
| 122 |
typedef struct |
| 123 |
{ |
| 124 |
ndr_refid ptrId; |
| 125 |
const char* name; |
| 126 |
void* hints; |
| 127 |
void* target; |
| 128 |
NdrMessageType msg; |
| 129 |
} NdrDeferredEntry; |
| 130 |
|
| 131 |
FREERDP_LOCAL void ndr_context_free(NdrContext* context); |
| 132 |
|
| 133 |
static inline void ndr_context_destroy(NdrContext** pcontext) |
| 134 |
{ |
| 135 |
WINPR_ASSERT(pcontext); |
| 136 |
ndr_context_free(*pcontext); |
| 137 |
*pcontext = nullptr; |
| 138 |
} |
| 139 |
|
| 140 |
WINPR_ATTR_MALLOC(ndr_context_free, 1) |
| 141 |
WINPR_ATTR_NODISCARD |
| 142 |
FREERDP_LOCAL NdrContext* ndr_context_new(BOOL bigEndianDrep, BYTE version); |
| 143 |
|
| 144 |
FREERDP_LOCAL void ndr_context_reset(NdrContext* context); |
| 145 |
|
| 146 |
WINPR_ATTR_MALLOC(ndr_context_free, 1) |
| 147 |
WINPR_ATTR_NODISCARD |
| 148 |
FREERDP_LOCAL NdrContext* ndr_context_copy(const NdrContext* src); |
| 149 |
|
| 150 |
WINPR_ATTR_MALLOC(ndr_context_free, 1) |
| 151 |
WINPR_ATTR_NODISCARD |
| 152 |
FREERDP_LOCAL NdrContext* ndr_read_header(wStream* s); |
| 153 |
|
| 154 |
WINPR_ATTR_NODISCARD FREERDP_LOCAL BOOL ndr_write_header(NdrContext* context, wStream* s); |
| 155 |
|
| 156 |
NDR_SIMPLE_TYPE_DECL(uint8, UINT8); |
| 157 |
NDR_SIMPLE_TYPE_DECL(uint16, UINT16); |
| 158 |
NDR_SIMPLE_TYPE_DECL(uint32, UINT32); |
| 159 |
NDR_SIMPLE_TYPE_DECL(uint64, UINT64); |
| 160 |
|
| 161 |
NDR_ARRAY_OF_TYPE_DECL(uint8, BYTE); |
| 162 |
NDR_ARRAY_OF_TYPE_DECL(uint16, UINT16); |
| 163 |
|
| 164 |
WINPR_ATTR_NODISCARD FREERDP_LOCAL BOOL ndr_skip_bytes(NdrContext* context, wStream* s, |
| 165 |
size_t nbytes); |
| 166 |
|
| 167 |
WINPR_ATTR_NODISCARD FREERDP_LOCAL BOOL ndr_read_align(NdrContext* context, wStream* s, |
| 168 |
size_t sz); |
| 169 |
|
| 170 |
WINPR_ATTR_NODISCARD FREERDP_LOCAL BOOL ndr_write_align(NdrContext* context, wStream* s, |
| 171 |
size_t sz); |
| 172 |
|
| 173 |
WINPR_ATTR_NODISCARD FREERDP_LOCAL BOOL ndr_write_data(NdrContext* context, wStream* s, |
| 174 |
const void* data, size_t sz); |
| 175 |
|
| 176 |
WINPR_ATTR_NODISCARD FREERDP_LOCAL BOOL ndr_read_pickle(NdrContext* context, wStream* s); |
| 177 |
|
| 178 |
WINPR_ATTR_NODISCARD FREERDP_LOCAL BOOL ndr_write_pickle(NdrContext* context, wStream* s); |
| 179 |
|
| 180 |
WINPR_ATTR_NODISCARD FREERDP_LOCAL BOOL ndr_read_constructed(NdrContext* context, wStream* s, |
| 181 |
wStream* target); |
| 182 |
|
| 183 |
WINPR_ATTR_NODISCARD FREERDP_LOCAL BOOL ndr_write_constructed(NdrContext* context, wStream* s, |
| 184 |
wStream* payload); |
| 185 |
|
| 186 |
WINPR_ATTR_NODISCARD FREERDP_LOCAL BOOL ndr_start_constructed(NdrContext* context, wStream* s); |
| 187 |
|
| 188 |
WINPR_ATTR_NODISCARD FREERDP_LOCAL BOOL ndr_end_constructed(NdrContext* context, wStream* s); |
| 189 |
|
| 190 |
WINPR_ATTR_NODISCARD FREERDP_LOCAL BOOL ndr_read_wchar(NdrContext* context, wStream* s, |
| 191 |
WCHAR* ptr); |
| 192 |
|
| 193 |
/** @brief hints for a varying conformant array */ |
| 194 |
typedef struct |
| 195 |
{ |
| 196 |
UINT32 length; |
| 197 |
UINT32 maxLength; |
| 198 |
} NdrVaryingArrayHints; |
| 199 |
|
| 200 |
WINPR_ATTR_NODISCARD FREERDP_LOCAL BOOL ndr_read_uconformant_varying_array( |
| 201 |
NdrContext* context, wStream* s, const NdrVaryingArrayHints* hints, NdrMessageType itemType, |
| 202 |
void* ptarget); |
| 203 |
|
| 204 |
WINPR_ATTR_NODISCARD FREERDP_LOCAL BOOL ndr_write_uconformant_varying_array( |
| 205 |
NdrContext* context, wStream* s, const NdrVaryingArrayHints* hints, NdrMessageType itemType, |
| 206 |
const void* src); |
| 207 |
|
| 208 |
/** @brief hints for a conformant array */ |
| 209 |
typedef struct |
| 210 |
{ |
| 211 |
UINT32 count; |
| 212 |
} NdrArrayHints; |
| 213 |
|
| 214 |
WINPR_ATTR_NODISCARD FREERDP_LOCAL BOOL ndr_read_uconformant_array(NdrContext* context, |
| 215 |
wStream* s, |
| 216 |
const NdrArrayHints* hints, |
| 217 |
NdrMessageType itemType, |
| 218 |
void* vtarget); |
| 219 |
|
| 220 |
WINPR_ATTR_NODISCARD FREERDP_LOCAL BOOL ndr_write_uconformant_array(NdrContext* context, |
| 221 |
wStream* s, UINT32 len, |
| 222 |
NdrMessageType itemType, |
| 223 |
const BYTE* ptr); |
| 224 |
|
| 225 |
WINPR_ATTR_NODISCARD FREERDP_LOCAL BOOL ndr_struct_read_fromDescr(NdrContext* context, |
| 226 |
wStream* s, |
| 227 |
const NdrStructDescr* descr, |
| 228 |
void* target); |
| 229 |
WINPR_ATTR_NODISCARD FREERDP_LOCAL BOOL ndr_struct_write_fromDescr(NdrContext* context, |
| 230 |
wStream* s, |
| 231 |
const NdrStructDescr* descr, |
| 232 |
const void* src); |
| 233 |
FREERDP_LOCAL |
| 234 |
void ndr_struct_dump_fromDescr(wLog* logger, UINT32 lvl, size_t identLevel, |
| 235 |
const NdrStructDescr* descr, const void* obj); |
| 236 |
FREERDP_LOCAL |
| 237 |
void ndr_struct_destroy(NdrContext* context, const NdrStructDescr* descr, void* pptr); |
| 238 |
|
| 239 |
WINPR_ATTR_NODISCARD FREERDP_LOCAL ndr_refid ndr_pointer_refid(const void* ptr); |
| 240 |
|
| 241 |
WINPR_ATTR_NODISCARD FREERDP_LOCAL BOOL ndr_read_refpointer(NdrContext* context, wStream* s, |
| 242 |
UINT32* refId); |
| 243 |
|
| 244 |
WINPR_ATTR_NODISCARD FREERDP_LOCAL BOOL ndr_context_allocatePtr(NdrContext* context, |
| 245 |
const void* ptr, |
| 246 |
ndr_refid* prefId, |
| 247 |
BOOL* pnewPtr); |
| 248 |
|
| 249 |
WINPR_ATTR_NODISCARD FREERDP_LOCAL BOOL ndr_read_pointedMessageEx(NdrContext* context, |
| 250 |
wStream* s, ndr_refid ptrId, |
| 251 |
NdrMessageType descr, |
| 252 |
void* hints, void** target); |
| 253 |
|
| 254 |
WINPR_ATTR_NODISCARD FREERDP_LOCAL BOOL ndr_push_deferreds(NdrContext* context, |
| 255 |
NdrDeferredEntry* deferreds, |
| 256 |
size_t ndeferred); |
| 257 |
|
| 258 |
WINPR_ATTR_NODISCARD FREERDP_LOCAL BOOL ndr_treat_deferred_read(NdrContext* context, |
| 259 |
wStream* s); |
| 260 |
|
| 261 |
WINPR_ATTR_NODISCARD FREERDP_LOCAL BOOL ndr_treat_deferred_write(NdrContext* context, |
| 262 |
wStream* s); |
| 263 |
|
| 264 |
#ifdef __cplusplus |
| 265 |
} |
| 266 |
#endif |
| 267 |
|
| 268 |
#endif /* CHANNELS_RDPEAR_NDR_H_ */ |