summaryrefslogblamecommitdiffstats
path: root/private/utils/cuhpfs/bldnames.cxx
blob: 3c9ac2d550fdbc2b57b1577988e1d9406258df7c (plain) (tree)
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



















































































































































































































































































































































































































































































                                                                                
/*++

Copyright (c) 1994  Microsoft Corporation

Module Name:

    bldnames.cxx

Abstract:

    This module contains the routines which build up the name-translation
    table.

Author:

    Bill McJohn (billmc) 02-Mar-1994

Environment:

    ULIB, User Mode

--*/

#include <pch.cxx>

#define _NTAPI_ULIB_

#include "ulib.hxx"

#include "wstring.hxx"
#include "message.hxx"
#include "rtmsg.h"

#include "nametab.hxx"

#include "uhpfs.hxx"
#include "hpfsvol.hxx"
#include "hpfssa.hxx"
#include "superb.hxx"
#include "cpinfo.hxx"
#include "fnode.hxx"
#include "dirblk.hxx"

// Private prototypes:
//
BOOLEAN
NameIsCodepageInvariant(
    IN  USHORT  NameLength,
    IN  PSTR    Name
    );

BOOLEAN
ConstructNameTableFromDirblk(
    IN OUT  PHPFS_VOL   HpfsVol,
    IN OUT  PHPFS_SA    HpfsSa,
    IN      LBN         DirblkLbn,
    IN OUT  PNAME_TABLE NameTable,
    IN OUT  PMESSAGE    Message,
    IN      PCWSTRING   Path
    );

BOOLEAN
ConstructNameTableFromFnode(
    IN OUT  PHPFS_VOL   HpfsVol,
    IN OUT  PHPFS_SA    HpfsSa,
    IN      LBN         FnodeLbn,
    IN OUT  PNAME_TABLE NameTable,
    IN OUT  PMESSAGE    Message,
    IN      PCWSTRING   Path
    );




BOOLEAN
NameIsCodepageInvariant(
    IN  USHORT  NameLength,
    IN  PUCHAR  Name
    )
/*++

Routine Description:

    This function determines whether a name is codepage-invariant,
    i.e. has no characters greater than 127.

Arguments:

    NameLength  --  Supplies the number of bytes in the name.
    Name        --  Supplies the name.

Return Value:

    TRUE if none of the bytes in the name are greater than 127.

--*/
{
    USHORT i;
    PUCHAR puch = (PUCHAR)Name;

    for( i = 0; i < NameLength; i++ ) {

        if( *puch > 127 ) {

            return FALSE;
        }

        puch++;
    }

    return TRUE;
}

BOOLEAN
ConstructNameTableFromDirblk(
    IN OUT  PHPFS_VOL   HpfsVol,
    IN OUT  PHPFS_SA    HpfsSa,
    IN      LBN         DirblkLbn,
    IN OUT  PNAME_TABLE NameTable,
    IN OUT  PMESSAGE    Message,
    IN      PCWSTRING   Path
    )
/*++

Routine Description:

    This adds to the Name Table the names in the directory sub-tree
    rooted at DirblkLbn and all subdirectories.

Arguments:

    HpfsVol     --  Supplies the HPFS Volume being converted.
    HpfsSa      --  Supplies the Super Area for the volume.
    DirblkLbn   --  Supplies the LBN for the Dirblk in question.
    NameTable   --  Supplies the name table being constructed.
    Message     --  Supplies an outlet for messages.
    Path        --  Supplies the path for the directory containing
                    this dirblk.

Return Value:

    TRUE upon successful completion.

--*/
{
    CONST UnicodeBufferLength = 256;
    WCHAR UnicodeNameBuffer[UnicodeBufferLength];

    FSTRING     Backslash;
    DSTRING     ChildName, ChildPath;
    DIRBLK      Dirblk;
    PDIRENTD    CurrentEntry;
    ULONG       CurrentOffset;
    ULONG       UnicodeNameLength;
    USHORT      CodepageId;

    Backslash.Initialize( L"\\" );

    if( !Dirblk.Initialize( HpfsVol, HpfsSa->GetHotfixList(), DirblkLbn ) ) {

        Message->Set( MSG_CONV_NO_MEMORY );
        Message->Display( "" );
        return FALSE;
    }

    if( !Dirblk.Read() || !Dirblk.IsDirblk() ) {

        Message->Set( MSG_CONV_HPFS_CORRUPT_VOLUME );
        Message->Display( "" );
        return FALSE;
    }

    // Spin through the directory entries in the DIRBLK, converting
    // their names to Unicode and adding them to the Name Table.
    // and recursing into child DIRBLK's.
    //
    CurrentEntry = Dirblk.GetFirstEntry();
    CurrentOffset = Dirblk.QueryEntryOffset( CurrentEntry );

    while( TRUE ) {

        if( CurrentOffset + sizeof( USHORT ) > DIRBLK_SIZE             ||
            CurrentOffset + CurrentEntry->cchThisEntry > DIRBLK_SIZE   ||
            CurrentEntry->cchThisEntry == 0 ) {

            Message->Set( MSG_CONV_HPFS_CORRUPT_VOLUME );
            Message->Display( "" );
            return FALSE;
        }

        // If this DIRBLK has a child, recurse into it.
        //
        if( (CurrentEntry->fFlags & DF_BTP) &&
            !ConstructNameTableFromDirblk( HpfsVol,
                                           HpfsSa,
                                           BTP(CurrentEntry),
                                           NameTable,
                                           Message,
                                           Path ) ) {

            return FALSE;
        }

        if( CurrentEntry->fFlags & DF_END ) {

            break;
        }

        if( CurrentEntry->fFlags & DF_SPEC ) {

            // Move on to the next entry.
            //
            CurrentOffset += CurrentEntry->cchThisEntry;
            CurrentEntry = NEXT_ENTRY( CurrentEntry );
            continue;
        }

        if( CurrentEntry->cchName == 0 ) {

            Message->Set( MSG_CONV_HPFS_CORRUPT_VOLUME );
            Message->Display( "" );
            return FALSE;
        }

        // Convert the name of this directory entry and
        // add it to the name table.  Note that it can
        // be omitted from the name-table if it is
        // codepage-invariant, but we need the unicode
        // name to construct the full path.
        //
        CodepageId = HpfsSa->GetCasemap()->
                         QueryCodepageId( CurrentEntry->bCodePage );

        UnicodeNameLength =
            MultiByteToWideChar( CodepageId,
                                 MB_PRECOMPOSED,
                                 (PCHAR)&CurrentEntry->bName[0],
                                 CurrentEntry->cchName,
                                 UnicodeNameBuffer,
                                 UnicodeBufferLength );

        if( UnicodeNameLength == 0 ) {

            if( Path->QueryChCount() == 0 ) {
                Message->Set( MSG_CONV_INCONVERTIBLE_NAME_IN_ROOT );
                Message->Display( "" );
            } else {
                Message->Set( MSG_CONV_INCONVERTIBLE_NAME );
                Message->Display( "%W", Path );
            }
            return FALSE;
        }

        if( !NameIsCodepageInvariant( CurrentEntry->cchName,
                                      &CurrentEntry->bName[0] ) ) {

            if( !NameTable->Add( CodepageId,
                                 CurrentEntry->cchName,
                                 &CurrentEntry->bName[0],
                                 (USHORT)UnicodeNameLength,
                                 UnicodeNameBuffer ) ) {

                Message->Set( MSG_CONV_NO_MEMORY );
                Message->Display( "" );
                return FALSE;
            }
        }

        // If it's a subdirectory, recurse into it.
        //
        if( CurrentEntry->fAttr & ATTR_DIRECTORY ) {

            if( !ChildName.Initialize( UnicodeNameBuffer, UnicodeNameLength ) ||
                !ChildPath.Initialize( Path ) ||
                !ChildPath.Strcat( &Backslash ) ||
                !ChildPath.Strcat( &ChildName ) ) {

                Message->Set( MSG_CONV_NO_MEMORY );
                Message->Display( "" );
                return FALSE;
            }

            if( !ConstructNameTableFromFnode( HpfsVol,
                                              HpfsSa,
                                              CurrentEntry->lbnFnode,
                                              NameTable,
                                              Message,
                                              &ChildPath ) ) {

                return FALSE;
            }
        }

        // Move on to the next entry.
        //
        CurrentOffset += CurrentEntry->cchThisEntry;
        CurrentEntry = NEXT_ENTRY( CurrentEntry );
    }

    return TRUE;
}

BOOLEAN
ConstructNameTableFromFnode(
    IN OUT  PHPFS_VOL   HpfsVol,
    IN OUT  PHPFS_SA    HpfsSa,
    IN      LBN         FnodeLbn,
    IN OUT  PNAME_TABLE NameTable,
    IN OUT  PMESSAGE    Message,
    IN      PCWSTRING   Path
    )
/*++

Routine Description:

    This adds to the Name Table the names in the directory which has
    its FNODE at FnodeLbn, and all subdirectories.

Arguments:

Return Value:

    HpfsVol     --  Supplies the HPFS Volume being converted.
    HpfsSa      --  Supplies the Super Area for the volume.
    FnodeLbn    --  Supplies the LBN for the FNode in question.
                    Note that this must be a directory FNode.
    NameTable   --  Supplies the name table being constructed.
    Message     --  Supplies an outlet for messages.

--*/
{
    FNODE   Fnode;

    if( !Fnode.Initialize( HpfsVol, FnodeLbn ) ) {

        Message->Set( MSG_CONV_NO_MEMORY );
        Message->Display( "" );
        return FALSE;
    }

    if( !Fnode.Read() || !Fnode.IsFnode() ) {

        Message->Set( MSG_CONV_HPFS_CORRUPT_VOLUME );
        Message->Display( "" );
        return FALSE;
    }

    return( ConstructNameTableFromDirblk( HpfsVol,
                                          HpfsSa,
                                          Fnode.QueryRootDirblkLbn(),
                                          NameTable,
                                          Message,
                                          Path ) );
}

extern "C"
BOOLEAN
FAR APIENTRY
ConstructNameTable(
    IN      PCWSTRING   NtDriveName,
    IN      PCWSTRING   FileName,
    IN OUT  PMESSAGE    Message
    )
/*++

Routine Description:

    This function creates a MBCS-to-Unicode name translation
    table for use by Autoconv.  This allows Convert to squirrel
    away Unicode translations for names that Autoconv can't
    translate for itself.

Arguments:

    NtDriveName --  Supplies the name of the volume for which the
                    name table is to be created.
    FileName    --  Supplies the name of the file in which the name
                    table will be stored.  (This file will always
                    be in the root directory of the volume being
                    converted.)
    Message     --  Supplies an outlet for messages.

Return Value:

    TRUE upon successful completion.

--*/
{
    HPFS_VOL            HpfsVol;
    PHPFS_SA            HpfsSa;
    NAME_TABLE          NameTable;
    FSTRING             BackSlash, RootName;
    DSTRING             QualifiedFileName;

    if( !BackSlash.Initialize( L"\\" )                  ||
        !QualifiedFileName.Initialize( NtDriveName )    ||
        !QualifiedFileName.Strcat( &BackSlash )         ||
        !QualifiedFileName.Strcat( FileName ) ) {

        Message->Set( MSG_CONV_NO_MEMORY );
        Message->Display( "" );
        return FALSE;
    }

    if( !NameTable.Initialize() ) {

        Message->Set( MSG_CONV_NO_MEMORY );
        Message->Display( "" );
        return FALSE;
    }

    // Initialize the HPFS volume object and set up the helpers
    // (bitmap, codepage and casemap, and hotfix list)
    //
    if( !HpfsVol.Initialize( NtDriveName, Message ) ) {

        return FALSE;
    }

    HpfsSa = HpfsVol.GetHPFSSuperArea();

    if( !HpfsSa->CheckSuperBlockSignatures() ) {

        Message->Set( MSG_CONV_HPFS_NOT_HPFS );
        Message->Display( "" );
        return FALSE;
    }

    if( !HpfsSa->IsClean() ) {

        Message->Set( MSG_CONV_HPFS_DIRTY_VOLUME );
        Message->Display( "" );
        return FALSE;
    }

    if( HpfsSa->GetSpare()->QueryHotFixCount() != 0 ||
        HpfsSa->GetSpare()->IsHotFixesUsed() ) {

        Message->Set( MSG_CONV_HPFS_HOTFIXES_PRESENT );
        Message->Display( "" );
        return FALSE;
    }

    if( !HpfsSa->SetupHelpers() ) {

        Message->Set( MSG_CONV_HPFS_CORRUPT_VOLUME );
        Message->Display( "" );
        return FALSE;
    }

    Message->Set( MSG_CONV_CONSTRUCTING_NAME_TABLE );
    Message->Display( "" );

    RootName.Initialize( L"" );

    if( !ConstructNameTableFromFnode( &HpfsVol,
                                      HpfsSa,
                                      HpfsSa->GetSuper()->QueryRootFnodeLbn(),
                                      &NameTable,
                                      Message,
                                      &RootName ) ||
        !NameTable.Write( &QualifiedFileName, Message ) ) {

        return FALSE;
    }

    return TRUE;
}