Modules/NTFS - Implimented update sequences, fixes bad filenames
authorJohn Hodge <[email protected]>
Tue, 25 Jun 2013 00:02:24 +0000 (08:02 +0800)
committerJohn Hodge <[email protected]>
Tue, 25 Jun 2013 00:02:24 +0000 (08:02 +0800)
KernelLand/Modules/Filesystems/NTFS/common.h
KernelLand/Modules/Filesystems/NTFS/dir.c
KernelLand/Modules/Filesystems/NTFS/main.c

index db117a6..80e5bbe 100644 (file)
@@ -85,6 +85,7 @@ struct sNTFS_Attrib
 extern tVFS_NodeType   gNTFS_DirType;
 extern tVFS_NodeType   gNTFS_FileType;
 
+extern int     NTFS_int_ApplyUpdateSequence(void *Buf, size_t BufLen, const Uint16 *Sequence, size_t NumEntries);
 // -- MFT Access / Manipulation
 extern tNTFS_FILE_Header       *NTFS_GetMFT(tNTFS_Disk *Disk, Uint32 MFTEntry);
 extern void    NTFS_ReleaseMFT(tNTFS_Disk *Disk, Uint32 MFTEntry, tNTFS_FILE_Header *Entry);
index 17785fb..617d7db 100644 (file)
@@ -76,6 +76,9 @@ int NTFS_ReadDir(tVFS_Node *Node, int Pos, char Dest[FILENAME_MAX])
                // Check allocation
                struct sNTFS_IndexHeader *hdr = (void*)buf;
                ASSERT(hdr->EntriesOffset + 0x18 < len);
+               // Apply update sequence
+               ASSERT(hdr->UpdateSequenceOfs + 2*hdr->UpdateSequenceSize <= len);
+               NTFS_int_ApplyUpdateSequence(buf,len, (void*)(buf+hdr->UpdateSequenceOfs), hdr->UpdateSequenceSize);
                size_t  ofs = hdr->EntriesOffset + 0x18;
                ent = NTFS_int_IterateIndex(buf + ofs, len - ofs, &Pos);
                vcn ++;
@@ -163,6 +166,10 @@ void NTFS_int_DumpIndex(tNTFS_Attrib *Allocation, Uint AttribID)
        while( (len = NTFS_ReadAttribData(Allocation, vcn*block_size, block_size, buf)) )
        {
                struct sNTFS_IndexHeader        *hdr = (void*)buf;
+               // Apply update sequence
+               ASSERT(hdr->UpdateSequenceOfs + 2*hdr->UpdateSequenceSize <= len);
+               NTFS_int_ApplyUpdateSequence(buf,len, (void*)(buf+hdr->UpdateSequenceOfs), hdr->UpdateSequenceSize);
+               
                LOG("VCN %x: Ofs=%x, Size=%x",
                        vcn, hdr->EntriesOffset, hdr->EntriesSize);
                if( hdr->ThisVCN != vcn ) {
@@ -312,6 +319,10 @@ tVFS_Node *NTFS_FindDir(tVFS_Node *Node, const char *Name, Uint Flags)
                if( len == 0 )
                        break;
                tNTFS_IndexHeader       *hdr = (void*)buf;
+               // Apply update sequence
+               ASSERT(hdr->UpdateSequenceOfs + 2*hdr->UpdateSequenceSize <= len);
+               NTFS_int_ApplyUpdateSequence(buf,len, (void*)(buf+hdr->UpdateSequenceOfs), hdr->UpdateSequenceSize);
+               // Search
                //mftent = NTFS_BTreeSearch(len, (void*)buf, NTFS_BTreeSearch_CmpI30, name16len*2, name16);
                mftent = NTFS_BTreeSearch(
                        len-(hdr->EntriesOffset+0x18), buf+hdr->EntriesOffset+0x18,
index 5e1c2ae..8d6a724 100644 (file)
@@ -177,9 +177,33 @@ void NTFS_Unmount(tVFS_Node *Node)
        free(Disk);
 }
 
+int NTFS_int_ApplyUpdateSequence(void *Buffer, size_t BufLen, const Uint16 *Sequence, size_t NumEntries)
+{
+       Uint16  cksum = Sequence[0];
+       LOG("cksum = %04x", cksum);
+       Sequence ++;
+       Uint16  *buf16 = Buffer;
+       for( int i = 0; i < NumEntries-1; i ++ )
+       {
+               size_t  ofs = (i+1)*512 - 2;
+               if( ofs + 2 > BufLen ) {
+                       // Oops?
+                       Log_Warning("NTFS", "%x > %x", ofs+2, BufLen);
+               }
+               Uint16  *cksum_word = &buf16[ofs/2];
+               LOG("[%i]: %04x => %04x", i, Sequence[i], *cksum_word);
+               if( *cksum_word != cksum ) {
+                       Log_Warning("NTFS", "Disk corruption detected");
+                       return 1;
+               }
+               *cksum_word = Sequence[i];
+       }
+       return 0;
+}
+
 tNTFS_FILE_Header *NTFS_GetMFT(tNTFS_Disk *Disk, Uint32 MFTEntry)
 {
-       void    *ret = malloc( Disk->MFTRecSize );
+       tNTFS_FILE_Header       *ret = malloc( Disk->MFTRecSize );
        if(!ret) {
                Log_Warning("FS_NTFS", "malloc() fail!");
                return NULL;
@@ -195,7 +219,11 @@ tNTFS_FILE_Header *NTFS_GetMFT(tNTFS_Disk *Disk, Uint32 MFTEntry)
        else {
                NTFS_ReadAttribData(Disk->MFTDataAttr, MFTEntry * Disk->MFTRecSize, Disk->MFTRecSize, ret);
        }
-       
+
+       NTFS_int_ApplyUpdateSequence(ret, Disk->MFTRecSize,
+               (void*)((char*)ret + ret->UpdateSequenceOfs), ret->UpdateSequenceSize
+               );
+
        return ret;
 }
 

UCC git Repository :: git.ucc.asn.au