forked from ryrychj/PcapngUtils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIReaderFactory.cs
73 lines (65 loc) · 2.62 KB
/
IReaderFactory.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
using PcapngUtils.Common;
using PcapngUtils.Extensions;
using PcapngUtils.Pcap;
using PcapngUtils.PcapNG;
using PcapngUtils.PcapNG.BlockTypes;
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PcapngUtils
{
public sealed class IReaderFactory
{
#region fields && properties
#endregion
#region methods
public static IReader GetReader(string path)
{
Contract.Requires<ArgumentNullException>(!string.IsNullOrWhiteSpace(path), "path cannot be null or empty");
Contract.Requires<ArgumentException>(File.Exists(path), "file must exists");
UInt32 mask = 0;
using (FileStream stream = new FileStream(path, FileMode.Open))
{
using (BinaryReader binaryReader = new BinaryReader(stream))
{
if (binaryReader.BaseStream.Length < 12)
throw new ArgumentException(string.Format("[IReaderFactory.GetReader] file {0} is too short ", path));
mask = binaryReader.ReadUInt32();
if (mask == (uint)BaseBlock.Types.SectionHeader)
{
binaryReader.ReadUInt32();
mask = binaryReader.ReadUInt32();
}
}
}
switch (mask)
{
case (uint)Pcap.SectionHeader.MagicNumbers.microsecondIdentical:
case (uint)Pcap.SectionHeader.MagicNumbers.microsecondSwapped:
case (uint)Pcap.SectionHeader.MagicNumbers.nanosecondSwapped:
case (uint)Pcap.SectionHeader.MagicNumbers.nanosecondIdentical:
{
IReader reader = new PcapReader(path);
return reader;
}
case (uint)PcapNG.BlockTypes.SectionHeaderBlock.MagicNumbers.Identical:
{
IReader reader = new PcapNGReader(path, false);
return reader;
}
case (uint)PcapNG.BlockTypes.SectionHeaderBlock.MagicNumbers.Swapped:
{
IReader reader = new PcapNGReader(path, true);
return reader;
}
default:
throw new ArgumentException(string.Format("[IReaderFactory.GetReader] file {0} is not PCAP/PCAPNG file", path));
}
}
#endregion
}
}