コード例 #1
0
ファイル: Package2.cs プロジェクト: garoxas/LibHac
        public Result Verify()
        {
            // Get the obfuscated metadata.
            uint size          = Size;
            byte keyGeneration = KeyGeneration;

            // Check that size is big enough for the header.
            if (size < Unsafe.SizeOf <Package2Header>())
            {
                return(ResultLibHac.InvalidPackage2MetaSizeA.Log());
            }

            // Check that the size isn't larger than what we allow.
            if (size > Package2Header.Package2SizeMax)
            {
                return(ResultLibHac.InvalidPackage2MetaSizeB.Log());
            }

            // Check that the key generation is one that we can use.
            if (keyGeneration >= 0x20)
            {
                return(ResultLibHac.InvalidPackage2MetaKeyGeneration.Log());
            }

            // Check the magic number.
            if (Magic != ExpectedMagicValue)
            {
                return(ResultLibHac.InvalidPackage2MetaMagic.Log());
            }

            // Check the payload alignments.
            if (EntryPoint % Package2Header.PayloadAlignment != 0)
            {
                return(ResultLibHac.InvalidPackage2MetaEntryPointAlignment.Log());
            }

            for (int i = 0; i < Package2Header.PayloadCount; i++)
            {
                if (PayloadSizes[i] % Package2Header.PayloadAlignment != 0)
                {
                    return(ResultLibHac.InvalidPackage2MetaPayloadSizeAlignment.Log());
                }
            }

            // Check that the sizes sum to the total.
            if (Size != Unsafe.SizeOf <Package2Header>() + PayloadSizes[0] + PayloadSizes[1] + PayloadSizes[2])
            {
                return(ResultLibHac.InvalidPackage2MetaTotalSize.Log());
            }

            // Check that the payloads do not overflow.
            for (int i = 0; i < Package2Header.PayloadCount; i++)
            {
                if (PayloadOffsets[i] > PayloadOffsets[i] + PayloadSizes[i])
                {
                    return(ResultLibHac.InvalidPackage2MetaPayloadSize.Log());
                }
            }

            // Verify that no payloads overlap.
            for (int i = 0; i < Package2Header.PayloadCount - 1; i++)
            {
                for (int j = i + 1; j < Package2Header.PayloadCount; j++)
                {
                    if (Overlap.HasOverlap(PayloadOffsets[i], PayloadSizes[i], PayloadOffsets[j], PayloadSizes[j]))
                    {
                        return(ResultLibHac.InvalidPackage2MetaPayloadsOverlap.Log());
                    }
                }
            }

            // Check whether any payload contains the entrypoint.
            for (int i = 0; i < Package2Header.PayloadCount; i++)
            {
                if (Overlap.Contains(PayloadOffsets[i], PayloadSizes[i], EntryPoint))
                {
                    return(Result.Success);
                }
            }

            // No payload contains the entrypoint, so we're not valid.
            return(ResultLibHac.InvalidPackage2MetaEntryPointNotFound.Log());
        }