public static SectionHeaderOption Parse(BinaryReader binaryReader, bool reverseByteOrder, Action <Exception> ActionOnException) { //Contract.Requires<ArgumentNullException>(binaryReader != null, "binaryReader cannot be null"); SectionHeaderOption option = new SectionHeaderOption(); List <KeyValuePair <ushort, byte[]> > optionsList = EkstractOptions(binaryReader, reverseByteOrder, ActionOnException); if (optionsList.Any()) { foreach (var item in optionsList) { try { switch (item.Key) { case (ushort)SectionHeaderOptionCode.CommentCode: option.Comment = UTF8Encoding.UTF8.GetString(item.Value); break; case (ushort)SectionHeaderOptionCode.HardwareCode: option.Hardware = UTF8Encoding.UTF8.GetString(item.Value); break; case (ushort)SectionHeaderOptionCode.OperatingSystemCode: option.OperatingSystem = UTF8Encoding.UTF8.GetString(item.Value); break; case (ushort)SectionHeaderOptionCode.UserApplicationCode: option.UserApplication = UTF8Encoding.UTF8.GetString(item.Value); break; case (ushort)SectionHeaderOptionCode.EndOfOptionsCode: default: break; } } catch (Exception exc) { if (ActionOnException != null) { ActionOnException(exc); } } } } return(option); }
public static void SectionHeaderOption_ConvertToByte_Test(bool reorder) { SectionHeaderOption preOption = new SectionHeaderOption(); SectionHeaderOption postOption; preOption.Comment = "Test Comment"; preOption.Hardware = "x86 Personal Computer"; preOption.OperatingSystem = "Windows 7"; preOption.UserApplication = "PcapngUtils"; byte[] preOptionByte = preOption.ConvertToByte(reorder, null); using (MemoryStream stream = new MemoryStream(preOptionByte)) { using (BinaryReader binaryReader = new BinaryReader(stream)) { postOption = SectionHeaderOption.Parse(binaryReader, reorder, null); } } Assert.IsNotNull(postOption); Assert.AreEqual(preOption.Comment, postOption.Comment); Assert.AreEqual(preOption.Hardware, postOption.Hardware); Assert.AreEqual(preOption.OperatingSystem, postOption.OperatingSystem); Assert.AreEqual(preOption.UserApplication, postOption.UserApplication); }
public static void SectionHeaderOption_ConvertToByte_Test(bool reorder) { SectionHeaderOption preOption = new SectionHeaderOption(); SectionHeaderOption postOption; preOption.Comment = "Test Comment"; preOption.Hardware = "x86 Personal Computer"; preOption.OperatingSystem = "Windows 7"; preOption.UserApplication = "PcapngUtils"; byte[] preOptionByte = preOption.ConvertToByte(reorder, null); using (MemoryStream stream = new MemoryStream(preOptionByte)) { using (BinaryReader binaryReader = new BinaryReader(stream)) { postOption = SectionHeaderOption.Parse(binaryReader, reorder, null); } } Assert.IsNotNull(postOption); Assert.AreEqual(preOption.Comment, postOption.Comment); Assert.AreEqual(preOption.Hardware, postOption.Hardware); Assert.AreEqual(preOption.OperatingSystem, postOption.OperatingSystem); Assert.AreEqual(preOption.UserApplication, postOption.UserApplication); }
public static SectionHeaderOption Parse(BinaryReader binaryReader, bool reverseByteOrder, Action<Exception> ActionOnException) { Contract.Requires<ArgumentNullException>(binaryReader != null, "binaryReader cannot be null"); SectionHeaderOption option = new SectionHeaderOption(); List<KeyValuePair<ushort, byte[]>> optionsList = EkstractOptions(binaryReader, reverseByteOrder, ActionOnException); if (optionsList.Any()) { foreach (var item in optionsList) { try { switch (item.Key) { case (ushort)SectionHeaderOptionCode.CommentCode: option.Comment = UTF8Encoding.UTF8.GetString(item.Value); break; case (ushort)SectionHeaderOptionCode.HardwareCode: option.Hardware = UTF8Encoding.UTF8.GetString(item.Value); break; case (ushort)SectionHeaderOptionCode.OperatingSystemCode: option.OperatingSystem = UTF8Encoding.UTF8.GetString(item.Value); break; case (ushort)SectionHeaderOptionCode.UserApplicationCode: option.UserApplication = UTF8Encoding.UTF8.GetString(item.Value); break; case (ushort)SectionHeaderOptionCode.EndOfOptionsCode: default: break; } } catch (Exception exc) { if (ActionOnException != null) ActionOnException(exc); } } } return option; }
/// <summary> /// The Section Header Block is mandatory. It identifies the beginning of a section of the capture dump file. The Section Header Block /// does not contain data but it rather identifies a list of blocks (interfaces, packets) that are logically correlated. /// </summary> public SectionHeaderBlock(MagicNumbers MagicNumber, UInt16 MajorVersion, UInt16 MinorVersion, Int64 SectionLength, SectionHeaderOption Options, long PositionInStream = 0) { Contract.Requires<ArgumentNullException>(Options != null, "Options cannot be null"); this.MagicNumber = MagicNumber; this.MajorVersion = MajorVersion; this.MinorVersion = MinorVersion; this.SectionLength = SectionLength; this.options = Options; this.PositionInStream = PositionInStream; }
public static SectionHeaderBlock GetEmptyHeader(bool ReverseByteOrder) { Assembly assembly = Assembly.GetExecutingAssembly(); AssemblyName assemblyName= assembly.GetName(); string app = string.Format("{0} {1}", assemblyName.Name, assemblyName.Version.ToString()); SectionHeaderOption options = new SectionHeaderOption(UserApplication: app); return new SectionHeaderBlock(ReverseByteOrder ? MagicNumbers.Swapped : MagicNumbers.Identical, 1, 0, -1, options); }