Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Winapi reformatting #677

Merged
merged 2 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# .git-blame-ignore-revs
# winapi: Reformat with clang-format
e3e9de54cb60e9fb5b798e11ac2ae4e2b27cd984
2 changes: 1 addition & 1 deletion lib/winapi/badptr.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ BOOL IsBadWritePtr (LPVOID lp, UINT_PTR ucb)
// The Microsoft implementation then probes each page.
// For Xbox, this is not suitable because `lp` potentially points at MMIO.

//FIXME: Walk allocations in region with NtQueryVirtualMemory and check write permission?
// FIXME: Walk allocations in region with NtQueryVirtualMemory and check write permission?

// We disallow all access for now, because memory is potentially unsafe
return TRUE;
Expand Down
2 changes: 1 addition & 1 deletion lib/winapi/debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ void WINAPI OutputDebugStringA (LPCTSTR lpOutputString)

__asm__ __volatile__("mov $1, %%eax\n" // $1 = BREAKPOINT_PRINT
"int $0x2D\n"
"int $3\n": :"c" (&s));
"int $3\n" : : "c"(&s));
}
3 changes: 1 addition & 2 deletions lib/winapi/debugapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
#include <windef.h>

#ifdef __cplusplus
extern "C"
{
extern "C" {
#endif

BOOL IsDebuggerPresent (VOID);
Expand Down
2 changes: 1 addition & 1 deletion lib/winapi/errhandlingapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

// SPDX-FileCopyrightText: 2019 Stefan Schmidt

#include <windows.h>
#include <string.h>
#include <windows.h>
#include <xboxkrnl/xboxkrnl.h>

VOID RaiseException (DWORD dwExceptionCode, DWORD dwExceptionFlags, DWORD nNumberOfArguments, const ULONG_PTR *lpArguments)
Expand Down
3 changes: 1 addition & 2 deletions lib/winapi/errhandlingapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
#include <winbase.h>

#ifdef __cplusplus
extern "C"
{
extern "C" {
#endif

VOID RaiseException (DWORD dwExceptionCode, DWORD dwExceptionFlags, DWORD nNumberOfArguments, const ULONG_PTR *lpArguments);
Expand Down
2 changes: 1 addition & 1 deletion lib/winapi/error.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

// SPDX-FileCopyrightText: 2019 Stefan Schmidt

#include <winbase.h>
#include <threads.h>
#include <winbase.h>

static thread_local DWORD lastError = 0;

Expand Down
10 changes: 6 additions & 4 deletions lib/winapi/fiber.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

// SPDX-FileCopyrightText: 2019-2022 Stefan Schmidt

#include <windows.h>
#include <assert.h>
#include <stdint.h>
#include <threads.h>
#include <windows.h>

// Each thread gets a node to keep track of its FLS data.
// Threads must register themselves, so their FLS nodes can be put into a list
Expand All @@ -29,7 +29,7 @@ static __cdecl VOID fls_init (VOID)
InitializeListHead(&fls_nodes_list);
}
#pragma comment(linker, "/include:___fls_init_p")
__attribute__((section(".CRT$XXT"))) void (__cdecl *const __fls_init_p)(void) = fls_init;
__attribute__((section(".CRT$XXT"))) void(__cdecl *const __fls_init_p)(void) = fls_init;

VOID fls_register_thread (VOID)
{
Expand Down Expand Up @@ -70,11 +70,13 @@ DWORD FlsAlloc (PFLS_CALLBACK_FUNCTION lpCallback)
EnterCriticalSection(&fls_lock);

for (size_t i = 0; i < (FLS_MAXIMUM_AVAILABLE / 32); i++) {
if (fls_bitmap[i] == 0xFFFFFFFF) continue;
if (fls_bitmap[i] == 0xFFFFFFFF) {
continue;
}

unsigned int index = __builtin_ctz(~fls_bitmap[i]);
fls_bitmap[i] |= (1 << index);
retval = i*32 + index;
retval = i * 32 + index;
fls_dtors[retval] = lpCallback;
break;
}
Expand Down
3 changes: 1 addition & 2 deletions lib/winapi/fibersapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
#include <winbase.h>

#ifdef __cplusplus
extern "C"
{
extern "C" {
#endif

DWORD FlsAlloc (PFLS_CALLBACK_FUNCTION lpCallback);
Expand Down
39 changes: 19 additions & 20 deletions lib/winapi/fileapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@
#ifndef __FILEAPI_H__
#define __FILEAPI_H__

#include <windef.h>
#include <winbase.h>
#include <windef.h>
#include <winnt.h>

#ifdef __cplusplus
extern "C"
{
extern "C" {
#endif

DWORD GetFileAttributesA (LPCSTR lpFileName);
Expand All @@ -22,10 +21,10 @@ BOOL SetFileAttributesA (LPCSTR lpFileName, DWORD dwFileAttributes);
BOOL GetFileTime (HANDLE hFile, LPFILETIME lpCreationTime, LPFILETIME lpLastAccessTime, LPFILETIME lpLastWriteTime);
BOOL SetFileTime (HANDLE hFile, const FILETIME *lpCreationTime, const FILETIME *lpLastAccessTime, const FILETIME *lpLastWriteTime);

#define CREATE_NEW 1
#define CREATE_ALWAYS 2
#define OPEN_EXISTING 3
#define OPEN_ALWAYS 4
#define CREATE_NEW 1
#define CREATE_ALWAYS 2
#define OPEN_EXISTING 3
#define OPEN_ALWAYS 4
#define TRUNCATE_EXISTING 5

HANDLE CreateFileA (LPCSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes, HANDLE hTemplateFile);
Expand Down Expand Up @@ -54,19 +53,19 @@ DWORD GetLogicalDrives (VOID);
DWORD GetLogicalDriveStringsA (DWORD nBufferLength, LPSTR lpBuffer);

#ifndef UNICODE
#define GetFileAttributes GetFileAttributesA
#define GetFileAttributesEx GetFileAttributesExA
#define SetFileAttributes SetFileAttributesA
#define CreateFile CreateFileA
#define FindFirstFile FindFirstFileA
#define FindNextFile FindNextFileA
#define DeleteFile(...) DeleteFileA(__VA_ARGS__)
#define RemoveDirectory(...) RemoveDirectoryA(__VA_ARGS__)
#define CreateDirectory(...) CreateDirectoryA(__VA_ARGS__)
#define MoveFile(...) MoveFileA(__VA_ARGS__)
#define CopyFile(...) CopyFileA(__VA_ARGS__)
#define GetDiskFreeSpaceEx GetDiskFreeSpaceExA
#define GetDiskFreeSpace GetDiskFreeSpaceA
#define GetFileAttributes GetFileAttributesA
#define GetFileAttributesEx GetFileAttributesExA
#define SetFileAttributes SetFileAttributesA
#define CreateFile CreateFileA
#define FindFirstFile FindFirstFileA
#define FindNextFile FindNextFileA
#define DeleteFile(...) DeleteFileA(__VA_ARGS__)
#define RemoveDirectory(...) RemoveDirectoryA(__VA_ARGS__)
#define CreateDirectory(...) CreateDirectoryA(__VA_ARGS__)
#define MoveFile(...) MoveFileA(__VA_ARGS__)
#define CopyFile(...) CopyFileA(__VA_ARGS__)
#define GetDiskFreeSpaceEx GetDiskFreeSpaceExA
#define GetDiskFreeSpace GetDiskFreeSpaceA
#define GetLogicalDriveStrings GetLogicalDriveStringsA
#else
#error nxdk does not support the Unicode API
Expand Down
10 changes: 5 additions & 5 deletions lib/winapi/fileio.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

// SPDX-FileCopyrightText: 2019-2021 Stefan Schmidt

#include <fileapi.h>
#include <winerror.h>
#include <assert.h>
#include <fileapi.h>
#include <stdbool.h>
#include <string.h>
#include <winerror.h>
#include <xboxkrnl/xboxkrnl.h>

HANDLE CreateFileA (LPCSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes, HANDLE hTemplateFile)
Expand Down Expand Up @@ -77,7 +77,7 @@ HANDLE CreateFileA (LPCSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode,
if (status == STATUS_OBJECT_NAME_COLLISION) {
SetLastError(ERROR_FILE_EXISTS);
} else if (status == STATUS_FILE_IS_A_DIRECTORY) {
if (lpFileName[path.Length-1] == '\\') {
if (lpFileName[path.Length - 1] == '\\') {
SetLastError(ERROR_PATH_NOT_FOUND);
} else {
SetLastError(ERROR_ACCESS_DENIED);
Expand Down Expand Up @@ -190,7 +190,7 @@ BOOL WriteFile (HANDLE hFile, LPCVOID lpBuffer, DWORD nNumberOfBytesToWrite, LPD
lpOverlapped->InternalHigh = 0;

status = NtWriteFile(hFile, lpOverlapped->hEvent, NULL, NULL, (PIO_STATUS_BLOCK)&lpOverlapped->Internal,
(PVOID)lpBuffer, nNumberOfBytesToWrite, &overlappedOffset);
(PVOID)lpBuffer, nNumberOfBytesToWrite, &overlappedOffset);

// The write can finish immediately. Handle this case
if (NT_SUCCESS(status) && status != STATUS_PENDING) {
Expand Down Expand Up @@ -341,7 +341,7 @@ BOOL SetFilePointerEx (HANDLE hFile, LARGE_INTEGER liDistanceToMove, PLARGE_INTE
case FILE_CURRENT:
status = NtQueryInformationFile(hFile, &ioStatusBlock, &positionInfo, sizeof(positionInfo), FilePositionInformation);

if(!NT_SUCCESS(status)) {
if (!NT_SUCCESS(status)) {
SetLastError(RtlNtStatusToDosError(status));
return FALSE;
}
Expand Down
53 changes: 27 additions & 26 deletions lib/winapi/filemanip.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
// SPDX-FileCopyrightText: 2019-2022 Stefan Schmidt
// SPDX-FileCopyrightText: 2021 Erik Abair

#include <fileapi.h>
#include <handleapi.h>
#include <winbase.h>
#include <winerror.h>
#include <assert.h>
#include <ctype.h>
#include <fileapi.h>
#include <handleapi.h>
#include <stdbool.h>
#include <string.h>
#include <winbase.h>
#include <winerror.h>
#include <xboxkrnl/xboxkrnl.h>

DWORD GetFileAttributesA (LPCSTR lpFileName)
Expand Down Expand Up @@ -356,26 +356,26 @@ BOOL CopyFileA (LPCSTR lpExistingFileName, LPCSTR lpNewFileName, BOOL bFailIfExi
InitializeObjectAttributes(&objectAttributes, &targetPath, OBJ_CASE_INSENSITIVE, ObDosDevicesDirectory(), NULL);

status = NtCreateFile(
&targetHandle,
FILE_GENERIC_WRITE,
&objectAttributes,
&ioStatusBlock,
&networkOpenInformation.AllocationSize,
networkOpenInformation.FileAttributes,
0,
bFailIfExists ? FILE_CREATE : FILE_SUPERSEDE,
FILE_SYNCHRONOUS_IO_NONALERT | FILE_NON_DIRECTORY_FILE | FILE_SEQUENTIAL_ONLY);
&targetHandle,
FILE_GENERIC_WRITE,
&objectAttributes,
&ioStatusBlock,
&networkOpenInformation.AllocationSize,
networkOpenInformation.FileAttributes,
0,
bFailIfExists ? FILE_CREATE : FILE_SUPERSEDE,
FILE_SYNCHRONOUS_IO_NONALERT | FILE_NON_DIRECTORY_FILE | FILE_SEQUENTIAL_ONLY);
if (!NT_SUCCESS(status)) {
NtClose(sourceHandle);
SetLastError(RtlNtStatusToDosError(status));
return FALSE;
}

status = NtAllocateVirtualMemory(&readBuffer,
0,
&readBufferRegionSize,
MEM_RESERVE | MEM_COMMIT,
PAGE_READWRITE);
0,
&readBufferRegionSize,
MEM_RESERVE | MEM_COMMIT,
PAGE_READWRITE);
if (!NT_SUCCESS(status)) {
NtClose(sourceHandle);
NtClose(targetHandle);
Expand Down Expand Up @@ -418,11 +418,11 @@ BOOL CopyFileA (LPCSTR lpExistingFileName, LPCSTR lpNewFileName, BOOL bFailIfExi
fileBasicInformation.LastWriteTime = networkOpenInformation.LastWriteTime;
fileBasicInformation.FileAttributes = networkOpenInformation.FileAttributes;
status = NtSetInformationFile(
targetHandle,
&ioStatusBlock,
&fileBasicInformation,
sizeof(fileBasicInformation),
FileBasicInformation);
targetHandle,
&ioStatusBlock,
&fileBasicInformation,
sizeof(fileBasicInformation),
FileBasicInformation);
if (!NT_SUCCESS(status)) {
SetLastError(RtlNtStatusToDosError(status));
NtClose(sourceHandle);
Expand All @@ -439,8 +439,8 @@ BOOL CopyFileA (LPCSTR lpExistingFileName, LPCSTR lpNewFileName, BOOL bFailIfExi

status = NtClose(targetHandle);
if (!NT_SUCCESS(status)) {
SetLastError(RtlNtStatusToDosError(status));
return FALSE;
SetLastError(RtlNtStatusToDosError(status));
return FALSE;
}
return TRUE;
}
Expand Down Expand Up @@ -555,7 +555,8 @@ DWORD GetLogicalDrives (VOID)
ANSI_STRING path;
HANDLE handle;
OBJECT_ATTRIBUTES attributes;
struct {
struct
{
OBJECT_DIRECTORY_INFORMATION objDirInfo;
CHAR filenameBuf[2];
} objDirInfoBuf;
Expand Down Expand Up @@ -614,7 +615,7 @@ DWORD GetLogicalDriveStringsA (DWORD nBufferLength, LPSTR lpBuffer)
}

if (nBufferLength == 0 || nBufferLength < requiredBufLength) {
return requiredBufLength+1;
return requiredBufLength + 1;
}

for (int bit = 0; bit < 26; bit++) {
Expand Down
18 changes: 10 additions & 8 deletions lib/winapi/findfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,29 @@

// SPDX-FileCopyrightText: 2019-2020 Stefan Schmidt

#include <string.h>
#include <assert.h>
#include <fileapi.h>
#include <stdbool.h>
#include <string.h>
#include <winbase.h>
#include <xboxkrnl/xboxkrnl.h>
#include <winerror.h>
#include <fileapi.h>
#include <xboxkrnl/xboxkrnl.h>

struct FileInfo {
struct FileInfo
{
FILE_DIRECTORY_INFORMATION dirInfo;
// Reserve path buffer minus the null-terminator and the first byte provided by dirInfo
char filename[MAX_PATH-2];
char filename[MAX_PATH - 2];
};

static void dirtofind (FILE_DIRECTORY_INFORMATION *dirInfo, LPWIN32_FIND_DATAA lpFindFileData)
{
lpFindFileData->dwFileAttributes = dirInfo->FileAttributes;
lpFindFileData->ftCreationTime.dwLowDateTime = dirInfo->CreationTime.LowPart;
lpFindFileData->ftCreationTime.dwHighDateTime = dirInfo->CreationTime.HighPart;
lpFindFileData->ftLastAccessTime.dwLowDateTime = dirInfo->LastAccessTime.LowPart;
lpFindFileData->ftLastAccessTime.dwLowDateTime = dirInfo->LastAccessTime.LowPart;
lpFindFileData->ftLastAccessTime.dwHighDateTime = dirInfo->LastAccessTime.HighPart;
lpFindFileData->ftLastWriteTime.dwLowDateTime = dirInfo->LastWriteTime.LowPart;
lpFindFileData->ftLastWriteTime.dwLowDateTime = dirInfo->LastWriteTime.LowPart;
lpFindFileData->ftLastWriteTime.dwHighDateTime = dirInfo->LastWriteTime.HighPart;
lpFindFileData->nFileSizeHigh = dirInfo->EndOfFile.HighPart;
lpFindFileData->nFileSizeLow = dirInfo->EndOfFile.LowPart;
Expand All @@ -48,8 +49,9 @@ HANDLE FindFirstFileA (LPCSTR lpFileName, LPWIN32_FIND_DATAA lpFindFileData)
RtlInitAnsiString(&dirPath, lpFileName);

for (maskOffset = dirPath.Length; maskOffset > 0; maskOffset--) {
if (dirPath.Buffer[maskOffset - 1] == '\\')
if (dirPath.Buffer[maskOffset - 1] == '\\') {
break;
}
}

mask.Buffer = dirPath.Buffer + maskOffset;
Expand Down
20 changes: 10 additions & 10 deletions lib/winapi/handleapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
#include <winbase.h>
#include <xboxkrnl/xboxkrnl.h>

BOOL CloseHandle (HANDLE hObject)
{
NTSTATUS status = NtClose(hObject);
BOOL CloseHandle (HANDLE hObject)
{
NTSTATUS status = NtClose(hObject);

if (NT_SUCCESS(status)) {
return TRUE;
} else {
SetLastError(RtlNtStatusToDosError(status));
return FALSE;
}
}
if (NT_SUCCESS(status)) {
return TRUE;
} else {
SetLastError(RtlNtStatusToDosError(status));
return FALSE;
}
}
Loading
Loading