Skip to content

Commit c5c9998

Browse files
committed
Fix #9, Fix Petrification Logic, change bbfcodec error messages, fix --offsets option
1 parent f9ed625 commit c5c9998

2 files changed

Lines changed: 62 additions & 57 deletions

File tree

src/bbfcodec.cpp

Lines changed: 59 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ BBFBuilder::BBFBuilder(const char* oFile, uint32_t alignment, uint32_t reamSize,
3636

3737
if ( !this->file )
3838
{
39-
fprintf(stderr, "[BBFMUX] Could not open file: %s\n", oFile);
39+
fprintf(stderr, "[BBFCODEC] Could not open file: %s\n", oFile);
4040
exit(1);
4141
}
4242

@@ -66,7 +66,7 @@ BBFBuilder::BBFBuilder(const char* oFile, uint32_t alignment, uint32_t reamSize,
6666

6767
if (written != sizeof(BBFHeader))
6868
{
69-
fprintf(stderr, "[BBFMUX] Failed to write blank header to %s\n",oFile);
69+
fprintf(stderr, "[BBFCODEC] Failed to write blank header to %s\n",oFile);
7070
exit(1);
7171
}
7272

@@ -115,7 +115,7 @@ void BBFBuilder::growAssets()
115115
// Memory error.
116116
if (!pAsset)
117117
{
118-
fprintf(stderr, "[BBFMUX] Unable to allocate %zu bytes for assets.", newCap);
118+
fprintf(stderr, "[BBFCODEC] Unable to allocate %zu bytes for assets.", newCap);
119119
exit(1);
120120
}
121121

@@ -132,7 +132,7 @@ void BBFBuilder::growPages()
132132

133133
if (!pPage)
134134
{
135-
fprintf(stderr, "[BBFMUX] Unable to allocate %zu bytes for page sector.", newCap);
135+
fprintf(stderr, "[BBFCODEC] Unable to allocate %zu bytes for page sector.", newCap);
136136
exit(1);
137137
}
138138

@@ -148,7 +148,7 @@ void BBFBuilder::growSections()
148148

149149
if (!pSection)
150150
{
151-
fprintf(stderr, "[BBFMUX] Unable to allocate %zu bytes for section sector.", newCap);
151+
fprintf(stderr, "[BBFCODEC] Unable to allocate %zu bytes for section sector.", newCap);
152152
exit(1);
153153
}
154154

@@ -164,7 +164,7 @@ void BBFBuilder::growMeta()
164164

165165
if (!pMeta)
166166
{
167-
fprintf(stderr, "[BBFMUX] Unable to allocate %zu bytes for metadata sector.", newCap);
167+
fprintf(stderr, "[BBFCODEC] Unable to allocate %zu bytes for metadata sector.", newCap);
168168
exit(1);
169169
}
170170

@@ -262,7 +262,7 @@ bool BBFBuilder::addPage(const char* fPath, uint32_t pFlags, uint32_t aFlags)
262262

263263
if (!iImg)
264264
{
265-
fprintf(stderr, "[BBFMUX] Unable to open %s for reading.\n", fPath);
265+
fprintf(stderr, "[BBFCODEC] Unable to open %s for reading.\n", fPath);
266266
return false;
267267
}
268268

@@ -403,7 +403,7 @@ bool BBFBuilder::addSection(const char* sectionName, uint64_t startIndex, const
403403

404404
if (startIndex > this->pageCount)
405405
{
406-
printf("[BBFMUX] Cannot add section %s: Index out of bounds.", sectionName);
406+
printf("[BBFCODEC] Cannot add section %s: Index out of bounds.", sectionName);
407407
return false;
408408
}
409409

@@ -449,7 +449,7 @@ bool BBFBuilder::finalize()
449449

450450
if (this->assetCount == 0)
451451
{
452-
printf("[BBFMUX] No assets to finalize.");
452+
printf("[BBFCODEC] No assets to finalize.");
453453
return false;
454454
}
455455

@@ -598,7 +598,7 @@ bool BBFBuilder::petrifyFile(const char* iPath, const char* oPath)
598598

599599
if (!sourceBBF)
600600
{
601-
fprintf(stderr, "[BBFMUX] Cannot petrify an already open file.\n");
601+
fprintf(stderr, "[BBFCODEC] Cannot petrify an already open file.\n");
602602
return false;
603603
}
604604

@@ -609,21 +609,21 @@ bool BBFBuilder::petrifyFile(const char* iPath, const char* oPath)
609609
if (headerSize != sizeof(BBFHeader))
610610
{
611611
fclose(sourceBBF);
612-
fprintf(stderr, "[BBFMUX] Invalid Header.\n");
612+
fprintf(stderr, "[BBFCODEC] Invalid Header.\n");
613613
return false;
614614
}
615615

616616
//Check Magic
617617
if (header.magic[0] != 'B' || header.magic[1] != 'B' || header.magic[2] != 'F' || header.magic[3] != '3')
618618
{
619-
fprintf(stderr, "[BBFMUX] Invalid Magic Detected. Closing File.\n");
619+
fprintf(stderr, "[BBFCODEC] Invalid Magic Detected. Closing File.\n");
620620
fclose(sourceBBF);
621621
return false;
622622
}
623623

624624
if (header.flags & BBF::BBF_PETRIFICATION_FLAG)
625625
{
626-
fprintf(stderr, "[BBFMUX] File Already Petrified. Closing File.\n");
626+
fprintf(stderr, "[BBFCODEC] File Already Petrified. Closing File.\n");
627627
fclose(sourceBBF);
628628
return false;
629629
}
@@ -637,50 +637,73 @@ bool BBFBuilder::petrifyFile(const char* iPath, const char* oPath)
637637
{
638638
// invalid footer
639639
fclose(sourceBBF);
640-
fprintf(stderr, "[BBFMUX] Invalid Footer.\n");
640+
fprintf(stderr, "[BBFCODEC] Invalid Footer.\n");
641641
return false;
642642
}
643643

644644
// get first offset.
645645
uint64_t indexStart = footer.assetOffset;
646-
uint64_t newIndexStart = sizeof(BBFHeader);
646+
// size of both header + footer combined
647+
uint64_t newIndexStart = sizeof(BBFHeader) + sizeof(BBFFooter);
647648

648649
fseek(sourceBBF, 0, SEEK_END);
649650
long endPos = ftell(sourceBBF);
650651

651652
if (endPos < 0)
652653
{
653654
fclose(sourceBBF);
654-
fprintf(stderr, "[BBFMUX] ftell failed. Got: %ld\n", endPos);
655+
fprintf(stderr, "[BBFCODEC] ftell failed. Got: %ld\n", endPos);
655656
return false;
656657
}
657658

658659
uint64_t fileSize = (uint64_t)endPos;
659660

660-
uint64_t indexSize = fileSize - indexStart;
661+
uint64_t indexSize = header.footerOffset - indexStart; // don't copy footer
661662
uint64_t dataSize = indexStart - header.headerLen;
662663

663664
FILE* tmpBBF = fopen(tmpPath, "wb+");
664665
if (!tmpBBF)
665666
{
666667
fclose(sourceBBF);
667-
fprintf(stderr, "[BBFMUX] Failed to open petrified.bbf.tmp\n");
668+
fprintf(stderr, "[BBFCODEC] Failed to open petrified.bbf.tmp\n");
668669
return false;
669670
}
670671

671672
BBFHeader newHeader = header;
673+
BBFFooter newFooter = footer; // make new footer
672674
newHeader.flags |= BBF::BBF_PETRIFICATION_FLAG;
673675

674-
newHeader.footerOffset = sizeof(BBFHeader) + indexSize - sizeof(BBFFooter);
676+
newHeader.footerOffset = sizeof(BBFHeader); // + indexSize - sizeof(BBFFooter);
677+
// Write header
675678
fwrite(&newHeader, 1, sizeof(BBFHeader), tmpBBF);
676679

680+
// Read footer, calculate shifts
681+
int64_t shiftIndex = (int64_t)newIndexStart - (int64_t)indexStart;
682+
683+
uint64_t newDataStart = newIndexStart + indexSize;
684+
int64_t shiftData = (int64_t)newDataStart - (int64_t)header.headerLen;
685+
686+
fseek(tmpBBF, (long)newHeader.footerOffset, SEEK_SET);
687+
688+
// Shift offsets
689+
// TODO: Alignment, maybe?
690+
newFooter.assetOffset += shiftIndex;
691+
newFooter.pageOffset += shiftIndex;
692+
newFooter.sectionOffset += shiftIndex;
693+
newFooter.metaOffset += shiftIndex;
694+
newFooter.expansionOffset = (newFooter.expansionOffset == 0) ? 0 : newFooter.expansionOffset + shiftIndex;
695+
newFooter.stringPoolOffset += shiftIndex;
696+
697+
// Write footer
698+
fwrite(&newFooter, 1, sizeof(BBFFooter), tmpBBF);
699+
677700
// copy index
678701
fseek(sourceBBF, (long)indexStart, SEEK_SET);
679702
if (!copyRange(sourceBBF, tmpBBF, indexSize))
680703
{
681704
fclose(sourceBBF);
682705
fclose(tmpBBF);
683-
fprintf(stderr, "[BBFMUX] Could not copy Index to TMPBBF\n");
706+
fprintf(stderr, "[BBFCODEC] Could not copy Index to TMPBBF\n");
684707
return false;
685708
}
686709

@@ -690,30 +713,10 @@ bool BBFBuilder::petrifyFile(const char* iPath, const char* oPath)
690713
{
691714
fclose(sourceBBF);
692715
fclose(tmpBBF);
693-
fprintf(stderr, "[BBFMUX] Could not copy header bytes to tmpBBF.\n");
716+
fprintf(stderr, "[BBFCODEC] Could not copy header bytes to tmpBBF.\n");
694717
return false;
695718
}
696719

697-
int64_t shiftIndex = (int64_t)newIndexStart - (int64_t)indexStart;
698-
int64_t shiftData = (int64_t)indexSize;
699-
700-
fseek(tmpBBF, (long)newHeader.footerOffset, SEEK_SET);
701-
702-
BBFFooter newFooter;
703-
fread(&newFooter, 1, sizeof(BBFFooter), tmpBBF);
704-
705-
// Shift offsets
706-
// TODO: Alignment, maybe?
707-
newFooter.assetOffset += shiftIndex;
708-
newFooter.pageOffset += shiftIndex;
709-
newFooter.sectionOffset += shiftIndex;
710-
newFooter.metaOffset += shiftIndex;
711-
newFooter.expansionOffset = 0;
712-
newFooter.stringPoolOffset += shiftIndex;
713-
714-
fseek(tmpBBF, -((long)sizeof(BBFFooter)), SEEK_CUR);
715-
fwrite(&newFooter, 1, sizeof(BBFFooter), tmpBBF);
716-
717720
fseek(tmpBBF, (long)newFooter.assetOffset, SEEK_SET);
718721
BBFAsset assetBuffer[64];
719722

@@ -727,7 +730,7 @@ bool BBFBuilder::petrifyFile(const char* iPath, const char* oPath)
727730

728731
if (readCount != assetBatch)
729732
{
730-
fprintf(stderr, "[BBFMUX] Error patching assets.\n");
733+
fprintf(stderr, "[BBFCODEC] Error patching assets.\n");
731734
fclose(sourceBBF);
732735
fclose(tmpBBF);
733736
remove(tmpPath);
@@ -754,13 +757,15 @@ bool BBFBuilder::petrifyFile(const char* iPath, const char* oPath)
754757

755758
if (rename("petrified.bbf.tmp", oPath) != 0)
756759
{
757-
fprintf(stderr, "[BBFMUX] Could not rename temp file. Result is in 'petrified.bbf.tmp'\n");
760+
fprintf(stderr, "[BBFCODEC] Could not rename temp file. Result is in 'petrified.bbf.tmp'\n");
758761
return false;
759762
}
760763

761764
return true;
762765
}
763766

767+
768+
764769
// READER FUNCTIONS
765770

766771
BBFReader::BBFReader(const char* iFile)
@@ -775,7 +780,7 @@ BBFReader::BBFReader(const char* iFile)
775780

776781
if (this->hFile == INVALID_HANDLE_VALUE)
777782
{
778-
fprintf(stderr, "[BBFMUX] Unable to open file %s\n", iFile);
783+
fprintf(stderr, "[BBFCODEC] Unable to open file %s\n", iFile);
779784
return;
780785
}
781786

@@ -789,15 +794,15 @@ BBFReader::BBFReader(const char* iFile)
789794

790795
if (this->hMap == NULL)
791796
{
792-
fprintf(stderr, "[BBFMUX] Failed to map file (CreateFileMapping)\n");
797+
fprintf(stderr, "[BBFCODEC] Failed to map file (CreateFileMapping)\n");
793798
CloseHandle(this->hFile);
794799
return;
795800
}
796801

797802
this->fileBuffer = (uint8_t*)MapViewOfFile(this->hMap, FILE_MAP_READ, 0, 0, 0);
798803
if (this->fileBuffer == NULL)
799804
{
800-
fprintf(stderr, "[BBFMUX] Failed to map file (MapViewOfFile)\n");
805+
fprintf(stderr, "[BBFCODEC] Failed to map file (MapViewOfFile)\n");
801806
CloseHandle(this->hMap);
802807
CloseHandle(this->hFile);
803808
return;
@@ -806,14 +811,14 @@ BBFReader::BBFReader(const char* iFile)
806811
this->fileDescriptor = open(iFile, O_RDONLY);
807812
if (this->fileDescriptor == -1)
808813
{
809-
fprintf(stderr, "[BBFMUX] Unable to open file %s\n", iFile);
814+
fprintf(stderr, "[BBFCODEC] Unable to open file %s\n", iFile);
810815
return;
811816
}
812817

813818
struct stat fileStat;
814819
if (fstat(this->fileDescriptor, &fileStat) == -1)
815820
{
816-
fprintf(stderr, "[BBFMUX] Unable to stat file\n");
821+
fprintf(stderr, "[BBFCODEC] Unable to stat file\n");
817822
close(this->fileDescriptor);
818823
return;
819824
}
@@ -822,7 +827,7 @@ BBFReader::BBFReader(const char* iFile)
822827
void* fMap = mmap(NULL, this->fileSize, PROT_READ, MAP_PRIVATE, this->fileDescriptor, 0);
823828
if (fMap == MAP_FAILED)
824829
{
825-
fprintf(stderr, "[BBFMUX] mmap failed\n");
830+
fprintf(stderr, "[BBFCODEC] mmap failed\n");
826831
close(this->fileDescriptor);
827832
this->fileBuffer = nullptr;
828833
return;
@@ -833,7 +838,7 @@ BBFReader::BBFReader(const char* iFile)
833838

834839
// if (!this->file)
835840
// {
836-
// printf("[BBFMUX] Unable to open file %s", iFile);
841+
// printf("[BBFCODEC] Unable to open file %s", iFile);
837842
// return;
838843
// }
839844

@@ -955,7 +960,7 @@ const char* BBFReader::getStringView(uint64_t strOffset)
955960
{
956961
if (!this->footerCache)
957962
{
958-
printf("[BBFMUX] Cannot Access String Pool. Ensure File is opened.");
963+
printf("[BBFCODEC] Cannot Access String Pool. Ensure File is opened.");
959964
return nullptr;
960965
}
961966

@@ -999,7 +1004,7 @@ XXH128_hash_t BBFReader::computeAssetHash(const BBFAsset* assetView)
9991004

10001005
if (!dataView)
10011006
{
1002-
printf("[BBFMUX] ERROR: Asset data out of bounds");
1007+
printf("[BBFCODEC] ERROR: Asset data out of bounds");
10031008
return {0,0};
10041009
}
10051010

@@ -1011,15 +1016,15 @@ XXH128_hash_t BBFReader::computeAssetHash(uint8_t* assetTableView, int assetInde
10111016
const BBFAsset* assetView = getAssetEntryView(assetTableView, assetIndex);
10121017
if (!assetView)
10131018
{
1014-
printf("[BBFMUX] ERROR: Unable to get pointer for asset %i", assetIndex);
1019+
printf("[BBFCODEC] ERROR: Unable to get pointer for asset %i", assetIndex);
10151020
return {0,0};
10161021
}
10171022

10181023
const uint8_t* dataView = this->getAssetDataView(assetView->fileOffset);
10191024

10201025
if (!dataView)
10211026
{
1022-
printf("[BBFMUX] ERROR: Unable to calculate hash for asset %i", assetIndex);
1027+
printf("[BBFCODEC] ERROR: Unable to calculate hash for asset %i", assetIndex);
10231028
return {0,0};
10241029
}
10251030

src/muxer/bbfmux.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ char* readTxtFile(const char* fPath)
6060
if(!file)
6161
{
6262
// can't open file
63-
printf("[BBFMUX] Unable to read text file: %s", fPath);
63+
printf("[BBFMUX] Unable to read text file: %s\n", fPath);
6464
return 0;
6565
}
6666

@@ -73,7 +73,7 @@ char* readTxtFile(const char* fPath)
7373

7474
if (readSize != fileSize)
7575
{
76-
printf("[BBFMUX] Unable to read text file: %s", fPath);
76+
printf("[BBFMUX] Unable to read text file: %s\n", fPath);
7777
return 0;
7878
}
7979

@@ -444,7 +444,7 @@ int main(int argc, char** argv)
444444
case val32("--header"): cfg.info.showHeader = true; break;
445445
case val32("--footer"): cfg.info.showFooter = true; break;
446446
case val32("--strings"): cfg.info.showStringPool = true; break;
447-
case val32("--offsets"): cfg.info.showStringPool = true; break;
447+
case val32("--offsets"): cfg.info.showOffsets = true; break;
448448
}
449449
}
450450

0 commit comments

Comments
 (0)