-
Notifications
You must be signed in to change notification settings - Fork 2
/
Header.cs
277 lines (235 loc) · 9.59 KB
/
Header.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
using System.IO;
using CompoundFileStorage.Exceptions;
/*
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in
compliance with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS"
basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
License for the specific language governing rights and limitations
under the License.
The Original Code is OpenMCDF - Compound Document Format library.
The Initial Developer of the Original Code is Federico Blaseotto.
The code is modified to more now a days standards and upgraded to
C# .NET 4.0 by Kees van Spelde
*/
namespace CompoundFileStorage
{
/// <summary>
/// This class contains the header of an OLE structured storage file
/// </summary>
internal class Header
{
#region Fields
/// <summary>
/// Structured Storage signature
/// </summary>
private readonly byte[] _oleCFSSignature = {0xD0, 0xCF, 0x11, 0xE0, 0xA1, 0xB1, 0x1A, 0xE1};
#endregion
#region Properties
/// <summary>
/// Compound document file identifier: D0H CFH 11H E0H A1H B1H 1AH E1H
/// </summary>
public byte[] HeaderSignature { get; private set; }
/// <summary>
/// Unique identifier (UID) of this file (not of interest in the following, may be all 0)
/// </summary>
public byte[] CLSID { get; set; }
/// <summary>
/// Revision number of the file format (most used is 003EH)
/// </summary>
public ushort MinorVersion { get; private set; }
/// <summary>
/// Version number of the file format (most used is 0003H)
/// </summary>
public ushort MajorVersion { get; private set; }
/// <summary>
/// Byte order identifier (➜4.2): FEH FFH = Little-Endian FFH FEH = Big-Endian
/// </summary>
public ushort ByteOrder { get; private set; }
/// <summary>
/// Size of a sector in the compound document file (➜3.1) in power-of-two (ssz), real sector
/// size is sec_size = 2ssz bytes (minimum value is 7 which means 128 bytes, most used
/// value is 9 which means 512 bytes)
/// </summary>
public ushort SectorShift { get; private set; }
/// <summary>
/// Size of a short-sector in the short-stream container stream (➜6.1) in power-of-two (sssz),
/// real short-sector size is short_sec_size = 2sssz bytes (maximum value is sector size
/// ssz, see above, most used value is 6 which means 64 bytes)
/// </summary>
public ushort MiniSectorShift { get; private set; }
/// <summary>
/// Not used
/// </summary>
public byte[] UnUsed { get; private set; }
/// <summary>
/// Total number of sectors used Directory (➜5.2)
/// </summary>
public int DirectorySectorsNumber { get; set; }
/// <summary>
/// Total number of sectors used for the sector allocation table (➜5.2)
/// </summary>
public int FATSectorsNumber { get; set; }
/// <summary>
/// SecID of first sector of the directory stream (➜7)
/// </summary>
public int FirstDirectorySectorId { get; set; }
/// <summary>
/// Not used
/// </summary>
public uint UnUsed2 { get; private set; }
/// <summary>
/// Minimum size of a standard stream (in bytes, minimum allowed and most used size is 4096
/// bytes), streams with an actual size smaller than (and not equal to) this value are stored as
/// short-streams (➜6)
/// </summary>
public uint MinSizeStandardStream { get; set; }
//60 4 SecID of first sector of the short-sector allocation table (➜6.2), or –2 (End Of Chain
//SecID, ➜3.1) if not extant
/// <summary>
/// This integer field contains the starting sector number for the mini FAT
/// </summary>
public int FirstMiniFATSectorId { get; set; }
/// <summary>
/// Total number of sectors used for the short-sector allocation table (➜6.2)
/// </summary>
public uint MiniFATSectorsNumber { get; set; }
/// <summary>
/// SecID of first sector of the master sector allocation table (➜5.1), or –2 (End Of Chain
/// SecID, ➜3.1) if no additional sectors used
/// </summary>
public int FirstDIFATSectorId { get; set; }
/// <summary>
/// Total number of sectors used for the master sector allocation table (➜5.1)
/// </summary>
public uint DIFATSectorsNumber { get; set; }
/// <summary>
/// First part of the master sector allocation table (➜5.1) containing 109 SecIDs
/// </summary>
public int[] DIFAT { get; private set; }
#endregion
#region Constructors
public Header() : this(3)
{
}
public Header(ushort version)
{
HeaderSignature = _oleCFSSignature;
DIFAT = new int[109];
UnUsed = new byte[6];
MiniSectorShift = 6;
SectorShift = 9;
ByteOrder = 0xFFFE;
MajorVersion = 0x0003;
MinorVersion = 0x003E;
CLSID = new byte[16];
FirstDirectorySectorId = Sector.Endofchain;
MinSizeStandardStream = 4096;
FirstMiniFATSectorId = unchecked((int) 0xFFFFFFFE);
FirstDIFATSectorId = Sector.Endofchain;
switch (version)
{
case 3:
MajorVersion = 3;
SectorShift = 0x0009;
break;
case 4:
MajorVersion = 4;
SectorShift = 0x000C;
break;
default:
throw new CFException("Invalid Compound File Format version");
}
for (var i = 0; i < 109; i++)
DIFAT[i] = Sector.FreeSector;
}
#endregion
#region Read
/// <summary>
/// Reads from the <see cref="stream" />
/// </summary>
/// <param name="stream"></param>
public void Read(Stream stream)
{
var streamRw = new StreamRW(stream);
HeaderSignature = streamRw.ReadBytes(8);
CheckSignature();
CLSID = streamRw.ReadBytes(16);
MinorVersion = streamRw.ReadUInt16();
MajorVersion = streamRw.ReadUInt16();
CheckVersion();
ByteOrder = streamRw.ReadUInt16();
SectorShift = streamRw.ReadUInt16();
MiniSectorShift = streamRw.ReadUInt16();
UnUsed = streamRw.ReadBytes(6);
DirectorySectorsNumber = streamRw.ReadInt32();
FATSectorsNumber = streamRw.ReadInt32();
FirstDirectorySectorId = streamRw.ReadInt32();
UnUsed2 = streamRw.ReadUInt32();
MinSizeStandardStream = streamRw.ReadUInt32();
FirstMiniFATSectorId = streamRw.ReadInt32();
MiniFATSectorsNumber = streamRw.ReadUInt32();
FirstDIFATSectorId = streamRw.ReadInt32();
DIFATSectorsNumber = streamRw.ReadUInt32();
for (var i = 0; i < 109; i++)
DIFAT[i] = streamRw.ReadInt32();
streamRw.Close();
}
#endregion
#region Write
/// <summary>
/// Writes to the <see cref="stream" />
/// </summary>
/// <param name="stream"></param>
public void Write(Stream stream)
{
var streamRw = new StreamRW(stream);
streamRw.Write(HeaderSignature);
streamRw.Write(CLSID);
streamRw.Write(MinorVersion);
streamRw.Write(MajorVersion);
streamRw.Write(ByteOrder);
streamRw.Write(SectorShift);
streamRw.Write(MiniSectorShift);
streamRw.Write(UnUsed);
streamRw.Write(DirectorySectorsNumber);
streamRw.Write(FATSectorsNumber);
streamRw.Write(FirstDirectorySectorId);
streamRw.Write(UnUsed2);
streamRw.Write(MinSizeStandardStream);
streamRw.Write(FirstMiniFATSectorId);
streamRw.Write(MiniFATSectorsNumber);
streamRw.Write(FirstDIFATSectorId);
streamRw.Write(DIFATSectorsNumber);
foreach (var i in DIFAT)
streamRw.Write(i);
if (MajorVersion == 4)
{
var zeroHead = new byte[3584];
streamRw.Write(zeroHead);
}
streamRw.Close();
}
#endregion
#region CheckVersion
private void CheckVersion()
{
if (MajorVersion != 3 && MajorVersion != 4)
throw new CFFileFormatException(
"Unsupported binary file format version there is only support for compound files with major version equal to 3 or 4");
}
#endregion
#region CheckSignature
private void CheckSignature()
{
for (var i = 0; i < HeaderSignature.Length; i++)
{
if (HeaderSignature[i] != _oleCFSSignature[i])
throw new CFFileFormatException("Invalid OLE structured storage file");
}
}
#endregion
}
}