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