forked from ironfede/openmcdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFatSectorEnumerator.cs
153 lines (128 loc) · 4.33 KB
/
FatSectorEnumerator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
using System.Collections;
namespace OpenMcdf;
/// <summary>
/// Enumerates the <see cref="Sector"/>s of a compound file.
/// </summary>
internal sealed class FatSectorEnumerator : ContextBase, IEnumerator<Sector>
{
private readonly DifatSectorEnumerator difatSectorEnumerator;
private bool start = true;
private uint index = uint.MaxValue;
private Sector current = Sector.EndOfChain;
public FatSectorEnumerator(RootContextSite rootContextSite)
: base(rootContextSite)
{
difatSectorEnumerator = new(rootContextSite);
}
/// <inheritdoc/>
public void Dispose()
{
// Context is owned by a parent
difatSectorEnumerator.Dispose();
}
/// <inheritdoc/>
public Sector Current
{
get
{
if (current.Id == SectorType.EndOfChain)
throw new InvalidOperationException("Enumeration has not started. Call MoveNext.");
return current;
}
}
/// <inheritdoc/>
object IEnumerator.Current => Current;
/// <inheritdoc/>
public bool MoveNext()
{
if (start)
{
start = false;
index = uint.MaxValue;
}
uint nextIndex = index + 1;
return MoveTo(nextIndex);
}
/// <summary>
/// Moves the enumerator to the specified sector.
/// </summary>
public bool MoveTo(uint index)
{
ThrowHelper.ThrowIfSectorIdIsInvalid(index);
start = false;
if (index == this.index)
return true;
if (index >= Context.Header.FatSectorCount)
{
this.index = uint.MaxValue;
current = Sector.EndOfChain;
return false;
}
if (index < Header.DifatArrayLength)
{
this.index = index;
uint difatId = Context.Header.Difat[this.index];
current = new(difatId, Context.SectorSize);
return true;
}
if (index < this.index)
{
this.index = Header.DifatArrayLength;
}
uint difatChainIndex = GetDifatChainIndexAndDifatEntryIndex(index, out long difatElementIndex);
if (!difatSectorEnumerator.MoveTo(difatChainIndex))
{
this.index = uint.MaxValue;
current = Sector.EndOfChain;
return false;
}
Sector difatSector = difatSectorEnumerator.Current;
Context.Reader.Position = difatSector.Position + (difatElementIndex * sizeof(uint));
uint id = Context.Reader.ReadUInt32();
this.index = index;
current = new Sector(id, Context.SectorSize);
return true;
}
private uint GetDifatChainIndexAndDifatEntryIndex(uint index, out long difatElementIndex)
=> (uint)Math.DivRem(index - Header.DifatArrayLength, Context.DifatEntriesPerSector, out difatElementIndex);
/// <inheritdoc/>
public void Reset()
{
start = true;
index = uint.MaxValue;
current = Sector.EndOfChain;
difatSectorEnumerator.Reset();
}
/// <summary>
/// Extends the FAT by adding a new sector.
/// </summary>
/// <returns>The ID of the new sector that was added</returns>
public uint Add()
{
Header header = Context.Header;
uint nextIndex = Context.Header.FatSectorCount;
Sector newFatSector = new(Context.SectorCount, Context.SectorSize);
CfbBinaryWriter writer = Context.Writer;
writer.Position = newFatSector.Position;
writer.Write(SectorDataCache.GetFatEntryData(newFatSector.Length));
Context.ExtendStreamLength(newFatSector.EndPosition);
header.FatSectorCount++;
index = nextIndex;
current = newFatSector;
if (nextIndex < Header.DifatArrayLength)
{
header.Difat[nextIndex] = newFatSector.Id;
}
else
{
uint difatSectorIndex = GetDifatChainIndexAndDifatEntryIndex(nextIndex, out long difatElementIndex);
if (!difatSectorEnumerator.MoveTo(difatSectorIndex))
difatSectorEnumerator.Add();
Sector difatSector = difatSectorEnumerator.Current;
writer.Position = difatSector.Position + difatElementIndex * sizeof(uint);
writer.Write(newFatSector.Id);
}
Context.Fat[newFatSector.Id] = SectorType.Fat;
return newFatSector.Id;
}
}