/// <summary> /// Reads TShark formatted data files. /// </summary> /// <param name="sourceFile">The original capture file path.</param> /// <param name="schema">The TShark schema.</param> /// <param name="dataFile">The TShark data file path.</param> /// <param name="callback">Callback that will receive data rows.</param> /// <param name="fixups">Fixups object containing any fixups to apply.</param> public static void Read(string sourceFile, TSharkDataSchema schema, string dataFile, TSharkDataReaderCallback callback, TSharkFixups fixups) { XmlReaderSettings xmlReaderSettings = new XmlReaderSettings(); xmlReaderSettings.ConformanceLevel = ConformanceLevel.Document; xmlReaderSettings.IgnoreComments = true; xmlReaderSettings.IgnoreProcessingInstructions = true; xmlReaderSettings.IgnoreWhitespace = true; xmlReaderSettings.ValidationType = ValidationType.None; CapFile capFile = new CapFile(); capFile.Path = sourceFile; using (StreamReader dataReader = new StreamReader(dataFile, Encoding.UTF8)) using (XmlReader xmlReader = XmlReader.Create(dataReader, xmlReaderSettings)) { ParseState state = new ParseState(); state.Schema = schema; state.Document = new XmlDocument(); state.Reader = xmlReader; state.File = capFile; state.Callback = callback; state.Fixups = fixups; int index = 0; // read all packets in the file while (true) { if (!xmlReader.ReadToFollowing("packet")) { break; } CapPacket capPacket = new CapPacket(); capPacket.File = state.File; capPacket.Number = index++; try { state.Packet = capPacket; ReadPacket(state); } catch (Exception ex) { Log.WriteError("Error processing packet.\nIndex: {0}\nError: {1}", index, ex.Message); } finally { state.Packet = null; } } } }
/// <summary> /// Reads TShark formatted data files. /// </summary> /// <param name="sourceFile">The original capture file path.</param> /// <param name="schema">The TShark schema.</param> /// <param name="dataFile">The TShark data file path.</param> /// <param name="callback">Callback that will receive data rows.</param> /// <param name="fixups">Fixups object containing any fixups to apply.</param> public static void Read(string sourceFile, TSharkDataSchema schema, string dataFile, TSharkDataReaderCallback callback, TSharkFixups fixups) { XmlReaderSettings xmlReaderSettings = new XmlReaderSettings(); xmlReaderSettings.ConformanceLevel = ConformanceLevel.Document; xmlReaderSettings.IgnoreComments = true; xmlReaderSettings.IgnoreProcessingInstructions = true; xmlReaderSettings.IgnoreWhitespace = true; xmlReaderSettings.ValidationType = ValidationType.None; CapFile capFile = new CapFile(); capFile.Path = sourceFile; using (StreamReader dataReader = new StreamReader(dataFile, Encoding.UTF8)) using (XmlReader xmlReader = XmlReader.Create(dataReader, xmlReaderSettings)) { ParseState state = new ParseState(); state.Schema = schema; state.Document = new XmlDocument(); state.Reader = xmlReader; state.File = capFile; state.Callback = callback; state.Fixups = fixups; int index = 0; // read all packets in the file while (true) { if (!xmlReader.ReadToFollowing("packet")) break; CapPacket capPacket = new CapPacket(); capPacket.File = state.File; capPacket.Number = index++; try { state.Packet = capPacket; ReadPacket(state); } catch (Exception ex) { Log.WriteError("Error processing packet.\nIndex: {0}\nError: {1}", index, ex.Message); } finally { state.Packet = null; } } } }
/// <summary> /// Reads TShark formatted schema files. /// </summary> /// <param name="protocolsFile">Protocols file.</param> /// <param name="fieldsFile">Fields file.</param> /// <param name="valuesFile">Values file.</param> /// <param name="decodesFile">Decodes file.</param> /// <returns>Data structure containing the schema that was read.</returns> public static TSharkDataSchema Read(string protocolsFile, string fieldsFile, string valuesFile, string decodesFile) { TSharkDataSchema schema = new TSharkDataSchema(); int lineIndex = 0; // read protocols using (StreamReader reader = new StreamReader(protocolsFile, Encoding.UTF8)) { while (true) { try { string line = reader.ReadLine(); if (line == null) break; lineIndex++; if (line.Length == 0) continue; string[] parts = SplitParts(line, 3); if (parts.Length != 3) throw new Exception(string.Format("Badly formatted protocols file. Line: {0}", lineIndex)); schema.AddProtocol(parts[0], parts[1], parts[2]); } catch (Exception ex) { Log.WriteWarning(ex.Message); } } } // read fields using (StreamReader reader = new StreamReader(fieldsFile, Encoding.UTF8)) { while (true) { try { string line = reader.ReadLine(); if (line == null) break; lineIndex++; if (line.Length == 0) continue; string[] parts; switch (line[0]) { case 'F': parts = SplitParts(line, 8); if (parts.Length != 8) throw new Exception(string.Format("Badly formatted fields file. Line: {0}", lineIndex)); schema.AddField(parts[1], parts[2], parts[3], parts[4], parts[5], parts[6], parts[7]); break; case 'P': parts = SplitParts(line, 3); if (parts.Length != 3) throw new Exception(string.Format("Badly formatted fields file. Line: {0}", lineIndex)); if (!schema.Protocols.ContainsKey(parts[2].ToLowerInvariant())) { schema.AddProtocol(parts[1], parts[2], parts[2].ToLowerInvariant()); } break; } } catch (Exception ex) { Log.WriteWarning(ex.Message); } } } // read values using (StreamReader reader = new StreamReader(valuesFile, Encoding.UTF8)) { while (true) { try { string line = reader.ReadLine(); if (line == null) break; lineIndex++; if (line.Length == 0) continue; string[] parts; switch (line[0]) { case 'V': parts = SplitParts(line, 4); long value; if (parts.Length != 4 || !TSharkTypeParser.TryParseInt64(parts[2], out value)) throw new Exception(string.Format("Badly formatted values file. Line: {0}", lineIndex)); schema.AddValueString(parts[1], value, parts[3]); break; case 'R': parts = SplitParts(line, 5); long lowerBound, upperBound; if (parts.Length != 5 || !TSharkTypeParser.TryParseInt64(parts[2], out lowerBound) || !TSharkTypeParser.TryParseInt64(parts[3], out upperBound)) throw new Exception(string.Format("Badly formatted values file. Line: {0}", lineIndex)); schema.AddRangeString(parts[1], lowerBound, upperBound, parts[4]); break; case 'T': parts = SplitParts(line, 4); if (parts.Length != 4) throw new Exception(string.Format("Badly formatted values file. Line: {0}", lineIndex)); schema.AddTrueFalseString(parts[1], parts[2], parts[3]); break; } } catch (Exception ex) { Log.WriteWarning(ex.Message); } } } return schema; }
/// <summary> /// Reads TShark formatted schema files. /// </summary> /// <param name="protocolsFile">Protocols file.</param> /// <param name="fieldsFile">Fields file.</param> /// <param name="valuesFile">Values file.</param> /// <param name="decodesFile">Decodes file.</param> /// <returns>Data structure containing the schema that was read.</returns> public static TSharkDataSchema Read(string protocolsFile, string fieldsFile, string valuesFile, string decodesFile) { TSharkDataSchema schema = new TSharkDataSchema(); int lineIndex = 0; // read protocols using (StreamReader reader = new StreamReader(protocolsFile, Encoding.UTF8)) { while (true) { try { string line = reader.ReadLine(); if (line == null) { break; } lineIndex++; if (line.Length == 0) { continue; } string[] parts = SplitParts(line, 3); if (parts.Length != 3) { throw new Exception(string.Format("Badly formatted protocols file. Line: {0}", lineIndex)); } schema.AddProtocol(parts[0], parts[1], parts[2]); } catch (Exception ex) { Log.WriteWarning(ex.Message); } } } // read fields using (StreamReader reader = new StreamReader(fieldsFile, Encoding.UTF8)) { while (true) { try { string line = reader.ReadLine(); if (line == null) { break; } lineIndex++; if (line.Length == 0) { continue; } string[] parts; switch (line[0]) { case 'F': parts = SplitParts(line, 8); if (parts.Length != 8) { throw new Exception(string.Format("Badly formatted fields file. Line: {0}", lineIndex)); } schema.AddField(parts[1], parts[2], parts[3], parts[4], parts[5], parts[6], parts[7]); break; case 'P': parts = SplitParts(line, 3); if (parts.Length != 3) { throw new Exception(string.Format("Badly formatted fields file. Line: {0}", lineIndex)); } if (!schema.Protocols.ContainsKey(parts[2].ToLowerInvariant())) { schema.AddProtocol(parts[1], parts[2], parts[2].ToLowerInvariant()); } break; } } catch (Exception ex) { Log.WriteWarning(ex.Message); } } } // read values using (StreamReader reader = new StreamReader(valuesFile, Encoding.UTF8)) { while (true) { try { string line = reader.ReadLine(); if (line == null) { break; } lineIndex++; if (line.Length == 0) { continue; } string[] parts; switch (line[0]) { case 'V': parts = SplitParts(line, 4); long value; if (parts.Length != 4 || !TSharkTypeParser.TryParseInt64(parts[2], out value)) { throw new Exception(string.Format("Badly formatted values file. Line: {0}", lineIndex)); } schema.AddValueString(parts[1], value, parts[3]); break; case 'R': parts = SplitParts(line, 5); long lowerBound, upperBound; if (parts.Length != 5 || !TSharkTypeParser.TryParseInt64(parts[2], out lowerBound) || !TSharkTypeParser.TryParseInt64(parts[3], out upperBound)) { throw new Exception(string.Format("Badly formatted values file. Line: {0}", lineIndex)); } schema.AddRangeString(parts[1], lowerBound, upperBound, parts[4]); break; case 'T': parts = SplitParts(line, 4); if (parts.Length != 4) { throw new Exception(string.Format("Badly formatted values file. Line: {0}", lineIndex)); } schema.AddTrueFalseString(parts[1], parts[2], parts[3]); break; } } catch (Exception ex) { Log.WriteWarning(ex.Message); } } } return(schema); }