ReactOS 0.4.17-dev-650-gd0e71de
memres.c File Reference
#include <rtl.h>
#include <debug.h>
Include dependency graph for memres.c:

Go to the source code of this file.

Macros

#define NDEBUG
 

Functions

static ULONGLONG RtlpDecodeMemIoValue (_In_ UCHAR Type, _In_ USHORT Flags, _In_ ULONG Encoded)
 
ULONGLONG NTAPI RtlCmDecodeMemIoResource (_In_ PCM_PARTIAL_RESOURCE_DESCRIPTOR Descriptor, _Out_opt_ PULONGLONG Start)
 
ULONGLONG NTAPI RtlIoDecodeMemIoResource (_In_ PIO_RESOURCE_DESCRIPTOR Descriptor, _Out_opt_ PULONGLONG Alignment, _Out_opt_ PULONGLONG MinimumAddress, _Out_opt_ PULONGLONG MaximumAddress)
 
NTSTATUS NTAPI RtlCmEncodeMemIoResource (_In_ PCM_PARTIAL_RESOURCE_DESCRIPTOR Descriptor, _In_ UCHAR Type, _In_ ULONGLONG Length, _In_ ULONGLONG Start)
 
static BOOLEAN RtlpEncodeLargeAlignment (_In_ ULONGLONG Alignment, _In_ ULONG Shift, _Out_ PULONG Encoded)
 
NTSTATUS NTAPI RtlIoEncodeMemIoResource (_In_ PIO_RESOURCE_DESCRIPTOR Descriptor, _In_ UCHAR Type, _In_ ULONGLONG Length, _In_ ULONGLONG Alignment, _In_ ULONGLONG MinimumAddress, _In_ ULONGLONG MaximumAddress)
 
NTSTATUS NTAPI RtlFindClosestEncodableLength (_In_ ULONGLONG SourceLength, _Out_ PULONGLONG TargetLength)
 

Macro Definition Documentation

◆ NDEBUG

#define NDEBUG

Definition at line 12 of file memres.c.

Function Documentation

◆ RtlCmDecodeMemIoResource()

ULONGLONG NTAPI RtlCmDecodeMemIoResource ( _In_ PCM_PARTIAL_RESOURCE_DESCRIPTOR  Descriptor,
_Out_opt_ PULONGLONG  Start 
)

Definition at line 58 of file memres.c.

61{
62 if (Start != NULL)
63 *Start = Descriptor->u.Generic.Start.QuadPart;
64
66 Descriptor->Flags,
67 Descriptor->u.Generic.Length);
68}
static _Out_opt_ PULONGLONG Start
#define NULL
Definition: types.h:112
static ULONGLONG RtlpDecodeMemIoValue(_In_ UCHAR Type, _In_ USHORT Flags, _In_ ULONG Encoded)
Definition: memres.c:30
_Must_inspect_result_ _In_ WDFIORESLIST _In_ PIO_RESOURCE_DESCRIPTOR Descriptor
Definition: wdfresource.h:342

◆ RtlCmEncodeMemIoResource()

NTSTATUS NTAPI RtlCmEncodeMemIoResource ( _In_ PCM_PARTIAL_RESOURCE_DESCRIPTOR  Descriptor,
_In_ UCHAR  Type,
_In_ ULONGLONG  Length,
_In_ ULONGLONG  Start 
)

Definition at line 116 of file memres.c.

121{
122 if (Type != CmResourceTypePort &&
125 {
127 }
128
129 /* A port descriptor has only a 32-bit length and no large form. */
131 {
132 if (Length > MAXULONG)
134
136 Descriptor->u.Generic.Start.QuadPart = Start;
137 Descriptor->u.Generic.Length = (ULONG)Length;
138 return STATUS_SUCCESS;
139 }
140
141 /* Drop any stale large-form flags and record the start address. */
142 Descriptor->Flags &= ~CM_RESOURCE_MEMORY_LARGE;
143 Descriptor->u.Generic.Start.QuadPart = Start;
144
145 /* A length that fits 32 bits stays a plain memory descriptor. */
146 if (Length <= MAXULONG)
147 {
149 Descriptor->u.Generic.Length = (ULONG)Length;
150 return STATUS_SUCCESS;
151 }
152
153 /* Otherwise pick the tightest large form whose dropped low bits are zero. */
154 if (Length <= ((ULONGLONG)MAXULONG << 8) && (Length & 0xFF) == 0)
155 {
156 Descriptor->u.Generic.Length = (ULONG)(Length >> 8);
158 }
159 else if (Length <= ((ULONGLONG)MAXULONG << 16) && (Length & 0xFFFF) == 0)
160 {
161 Descriptor->u.Generic.Length = (ULONG)(Length >> 16);
163 }
164 else if (Length <= ((ULONGLONG)MAXULONG << 32) && (Length & 0xFFFFFFFF) == 0)
165 {
166 Descriptor->u.Generic.Length = (ULONG)(Length >> 32);
168 }
169 else
170 {
171 return STATUS_UNSUCCESSFUL;
172 }
173
175 return STATUS_SUCCESS;
176}
Type
Definition: Type.h:7
#define CM_RESOURCE_MEMORY_LARGE_40
Definition: cmtypes.h:134
#define CM_RESOURCE_MEMORY_LARGE_64
Definition: cmtypes.h:136
#define CM_RESOURCE_MEMORY_LARGE_48
Definition: cmtypes.h:135
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:102
#define CmResourceTypeMemory
Definition: restypes.h:106
#define CmResourceTypePort
Definition: restypes.h:104
#define STATUS_SUCCESS
Definition: shellext.h:65
#define MAXULONG
Definition: typedefs.h:251
uint64_t ULONGLONG
Definition: typedefs.h:67
uint32_t ULONG
Definition: typedefs.h:59
#define STATUS_INVALID_PARAMETER
Definition: udferr_usr.h:135
#define STATUS_UNSUCCESSFUL
Definition: udferr_usr.h:132
#define CmResourceTypeMemoryLarge
Definition: cmtypes.h:231

◆ RtlFindClosestEncodableLength()

NTSTATUS NTAPI RtlFindClosestEncodableLength ( _In_ ULONGLONG  SourceLength,
_Out_ PULONGLONG  TargetLength 
)

Definition at line 310 of file memres.c.

313{
314 if (SourceLength <= MAXULONG)
315 *TargetLength = SourceLength;
316 else if (SourceLength <= ((ULONGLONG)MAXULONG << 8))
317 *TargetLength = (SourceLength + 0xFF) & ~(ULONGLONG)0xFF;
318 else if (SourceLength <= ((ULONGLONG)MAXULONG << 16))
319 *TargetLength = (SourceLength + 0xFFFF) & ~(ULONGLONG)0xFFFF;
320 else if (SourceLength <= ((ULONGLONG)MAXULONG << 32))
321 *TargetLength = (SourceLength + 0xFFFFFFFF) & ~(ULONGLONG)0xFFFFFFFF;
322 else
323 {
324 *TargetLength = 0;
325 return STATUS_UNSUCCESSFUL;
326 }
327
328 return STATUS_SUCCESS;
329}
static _Out_ PULONGLONG TargetLength

◆ RtlIoDecodeMemIoResource()

ULONGLONG NTAPI RtlIoDecodeMemIoResource ( _In_ PIO_RESOURCE_DESCRIPTOR  Descriptor,
_Out_opt_ PULONGLONG  Alignment,
_Out_opt_ PULONGLONG  MinimumAddress,
_Out_opt_ PULONGLONG  MaximumAddress 
)

Definition at line 79 of file memres.c.

84{
85 if (Alignment != NULL)
86 {
88 Descriptor->Flags,
89 Descriptor->u.Generic.Alignment);
90 }
91
92 if (MinimumAddress != NULL)
93 *MinimumAddress = Descriptor->u.Generic.MinimumAddress.QuadPart;
94
95 if (MaximumAddress != NULL)
96 *MaximumAddress = Descriptor->u.Generic.MaximumAddress.QuadPart;
97
99 Descriptor->Flags,
100 Descriptor->u.Generic.Length);
101}
static _Out_opt_ PULONGLONG _Out_opt_ PULONGLONG MinimumAddress
static _Out_opt_ PULONGLONG _Out_opt_ PULONGLONG _Out_opt_ PULONGLONG MaximumAddress

◆ RtlIoEncodeMemIoResource()

NTSTATUS NTAPI RtlIoEncodeMemIoResource ( _In_ PIO_RESOURCE_DESCRIPTOR  Descriptor,
_In_ UCHAR  Type,
_In_ ULONGLONG  Length,
_In_ ULONGLONG  Alignment,
_In_ ULONGLONG  MinimumAddress,
_In_ ULONGLONG  MaximumAddress 
)

Definition at line 215 of file memres.c.

222{
223 ULONG Shift;
224 USHORT LargeFlag;
225
226 if (Type != CmResourceTypePort &&
229 {
231 }
232
233 /* A port descriptor is limited to 32-bit length and alignment. */
235 {
238
240 Descriptor->u.Generic.MinimumAddress.QuadPart = MinimumAddress;
241 Descriptor->u.Generic.MaximumAddress.QuadPart = MaximumAddress;
242 Descriptor->u.Generic.Length = (ULONG)Length;
243 Descriptor->u.Generic.Alignment = (ULONG)Alignment;
244 return STATUS_SUCCESS;
245 }
246
247 /* Memory: reset any stale large-form flags and record the address bounds. */
248 Descriptor->Flags &= ~CM_RESOURCE_MEMORY_LARGE;
249 Descriptor->u.Generic.MinimumAddress.QuadPart = MinimumAddress;
250 Descriptor->u.Generic.MaximumAddress.QuadPart = MaximumAddress;
251
252 /* Both values fit 32 bits: a plain memory descriptor. */
253 if (Length <= MAXULONG && Alignment <= MAXULONG)
254 {
256 Descriptor->u.Generic.Length = (ULONG)Length;
257 Descriptor->u.Generic.Alignment = (ULONG)Alignment;
258 return STATUS_SUCCESS;
259 }
260
261 /* Select the large form from the length; the alignment must fit that tier. */
262 if (Length <= ((ULONGLONG)MAXULONG << 8))
263 {
264 if (Alignment > ((ULONGLONG)MAXULONG << 8))
265 return STATUS_UNSUCCESSFUL;
266
267 Shift = 8;
268 LargeFlag = CM_RESOURCE_MEMORY_LARGE_40;
269 }
270 else if (Length <= ((ULONGLONG)MAXULONG << 16))
271 {
272 if (Alignment > ((ULONGLONG)MAXULONG << 16))
273 return STATUS_UNSUCCESSFUL;
274
275 Shift = 16;
276 LargeFlag = CM_RESOURCE_MEMORY_LARGE_48;
277 }
278 else if (Length <= ((ULONGLONG)MAXULONG << 32))
279 {
280 Shift = 32;
281 LargeFlag = CM_RESOURCE_MEMORY_LARGE_64;
282 }
283 else
284 {
285 return STATUS_UNSUCCESSFUL;
286 }
287
288 /* The length must be exactly representable at this granularity. */
289 if ((Length & (((ULONGLONG)1 << Shift) - 1)) != 0)
290 return STATUS_UNSUCCESSFUL;
291
292 if (!RtlpEncodeLargeAlignment(Alignment, Shift, &Descriptor->u.Generic.Alignment))
293 return STATUS_UNSUCCESSFUL;
294
296 Descriptor->u.Generic.Length = (ULONG)(Length >> Shift);
297 Descriptor->Flags |= LargeFlag;
298 return STATUS_SUCCESS;
299}
static BOOLEAN RtlpEncodeLargeAlignment(_In_ ULONGLONG Alignment, _In_ ULONG Shift, _Out_ PULONG Encoded)
Definition: memres.c:184
unsigned short USHORT
Definition: pedump.c:61
_In_ ULONG Shift
Definition: rtlfuncs.h:2698

◆ RtlpDecodeMemIoValue()

static ULONGLONG RtlpDecodeMemIoValue ( _In_ UCHAR  Type,
_In_ USHORT  Flags,
_In_ ULONG  Encoded 
)
static

Definition at line 30 of file memres.c.

34{
36 return Encoded;
37
39 return (ULONGLONG)Encoded << 8;
40
42 return (ULONGLONG)Encoded << 16;
43
45 return (ULONGLONG)Encoded << 32;
46
47 return 0;
48}
_Must_inspect_result_ _In_ ULONG Flags
Definition: wsk.h:170

Referenced by RtlCmDecodeMemIoResource(), and RtlIoDecodeMemIoResource().

◆ RtlpEncodeLargeAlignment()

static BOOLEAN RtlpEncodeLargeAlignment ( _In_ ULONGLONG  Alignment,
_In_ ULONG  Shift,
_Out_ PULONG  Encoded 
)
static

Definition at line 184 of file memres.c.

188{
189 ULONGLONG LowMask = ((ULONGLONG)1 << Shift) - 1;
190
191 while ((Alignment & LowMask) != 0)
192 {
193 ULONGLONG Doubled = Alignment << 1;
194
195 if (Doubled < Alignment) /* overflowed past 64 bits */
196 return FALSE;
197
198 Alignment = Doubled;
199 }
200
201 *Encoded = (ULONG)(Alignment >> Shift);
202 return TRUE;
203}
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117

Referenced by RtlIoEncodeMemIoResource().