Exemple #1
0
        private static UInt32 CalculateChecksum(byte[] PEFile)
        {
            UInt32 Checksum = 0;
            UInt32 Hi;

            // Clear file checksum
            ByteOperations.WriteUInt32(PEFile, GetChecksumOffset(PEFile), 0);

            for (UInt32 i = 0; i < ((UInt32)PEFile.Length & 0xfffffffe); i += 2)
            {
                Checksum += ByteOperations.ReadUInt16(PEFile, i);
                Hi        = Checksum >> 16;
                if (Hi != 0)
                {
                    Checksum = Hi + (Checksum & 0xFFFF);
                }
            }
            if ((PEFile.Length % 2) != 0)
            {
                Checksum += (UInt32)ByteOperations.ReadUInt8(PEFile, (UInt32)PEFile.Length - 1);
                Hi        = Checksum >> 16;
                if (Hi != 0)
                {
                    Checksum = Hi + (Checksum & 0xFFFF);
                }
            }
            Checksum += (UInt32)PEFile.Length;

            // Write file checksum
            ByteOperations.WriteUInt32(PEFile, GetChecksumOffset(PEFile), Checksum);

            return(Checksum);
        }
Exemple #2
0
        internal UInt32 CalculateChecksum()
        {
            UInt32 Checksum = 0;
            UInt32 Hi;

            // Clear file checksum
            // ByteOperations.WriteUInt32(PEFile, GetChecksumOffset(), 0);
            UInt32 ChecksumOffset = GetChecksumOffset();

            for (UInt32 i = 0; i < ((UInt32)Buffer.Length & 0xfffffffe); i += 2)
            {
                if ((i < ChecksumOffset) || (i >= (ChecksumOffset + 4)))
                {
                    Checksum += ByteOperations.ReadUInt16(Buffer, i);
                }

                Hi = Checksum >> 16;
                if (Hi != 0)
                {
                    Checksum = Hi + (Checksum & 0xFFFF);
                }
            }
            if ((Buffer.Length % 2) != 0)
            {
                Checksum += (UInt32)ByteOperations.ReadUInt8(Buffer, (UInt32)Buffer.Length - 1);
                Hi        = Checksum >> 16;
                if (Hi != 0)
                {
                    Checksum = Hi + (Checksum & 0xFFFF);
                }
            }
            Checksum += (UInt32)Buffer.Length;

            // Write file checksum
            // ByteOperations.WriteUInt32(Buffer, GetChecksumOffset(), Checksum);

            return(Checksum);
        }