aboutsummaryrefslogtreecommitdiff
path: root/board/MAI/bios_emulator/scitech/src/pm/ntdrv/stdio.c
blob: 856215206ff7274735c2092c81e5bf63c246fc6c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/****************************************************************************
*
*                   SciTech OS Portability Manager Library
*
*  ========================================================================
*
*    The contents of this file are subject to the SciTech MGL Public
*    License Version 1.0 (the "License"); you may not use this file
*    except in compliance with the License. You may obtain a copy of
*    the License at http://www.scitechsoft.com/mgl-license.txt
*
*    Software distributed under the License is distributed on an
*    "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
*    implied. See the License for the specific language governing
*    rights and limitations under the License.
*
*    The Original Code is Copyright (C) 1991-1998 SciTech Software, Inc.
*
*    The Initial Developer of the Original Code is SciTech Software, Inc.
*    All Rights Reserved.
*
*  ========================================================================
*
* Language:     ANSI C
* Environment:  32-bit Windows NT driver
*
* Description:  C library compatible I/O functions for use within a Windows
*               NT driver.
*
****************************************************************************/

#include "pmapi.h"
#include "oshdr.h"

/*------------------------ Main Code Implementation -----------------------*/

/****************************************************************************
REMARKS:
NT driver implementation of the ANSI C fopen function.
****************************************************************************/
FILE * fopen(
    const char *filename,
    const char *mode)
{
    ACCESS_MASK                 DesiredAccess;      // for ZwCreateFile...
    OBJECT_ATTRIBUTES           ObjectAttributes;
    ULONG                       ShareAccess;
    ULONG                       CreateDisposition;
    NTSTATUS                    status;
    HANDLE                      FileHandle;
    UNICODE_STRING              *uniFile = NULL;
    PWCHAR                      bufFile = NULL;
    IO_STATUS_BLOCK             IoStatusBlock;
    FILE_STANDARD_INFORMATION   FileInformation;
    FILE_POSITION_INFORMATION   FilePosition;
    char                        kernelFilename[PM_MAX_PATH+5];
    FILE                        *f;

    // Add prefix for addressing the file system. "\??\" is short for "\DosDevices\"
    strcpy(kernelFilename, "\\??\\");
    strcat(kernelFilename, filename);
    if ((f = PM_malloc(sizeof(FILE))) == NULL)
        goto Error;
    f->offset = 0;
    f->text = (mode[1] == 't' || mode[2] == 't');
    f->writemode = (mode[0] == 'w') || (mode[0] == 'a');
    if (mode[0] == 'r') {
        // omode = OPEN_ACCESS_READONLY | OPEN_SHARE_COMPATIBLE;
        // action = ACTION_IFEXISTS_OPEN | ACTION_IFNOTEXISTS_FAIL;
        DesiredAccess = GENERIC_READ;
        ShareAccess = FILE_SHARE_READ | FILE_SHARE_WRITE;
        CreateDisposition = FILE_OPEN;
        }
    else if (mode[0] == 'w') {
        // omode = OPEN_ACCESS_WRITEONLY | OPEN_SHARE_COMPATIBLE;
        // action = ACTION_IFEXISTS_TRUNCATE | ACTION_IFNOTEXISTS_CREATE;
        DesiredAccess = GENERIC_WRITE;
        ShareAccess = FILE_SHARE_READ | FILE_SHARE_WRITE;
        CreateDisposition = FILE_SUPERSEDE;
        }
    else {
        // omode = OPEN_ACCESS_READWRITE | OPEN_SHARE_COMPATIBLE;
        // action = ACTION_IFEXISTS_OPEN | ACTION_IFNOTEXISTS_CREATE;
        DesiredAccess = GENERIC_READ | GENERIC_WRITE;
        ShareAccess = FILE_SHARE_READ;
        CreateDisposition = FILE_OPEN_IF;
        }

    // Convert filename string to ansi string and then to UniCode string
    if ((uniFile = _PM_CStringToUnicodeString(kernelFilename)) == NULL)
        return NULL;

    // Create the file
    InitializeObjectAttributes (&ObjectAttributes,
                                uniFile,
                                OBJ_CASE_INSENSITIVE,
                                NULL,
                                NULL);
    status = ZwCreateFile( &FileHandle,
                            DesiredAccess | SYNCHRONIZE,
                            &ObjectAttributes,
                            &IoStatusBlock,
                            NULL,                   // AllocationSize  OPTIONAL,
                            FILE_ATTRIBUTE_NORMAL,
                            ShareAccess,
                            CreateDisposition,
                            FILE_RANDOM_ACCESS,     // CreateOptions,
                            NULL,                   // EaBuffer  OPTIONAL,
                            0                       // EaLength (required if EaBuffer)
                            );
    if (!NT_SUCCESS (status))
        goto Error;
    f->handle = (int)FileHandle;

    // Determine size of the file
    status = ZwQueryInformationFile(  FileHandle,
                                      &IoStatusBlock,
                                      &FileInformation,
                                      sizeof(FILE_STANDARD_INFORMATION),
                                      FileStandardInformation
                                      );
    if (!NT_SUCCESS (status))
        goto Error;
    f->filesize = FileInformation.EndOfFile.LowPart;

    // Move to the end of the file if we are appending
    if (mode[0] == 'a') {
        FilePosition.CurrentByteOffset.HighPart = 0;
        FilePosition.CurrentByteOffset.LowPart = f->filesize;
        status = ZwSetInformationFile(  FileHandle,
                                        &IoStatusBlock,
                                        &FilePosition,
                                        sizeof(FILE_POSITION_INFORMATION),
                                        FilePositionInformation
                                        );
        if (!NT_SUCCESS (status))
            goto Error;
        }
    return f;

Error:
    if (f) PM_free(f);
    if (uniFile) _PM_FreeUnicodeString(uniFile);
    return NULL;
}

/****************************************************************************
REMARKS:
NT driver implementation of the ANSI C fread function.
****************************************************************************/
size_t fread(
    void *ptr,
    size_t size,
    size_t n,
    FILE *f)
{
    NTSTATUS        status;
    IO_STATUS_BLOCK IoStatusBlock;
    LARGE_INTEGER   ByteOffset;

    // Read any extra bytes from the file
    ByteOffset.HighPart = 0;
    ByteOffset.LowPart = f->offset;
    status = ZwReadFile( (HANDLE)f->handle,
                         NULL,              //IN HANDLE  Event  OPTIONAL,
                         NULL,              //  IN PIO_APC_ROUTINE  ApcRoutine  OPTIONAL,
                         NULL,              //  IN PVOID  ApcContext  OPTIONAL,
                         &IoStatusBlock,
                         ptr,               //  OUT PVOID  Buffer,
                         size * n,          //IN ULONG  Length,
                         &ByteOffset,       //OPTIONAL,
                         NULL               //IN PULONG  Key  OPTIONAL
                         );
    if (!NT_SUCCESS (status))
        return 0;
    f->offset += IoStatusBlock.Information;
    return IoStatusBlock.Information / size;
}

/****************************************************************************
REMARKS:
NT driver implementation of the ANSI C fwrite function.
****************************************************************************/
size_t fwrite(
    const void *ptr,
    size_t size,
    size_t n,
    FILE *f)
{
    NTSTATUS        status;
    IO_STATUS_BLOCK IoStatusBlock;
    LARGE_INTEGER   ByteOffset;

    if (!f->writemode)
        return 0;
    ByteOffset.HighPart = 0;
    ByteOffset.LowPart = f->offset;
    status = ZwWriteFile( (HANDLE)f->handle,
                          NULL,             //IN HANDLE  Event  OPTIONAL,
                          NULL,             //  IN PIO_APC_ROUTINE  ApcRoutine  OPTIONAL,
                          NULL,             //  IN PVOID  ApcContext  OPTIONAL,
                          &IoStatusBlock,
                          (void*)ptr,       //  OUT PVOID  Buffer,
                          size * n,         //IN ULONG  Length,
                          &ByteOffset,      //OPTIONAL,
                          NULL              //IN PULONG  Key  OPTIONAL
                          );
    if (!NT_SUCCESS (status))
        return 0;
    f->offset += IoStatusBlock.Information;
    if (f->offset > f->filesize)
        f->filesize = f->offset;
    return IoStatusBlock.Information / size;
}

/****************************************************************************
REMARKS:
NT driver implementation of the ANSI C fflush function.
****************************************************************************/
int fflush(
    FILE *f)
{
    // Nothing to do here as we are not doing buffered I/O
    (void)f;
    return 0;
}

/****************************************************************************
REMARKS:
NT driver implementation of the ANSI C fseek function.
****************************************************************************/
int fseek(
    FILE *f,
    long int offset,
    int whence)
{
    NTSTATUS                    status;
    FILE_POSITION_INFORMATION   FilePosition;
    IO_STATUS_BLOCK             IoStatusBlock;

    if (whence == 0)
        f->offset = offset;
    else if (whence == 1)
        f->offset += offset;
    else if (whence == 2)
        f->offset = f->filesize + offset;
    FilePosition.CurrentByteOffset.HighPart = 0;
    FilePosition.CurrentByteOffset.LowPart = f->offset;
    status = ZwSetInformationFile( (HANDLE)f->handle,
                                   &IoStatusBlock,
                                   &FilePosition,
                                   sizeof(FILE_POSITION_INFORMATION),
                                   FilePositionInformation
                                   );
    if (!NT_SUCCESS (status))
        return -1;
    return 0;
}

/****************************************************************************
REMARKS:
NT driver implementation of the ANSI C ftell function.
****************************************************************************/
long ftell(
    FILE *f)
{
    return f->offset;
}

/****************************************************************************
REMARKS:
NT driver implementation of the ANSI C feof function.
****************************************************************************/
int feof(
    FILE *f)
{
    return (f->offset == f->filesize);
}

/****************************************************************************
REMARKS:
NT driver implementation of the ANSI C fgets function.
****************************************************************************/
char *fgets(
    char *s,
    int n,
    FILE *f)
{
    int     len;
    char    *cs;

    // Read the entire buffer into memory (our functions are unbuffered!)
    if ((len = fread(s,1,n,f)) == 0)
        return NULL;

    // Search for '\n' or end of string
    if (n > len)
        n = len;
    cs = s;
    while (--n > 0) {
        if (*cs == '\n')
            break;
        cs++;
        }
    *cs = '\0';
    return s;
}

/****************************************************************************
REMARKS:
NT driver implementation of the ANSI C fputs function.
****************************************************************************/
int fputs(
    const char *s,
    FILE *f)
{
    return fwrite(s,1,strlen(s),f);
}

/****************************************************************************
REMARKS:
NT driver implementation of the ANSI C fclose function.
****************************************************************************/
int fclose(
    FILE *f)
{
    ZwClose((HANDLE)f->handle);
    PM_free(f);
    return 0;
}