Пример #1
0
        private List<Base> ReadDirectoryContents(byte[] xData)
        {
            var xResult = new List<Base>();
            //TODO: Change xLongName to StringBuilder
            string xLongName = "";
            string xName = "";
            for (UInt32 i = 0; i < xData.Length; i = i + 32)
            {
                FatHelpers.Debug("-------------------------------------------------");
                byte xAttrib = xData[i + 11];
                byte xStatus = xData[i];

                FatHelpers.Debug("Attrib = " + xAttrib.ToString() + ", Status = " + xStatus);
                if (xAttrib == DirectoryEntryAttributeConsts.LongName)
                {
                    byte xType = xData[i + 12];
                    byte xOrd = xData[i];
                    FatHelpers.Debug("Reading LFN with Seqnr " + xOrd.ToString() + ", Type = " + xType);
                    if (xOrd == 0xE5)
                    {
                        FatHelpers.Debug("Skipping deleted entry");
                        continue;
                    }
                    if (xType == 0)
                    {
                        if ((xOrd & 0x40) > 0)
                        {
                            xLongName = "";
                        }
                        //TODO: Check LDIR_Ord for ordering and throw exception
                        // if entries are found out of order.
                        // Also save buffer and only copy name if a end Ord marker is found.
                        string xLongPart = xData.GetUtf16String(i + 1, 5);
                        // We have to check the length because 0xFFFF is a valid Unicode codepoint.
                        // So we only want to stop if the 0xFFFF is AFTER a 0x0000. We can determin
                        // this by also looking at the length. Since we short circuit the or, the length
                        // is rarely evaluated.
                        if (xData.ToUInt16(i + 14) != 0xFFFF || xLongPart.Length == 5)
                        {
                            xLongPart = xLongPart + xData.GetUtf16String(i + 14, 6);
                            if (xData.ToUInt16(i + 28) != 0xFFFF || xLongPart.Length == 11)
                            {
                                xLongPart = xLongPart + xData.GetUtf16String(i + 28, 2);
                            }
                        }
                        xLongName = xLongPart + xLongName;
                        xLongPart = null;
                        //TODO: LDIR_Chksum
                    }
                }
                else
                {
                    xName = xLongName;
                    if (xStatus == 0x00)
                    {
                        // Empty slot, and no more entries after this
                        break;
                    }
                    else if (xStatus == 0x05)
                    {
                        // Japanese characters - We dont handle these
                    }
                    else if (xStatus == 0xE5)
                    {
                        // Empty slot, skip it
                    }
                    else if (xStatus >= 0x20)
                    {
                        if (xLongName.Length > 0)
                        {
                            // Leading and trailing spaces are to be ignored according to spec.
                            // Many programs (including Windows) pad trailing spaces although it
                            // it is not required for long names.
                            // As per spec, ignore trailing periods
                            xName = xLongName.Trim();

                            //If there are trailing periods
                            int nameIndex = xName.Length - 1;
                            if (xName[nameIndex] == '.')
                            {
                                //Search backwards till we find the first non-period character
                                for (; nameIndex > 0; nameIndex--)
                                {
                                    if (xName[nameIndex] != '.')
                                    {
                                        break;
                                    }
                                }
                                //Substring to remove the periods
                                xName = xName.Substring(0, nameIndex + 1);
                            }
                            xLongName = "";
                        }
                        else
                        {
                            string xEntry = xData.GetAsciiString(i, 11);
                            xName = xEntry.Substring(0, 8).TrimEnd();
                            string xExt = xEntry.Substring(8, 3).TrimEnd();
                            if (xExt.Length > 0)
                            {
                                xName = xName + "." + xExt;
                            }
                        }
                    }
                }
                UInt32 xFirstCluster = (UInt32)(xData.ToUInt16(i + 20) << 16 | xData.ToUInt16(i + 26));

                var xTest = xAttrib & (DirectoryEntryAttributeConsts.Directory | DirectoryEntryAttributeConsts.VolumeID);
                if (xAttrib == DirectoryEntryAttributeConsts.LongName)
                {
                    // skip adding, as it's a LongFileName entry, meaning the next normal entry is the item with the name.
                    FatHelpers.Debug("Entry was an Long FileName entry. Current LongName = '" + xLongName + "'");
                }
                else if (xTest == 0)
                {
                    UInt32 xSize = xData.ToUInt32(i + 28);
                    if (xSize == 0 && xName.Length == 0)
                    {
                        continue;
                    }
                    xResult.Add(new FatFile(this, xName, xSize, xFirstCluster));
                    FatHelpers.Debug("Returning file '" + xName + "'");
                }
                else if (xTest == DirectoryEntryAttributeConsts.Directory)
                {
                    UInt32 xSize = xData.ToUInt32(i + 28);
                    var xFatDirectory = new FatDirectory(this, xName, xFirstCluster);
                    FatHelpers.Debug("Returning directory '" + xFatDirectory.Name + "', FirstCluster = " + xFirstCluster);
                    xResult.Add(xFatDirectory);
                }
                else if (xTest == DirectoryEntryAttributeConsts.VolumeID)
                {
                    FatHelpers.Debug("Directory entry is VolumeID");
                    //
                }
                else
                {
                    FatHelpers.Debug("Not sure what to do!");
                }
            }

            return xResult;
        }
Пример #2
0
        public List<Base> GetDirectoryContents(FatDirectory directory)
        {
            if (directory == null)
            {
                throw new ArgumentNullException("directory");
            }

            byte[] xData;
            if (FatType == FatTypeEnum.Fat32)
            {
                xData = NewClusterArray();
                ReadCluster(directory.FirstClusterNr, xData);
            }
            else
            {
                xData = mDevice.NewBlockArray(1);
                mDevice.ReadBlock(directory.FirstClusterNr, RootSectorCount, xData);
            }
            // todo: what about larger directories?


            return ReadDirectoryContents(xData);
        }