Exemplo n.º 1
0
        /// <summary>
        /// Read from a stream and get the Metafile data, if possible
        /// </summary>
        /// <param name="reader">BinaryReader representing the input stream</param>
        /// <returns>Metafile data object, null on error</returns>
        public static MetaFile Read(BinaryReader reader)
        {
            MetaFile metaFile = new MetaFile();

            try
            {
                metaFile.TitleIDDependencyList = reader.ReadBytes(0x180);
                metaFile.Reserved1             = reader.ReadBytes(0x180);
                metaFile.CoreVersion           = reader.ReadUInt32();
                metaFile.Reserved2             = reader.ReadBytes(0xFC);
                metaFile.IconData = reader.ReadBytes(0x36C0);

                return(metaFile);
            }
            catch
            {
                return(null);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Read from a stream and get a CIA header, if possible
        /// </summary>
        /// <param name="reader">BinaryReader representing the input stream</param>
        /// <returns>CIA header object, null on error</returns>
        public static CIAHeader Read(BinaryReader reader)
        {
            CIAHeader header = new CIAHeader();

            try
            {
                header.HeaderSize           = reader.ReadInt32();
                header.Type                 = reader.ReadUInt16();
                header.Version              = reader.ReadUInt16();
                header.CertificateChainSize = reader.ReadInt32();
                header.TicketSize           = reader.ReadInt32();
                header.TMDFileSize          = reader.ReadInt32();
                header.MetaSize             = reader.ReadInt32();
                header.ContentSize          = reader.ReadInt64();
                header.ContentIndex         = reader.ReadBytes(0x2000);
                if (reader.BaseStream.Position % 64 != 0)
                {
                    reader.BaseStream.Seek(64 - (reader.BaseStream.Position % 64), SeekOrigin.Current);
                }

                header.CertificateChain    = new Certificate[3];
                header.CertificateChain[0] = Certificate.Read(reader); // CA
                header.CertificateChain[1] = Certificate.Read(reader); // Ticket
                header.CertificateChain[2] = Certificate.Read(reader); // TMD
                if (reader.BaseStream.Position % 64 != 0)
                {
                    reader.BaseStream.Seek(64 - (reader.BaseStream.Position % 64), SeekOrigin.Current);
                }

                header.Ticket = Ticket.Read(reader, header.TicketSize);
                if (reader.BaseStream.Position % 64 != 0)
                {
                    reader.BaseStream.Seek(64 - (reader.BaseStream.Position % 64), SeekOrigin.Current);
                }

                header.TMDFileData = TitleMetadata.Read(reader, header.TMDFileSize);
                if (reader.BaseStream.Position % 64 != 0)
                {
                    reader.BaseStream.Seek(64 - (reader.BaseStream.Position % 64), SeekOrigin.Current);
                }

                long startingPosition     = reader.BaseStream.Position;
                List <NCCHHeader> headers = new List <NCCHHeader>();
                while (reader.BaseStream.Position < startingPosition + header.ContentSize)
                {
                    long       initPosition = reader.BaseStream.Position;
                    NCCHHeader ncchHeader   = NCCHHeader.Read(reader, readSignature: true);
                    if (ncchHeader == null)
                    {
                        break;
                    }

                    headers.Add(ncchHeader);
                    reader.BaseStream.Seek(initPosition + ncchHeader.ContentSizeInMediaUnits * 0x200, SeekOrigin.Begin);
                }

                header.Partitions = headers.ToArray();
                if (header.MetaSize > 0)
                {
                    header.MetaFileData = MetaFile.Read(reader);
                }

                return(header);
            }
            catch
            {
                return(null);
            }
        }