Exemple #1
0
        private static long FindNextEntry(Core.Utility.BinaryReader reader, GZipMemberEntry currentEntry)
        {
            var foundMagic          = false;
            var positionOfNextEntry = -1L;

            try
            {
                var magic = reader.ReadByte();
                while (true)
                {
                    if (magic == MagicIdentifier[0])
                    {
                        magic = reader.ReadByte();
                        if (magic == MagicIdentifier[1])
                        {
                            foundMagic = true;
                            break;
                        }
                    }
                    else
                    {
                        magic = reader.ReadByte();
                    }
                }
            }
            catch (EndOfStreamException)
            {
                positionOfNextEntry = reader.BaseStream.Length;
            }
            catch (IOException)
            {
            }

            if (foundMagic || (positionOfNextEntry == reader.BaseStream.Length))
            {
                // go back 4 (CRC32) + 4 (size mod 2^32) [+ 2 (magic)]
                var offset = sizeof(uint) + sizeof(int);
                var origin = SeekOrigin.End;
                if (foundMagic)
                {
                    offset += MagicIdentifier.Length;
                    origin  = SeekOrigin.Current;
                }
                reader.BaseStream.Seek(-offset, origin);
                currentEntry.Crc32   = reader.ReadUInt32();
                currentEntry._length = reader.ReadInt32();
                positionOfNextEntry  = reader.BaseStream.Position;
            }

            return(positionOfNextEntry);
        }
        private static uint DecodeLength(Core.Utility.BinaryReader reader, out int additionalBytesInPayloadLength)
        {
            var  data          = reader.ReadByte();
            uint payloadLength = (uint)(data & ~PayloadLengthMask); // low 6 bits are the low 6 bits of payload length

            additionalBytesInPayloadLength = (data & PayloadLengthMask) >> PayloadLengthBitShift;
            for (var i = 0; i < additionalBytesInPayloadLength; ++i)
            {
                uint partialLengthBits = reader.ReadByte();
                partialLengthBits = partialLengthBits << ((i * 8) + PayloadLengthBitShift);
                payloadLength    |= partialLengthBits;
            }
            return(payloadLength);
        }
Exemple #3
0
        private static GZipOS ReadOperatingSystem(Core.Utility.BinaryReader reader, ref int bytesRead)
        {
            var operatingSystem = (GZipOS)reader.ReadByte();

            bytesRead += sizeof(GZipOS);
            return(operatingSystem);
        }
Exemple #4
0
        private static ExtraFlags ReadExtraFlags(Core.Utility.BinaryReader reader, ref int bytesRead)
        {
            var extraFlags = (ExtraFlags)reader.ReadByte();

            bytesRead += sizeof(ExtraFlags);
            return(extraFlags);
        }
        private static CfgVarMetadataIdTag DecodeBlockType(Core.Utility.BinaryReader reader)
        {
            var type          = CfgVarMetadataIdTag.Invalid;
            var payloadLength = reader.BaseStream.Length;
            var data          = new byte[payloadLength];
            var bytesRead     = 0;
            var foundEquals   = false;

            while (!foundEquals && (bytesRead < payloadLength))
            {
                var byteChar  = reader.ReadByte();
                var character = Convert.ToChar(byteChar);
                foundEquals = character == '=';
                if (!foundEquals)
                {
                    data[bytesRead++] = byteChar;
                }
            }
            if (foundEquals)
            {
                var tagString = System.Text.Encoding.UTF8.GetString(data, 0, data.Length).Trim('\0').Trim();
                type = tagString.ToCfgVarMetadataIdTag();
            }
            return(type);
        }
Exemple #6
0
        /// <inheritdoc/>
        protected override int DeserializePayload(Core.Utility.BinaryReader reader)
        {
            // This is inefficient, but simple. Read and walk the payload twice. First time validates its checksum. Second pass actually parses it.
            base.DeserializePayload(reader);
            reader.BaseStream.Seek(-Length, System.IO.SeekOrigin.Current);

            var runningBytesRead = 0;

            while (runningBytesRead < Length)
            {
                var tag = (LuigiMetadataIdTag)reader.ReadByte();
                ++runningBytesRead;
                var dataLength = reader.ReadByte();
                ++runningBytesRead;

                MetadataParser parser;
                if (!MetadataParsers.TryGetValue(tag, out parser))
                {
                    parser = DefaultMetadataParser;
                }

                var metadata = parser(reader, dataLength);
                runningBytesRead += dataLength;

                switch (tag)
                {
                case LuigiMetadataIdTag.Date:
                    var date = (MetadataDateTime)metadata;
                    _releaseDates.Add(date);
                    break;

                default:
                    List <string> metadataStringStorage;
                    if (_stringMetadataEntries.TryGetValue(tag, out metadataStringStorage))
                    {
                        var stringResult = (string)metadata;
                        metadataStringStorage.Add(stringResult);
                    }
                    else
                    {
                        System.Diagnostics.Debug.WriteLine("Either failed to parse or could not find storage for tag: " + tag);
                    }
                    break;
                }
            }
            return(Length);
        }
Exemple #7
0
        private static MetadataFlags ReadFlags(Core.Utility.BinaryReader reader, ref int bytesRead)
        {
            var flags = (MetadataFlags)reader.ReadByte();

            bytesRead += sizeof(MetadataFlags);
            if (flags.HasFlag(MetadataFlags.Reserved5 | MetadataFlags.Reserved6 | MetadataFlags.Reserved7))
            {
                throw new InvalidOperationException(Resources.Strings.GZipHeaderError_ReservedFlagsSet);
            }
            return(flags);
        }
Exemple #8
0
        private static CompressionMethod ReadCompressionMethod(Core.Utility.BinaryReader reader, ref int bytesRead)
        {
            var compressionMethod = (CompressionMethod)reader.ReadByte();

            bytesRead += sizeof(CompressionMethod);
            if (compressionMethod != CompressionMethod.Deflate)
            {
                throw new InvalidOperationException(Resources.Strings.GZipHeaderError_InvalidCompression);
            }
            return(compressionMethod);
        }
Exemple #9
0
        /// <inheritdoc />
        /// <remarks>The precondition here is that the reader is positioned immediately after the LUIGI block type value.</remarks>
        public override int Deserialize(Core.Utility.BinaryReader reader)
        {
            Length = reader.ReadUInt16();
            var bytesRead = PayloadLengthSize;

            HeaderCrc  = reader.ReadByte();
            bytesRead += HeaderChecksumSize;
            ValidateHeaderCrc();

            if (Length > 0)
            {
                PayloadCrc = reader.ReadUInt32();
                bytesRead += PayloadChecksumSize;
                bytesRead += DeserializePayload(reader);
            }
            return(bytesRead);
        }
Exemple #10
0
        private static string ReadNullTerminatedString(Core.Utility.BinaryReader reader, ref int bytesRead)
        {
            var  fileNameBytes    = new List <byte>();
            byte currentCharacter = 0;

            do
            {
                currentCharacter = reader.ReadByte();
                ++bytesRead;
                fileNameBytes.Add(currentCharacter);
            }while (currentCharacter != 0);

            var encoding = Encoding.GetEncoding("iso-8859-1");

            var stringValue = encoding.GetString(fileNameBytes.ToArray(), 0, fileNameBytes.Count - 1);

            return(stringValue);
        }
        /// <inheritdoc />
        /// <remarks>The precondition here is that the reader is positioned immediately after the ROM metadata block type.</remarks>
        public override int Deserialize(Core.Utility.BinaryReader reader)
        {
            int deserializedPayloadLength = 0;
            var payloadLength             = reader.BaseStream.Length;
            var data = new byte[payloadLength];

            try
            {
                while (deserializedPayloadLength < payloadLength)
                {
                    data[deserializedPayloadLength++] = reader.ReadByte();
                }
            }
            catch (System.IO.EndOfStreamException)
            {
            }
            var payload = ConvertPayloadToString(data);

            Parse(payload);
            return(deserializedPayloadLength);
        }
Exemple #12
0
        /// <inheritdoc />
        public override int Deserialize(Core.Utility.BinaryReader reader)
        {
            var header    = reader.ReadBytes(MagicKey.Length);
            var bytesRead = header.Length;

            if (!header.SequenceEqual(MagicKey))
            {
                throw new INTV.Core.UnexpectedFileTypeException("LUIGI");
            }

            Version    = reader.ReadByte();
            bytesRead += sizeof(byte);
            if (Version > LuigiFileHeader.CurrentVersion)
            {
                // Perhaps throwing here is a bad idea...
                throw new System.InvalidOperationException(string.Format(System.Globalization.CultureInfo.CurrentCulture, Resources.Strings.UnsupportedLuigiVersion_Format, Version));
            }

            Features   = (LuigiFeatureFlags)reader.ReadUInt64();
            bytesRead += sizeof(LuigiFeatureFlags);
            if (Version > 0)
            {
                Features2  = (LuigiFeatureFlags2)reader.ReadUInt64();
                bytesRead += sizeof(LuigiFeatureFlags2);
                var uidBytes = reader.ReadBytes(VersionOneUidSize);
                if (uidBytes.Length < sizeof(ulong))
                {
                    throw new System.IO.EndOfStreamException();
                }
                Uid        = BitConverter.ToUInt64(uidBytes, 0);
                bytesRead += sizeof(ulong);
                var originalFormatKey = uidBytes.Skip(OriginalRomCrc32Size).Take(OriginalRomKeySize);
                var originalCrcData   = uidBytes.Take(OriginalRomCrc32Size).ToArray();
                if (originalFormatKey.SequenceEqual(RomKey))
                {
                    OriginalRomFormat = RomFormat.Rom;
                    OriginalRomCrc32  = BitConverter.ToUInt32(originalCrcData, 0);
                }
                else
                {
                    OriginalRomFormat = RomFormat.Bin;
                    OriginalRomCrc32  = BitConverter.ToUInt32(originalCrcData, 0);
                    if (!originalFormatKey.SequenceEqual(BinKey))
                    {
                        OriginalCfgCrc32 = BitConverter.ToUInt32(originalFormatKey.ToArray(), 0);
                    }
                }
            }

            Reserved   = reader.ReadBytes(ReservedHeaderBytesSize);
            bytesRead += ReservedHeaderBytesSize;

            Crc        = reader.ReadByte();
            bytesRead += HeaderChecksumSize;

            ValidateHeaderBytesRead(bytesRead);
            ValidateHeaderCrc(reader, bytesRead);

            _deserializeByteCount = bytesRead;
            return(bytesRead);
        }