Пример #1
0
        /// This is the core method overridden by all subclasses of Region
        // TODO: M: very inefficient if no matches found - will iterate over WORDs to end of file
        protected override void ProcessInternal(CustomBinaryReader reader)
        {
            WriteLine(LogLevel.DEBUG, "Processing...");
            SignatureRegion.Reset();
            Validator.Reset();

            var unmatchedRegions = 0;

            // in this case we read a magic and try to process it
            while (true)
            {
                byte[] magic     = new byte[2];
                int    bytesRead = reader.BaseStream.Read(magic, 0, 2);
                long   magicPos  = reader.BaseStream.Position - 2;

                if (bytesRead == 0)
                {
                    // end of file - nothing more to read
                    break;
                }

                if (bytesRead == 1)
                {
                    // this can happen if zipping over unmatched bytes at end of file - should handle better
                    if (DataFile.StrictProcessing)
                    {
                        throw new InvalidOperationException("Could only read one byte of identifier at end of stream");
                    }
                    break;
                }

                // test whether the magic matches one of our child objects
                string magicString = string.Format("0x{0:X2}{1:X2}", magic[0], magic[1]);
                bool   matched     = false;
                foreach (IdentifiedObjectRegion r in regions)
                {
                    if (r.Matches(magicString))
                    {
                        WriteLine(LogLevel.DEBUG, "Identified region: {0} with magic {1} at 0x{2:X4}", r.Name, magicString, magicPos);
                        var newRegion = r.Copy();
                        newRegion.Process(reader);
                        this.ProcessedRegions.Add(newRegion);
                        matched = true;
                        break;
                    }
                }

                if (!matched)
                {
                    unmatchedRegions++;
                    if (unmatchedRegions == 1)
                    {
                        WriteLine(LogLevel.WARN, "First unrecognised region with magic {1} at 0x{1:X4}", magicString, magicPos);
                    }
                }

                if (!matched && DataFile.StrictProcessing)
                {
                    WriteLine(LogLevel.WARN, "Unrecognized magic=0x{0:X2}{1:X2} at offset 0x{2:X4}  ", magic[0], magic[1], magicPos);
                    throw new NotImplementedException("Unrecognized magic " + magicString);
                }
            }
            if (unmatchedRegions > 0)
            {
                WriteLine(LogLevel.WARN, "There were {0} unmatched regions (magics) in the file.", unmatchedRegions);
            }

            // ensure that all ElementaryFileRegion has signature present
            for (var i = 0; i < this.ProcessedRegions.Count; i++)
            {
                var region = this.ProcessedRegions[i];
                if (region is ElementaryFileRegion)
                {
                    if (((ElementaryFileRegion)region).Unsigned || !Validator.ValidateSignatures)
                    {
                        continue;
                    }

                    if (i + 1 < this.ProcessedRegions.Count)
                    {
                        var nextRegion = this.ProcessedRegions[i + 1];
                        if (region.Name == nextRegion.Name && ((ElementaryFileRegion)nextRegion).signature != null)
                        {
                            i++;
                            continue;
                        }
                    }

                    throw new InvalidSignatureException(string.Format("No signature present for {0}!", region.Name));
                }
            }

            Validator.CheckIfValidated(SignatureRegion.newestDateTime);
            WriteLine(LogLevel.DEBUG, "Processing done.");
        }