Example #1
0
        public async Task <ScanResult[]> PerformScan(Stream input, ScanSettings settings, ScanProgress progress)
        {
            var _settings = settings.Split(Threads);

            scanners = Enumerable.Range(0, Threads).Select(x => new Scanner()).ToArray();
            var tasks = new Task <ScanResult[]> [Threads];

            for (int i = 0; i < tasks.Length; i++)
            {
                tasks[i] = scanners[i].PerformScan(input, _settings[i], progress);
            }

            var results = await Task.WhenAll(tasks);

            return(results.SelectMany(x => x).ToArray());
        }
        public async Task <ScanResult[]> PerformScan(Stream input, ScanSettings settings, ScanProgress progress)
        {
            token = new CancellationTokenSource();
            var results = await Task.Factory.StartNew <ScanResult[]>(() =>
            {
                var _results  = new List <ScanResult>();
                var buffer    = new byte[4096];
                var address   = settings.Address;
                var bytesLeft = settings.Size;
                var bytesRead = 0;

                while (bytesLeft > 0 && !token.IsCancellationRequested)
                {
                    lock (readLock)
                    {
                        input.Position = address;
                        bytesRead      = input.Read(buffer, 0, (int)Math.Min(buffer.Length, bytesLeft));
                    }
                    bytesLeft -= bytesRead;

                    if (bytesRead > 0)
                    {
                        for (int i = 0; i < bytesRead - settings.Pattern.Bytes.Length && !token.IsCancellationRequested; i++)
                        {
                            if (Matches(buffer, i, settings.Pattern))
                            {
                                var resultBuffer = new byte[settings.Pattern.Bytes.Length];
                                Array.Copy(buffer, i, resultBuffer, 0, resultBuffer.Length);
                                _results.Add(new ScanResult(address + i, resultBuffer));
                                progress.Found();
                            }
                        }
                    }

                    address += bytesRead;
                    progress.Progress(bytesRead);
                }

                return(_results.ToArray());
            });

            return(results);
        }
Example #3
0
        public ScanSettings[] Split(int count)
        {
            if (count <= 0)
            {
                throw new Exception("Invalid count of splits");
            }

            if (count == 1)
            {
                return new ScanSettings[] { this }
            }
            ;

            var settings = new ScanSettings[count];
            var size     = Size / count;

            for (int i = 0; i < settings.Length; i++)
            {
                settings[i] = new ScanSettings(Address + size * i, size, Pattern);
            }
            return(settings);
        }
    }