예제 #1
0
        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);
        }