Exemplo n.º 1
0
        public static ReaderParameters GetReaderParameters(this IScanContext context)
        {
            var provide = context.Options.Find <IProvideReaderParameters>();

            if (provide == null)
            {
                provide = new ProvideReaderParameters();
                context.Options.Add(provide);
            }

            var parameters = provide.ReaderParameters;

            if (parameters == null)
            {
                parameters = new ReaderParameters();
                provide.ReaderParameters = parameters;
            }

            if (parameters.AssemblyResolver == null)
            {
                parameters.AssemblyResolver = new DefaultAssemblyResolver();
            }

            return(parameters);
        }
Exemplo n.º 2
0
        public override IEnumerable <T> Scan(IScanContext context)
        {
            var items   = GetParentItemsOrEmpty(context);
            var results = items.Select(MapItem);

            return(results);
        }
Exemplo n.º 3
0
        public override IEnumerable <T> Scan(IScanContext context)
        {
            var items   = GetParentItemsOrEmpty(context);
            var results = items.Distinct(Comparer);

            return(results);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Helper function for child scanner implementations that will always
        /// returns a valid non-null collection of items from the parent <see cref="IScanner{TIn}"/>.
        /// </summary>
        /// <param name="context">An <see cref="IScanContext"/> that provides scanning options at runtime.</param>
        /// <returns>An <see cref="IEnumerable{T}"/> that contains the items provided by the <see cref="Parent"/> scanner.</returns>
        protected virtual IEnumerable <TIn> GetParentItemsOrEmpty(IScanContext context)
        {
            var parent = Parent ?? context.Factory.Empty <TIn>();
            var items  = parent.Scan(context) ?? Enumerable.Empty <TIn>();

            return(items);
        }
Exemplo n.º 5
0
        public override IEnumerable <TOut> Scan(IScanContext context)
        {
            var items   = GetParentItemsOrEmpty(context);
            var results = items.SelectMany(item => _transform(context, item));

            return(results);
        }
Exemplo n.º 6
0
        public static IEnumerable <Assembly> LoadAssembly(IScanContext context, AssemblyName assemblyName)
        {
            if (assemblyName == null)
            {
                return(Enumerable.Empty <Assembly>());
            }

            try
            {
                var assembly = Assembly.Load(assemblyName);
                return(new[] { assembly });
            }
            catch (BadImageFormatException)
            {
                // skip on error
            }
            catch (FileNotFoundException)
            {
                // skip on error
            }
            catch (FileLoadException)
            {
                // skip on error
            }
            catch (Exception exception)
            {
                var ignore = context.Options.FindAll <ITransformIgnoreException>();
                if (!ignore.Any(_ => _.IgnoreException("LoadAssembly", exception)))
                {
                    throw;
                }
            }

            return(Enumerable.Empty <Assembly>());
        }
Exemplo n.º 7
0
 public Mpeg2SystemCarver(IMpeg2SystemReader reader, IResultParser <IMpeg2SystemReader> systemHeaderParser, IScanContext scanContext)
 {
     _reader             = reader;
     _systemHeaderParser = systemHeaderParser;
     _scanContext        = scanContext;
     _state          = reader.State;
     _minHeaderCount = (uint)Mpeg2SystemDetector.Configurable[Mpeg2SystemDetector.ConfigurationKey.MinSystemHeaderCount];
 }
Exemplo n.º 8
0
 public Mpeg2VideoCarver(IMpeg2VideoReader reader, IResultParser <IMpeg2VideoReader> videoHeaderParser, IScanContext scanContext)
 {
     _reader            = reader;
     _videoHeaderParser = videoHeaderParser;
     _scanContext       = scanContext;
     _state             = reader.State;
     _minHeaderCount    = (uint)Mpeg2VideoDetector.Configurable[Mpeg2VideoDetector.ConfigurationKey.MinVideoHeaderCount];
 }
Exemplo n.º 9
0
        public override IEnumerable <T> Scan(IScanContext context)
        {
            var items  = GetParentItemsOrEmpty(context);
            var result = items
                         .Where(item => _includeList.Any(predicate => predicate(context, item)))
                         .Where(item => !_excludeList.Any(predicate => predicate(context, item)));

            return(result);
        }
Exemplo n.º 10
0
        public static TypeDefinition ResolveTypeDefinition(this IScanContext context, Type type)
        {
            var cache = context.Options.Find <TypeDefinitionCache>();

            if (cache == null)
            {
                cache = new TypeDefinitionCache();
                context.Options.Add(cache);
            }
            return(cache.ResolveTypeDefinition(context, type));
        }
        /// <summary>
        /// Gets the data for the given <paramref name="codecStream"/> and
        /// reports progress to the given <paramref name="progressReporter"/>.
        /// This rescans the parent (container) of the codecStream to determine
        /// the data of codecStream.
        /// </summary>
        /// <param name="codecStream">the codec stream to retrieve the data for</param>
        /// <param name="progressReporter">the progress reporter</param>
        /// <param name="dataReaderPool">The shared pool of file data readers</param>
        /// <returns>the root node of the results</returns>
        public IDataPacket GetData(ICodecStream codecStream, IProgressReporter progressReporter, IDataReaderPool dataReaderPool)
        {
            IDataPacket data;

            if (_dataCache.TryGetValue(codecStream, out data))
            {
                return(new RescannedCodecStream(codecStream, data));
            }

            IDataBlock containerStreamDataBlock = codecStream.DataBlock;

            using (IDataReader dataReader = new ProgressDataReader(new FragmentedDataReader(containerStreamDataBlock, dataReaderPool), progressReporter))
            {
                IDetector    containerDetector = containerStreamDataBlock.Detectors.FirstOrDefault();
                IScanContext scanContext       = _createScanContext(containerStreamDataBlock.InputFile.Project);
                scanContext.Detectors = new[] { containerDetector };
                IDataBlockBuilder containerStreamDataBlockBuilder = _createDataBlockBuilder();
                containerStreamDataBlockBuilder.Detectors = scanContext.Detectors;
                containerStreamDataBlockBuilder.InputFile = containerStreamDataBlock.InputFile;
                IDataBlock newContainerStream = containerDetector.DetectData(dataReader, containerStreamDataBlockBuilder, scanContext);

                // Check cancellation request
                if (progressReporter.CancellationPending)
                {
                    return(null);
                }

                // Check results / container stream
                if (newContainerStream == null)
                {
                    RetrievedDetectableIsDifferentThanTheOneRetrievedDuringTheInitialScan();
                    return(null);
                }

                data = newContainerStream.CodecStreams.Where(c => c.StreamNumber == codecStream.StreamNumber).FirstOrDefault();

                if ((data == null) || (data.Length < codecStream.EndOffset))
                {
                    RetrievedDetectableIsDifferentThanTheOneRetrievedDuringTheInitialScan();
                    return(null);
                }

                data = data.GetSubPacket(codecStream.StartOffset, codecStream.Length);
            }

            CacheData(codecStream, data);

            return(new RescannedCodecStream(codecStream, data));
        }
Exemplo n.º 12
0
        private bool GetNextDataBlock(IInputFile file, IDataReader dataReader, IScanContext scanContext, out IDataBlock volBlock, out IDataBlockBuilder volBuilder)
        {
            IDataBlock        volBlockAttempt        = null;
            IDataBlockBuilder volBlockAttemptBuilder = null;

            while (volBlockAttempt == null && dataReader.State == DataReaderState.Ready)
            {
                volBlockAttemptBuilder = CreateBuilder(file);
                scanContext.Detectors  = new[] { _detector };
                volBlockAttempt        = _detector.DetectData(dataReader, volBlockAttemptBuilder, scanContext);
            }
            volBlock   = volBlockAttempt;
            volBuilder = volBlockAttemptBuilder;
            return(volBlock != null);
        }
Exemplo n.º 13
0
        public static IEnumerable <AssemblyDefinition> ReadAssembly(IScanContext context, FileInfo fileInfo)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }
            if (fileInfo == null)
            {
                throw new ArgumentNullException(nameof(fileInfo));
            }

            var path     = fileInfo.FullName;
            var assembly = ReadAssembly(context, path);

            return(assembly);
        }
Exemplo n.º 14
0
        public override IEnumerable <T> Scan(IScanContext context)
        {
            var cache = _cache;

            if (cache != null)
            {
                return(cache);
            }

            var items = GetParentItemsOrEmpty(context);

            cache  = items.ToArray();
            _cache = cache;

            return(cache);
        }
        public H264ResultTreeBuilder(BitStreamDataReader dataReader, IH264Reader reader, IH264State state, IScanContext scanContext, NalUnitStreamParser nalUnitStreamParser, ByteStreamParser byteStreamParser)
        {
            _falseHitReduction   = (bool)H264Detector.Configurable[H264Detector.ConfigurationKey.FalseHitReduction];
            _dataReader          = dataReader;
            _reader              = reader;
            _state               = state;
            _scanContext         = scanContext;
            _nalUnitStreamParser = nalUnitStreamParser;
            _byteStreamParser    = byteStreamParser;

            Configurable <H264Detector.ConfigurationKey> configurable = H264Detector.Configurable;

            _maxGapBetweenPpsAndSlice = (uint)configurable[H264Detector.ConfigurationKey.MaxGapBetweenPpsAndSlice];
            _maxGapBetweenPpsAndSps   = (uint)configurable[H264Detector.ConfigurationKey.MaxGapBetweenPpsAndSps];
            _maxGapBetweenNalUnits    = (uint)configurable[H264Detector.ConfigurationKey.MaxGapBetweenNalUnits];
        }
Exemplo n.º 16
0
        public TypeDefinition ResolveTypeDefinition(IScanContext context, Type type)
        {
            var token          = new MetadataToken((uint)type.MetadataToken);
            var typeDefinition = _cache[token] as TypeDefinition;

            if (typeDefinition == null)
            {
                var parameters    = context.GetReaderParameters();
                var assembly      = AssemblyDefinition.ReadAssembly(type.Assembly.Location, parameters);
                var typeReference = assembly.MainModule.GetTypes().Single(_ => _.MetadataToken == token);
                typeDefinition = typeReference.Resolve();
                _cache[token]  = typeDefinition;
            }

            return(typeDefinition);
        }
Exemplo n.º 17
0
        public static IEnumerable <AssemblyDefinition> ReadAssembly(IScanContext context, AssemblyNameReference assemblyName)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }
            if (assemblyName == null)
            {
                throw new ArgumentNullException(nameof(assemblyName));
            }

            var parameters = context.GetReaderParameters();
            var resolver   = parameters.AssemblyResolver ?? new DefaultAssemblyResolver();
            var assembly   = resolver.Resolve(assemblyName, parameters);

            return(new[] { assembly });
        }
Exemplo n.º 18
0
        public ReaderState(IActiveState activeState, IResultNodeCallback resultNodeCallback, IResultNode rootResultNode, IResultMetadata resultMetadata, IScanContext scanContext,
                           Creator <IResultNodeBuilder> createResultNodeBuilder, Creator <IResultNodeState, IState, IResultNodeBuilder> createResultNodeReaderState)
        {
            _activeState                 = activeState;
            _resultNodeCallback          = resultNodeCallback;
            _rootResultNode              = rootResultNode;
            _resultMetadata              = resultMetadata;
            _scanContext                 = scanContext;
            _createResultNodeBuilder     = createResultNodeBuilder;
            _createResultNodeReaderState = createResultNodeReaderState;

            _scanContext.Results = rootResultNode;

            Valid = true;

            _activeState.ChangeState(this);
        }
Exemplo n.º 19
0
        public static IEnumerable <FileInfo> EnumerateFiles(IScanContext context, string directory, string pattern, SearchOption option)
        {
            if (String.IsNullOrEmpty(directory))
            {
                return(Enumerable.Empty <FileInfo>());
            }

            if (String.IsNullOrEmpty(pattern))
            {
                return(Enumerable.Empty <FileInfo>());
            }

            try
            {
                var info = new DirectoryInfo(directory);
                return(!info.Exists
                                        ? Enumerable.Empty <FileInfo>()
                                        : info.EnumerateFiles(pattern, option));
            }
            catch (UnauthorizedAccessException)
            {
                // skip on error
            }
            catch (DirectoryNotFoundException)
            {
                // skip on error
            }
            catch (SecurityException)
            {
                // skip on error
            }
            catch (IOException)
            {
                // skip on error
            }
            catch (Exception exception)
            {
                var ignore = context.Options.FindAll <ITransformIgnoreException>();
                if (!ignore.Any(_ => _.IgnoreException("EnumerateFiles", exception)))
                {
                    throw;
                }
            }

            return(Enumerable.Empty <FileInfo>());
        }
 public void TestConstructorDefaultFormat()
 {
     using (IDataReader dataReader = new MockDataReader(new byte[DataFileLength], _inputFile))
     {
         IDetector    detector    = new UnknownFormatDetector();
         IScanContext scanContext = TestFramework.CreateScanContext(_project);
         scanContext.Detectors = new[] { detector };
         IDataBlockBuilder builder = TestFramework.CreateDataBlockBuilder();
         builder.Detectors = scanContext.Detectors;
         builder.InputFile = _inputFile;
         IDataBlock dataBlock = detector.DetectData(dataReader, builder, scanContext);
         Assert.IsNotNull(dataBlock);
         IResultNode results = scanContext.Results;
         Assert.IsNotNull(results);
         Assert.GreaterOrEqual(results.Children.Count, 1);
         Assert.AreEqual(0, results.Children[0].Attributes.Count);
     }
 }
Exemplo n.º 21
0
        public static IEnumerable <AssemblyDefinition> ReadAssembly(IScanContext context, string path)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }
            if (path == null)
            {
                throw new ArgumentNullException(nameof(path));
            }

            try
            {
                var parameters = context.GetReaderParameters();
                var assembly   = AssemblyDefinition.ReadAssembly(path, parameters);
                return(new[] { assembly });
            }
            catch (BadImageFormatException)
            {
                // skip on error
            }
            catch (DirectoryNotFoundException)
            {
                // skip on error
            }
            catch (FileNotFoundException)
            {
                // skip on error
            }
            catch (FileLoadException)
            {
                // skip on error
            }
            catch (Exception exception)
            {
                var ignore = context.Options.FindAll <ITransformIgnoreException>();
                if (!ignore.Any(_ => _.IgnoreException("ReadAssembly", exception)))
                {
                    throw;
                }
            }

            return(Enumerable.Empty <AssemblyDefinition>());
        }
Exemplo n.º 22
0
        /// <summary>
        /// You do not need a project file for this kind of scan
        /// </summary>
        /// <param name="byteStream"></param>
        public DataBlockVerification Scan(byte[] byteStream)
        {
            IScanContext scanContext = TestFramework.CreateScanContext(_project);
            IDataBlock   dataBlock   = ScanDataBlock(byteStream, scanContext);

            Assert.IsNotNull(dataBlock);

            // Data block detected, perform a rescan for the results
            IScanContext rescanContext = TestFramework.CreateScanContext(_project);

            rescanContext.Detectors = dataBlock.Detectors;
            var detectedByteStream = new byte[dataBlock.Length];

            Array.Copy(byteStream, dataBlock.StartOffset, detectedByteStream, 0, detectedByteStream.Length);
            ScanDataBlock(detectedByteStream, rescanContext);
            Assert.IsNotNull(rescanContext.Results);

            return(new DataBlockVerification(dataBlock, rescanContext.Results, "using " + _videoDetector.Name + " detected datablock within memory buffer of size " + byteStream.Length + "." + TestFramework.GetDescriptionText(rescanContext.Results)));
        }
Exemplo n.º 23
0
        private IDataBlock ScanDataBlock(byte[] byteStream, IScanContext scanContext)
        {
            scanContext.Detectors = new[] { _videoDetector };
            IDataBlockBuilder builder = TestFramework.CreateDataBlockBuilder();

            builder.Detectors = scanContext.Detectors;
            var inputFile = _mockRepository.StrictMock <IInputFile>();

            With.Mocks(_mockRepository).Expecting(delegate
            {
                SetupResult.For(inputFile.Name).Return("<ByteArrayDataReader>");
                SetupResult.For(inputFile.Length).Return(byteStream.Length);
            });
            builder.InputFile = inputFile;
            var        mockDataReader = new MockDataReader(byteStream, inputFile);
            IDataBlock dataBlock      = _videoDetector.DetectData(mockDataReader, builder, scanContext);

            Assert.IsNotNull(dataBlock);
            return(dataBlock);
        }
Exemplo n.º 24
0
        private void BuildReferenceHeaders(IScanContext context, IDataBlockBuilder dataBlockBuilder)
        {
            var referenceHeaders = new Dictionary <IDataPacket, IPictureState>();

            foreach (IReferenceHeader header in ReferenceHeaders)
            {
                IInputFile headerFile = context.CreateReferenceHeaderFile(header);
                using (var reader = headerFile.CreateDataReader())
                {
                    var headerDataBlock = _carver.Carve(new BitStreamDataReader(reader), dataBlockBuilder, context);
                    if (headerDataBlock != null)
                    {
                        referenceHeaders[headerFile.CreateDataPacket()] = context.ReferenceHeader as IPictureState;
                    }
                }
            }

            // Set reference header and reset other state
            context.ReferenceHeader = referenceHeaders;
            context.Results         = null;
            context.IsFragmented    = false;
        }
Exemplo n.º 25
0
        public static IEnumerable <AssemblyDefinition> ReadAssembly(IScanContext context, AssemblyName assemblyName)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }
            if (assemblyName == null)
            {
                throw new ArgumentNullException(nameof(assemblyName));
            }

            var path = assemblyName.CodeBase;

            if (!String.IsNullOrEmpty(path) && File.Exists(path))
            {
                return(ReadAssembly(context, path));
            }

            var assemblyNameDefinition = assemblyName.GetAssemblyNameDefinition();
            var assembly = ReadAssembly(context, assemblyNameDefinition);

            return(assembly);
        }
 public void TestDetectData()
 {
     using (IDataReader dataReader = new MockDataReader(new byte[DataFileLength], _inputFile))
     {
         IScanContext scanContext = TestFramework.CreateScanContext(_project);
         scanContext.Detectors = new[] { _detector };
         IDataBlockBuilder builder = TestFramework.CreateDataBlockBuilder();
         builder.Detectors = scanContext.Detectors;
         builder.InputFile = _inputFile;
         IDataBlock dataBlock = _detector.DetectData(dataReader, builder, scanContext);
         Assert.IsNotNull(dataBlock, "Unknown format detector (DetectData not null)");
         IResultNode results = scanContext.Results;
         Assert.IsNotNull(results, "Unknown format detector (Results not null)");
         Assert.AreEqual(_detector, dataBlock.Detectors.First(), "Detector of data block");
         Assert.AreEqual(1, results.Children.Count, "One child 'Data'");
         IResultNode data = results.Children[0];
         Assert.IsEmpty((ICollection)data.Children, "Data has no children");
         Assert.AreEqual(0, data.StartOffset, "");
         Assert.AreEqual(DataFileLength, data.Length, "");
         Assert.IsFalse(data.IsFragmented(), "Single fragment");
         Assert.AreEqual(_inputFile, data.InputFile, "Input file");
         Assert.IsFalse(dataBlock.IsFullFile, "Does not produce full files");
     }
 }
Exemplo n.º 27
0
        public static IEnumerable <AssemblyName> GetAssemblyName(IScanContext context, FileInfo fileInfo)
        {
            if (fileInfo == null || !fileInfo.Exists)
            {
                return(Enumerable.Empty <AssemblyName>());
            }

            try
            {
                var path         = fileInfo.FullName;
                var assemblyName = AssemblyName.GetAssemblyName(path);
                return(new[] { assemblyName });
            }
            catch (BadImageFormatException)
            {
                // skip on error
            }
            catch (FileNotFoundException)
            {
                // skip on error
            }
            catch (FileLoadException)
            {
                // skip on error
            }
            catch (Exception exception)
            {
                var ignore = context.Options.FindAll <ITransformIgnoreException>();
                if (!ignore.Any(_ => _.IgnoreException("GetAssemblyName", exception)))
                {
                    throw;
                }
            }

            return(Enumerable.Empty <AssemblyName>());
        }
Exemplo n.º 28
0
            /// <summary>Detects the next data block.</summary>
            /// <param name="dataReader">the data to scan</param>
            /// <param name="dataBlockBuilder">the builder for creating the data block</param>
            /// <returns>the data block that was detected; <c>null</c> for none</returns>
            public void DetectNextDataBlock(IDataReader dataReader, IDataBlockBuilder dataBlockBuilder)
            {
                IInputFile   inputFile   = dataReader.GetDataPacket(Position, 1).InputFile;
                IScanContext scanContext = _createScanContext(inputFile.Project);

                scanContext.Detectors             = new[] { /*?*/ Detector };
                dataBlockBuilder.InputFile        = inputFile;
                dataBlockBuilder.PreviousFragment = LastDataBlock;
                dataBlockBuilder.Detectors        = scanContext.Detectors;
                LastDataBlock = Detector.DetectData(dataReader, dataBlockBuilder, scanContext);
                Position      = ((LastDataBlock == null) || _dataScanner.AllowOverlap) ? dataReader.Position : LastDataBlock.EndOffset;

                if (!_dataScanner.AllowOverlap && (LastDataBlock != null))
                {
                    if (OverlapsWithLargerDataBlock())
                    {
                        DiscardLastDataBlock();
                    }
                    else
                    {
                        DiscardOverlappingDataBlocks();
                    }
                }
            }
Exemplo n.º 29
0
 public override IEnumerable <T> Scan(IScanContext context)
 {
     return(GetParentItemsOrEmpty(context));
 }
Exemplo n.º 30
0
 public override IEnumerable <T> Scan(IScanContext context)
 {
     return(Enumerable.Empty <T>());
 }