ReactOS 0.4.15-dev-8222-g9164419
util.c File Reference
#include "../precomp.h"
#include "wine/debug.h"
Include dependency graph for util.c:

Go to the source code of this file.

Macros

#define NTLM_ALLOC_TAG   "NTLM"
 
#define NTLM_ALLOC_TAG_SIZE   strlen(NTLM_ALLOC_TAG)
 

Functions

 WINE_DEFAULT_DEBUG_CHANNEL (ntlm)
 
PVOID NtlmAllocate (_In_ size_t Size, _In_ bool UsePrivateLsaHeap)
 
VOID NtlmFree (_In_ PVOID Buffer, _In_ bool FromPrivateLsaHeap)
 
bool NtlmUStrAlloc (_Inout_ PUNICODE_STRING Dst, _In_ UINT16 SizeInBytes, _In_ UINT16 InitLength)
 
VOID NtlmUStrFree (_In_ PUNICODE_STRING String)
 
static bool NtlmStructWriteStrW (_In_ PVOID DataStart, _In_ ULONG DataSize, _Out_ PWCHAR *DstDataWPtr, _In_ const WCHAR *SrcDataW, _In_ ULONG SrcDataLen, _Inout_ PBYTE *AbsoluteOffsetPtr, _In_ bool TerminateWith0)
 Helper to fill a WCHAR-String in a struct. The stringdata is appended to the struct. The function does not allocate memory.
 
bool NtlmUStrWriteToStruct (_In_ PVOID DataStart, _In_ ULONG DataSize, _Out_ PUNICODE_STRING DstData, _In_ const PUNICODE_STRING SrcData, _Inout_ PBYTE *AbsoluteOffsetPtr, _In_ bool TerminateWith0)
 
bool NtlmFixupAndValidateUStr (_Inout_ PUNICODE_STRING String, _In_ ULONG_PTR FixupOffset)
 
bool NtlmFixupAStr (_Inout_ PSTRING String, _In_ ULONG_PTR FixupOffset)
 
NTSTATUS NtlmAllocateClientBuffer (_In_ PLSA_CLIENT_REQUEST ClientRequest, _In_ ULONG BufferLength, _Inout_ PNTLM_CLIENT_BUFFER Buffer)
 
NTSTATUS NtlmCopyToClientBuffer (_In_ PLSA_CLIENT_REQUEST ClientRequest, _In_ ULONG BufferLength, _Inout_ PNTLM_CLIENT_BUFFER Buffer)
 
VOID NtlmFreeClientBuffer (_In_ PLSA_CLIENT_REQUEST ClientRequest, _In_ bool FreeClientBuffer, _Inout_ PNTLM_CLIENT_BUFFER Buffer)
 

Macro Definition Documentation

◆ NTLM_ALLOC_TAG

#define NTLM_ALLOC_TAG   "NTLM"

Definition at line 14 of file util.c.

◆ NTLM_ALLOC_TAG_SIZE

#define NTLM_ALLOC_TAG_SIZE   strlen(NTLM_ALLOC_TAG)

Definition at line 15 of file util.c.

Function Documentation

◆ NtlmAllocate()

PVOID NtlmAllocate ( _In_ size_t  Size,
_In_ bool  UsePrivateLsaHeap 
)

Definition at line 18 of file util.c.

21{
23
24 if (Size == 0)
25 {
26 ERR("Allocating 0 bytes!\n");
27 return NULL;
28 }
29
31
32 switch (NtlmMode)
33 {
34 case NtlmLsaMode:
35 {
36 if (UsePrivateLsaHeap)
38 else
40
41 if (buffer != NULL)
43 break;
44 }
45 case NtlmUserMode:
46 {
48 break;
49 }
50 default:
51 {
52 ERR("NtlmState unknown!\n");
53 break;
54 }
55 }
56
59
60 return buffer;
61}
#define ERR(fmt,...)
Definition: debug.h:113
#define NULL
Definition: types.h:112
#define GetProcessHeap()
Definition: compat.h:736
#define HeapAlloc
Definition: compat.h:733
#define HEAP_ZERO_MEMORY
Definition: compat.h:134
#define NTLM_ALLOC_TAG_SIZE
Definition: util.c:15
#define NTLM_ALLOC_TAG
Definition: util.c:14
GLuint buffer
Definition: glext.h:5915
@ NtlmLsaMode
Definition: global.h:25
@ NtlmUserMode
Definition: global.h:26
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
NTLM_MODE NtlmMode
Definition: global.c:15
PLSA_SECPKG_FUNCTION_TABLE LsaFunctions
Definition: global.c:19
BYTE * PBYTE
Definition: pedump.c:66
PLSA_ALLOCATE_LSA_HEAP AllocateLsaHeap
Definition: ntsecpkg.h:314
PLSA_ALLOCATE_PRIVATE_HEAP AllocatePrivateHeap
Definition: ntsecpkg.h:363
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:262
_Must_inspect_result_ _In_ WDFDEVICE _In_ PWDF_DEVICE_PROPERTY_DATA _In_ DEVPROPTYPE _In_ ULONG Size
Definition: wdfdevice.h:4533

Referenced by NtlmAllocateClientBuffer(), and NtlmUStrAlloc().

◆ NtlmAllocateClientBuffer()

NTSTATUS NtlmAllocateClientBuffer ( _In_ PLSA_CLIENT_REQUEST  ClientRequest,
_In_ ULONG  BufferLength,
_Inout_ PNTLM_CLIENT_BUFFER  Buffer 
)

Definition at line 264 of file util.c.

268{
270
271 if (!Buffer)
272 return STATUS_NO_MEMORY;
273
274 Buffer->LocalBuffer = NtlmAllocate(BufferLength, false);
275 if (!Buffer->LocalBuffer)
276 return STATUS_NO_MEMORY;
277
278 if ((HANDLE)ClientRequest == INVALID_HANDLE_VALUE)
279 {
280 Buffer->ClientBaseAddress = Buffer->LocalBuffer;
281 //if (!ClientBaseAddress)
282 // return STATUS_INSUFFICIENT_RESOURCES;
283 }
284 else
285 {
286 Status = DispatchTable.AllocateClientBuffer(ClientRequest,
288 &Buffer->ClientBaseAddress);
289 if (!NT_SUCCESS(Status))
290 {
291 NtlmFree(Buffer->LocalBuffer, false);
292 Buffer->LocalBuffer = NULL;
293 }
294 //FIXME: Maybe we have to free ClientBaseAddress if something
295 // goes wrong ...? I'm not sure about that ...
296 }
297 return Status;
298}
LONG NTSTATUS
Definition: precomp.h:26
Definition: bufpool.h:45
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define INVALID_HANDLE_VALUE
Definition: compat.h:731
VOID NtlmFree(_In_ PVOID Buffer, _In_ bool FromPrivateLsaHeap)
Definition: util.c:64
PVOID NtlmAllocate(_In_ size_t Size, _In_ bool UsePrivateLsaHeap)
Definition: util.c:18
Status
Definition: gdiplustypes.h:25
#define STATUS_NO_MEMORY
Definition: ntstatus.h:260
#define STATUS_SUCCESS
Definition: shellext.h:65
_Must_inspect_result_ _In_ WDFDEVICE _In_ DEVICE_REGISTRY_PROPERTY _In_ ULONG BufferLength
Definition: wdfdevice.h:3771
_In_ PWDFDEVICE_INIT _In_ PWDF_PDO_EVENT_CALLBACKS DispatchTable
Definition: wdfpdo.h:248

Referenced by BuildLm20LogonProfileBuffer().

◆ NtlmCopyToClientBuffer()

NTSTATUS NtlmCopyToClientBuffer ( _In_ PLSA_CLIENT_REQUEST  ClientRequest,
_In_ ULONG  BufferLength,
_Inout_ PNTLM_CLIENT_BUFFER  Buffer 
)

Definition at line 301 of file util.c.

305{
307
308 if ((HANDLE)ClientRequest == INVALID_HANDLE_VALUE)
309 {
310 // If ClientRequest ist INVALID_HANDLE_VALUE
311 // Buffer->LocalBuffer == Buffer->ClientBaseAddress
312 if (Buffer->ClientBaseAddress != Buffer->LocalBuffer)
313 {
314 ERR("Buffer->ClientBaseAddress != Buffer->LocalBuffer (something must be wrong!)\n");
316 }
317 }
318 else
319 {
320 if (!Buffer->ClientBaseAddress ||
321 !Buffer->LocalBuffer)
322 {
323 ERR("Invalid Buffer - not allocated!\n");
324 return STATUS_NO_MEMORY;
325 }
326 Status = DispatchTable.CopyToClientBuffer(ClientRequest,
328 Buffer->ClientBaseAddress,
329 Buffer->LocalBuffer);
330 }
331 return Status;
332}
#define STATUS_INTERNAL_ERROR
Definition: ntstatus.h:465

Referenced by BuildLm20LogonProfileBuffer().

◆ NtlmFixupAndValidateUStr()

bool NtlmFixupAndValidateUStr ( _Inout_ PUNICODE_STRING  String,
_In_ ULONG_PTR  FixupOffset 
)

Definition at line 223 of file util.c.

226{
228
229 if (String->Length)
230 {
231 String->Buffer = FIXUP_POINTER(String->Buffer, FixupOffset);
232 String->MaximumLength = String->Length;
233 }
234 else
235 {
236 String->Buffer = NULL;
237 String->MaximumLength = 0;
238 }
239
241 return NT_SUCCESS(Status);
242}
#define FIXUP_POINTER(Pointer, Offset)
Definition: msv1_0.h:9
NTSYSAPI NTSTATUS NTAPI RtlValidateUnicodeString(_In_ ULONG Flags, _In_ PCUNICODE_STRING String)
Definition: unicode.c:2605
_Must_inspect_result_ _In_ WDFDEVICE _In_ WDFSTRING String
Definition: wdfdevice.h:2433

Referenced by LsaApLogonUserEx2_Network().

◆ NtlmFixupAStr()

bool NtlmFixupAStr ( _Inout_ PSTRING  String,
_In_ ULONG_PTR  FixupOffset 
)

Definition at line 245 of file util.c.

248{
249 if (String->Length)
250 {
251 String->Buffer = (PCHAR)FIXUP_POINTER(String->Buffer, FixupOffset);
252 String->MaximumLength = String->Length;
253 }
254 else
255 {
256 String->Buffer = NULL;
257 String->MaximumLength = 0;
258 }
259
260 return true;
261}
#define PCHAR
Definition: match.c:90

Referenced by LsaApLogonUserEx2_Network().

◆ NtlmFree()

VOID NtlmFree ( _In_ PVOID  Buffer,
_In_ bool  FromPrivateLsaHeap 
)

Definition at line 64 of file util.c.

67{
68 if (Buffer)
69 {
72 *(char*)Buffer = 'D';
73
74 switch (NtlmMode)
75 {
76 case NtlmLsaMode:
77 {
78 if (FromPrivateLsaHeap)
80 else
82 break;
83 }
84 case NtlmUserMode:
85 {
87 break;
88 }
89 default:
90 {
91 ERR("NtlmState unknown!\n");
92 break;
93 }
94 }
95 }
96 else
97 {
98 ERR("Trying to free NULL!\n");
99 }
100}
int memcmp(void *Buffer1, void *Buffer2, ACPI_SIZE Count)
Definition: utclib.c:112
#define HeapFree(x, y, z)
Definition: compat.h:735
#define ASSERT(a)
Definition: mode.c:44
PLSA_FREE_PRIVATE_HEAP FreePrivateHeap
Definition: ntsecpkg.h:364
PLSA_FREE_LSA_HEAP FreeLsaHeap
Definition: ntsecpkg.h:315

Referenced by NtlmAllocateClientBuffer(), NtlmFreeClientBuffer(), and NtlmUStrFree().

◆ NtlmFreeClientBuffer()

VOID NtlmFreeClientBuffer ( _In_ PLSA_CLIENT_REQUEST  ClientRequest,
_In_ bool  FreeClientBuffer,
_Inout_ PNTLM_CLIENT_BUFFER  Buffer 
)

Definition at line 335 of file util.c.

339{
340 if (!Buffer->ClientBaseAddress)
341 return;
342
343 if ((HANDLE)ClientRequest == INVALID_HANDLE_VALUE)
344 {
345 if (Buffer->ClientBaseAddress != Buffer->LocalBuffer)
346 {
347 ERR("Buffer->ClientBaseAddress != Buffer->LocalBuffer (something must be wrong!)\n");
348 return;
349 }
350 // LocalBuffer and ClientBaseAddress is the same
351 // so we have only to free it if FreeClientBuffer is TRUE.
352 Buffer->LocalBuffer = NULL;
353 if (FreeClientBuffer)
354 {
355 NtlmFree(Buffer->ClientBaseAddress, false);
356 Buffer->ClientBaseAddress = NULL;
357 }
358 }
359 else
360 {
361 NtlmFree(Buffer->LocalBuffer, false);
362 Buffer->LocalBuffer = NULL;
363 if (FreeClientBuffer)
364 DispatchTable.FreeClientBuffer(ClientRequest, Buffer->ClientBaseAddress);
365 Buffer->ClientBaseAddress = NULL;
366 }
367}

Referenced by BuildLm20LogonProfileBuffer().

◆ NtlmStructWriteStrW()

static bool NtlmStructWriteStrW ( _In_ PVOID  DataStart,
_In_ ULONG  DataSize,
_Out_ PWCHAR DstDataWPtr,
_In_ const WCHAR SrcDataW,
_In_ ULONG  SrcDataLen,
_Inout_ PBYTE AbsoluteOffsetPtr,
_In_ bool  TerminateWith0 
)
static

Helper to fill a WCHAR-String in a struct. The stringdata is appended to the struct. The function does not allocate memory.

Parameters
[in]DataStartStart address of the struct
[in]DataSizeSize of allocated memory (including payload)
[out]DstDataWPtrPointer to the WCHAR* datafield. The address of the data will be written to it.
[in]SrcDataWData to write/append at pOffset (payload). pOffset will be increased after writing data.
[in]SrcDataLenSrcDataLen is the length in bytes without terminator. if 0 it will be autodetected by assuming a 0-terminating string.
[in,out]AbsoluteOffsetPtrCurrent absolute offset. Will be increased by data length.
[in]TerminateWith0Whether to terminate the string with a NULL-char.
Returns
FALSE if something went wrong

Definition at line 157 of file util.c.

165{
166 ULONG SrcDataMaxLen;
167
168 if (SrcDataLen == 0)
169 SrcDataLen = wcslen(SrcDataW) * sizeof(WCHAR);
170
171 SrcDataMaxLen = SrcDataLen;
172 if (TerminateWith0)
173 SrcDataMaxLen += sizeof(WCHAR);
174
175 if (*AbsoluteOffsetPtr < (PBYTE)DataStart)
176 {
177 ERR("Invalid offset\n");
178 return false;
179 }
180
181 if (*AbsoluteOffsetPtr + SrcDataMaxLen > (PBYTE)DataStart + DataSize)
182 {
183 ERR("Out of bounds!\n");
184 return false;
185 }
186
187 memcpy(*AbsoluteOffsetPtr, SrcDataW, SrcDataLen);
188 *DstDataWPtr = (WCHAR*)*AbsoluteOffsetPtr;
189 if (TerminateWith0)
190 (*DstDataWPtr)[SrcDataLen / sizeof(WCHAR)] = 0;
191 *AbsoluteOffsetPtr += SrcDataMaxLen;
192
193 return true;
194}
_CRTIMP size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)
_In_ NDIS_STATUS _In_ ULONG _In_ USHORT _In_opt_ PVOID _In_ ULONG DataSize
Definition: ndis.h:4755
uint32_t ULONG
Definition: typedefs.h:59
__wchar_t WCHAR
Definition: xmlstorage.h:180

Referenced by NtlmUStrWriteToStruct().

◆ NtlmUStrAlloc()

bool NtlmUStrAlloc ( _Inout_ PUNICODE_STRING  Dst,
_In_ UINT16  SizeInBytes,
_In_ UINT16  InitLength 
)

Definition at line 103 of file util.c.

107{
108 Dst->Length = InitLength;
109 Dst->MaximumLength = SizeInBytes;
110 Dst->Buffer = NtlmAllocate(SizeInBytes, false);
111 return (Dst->Buffer != NULL);
112}
#define Dst
Definition: mesh.h:153

Referenced by BuildLm20LogonProfileBuffer().

◆ NtlmUStrFree()

VOID NtlmUStrFree ( _In_ PUNICODE_STRING  String)

Definition at line 115 of file util.c.

117{
118 if (String == NULL || String->Buffer == NULL || String->MaximumLength == 0)
119 return;
120
121 NtlmFree(String->Buffer, false);
122 String->Buffer = NULL;
123 String->MaximumLength = 0;
124}

Referenced by BuildLm20LogonProfileBuffer().

◆ NtlmUStrWriteToStruct()

bool NtlmUStrWriteToStruct ( _In_ PVOID  DataStart,
_In_ ULONG  DataSize,
_Out_ PUNICODE_STRING  DstData,
_In_ const PUNICODE_STRING  SrcData,
_Inout_ PBYTE AbsoluteOffsetPtr,
_In_ bool  TerminateWith0 
)

Definition at line 197 of file util.c.

204{
205 if (!NtlmStructWriteStrW(DataStart,
206 DataSize,
207 &DstData->Buffer,
208 SrcData->Buffer,
209 SrcData->Length,
210 AbsoluteOffsetPtr,
211 TerminateWith0))
212 return false;
213
214 DstData->Length = SrcData->Length;
215 DstData->MaximumLength = SrcData->Length;
216 if (TerminateWith0)
217 SrcData->MaximumLength += sizeof(WCHAR);
218
219 return true;
220}
static bool NtlmStructWriteStrW(_In_ PVOID DataStart, _In_ ULONG DataSize, _Out_ PWCHAR *DstDataWPtr, _In_ const WCHAR *SrcDataW, _In_ ULONG SrcDataLen, _Inout_ PBYTE *AbsoluteOffsetPtr, _In_ bool TerminateWith0)
Helper to fill a WCHAR-String in a struct. The stringdata is appended to the struct....
Definition: util.c:157

Referenced by BuildLm20LogonProfileBuffer().

◆ WINE_DEFAULT_DEBUG_CHANNEL()

WINE_DEFAULT_DEBUG_CHANNEL ( ntlm  )