Esempio n. 1
0
        private NintendoContentFileSystemInfo.EntryInfo BuildData(NintendoContentFileSystemInfo.EntryInfo entryInfo, NintendoContentArchiveSource.BuildDataInfo buildInfo)
        {
            entryInfo.hashType       = NintendoContentArchiveSource.VerifyHashType(this.m_fileSystemInfo, entryInfo);
            entryInfo.encryptionType = NintendoContentArchiveSource.VerifyEncryptionType(this.m_fileSystemInfo, entryInfo);
            entryInfo.startOffset    = (ulong)buildInfo.CurrentOffset + buildInfo.Meta.GetHeaderSize();
            entryInfo.hashTargetSize = (ulong)buildInfo.DataSource.Size;
            IHierarchicalHashCalculatedSource calculatedSource;

            switch (entryInfo.hashType)
            {
            case 2:
                calculatedSource = (IHierarchicalHashCalculatedSource) new Sha256HierarchicalHashCalculatedSource(buildInfo.DataSource, buildInfo.Meta.GetHashBlockSize((ValueType)entryInfo), 2);
                break;

            case 3:
                calculatedSource = (IHierarchicalHashCalculatedSource) new IntegrityHierarchicalHashCalculatedSource(buildInfo.DataSource);
                break;

            default:
                throw new NotImplementedException();
            }
            long    offset1         = 0;
            ISource layerHashSource = calculatedSource.GetLayerHashSource();

            entryInfo.hashDataSize = (ulong)layerHashSource.Size;
            ConcatenatedSource.Element element1 = new ConcatenatedSource.Element(layerHashSource, "fsHash" + (object)buildInfo.Index, offset1);
            buildInfo.DataSourceElements.Add(element1);
            long offset2 = offset1 + element1.Source.Size;
            int  num1    = (int)(offset2 % 512L);

            if (0 < num1)
            {
                ConcatenatedSource.Element element2 = new ConcatenatedSource.Element((ISource) new PaddingSource((long)(512 - num1)), "fsHashPadding" + (object)buildInfo.Index, offset2);
                buildInfo.DataSourceElements.Add(element2);
                offset2 += element2.Source.Size;
            }
            entryInfo.hashTargetOffset = (ulong)offset2;
            ConcatenatedSource.Element element3 = new ConcatenatedSource.Element((ISource)calculatedSource, "fsData" + (object)buildInfo.Index, offset2);
            buildInfo.DataSourceElements.Add(element3);
            long offset3 = offset2 + element3.Source.Size;
            int  num2    = (int)(offset3 % 512L);

            if (0 < num2)
            {
                ConcatenatedSource.Element element2 = new ConcatenatedSource.Element((ISource) new PaddingSource((long)(512 - num2)), "fsDataPadding" + (object)buildInfo.Index, offset3);
                buildInfo.DataSourceElements.Add(element2);
                offset3 += element2.Source.Size;
            }
            buildInfo.CurrentOffset += offset3;
            entryInfo.endOffset      = (ulong)buildInfo.CurrentOffset + buildInfo.Meta.GetHeaderSize();
            buildInfo.HeaderSource   = calculatedSource.GetFsHeaderSource(entryInfo);
            return(entryInfo);
        }
        public NintendoContentFileSystemInfo GetFileSystemInfo()
        {
            NintendoContentFileSystemInfo fileSystemInfo1 = new NintendoContentFileSystemInfo();

            using (StreamReader streamReader = new StreamReader(this.m_adfPath, Encoding.UTF8))
            {
                YamlStream yamlStream = new YamlStream();
                yamlStream.Load((TextReader)streamReader);
                YamlMappingNode  rootNode;
                YamlSequenceNode child1;
                try
                {
                    rootNode = (YamlMappingNode)yamlStream.Documents[0].RootNode;
                    if (((YamlScalarNode)rootNode.Children[(YamlNode) new YamlScalarNode("formatType")]).Value != "NintendoContent")
                    {
                        throw new ArgumentException();
                    }
                    YamlScalarNode child2 = (YamlScalarNode)rootNode.Children[(YamlNode) new YamlScalarNode("contentType")];
                    YamlScalarNode child3 = (YamlScalarNode)rootNode.Children[(YamlNode) new YamlScalarNode("keyGeneration")];
                    YamlScalarNode child4 = (YamlScalarNode)rootNode.Children[(YamlNode) new YamlScalarNode("programId")];
                    YamlScalarNode child5 = (YamlScalarNode)rootNode.Children[(YamlNode) new YamlScalarNode("contentIndex")];
                    child1 = (YamlSequenceNode)rootNode.Children[(YamlNode) new YamlScalarNode("entries")];
                    fileSystemInfo1.contentType   = this.ConvertToContentTypeByte(child2.Value);
                    fileSystemInfo1.keyGeneration = Convert.ToByte(child3.Value);
                    fileSystemInfo1.programId     = Convert.ToUInt64(child4.Value, 16);
                    fileSystemInfo1.contentIndex  = Convert.ToUInt32(child5.Value);
                }
                catch
                {
                    throw new ArgumentException("invalid format .adf file.");
                }
                try
                {
                    YamlScalarNode child2 = (YamlScalarNode)rootNode.Children[(YamlNode) new YamlScalarNode("keyAreaEncryptionKeyIndex")];
                    fileSystemInfo1.keyAreaEncryptionKeyIndex = Convert.ToByte(child2.Value);
                }
                catch
                {
                    fileSystemInfo1.keyAreaEncryptionKeyIndex = (byte)0;
                }
                string descFilePath;
                if (fileSystemInfo1.contentType == (byte)0)
                {
                    try
                    {
                        descFilePath = ((YamlScalarNode)rootNode.Children[(YamlNode) new YamlScalarNode("descFilePath")]).Value;
                    }
                    catch
                    {
                        throw new ArgumentException(".desc file is not specified for program content.");
                    }
                }
                else
                {
                    descFilePath = (string)null;
                }
                byte[] numArray = (byte[])null;
                if (descFilePath != null)
                {
                    numArray = NintendoContentAdfReader.RetrieveInfoFromDesc(ref fileSystemInfo1, descFilePath);
                }
                foreach (YamlMappingNode yamlMappingNode in child1)
                {
                    NintendoContentFileSystemInfo.EntryInfo entryInfo1 = new NintendoContentFileSystemInfo.EntryInfo();
                    string adfPath = (string)null;
                    foreach (KeyValuePair <YamlNode, YamlNode> keyValuePair in yamlMappingNode)
                    {
                        switch (((YamlScalarNode)keyValuePair.Key).Value)
                        {
                        case "encryptionType":
                            entryInfo1.encryptionType = Convert.ToByte(((YamlScalarNode)keyValuePair.Value).Value);
                            continue;

                        case "formatType":
                            entryInfo1.formatType = ((YamlScalarNode)keyValuePair.Value).Value;
                            continue;

                        case "hashType":
                            entryInfo1.hashType = Convert.ToByte(((YamlScalarNode)keyValuePair.Value).Value);
                            continue;

                        case "partitionType":
                            entryInfo1.partitionType  = ((YamlScalarNode)keyValuePair.Value).Value;
                            entryInfo1.partitionIndex = this.GetPartitionIndex(entryInfo1.partitionType);
                            continue;

                        case "path":
                            adfPath = ((YamlScalarNode)keyValuePair.Value).Value;
                            continue;

                        case "type":
                            entryInfo1.type = ((YamlScalarNode)keyValuePair.Value).Value;
                            continue;

                        case "version":
                            entryInfo1.version = Convert.ToUInt16(((YamlScalarNode)keyValuePair.Value).Value);
                            continue;

                        default:
                            throw new ArgumentException("invalid format .adf file. invalid key is specified\n" + yamlMappingNode.ToString());
                        }
                    }
                    if (entryInfo1.type == null || entryInfo1.formatType == null || adfPath == null)
                    {
                        throw new ArgumentException("invalid format .adf file. \"type\" or \"formatType\" is not specified\n" + yamlMappingNode.ToString());
                    }
                    entryInfo1.version        = (ushort)2;
                    entryInfo1.hashType       = NintendoContentArchiveSource.VerifyHashType(fileSystemInfo1, entryInfo1);
                    entryInfo1.encryptionType = NintendoContentArchiveSource.VerifyEncryptionType(fileSystemInfo1, entryInfo1);
                    string formatType = entryInfo1.formatType;
                    if (!(formatType == "PartitionFs"))
                    {
                        if (!(formatType == "RomFs"))
                        {
                            throw new NotImplementedException("invalid format .adf file. invalid formatType." + yamlMappingNode.ToString());
                        }
                        RomFsAdfReader romFsAdfReader = new RomFsAdfReader(adfPath);
                        entryInfo1.fileSystemInfo = (Nintendo.Authoring.FileSystemMetaLibrary.FileSystemInfo)romFsAdfReader.GetFileSystemInfo();
                    }
                    else
                    {
                        PartitionFileSystemInfo fileSystemInfo2 = new PartitionFsAdfReader(adfPath).GetFileSystemInfo();
                        if (numArray != null && entryInfo1.partitionType == "code")
                        {
                            IEnumerable <PartitionFileSystemInfo.EntryInfo> source = fileSystemInfo2.entries.Where <PartitionFileSystemInfo.EntryInfo>((Func <PartitionFileSystemInfo.EntryInfo, bool>)(p => p.name == "main.npdm"));
                            if (fileSystemInfo1.contentType == (byte)0 && source.Count <PartitionFileSystemInfo.EntryInfo>() != 1)
                            {
                                throw new ArgumentException("\"main.npdm\" must be included in the code region.");
                            }
                            foreach (PartitionFileSystemInfo.EntryInfo entryInfo2 in source)
                            {
                                if (!((IEnumerable <byte>) this.ReadAcid(entryInfo2.path)).SequenceEqual <byte>((IEnumerable <byte>)numArray))
                                {
                                    throw new ArgumentException(".desc file specified differ from the one used to build code.");
                                }
                            }
                        }
                        entryInfo1.fileSystemInfo = (Nintendo.Authoring.FileSystemMetaLibrary.FileSystemInfo)fileSystemInfo2;
                    }
                    fileSystemInfo1.fsEntries.Add(entryInfo1);
                }
            }
            fileSystemInfo1.numFsEntries     = fileSystemInfo1.fsEntries.Count;
            fileSystemInfo1.isProdEncryption = false;
            return(fileSystemInfo1);
        }