Esempio n. 1
0
        public LogPageRstr(byte[] rawBytes, int offset)
        {
            var index = 0x0;

            var sigCheck = BitConverter.ToInt32(rawBytes, index);

            if (sigCheck != RstrSig && sigCheck != ChkdSig)
            {
                {
                    throw new Exception("Invalid signature! Expected 'RSTR|CHKD' signature.");
                }
            }

            Offset = offset;

            index += 4;

            var fixupOffset = BitConverter.ToInt16(rawBytes, index);

            index += 2;
            var numFixupPairs = BitConverter.ToInt16(rawBytes, index);

            index += 2;

            CheckDiskLsn   = BitConverter.ToInt64(rawBytes, index);
            index         += 8;
            SystemPageSize = BitConverter.ToInt32(rawBytes, index);
            index         += 4;

            LogPageSize = BitConverter.ToInt32(rawBytes, index);
            index      += 4;

            RestartOffset = BitConverter.ToInt16(rawBytes, index);
            index        += 2;

            MinorFormatVersion = BitConverter.ToInt16(rawBytes, index);
            index += 2;

            MajorFormatVersion = BitConverter.ToInt16(rawBytes, index);
            index += 2;



            var fixupTotalLength = numFixupPairs * 2;

            var fixupBuffer = new byte[fixupTotalLength];

            Buffer.BlockCopy(rawBytes, fixupOffset, fixupBuffer, 0, fixupTotalLength);

            var fixupData = new FixupData(fixupBuffer);

            var fixupOk = true;

            //fixup verification
            var counter = 512;

            foreach (var bytese in fixupData.FixupActual)
            {
                //adjust the offset to where we need to check
                var fixupOffset1 = counter - 2;

                var expected = BitConverter.ToInt16(rawBytes, fixupOffset1);
                if (expected != fixupData.FixupExpected)
                {
                    fixupOk = false;
                    _logger.Warn($"Fixup values do not match at 0x{fixupOffset1:X}. Expected: 0x{fixupData.FixupExpected:X2}, actual: 0x{expected:X2}");
                }

                //replace fixup expected with actual bytes. bytese has actual replacement values in it.
                Buffer.BlockCopy(bytese, 0, rawBytes, fixupOffset1, 2);

                counter += 512;
            }

            index += fixupTotalLength;

            while (index % 8 != 0)
            {
                index += 1;
            }

            CurrentLsn        = BitConverter.ToInt64(rawBytes, index);
            index            += 8;
            LogClientCount    = BitConverter.ToInt16(rawBytes, index);
            index            += 2;
            ClientFreeList    = BitConverter.ToInt16(rawBytes, index);
            index            += 2;
            ClientInUseList   = BitConverter.ToInt16(rawBytes, index);
            index            += 2;
            Flags             = (RestartFlag)BitConverter.ToInt16(rawBytes, index);
            index            += 2;
            SeqNumBits        = BitConverter.ToInt32(rawBytes, index);
            index            += 4;
            RestartAreaLen    = BitConverter.ToInt16(rawBytes, index);
            index            += 2;
            ClientArrayOffset = BitConverter.ToInt16(rawBytes, index);
            index            += 2;
            LogFileSize       = BitConverter.ToInt64(rawBytes, index);
            index            += 8;
            LastLsnDataLen    = BitConverter.ToInt32(rawBytes, index);
            index            += 4;
            RecordHeaderLen   = BitConverter.ToInt16(rawBytes, index);
            index            += 2;
            LogPageDataOffset = BitConverter.ToInt16(rawBytes, index);
            index            += 2;
            RevisionNumber    = BitConverter.ToInt32(rawBytes, index);

            index         = 0x30 + ClientArrayOffset;
            ClientRecords = new List <ClientRecord>();

            for (var i = 0; i < LogClientCount; i++)
            {
                var buff = new byte[160]; //len of clientRecord

                Buffer.BlockCopy(rawBytes, index, buff, 0, 160);

                var cr = new ClientRecord(buff);
                ClientRecords.Add(cr);
                index += 160;
            }
        }
Esempio n. 2
0
        public LogPageRcrd(byte[] rawBytes, int offset)
        {
            var index    = 0x0;
            var sigCheck = BitConverter.ToInt32(rawBytes, index);

            if (sigCheck != RcrdSig)
            {
                throw new Exception("Invalid signature! Expected 'RCRD' signature.");
            }

            Offset = offset;

            index += 4;

            var fixupOffset = BitConverter.ToInt16(rawBytes, index);

            index += 2;
            var numFixupPairs = BitConverter.ToInt16(rawBytes, index);

            index += 2;

            LastLogFileSequenceNumber = BitConverter.ToInt64(rawBytes, index);
            index += 8;
            Flags  = BitConverter.ToInt32(rawBytes, index);
            index += 4;

            PageCount = BitConverter.ToInt16(rawBytes, index);
            index    += 2;

            PagePosition = BitConverter.ToInt16(rawBytes, index);
            index       += 2;

            FreeSpaceOffset = BitConverter.ToInt16(rawBytes, index);
            index          += 2;

            var wordAlign = BitConverter.ToInt16(rawBytes, index);

            index += 2;

            var dwordAlign = BitConverter.ToInt32(rawBytes, index);

            index += 4;

            LastEndLogFileSequenceNumber = BitConverter.ToInt64(rawBytes, index);
            index += 8;

            var fixupTotalLength = numFixupPairs * 2;

            var fixupBuffer = new byte[fixupTotalLength];

            Buffer.BlockCopy(rawBytes, fixupOffset, fixupBuffer, 0, fixupTotalLength);

            var fixupData = new FixupData(fixupBuffer);

            var fixupOk = true;

            //fixup verification
            var counter = 512;

            foreach (var bytese in fixupData.FixupActual)
            {
                //adjust the offset to where we need to check
                var fixupOffset1 = counter - 2;

                var expected = BitConverter.ToInt16(rawBytes, fixupOffset1);
                if (expected != fixupData.FixupExpected)
                {
                    fixupOk = false;
                    _logger.Warn($"Fixup values do not match at 0x{fixupOffset1:X}. Expected: 0x{fixupData.FixupExpected:X2}, actual: 0x{expected:X2}");
                }

                //replace fixup expected with actual bytes. bytese has actual replacement values in it.
                Buffer.BlockCopy(bytese, 0, rawBytes, fixupOffset1, 2);

                counter += 512;
            }

            index += fixupTotalLength;

            while (index % 8 != 0)
            {
                index += 1;
            }

            //header is 0x58 bytes, so go past it

            // index = 0x58;

            _logger.Info($"   LastLogFileSequenceNumber: 0x{LastLogFileSequenceNumber:X} Flags: {Flags} PageCount: 0x{PageCount:X} PagePosition: 0x{PagePosition:X} Free space offset: 0x{FreeSpaceOffset:X} LastEndLogFileSequenceNumber: 0x{LastEndLogFileSequenceNumber:X} LastLogFileSequenceNumber==LastEndLogFileSequenceNumber: {LastEndLogFileSequenceNumber==LastLogFileSequenceNumber}");

            //record is 0x30 + clientDatalen long



            Records = new List <Record>();

            while (index < rawBytes.Length)
            {
                var so            = index;
                var thisLsn       = BitConverter.ToInt64(rawBytes, index);
                var prevLsn       = BitConverter.ToInt64(rawBytes, index + 8);
                var clientUndoLsn = BitConverter.ToInt64(rawBytes, index + 16);

                //     _logger.Info($"     this: {thisLsn:X} prev: {prevLsn:X} undo: {clientUndoLsn:X}");
                var clientDataLen = BitConverter.ToInt32(rawBytes, index + 24);
                var buff          = new byte[clientDataLen + 0x30];
                Buffer.BlockCopy(rawBytes, index, buff, 0, buff.Length);

                var rec = new Record(buff);

                Records.Add(rec);

                index += buff.Length;

                _logger.Info($"     Record: {rec}");

                if (thisLsn == LastEndLogFileSequenceNumber)
                {
                    _logger.Warn($"At last LSN in this page (0x{thisLsn:X}). found it at offset 0x{so:X}\r\n");
                    break;
                }
            }



            //Debug.WriteLine($"at abs offset: 0x{(offset+index):X}, RCRD Offset: 0x{Offset:X}");
        }