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