Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Btrieve v6 support #1

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open

Btrieve v6 support #1

wants to merge 7 commits into from

Conversation

paladine
Copy link

Not sure if variable data length reading is 100% correct, but it can handle simple fixed length v6 databases.

Give it a run through and tell me how bad my C# is

@paladine paladine requested a review from enusbaum November 22, 2023 20:46
Copy link
Member

@enusbaum enusbaum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a lot of var-ing that's needed and some smaller stuff.

Looking good!

@@ -4,6 +4,8 @@
using MBBSEmu.Btrieve.Enums;
using Microsoft.Extensions.Logging;
using System.Text;
using System.ComponentModel;
using System.Net.Mail;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think these new imports are needed

@@ -143,6 +147,16 @@ public ushort KeyCount
}
}

public ushort DupOffset
{
get => BitConverter.ToUInt16(_fcr, 0x72);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can make these expression-bodied members:

public ushort DupOffset => BitConverter.ToUInt16(_fcr, 0x72);

var usageCount2 = Data[PageLength + 4] | Data[PageLength + 5] << 8 | Data[PageLength + 6] << 16 | Data[PageLength + 7] << 24;
// get the FCR
if (usageCount1 > usageCount2)
Data.AsSpan().Slice(0, PageLength).CopyTo(_fcr.AsSpan());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dealers choice on this one, but you can use a range indexer on these now vs. slice if it starts from 0

Data.AsSpan()[..PageLength].CopyTo(_fcr.AsSpan());

LogKeyPresent = (btrieveFileContentSpan[0x10C] == 1);
LogKeyPresent = (_fcr[0x10C] == 1);

int[] keyOffsets = new int[KeyCount];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

var all the things

// ACS page immediately follows FCR (the first)
var offset = PageLength;
var data = Data.AsSpan().Slice(offset);
Span<Byte> pat1 = Data.AsSpan().Slice(PageLength * 2, PageLength);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

var all the things

}
}

private bool LoadACS(ILogger logger, bool v6, int pageNumber)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be made void as the return type looks like it's never evaluated

catch (ArgumentException ex)
{
if (!allowCorruptedRecords)
throw ex;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should just be throw if you want to throw the exception that was caught

/// <summary>
/// Gets the complete variable length data from the specified <paramref name="recordOffset"/>,
/// walking through all data pages and returning the concatenated data.
/// </summary>
/// <param name="first">Fixed record pointer offset of the record from a data page</param>
/// <param name="stream">MemoryStream containing the fixed record data already read.</param>
private byte[] GetVariableLengthData(uint recordOffset, MemoryStream stream) {
private byte[] GetVariableLengthData(uint recordOffset, MemoryStream stream, bool v6) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit Update the XML documentation to match the new signature

@@ -554,8 +736,12 @@ private bool IsUnusedRecord(ReadOnlySpan<byte> fixedRecordData)
var offsetPointer = (uint)PageLength - 2u * (fragment + 1u);
var (offset, nextPointerExists) = GetPageOffsetFromFragmentArray(page.Slice((int)offsetPointer, 2));

// check offset for corruption now?
if (offset < 0xC)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because of this, we can remove the check on 763

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants