summaryrefslogtreecommitdiffstats
path: root/private/ntos/nbt/vxd/fileio.c
blob: b319ce2907b14452f496673ff6379c5b469409cb (plain) (blame)
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/*++

Copyright (c) 1991  Microsoft Corporation

Module Name:

    fileio.c

Abstract:

    This source implements a stdio-like facility.

Author:

    Eric Chin (ericc)           April 28, 1992
    John Ludeman (johnl)        Oct    8, 1993 - Rewrote for Vxd

Revision History:

--*/

#include <nbtprocs.h>
#include "hosts.h"

//
//  The maximum line length for a file in the lmhosts file is 256 bytes for
//  the Vxd
//
#define MAX_LMHOSTS_LINE        256

//
//  The number of bytes we buffer on each read
//
#define LMHOSTS_READ_BUFF_SIZE  256

UCHAR GetNextChar( PLM_FILE pFile ) ;

VOID RestoreOldData( PLM_FILE  pFile );

BackupCurrentData( PLM_FILE  pFile );

//*******************  Pageable Routine Declarations ****************
#ifdef ALLOC_PRAGMA
#pragma CTEMakePageable(PAGE, LmCloseFile)
#pragma CTEMakePageable(PAGE, LmFgets)
#pragma CTEMakePageable(PAGE, GetNextChar)
#pragma CTEMakePageable(PAGE, LmOpenFile)
#pragma CTEMakePageable(PAGE, BackupCurrentData)
#pragma CTEMakePageable(PAGE, RestoreOldData)
#endif

#ifdef CHICAGO
//
// In case of chicago, use only linear addresses (Allocate_Global_V86_Data_Area
// call is available only at init time, not if vnbt is load dynamically!)
//
#define pMappedFileBuff pFileBuff
#define pMappedFilePath pFilePath

#else
//
//  In case of snowball, this is the address of the V86 mapped memory for
//  the file read buffer and lmhosts file path
//
PVOID   pMappedFileBuff = NULL ;
PVOID   pMappedFilePath = NULL ;
#endif

//
//  Linear address for file buffer and path (accessible from Vxd)
//
PUCHAR  pFileBuff       = NULL ;
PUCHAR  pFilePath       = NULL ;


/*******************************************************************

    NAME:       VxdInitLmHostsSupport

    SYNOPSIS:   This function just allocates memory to read the contents
                of file into.
                (trying to minimize on changes to snowball side of code
                whic has already shipped: that's why this function!)

    ENTRY:      pchLmHostPath - path to lmhosts file (not used here)
                ulPathSize - size, in chars, of the path

    RETURNS:    TRUE if it works, FALSE if it doesn't.
    COMMENTS:   This is Chicago version of the function.  Snowball's
                version is in vxdfile.asm

    HISTORY:
        Koti    Oct 10, 94

********************************************************************/

#ifdef CHICAGO
BOOL
VxdInitLmHostsSupport( PUCHAR pchLmHostPath, USHORT ulPathSize )
{

    USHORT    Size;

    Size = ulPathSize + LMHOSTS_READ_BUFF_SIZE;

    pFileBuff = CTEAllocInitMem( Size );
    if (pFileBuff == NULL)
    {
        DbgPrint("VxdInitLmHostsSupport: failed to allocate memory") ;
        return( FALSE );
    }

    pFilePath = pFileBuff + LMHOSTS_READ_BUFF_SIZE;

    return( TRUE );

}

#endif
//----------------------------------------------------------------------------

NTSTATUS
LmCloseFile (
    IN PLM_FILE pfile
    )

/*++

Routine Description:

    This function closes a file opened via LmOpenFile(), and frees its
    LM_FILE object.

Arguments:

    pfile  -  pointer to the LM_FILE object

Return Value:

    An NTSTATUS value.

--*/


{
    CTEPagedCode();

    CDbgPrint(DBGFLAG_LMHOST, "LmCloseFile entered\r\n") ;
    CTEFreeMem( pfile->f_linebuffer );

    VxdFileClose( pfile->f_handle ) ;

    CTEFreeMem(pfile);

    CDbgPrint(DBGFLAG_LMHOST, "LmCloseFile leaving\r\n") ;
    return STATUS_SUCCESS ;

} // LmCloseFile



//----------------------------------------------------------------------------

PUCHAR
LmFgets (
    IN PLM_FILE pfile,
    OUT int *nbytes
    )

/*++

Routine Description:

    This function is vaguely similar to fgets(3).

    Starting at the current seek position, it reads through a newline
    character, or the end of the file. If a newline is encountered, it
    is replaced with a NULL character.

Arguments:

    pfile   -  file to read from
    nbytes  -  the number of characters read, excluding the NULL character

Return Value:

    A pointer to the beginning of the line, or NULL if we are at or past
    the end of the file.

--*/
{
    ULONG  cbLine = 0 ;
    UCHAR  ch ;
    BOOL   fDone = FALSE ;
    BOOL   fEOL  = FALSE ;


    CTEPagedCode();

    while ( TRUE )
    {
        switch ( ch = GetNextChar( pfile ))
        {
        case '\n':              // End of line
            if ( !cbLine )      // If it's just a '\n' by itself, ignore it
                continue ;
            //
            //  Fall through
            //

        case '\0':              // End of file
            pfile->f_linebuffer[cbLine] = '\0' ;
            fDone = TRUE ;
            fEOL  = TRUE ;
            break ;

        case '\r':              // Ignore
            continue ;

        default:
            pfile->f_linebuffer[cbLine] = ch ;
            if ( cbLine == (MAX_LMHOSTS_LINE-1) )
            {
                pfile->f_linebuffer[cbLine--] = '\0' ;
                fDone = TRUE ;
            }
            break ;
        }

        if ( fDone )
            break ;

        cbLine++ ;
    }

    //
    //  Scan till the end of this line
    //
    if ( !fEOL )
    {
        while ( (ch = GetNextChar(pfile)) && ch != '\n' )
            ;
    }

    if ( cbLine )
    {
        (pfile->f_lineno)++ ;
        *nbytes = cbLine ;

        CDbgPrint( DBGFLAG_LMHOST, "LmFgets returning \"") ;
        CDbgPrint( DBGFLAG_LMHOST, pfile->f_linebuffer ) ;
        CDbgPrint( DBGFLAG_LMHOST, "\", nbytes = 0x")  ;
        CDbgPrintNum( DBGFLAG_LMHOST, *nbytes ) ;
        CDbgPrint( DBGFLAG_LMHOST, "\r\n")  ;

        return pfile->f_linebuffer ;
    }

    return NULL ;
}

/*******************************************************************

    NAME:       GetNextChar

    SYNOPSIS:   Gets the next character from the file or the line buffer

    ENTRY:      pFile - File we are operating on

    RETURNS:    Next character or '\0' if at the end of file (or there is an
                embedded '\0' in the file).

    NOTES:

********************************************************************/

UCHAR GetNextChar( PLM_FILE pFile )
{
    ULONG BytesRead ;

    CTEPagedCode();

    if ( pFile->f_CurPos < pFile->f_EndOfData )
        return pFile->f_buffer[pFile->f_CurPos++] ;

    if ( pFile->f_EOF )
        return '\0' ;

    //
    //  We've reached the end of the buffer, get more data
    //
    BytesRead = VxdFileRead( pFile->f_handle,
                             LMHOSTS_READ_BUFF_SIZE,
                             pMappedFileBuff ) ;
    pFile->f_CurPos = 0 ;
    if ( BytesRead < LMHOSTS_READ_BUFF_SIZE )
        pFile->f_EOF = TRUE ;

    //
    //  If haven't hit the end of the file, return the next character
    //
    if ( (pFile->f_EndOfData = BytesRead) )
        return pFile->f_buffer[pFile->f_CurPos++] ;

    return '\0' ;
}



//----------------------------------------------------------------------------

PLM_FILE
LmOpenFile (
    IN PUCHAR path
    )

/*++

Routine Description:

    This function opens a file for use by LmFgets().

Arguments:

    path    -  a fully specified, complete path to the file.

Return Value:

    A pointer to an LM_FILE object, or NULL if unsuccessful.

Notes:

    The first time through, we map the lmhosts memory to vm memory and
    allocate a read buffer and map that to VM memory.  Note that this means
    the path must not change and this routine is not reentrant!!

    The reason for this is because the mapping is an expensive operation
    (and there isn't a way to unmap when using Map_Lin_To_VM_Addr).

--*/


{
    HANDLE         handle;
    PLM_FILE       pfile;
    PCHAR          pLineBuff = CTEAllocMem( MAX_LMHOSTS_LINE ) ;
    static int     fInRoutine = 0 ;

    CTEPagedCode();


    if (fInRoutine++)
    {
        CDbgPrint( DBGFLAG_LMHOST, "exiting LmOpenFile: not reentrant!\r\n") ;
        goto ErrorExit;        // We're not reentrant
    }

    CDbgPrint( DBGFLAG_LMHOST, "LmOpenFile entered\r\n") ;

    strcpy( pFilePath, path ) ;

    if ( !pLineBuff || !pFileBuff )
        goto ErrorExit ;

    handle = (HANDLE) VxdFileOpen( pMappedFilePath ) ;

    if ( handle == NULL )
    {
        goto ErrorExit ;
    }

    pfile = (PLM_FILE) CTEAllocMem( sizeof(LM_FILE) );

    if (!pfile)
    {
        VxdFileClose( handle ) ;
        goto ErrorExit ;
    }

    pfile->f_handle              = handle;
    pfile->f_lineno              = 0;
    pfile->f_buffer              = pFileBuff ;
    pfile->f_linebuffer          = pLineBuff ;
    pfile->f_EndOfData           = 0 ;
    pfile->f_CurPos              = 0 ;
    pfile->f_EOF                 = FALSE ;

    CDbgPrint( DBGFLAG_LMHOST, "LmOpenFile returning\r\n") ;

    fInRoutine-- ;

    return pfile ;

ErrorExit:

    fInRoutine--;

    if ( pLineBuff )
        CTEFreeMem( pLineBuff ) ;

    return NULL ;

} // LmOpenFile


//----------------------------------------------------------------------------

BOOL
BackupCurrentData( PLM_FILE  pFile )

/*++

Routine Description:

    This function backs up all the data from lmhosts file into another buffer
    (which is allocated).
    This function is called before opening the next file that we encountered
    via #INCLUDE.  Since the same buffer is used to store the VxdReadFile data,
    we need to save this data.

Arguments:

    pFile - LMfile pointer

Return Value:

    TRUE if everything went ok
    FALSE if memory couldn't be allocated

--*/
{

    CTEPagedCode();

    pFile->f_BackUp = CTEAllocMem( MAX_LMHOSTS_LINE ) ;
    if (pFile->f_BackUp == NULL )
    {
        return( FALSE );
    }

    CTEMemCopy( pFile->f_BackUp, pFile->f_buffer, LMHOSTS_READ_BUFF_SIZE );

    return( TRUE );
}


//----------------------------------------------------------------------------

VOID RestoreOldData( PLM_FILE  pFile )
/*++

Routine Description:

    This function restores all the data we backed up in BackupCurrentData

Arguments:

    pFile - LMfile pointer

Return Value:

    TRUE if everything went ok
    FALSE if memory couldn't be allocated

--*/
{

    CTEPagedCode();

    CTEMemCopy( pFile->f_buffer, pFile->f_BackUp, LMHOSTS_READ_BUFF_SIZE );

    CTEFreeMem( pFile->f_BackUp ) ;

}