예제 #1
0
        /// <summary>
        /// 搜索文件或者目录的主文件表
        /// </summary>
        /// <param name="dirverHandle"></param>
        /// <param name="paths"></param>
        /// <param name="buffer"></param>
        /// <returns></returns>
        public MainFileTable SearchPath(Microsoft.Win32.SafeHandles.SafeFileHandle dirverHandle, IEnumerable <string> paths, byte[] buffer = null)
        {
            int           bufferSize    = BytePerSector << 1;
            MainFileTable mainFileTable = FindRoot(dirverHandle);

            foreach (string path in paths)
            {
                if (!mainFileTable.IsBuffer)
                {
                    return(default(MainFileTable));
                }
                bool isPath = false;
                foreach (FileIndexEntry fileIndexEntry in mainFileTable.GetIndexEntrys(dirverHandle, this))
                {
                    if (fileIndexEntry.IsFileName(path))
                    {
                        if (buffer == null || buffer.Length < bufferSize)
                        {
                            buffer = new byte[bufferSize];
                        }
                        mainFileTable = Get(dirverHandle, fileIndexEntry, buffer);
                        isPath        = true;
                        break;
                    }
                }
                if (!isPath)
                {
                    return(default(MainFileTable));
                }
            }
            return(mainFileTable);
        }
예제 #2
0
        /// <summary>
        /// 获取跟目录的主文件表
        /// </summary>
        /// <param name="dirverHandle"></param>
        /// <param name="buffer"></param>
        /// <returns></returns>
        public MainFileTable FindRoot(Microsoft.Win32.SafeHandles.SafeFileHandle dirverHandle, byte[] buffer = null)
        {
            int  bytePerCluster = (int)BytePerCluster;
            long fileIndex      = (long)(MainFileTableClusterIndex * (uint)bytePerCluster);

            if (Kernel32.SetFilePointer(dirverHandle, fileIndex))
            {
                if (buffer == null || buffer.Length < bytePerCluster)
                {
                    buffer = new byte[bytePerCluster];
                }
                do
                {
                    if (Kernel32.ReadFile(dirverHandle, buffer, bytePerCluster) == bytePerCluster)
                    {
                        int bufferIndex = 0;
                        do
                        {
                            MainFileTable mainFileTable = new MainFileTable(buffer, bufferIndex, fileIndex);
                            if (mainFileTable.Buffer != null)
                            {
                                foreach (MainFileTableAttribute mainFileTableAttribute in mainFileTable.Attributes)
                                {
                                    if (mainFileTableAttribute.Type == MainFileTableAttributeType.FileName && !mainFileTableAttribute.ResidentFlag)
                                    {
                                        FileNameAttribute fileNameAttribute = new FileNameAttribute(mainFileTableAttribute);
                                        if (fileNameAttribute.IsRoot)
                                        {
                                            return(mainFileTable);
                                        }
                                    }
                                }
                                bufferIndex += (int)mainFileTable.BytesAllocated;
                            }
                            else
                            {
                                return(default(MainFileTable));
                            }
                        }while (bufferIndex < bytePerCluster);
                        fileIndex += bytePerCluster;
                    }
                    else
                    {
                        return(default(MainFileTable));
                    }
                }while (true);
            }
            return(default(MainFileTable));
        }