-
-
Notifications
You must be signed in to change notification settings - Fork 166
/
Storage.cs
622 lines (525 loc) · 24.6 KB
/
Storage.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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using System.Text;
using STATSTG = System.Runtime.InteropServices.ComTypes.STATSTG;
/*
Copyright 2013-2015 Kees van Spelde
Licensed under The Code Project Open License (CPOL) 1.02;
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.codeproject.com/info/cpol10.aspx
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
namespace MsgReader.Outlook
{
/// <summary>
/// The base class for reading an Outlook MSG file
/// </summary>
public partial class Storage : IDisposable
{
#region Fields
/// <summary>
/// The statistics for all streams in the IStorage associated with this instance
/// </summary>
private readonly Dictionary<string, STATSTG> _streamStatistics = new Dictionary<string, STATSTG>();
/// <summary>
/// The statistics for all storgages in the IStorage associated with this instance
/// </summary>
private readonly Dictionary<string, STATSTG> _subStorageStatistics = new Dictionary<string, STATSTG>();
/// <summary>
/// Header size of the property stream in the IStorage associated with this instance
/// </summary>
private int _propHeaderSize = MapiTags.PropertiesStreamHeaderTop;
/// <summary>
/// A reference to the parent message that this message may belong to
/// </summary>
private Storage _parentMessage;
/// <summary>
/// The IStorage associated with this instance.
/// </summary>
private NativeMethods.IStorage _storage;
/// <summary>
/// Will contain all the named MAPI properties when the class that inherits the <see cref="Storage"/> class
/// is a <see cref="Storage.Message"/> class. Otherwhise the List will be null
/// mapped to
/// </summary>
private List<MapiTagMapping> _namedProperties;
#endregion
#region Properties
/// <summary>
/// Gets the top level Outlook message from a sub message at any level.
/// </summary>
/// <value> The top level outlook message. </value>
private Storage TopParent
{
get { return _parentMessage != null ? _parentMessage.TopParent : this; }
}
/// <summary>
/// Gets a value indicating whether this instance is the top level Outlook message.
/// </summary>
/// <value> <c>true</c> if this instance is the top level outlook message; otherwise, <c>false</c> . </value>
private bool IsTopParent
{
get { return _parentMessage == null; }
}
/// <summary>
/// The way the storage is opened
/// </summary>
public FileAccess FileAccess { get; private set; }
#endregion
#region Constructors & Destructor
private Storage()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="Storage" /> class from a file.
/// </summary>
/// <param name="storageFilePath"> The file to load. </param>
/// <param name="fileAccess">FileAcces mode, default is Read</param>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
private Storage(string storageFilePath, FileAccess fileAccess = FileAccess.Read)
{
// Ensure provided file is an IStorage
if (NativeMethods.StgIsStorageFile(storageFilePath) != 0)
// ReSharper disable once LocalizableElement
throw new ArgumentException("The provided file is not a valid IStorage", "storageFilePath");
var accesMode = NativeMethods.STGM.READWRITE;
FileAccess = fileAccess;
switch (fileAccess)
{
case FileAccess.Read:
accesMode = NativeMethods.STGM.READ;
break;
case FileAccess.Write:
case FileAccess.ReadWrite:
accesMode = NativeMethods.STGM.READWRITE;
break;
}
// Open and load IStorage from file
NativeMethods.IStorage fileStorage;
NativeMethods.StgOpenStorage(storageFilePath, null,
accesMode | NativeMethods.STGM.SHARE_EXCLUSIVE, IntPtr.Zero, 0, out fileStorage);
// ReSharper disable once DoNotCallOverridableMethodsInConstructor
LoadStorage(fileStorage);
}
/// <summary>
/// Initializes a new instance of the <see cref="Storage" /> class from a <see cref="Stream" /> containing an IStorage.
/// </summary>
/// <param name="storageStream"> The <see cref="Stream" /> containing an IStorage. </param>
/// <param name="fileAccess">FileAcces mode, default is Read</param>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
private Storage(Stream storageStream, FileAccess fileAccess = FileAccess.Read)
{
NativeMethods.IStorage memoryStorage = null;
NativeMethods.ILockBytes memoryStorageBytes = null;
try
{
// Read stream into buffer
var buffer = new byte[storageStream.Length];
storageStream.Read(buffer, 0, buffer.Length);
// Create a ILockBytes (unmanaged byte array) and write buffer into it
NativeMethods.CreateILockBytesOnHGlobal(IntPtr.Zero, true, out memoryStorageBytes);
memoryStorageBytes.WriteAt(0, buffer, buffer.Length, null);
// Ensure provided stream data is an IStorage
if (NativeMethods.StgIsStorageILockBytes(memoryStorageBytes) != 0)
// ReSharper disable once LocalizableElement
throw new ArgumentException("The provided stream is not a valid IStorage", "storageStream");
var accesMode = NativeMethods.STGM.READWRITE;
FileAccess = fileAccess;
switch (fileAccess)
{
case FileAccess.Read:
accesMode = NativeMethods.STGM.READ;
break;
case FileAccess.Write:
case FileAccess.ReadWrite:
accesMode = NativeMethods.STGM.READWRITE;
break;
}
// Open and load IStorage on the ILockBytes
NativeMethods.StgOpenStorageOnILockBytes(memoryStorageBytes, null,
accesMode | NativeMethods.STGM.SHARE_EXCLUSIVE, IntPtr.Zero, 0, out memoryStorage);
// ReSharper disable once DoNotCallOverridableMethodsInConstructor
LoadStorage(memoryStorage);
}
catch
{
if (memoryStorage != null)
Marshal.ReleaseComObject(memoryStorage);
throw;
}
finally
{
if (memoryStorageBytes != null)
Marshal.ReleaseComObject(memoryStorageBytes);
}
}
/// <summary>
/// Initializes a new instance of the <see cref="Storage" /> class on the specified <see cref="NativeMethods.IStorage" />.
/// </summary>
/// <param name="storage"> The storage to create the <see cref="Storage" /> on. </param>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
private Storage(NativeMethods.IStorage storage)
{
// ReSharper disable once DoNotCallOverridableMethodsInConstructor
LoadStorage(storage);
}
/// <summary>
/// Releases unmanaged resources and performs other cleanup operations before the
/// <see cref="Storage" /> is reclaimed by garbage collection.
/// </summary>
~Storage()
{
Dispose(false);
}
#endregion
#region LoadStorage
/// <summary>
/// Processes sub streams and storages on the specified storage.
/// </summary>
/// <param name="storage"> The storage to get sub streams and storages for. </param>
protected virtual void LoadStorage(NativeMethods.IStorage storage)
{
if (storage == null)
throw new ArgumentNullException("storage", "Storage can not be null");
_storage = storage;
// Ensures memory is released
ReferenceManager.AddItem(storage);
NativeMethods.IEnumSTATSTG storageElementEnum = null;
try
{
// Enum all elements of the storage
storage.EnumElements(0, IntPtr.Zero, 0, out storageElementEnum);
// Iterate elements
while (true)
{
// Get 1 element out of the COM enumerator
uint elementStatCount;
var elementStats = new STATSTG[1];
storageElementEnum.Next(1, elementStats, out elementStatCount);
// Break loop if element not retrieved
if (elementStatCount != 1)
break;
var elementStat = elementStats[0];
switch (elementStat.type)
{
case 1:
// Element is a storage, add its statistics object to the storage dictionary
_subStorageStatistics.Add(elementStat.pwcsName, elementStat);
break;
case 2:
// Element is a stream, add its statistics object to the stream dictionary
_streamStatistics.Add(elementStat.pwcsName, elementStat);
break;
}
}
}
finally
{
// Free memory
if (storageElementEnum != null)
Marshal.ReleaseComObject(storageElementEnum);
}
}
#endregion
#region GetStreamBytes
/// <summary>
/// Gets the data in the specified stream as a byte array.
/// Returns null when the <param ref="streamName"/> does not exists.
/// </summary>
/// <param name="streamName"> Name of the stream to get data for. </param>
/// <returns> A byte array containg the stream data. </returns>
private byte[] GetStreamBytes(string streamName)
{
if (!_streamStatistics.ContainsKey(streamName))
return null;
// Get statistics for stream
var streamStatStg = _streamStatistics[streamName];
byte[] iStreamContent;
IStream stream = null;
try
{
// Open stream from the storage
stream = _storage.OpenStream(streamStatStg.pwcsName, IntPtr.Zero,
NativeMethods.STGM.READ | NativeMethods.STGM.SHARE_EXCLUSIVE, 0);
// Read the stream into a managed byte array
iStreamContent = new byte[streamStatStg.cbSize];
stream.Read(iStreamContent, iStreamContent.Length, IntPtr.Zero);
}
finally
{
if (stream != null)
Marshal.ReleaseComObject(stream);
}
// Return the stream bytes
return iStreamContent;
}
#endregion
#region GetStreamAsString
/// <summary>
/// Gets the data in the specified stream as a string using the specifed encoding to decode the stream data.
/// Returns null when the <param ref="streamName"/> does not exists.
/// </summary>
/// <param name="streamName"> Name of the stream to get string data for. </param>
/// <param name="streamEncoding"> The encoding to decode the stream data with. </param>
/// <returns> The data in the specified stream as a string. </returns>
private string GetStreamAsString(string streamName, Encoding streamEncoding)
{
var bytes = GetStreamBytes(streamName);
if (bytes == null)
return null;
var streamReader = new StreamReader(new MemoryStream(bytes), streamEncoding);
var streamContent = streamReader.ReadToEnd();
streamReader.Close();
// Remove null termination chars when they exist
return streamContent.Replace("\0", string.Empty);
}
#endregion
#region GetMapiProperty
/// <summary>
/// Gets the raw value of the MAPI property.
/// </summary>
/// <param name="propIdentifier"> The 4 char hexadecimal prop identifier. </param>
/// <returns> The raw value of the MAPI property. </returns>
private object GetMapiProperty(string propIdentifier)
{
// Check if the propIdentifier is a named property and if so replace it with
// the correct mapped property
if (_namedProperties != null)
{
var mapiTagMapping = _namedProperties.Find(m => m.EntryOrStringIdentifier == propIdentifier);
if (mapiTagMapping != null)
propIdentifier = mapiTagMapping.PropertyIdentifier;
}
// Try get prop value from stream or storage
// If not found in stream or storage try get prop value from property stream
var propValue = GetMapiPropertyFromStreamOrStorage(propIdentifier) ??
GetMapiPropertyFromPropertyStream(propIdentifier);
return propValue;
}
#endregion
#region GetMapiPropertyFromStreamOrStorage
/// <summary>
/// Gets the MAPI property value from a stream or storage in this storage.
/// </summary>
/// <param name="propIdentifier"> The 4 char hexadecimal prop identifier. </param>
/// <returns> The value of the MAPI property or null if not found. </returns>
private object GetMapiPropertyFromStreamOrStorage(string propIdentifier)
{
// Get list of stream and storage identifiers which map to properties
var propKeys = new List<string>();
propKeys.AddRange(_streamStatistics.Keys);
propKeys.AddRange(_subStorageStatistics.Keys);
// Determine if the property identifier is in a stream or sub storage
string propTag = null;
var propType = MapiTags.PT_UNSPECIFIED;
foreach (var propKey in propKeys)
{
if (!propKey.StartsWith(MapiTags.SubStgVersion1 + "_" + propIdentifier)) continue;
propTag = propKey.Substring(12, 8);
propType = ushort.Parse(propKey.Substring(16, 4), NumberStyles.HexNumber);
break;
}
// When null then we didn't find the property
if (propTag == null)
return null;
// Depending on prop type use method to get property value
var containerName = MapiTags.SubStgVersion1 + "_" + propTag;
switch (propType)
{
case MapiTags.PT_UNSPECIFIED:
return null;
case MapiTags.PT_STRING8:
//return GetStreamAsString(containerName, Encoding.UTF8);
return GetStreamAsString(containerName, Encoding.Default);
case MapiTags.PT_UNICODE:
return GetStreamAsString(containerName, Encoding.Unicode);
case MapiTags.PT_BINARY:
return GetStreamBytes(containerName);
case MapiTags.PT_MV_STRING8:
case MapiTags.PT_MV_UNICODE:
// If the property is a unicode multiview item we need to read all the properties
// again and filter out all the multivalue names, they end with -00000000, -00000001, etc..
var multiValueContainerNames = propKeys.Where(propKey => propKey.StartsWith(containerName + "-")).ToList();
var values = new List<string>();
foreach (var multiValueContainerName in multiValueContainerNames)
{
var value = GetStreamAsString(multiValueContainerName,
propType == MapiTags.PT_MV_STRING8 ? Encoding.Default : Encoding.Unicode);
// Multi values always end with a null char so we need to strip that one off
if (value.EndsWith("/0"))
value = value.Substring(0, value.Length - 1);
values.Add(value);
}
return values;
case MapiTags.PT_OBJECT:
return
NativeMethods.CloneStorage(
_storage.OpenStorage(containerName, IntPtr.Zero,
NativeMethods.STGM.READ | NativeMethods.STGM.SHARE_EXCLUSIVE,
IntPtr.Zero, 0), true);
default:
throw new ApplicationException("MAPI property has an unsupported type and can not be retrieved.");
}
}
/// <summary>
/// Gets the MAPI property value from the property stream in this storage.
/// </summary>
/// <param name="propIdentifier"> The 4 char hexadecimal prop identifier. </param>
/// <returns> The value of the MAPI property or null if not found. </returns>
private object GetMapiPropertyFromPropertyStream(string propIdentifier)
{
// If no property stream return null
if (!_streamStatistics.ContainsKey(MapiTags.PropertiesStream))
return null;
// Get the raw bytes for the property stream
var propBytes = GetStreamBytes(MapiTags.PropertiesStream);
// Iterate over property stream in 16 byte chunks starting from end of header
for (var i = _propHeaderSize; i < propBytes.Length; i = i + 16)
{
// Get property type located in the 1st and 2nd bytes as a unsigned short value
var propType = BitConverter.ToUInt16(propBytes, i);
// Get property identifer located in 3nd and 4th bytes as a hexdecimal string
var propIdent = new[] { propBytes[i + 3], propBytes[i + 2] };
var propIdentString = BitConverter.ToString(propIdent).Replace("-", string.Empty);
// If this is not the property being gotten continue to next property
if (propIdentString != propIdentifier) continue;
// Depending on prop type use method to get property value
switch (propType)
{
case MapiTags.PT_I2:
return BitConverter.ToInt16(propBytes, i + 8);
case MapiTags.PT_LONG:
return BitConverter.ToInt32(propBytes, i + 8);
case MapiTags.PT_DOUBLE:
return BitConverter.ToDouble(propBytes, i + 8);
case MapiTags.PT_SYSTIME:
var fileTime = BitConverter.ToInt64(propBytes, i + 8);
return DateTime.FromFileTime(fileTime);
case MapiTags.PT_BOOLEAN:
return BitConverter.ToBoolean(propBytes, i + 8);
//default:
//throw new ApplicationException("MAPI property has an unsupported type and can not be retrieved.");
}
}
// Property not found return null
return null;
}
/// <summary>
/// Gets the value of the MAPI property as a string.
/// </summary>
/// <param name="propIdentifier"> The 4 char hexadecimal prop identifier. </param>
/// <returns> The value of the MAPI property as a string. </returns>
private string GetMapiPropertyString(string propIdentifier)
{
return GetMapiProperty(propIdentifier) as string;
}
/// <summary>
/// Gets the value of the MAPI property as a list of string.
/// </summary>
/// <param name="propIdentifier"> The 4 char hexadecimal prop identifier. </param>
/// <returns> The value of the MAPI property as a list of string. </returns>
private ReadOnlyCollection<string> GetMapiPropertyStringList(string propIdentifier)
{
var list = GetMapiProperty(propIdentifier) as List<string>;
return list == null ? null : list.AsReadOnly();
}
/// <summary>
/// Gets the value of the MAPI property as a integer.
/// </summary>
/// <param name="propIdentifier"> The 4 char hexadecimal prop identifier. </param>
/// <returns> The value of the MAPI property as a integer. </returns>
private int? GetMapiPropertyInt32(string propIdentifier)
{
var value = GetMapiProperty(propIdentifier);
if (value != null)
return (int)value;
return null;
}
/// <summary>
/// Gets the value of the MAPI property as a double.
/// </summary>
/// <param name="propIdentifier"> The 4 char hexadecimal prop identifier. </param>
/// <returns> The value of the MAPI property as a double. </returns>
private double? GetMapiPropertyDouble(string propIdentifier)
{
var value = GetMapiProperty(propIdentifier);
if (value != null)
return (double)value;
return null;
}
/// <summary>
/// Gets the value of the MAPI property as a datetime.
/// </summary>
/// <param name="propIdentifier"> The 4 char hexadecimal prop identifier. </param>
/// <returns> The value of the MAPI property as a datetime or null when not set </returns>
private DateTime? GetMapiPropertyDateTime(string propIdentifier)
{
var value = GetMapiProperty(propIdentifier);
if (value != null)
return (DateTime)value;
return null;
}
/// <summary>
/// Gets the value of the MAPI property as a bool.
/// </summary>
/// <param name="propIdentifier"> The 4 char hexadecimal prop identifier. </param>
/// <returns> The value of the MAPI property as a boolean or null when not set. </returns>
private bool? GetMapiPropertyBool(string propIdentifier)
{
var value = GetMapiProperty(propIdentifier);
if (value != null)
return (bool)value;
return null;
}
/// <summary>
/// Gets the value of the MAPI property as a byte array.
/// </summary>
/// <param name="propIdentifier"> The 4 char hexadecimal prop identifier. </param>
/// <returns> The value of the MAPI property as a byte array. </returns>
private byte[] GetMapiPropertyBytes(string propIdentifier)
{
return (byte[])GetMapiProperty(propIdentifier);
}
#endregion
#region IDisposable Members
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Disposes this object
/// </summary>
/// <param name="disposing"></param>
protected virtual void Dispose(bool disposing)
{
if (disposing)
Disposing();
if (_storage == null) return;
ReferenceManager.RemoveItem(_storage);
Marshal.ReleaseComObject(_storage);
_storage = null;
}
/// <summary>
/// Gives sub classes the chance to free resources during object disposal.
/// </summary>
protected virtual void Disposing()
{
}
#endregion
}
}