Esempio n. 1
0
        private bool VerifyFileChecksum(byte[] Image, UInt32 Offset)
        {
            // This function only checks fixed checksum-values 0x55 and 0xAA.

            UInt16 FileHeaderSize = 0x18;
            UInt32 FileSize       = ByteOperations.ReadUInt24(Image, Offset + 0x14);

            byte[] Header = new byte[FileHeaderSize - 1];
            System.Buffer.BlockCopy(Image, (int)Offset, Header, 0, FileHeaderSize - 1);
            ByteOperations.WriteUInt16(Header, 0x10, 0); // Clear checksum
            byte CurrentHeaderChecksum    = ByteOperations.ReadUInt8(Image, Offset + 0x10);
            byte CalculatedHeaderChecksum = ByteOperations.CalculateChecksum8(Header, 0, (UInt32)FileHeaderSize - 1);

            if (CurrentHeaderChecksum != CalculatedHeaderChecksum)
            {
                return(false);
            }

            byte FileAttribs         = ByteOperations.ReadUInt8(Image, Offset + 0x13);
            byte CurrentFileChecksum = ByteOperations.ReadUInt8(Image, Offset + 0x11);

            if ((FileAttribs & 0x40) > 0)
            {
                // Calculate file checksum
                byte CalculatedFileChecksum = ByteOperations.CalculateChecksum8(Image, Offset + FileHeaderSize, FileSize - FileHeaderSize);
                if (CurrentFileChecksum != CalculatedFileChecksum)
                {
                    return(false);
                }
            }
            else
            {
                // Fixed file checksum
                if ((CurrentFileChecksum != 0xAA) && (CurrentFileChecksum != 0x55))
                {
                    return(false);
                }
            }

            return(true);
        }
Esempio n. 2
0
        private void CalculateFileChecksum(byte[] Image, UInt32 Offset)
        {
            UInt16 FileHeaderSize = 0x18;
            UInt32 FileSize       = ByteOperations.ReadUInt24(Image, Offset + 0x14);

            ByteOperations.WriteUInt16(Image, Offset + 0x10, 0); // Clear checksum
            byte NewChecksum = ByteOperations.CalculateChecksum8(Image, Offset, (UInt32)FileHeaderSize - 1);

            ByteOperations.WriteUInt8(Image, Offset + 0x10, NewChecksum); // File-Header checksum

            byte FileAttribs = ByteOperations.ReadUInt8(Image, Offset + 0x13);

            if ((FileAttribs & 0x40) > 0)
            {
                // Calculate file checksum
                byte CalculatedFileChecksum = ByteOperations.CalculateChecksum8(Image, Offset + FileHeaderSize, FileSize - FileHeaderSize);
                ByteOperations.WriteUInt8(Image, Offset + 0x11, CalculatedFileChecksum);
            }
            else
            {
                // Fixed file checksum
                ByteOperations.WriteUInt8(Image, Offset + 0x11, 0xAA);
            }
        }