Skip to content

Commit

Permalink
Fix some linux issues
Browse files Browse the repository at this point in the history
  • Loading branch information
paladine committed Oct 7, 2024
1 parent 00d8e94 commit c0bb8be
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 21 deletions.
5 changes: 5 additions & 0 deletions assets/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@ package(default_visibility = ["//visibility:public"])
filegroup(
name = "test_dats",
srcs = [
"GALTELA.DAT",
"MBBSEMU.DAT",
"MBBSEMU.DB",
"MBBSEMU.DB.V2",
"MULTIACS.DAT",
"VARIABLE.DAT",
"WCCACMS2.DAT",
"WGSMENU2.DAT",
],
)
26 changes: 6 additions & 20 deletions btrieve/BtrieveDatabase.cc
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,6 @@ static inline uint32_t getPageFromVariableLengthRecordPointer(
static_cast<uint32_t>(data[2]) << 8;
}

static uint16_t getPageOffsetFromFragmentArray(
std::basic_string_view<uint8_t> arrayEntry, bool& nextPointerExists) {
if (arrayEntry[0] == 0xFF && arrayEntry[1] == 0xFF) {
nextPointerExists = false;
return 0xFFFF;
}

uint16_t offset = static_cast<uint16_t>(arrayEntry[0]) |
(static_cast<uint16_t>(arrayEntry[1]) & 0x7F) << 8;
nextPointerExists = (arrayEntry[1] & 0x80) != 0;
return offset;
}

static void append(std::vector<uint8_t>& vector,
std::basic_string_view<uint8_t> data) {
size_t vectorSize = vector.size();
Expand Down Expand Up @@ -110,7 +97,7 @@ BtrieveDatabase::FCRDATA BtrieveDatabase::validateDatabase(

// find the valid FCR in v6
if (v6) {
uint8_t* wholePage = reinterpret_cast<uint8_t*>(_alloca(pageLength));
uint8_t* wholePage = reinterpret_cast<uint8_t*>(alloca(pageLength));
fseek_s(f, pageLength, SEEK_SET);
fread_s(wholePage, pageLength, f);

Expand Down Expand Up @@ -220,7 +207,7 @@ bool BtrieveDatabase::isUnusedRecord(std::basic_string_view<uint8_t> data) {
return true;
}
// first two bytes are usage count, which will be non-zero if used
uint16_t usageCount = data[0] << 8 || data[1];
uint16_t usageCount = data[0] << 8 | data[1];
return usageCount == 0;
} else if (data.size() >= 4 &&
data.substr(4).find_first_not_of(static_cast<uint8_t>(0)) ==
Expand Down Expand Up @@ -347,7 +334,7 @@ BtrieveError BtrieveDatabase::parseDatabase(

bool BtrieveDatabase::loadPAT(FILE* f, std::string& acsName,
std::vector<char>& acs) {
uint8_t* pat1 = reinterpret_cast<uint8_t*>(_alloca(pageLength * 2));
uint8_t* pat1 = reinterpret_cast<uint8_t*>(alloca(pageLength * 2));
uint8_t* pat2 = pat1 + pageLength; // pat2 is sequentially after pat1

fseek_s(f, pageLength * 2, SEEK_SET); // starts on third page
Expand All @@ -367,7 +354,6 @@ bool BtrieveDatabase::loadPAT(FILE* f, std::string& acsName,
uint16_t usageCount2 = toUint16(pat2 + 4);
// scan page type code to find ACS/Index/etc pages
uint8_t* activePat = (usageCount1 > usageCount2) ? pat1 : pat2;
uint16_t sequenceNumber = activePat[2] << 8 | activePat[3];

// enumerate all pages
for (int i = 8; i < pageLength; i += 4) {
Expand Down Expand Up @@ -409,7 +395,7 @@ bool BtrieveDatabase::loadACSAtPhysicalOffset(FILE* f, std::string& acsName,
uint32_t physicalOffset) {
static const uint8_t ACS_PAGE_HEADER[] = {0, 0, 1, 0, 0, 0, 0xAC};

char* acsPage = reinterpret_cast<char*>(_alloca(pageLength));
char* acsPage = reinterpret_cast<char*>(alloca(pageLength));

fseek_s(f, physicalOffset, SEEK_SET);
fread_s(acsPage, pageLength, f);
Expand Down Expand Up @@ -572,7 +558,7 @@ static inline int32_t getVRecordPage(VRECPTR* x) {
void BtrieveDatabase::getVariableLengthData(
FILE* f, std::basic_string_view<uint8_t> recordData,
std::vector<uint8_t>& stream) {
uint8_t* const data = reinterpret_cast<uint8_t*>(_alloca(pageLength));
uint8_t* const data = reinterpret_cast<uint8_t*>(alloca(pageLength));
const unsigned int filePosition = ftell(f);
VRECPTR Vrec =
*reinterpret_cast<const VRECPTR*>(recordData.data() + recordLength);
Expand Down Expand Up @@ -649,7 +635,7 @@ int32_t BtrieveDatabase::logicalPageToPhysicalOffset(FILE* f,
ret += (pageLength / 4u);
}

uint8_t* pat1 = reinterpret_cast<uint8_t*>(_alloca(pageLength * 2));
uint8_t* pat1 = reinterpret_cast<uint8_t*>(alloca(pageLength * 2));
uint8_t* pat2 = pat1 + pageLength;

const uint32_t physicalOffset = ret * pageLength;
Expand Down
2 changes: 1 addition & 1 deletion btrieve/BtrieveDatabase_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ TEST(BtrieveDatabase, LoadsVariableDatV6) {
BtrieveDatabase database;

std::unordered_map<std::string, uint32_t> expectedData;
for (int i = 0; i < sizeof(variableData) / sizeof(variableData[0]); ++i) {
for (size_t i = 0; i < sizeof(variableData) / sizeof(variableData[0]); ++i) {
expectedData[variableData[i].key] = variableData[i].recordLength;
}

Expand Down
4 changes: 4 additions & 0 deletions btrieve/SqliteDatabase.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
#include "SqliteUtil.h"
#include "sqlite/sqlite3.h"

#ifndef WIN32
#include <unistd.h>
#endif

namespace btrieve {

static const unsigned int CURRENT_VERSION = 3;
Expand Down

0 comments on commit c0bb8be

Please sign in to comment.