예제 #1
0
        public EnhancedPacketBlock GetPacketAt(long packetBlockOffset)
        {
            // TODO:
            bool reverseByteOrder = false;

            using (FileStream fileStream = File.Open(Path, FileMode.Open, FileAccess.ReadWrite, FileShare.Read))
                using (BinaryReader binReader = new BinaryReader(fileStream)) {
                    // Navigate to the overridden block start
                    long actualOffset = fileStream.Seek(packetBlockOffset, SeekOrigin.Begin);
                    // Make sure seek succeeded
                    if (actualOffset != packetBlockOffset)
                    {
                        throw new Exception($"Couldn't seek to offset {packetBlockOffset}");
                    }

                    // Read next 8 bytes of the block we are overriding:
                    BaseBlock.Types type = (BaseBlock.Types)binReader.ReadUInt32();
                    if (type != BaseBlock.Types.EnhancedPacket)
                    {
                        throw new Exception($"Expected an ENHANCED PACKET BLOCK (val:{BaseBlock.Types.EnhancedPacket}) in the given offset but got: {type}");
                    }
                    fileStream.Seek(-4, SeekOrigin.Current);

                    var block = AbstractBlockFactory.ReadNextBlock(binReader, reverseByteOrder, (ex) => Debug.WriteLine("Kek!" + ex));
                    if (block == null || block.BlockType != BaseBlock.Types.EnhancedPacket)
                    {
                        throw new Exception("Block at given position was not parsed to a ENHANCED PACKET BLOCK");
                    }
                    return(block as EnhancedPacketBlock);
                }
        }
예제 #2
0
        public List <long> GetPacketsOffsets()
        {
            if (_cachedOffsets == null)
            {
                _cachedOffsets = new List <long>();

                int packetCounter = 0;
                using (FileStream fileStream = File.OpenRead(Path))
                    using (BinaryReader binReader = new BinaryReader(fileStream)) {
                        while (fileStream.Position != fileStream.Length)
                        {
                            // Read next 8 bytes of block:
                            BaseBlock.Types type = (BaseBlock.Types)binReader.ReadUInt32();
                            uint            len  = binReader.ReadUInt32();
                            if (type == BaseBlock.Types.EnhancedPacket)
                            {
                                _cachedOffsets.Add(fileStream.Position - 8);
                                packetCounter++;
                            }

                            // Advance to next block start
                            fileStream.Seek(len - 8, SeekOrigin.Current);
                        }
                    }
            }
            return(_cachedOffsets);
        }
예제 #3
0
        public List <InterfaceDescriptionBlock> GetInterfaces()
        {
            if (_cachedIfacesBlock == null)
            {
                // TODO:
                bool reverseByteOrder = false;

                _cachedIfacesBlock = new List <InterfaceDescriptionBlock>();


                // Checking until first packet block or end of file
                using (FileStream fileStream = File.OpenRead(Path))
                    using (BinaryReader binReader = new BinaryReader(fileStream)) {
                        while (fileStream.Position != fileStream.Length)
                        {
                            // Read next 8 bytes of block:
                            BaseBlock.Types type = (BaseBlock.Types)binReader.ReadUInt32();
                            uint            len  = binReader.ReadUInt32();
                            if (type == BaseBlock.Types.EnhancedPacket)
                            {
                                // Found a packet block, stopping search
                                break;
                            }

                            if (type == BaseBlock.Types.InterfaceDescription)
                            {
                                fileStream.Seek(-8, SeekOrigin.Current);
                                var block = AbstractBlockFactory.ReadNextBlock(binReader, reverseByteOrder, (ex) => Debug.WriteLine("Kek!" + ex));
                                if (block == null || block.BlockType != BaseBlock.Types.InterfaceDescription)
                                {
                                    throw new Exception("Block at given position was not parsed to a INTERFACE DESCRIPTION BLOCK");
                                }
                                _cachedIfacesBlock.Add(block as InterfaceDescriptionBlock);
                            }
                            else
                            {
                                // Advance to next block start
                                fileStream.Seek(len - 8, SeekOrigin.Current);
                            }
                        }
                    }
            }
            return(_cachedIfacesBlock);
        }
예제 #4
0
        /// <summary>
        /// Replaces a packet with a new packet
        /// </summary>
        /// <param name="packetBlockOffset">Offset in the file where the block begins</param>
        /// <param name="newPacket">New packet block</param>
        public void ReplacePacket(long packetBlockOffset, EnhancedPacketBlock newPacket)
        {
            // TODO: Reverse Byte order not always false probably...
            byte[] data = newPacket.ConvertToByte(false, (ex) => Debug.WriteLine("LOL!" + ex));

            using (FileStream fileStream = File.Open(Path, FileMode.Open, FileAccess.ReadWrite))
                using (BinaryReader binReader = new BinaryReader(fileStream)) {
                    // Navigate to the overridden block start
                    long actualOffset = fileStream.Seek(packetBlockOffset, SeekOrigin.Begin);
                    // Make sure seek succeeded
                    if (actualOffset != packetBlockOffset)
                    {
                        throw new Exception($"Couldn't seek to offset {packetBlockOffset}");
                    }

                    // Read next 8 bytes of the block we are overriding:
                    BaseBlock.Types type = (BaseBlock.Types)binReader.ReadUInt32();
                    uint            len  = binReader.ReadUInt32();
                    if (type != BaseBlock.Types.EnhancedPacket)
                    {
                        throw new Exception($"Expected an ENHANCED PACKET BLOCK (val:{BaseBlock.Types.EnhancedPacket}) in the given offset but got: {type}");
                    }
                    if (len < data.Length)
                    {
                        ReplacePacketLonger(data, fileStream, len);
                    }
                    if (len > data.Length)
                    {
                        ReplacePacketShorter(data, fileStream, len);
                    }
                    else
                    {
                        // Lengths match exactly
                        fileStream.Seek(-4, SeekOrigin.Current);
                        fileStream.Write(data, 4, data.Length - 4);
                    }
                }
        }