Skip to content

Commit

Permalink
Share StreamRW for directory entries
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy-visionaid committed Oct 8, 2024
1 parent 9b5e572 commit e3a6d59
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 42 deletions.
15 changes: 9 additions & 6 deletions sources/OpenMcdf/CompoundFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1569,13 +1569,14 @@ List<Sector> 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);
}
}

Expand All @@ -1591,17 +1592,19 @@ List<Sector> 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;

while (delta % (SectorSize / DIRECTORY_SIZE) != 0)
{
IDirectoryEntry dummy = DirectoryEntry.New(string.Empty, StgType.StgInvalid, directoryEntries);
dummy.Write(sv);
dummy.Write(svRW);
delta++;
}

Expand Down
64 changes: 30 additions & 34 deletions sources/OpenMcdf/DirectoryEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand All @@ -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();
}
}

Expand Down
4 changes: 2 additions & 2 deletions sources/OpenMcdf/IDirectoryEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand All @@ -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();
}
}

0 comments on commit e3a6d59

Please sign in to comment.