Esempio n. 1
0
        private static List <long> IndexOfSequence(FileStream fs, byte[] pattern, int startIndex, bool debug)
        {
            // Debug only variables
            long   readedtot     = 0;
            string percentString = "# Indexing all files - 0%";

            List <long> positions  = new List <long>();
            List <long> allToCheck = new List <long>();

            int buffLenght = 0x100000;

            byte[] buffer = new byte[buffLenght];

            int readed = fs.Read(buffer, 0, buffLenght);

            long ToRead = fs.Length;

            while (readed > 0)
            {
                long current = Array.IndexOf <byte>(buffer, pattern[0], startIndex);
                while (current >= 0 && current <= buffer.Length - pattern.Length)
                {
                    if ((buffer[current + 1] == pattern[1]) && (buffer[current + 2] == pattern[2]))
                    {
                        allToCheck.Add(current);
                    }
                    current = Array.IndexOf <byte>(buffer, pattern[0], (int)current + 1);
                }

                foreach (long toCheck in allToCheck)
                {
                    byte[] segment = new byte[pattern.Length];
                    Buffer.BlockCopy(buffer, (int)toCheck, segment, 0, pattern.Length);
                    //if (segment.SequenceEqual<byte>(pattern))
                    if (Linq.SequenceEqual(segment, pattern))
                    {
                        if (!positions.Contains(toCheck + readedtot))
                        {
                            positions.Add(toCheck + readedtot);
                        }
                    }
                }

                readedtot += (long)readed;

                if (debug)
                {
                    int percent = (int)((readedtot * 100) / ToRead);
                    if (percentString != "# Indexing all files - " + percent + "%")
                    {
                        percentString = "# Indexing all files - " + percent + "%";
                        Console.Write("\r" + percentString);
                    }
                }

                readed = fs.Read(buffer, 0, buffLenght);
            }

            return(positions);
        }
Esempio n. 2
0
        private static object SingleTR2HeaderAnalyzer(byte[] rawdata, int mode)
        {
            // Restituisce il nome
            if (mode == 0)
            {
                //byte[] endsequence = new byte[4] { 0x40, 0x00, 0x00, 0x00 };
                //long endposition = SingleIndexOfSequence(rawdata, endsequence, 0);
                byte[] rawtr2name = new byte[0x30];
                Buffer.BlockCopy(rawdata, 0, rawtr2name, 0, 0x30);
                string test = Encoding.UTF8.GetString(rawtr2name);
                return(test.Replace("\0", ""));
            }
            // Restituisce la dimensione
            else
            {
                byte[] rawtestdata = new byte[9];

                long endposition = SingleIndexOfSequence(rawdata, TR2Info.endingheader, 0);

                if (endposition > 0)
                {
                    Buffer.BlockCopy(rawdata, (int)endposition, rawtestdata, 0, 0x09);

                    if (Linq.SequenceEqual(rawtestdata, TR2Info.specialendingheader))
                    {
                        return(endposition + TR2Info.specialendingheader.Length);
                    }
                    return(endposition + TR2Info.endingheader.Length);
                }
                return(0);
            }
        }
Esempio n. 3
0
        // Trova una sequenza di byte all'interno di un array
        private static long SingleIndexOfSequence(byte[] buffer, byte[] pattern, int startIndex)
        {
            long        position   = -1;
            List <long> allToCheck = new List <long>();

            long current = Array.IndexOf <byte>(buffer, pattern[0], startIndex);

            while (current >= 0 && current <= buffer.Length - pattern.Length)
            {
                if ((buffer[current + 1] == pattern[1]) && (buffer[current + 2] == pattern[2]))
                {
                    allToCheck.Add(current);
                }
                current = Array.IndexOf <byte>(buffer, pattern[0], (int)current + 1);
            }

            foreach (long toCheck in allToCheck)
            {
                byte[] segment = new byte[pattern.Length];
                Buffer.BlockCopy(buffer, (int)toCheck, segment, 0, pattern.Length);
                if (Linq.SequenceEqual(segment, pattern))
                {
                    return(toCheck);
                }
            }

            return(position);
        }
Esempio n. 4
0
        private static long IndexOfSequence(FileStream fs, byte[] pattern, int startIndex)
        {
            long        readedtot  = 0;
            List <long> allToCheck = new List <long>();

            int buffLenght = 0x100000;

            byte[] buffer = new byte[buffLenght];

            int readed = fs.Read(buffer, 0, buffLenght);

            long ToRead = fs.Length;

            while (readed > 0)
            {
                long current = Array.IndexOf <byte>(buffer, pattern[0], startIndex);
                while (current >= 0 && current <= buffer.Length - pattern.Length)
                {
                    if ((buffer[current + 1] == pattern[1]) && (buffer[current + 2] == pattern[2]))
                    {
                        allToCheck.Add(current);
                    }
                    current = Array.IndexOf <byte>(buffer, pattern[0], (int)current + 1);
                }

                foreach (long toCheck in allToCheck)
                {
                    byte[] segment = new byte[pattern.Length];
                    Buffer.BlockCopy(buffer, (int)toCheck, segment, 0, pattern.Length);
                    if (Linq.SequenceEqual(segment, pattern))
                    {
                        return(toCheck + readedtot);
                    }
                }

                readedtot += (long)readed;
                readed     = fs.Read(buffer, 0, buffLenght);
            }

            return(-1);
        }
Esempio n. 5
0
        private static List <List <long> > MultiIndexOfSequenceSpecial(FileStream fs, List <byte[]> patterns, int startIndex, List <Dictionary <long, byte> > specials, bool debug)
        {
            // Debug only variables
            long   readedtot     = 0;
            string percentString = "# Indexing all files - 0%";

            List <List <long> > allpositions  = new List <List <long> >();
            List <List <long> > allallToCheck = new List <List <long> >();

            int buffLenght = 0x100000;

            byte[] buffer = new byte[buffLenght];

            int readed = fs.Read(buffer, 0, buffLenght);

            for (int i = 0; i < patterns.Count; i++)
            {
                allpositions.Add(new List <long>());
            }

            long ToRead = fs.Length;

            while (readed > 0)
            {
                allallToCheck.Clear();
                for (int i = 0; i < patterns.Count; i++)
                {
                    allallToCheck.Add(new List <long>());
                    long current = Array.IndexOf <byte>(buffer, patterns[i][0], startIndex);
                    while (current >= 0 && current <= buffer.Length - patterns[i].Length)
                    {
                        if ((buffer[current + 1] == patterns[i][1]) && (buffer[current + 2] == patterns[i][2]))
                        {
                            allallToCheck[i].Add(current);
                        }
                        current = Array.IndexOf <byte>(buffer, patterns[i][0], (int)current + 1);
                    }
                }

                for (int i = 0; i < patterns.Count; i++)
                {
                    if (allallToCheck[i].Count > 0)
                    {
                        foreach (long toCheck in allallToCheck[i])
                        {
                            byte[] segment = new byte[patterns[i].Length];
                            Buffer.BlockCopy(buffer, (int)toCheck, segment, 0, patterns[i].Length);

                            if (Linq.SequenceEqual(segment, patterns[i]))
                            {
                                bool toAdd = true;
                                if (specials[i].Count > 0)
                                {
                                    foreach (KeyValuePair <long, byte> kvp in specials[i])
                                    {
                                        if (buffer[toCheck + kvp.Key] != kvp.Value)
                                        {
                                            toAdd = false;
                                            break;
                                        }
                                    }
                                }
                                if (toAdd)
                                {
                                    if ((toCheck + readedtot) < fs.Length)
                                    {
                                        if (!allpositions[i].Contains(toCheck + readedtot))
                                        {
                                            allpositions[i].Add(toCheck + readedtot);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                readedtot += (long)readed;

                if (debug)
                {
                    int percent = (int)((readedtot * 100) / ToRead);
                    if (percentString != "# Indexing all files - " + percent + "%")
                    {
                        percentString = "# Indexing all files - " + percent + "%";
                        Console.Write("\r" + percentString);
                    }
                }
                readed = fs.Read(buffer, 0, buffLenght);
            }

            return(allpositions);
        }