-
Notifications
You must be signed in to change notification settings - Fork 0
/
DocumentHeader.cs
195 lines (175 loc) · 8.27 KB
/
DocumentHeader.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
using System;
using System.IO;
namespace Storage.CompoundDocument
{
/// <summary>
///
/// </summary>
public sealed class DocumentHeader
{
/// <summary>
/// Gets or sets the revision number.
/// </summary>
/// <value>The revision number.</value>
public int RevisionNumber { get; private set; } // 2
/// <summary>
/// Gets or sets the version number.
/// </summary>
/// <value>The version number.</value>
public int VersionNumber { get; private set; }
/// <summary>
/// Gets or sets the byte order identifier.
/// </summary>
/// <value>The byte order identifier.</value>
public ByteOrderIdentifiers ByteOrderIdentifier { get; private set; } // 4
/// <summary>
/// Gets or sets the size of sector.
/// </summary>
/// <value>The size of sector.</value>
public int SizeOfSector { get; private set; }
/// <summary>
/// Gets or sets the size of sector in short stream.
/// </summary>
/// <value>The size of sector in short stream.</value>
public int SizeOfSectorInShortStream { get; private set; } // 6
/// <summary>
/// Gets or sets the total number of sectors used for sector allocation table.
/// </summary>
/// <value>The total number of sectors used for sector allocation table.</value>
public int TotalNumberOfSectorsUsedForSectorAllocationTable { get; private set; } // 8
/// <summary>
/// Gets or sets the special value of first sector.
/// </summary>
/// <value>The special value of first sector.</value>
public SpecialSecIds SpecialValueOfFirstSector { get; private set; }
/// <summary>
/// Gets or sets the sec ID of first sector of directory.
/// </summary>
/// <value>The sec ID of first sector of directory.</value>
public int SecIDOfFirstSectorOfDirectory { get; private set; }
/// <summary>
/// Gets or sets the minimum size of standard stream.
/// </summary>
/// <value>The minimum size of standard stream.</value>
public int MinimumSizeOfStandardStream { get; private set; }
/// <summary>
/// Gets or sets the special value of first sector of the short sector.
/// </summary>
/// <value>The special value of first sector of the short sector.</value>
public SpecialSecIds SpecialValueOfFirstSectorOfTheShortSector { get; private set; } // 12
/// <summary>
/// Gets or sets the sec ID of first sector of the short sector.
/// </summary>
/// <value>The sec ID of first sector of the short sector.</value>
public int SecIDOfFirstSectorOfTheShortSector { get; private set; } // 12
/// <summary>
/// Gets or sets the total number of sectors used for sector allocation table of the short sector.
/// </summary>
/// <value>
/// The total number of sectors used for sector allocation table of the short sector.
/// </value>
public int TotalNumberOfSectorsUsedForSectorAllocationTableOfTheShortSector { get; private set; }
/// <summary>
/// Gets or sets the special value of first sector of the master sector allocation table.
/// </summary>
/// <value>
/// The special value of first sector of the master sector allocation table.
/// </value>
public SpecialSecIds SpecialValueOfFirstSectorOfTheMasterSectorAllocationTable { get; private set; } // 14
/// <summary>
/// Gets or sets the sec ID of first sector of the master sector allocation table.
/// </summary>
/// <value>
/// The sec ID of first sector of the master sector allocation table.
/// </value>
public int SecIDOfFirstSectorOfTheMasterSectorAllocationTable { get; private set; } // 14
/// <summary>
/// Gets or sets the total number of sectors used for sector allocation table for the master sector allocation table.
/// </summary>
/// <value>
/// The total number of sectors used for sector allocation table for the master sector allocation table.
/// </value>
public int TotalNumberOfSectorsUsedForSectorAllocationTableForTheMasterSectorAllocationTable { get; private set; }
/// <summary>
/// Initializes a new instance of the <see cref="DocumentHeader"/> class.
/// </summary>
/// <param name="inStream">The in stream.</param>
public DocumentHeader(Stream inStream)
{
byte[] tempBytes;
int tempInt;
var compoundDocumentFileIdentifier = inStream.ReadStreamPart(8); // 0
if (SetIsCompoundDocument(compoundDocumentFileIdentifier) == false)
return;
inStream.ReadStreamPart(16); // unique identifier, not used
RevisionNumber = inStream.ReadStreamPart(2).ConvertToInt();
VersionNumber = inStream.ReadStreamPart(2).ConvertToInt();
tempBytes = inStream.ReadStreamPart(2); // 4
if (tempBytes[0] == 0xFE && tempBytes[1] == 0xFF)
ByteOrderIdentifier = ByteOrderIdentifiers.LittleEndian;
else if (tempBytes[0] == 0xFF && tempBytes[1] == 0xFE)
ByteOrderIdentifier = ByteOrderIdentifiers.BigEndian;
SizeOfSector = (int)Math.Pow(2, inStream.ReadStreamPart(2).ConvertToInt());
SizeOfSectorInShortStream = (int)Math.Pow(2, inStream.ReadStreamPart(2).ConvertToInt());
inStream.ReadStreamPart(10); // not used
TotalNumberOfSectorsUsedForSectorAllocationTable = inStream.ReadStreamPart(4).ConvertToInt();
tempBytes = inStream.ReadStreamPart(4);
tempInt = tempBytes.ConvertToInt();
if (tempInt <= 0)
SpecialValueOfFirstSector = (SpecialSecIds)tempInt;
else
SecIDOfFirstSectorOfDirectory = tempInt;
inStream.ReadStreamPart(4); // not used
MinimumSizeOfStandardStream = inStream.ReadStreamPart(4).ConvertToInt();
tempBytes = inStream.ReadStreamPart(4); // 12
tempInt = tempBytes.ConvertToInt();
if (tempInt <= 0)
SpecialValueOfFirstSectorOfTheShortSector = (SpecialSecIds)tempInt;
else
SecIDOfFirstSectorOfTheShortSector = tempInt;
TotalNumberOfSectorsUsedForSectorAllocationTableOfTheShortSector = inStream.ReadStreamPart(4).ConvertToInt();
tempBytes = inStream.ReadStreamPart(4); // 14
tempInt = tempBytes.ConvertToInt();
if (tempInt <= 0)
SpecialValueOfFirstSectorOfTheMasterSectorAllocationTable = (SpecialSecIds)tempInt;
else
SecIDOfFirstSectorOfTheMasterSectorAllocationTable = tempInt;
TotalNumberOfSectorsUsedForSectorAllocationTableForTheMasterSectorAllocationTable = inStream.ReadStreamPart(4).ConvertToInt();
}
/// <summary>
///
/// </summary>
private bool? _isCompoundHeader;
/// <summary>
/// Determines whether [is compound document].
/// </summary>
/// <returns>
/// <c>true</c> if [is compound document]; otherwise, <c>false</c>.
/// </returns>
public bool IsCompoundDocument()
{
if (_isCompoundHeader.HasValue)
return _isCompoundHeader.Value;
return false;
}
/// <summary>
/// Sets the is compound document.
/// </summary>
/// <param name="compoundDocumentFileIdentifier">The compound document file identifier.</param>
/// <returns></returns>
private bool SetIsCompoundDocument(byte[] compoundDocumentFileIdentifier )
{
for (int i = 0; i < compoundDocumentFileIdentifier.Length; i++)
{
var headerByte = compoundDocumentFileIdentifier[i];
if (headerByte != compoundDocumentFileIdentifier[i])
{
_isCompoundHeader = false;
}
}
if (_isCompoundHeader.HasValue == false)
_isCompoundHeader = true;
return _isCompoundHeader.Value;
}
}
}