Author: mjansen Date: Thu Mar 2 20:40:21 2017 New Revision: 74029
URL: http://svn.reactos.org/svn/reactos?rev=74029&view=rev Log: [NTDLL] Implement RtlpEnsureBufferSize. Patch by Hermes Belusca-Maito. CORE-11990 Some small changes by me.
Modified: trunk/reactos/dll/ntdll/def/ntdll.spec trunk/reactos/sdk/include/ndk/rtlfuncs.h trunk/reactos/sdk/lib/rtl/unicode.c trunk/rostests/apitests/ntdll/RtlpEnsureBufferSize.c
Modified: trunk/reactos/dll/ntdll/def/ntdll.spec URL: http://svn.reactos.org/svn/reactos/trunk/reactos/dll/ntdll/def/ntdll.spec?re... ============================================================================== --- trunk/reactos/dll/ntdll/def/ntdll.spec [iso-8859-1] (original) +++ trunk/reactos/dll/ntdll/def/ntdll.spec [iso-8859-1] Thu Mar 2 20:40:21 2017 @@ -956,7 +956,7 @@ 948 stdcall RtlZeroMemory(ptr long) 949 stdcall RtlZombifyActivationContext(ptr) 950 stdcall RtlpApplyLengthFunction(long long ptr ptr) -951 stdcall RtlpEnsureBufferSize(ptr ptr ptr) ; CHECKME +951 stdcall RtlpEnsureBufferSize(long ptr long) # stdcall RtlpNotOwnerCriticalSection 953 stdcall RtlpNtCreateKey(ptr long ptr long ptr ptr) 954 stdcall RtlpNtEnumerateSubKey(ptr ptr long long)
Modified: trunk/reactos/sdk/include/ndk/rtlfuncs.h URL: http://svn.reactos.org/svn/reactos/trunk/reactos/sdk/include/ndk/rtlfuncs.h?... ============================================================================== --- trunk/reactos/sdk/include/ndk/rtlfuncs.h [iso-8859-1] (original) +++ trunk/reactos/sdk/include/ndk/rtlfuncs.h [iso-8859-1] Thu Mar 2 20:40:21 2017 @@ -1048,6 +1048,8 @@
#endif // NTOS_MODE_USER
+#define NtCurrentPeb() (NtCurrentTeb()->ProcessEnvironmentBlock) + NTSYSAPI SIZE_T NTAPI @@ -2312,6 +2314,60 @@ _In_ PCUNICODE_STRING String );
+#define RTL_SKIP_BUFFER_COPY 0x00000001 + +NTSYSAPI +NTSTATUS +NTAPI +RtlpEnsureBufferSize( + _In_ ULONG Flags, + _Inout_ PRTL_BUFFER Buffer, + _In_ SIZE_T RequiredSize +); + +#ifdef NTOS_MODE_USER + +FORCEINLINE +VOID +RtlInitBuffer( + _Inout_ PRTL_BUFFER Buffer, + _In_ PUCHAR Data, + _In_ ULONG DataSize +) +{ + Buffer->Buffer = Buffer->StaticBuffer = Data; + Buffer->Size = Buffer->StaticSize = DataSize; + Buffer->ReservedForAllocatedSize = 0; + Buffer->ReservedForIMalloc = NULL; +} + +FORCEINLINE +NTSTATUS +RtlEnsureBufferSize( + _In_ ULONG Flags, + _Inout_ PRTL_BUFFER Buffer, + _In_ ULONG RequiredSize +) +{ + if (Buffer && RequiredSize <= Buffer->Size) + return STATUS_SUCCESS; + return RtlpEnsureBufferSize(Flags, Buffer, RequiredSize); +} + +FORCEINLINE +VOID +RtlFreeBuffer( + _Inout_ PRTL_BUFFER Buffer +) +{ + if (Buffer->Buffer != Buffer->StaticBuffer && Buffer->Buffer) + RtlFreeHeap(RtlGetProcessHeap(), 0, Buffer->Buffer); + Buffer->Buffer = Buffer->StaticBuffer; + Buffer->Size = Buffer->StaticSize; +} + +#endif /* NTOS_MODE_USER */ + // // Ansi String Functions // @@ -2626,7 +2682,6 @@ VOID );
-#define NtCurrentPeb() (NtCurrentTeb()->ProcessEnvironmentBlock)
// // Thread Pool Functions
Modified: trunk/reactos/sdk/lib/rtl/unicode.c URL: http://svn.reactos.org/svn/reactos/trunk/reactos/sdk/lib/rtl/unicode.c?rev=7... ============================================================================== --- trunk/reactos/sdk/lib/rtl/unicode.c [iso-8859-1] (original) +++ trunk/reactos/sdk/lib/rtl/unicode.c [iso-8859-1] Thu Mar 2 20:40:21 2017 @@ -2566,14 +2566,68 @@ }
/* - * @unimplemented - */ -NTSTATUS -NTAPI -RtlpEnsureBufferSize(ULONG Unknown1, ULONG Unknown2, ULONG Unknown3) -{ - DPRINT1("RtlpEnsureBufferSize: stub\n"); - return STATUS_NOT_IMPLEMENTED; + * @implemented + */ +NTSTATUS +NTAPI +RtlpEnsureBufferSize( + IN ULONG Flags, + IN OUT PRTL_BUFFER Buffer, + IN SIZE_T RequiredSize) +{ + PUCHAR NewBuffer; + + /* Parameter checks */ + if (Flags & ~RTL_SKIP_BUFFER_COPY) + return STATUS_INVALID_PARAMETER; + if (Buffer == NULL) + return STATUS_INVALID_PARAMETER; + + /* + * We don't need to grow the buffer if its size + * is already larger than the required size. + */ + if (Buffer->Size >= RequiredSize) + return STATUS_SUCCESS; + + /* + * When we are using the static buffer as our buffer, we don't need + * to grow it if its size is already larger than the required size. + * In this case, just keep it but update the current buffer size to + * the one requested. + * (But NEVER EVER modify the size of the static buffer!!) + * Otherwise, we'll need to create a new buffer and use this one instead. + */ + if ( (Buffer->Buffer == Buffer->StaticBuffer) && + (Buffer->StaticSize >= RequiredSize) ) + { + Buffer->Size = RequiredSize; + return STATUS_SUCCESS; + } + + /* The buffer we are using is not large enough, try to create a bigger one */ + NewBuffer = RtlpAllocateStringMemory(RequiredSize, TAG_USTR); + if (NewBuffer == NULL) + return STATUS_NO_MEMORY; + + /* Copy the original content if needed */ + if (!(Flags & RTL_SKIP_BUFFER_COPY)) + { + RtlMoveMemory(NewBuffer, Buffer->Buffer, Buffer->Size); + } + + /* Free the original buffer only if it's not the static buffer */ + if (Buffer->Buffer != Buffer->StaticBuffer) + { + RtlpFreeStringMemory(Buffer->Buffer, TAG_USTR); + } + + /* Update the members */ + Buffer->Buffer = NewBuffer; + Buffer->Size = RequiredSize; + + /* Done */ + return STATUS_SUCCESS; }
static
Modified: trunk/rostests/apitests/ntdll/RtlpEnsureBufferSize.c URL: http://svn.reactos.org/svn/reactos/trunk/rostests/apitests/ntdll/RtlpEnsureB... ============================================================================== --- trunk/rostests/apitests/ntdll/RtlpEnsureBufferSize.c [iso-8859-1] (original) +++ trunk/rostests/apitests/ntdll/RtlpEnsureBufferSize.c [iso-8859-1] Thu Mar 2 20:40:21 2017 @@ -11,29 +11,6 @@ #include <ndk/rtlfuncs.h> #include <tlhelp32.h>
-#ifndef RtlInitBuffer -#define RtlInitBuffer(RtlBuf, StaticData, StaticDataSize) \ - do { \ - (RtlBuf)->Buffer = (RtlBuf)->StaticBuffer = (PUCHAR)StaticData; \ - (RtlBuf)->Size = (RtlBuf)->StaticSize = StaticDataSize; \ - (RtlBuf)->ReservedForAllocatedSize = 0; \ - (RtlBuf)->ReservedForIMalloc = NULL; \ - } while (0) -#endif - -#ifndef RtlFreeBuffer -#define RtlFreeBuffer(RtlBuf) \ - do { \ - if ((RtlBuf)->Buffer != (RtlBuf)->StaticBuffer && (RtlBuf)->Buffer) \ - RtlFreeHeap(RtlGetProcessHeap(), 0, (RtlBuf)->Buffer); \ - (RtlBuf)->Buffer = (RtlBuf)->StaticBuffer; \ - (RtlBuf)->Size = (RtlBuf)->StaticSize; \ - } while (0) -#endif - -#ifndef RTL_SKIP_BUFFER_COPY -#define RTL_SKIP_BUFFER_COPY 0x00000001 -#endif
NTSTATUS (NTAPI *pRtlpEnsureBufferSize)(_In_ ULONG Flags, _Inout_ PRTL_BUFFER Buffer, _In_ SIZE_T RequiredSize);