-
Notifications
You must be signed in to change notification settings - Fork 0
/
Header.cs
317 lines (251 loc) · 9.51 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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
/*
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.
*/
namespace OpenMcdf
{
internal class Header
{
//0 8 Compound document file identifier: D0H CFH 11H E0H A1H B1H 1AH E1H
private byte[] headerSignature
= new byte[] { 0xD0, 0xCF, 0x11, 0xE0, 0xA1, 0xB1, 0x1A, 0xE1 };
public byte[] HeaderSignature
{
get { return headerSignature; }
}
//8 16 Unique identifier (UID) of this file (not of interest in the following, may be all 0)
private byte[] clsid = new byte[16];
public byte[] CLSID
{
get { return clsid; }
set { clsid = value; }
}
//24 2 Revision number of the file format (most used is 003EH)
private ushort minorVersion = 0x003E;
public ushort MinorVersion
{
get { return minorVersion; }
}
//26 2 Version number of the file format (most used is 0003H)
private ushort majorVersion = 0x0003;
public ushort MajorVersion
{
get { return majorVersion; }
}
//28 2 Byte order identifier (➜4.2): FEH FFH = Little-Endian FFH FEH = Big-Endian
private ushort byteOrder = 0xFFFE;
public ushort ByteOrder
{
get { return byteOrder; }
}
//30 2 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)
private ushort sectorShift = 9;
public ushort SectorShift
{
get { return sectorShift; }
}
//32 2 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)
private ushort miniSectorShift = 6;
public ushort MiniSectorShift
{
get { return miniSectorShift; }
}
//34 10 Not used
private byte[] unUsed = new byte[6];
public byte[] UnUsed
{
get { return unUsed; }
}
//44 4 Total number of sectors used Directory (➜5.2)
private int directorySectorsNumber;
public int DirectorySectorsNumber
{
get { return directorySectorsNumber; }
set { directorySectorsNumber = value; }
}
//44 4 Total number of sectors used for the sector allocation table (➜5.2)
private int fatSectorsNumber;
public int FATSectorsNumber
{
get { return fatSectorsNumber; }
set { fatSectorsNumber = value; }
}
//48 4 SecID of first sector of the directory stream (➜7)
private int firstDirectorySectorID = Sector.ENDOFCHAIN;
public int FirstDirectorySectorID
{
get { return firstDirectorySectorID; }
set { firstDirectorySectorID = value; }
}
//52 4 Not used
private uint unUsed2;
public uint UnUsed2
{
get { return unUsed2; }
}
//56 4 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)
private uint minSizeStandardStream = 4096;
public uint MinSizeStandardStream
{
get { return minSizeStandardStream; }
set { minSizeStandardStream = value; }
}
//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
private int firstMiniFATSectorID = unchecked((int)0xFFFFFFFE);
/// <summary>
/// This integer field contains the starting sector number for the mini FAT
/// </summary>
public int FirstMiniFATSectorID
{
get { return firstMiniFATSectorID; }
set { firstMiniFATSectorID = value; }
}
//64 4 Total number of sectors used for the short-sector allocation table (➜6.2)
private uint miniFATSectorsNumber;
public uint MiniFATSectorsNumber
{
get { return miniFATSectorsNumber; }
set { miniFATSectorsNumber = value; }
}
//68 4 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
private int firstDIFATSectorID = Sector.ENDOFCHAIN;
public int FirstDIFATSectorID
{
get { return firstDIFATSectorID; }
set { firstDIFATSectorID = value; }
}
//72 4 Total number of sectors used for the master sector allocation table (➜5.1)
private uint difatSectorsNumber;
public uint DIFATSectorsNumber
{
get { return difatSectorsNumber; }
set { difatSectorsNumber = value; }
}
//76 436 First part of the master sector allocation table (➜5.1) containing 109 SecIDs
private int[] difat = new int[109];
public int[] DIFAT
{
get { return difat; }
}
public Header()
: this(3)
{
}
public Header(ushort version)
{
switch (version)
{
case 3:
this.majorVersion = 3;
this.sectorShift = 0x0009;
break;
case 4:
this.majorVersion = 4;
this.sectorShift = 0x000C;
break;
default:
throw new CFException("Invalid Compound File Format version");
}
for (int i = 0; i < 109; i++)
{
difat[i] = Sector.FREESECT;
}
}
public void Write(Stream stream)
{
StreamRW rw = new StreamRW(stream);
rw.Write(headerSignature);
rw.Write(clsid);
rw.Write(minorVersion);
rw.Write(majorVersion);
rw.Write(byteOrder);
rw.Write(sectorShift);
rw.Write(miniSectorShift);
rw.Write(unUsed);
rw.Write(directorySectorsNumber);
rw.Write(fatSectorsNumber);
rw.Write(firstDirectorySectorID);
rw.Write(unUsed2);
rw.Write(minSizeStandardStream);
rw.Write(firstMiniFATSectorID);
rw.Write(miniFATSectorsNumber);
rw.Write(firstDIFATSectorID);
rw.Write(difatSectorsNumber);
foreach (int i in difat)
{
rw.Write(i);
}
if (majorVersion == 4)
{
byte[] zeroHead = new byte[3584];
rw.Write(zeroHead);
}
rw.Close();
}
public void Read(Stream stream)
{
StreamRW rw = new StreamRW(stream);
headerSignature = rw.ReadBytes(8);
CheckSignature();
clsid = rw.ReadBytes(16);
minorVersion = rw.ReadUInt16();
majorVersion = rw.ReadUInt16();
CheckVersion();
byteOrder = rw.ReadUInt16();
sectorShift = rw.ReadUInt16();
miniSectorShift = rw.ReadUInt16();
unUsed = rw.ReadBytes(6);
directorySectorsNumber = rw.ReadInt32();
fatSectorsNumber = rw.ReadInt32();
firstDirectorySectorID = rw.ReadInt32();
unUsed2 = rw.ReadUInt32();
minSizeStandardStream = rw.ReadUInt32();
firstMiniFATSectorID = rw.ReadInt32();
miniFATSectorsNumber = rw.ReadUInt32();
firstDIFATSectorID = rw.ReadInt32();
difatSectorsNumber = rw.ReadUInt32();
for (int i = 0; i < 109; i++)
{
this.DIFAT[i] = rw.ReadInt32();
}
rw.Close();
}
private void CheckVersion()
{
if (this.majorVersion != 3 && this.majorVersion != 4)
throw new CFFileFormatException("Unsupported Binary File Format version: OpenMcdf only supports Compound Files with major version equal to 3 or 4 ");
}
/// <summary>
/// Structured Storage signature
/// </summary>
private byte[] OLE_CFS_SIGNATURE = new byte[] { 0xD0, 0xCF, 0x11, 0xE0, 0xA1, 0xB1, 0x1A, 0xE1 };
private void CheckSignature()
{
for (int i = 0; i < headerSignature.Length; i++)
{
if (headerSignature[i] != OLE_CFS_SIGNATURE[i])
throw new CFFileFormatException("Invalid OLE structured storage file");
}
}
}
}