Exemplo n.º 1
0
        public FileGraph ApplyFilterToFile(EmbeddedFile file, string filterName,
                                           IParserOutput output)
        {
            if (file == null || filterName == null || output == null)
            {
                throw new ArgumentException("Invalid parameters specified.");
            }

            // lookup the filter with the specified name
            DataFileDef filter = m_mapping.GetFilterByName(filterName);

            if (filter == null)
            {
                output.HandleFatalError("The specified filter does not exist.");
            }

            FileGraph graph = new FileGraph(file.FileId);

            try
            {
                // open the input file
                file.PrepareFileForReading();

                // create a root node in the graph
                graph.RootNode = new GraphNode("Root node", NodeType.Complex, file.Position);
                foreach (object filterObject in filter.Items)
                {
                    DecodeEntry(graph.RootNode, filterObject, output, file);
                }
                graph.RootNode.FilePositionEnd = file.Position;

                graph.UpdateEndPositionOfNodes();
            }
            catch (Exception ex)
            {
                output.HandleFatalError("Unexpected exception: " + ex.Message);
            }
            finally
            {
                file.FileReadingComplete();
            }
            return(graph);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Displays the contents of the file
        /// </summary>
        /// <param name="highlightMinPos">Start position to highlight. Zero based.</param>
        /// <param name="highlightMaxPos">End position to highlight. Zero based, upper bound inclusive.</param>
        private void ShowFileContents(int highlightMinPos = -1, int highlightMaxPos = -1)
        {
            // obtain the data of the current file
            byte[] fileData;
            try
            {
                m_currentFile.PrepareFileForReading();
                fileData = m_currentFile.FileData;
            }
            finally
            {
                m_currentFile.FileReadingComplete();
            }

            const int lineSize = 32; // amount of chars per line

            int           dataPos           = 0;
            int           length            = Math.Min(lineSize, fileData.Length);
            StringBuilder sb                = new StringBuilder();
            int           highLightRtfStart = -1;
            int           highLightRtfEnd   = -1;

            while (length > 0)
            {
                bool doHighLight = false;
                for (int i = dataPos; i < dataPos + length; i++)
                {
                    doHighLight = (i >= highlightMinPos) && (i <= highlightMaxPos);


                    if (doHighLight)
                    {
                        if (highLightRtfStart < 0)
                        {
                            highLightRtfStart = i;
                            highLightRtfEnd   = i;
                        }
                        else
                        {
                            highLightRtfEnd++;
                        }
                    }


                    sb.Append(fileData[i].ToString("X2"));
                    sb.Append(' ');
                }
                //sb.Append("\t\t");
                //for (int i = dataPos; i < dataPos + length; i++)
                //{
                //    sb.Append(Convert.ToChar(fileData[i]));
                //}
                dataPos += length;
                length   = Math.Min(lineSize, fileData.Length - dataPos);

                // add newline
                //sb.Append(Environment.NewLine);
            }

            // set text
            rtbHex.Clear();
            rtbHex.ResetText();
            rtbHex.Text = sb.ToString();

            // highlight selection
            if (highLightRtfStart >= 0 && highLightRtfEnd > highLightRtfStart)
            {
                // the selection is known as byte poitions. Convert these to text positions
                // each byte is rendered as three characters (hex x 2 + space)
                highLightRtfStart *= 3;
                highLightRtfEnd   *= 3;

                rtbHex.Select(highLightRtfStart, highLightRtfEnd - highLightRtfStart - 1);
                rtbHex.SelectionColor     = Color.White;
                rtbHex.SelectionBackColor = Color.Black;
            }

            // add line splits
            if (fileData.Length > lineSize)
            {
                const int charsPerByte = 3;
                int       textSizePos  = (fileData.Length / lineSize) * lineSize;
                while (textSizePos > 0)
                {
                    rtbHex.SelectionStart  = (textSizePos * charsPerByte);
                    rtbHex.SelectionLength = 0;
                    rtbHex.SelectedText    = Environment.NewLine;

                    textSizePos -= lineSize;
                }
            }
        }