📄 kernel32.c
字号:
\*|______________________________|*/static LPVOID WINAPI fd32_imp__VirtualAlloc( LPVOID lpAddress, size_t dwSize, DWORD flAllocationType, DWORD flProtect ){#ifdef __WINB_DEBUG__ fd32_log_printf("[WINB] VirtualAlloc: at %lx with %d bytes\n", (DWORD)lpAddress, dwSize);#endif if (mem_get_region((uint32_t)lpAddress, dwSize+sizeof(DWORD)) == 1) { DWORD *pd = (DWORD *) lpAddress; pd[0] = dwSize+sizeof(DWORD); return pd+1; } else { return NULL; }}static BOOL WINAPI fd32_imp__VirtualFree( LPVOID lpAddress, size_t dwSize, DWORD dwFreeType ){ DWORD *pd = (DWORD *)lpAddress;#ifdef __WINB_DEBUG__ fd32_log_printf("[WINB] VirtualFree: at %lx with %d bytes\n", (DWORD)lpAddress, dwSize);#endif if (pd != NULL) { mem_free((uint32_t)(pd-1), pd[0]); return TRUE; } else { return FALSE; }}/* * The GlobalAlloc function allocates the specified number of bytes from the heap. In the linear Win32 API environment, there is no difference between the local heap and the global heap. * * Return Values * If the function succeeds, the return value is the handle of the newly allocated memory object. * If the function fails, the return value is NULL. To get extended error information, call GetLastError. */static HGLOBAL WINAPI fd32_imp__GlobalAlloc( UINT uFlags, size_t uBytes ){#ifdef __WINB_DEBUG__ fd32_log_printf("[WINB] GlobalAlloc: %d bytes\n", uBytes);#endif /* Allocate memory from heap, so using malloc implemented in newlib */ return malloc(uBytes);}/* * The GlobalFree function frees the specified global memory object and invalidates its handle. * * Return Values * If the function succeeds, the return value is NULL. * If the function fails, the return value is equal to the handle of the global memory object. To get extended error information, call GetLastError. */static HGLOBAL WINAPI fd32_imp__GlobalFree( HGLOBAL hMem ){#ifdef __WINB_DEBUG__ fd32_log_printf("[WINB] GlobalFree: %lx\n", (DWORD)hMem);#endif free(hMem); return NULL;}/* * The LocalAlloc function allocates the specified number of bytes from the heap. In the linear Win32 API environment, there is no difference between the local heap and the global heap. * * Return Values * If the function succeeds, the return value is the handle of the newly allocated memory object. * If the function fails, the return value is NULL. To get extended error information, call GetLastError. */static HLOCAL WINAPI fd32_imp__LocalAlloc( UINT uFlags, size_t uBytes ){#ifdef __WINB_DEBUG__ fd32_log_printf("[WINB] LocalAlloc: %d bytes\n", uBytes);#endif /* Allocate memory from heap, so using malloc implemented in newlib */ return malloc(uBytes);}/* * The LocalFree function frees the specified local memory object and invalidates its handle. * * Return Values * If the function succeeds, the return value is NULL. * If the function fails, the return value is equal to the handle of the local memory object. */static HLOCAL WINAPI fd32_imp__LocalFree( HLOCAL hMem ){#ifdef __WINB_DEBUG__ fd32_log_printf("[WINB] LocalFree: %lx\n", (DWORD)hMem);#endif free(hMem); return NULL;}/* ______________________________ *\ *| |* *| File Functions |*\*|______________________________|*//* The CloseHandle function closes an open object handle. * * Return Values * If the function succeeds, the return value is nonzero. * If the function fails, the return value is zero. To get extended error information, call GetLastError. */static BOOL WINAPI fd32_imp__CloseHandle(HANDLE hObject){#ifdef __WINB_DEBUG__ fd32_log_printf("[WINB] CloseHandle: %lx\n", (DWORD)hObject);#endif if (close((int)hObject) == 0) return TRUE; else return FALSE;}/* The CreateFile function creates or opens the following objects and returns a handle that can be used to access the object: * files * pipes * mailslots * communications resources * disk devices (Windows NT only) * consoles * directories (open only) * Return Values * If the function succeeds, the return value is an open handle to the specified file. If the specified file exists before the function call and dwCreationDistribution is CREATE_ALWAYS or OPEN_ALWAYS, a call to GetLastError returns ERROR_ALREADY_EXISTS (even though the function has succeeded). If the file does not exist before the call, GetLastError returns zero. * If the function fails, the return value is INVALID_HANDLE_VALUE. To get extended error information, call GetLastError. */static HANDLE WINAPI fd32_imp__CreateFileA(LPCSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes, HANDLE hTemplateFile){ int filedes, mode, flags;#ifdef __WINB_DEBUG__ fd32_log_printf("[WINB] CreateFileA: %s (%lx), %lx, %lx, ...\n", lpFileName, *((DWORD *)lpFileName), dwDesiredAccess, dwShareMode);#endif if (*((DWORD *)lpFileName) == 0x494E4F43 && *((WORD *)(lpFileName+4)) == 0x244E) {#ifdef __WINB_DEBUG__ fd32_log_printf("[WINB] Create CONIN$ handle\n");#endif return (HANDLE)0; } else if (*((DWORD *)lpFileName) == 0x4F4E4F43 && *((DWORD *)(lpFileName+4)) == 0x00245455) {#ifdef __WINB_DEBUG__ fd32_log_printf("[WINB] Create CONOUT$ handle\n");#endif return (HANDLE)1; } /* access (read-write) mode */ if (dwDesiredAccess == (GENERIC_READ|GENERIC_WRITE)) mode = O_RDWR; else if (dwDesiredAccess == GENERIC_READ) mode = O_RDONLY; else if (dwDesiredAccess == GENERIC_WRITE) mode = O_WRONLY; else mode = 0; /* share mode */ /* security attributes */ /* how to create */ if (dwCreationDisposition == CREATE_NEW) flags = O_CREAT|O_EXCL; else if (dwCreationDisposition == CREATE_ALWAYS) flags = O_CREAT|O_TRUNC; else if (dwCreationDisposition == OPEN_EXISTING) flags = O_EXCL; else if (dwCreationDisposition == OPEN_ALWAYS) flags = O_CREAT; else if (dwCreationDisposition == TRUNCATE_EXISTING) flags = O_TRUNC; else flags = 0; /* file attributes */ filedes = open (lpFileName, flags, mode); if (filedes >= 0) return (HANDLE)filedes; else return INVALID_HANDLE_VALUE;}static BOOL WINAPI fd32_imp__FlushFileBuffers(HANDLE hFile){#ifdef __WINB_DEBUG__ fd32_log_printf("[WINB] FlushFileBuffers: %lx\n", (DWORD)hFile);#endif /* NOTE: Direct using FD32 system call */ if (fd32_fflush((int)hFile) == 0) return TRUE; else return FALSE;}/* The ReadFile function reads data from a file, starting at the position indicated by the file pointer. After the read operation has been completed, the file pointer is adjusted by the number of bytes actually read, unless the file handle is created with the overlapped attribute. If the file handle is created for overlapped input and output (I/O), the application must adjust the position of the file pointer after the read operation. * * Return Values * If the function succeeds, the return value is nonzero. * If the return value is nonzero and the number of bytes read is zero, the file pointer was beyond the current end of the file at the time of the read operation. However, if the file was opened with FILE_FLAG_OVERLAPPED and lpOverlapped is not NULL, the return value is FALSE and GetLastError returns ERROR_HANDLE_EOF when the file pointer goes beyond the current end of file. * If the function fails, the return value is zero. To get extended error information, call GetLastError. */static BOOL WINAPI fd32_imp__ReadFile(HANDLE hFile, LPVOID lpBuffer, DWORD nNumberOfBytesToRead, LPDWORD lpNumberOfBytesRead, LPOVERLAPPED lpOverlapped){ ssize_t res;#ifdef __WINB_DEBUG__ fd32_log_printf("[WINB] ReadFile: %lx %lx %ld\n", (DWORD)hFile, (DWORD)lpBuffer, nNumberOfBytesToRead);#endif res = read((int)hFile, lpBuffer, nNumberOfBytesToRead); if (res >= 0) { *lpNumberOfBytesRead = res; return TRUE; } else { return FALSE; }}/* The SetEndOfFile function moves the end-of-file (EOF) position for the specified file to the current position of the file pointer. * * Return Values * If the function succeeds, the return value is nonzero. * If the function fails, the return value is zero. To get extended error information, call GetLastError. */static BOOL WINAPI fd32_imp__SetEndOfFile(HANDLE hFile){ off_t length = lseek((int)hFile, 0, SEEK_CUR);#ifdef __WINB_DEBUG__ fd32_log_printf("[WINB] SetEndOfFile: %lx\n", (DWORD)hFile);#endif if (length >= 0) { if (ftruncate((int)hFile, length) == 0) return TRUE; } return FALSE;}/* The SetFilePointer function moves the file pointer of an open file. * * Return Values * If the SetFilePointer function succeeds, the return value is the low-order doubleword of the new file pointer, and if lpDistanceToMoveHigh is not NULL, the function puts the high-order doubleword of the new file pointer into the LONG pointed to by that parameter. * If the function fails and lpDistanceToMoveHigh is NULL, the return value is 0xFFFFFFFF. To get extended error information, call GetLastError. * If the function fails, and lpDistanceToMoveHigh is non-NULL, the return value is 0xFFFFFFFF and GetLastError will return a value other than NO_ERROR. */DWORD WINAPI fd32_imp__SetFilePointer(HANDLE hFile, LONG lDistanceToMove, PLONG lpDistanceToMoveHigh, DWORD dwMoveMethod){ /* The move method is compatible with newlib and FD32's FS layer #define FILE_BEGIN 0 #define FILE_CURRENT 1 #define FILE_END 2 */ off_t res;#ifdef __WINB_DEBUG__ fd32_log_printf("[WINB] SetFilePointer: %lx\n", (DWORD)hFile);#endif /* TODO: Support 64 bits mode of file seek */ res = lseek((int)hFile, lDistanceToMove, dwMoveMethod); if (res >= 0) { return res; } else { return 0xFFFFFFFF; }}/* The WriteFile function writes data to a file and is designed for both synchronous and asynchronous operation. The function starts writing data to the file at the position indicated by the file pointer. After the write operation has been completed, the file pointer is adjusted by the number of bytes actually written, except when the file is opened with FILE_FLAG_OVERLAPPED. If the file handle was created for overlapped input and output (I/O), the application must adjust the position of the file pointer after the write operation is finished. * * Return Values * If the function succeeds, the return value is nonzero. * If the function fails, the return value is zero. To get extended error information, call GetLastError. */static BOOL WINAPI fd32_imp__WriteFile( HANDLE hFile, LPCVOID lpBuffer, DWORD nNumberOfBytesToWrite, LPDWORD lpNumberOfBytesWritten, LPOVERLAPPED lpOverlapped){ ssize_t res;#ifdef __WINB_DEBUG__ fd32_log_printf("[WINB] WriteFile: %lx %lx %ld\n", (DWORD)hFile, (DWORD)lpBuffer, nNumberOfBytesToWrite); if (hFile == (HANDLE)1) fd32_log_printf("[WINB] buf: %x %x\n", ((BYTE *)lpBuffer)[0], ((BYTE *)lpBuffer)[1]);#endif res = write((int)hFile, lpBuffer, nNumberOfBytesToWrite);#ifdef __WINB_DEBUG__
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -