Esempio n. 1
0
        protected override void ProcessInternal(CustomBinaryReader reader)
        {
            // read the type
            byte type = reader.ReadByte();

            regionLength = reader.ReadSInt16();
            long fileLength = regionLength;

            if (type == 0x01)
            {
                // this is just the signature
                this.signature = reader.ReadBytes((int)fileLength);
                fileLength     = 0;

                long currentOffset = reader.BaseStream.Position;

                reader.BaseStream.Position = SignatureRegion.signedDataOffsetBegin;
                Validator.ValidateDelayedGen1(reader.ReadBytes(SignatureRegion.GetSignedDataLength()), this.signature);

                reader.BaseStream.Position = currentOffset;
            }
            else
            {
                long start = reader.BaseStream.Position;

                base.ProcessInternal(reader);

                long amountProcessed = reader.BaseStream.Position - start;
                fileLength -= amountProcessed;

                if (this.Name == "CardCertificate")
                {
                    Validator.SetCertificate(this);
                }
                else if (this.Name == "CACertificate")
                {
                    Validator.SetCACertificate(this);
                }
                ;
            }

            if (fileLength > 0)
            {
                // deal with a remaining fileLength that is greater than int
                while (fileLength > int.MaxValue)
                {
                    reader.ReadBytes(int.MaxValue);
                    fileLength -= int.MaxValue;
                }
                reader.ReadBytes((int)fileLength);
            }
        }
        protected override void ProcessInternal(CustomBinaryReader reader)
        {
            SignatureRegion.signedDataOffsetEnd = reader.BaseStream.Position;

            base.ProcessInternal(reader);

            long currentOffset = reader.BaseStream.Position;

            reader.BaseStream.Position = SignatureRegion.signedDataOffsetBegin;
            Validator.ValidateGen1(reader.ReadBytes(SignatureRegion.GetSignedDataLength()), this.ToBytes(), SignatureRegion.newestDateTime);

            reader.BaseStream.Position = currentOffset;
        }
        protected override void ProcessInternal(CustomBinaryReader reader)
        {
            previousRecordLength = reader.ReadSInt16();
            currentRecordLength  = reader.ReadSInt16();
            recordDate           = reader.ReadTimeReal();
            SignatureRegion.UpdateTime(recordDate);
            dailyPresenceCounter = reader.ReadBCDString(2);
            distance             = reader.ReadSInt16();

            uint recordCount = (currentRecordLength - 12) / 2;

            this.ProcessedRegions.Capacity = (int)recordCount;

            WriteLine(LogLevel.DEBUG, "Reading {0} activity records", recordCount);

            while (recordCount > 0)
            {
                ActivityChangeRegion acr = new ActivityChangeRegion();
                acr.Name = "ActivityChangeInfo";
                acr.Process(reader);
                this.ProcessedRegions.Add(acr);
                recordCount--;
            }
        }
Esempio n. 4
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.");
        }
 public static void UpdateTime(DateTime dateTime)
 {
     SignatureRegion.UpdateTime((DateTimeOffset)DateTime.SpecifyKind(dateTime, DateTimeKind.Utc));
 }
Esempio n. 6
0
        protected override void ProcessInternal(CustomBinaryReader reader)
        {
            // read the type
            byte type = reader.ReadByte();

            regionLength = reader.ReadSInt16();
            long fileLength = regionLength;

            long start = reader.BaseStream.Position;

            if (DataFile.StrictProcessing && start + regionLength > reader.BaseStream.Length)
            {
                throw new InvalidOperationException(string.Format("{0}: Would try to read more than length of stream! Position 0x{1:X4} + RegionLength 0x{2:X4} > Length 0x{3:X4}", Name, start, regionLength, reader.BaseStream.Length));
            }

            if (type == 0x01)
            {
                // this is just the signature
                this.signature = reader.ReadBytes((int)fileLength);
                fileLength     = 0;

                long currentOffset = reader.BaseStream.Position;

                reader.BaseStream.Position = SignatureRegion.signedDataOffsetBegin;
                Validator.ValidateDelayedGen1(reader.ReadBytes(SignatureRegion.GetSignedDataLength()), this.signature, () => { return(SignatureRegion.newestDateTime); });

                reader.BaseStream.Position = currentOffset;
            }
            else
            {
                base.ProcessInternal(reader);

                long amountProcessed = reader.BaseStream.Position - start;
                fileLength -= amountProcessed;

                if (this.Name == "CardCertificate")
                {
                    Validator.SetCertificate(this);
                }
                else if (this.Name == "CACertificate")
                {
                    Validator.SetCACertificate(this);
                }
                ;
            }

            if (DataFile.StrictProcessing && fileLength != 0)
            {
                throw new InvalidOperationException(string.Format("{0}: Read wrong number of bytes! Position 0x{1:X4} and end of region 0x{2:X4} but bytes left 0x{3:X4}", Name, reader.BaseStream.Position, start + regionLength, fileLength));
            }

            if (DataFile.StrictProcessing && reader.BaseStream.Position > start + regionLength)
            {
                throw new InvalidOperationException(string.Format("{0}: Read past end of region! Position 0x{1:X4} > end of region 0x{2:X4}", Name, reader.BaseStream.Position, start + regionLength));
            }

            if (fileLength > 0)
            {
                // deal with a remaining fileLength that is greater than int
                while (fileLength > int.MaxValue)
                {
                    reader.ReadBytes(int.MaxValue);
                    fileLength -= int.MaxValue;
                }
                reader.ReadBytes((int)fileLength);
            }
        }
Esempio n. 7
0
 protected override void ProcessInternal(CustomBinaryReader reader)
 {
     dateTime = reader.ReadTimeReal();
     SignatureRegion.UpdateTime(dateTime);
 }