[KERNEL32] Move some functions into a static library to be shared between kernel32 and kernel32_vista

This commit is contained in:
Timo Kreuzer 2020-09-13 10:03:58 +02:00
parent 4e72da0858
commit fcd83242d4
5 changed files with 206 additions and 166 deletions

View file

@ -5,6 +5,14 @@ add_definitions(-D_KERNEL32_)
include_directories(${REACTOS_SOURCE_DIR}/sdk/include/reactos/subsys)
spec2def(kernel32.dll kernel32.spec ADD_IMPORTLIB)
# Shared between kernel32 and kernel32_vista
add_library(kernel32_shared
client/shared_utils.c
client/file/fileutils.c
)
add_dependencies(kernel32_shared psdk)
list(APPEND SOURCE
client/actctx.c
client/appcache.c
@ -114,7 +122,7 @@ set_module_type(kernel32 win32dll ENTRYPOINT DllMain 12)
set_subsystem(kernel32 console)
################# END HACK #################
target_link_libraries(kernel32 wine chkstk ${PSEH_LIB})
target_link_libraries(kernel32 kernel32_shared wine chkstk ${PSEH_LIB})
add_importlibs(kernel32 ntdll)
add_pch(kernel32 k32.h SOURCE)
add_dependencies(kernel32 psdk errcodes asm)

View file

@ -1036,21 +1036,6 @@ SetLastError(IN DWORD dwErrCode)
if (NtCurrentTeb()->LastErrorValue != dwErrCode) NtCurrentTeb()->LastErrorValue = dwErrCode;
}
/*
* @implemented
*/
DWORD
WINAPI
BaseSetLastNTError(IN NTSTATUS Status)
{
DWORD dwErrCode;
/* Convert from NT to Win32, then set */
dwErrCode = RtlNtStatusToDosError(Status);
SetLastError(dwErrCode);
return dwErrCode;
}
/*
* @implemented
*/

View file

@ -18,156 +18,6 @@ DEBUG_CHANNEL(kernel32file);
/* FUNCTIONS ****************************************************************/
PWCHAR
FilenameA2W(LPCSTR NameA, BOOL alloc)
{
ANSI_STRING str;
UNICODE_STRING strW;
PUNICODE_STRING pstrW;
NTSTATUS Status;
//ASSERT(NtCurrentTeb()->StaticUnicodeString.Buffer == NtCurrentTeb()->StaticUnicodeBuffer);
ASSERT(NtCurrentTeb()->StaticUnicodeString.MaximumLength == sizeof(NtCurrentTeb()->StaticUnicodeBuffer));
RtlInitAnsiString(&str, NameA);
pstrW = alloc ? &strW : &NtCurrentTeb()->StaticUnicodeString;
if (bIsFileApiAnsi)
Status= RtlAnsiStringToUnicodeString( pstrW, &str, (BOOLEAN)alloc );
else
Status= RtlOemStringToUnicodeString( pstrW, &str, (BOOLEAN)alloc );
if (NT_SUCCESS(Status))
return pstrW->Buffer;
if (Status== STATUS_BUFFER_OVERFLOW)
SetLastError( ERROR_FILENAME_EXCED_RANGE );
else
BaseSetLastNTError(Status);
return NULL;
}
/*
No copy/conversion is done if the dest. buffer is too small.
Returns:
Success: number of TCHARS copied into dest. buffer NOT including nullterm
Fail: size of buffer in TCHARS required to hold the converted filename, including nullterm
*/
DWORD
FilenameU2A_FitOrFail(
LPSTR DestA,
INT destLen, /* buffer size in TCHARS incl. nullchar */
PUNICODE_STRING SourceU
)
{
DWORD ret;
/* destLen should never exceed MAX_PATH */
if (destLen > MAX_PATH) destLen = MAX_PATH;
ret = bIsFileApiAnsi? RtlUnicodeStringToAnsiSize(SourceU) : RtlUnicodeStringToOemSize(SourceU);
/* ret incl. nullchar */
if (DestA && (INT)ret <= destLen)
{
ANSI_STRING str;
str.Buffer = DestA;
str.MaximumLength = (USHORT)destLen;
if (bIsFileApiAnsi)
RtlUnicodeStringToAnsiString(&str, SourceU, FALSE );
else
RtlUnicodeStringToOemString(&str, SourceU, FALSE );
ret = str.Length; /* SUCCESS: length without terminating 0 */
}
return ret;
}
/*
No copy/conversion is done if the dest. buffer is too small.
Returns:
Success: number of TCHARS copied into dest. buffer NOT including nullterm
Fail: size of buffer in TCHARS required to hold the converted filename, including nullterm
*/
DWORD
FilenameW2A_FitOrFail(
LPSTR DestA,
INT destLen, /* buffer size in TCHARS incl. nullchar */
LPCWSTR SourceW,
INT sourceLen /* buffer size in TCHARS incl. nullchar */
)
{
UNICODE_STRING strW;
if (sourceLen < 0) sourceLen = wcslen(SourceW) + 1;
strW.Buffer = (PWCHAR)SourceW;
strW.MaximumLength = sourceLen * sizeof(WCHAR);
strW.Length = strW.MaximumLength - sizeof(WCHAR);
return FilenameU2A_FitOrFail(DestA, destLen, &strW);
}
/*
Return: num. TCHARS copied into dest including nullterm
*/
DWORD
FilenameA2W_N(
LPWSTR dest,
INT destlen, /* buffer size in TCHARS incl. nullchar */
LPCSTR src,
INT srclen /* buffer size in TCHARS incl. nullchar */
)
{
DWORD ret;
if (srclen < 0) srclen = strlen( src ) + 1;
if (bIsFileApiAnsi)
RtlMultiByteToUnicodeN( dest, destlen* sizeof(WCHAR), &ret, (LPSTR)src, srclen );
else
RtlOemToUnicodeN( dest, destlen* sizeof(WCHAR), &ret, (LPSTR)src, srclen );
if (ret) dest[(ret/sizeof(WCHAR))-1]=0;
return ret/sizeof(WCHAR);
}
/*
Return: num. TCHARS copied into dest including nullterm
*/
DWORD
FilenameW2A_N(
LPSTR dest,
INT destlen, /* buffer size in TCHARS incl. nullchar */
LPCWSTR src,
INT srclen /* buffer size in TCHARS incl. nullchar */
)
{
DWORD ret;
if (srclen < 0) srclen = wcslen( src ) + 1;
if (bIsFileApiAnsi)
RtlUnicodeToMultiByteN( dest, destlen, &ret, (LPWSTR) src, srclen * sizeof(WCHAR));
else
RtlUnicodeToOemN( dest, destlen, &ret, (LPWSTR) src, srclen * sizeof(WCHAR) );
if (ret) dest[ret-1]=0;
return ret;
}
/*
* @implemented
*/

View file

@ -0,0 +1,167 @@
/*
* COPYRIGHT: See COPYING in the top level directory
* PROJECT: ReactOS system libraries
* FILE: dll/win32/kernel32/client/file/fileutils.c
* PURPOSE: File utility function shared with kernel32_vista
* PROGRAMMER: Taken from wine
*/
/* INCLUDES *****************************************************************/
#include <k32.h>
#define NDEBUG
#include <debug.h>
/* FUNCTIONS ****************************************************************/
PWCHAR
FilenameA2W(LPCSTR NameA, BOOL alloc)
{
ANSI_STRING str;
UNICODE_STRING strW;
PUNICODE_STRING pstrW;
NTSTATUS Status;
//ASSERT(NtCurrentTeb()->StaticUnicodeString.Buffer == NtCurrentTeb()->StaticUnicodeBuffer);
ASSERT(NtCurrentTeb()->StaticUnicodeString.MaximumLength == sizeof(NtCurrentTeb()->StaticUnicodeBuffer));
RtlInitAnsiString(&str, NameA);
pstrW = alloc ? &strW : &NtCurrentTeb()->StaticUnicodeString;
if (bIsFileApiAnsi)
Status= RtlAnsiStringToUnicodeString( pstrW, &str, (BOOLEAN)alloc );
else
Status= RtlOemStringToUnicodeString( pstrW, &str, (BOOLEAN)alloc );
if (NT_SUCCESS(Status))
return pstrW->Buffer;
if (Status== STATUS_BUFFER_OVERFLOW)
SetLastError( ERROR_FILENAME_EXCED_RANGE );
else
BaseSetLastNTError(Status);
return NULL;
}
/*
No copy/conversion is done if the dest. buffer is too small.
Returns:
Success: number of TCHARS copied into dest. buffer NOT including nullterm
Fail: size of buffer in TCHARS required to hold the converted filename, including nullterm
*/
DWORD
FilenameU2A_FitOrFail(
LPSTR DestA,
INT destLen, /* buffer size in TCHARS incl. nullchar */
PUNICODE_STRING SourceU
)
{
DWORD ret;
/* destLen should never exceed MAX_PATH */
if (destLen > MAX_PATH) destLen = MAX_PATH;
ret = bIsFileApiAnsi? RtlUnicodeStringToAnsiSize(SourceU) : RtlUnicodeStringToOemSize(SourceU);
/* ret incl. nullchar */
if (DestA && (INT)ret <= destLen)
{
ANSI_STRING str;
str.Buffer = DestA;
str.MaximumLength = (USHORT)destLen;
if (bIsFileApiAnsi)
RtlUnicodeStringToAnsiString(&str, SourceU, FALSE );
else
RtlUnicodeStringToOemString(&str, SourceU, FALSE );
ret = str.Length; /* SUCCESS: length without terminating 0 */
}
return ret;
}
/*
No copy/conversion is done if the dest. buffer is too small.
Returns:
Success: number of TCHARS copied into dest. buffer NOT including nullterm
Fail: size of buffer in TCHARS required to hold the converted filename, including nullterm
*/
DWORD
FilenameW2A_FitOrFail(
LPSTR DestA,
INT destLen, /* buffer size in TCHARS incl. nullchar */
LPCWSTR SourceW,
INT sourceLen /* buffer size in TCHARS incl. nullchar */
)
{
UNICODE_STRING strW;
if (sourceLen < 0) sourceLen = wcslen(SourceW) + 1;
strW.Buffer = (PWCHAR)SourceW;
strW.MaximumLength = sourceLen * sizeof(WCHAR);
strW.Length = strW.MaximumLength - sizeof(WCHAR);
return FilenameU2A_FitOrFail(DestA, destLen, &strW);
}
/*
Return: num. TCHARS copied into dest including nullterm
*/
DWORD
FilenameA2W_N(
LPWSTR dest,
INT destlen, /* buffer size in TCHARS incl. nullchar */
LPCSTR src,
INT srclen /* buffer size in TCHARS incl. nullchar */
)
{
DWORD ret;
if (srclen < 0) srclen = strlen( src ) + 1;
if (bIsFileApiAnsi)
RtlMultiByteToUnicodeN( dest, destlen* sizeof(WCHAR), &ret, (LPSTR)src, srclen );
else
RtlOemToUnicodeN( dest, destlen* sizeof(WCHAR), &ret, (LPSTR)src, srclen );
if (ret) dest[(ret/sizeof(WCHAR))-1]=0;
return ret/sizeof(WCHAR);
}
/*
Return: num. TCHARS copied into dest including nullterm
*/
DWORD
FilenameW2A_N(
LPSTR dest,
INT destlen, /* buffer size in TCHARS incl. nullchar */
LPCWSTR src,
INT srclen /* buffer size in TCHARS incl. nullchar */
)
{
DWORD ret;
if (srclen < 0) srclen = wcslen( src ) + 1;
if (bIsFileApiAnsi)
RtlUnicodeToMultiByteN( dest, destlen, &ret, (LPWSTR) src, srclen * sizeof(WCHAR));
else
RtlUnicodeToOemN( dest, destlen, &ret, (LPWSTR) src, srclen * sizeof(WCHAR) );
if (ret) dest[ret-1]=0;
return ret;
}
/* EOF */

View file

@ -0,0 +1,30 @@
/*
* COPYRIGHT: See COPYING in the top level directory
* PROJECT: ReactOS system libraries
* FILE: dll/win32/kernel32/client/utils_shared.c
* PURPOSE: Utility functions shared with kernel32_vista
* PROGRAMMER: Thomas Faber
*/
/* INCLUDES *******************************************************************/
#include <k32.h>
#include <strsafe.h>
#define NDEBUG
#include <debug.h>
/*
* @implemented
*/
DWORD
WINAPI
BaseSetLastNTError(IN NTSTATUS Status)
{
DWORD dwErrCode;
/* Convert from NT to Win32, then set */
dwErrCode = RtlNtStatusToDosError(Status);
SetLastError(dwErrCode);
return dwErrCode;
}