[NTVDM]
[reactos.git] / subsystems / ntvdm / utils.c
1 /*
2 * COPYRIGHT: GPL - See COPYING in the top level directory
3 * PROJECT: ReactOS Virtual DOS Machine
4 * FILE: utils.c
5 * PURPOSE: Utility Functions
6 * PROGRAMMERS: Hermes Belusca-Maito (hermes.belusca@sfr.fr)
7 */
8
9 /* INCLUDES *******************************************************************/
10
11 #define NDEBUG
12
13 #include "emulator.h"
14
15 /* PRIVATE FUNCTIONS **********************************************************/
16
17 /* PUBLIC FUNCTIONS ***********************************************************/
18
19 VOID
20 FileClose(IN HANDLE FileHandle)
21 {
22 CloseHandle(FileHandle);
23 }
24
25 HANDLE
26 FileOpen(IN PCSTR FileName,
27 OUT PULONG FileSize OPTIONAL)
28 {
29 HANDLE hFile;
30 ULONG ulFileSize;
31
32 /* Open the file */
33 SetLastError(0); // For debugging purposes
34 hFile = CreateFileA(FileName,
35 GENERIC_READ,
36 FILE_SHARE_READ,
37 NULL,
38 OPEN_EXISTING,
39 FILE_ATTRIBUTE_NORMAL,
40 NULL);
41 DPRINT1("File '%s' opening %s ; GetLastError() = %u\n",
42 FileName, hFile != INVALID_HANDLE_VALUE ? "succeeded" : "failed", GetLastError());
43
44 /* If we failed, bail out */
45 if (hFile == INVALID_HANDLE_VALUE) return NULL;
46
47 /* OK, we have a handle to the file */
48
49 /*
50 * Retrieve the size of the file. In NTVDM we will handle files
51 * of maximum 1Mb so we can largely use GetFileSize only.
52 */
53 ulFileSize = GetFileSize(hFile, NULL);
54 if (ulFileSize == INVALID_FILE_SIZE && GetLastError() != ERROR_SUCCESS)
55 {
56 /* We failed, bail out */
57 DPRINT1("Error when retrieving file size, or size too large (%d)\n", ulFileSize);
58 FileClose(hFile);
59 return NULL;
60 }
61
62 /* Success, return file handle and size if needed */
63 if (FileSize) *FileSize = ulFileSize;
64 return hFile;
65 }
66
67 BOOLEAN
68 FileLoadByHandle(IN HANDLE FileHandle,
69 IN PVOID Location,
70 IN ULONG FileSize,
71 OUT PULONG BytesRead)
72 {
73 BOOLEAN Success;
74
75 /* Attempt to load the file into memory */
76 SetLastError(0); // For debugging purposes
77 Success = !!ReadFile(FileHandle,
78 Location, // REAL_TO_PHYS(LocationRealPtr),
79 FileSize,
80 BytesRead,
81 NULL);
82 DPRINT1("File loading %s ; GetLastError() = %u\n", Success ? "succeeded" : "failed", GetLastError());
83
84 return Success;
85 }
86
87 /* EOF */