diff --git a/sources/OpenMcdf/CompoundFile.cs b/sources/OpenMcdf/CompoundFile.cs index 72ad4f2..7f3f034 100644 --- a/sources/OpenMcdf/CompoundFile.cs +++ b/sources/OpenMcdf/CompoundFile.cs @@ -1569,13 +1569,14 @@ List directoryChain using StreamView dirReader = new StreamView(directoryChain, SectorSize, directoryChain.Count * SectorSize, null, sourceStream); + StreamRW dirReaderRW = new(dirReader); + while (dirReader.Position < directoryChain.Count * SectorSize) { - IDirectoryEntry de - = DirectoryEntry.New(string.Empty, StgType.StgInvalid, directoryEntries); + IDirectoryEntry de = DirectoryEntry.New(string.Empty, StgType.StgInvalid, directoryEntries); - //We are not inserting dirs. Do not use 'InsertNewDirectoryEntry' - de.Read(dirReader, Version); + // We are not inserting dirs. Do not use 'InsertNewDirectoryEntry' + de.Read(dirReaderRW, Version); } } @@ -1591,9 +1592,11 @@ List directorySectors using StreamView sv = new StreamView(directorySectors, SectorSize, 0, null, sourceStream); + StreamRW svRW = new(sv); + foreach (IDirectoryEntry di in directoryEntries) { - di.Write(sv); + di.Write(svRW); } int delta = directoryEntries.Count; @@ -1601,7 +1604,7 @@ List directorySectors while (delta % (SectorSize / DIRECTORY_SIZE) != 0) { IDirectoryEntry dummy = DirectoryEntry.New(string.Empty, StgType.StgInvalid, directoryEntries); - dummy.Write(sv); + dummy.Write(svRW); delta++; } diff --git a/sources/OpenMcdf/DirectoryEntry.cs b/sources/OpenMcdf/DirectoryEntry.cs index 01ba62e..5a4e1b3 100644 --- a/sources/OpenMcdf/DirectoryEntry.cs +++ b/sources/OpenMcdf/DirectoryEntry.cs @@ -210,23 +210,21 @@ public override int GetHashCode() return (int)fnv_hash(EntryName); } - public void Write(Stream stream) + public void Write(StreamRW streamRW) { - StreamRW rw = new StreamRW(stream); - - rw.Write(EntryName); - rw.Write(nameLength); - rw.Write((byte)StgType); - rw.Write((byte)StgColor); - rw.Write(LeftSibling); - rw.Write(RightSibling); - rw.Write(Child); - rw.Write(storageCLSID); - rw.Write(StateBits); - rw.Write(CreationDate); - rw.Write(ModifyDate); - rw.Write(StartSect); - rw.Write(Size); + streamRW.Write(EntryName); + streamRW.Write(nameLength); + streamRW.Write((byte)StgType); + streamRW.Write((byte)StgColor); + streamRW.Write(LeftSibling); + streamRW.Write(RightSibling); + streamRW.Write(Child); + streamRW.Write(storageCLSID); + streamRW.Write(StateBits); + streamRW.Write(CreationDate); + streamRW.Write(ModifyDate); + streamRW.Write(StartSect); + streamRW.Write(Size); } //public Byte[] ToByteArray() @@ -256,18 +254,16 @@ public void Write(Stream stream) // return ms.ToArray(); //} - public void Read(Stream stream, CFSVersion ver = CFSVersion.Ver_3) + public void Read(StreamRW streamRW, CFSVersion ver = CFSVersion.Ver_3) { - StreamRW rw = new StreamRW(stream); - - rw.ReadBytes(EntryName); - nameLength = rw.ReadUInt16(); - StgType = (StgType)rw.ReadByte(); + streamRW.ReadBytes(EntryName); + nameLength = streamRW.ReadUInt16(); + StgType = (StgType)streamRW.ReadByte(); //rw.ReadByte();//Ignore color, only black tree - StgColor = (StgColor)rw.ReadByte(); - LeftSibling = rw.ReadInt32(); - RightSibling = rw.ReadInt32(); - Child = rw.ReadInt32(); + StgColor = (StgColor)streamRW.ReadByte(); + LeftSibling = streamRW.ReadInt32(); + RightSibling = streamRW.ReadInt32(); + Child = streamRW.ReadInt32(); // Thanks to bugaccount (BugTrack id 3519554) if (StgType == StgType.StgInvalid) @@ -277,23 +273,23 @@ public void Read(Stream stream, CFSVersion ver = CFSVersion.Ver_3) Child = NOSTREAM; } - storageCLSID = rw.ReadGuid(); - StateBits = rw.ReadInt32(); - rw.ReadBytes(CreationDate); - rw.ReadBytes(ModifyDate); - StartSect = rw.ReadInt32(); + storageCLSID = streamRW.ReadGuid(); + StateBits = streamRW.ReadInt32(); + streamRW.ReadBytes(CreationDate); + streamRW.ReadBytes(ModifyDate); + StartSect = streamRW.ReadInt32(); if (ver == CFSVersion.Ver_3) { // avoid dirty read for version 3 files (max size: 32bit integer) // where most significant bits are not initialized to zero - Size = rw.ReadInt32(); - rw.Seek(4, SeekOrigin.Current); // discard most significant 4 (possibly) dirty bytes + Size = streamRW.ReadInt32(); + streamRW.Seek(4, SeekOrigin.Current); // discard most significant 4 (possibly) dirty bytes } else { - Size = rw.ReadInt64(); + Size = streamRW.ReadInt64(); } } diff --git a/sources/OpenMcdf/IDirectoryEntry.cs b/sources/OpenMcdf/IDirectoryEntry.cs index 9965487..3d843fc 100644 --- a/sources/OpenMcdf/IDirectoryEntry.cs +++ b/sources/OpenMcdf/IDirectoryEntry.cs @@ -21,7 +21,7 @@ internal interface IDirectoryEntry : IComparable, IRBNode byte[] ModifyDate { get; set; } string Name { get; } ushort NameLength { get; set; } - void Read(System.IO.Stream stream, CFSVersion ver = CFSVersion.Ver_3); + void Read(StreamRW streamRW, CFSVersion ver = CFSVersion.Ver_3); int RightSibling { get; set; } void SetEntryName(string entryName); int SID { get; set; } @@ -31,7 +31,7 @@ internal interface IDirectoryEntry : IComparable, IRBNode StgColor StgColor { get; set; } StgType StgType { get; set; } Guid StorageCLSID { get; set; } - void Write(System.IO.Stream stream); + void Write(StreamRW stream); void Reset(); } }