Beispiel #1
0
        private void runChecks()
        {
            // look at the position for the initial header first
            byte[] buffer = new byte[i.sectorSize];

            i.Seek(i.sectorSize, SeekOrigin.Begin);
            i.Read(buffer, 0, buffer.Length);

            int headersize = BitConverter.ToInt32(buffer, 0x0C);
            uint headercheck = BitConverter.ToUInt32(buffer, 0x10);

            // checksum of header is calculated with the checksum field set to zero
            Array.Copy(new byte[] { 0, 0, 0, 0 }, 0x0, buffer, 0x10, 4);

            // reserved field is not included
            byte[] headerbuffer = new byte[headersize];
            Array.Copy(buffer, headerbuffer, headerbuffer.Length);

            Crc32 headercrc = new Crc32();
            headercrc.ComputeHash(headerbuffer);
            if (headercrc.CrcValue == headercheck)
            {
                this.headerFound = true;
                // if the header is valid, add the header
                this.headerMatchesCRC = true;

                buffer = new byte[i.sectorSize];

                i.Seek(i.sectorSize, SeekOrigin.Begin);
                i.Read(buffer, 0, i.sectorSize);
                this.header = getHeader(buffer, i, this.header);
                this.tablestart = header.tablestart;

                // make sure the table CRC matches as well
                byte[] rawTable = new byte[tablelength];
                i.Seek(header.tablestart * i.sectorSize, SeekOrigin.Begin);
                i.Read(rawTable, 0, tablelength);

                Crc32 tablecrc = new Crc32();
                tablecrc.ComputeHash(rawTable);
                if (tablecrc.CrcValue == header.tablecheck)
                {
                    // if the table is valid, add the entries
                    this.tableMatchesCRC = true;
                    this.entries = buildTable(rawTable);
                }
            }

            // then do the same for the backup header
            buffer = new byte[i.sectorSize];
            if (this.headerMatchesCRC)
            {
                // if the original header is valid, use the value from it to find the backup header
                i.Seek(this.header.backupheader * i.sectorSize, SeekOrigin.Begin);
            }
            else
            {
                // if it is not valid, look 512 bytes from the end of the image
                i.Seek(-i.sectorSize, SeekOrigin.End);
            }
            i.Read(buffer, 0, buffer.Length);

            int backupheadersize = BitConverter.ToInt32(buffer, 0x0C);
            uint backupheadercheck = BitConverter.ToUInt32(buffer, 0x10);

            // checksum of header is calculated with the checksum field set to zero
            Array.Copy(new byte[] { 0, 0, 0, 0 }, 0x0, buffer, 0x10, 4);

            // reserved field is not included
            byte[] backupheaderbuffer = new byte[headersize];
            Array.Copy(buffer, backupheaderbuffer, backupheaderbuffer.Length);

            Crc32 backupheadercrc = new Crc32();
            backupheadercrc.ComputeHash(backupheaderbuffer);
            if (backupheadercrc.CrcValue == backupheadercheck)
            {
                // if the header is valid, add the header
                this.backupMatchesCRC = true;
                this.backupFound = true;

                buffer = new byte[i.sectorSize];

                // go back 512 bytes because previous read incremented position by 512
                i.Seek(-i.sectorSize, SeekOrigin.Current);
                i.Read(buffer, 0, buffer.Length);
                this.backupHeader = getHeader(buffer, i, this.backupHeader);

                // make sure the table CRC matches as well
                byte[] rawTable = new byte[tablelength];
                i.Seek(backupHeader.tablestart * i.sectorSize, SeekOrigin.Begin);
                i.Read(rawTable, 0, tablelength);

                Crc32 tablecrc = new Crc32();
                tablecrc.ComputeHash(rawTable);
                if (tablecrc.CrcValue == backupHeader.tablecheck)
                {
                    // if the table is valid, add the entries
                    this.backupTableMatchesCRC = true;

                    this.backupEntries = buildTable(rawTable);
                }
            }

            i.Seek(0, SeekOrigin.Begin);
            i.Read(buffer, 0, i.sectorSize);
            if (buffer[buffer.Length - 2] == 0x55 && buffer[buffer.Length - 1] == 0xAA)
            {
                this.protectiveMBRExists = true;
            }
        }
Beispiel #2
0
        private void runChecks()
        {
            // look at the position for the initial header first
            byte[] buffer = new byte[i.sectorSize];

            i.Seek(i.sectorSize, SeekOrigin.Begin);
            i.Read(buffer, 0, buffer.Length);

            int  headersize  = BitConverter.ToInt32(buffer, 0x0C);
            uint headercheck = BitConverter.ToUInt32(buffer, 0x10);

            // checksum of header is calculated with the checksum field set to zero
            Array.Copy(new byte[] { 0, 0, 0, 0 }, 0x0, buffer, 0x10, 4);

            // reserved field is not included
            byte[] headerbuffer = new byte[headersize];
            Array.Copy(buffer, headerbuffer, headerbuffer.Length);

            Crc32 headercrc = new Crc32();

            headercrc.ComputeHash(headerbuffer);
            if (headercrc.CrcValue == headercheck)
            {
                this.headerFound = true;
                // if the header is valid, add the header
                this.headerMatchesCRC = true;

                buffer = new byte[i.sectorSize];

                i.Seek(i.sectorSize, SeekOrigin.Begin);
                i.Read(buffer, 0, i.sectorSize);
                this.header     = getHeader(buffer, i, this.header);
                this.tablestart = header.tablestart;

                // make sure the table CRC matches as well
                byte[] rawTable = new byte[tablelength];
                i.Seek(header.tablestart * i.sectorSize, SeekOrigin.Begin);
                i.Read(rawTable, 0, tablelength);

                Crc32 tablecrc = new Crc32();
                tablecrc.ComputeHash(rawTable);
                if (tablecrc.CrcValue == header.tablecheck)
                {
                    // if the table is valid, add the entries
                    this.tableMatchesCRC = true;
                    this.entries         = buildTable(rawTable);
                }
            }

            // then do the same for the backup header
            buffer = new byte[i.sectorSize];
            if (this.headerMatchesCRC)
            {
                // if the original header is valid, use the value from it to find the backup header
                i.Seek(this.header.backupheader * i.sectorSize, SeekOrigin.Begin);
            }
            else
            {
                // if it is not valid, look 512 bytes from the end of the image
                i.Seek(-i.sectorSize, SeekOrigin.End);
            }
            i.Read(buffer, 0, buffer.Length);

            int  backupheadersize  = BitConverter.ToInt32(buffer, 0x0C);
            uint backupheadercheck = BitConverter.ToUInt32(buffer, 0x10);

            // checksum of header is calculated with the checksum field set to zero
            Array.Copy(new byte[] { 0, 0, 0, 0 }, 0x0, buffer, 0x10, 4);

            // reserved field is not included
            byte[] backupheaderbuffer = new byte[headersize];
            Array.Copy(buffer, backupheaderbuffer, backupheaderbuffer.Length);

            Crc32 backupheadercrc = new Crc32();

            backupheadercrc.ComputeHash(backupheaderbuffer);
            if (backupheadercrc.CrcValue == backupheadercheck)
            {
                // if the header is valid, add the header
                this.backupMatchesCRC = true;
                this.backupFound      = true;

                buffer = new byte[i.sectorSize];

                // go back 512 bytes because previous read incremented position by 512
                i.Seek(-i.sectorSize, SeekOrigin.Current);
                i.Read(buffer, 0, buffer.Length);
                this.backupHeader = getHeader(buffer, i, this.backupHeader);

                // make sure the table CRC matches as well
                byte[] rawTable = new byte[tablelength];
                i.Seek(backupHeader.tablestart * i.sectorSize, SeekOrigin.Begin);
                i.Read(rawTable, 0, tablelength);

                Crc32 tablecrc = new Crc32();
                tablecrc.ComputeHash(rawTable);
                if (tablecrc.CrcValue == backupHeader.tablecheck)
                {
                    // if the table is valid, add the entries
                    this.backupTableMatchesCRC = true;

                    this.backupEntries = buildTable(rawTable);
                }
            }

            i.Seek(0, SeekOrigin.Begin);
            i.Read(buffer, 0, i.sectorSize);
            if (buffer[buffer.Length - 2] == 0x55 && buffer[buffer.Length - 1] == 0xAA)
            {
                this.protectiveMBRExists = true;
            }
        }