Exemplo n.º 1
0
        public int SearchReplaceBytePattern(byte[] data, SearchReplacePattern srp)
        {
            int matches = 0;

            // Will browse the file multiple times
            // Can be made more effective to read once and match
            // all SearchReplacePattern at each position
            for (int i = 0; i < data.Length - srp.SearchPattern.Length; i++)
            {
                bool match = true;
                for (int k = 0; k < srp.SearchPattern.Length; k++)
                {
                    if (data[i + k] != srp.SearchPattern[k])
                    {
                        match = false;
                        break;
                    }
                }

                if (match)
                {
                    // Check if the Head AND Tail is matching
                    foreach (byte[][] htArray in srp.CheckHeadAndTail)
                    {
                        // Head in htArray[0]
                        bool headmatch = true;
                        for (int j = 0; j < htArray[0].Length; j++)
                        {
                            if (data[i - htArray[0].Length + j] == htArray[0][j])
                            {
                                headmatch = true;
                            }
                            else
                            {
                                headmatch = false;
                                break;
                            }
                        }

                        // Tail in htArray[1]
                        bool tailmatch = true;
                        for (int j = 0; j < htArray[1].Length; j++)
                        {
                            if (data[i + srp.SearchPattern.Length + j] == htArray[1][j])
                            {
                                tailmatch = true;
                            }
                            else
                            {
                                tailmatch = false;
                                break;
                            }
                        }

                        if (match && headmatch && tailmatch)
                        {
                            // Do the actual replacement
                            matches++;
                            for (int j = 0; j < srp.SearchPattern.Length; j++)
                            {
                                data[i + j] = srp.ReplaceWith[j];

                            }
                            break;
                        }
                    }
                }
            }
            return matches;
        }
Exemplo n.º 2
0
        public int performSearchAndReplace(SearchReplacePattern sp)
        {
            int num_replacements = 0;

            // Read the complete file into memory since we will scan it over and over again
            if (File.Exists(m_currentfile))
            {
                byte[] buff = File.ReadAllBytes(m_currentfile);

                // Search and replace
                //int num_replacements = ReplaceBytePattern(buff, find_p, replace_p, 0xFF, 0xFE);
                if (sp.isDirectReplace())
                {
                    num_replacements = 1;
                    ReplaceBytePattern(buff, sp);
                }
                else
                {
                    num_replacements = SearchReplaceBytePattern(buff, sp);
                }

                // Store the file
                File.WriteAllBytes(m_currentfile, buff);
            }
            return num_replacements;
        }
Exemplo n.º 3
0
 public void ReplaceBytePattern(byte[] data, SearchReplacePattern srp)
 {
     for (int j = 0; j < srp.ReplaceWith.Length; j++)
     {
         data[srp.ReplaceAddress+j] = srp.ReplaceWith[j];
     }
 }
Exemplo n.º 4
0
            public SearchReplaceTuningPackage(string searchReplace)
            {
                type = FileTuningPackType.SearchAndReplace;

                //
                // Parses a string and transform it to byte arrays
                // 'Name',{SEARCH},{REPLACE},{{{HEAD_1},{TAIL_1}},{{HEAD_N},{TAIL__N}}}
                // E.g.
                // 'NEW',{0x3D,0x7C,0x0C,0x4E},{0x3D,0x7C,0x0F,0xA0},{{{0x01, 0xAA},{0xFF}},{{},{0xFE}}}
                // 'NEW',{0x3D,0x7C},{0x0F,0xA0},{{{0x01, 0xAA},{0xFF}}}
                //
                int foundS1 = 0;
                int foundS2 = 0;
                byte[] bSearch = new byte[] {};
                byte[] bReplace = new byte[] {};
                byte[][][] myCheckHeadAndTail = new byte[][][] {};
                List<byte[][]> headTailList = new List<byte[][]>();

                // Validate input
                int count_in = 0;
                int count_out = 0;
                int count_in_br = 0;
                int count_out_br = 0;
                int count_st = 0;
                int count_cm = 0;
                foreach (char c in searchReplace)
                {
                    if (c == '{') count_in++;
                    if (c == '}') count_out++;
                    if (c == '[') count_in_br++;
                    if (c == ']') count_out_br++;
                    if (c == '\'') count_st++;
                    if (c == ',') count_cm++;
                }
                if (count_in_br > 0)
                {
                    if ((count_in != 2 || count_in != count_out || count_in_br != 1 || count_in_br != count_out_br || count_st != 2 || count_cm < 2))
                    {
                        _name = "FAIL IN REP VALIDATION: " + searchReplace.Substring(0, 6) + "...";
                        srp = new SearchReplacePattern(bSearch, bReplace, myCheckHeadAndTail);
                        return;
                    }
                }
                else
                {
                    if ((count_in < 6 || count_in != count_out || count_st != 2 || count_cm < 4))
                    {
                        _name = "FAIL IN SnR VALIDATION: " + searchReplace.Substring(0, 6) + "...";
                        srp = new SearchReplacePattern(bSearch, bReplace, myCheckHeadAndTail);
                        return;
                    }
                }

                // Create a string to work with
                string inputStr = searchReplace.Trim();

                // Extract the name
                foundS1 = inputStr.IndexOf('\'') + 1;
                foundS2 = inputStr.IndexOf('\'', foundS1);
                _name = inputStr.Substring(foundS1, foundS2 - foundS1);
                inputStr = inputStr.Remove(0, foundS2 + 2);

                // Remove all whitespace
                inputStr = Regex.Replace(inputStr, @"\s+", "");

                // Extract search pattern
                foundS1 = inputStr.IndexOf('{') + 1;
                foundS2 = inputStr.IndexOf('}');

                // Simple replace
                int foundS1B = 0;
                int foundS2B = 0;
                string adress_string = string.Empty;
                int address=-1;

                if ((foundS1B = inputStr.IndexOf('[')) >= 0)
                {
                    foundS1B++;
                    foundS2B = inputStr.IndexOf(']');
                    adress_string = inputStr.Substring(foundS1B, foundS2B - foundS1B);
                    adress_string = adress_string.Replace("0x", "");
                    address = Int32.Parse(adress_string, System.Globalization.NumberStyles.HexNumber);
                    if (address == -1)
                    {
                        bSearch = new byte[] { };
                        bReplace = new byte[] { };
                        _name = _name + " failing for unknown reason";
                        srp = new SearchReplacePattern(bSearch, bReplace, myCheckHeadAndTail);
                        return;
                    }
                }
                else
                {
                    string[] searchString = inputStr.Substring(foundS1, foundS2 - foundS1).Split(',');
                    bSearch = new byte[searchString.Length];
                    for (int i = 0; i < searchString.Length; i++)
                    {
                        bSearch[i] = Convert.ToByte(searchString[i].Trim(), 16);
                    }
                }
                inputStr = inputStr.Remove(0, foundS2 + 2);

                // Extract replace pattern
                foundS1 = inputStr.IndexOf('{') + 1;
                foundS2 = inputStr.IndexOf('}');
                string [] replaceString = inputStr.Substring(foundS1, foundS2 - foundS1).Split(',');
                bReplace = new byte[replaceString.Length];
                for (int i = 0; i < replaceString.Length; i++)
                {
                    bReplace[i] = Convert.ToByte(replaceString[i].Trim(), 16);
                }

                // Check that the search and replace match in length
                if ((address == -1) && (bSearch.Length != bReplace.Length))
                {
                    bSearch = new byte[] {};
                    bReplace = new byte[] {};
                    _name = _name + " failing due to mismatch in length";
                    srp = new SearchReplacePattern(bSearch, bReplace, myCheckHeadAndTail);
                    return;
                }

                if (address == -1) // No head/tail in a replace
                {
                    // Parse the head/tail arrays, e.g. {{{0xAA,0xBB},{0xCC,0xDD}},{{0xEE,0xFF},{0x11, 0x22}}}
                    // Remove leading "{{{"
                    inputStr = inputStr.Remove(0, foundS2 + 2);
                    for (int i = 0; i < 3; i++)
                        inputStr = inputStr.Remove(0, inputStr.IndexOf('{') + 1);

                    // Split the multiple headtails found by "}},{{". Whitespace already removed
                    string[] headTails = Regex.Split(inputStr, @"}},{{");

                    // Parse through the results, always start with a number or }
                    foreach (string headTail in headTails)
                    {
                        byte[] bHead = { };
                        byte[] bTail = { };
                        string ht = headTail;

                        // Extract the head
                        foundS1 = 0;
                        foundS2 = headTail.IndexOf('}');
                        string head = headTail.Substring(0, foundS2);

                        // Find the start of tail
                        foundS1 = ht.IndexOf('{');
                        ht = ht.Remove(0, foundS1 + 1);

                        // Find the end of the tail
                        foundS1 = 0;
                        foundS2 = ht.IndexOf('}');
                        string tail;
                        if (foundS2 <= 0) //All but last string lacks a }
                            tail = ht;
                        else
                            tail = ht.Substring(0, foundS2);

                        // Convert head to byte array
                        if (head.Length > 0)
                        {
                            if (head[0] == '*')
                            {
                                // This is as symbol, find it's flash address (XXXTBDXXX)
                                string searchSymbol = head.Substring(1);
                                foreach (SymbolHelper cfsh in m_symbols)
                                {
                                    if (cfsh.SmartVarname == searchSymbol)
                                    {
                                        bHead = BitConverter.GetBytes((int)cfsh.Flash_start_address);
                                        Array.Reverse(bHead);
                                        break;
                                    }
                                }
                            }
                            else
                            {
                                string[] lHead = head.Split(',');
                                bHead = new byte[lHead.Length];
                                for (int i = 0; i < lHead.Length; i++)
                                {
                                    if (lHead[i].Length > 0)
                                    {
                                        bHead[i] = Convert.ToByte(lHead[i].Trim(), 16);
                                    }
                                    else
                                    {
                                        bHead = new byte[] { };
                                        break;
                                    }
                                }
                            }
                        }

                        // Convert tail to byte array
                        if (tail.Length > 0)
                        {
                            if (tail[0] == '*')
                            {
                                // This is as symbol, find it's flash address (XXXTBDXXX)
                                string searchSymbol = tail.Substring(1);
                                foreach (SymbolHelper cfsh in m_symbols)
                                {
                                    if (cfsh.SmartVarname == searchSymbol)
                                    {
                                        bTail = BitConverter.GetBytes((int)cfsh.Flash_start_address);
                                        Array.Reverse(bTail);
                                        break;
                                    }
                                }
                            }
                            else
                            {
                                string[] lTail = tail.Split(',');
                                bTail = new byte[lTail.Length];
                                for (int i = 0; i < lTail.Length; i++)
                                {
                                    if (lTail[i].Length > 0)
                                    {
                                        bTail[i] = Convert.ToByte(lTail[i].Trim(), 16);
                                    }
                                    else
                                    {
                                        bTail = new byte[] { };
                                        break;
                                    }
                                }
                            }
                        }

                        // Add headtail to list
                        byte[][] bHeadTail = { bHead, bTail };
                        headTailList.Add(bHeadTail);
                    }

                    // Store headtail for output
                    myCheckHeadAndTail = new byte[headTailList.Count][][];
                    for (int i = 0; i < headTailList.Count; i++)
                    {
                        myCheckHeadAndTail[i] = headTailList[i];
                    }
                    srp = new SearchReplacePattern(bSearch, bReplace, myCheckHeadAndTail);
                }
                else
                {
                    srp = new SearchReplacePattern(address, bReplace);
                }
            }