public bool LoadFromFile(string fileName) { if (!File.Exists(fileName)) { return(false); } PacketLogTypes packetType = PacketLogTypes.Unknown; PacketLogFileFormats logType = PacketLogFileFormats.Unknown; var fn = fileName.ToLower(); // first check out, then in (as "in" is also in "ougoing") if ((packetType == PacketLogTypes.Unknown) && (Path.GetFileNameWithoutExtension(fn).IndexOf("out") >= 0)) { packetType = PacketLogTypes.Outgoing; } if ((packetType == PacketLogTypes.Unknown) && (Path.GetFileNameWithoutExtension(fn).IndexOf("in") >= 0)) { packetType = PacketLogTypes.Incoming; } if ((packetType == PacketLogTypes.Unknown) && (fn.IndexOf("out") >= 0)) { packetType = PacketLogTypes.Outgoing; } if ((packetType == PacketLogTypes.Unknown) && (fn.IndexOf("in") >= 0)) { packetType = PacketLogTypes.Incoming; } if ((logType == PacketLogFileFormats.Unknown) && (Path.GetExtension(fn) == ".log")) { logType = PacketLogFileFormats.WindowerPacketViewer; } if ((logType == PacketLogFileFormats.Unknown) && (Path.GetExtension(fn) == ".txt")) { logType = PacketLogFileFormats.AshitaPacketeer; } if ((logType == PacketLogFileFormats.Unknown) && (Path.GetExtension(fn) == ".sqlite")) { logType = PacketLogFileFormats.PacketDB; } if ((logType == PacketLogFileFormats.WindowerPacketViewer) || (logType == PacketLogFileFormats.AshitaPacketeer)) { List <string> sl = File.ReadAllLines(fileName).ToList(); return(LoadFromStringList(sl, logType, packetType)); } else if (logType == PacketLogFileFormats.PacketDB) { return(LoadFromSQLite3(fileName)); } else { return(false); } }
public bool LoadFromStringList(List <string> FileData, PacketLogFileFormats logFileType, PacketLogTypes preferedType) { // Add dummy blank lines to fix a bug of ignoring last packet if isn't finished by a blank line FileData.Add(""); // TODO: Loading Form Application.UseWaitCursor = true; using (LoadingForm loadform = new LoadingForm(MainForm.thisMainForm)) { try { loadform.Text = "Loading text log file"; loadform.Show(); loadform.pb.Minimum = 0; loadform.pb.Maximum = FileData.Count; loadform.pb.Step = 1000; PacketData PD = null; bool IsUndefinedPacketType = true; bool AskForPacketType = true; int c = 0; foreach (string s in FileData) { string sLower = s.ToLower(); if ((s != "") && (PD == null)) { // Begin building a new packet PD = new PacketData(); if (sLower.IndexOf("incoming") >= 0) { PD.PacketLogType = PacketLogTypes.Incoming; IsUndefinedPacketType = false; logFileType = PacketLogFileFormats.WindowerPacketViewer; } else if (sLower.IndexOf("outgoing") >= 0) { PD.PacketLogType = PacketLogTypes.Outgoing; IsUndefinedPacketType = false; logFileType = PacketLogFileFormats.WindowerPacketViewer; } else if (sLower.IndexOf("[s->c]") >= 0) { PD.PacketLogType = PacketLogTypes.Incoming; IsUndefinedPacketType = false; logFileType = PacketLogFileFormats.AshitaPacketeer; } else if (sLower.IndexOf("[c->s]") >= 0) { PD.PacketLogType = PacketLogTypes.Outgoing; IsUndefinedPacketType = false; logFileType = PacketLogFileFormats.AshitaPacketeer; } else { PD.PacketLogType = preferedType; } if ( // Not a comment or empty line ((s != "") && (!s.StartsWith("--"))) && // Unknown packet and we need to know ? (IsUndefinedPacketType && AskForPacketType && (PD.PacketLogType == PacketLogTypes.Unknown)) ) { AskForPacketType = false; // Ask for type var askDlgRes = DialogResult.Cancel; using (PacketTypeSelectForm askDlg = new PacketTypeSelectForm()) { askDlg.lHeaderData.Text = s; askDlgRes = askDlg.ShowDialog(); //var askDlgStr = "Unable to indentify the packet type.\r\nDo you want to assign a default type ?\r\n\r\nPress YES for Incomming\r\n\r\nPress NO for outgoing\r\n\r\nPress Cancel to keep it undefined\r\n\r\nLineData:\r\n\r\n" + s.Substring(0, Math.Min(s.Length, 100)) + " ..."; //MessageBox.Show(askDlgStr, "Packet Type ?", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question); } if (askDlgRes == DialogResult.Yes) { preferedType = PacketLogTypes.Incoming; IsUndefinedPacketType = false; PD.PacketLogType = preferedType; } else if (askDlgRes == DialogResult.No) { preferedType = PacketLogTypes.Outgoing; IsUndefinedPacketType = false; PD.PacketLogType = preferedType; } } PD.RawText.Add(s); PD.HeaderText = s; PD.OriginalHeaderText = s; if (logFileType == PacketLogFileFormats.Unknown) { // Assume the pasted data is just raw hex bytes PD.HeaderText = "Clipboard"; PD.OriginalHeaderText = "Clipboard Data"; PD.AddRawHexDataAsBytes(s); } } // end start new packet else if ((s != "") && (PD != null)) { // Add line of data PD.RawText.Add(s); // Actual packet data starts at the 3rd line after the header if ((logFileType != PacketLogFileFormats.AshitaPacketeer) && (PD.RawText.Count > 3)) { PD.AddRawLineAsBytes(s); } else if ((logFileType == PacketLogFileFormats.AshitaPacketeer) && (PD.RawText.Count > 1)) { PD.AddRawPacketeerLineAsBytes(s); } else if (logFileType == PacketLogFileFormats.Unknown) { // Assume the pasted data is just raw hex bytes PD.AddRawHexDataAsBytes(s); } } else if ((s == "") && (PD != null)) { // Close this packet and add it to list if (PD.CompileData(logFileType)) { PacketDataList.Add(PD); } else { // Invalid data } PD = null; } else if ((s == "") && (PD == null)) { // Blank line } else if (s.StartsWith("--") && (PD != null)) { // Comment } else { // ERROR, this should not be possible in a valid file, but just ignore it } c++; if ((c % 1000) == 0) { loadform.pb.PerformStep(); loadform.pb.Refresh(); } } // end foreach datafile line } catch { Application.UseWaitCursor = false; return(false); } } Application.UseWaitCursor = false; return(true); }
public bool CompileData(PacketLogFileFormats plff) { if (RawBytes.Count < 4) { PacketID = 0xFFFF; PacketDataSize = 0; HeaderText = "Invalid Packet Size < 4"; return(false); } PacketID = (UInt16)(GetByteAtPos(0) + ((GetByteAtPos(1) & 0x01) * 0x100)); PacketDataSize = (UInt16)((GetByteAtPos(1) & 0xFE) * 2); PacketSync = (UInt16)(GetByteAtPos(2) + (GetByteAtPos(3) * 0x100)); string TS = ""; if (TimeStamp.Ticks > 0) { TS = TimeStamp.ToString("HH:mm:ss"); } if (plff == PacketLogFileFormats.AshitaPacketeer) { // Packeteer doesn't have time info (yet) TimeStamp = new DateTime(0); OriginalTimeString = "0000-00-00 00:00"; } if (plff == PacketLogFileFormats.WindowerPacketViewer) { // Try to determine timestamp from header var P1 = OriginalHeaderText.IndexOf('['); var P2 = OriginalHeaderText.IndexOf(']'); if ((P1 >= 0) && (P2 >= 0) && (P2 > P1)) { OriginalTimeString = OriginalHeaderText.Substring(P1 + 1, P2 - P1 - 1); if (OriginalTimeString.Length > 0) { try { // try quick-parse first DateTime dt; if (DateTimeParse(OriginalTimeString, out dt)) { TimeStamp = dt; } else { TimeStamp = DateTime.Parse(OriginalTimeString, cultureForDateTimeParse, stylesForDateTimeParse); } TS = TimeStamp.ToString("HH:mm:ss"); } catch (FormatException) { TimeStamp = new DateTime(0); TS = ""; OriginalTimeString = "0000-00-00 00:00"; } } } } VirtualTimeStamp = TimeStamp; if (TimeStamp.Ticks == 0) { TS = ""; } string S = ""; switch (PacketLogType) { case PacketLogTypes.Outgoing: S = "OUT "; break; case PacketLogTypes.Incoming: S = "IN "; break; default: S = "??? "; break; } S = TS + " : " + S + "0x" + PacketID.ToString("X3") + " - "; HeaderText = S + DataLookups.PacketTypeToString(PacketLogType, PacketID); return(true); }