예제 #1
0
        public LogicalRecordSegment ReadLogicalRecordSegment(Stream dlisStream)
        {
            if (dlisStream == null)
            {
                return(null);
            }
            if (dlisStream.IsAtEndOfStream())
            {
                return(null);
            }

            var header = _logicalRecordSegmentHeaderBusiness.ReadLogicalRecordSegmentHeader(dlisStream);

            if (header == null)
            {
                return(null);
            }

            var bodySize = header.LogicalRecordSegmentLength - 4;

            LogicalRecordSegmentEncryptionPacket encryptionPacket = null;

            if (header.EncryptionPacket)
            {
                encryptionPacket = _logicalRecordSegmentEncryptionPacketBusiness.ReadLogicalRecordSegmentEncryptionPacket(dlisStream);
                if (encryptionPacket == null)
                {
                    return(null);
                }
                bodySize -= encryptionPacket.Size;
            }

            LogicalRecordSegmentTrailer trailer = null;
            var trailerSize = 0;

            if (header.HasTrailer())
            {
                dlisStream.Seek(bodySize, SeekOrigin.Current);
                trailer = _logicalRecordSegmentTrailerBusiness.ReadLogicalRecordSegmentTrailer(dlisStream, header);
                if (trailer == null)
                {
                    return(null);
                }
                trailerSize = header.TrailerSize(trailer);
                bodySize   -= trailerSize;
                dlisStream.Seek(-(bodySize + trailerSize), SeekOrigin.Current);
            }

            var body = dlisStream.ReadBytes(bodySize);

            dlisStream.Seek(trailerSize, SeekOrigin.Current);

            return(new LogicalRecordSegment
            {
                Header = header,
                EncryptionPacket = encryptionPacket,
                Body = body,
                Trailer = trailer
            });
        }
        public static int TrailerSize(this LogicalRecordSegmentHeader header, LogicalRecordSegmentTrailer trailer)
        {
            if (header == null)
            {
                return(0);
            }
            if (trailer == null)
            {
                return(0);
            }

            var size = 0;

            if (header.TrailingLength)
            {
                size += 2;
            }
            if (header.Checksum)
            {
                size += 2;
            }
            if (header.Padding)
            {
                size += trailer.PadCount;
            }

            return(size);
        }
        public LogicalRecordSegmentTrailer ReadLogicalRecordSegmentTrailer(Stream dlisStream, LogicalRecordSegmentHeader header)
        {
            if (dlisStream == null)
            {
                return(null);
            }
            if (dlisStream.IsAtBeginningOfStream())
            {
                return(null);
            }
            if (header == null)
            {
                return(null);
            }
            if (!header.HasTrailer())
            {
                return(null);
            }

            var trailer = new LogicalRecordSegmentTrailer();

            if (header.TrailingLength)
            {
                dlisStream.Seek(-2, SeekOrigin.Current);
            }
            if (header.Checksum)
            {
                dlisStream.Seek(-2, SeekOrigin.Current);
            }

            if (header.Padding)
            {
                dlisStream.Seek(-1, SeekOrigin.Current);
                trailer.PadCount = _ushortReader.ReadUSHORT(dlisStream);

                dlisStream.Seek(-trailer.PadCount, SeekOrigin.Current);
                trailer.Padding = dlisStream.ReadBytes(trailer.PadCount);
            }

            if (header.Checksum)
            {
                trailer.Checksum = _unormReader.ReadUNORM(dlisStream);
            }
            if (header.TrailingLength)
            {
                trailer.TrailingLength = _unormReader.ReadUNORM(dlisStream);
            }

            return(trailer);
        }
        public void LogicalRecordSegmentBusiness_ReadLogicalRecordSegment_Pass()
        {
            var dlisData = new byte[]
            {
                0x00, 0x14, 0xFF, 0x00,
                0x00, 0x06, 0x00, 0x01,
                0x00, 0x00, 0x0B, 0x0C,
                0x0D, 0x0E, 0x00, 0x02,
                0x00, 0x01, 0x00, 0x14
            };
            var dlisStream = new MemoryStream(dlisData);
            var header     = new LogicalRecordSegmentHeader
            {
                LogicalRecordSegmentLength     = 20,
                LogicalRecordSegmentAttributes = 0xFF
            };
            var encryptionPacket = new LogicalRecordSegmentEncryptionPacket
            {
                Size           = 6,
                CompanyCode    = 1,
                EncryptionInfo = new byte[] { 0, 0 }
            };
            var trailer = new LogicalRecordSegmentTrailer
            {
                PadCount       = 2,
                Padding        = new byte[] { 0, 2 },
                Checksum       = 1,
                TrailingLength = 20
            };

            _logicalRecordSegmentHeaderBusiness.Setup(x => x.ReadLogicalRecordSegmentHeader(dlisStream)).Returns(header);
            _logicalRecordSegmentEncryptionPacketBusiness.Setup(x => x.ReadLogicalRecordSegmentEncryptionPacket(dlisStream)).Returns(encryptionPacket);
            _logicalRecordSegmentTrailerBusiness.Setup(x => x.ReadLogicalRecordSegmentTrailer(dlisStream, header)).Returns(trailer);

            /* simulate the mocked objects reading the header and encryption packet by seeking 10 bytes */
            dlisStream.Seek(10, SeekOrigin.Begin);
            var result = _logicalRecordSegmentBusiness.ReadLogicalRecordSegment(dlisStream);

            Assert.AreEqual(0x0B, ((byte[])result.Body)[0]);
            Assert.AreEqual(0x0C, ((byte[])result.Body)[1]);
            Assert.AreEqual(0x0D, ((byte[])result.Body)[2]);
            Assert.AreEqual(0x0E, ((byte[])result.Body)[3]);
        }