internal static CFDirectoryEntry ReadDirectoryEntry(Stream s, long streamOffset, bool isVersion3) { byte[] data = ReadFragment(s, streamOffset, DirectoryEntrySize); CFDirectoryEntry entry = new CFDirectoryEntry(); entry.NameLength = BitConverter.ToUInt16(data, 64); int nameLength = Math.Min(32, (int)entry.NameLength / 2); entry.Name = new char[nameLength]; for (int i = 0; i < nameLength; i++) { entry.Name[i] = (char)BitConverter.ToChar(data, 2 * i); } entry.ObjectType = data[66]; entry.ColorFlag = data[67]; entry.LeftSiblingID = BitConverter.ToUInt32(data, 68); entry.RightSiblingID = BitConverter.ToUInt32(data, 72); entry.ChildID = BitConverter.ToUInt32(data, 76); entry.CLSID = new Guid(GetByteArrayPortion(data, 80, 16)); ; entry.StateBits = BitConverter.ToUInt32(data, 96); entry.CreationTime = BitConverter.ToUInt64(data, 100); entry.ModifiedTime = BitConverter.ToUInt64(data, 108); entry.StartingSectorLocation = BitConverter.ToUInt32(data, 116); entry.StreamSize = BitConverter.ToUInt64(data, 120); if (isVersion3) entry.StreamSize &= 0xFFFFFFFF; return entry; }
private void Initialize() { entry = ReaderUtils.ReadDirectoryEntry(System.BaseStream, GetStreamOffset(), System.IsVersion3); ReaderUtils.ValidateDirectoryEntry(entry); }
internal static void ValidateDirectoryEntry(CFDirectoryEntry entry) { if(entry.Name.Length * 2 != entry.NameLength || entry.NameLength == 0) throw new CompoundFileException("Invalid directory entry: name length"); if(entry.Name[entry.Name.Length - 1] != 0) throw new CompoundFileException("Invalid directory entry: name null termination"); foreach (char ch in entry.Name) { if(ch == '/' || ch == '\\' || ch == ':' || ch == '!') throw new CompoundFileException("Invalid directory entry: illegal char in name"); } if (entry.ObjectType != DirectoryObjectTypes.Unknown && entry.ObjectType != DirectoryObjectTypes.Storage && entry.ObjectType != DirectoryObjectTypes.Stream && entry.ObjectType != DirectoryObjectTypes.RootStorage) throw new CompoundFileException("Invalid directory entry: object type"); if(entry.ColorFlag != TreeColors.Red && entry.ColorFlag != TreeColors.Black) throw new CompoundFileException("Invalid directory entry: color flag"); if(entry.LeftSiblingID > DirectoryStreamIds.MAXREGSID && entry.LeftSiblingID != DirectoryStreamIds.NOSTREAM) throw new CompoundFileException("Invalid directory entry: left sibling"); if (entry.RightSiblingID > DirectoryStreamIds.MAXREGSID && entry.RightSiblingID != DirectoryStreamIds.NOSTREAM) throw new CompoundFileException("Invalid directory entry: right sibling"); if (entry.ChildID > DirectoryStreamIds.MAXREGSID && entry.ChildID != DirectoryStreamIds.NOSTREAM) throw new CompoundFileException("Invalid directory entry: child"); }