-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDirectoryEntry.cs
416 lines (347 loc) · 11.2 KB
/
DirectoryEntry.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Globalization;
using BinaryTrees;
/*
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
{
public enum StgType : int
{
StgInvalid = 0,
StgStorage = 1,
StgStream = 2,
StgLockbytes = 3,
StgProperty = 4,
StgRoot = 5
}
public enum StgColor : int
{
Red = 0,
Black = 1
}
internal class DirectoryEntry : IComparable, IDirectoryEntry
{
private int sid = -1;
public int SID
{
get { return sid; }
set { sid = value; }
}
internal static Int32 NOSTREAM
= unchecked((int)0xFFFFFFFF);
public DirectoryEntry(StgType stgType)
{
this.stgType = stgType;
switch (stgType)
{
case StgType.StgStream:
this.storageCLSID = new Guid("00000000000000000000000000000000");
this.creationDate = new byte[8] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
this.modifyDate = new byte[8] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
break;
case StgType.StgStorage:
this.creationDate = BitConverter.GetBytes((DateTime.Now.ToFileTime()));
break;
case StgType.StgRoot:
this.creationDate = new byte[8] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
this.modifyDate = new byte[8] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
break;
}
}
private byte[] entryName = new byte[64];
public byte[] EntryName
{
get
{
return entryName;
}
//set
//{
// entryName = value;
//}
}
public String GetEntryName()
{
if (entryName != null && entryName.Length > 0)
{
return Encoding.Unicode.GetString(entryName, 0, entryName.Length).Remove((this.nameLength - 1) / 2);
}
else
return String.Empty;
}
public void SetEntryName(String entryName)
{
if (
entryName.Contains(@"\") ||
entryName.Contains(@"/") ||
entryName.Contains(@":") ||
entryName.Contains(@"!")
)
throw new CFException("Invalid character in entry: the characters '\\', '/', ':','!' cannot be used in entry name");
if (entryName.Length > 31)
throw new CFException("Entry name MUST be smaller than 31 characters");
byte[] newName = null;
byte[] temp = Encoding.Unicode.GetBytes(entryName);
newName = new byte[64];
Buffer.BlockCopy(temp, 0, newName, 0, temp.Length);
newName[temp.Length] = 0x00;
newName[temp.Length + 1] = 0x00;
this.entryName = newName;
this.nameLength = (ushort)(temp.Length + 2);
}
private ushort nameLength;
public ushort NameLength
{
get
{
return nameLength;
}
set
{
throw new NotImplementedException();
}
}
private StgType stgType = StgType.StgInvalid;
public StgType StgType
{
get
{
return stgType;
}
set
{
stgType = value;
}
}
private StgColor stgColor = StgColor.Black;
public StgColor StgColor
{
get
{
return stgColor;
}
set
{
stgColor = value;
}
}
private Int32 leftSibling = NOSTREAM;
public Int32 LeftSibling
{
get { return leftSibling; }
set { leftSibling = value; }
}
private Int32 rightSibling = NOSTREAM;
public Int32 RightSibling
{
get { return rightSibling; }
set { rightSibling = value; }
}
private Int32 child = NOSTREAM;
public Int32 Child
{
get { return child; }
set { child = value; }
}
private Guid storageCLSID
= Guid.NewGuid();
public Guid StorageCLSID
{
get
{
return storageCLSID;
}
set
{
this.storageCLSID = value;
}
}
private Int32 stateBits;
public Int32 StateBits
{
get { return stateBits; }
set { stateBits = value; }
}
private byte[] creationDate = new byte[8] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
public byte[] CreationDate
{
get
{
return creationDate;
}
set
{
creationDate = value;
}
}
private byte[] modifyDate = new byte[8] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
public byte[] ModifyDate
{
get
{
return modifyDate;
}
set
{
modifyDate = value;
}
}
private Int32 startSetc = Sector.ENDOFCHAIN;
public Int32 StartSetc
{
get
{
return startSetc;
}
set
{
startSetc = value;
}
}
private long size;
public long Size
{
get
{
return size;
}
set
{
size = value;
}
}
public int CompareTo(object obj)
{
const int THIS_IS_GREATER = 1;
const int OTHER_IS_GREATER = -1;
IDirectoryEntry otherDir = obj as IDirectoryEntry;
if (otherDir == null)
throw new CFException("Invalid casting: compared object does not implement IDirectorEntry interface");
if (this.NameLength > otherDir.NameLength)
{
return THIS_IS_GREATER;
}
else if (this.NameLength < otherDir.NameLength)
{
return OTHER_IS_GREATER;
}
else
{
String thisName = Encoding.Unicode.GetString(this.EntryName, 0, this.NameLength).ToUpper(CultureInfo.InvariantCulture);
String otherName = Encoding.Unicode.GetString(otherDir.EntryName, 0, otherDir.NameLength).ToUpper(CultureInfo.InvariantCulture);
for (int z = 0; z < thisName.Length; z++)
{
if (BitConverter.ToInt16(BitConverter.GetBytes(thisName[z]), 0) > BitConverter.ToInt16(BitConverter.GetBytes(otherName[z]), 0))
return THIS_IS_GREATER;
else if (BitConverter.ToInt16(BitConverter.GetBytes(thisName[z]), 0) < BitConverter.ToInt16(BitConverter.GetBytes(otherName[z]), 0))
return OTHER_IS_GREATER;
}
return 0;
}
// return String.Compare(Encoding.Unicode.GetString(this.EntryName).ToUpper(), Encoding.Unicode.GetString(other.EntryName).ToUpper());
}
public override bool Equals(object obj)
{
return this.CompareTo(obj) == 0;
}
/// <summary>
/// FNV hash, short for Fowler/Noll/Vo
/// </summary>
/// <param name="buffer"></param>
/// <returns>(not warranted) unique hash for byte array</returns>
private static ulong fnv_hash(byte[] buffer)
{
ulong h = 2166136261;
int i;
for (i = 0; i < buffer.Length; i++)
h = (h * 16777619) ^ buffer[i];
return h;
}
public override int GetHashCode()
{
return (int)fnv_hash(this.entryName);
}
public void Write(Stream stream)
{
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.ToByteArray());
rw.Write(stateBits);
rw.Write(creationDate);
rw.Write(modifyDate);
rw.Write(startSetc);
rw.Write(size);
rw.Close();
}
//public Byte[] ToByteArray()
//{
// MemoryStream ms
// = new MemoryStream(128);
// BinaryWriter bw = new BinaryWriter(ms);
// byte[] paddedName = new byte[64];
// Array.Copy(entryName, paddedName, entryName.Length);
// bw.Write(paddedName);
// bw.Write(nameLength);
// bw.Write((byte)stgType);
// bw.Write((byte)stgColor);
// bw.Write(leftSibling);
// bw.Write(rightSibling);
// bw.Write(child);
// bw.Write(storageCLSID.ToByteArray());
// bw.Write(stateBits);
// bw.Write(creationDate);
// bw.Write(modifyDate);
// bw.Write(startSetc);
// bw.Write(size);
// return ms.ToArray();
//}
public void Read(Stream stream)
{
StreamRW rw = new StreamRW(stream);
entryName = rw.ReadBytes(64);
nameLength = rw.ReadUInt16();
stgType = (StgType)rw.ReadByte();
rw.ReadByte();//Ignore color, only black tree
//stgColor = (StgColor)br.ReadByte();
leftSibling = rw.ReadInt32();
rightSibling = rw.ReadInt32();
child = rw.ReadInt32();
// Thank you to bugaccount (BugTrack id 3519554)
if (stgType == StgType.StgInvalid)
{
leftSibling = NOSTREAM;
rightSibling = NOSTREAM;
child = NOSTREAM;
}
storageCLSID = new Guid(rw.ReadBytes(16));
stateBits = rw.ReadInt32();
creationDate = rw.ReadBytes(8);
modifyDate = rw.ReadBytes(8);
startSetc = rw.ReadInt32();
size = rw.ReadInt64();
}
public string Name
{
get { return GetEntryName(); }
}
}
}