示例#1
0
        private FwPacket ProcessNextFwPacket(BinaryReaderEndian packetReader)
        {
            FwPacket packet = new FwPacket();

            try
            {
                // Seek to next packet start
                while (true)
                {
                    if (packetReader.ReadByte() == 0x00)
                    {
                        byte tableId = packetReader.ReadByte();
                        if (tableId == 0xB5 || tableId == 0xB6)
                        {
                            packetReader.BaseStream.Seek(-2, SeekOrigin.Current);
                            break;
                        }
                        else
                        {
                            packetReader.BaseStream.Seek(-1, SeekOrigin.Current);
                        }
                    }
                }

                long startPos = packetReader.BaseStream.Position;

                // Skip over table id
                packetReader.ReadInt16();
                packet.Length      = (ushort)(packetReader.ReadUInt16() & 0x0FFF);
                packet.SectionType = packetReader.ReadUInt16();
                packet.Unknown     = packetReader.ReadUInt32();
                packet.VendorId    = packetReader.ReadByte();
                packetReader.ReadByte();
                packet.ModelId = packetReader.ReadByte();
                packetReader.BaseStream.Seek(3, SeekOrigin.Current);
                packet.FirmwareVersion = packetReader.ReadByte();
                packet.FileOffset      = packetReader.ReadUInt32();
                packet.FileLength      = packetReader.ReadUInt32();

                packet.Data  = packetReader.ReadBytes(packet.Length - 25);
                packet.Crc32 = packetReader.ReadUInt32();

                packetReader.BaseStream.Seek(startPos + 1, SeekOrigin.Begin);
                byte[] wholePacket = packetReader.ReadBytes(packet.Length + 3);

                packet.hasPassedCRC = (Crc32.crc32_mpeg(wholePacket, (uint)wholePacket.Length) == 0);
            }
            catch (EndOfStreamException ex)
            {
                throw ex;
            }
            catch (Exception)
            {
                packet.hasPassedCRC = false;
            }

            return(packet);
        }
示例#2
0
        protected Archive(string fileName, Encoding encoding, Endian byteOrder)
        {
            FullName = fileName ?? throw new ArgumentNullException(nameof(fileName));

            if (!File.Exists(fileName))
            {
                throw new FileNotFoundException(fileName);
            }

            this.encoding  = encoding ?? throw new ArgumentNullException(nameof(encoding));
            this.byteOrder = byteOrder;
            stream         = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
            binaryReader   = new BinaryReaderEndian(stream, this.encoding, false, Endian.Little);
        }
示例#3
0
        public static BinaryReader OpenReadFile(byte[] data, bool IsLittleEndian)
        {
            BinaryReader returned;

            if ((BitConverter.IsLittleEndian & IsLittleEndian) || !(BitConverter.IsLittleEndian | IsLittleEndian))
            {
                returned = new BinaryReader(new MemoryStream(data), Encoding.ASCII, false);
            }
            else
            {
                returned = new BinaryReaderEndian(new MemoryStream(data), Encoding.ASCII, false);
            }

            return(returned);
        }
示例#4
0
        public static BinaryReader OpenReadFile(Stream stream, bool IsLittleEndian, bool leaveOpen = true)
        {
            BinaryReader returned;

            if ((BitConverter.IsLittleEndian & IsLittleEndian) || !(BitConverter.IsLittleEndian | IsLittleEndian))
            {
                returned = new BinaryReader(stream, Encoding.ASCII, leaveOpen);
            }
            else
            {
                returned = new BinaryReaderEndian(stream, Encoding.ASCII, leaveOpen);
            }

            return(returned);
        }
示例#5
0
        private void Read(StreamPart streamFile)
        {
            streamFile.Stream.Position = streamFile.Position;

            using (BinaryReader reader = new BinaryReaderEndian(streamFile.Stream, Encoding.ASCII, true))
            {
                Header = new DDSAtlusHeader(reader);

                int temp = 0;

                temp += ReadTexture(reader);

                if (temp != Header.SizeTexture)
                {
                    throw new Exception("DDSAtlus: wrong texture size");
                }

                LastBlock = reader.ReadBytes(Header.SizeWOHeader - Header.SizeTexture);
            }
        }
        private FwPacket ProcessNextFwPacket(BinaryReaderEndian packetReader)
        {
            FwPacket packet = new FwPacket();

            try
            {
                // Seek to next packet start
                while (true)
                {
                    if (packetReader.ReadByte() == 0x00)
                    {
                        byte tableId = packetReader.ReadByte();
                        if (tableId == 0xB5 || tableId == 0xB6)
                        {
                            packetReader.BaseStream.Seek(-2, SeekOrigin.Current);
                            break;
                        }
                        else
                        {
                            packetReader.BaseStream.Seek(-1, SeekOrigin.Current);
                        }
                    }
                }

                long startPos = packetReader.BaseStream.Position;

                // Skip over table id
                packetReader.ReadInt16();
                packet.Length = (ushort)(packetReader.ReadUInt16() & 0x0FFF);
                packet.SectionType = packetReader.ReadUInt16();
                packet.Unknown = packetReader.ReadUInt32();
                packet.VendorId = packetReader.ReadByte();
                packetReader.ReadByte();
                packet.ModelId = packetReader.ReadByte();
                packetReader.BaseStream.Seek(3, SeekOrigin.Current);
                packet.FirmwareVersion = packetReader.ReadByte();
                packet.FileOffset = packetReader.ReadUInt32();
                packet.FileLength = packetReader.ReadUInt32();

                packet.Data = packetReader.ReadBytes(packet.Length - 25);
                packet.Crc32 = packetReader.ReadUInt32();

                packetReader.BaseStream.Seek(startPos + 1, SeekOrigin.Begin);
                byte[] wholePacket = packetReader.ReadBytes(packet.Length + 3);

                packet.hasPassedCRC = (Crc32.crc32_mpeg(wholePacket, (uint)wholePacket.Length) == 0);
            }
            catch (EndOfStreamException ex)
            {
                throw ex;
            }
            catch (Exception)
            {
                packet.hasPassedCRC = false;
            }

            return packet;
        }
        public void ProcessBuffer(byte[] buffer)
        {
            byte[] packetBuffer = new byte[188];
            using (MemoryStream tsStream = new MemoryStream(buffer))
            {
                while (tsStream.Position < tsStream.Length)
                {
                    _bytesInLastSecond += 188;
                    tsStream.Read(packetBuffer, 0, 188);
                    TransportStreamPacket tsPacket = PacketParsing.ParsePacket(packetBuffer);

                    if (tsPacket.packetID == 0x60 || tsPacket.packetID == 0x61)
                    {
                        if (_currentFwPackets.ContainsKey(tsPacket.packetID))
                        {
                            if (tsPacket.payload[0] == 0x00 && (tsPacket.payload[1] == 0xB5 || tsPacket.payload[1] == 0xB6))
                            {
                                // We have started a new packet
                                using (BinaryReaderEndian packetReader = new BinaryReaderEndian(_currentFwPackets[tsPacket.packetID], false))
                                {
                                    packetReader.DontDispose = true;
                                    packetReader.BaseStream.Seek(0, SeekOrigin.Begin);

                                    FwPacket fwPacket;
                                    try
                                    {
                                        fwPacket = ProcessNextFwPacket(packetReader);
                                    }
                                    catch (EndOfStreamException)
                                    {
                                        // Probably a better way of doing this! (When 0x00 0xB5 exists at the beginning of a packet naturaly)
                                        _currentFwPackets[tsPacket.packetID].Write(tsPacket.payload, 0, tsPacket.payload.Length);
                                        continue;
                                    }

                                    string fwName = string.Format("{0:X2}-{1:X2}-{2:X2}", fwPacket.VendorId, fwPacket.ModelId, fwPacket.FirmwareVersion);

                                    lock (CurrentFiles)
                                    {
                                        CurrentFiles[tsPacket.packetID] = string.Format("{0:X2}{1:X2}-r{2}", fwPacket.VendorId, fwPacket.ModelId, fwPacket.FirmwareVersion);
                                    }

                                    if (fwPacket.SectionType == 1 && !CompletedFiles.ContainsKey(fwName))
                                    {
                                        if (!InProgressFiles.ContainsKey(fwName))
                                        {
                                            InProgressFiles.Add(fwName, new FirmwareFile(fwPacket, _extractFolder));
                                            InProgressFileList.Add(InProgressFiles[fwName]);
                                        }
                                        else
                                        {
                                            InProgressFiles[fwName].AddPacket(fwPacket);
                                        }

                                        if (InProgressFiles[fwName].isComplete)
                                        {
                                            _completedListHasChanged = true;
                                            CompletedFiles.Add(fwName, InProgressFiles[fwName]);
                                            CompletedFileList.Add(InProgressFiles[fwName]);
                                            InProgressFileList.Remove(InProgressFiles[fwName]);
                                            InProgressFiles.Remove(fwName);
                                        }
                                    }
                                }

                                _currentFwPackets[tsPacket.packetID].Close();
                                _currentFwPackets[tsPacket.packetID] = new MemoryStream();
                            }

                            _currentFwPackets[tsPacket.packetID].Write(tsPacket.payload, 0, tsPacket.payload.Length);
                        }
                        else if (tsPacket.payload[0] == 0x00 && (tsPacket.payload[1] == 0xB5 || tsPacket.payload[1] == 0xB6))
                        {
                            _currentFwPackets.Add(tsPacket.packetID, new MemoryStream());
                            _currentFwPackets[tsPacket.packetID].Write(tsPacket.payload, 0, tsPacket.payload.Length);
                        }
                    }
                }
            }
        }
示例#8
0
        public void ProcessBuffer(byte[] buffer)
        {
            byte[] packetBuffer = new byte[188];
            using (MemoryStream tsStream = new MemoryStream(buffer))
            {
                while (tsStream.Position < tsStream.Length)
                {
                    _bytesInLastSecond += 188;
                    tsStream.Read(packetBuffer, 0, 188);
                    TransportStreamPacket tsPacket = PacketParsing.ParsePacket(packetBuffer);

                    if (tsPacket.packetID == 0x60 || tsPacket.packetID == 0x61)
                    {
                        if (_currentFwPackets.ContainsKey(tsPacket.packetID))
                        {
                            if (tsPacket.payload[0] == 0x00 && (tsPacket.payload[1] == 0xB5 || tsPacket.payload[1] == 0xB6))
                            {
                                // We have started a new packet
                                using (BinaryReaderEndian packetReader = new BinaryReaderEndian(_currentFwPackets[tsPacket.packetID], false))
                                {
                                    packetReader.DontDispose = true;
                                    packetReader.BaseStream.Seek(0, SeekOrigin.Begin);

                                    FwPacket fwPacket;
                                    try
                                    {
                                        fwPacket = ProcessNextFwPacket(packetReader);
                                    }
                                    catch (EndOfStreamException)
                                    {
                                        // Probably a better way of doing this! (When 0x00 0xB5 exists at the beginning of a packet naturaly)
                                        _currentFwPackets[tsPacket.packetID].Write(tsPacket.payload, 0, tsPacket.payload.Length);
                                        continue;
                                    }

                                    string fwName = string.Format("{0:X2}-{1:X2}-{2:X2}", fwPacket.VendorId, fwPacket.ModelId, fwPacket.FirmwareVersion);

                                    lock (CurrentFiles)
                                    {
                                        CurrentFiles[tsPacket.packetID] = string.Format("{0:X2}{1:X2}-r{2}", fwPacket.VendorId, fwPacket.ModelId, fwPacket.FirmwareVersion);
                                    }

                                    if (fwPacket.SectionType == 1 && !CompletedFiles.ContainsKey(fwName))
                                    {
                                        if (!InProgressFiles.ContainsKey(fwName))
                                        {
                                            InProgressFiles.Add(fwName, new FirmwareFile(fwPacket, _extractFolder));
                                            InProgressFileList.Add(InProgressFiles[fwName]);
                                        }
                                        else
                                        {
                                            InProgressFiles[fwName].AddPacket(fwPacket);
                                        }

                                        if (InProgressFiles[fwName].isComplete)
                                        {
                                            _completedListHasChanged = true;
                                            CompletedFiles.Add(fwName, InProgressFiles[fwName]);
                                            CompletedFileList.Add(InProgressFiles[fwName]);
                                            InProgressFileList.Remove(InProgressFiles[fwName]);
                                            InProgressFiles.Remove(fwName);
                                        }
                                    }
                                }

                                _currentFwPackets[tsPacket.packetID].Close();
                                _currentFwPackets[tsPacket.packetID] = new MemoryStream();
                            }

                            _currentFwPackets[tsPacket.packetID].Write(tsPacket.payload, 0, tsPacket.payload.Length);
                        }
                        else if (tsPacket.payload[0] == 0x00 && (tsPacket.payload[1] == 0xB5 || tsPacket.payload[1] == 0xB6))
                        {
                            _currentFwPackets.Add(tsPacket.packetID, new MemoryStream());
                            _currentFwPackets[tsPacket.packetID].Write(tsPacket.payload, 0, tsPacket.payload.Length);
                        }
                    }
                }
            }
        }