1+ // BBF Codec
2+ // Builder
3+ #ifndef BBFCODEC_H
4+ #define BBFCODEC_H
5+
6+ #include " xxhash.h"
7+ #include " libbbf.h"
8+ #include " dedupemap.h"
9+ #include " stringpool.h"
10+
11+ // Handle Memory Mapping
12+ #ifdef _WIN32
13+ #define WIN32_LEAN_AND_MEAN
14+ #include < windows.h>
15+ #else
16+ #include < sys/mman.h>
17+ #include < sys/stat.h>
18+ #include < fcntl.h>
19+ #include < unistd.h>
20+ #endif
21+
22+ #include < stdint.h>
23+ #include < stdlib.h>
24+ #include < stdio.h>
25+
26+ class BBFBuilder
27+ {
28+ public:
29+ BBFBuilder (const char * oFile, uint32_t alignment = BBF ::DEFAULT_GUARD_ALIGNMENT , uint32_t reamSize = BBF ::DEFAULT_SMALL_REAM_THRESHOLD , uint32_t hFlags = BBF ::BBF_VARIABLE_REAM_SIZE_FLAG );
30+ ~BBFBuilder (); // Deconstructor.
31+ // TODO: Copy constructor.
32+
33+ // Default flag is variable alignment.
34+ bool addPage (const char * fPath , uint32_t pFlags = 0 , uint32_t aFlags = 0 );
35+ bool addMeta (const char * key, const char * value, const char * parent = nullptr );
36+ bool addSection (const char * sectionName, uint64_t startIndex, const char * parentName = nullptr );
37+
38+ bool finalize ();
39+ bool petrifyFile (const char * iPath, const char * oPath); // Petrify!
40+
41+ // Getters
42+ size_t getAssetCount () { if (!assetCount) {return 0 ;} return assetCount; }
43+ size_t getPageCount () { if (!pageCount) {return 0 ;} return pageCount; }
44+ size_t getSectionCount () { if (!sectionCount) {return 0 ;} return sectionCount; }
45+ size_t getKeyCount () { if (!keyCount) {return 0 ;} return keyCount; }
46+
47+
48+ private:
49+ FILE * file;
50+ uint64_t currentOffset;
51+
52+ BBFStringPool stringPool;
53+ BBFAssetTable assetLookupTable;
54+
55+ // Config from args
56+ uint32_t headerFlags;
57+ uint32_t guardValue;
58+ uint32_t reamValue;
59+
60+ // Values
61+ BBFAsset* assets;
62+ size_t assetCount;
63+ size_t assetCap;
64+
65+ BBFPage* pages; // read order.
66+ size_t pageCount;
67+ size_t pageCap;
68+
69+ BBFSection* sections;
70+ size_t sectionCount;
71+ size_t sectionCap;
72+
73+ BBFMeta* metadata;
74+ size_t keyCount;
75+ size_t keyCap;
76+
77+ void growAssets (); // realloc(this->assets)
78+ void growPages ();
79+ void growSections ();
80+ void growMeta ();
81+
82+ // Other Helpers
83+ void writePadding (uint64_t alignmentBoundary);
84+ uint8_t detectType (const char * iPath);
85+ };
86+
87+ class BBFReader
88+ {
89+ public:
90+ BBFReader (const char * iFile);
91+ ~BBFReader ();
92+ // TODO: Copy constructor.
93+
94+ BBFHeader* getHeaderView () {if (!this ->fileBuffer ){ return nullptr ; } return (BBFHeader*)this ->fileBuffer ; }
95+ BBFFooter* getFooterView (uint64_t fOffset );
96+
97+ // Unsure what type these pointers should be
98+ const uint8_t * getPageTableView (uint64_t pgOffset) { if (!isSafe (pgOffset)){return nullptr ;} return ((const uint8_t *)this ->fileBuffer + pgOffset); }
99+ const uint8_t * getAssetTableView (uint64_t aOffset) { if (!isSafe (aOffset)){return nullptr ;} return ((const uint8_t *)this ->fileBuffer + aOffset); }
100+ const uint8_t * getSectionTableView (uint64_t sOffset ) { if (!isSafe (sOffset )){return nullptr ;} return ((const uint8_t *)this ->fileBuffer + sOffset ); }
101+ const uint8_t * getMetadataView (uint64_t mOffset ) { if (!isSafe (mOffset )){return nullptr ;} return ((const uint8_t *)this ->fileBuffer + mOffset ); }
102+ const uint8_t * getExpansionTableView (uint64_t eOffset) { if (!isSafe (eOffset)){return nullptr ;} return ((const uint8_t *)this ->fileBuffer + eOffset); }
103+
104+ // This, however, is
105+ const BBFAsset* getAssetEntryView (uint8_t * assetTable, int assetIndex) { if (!this ->footerCache ) {return nullptr ;} if (!isSafe (this ->footerCache ->assetCount , assetIndex)) {return nullptr ;} return (const BBFAsset*)(assetTable + sizeof (BBFAsset) * assetIndex); }
106+ const BBFPage* getPageEntryView (uint8_t * pageTable, int pageIndex) { if (!this ->footerCache ) {return nullptr ;} if (!isSafe (this ->footerCache ->pageCount , pageIndex)) {return nullptr ;} return (const BBFPage*)(pageTable + sizeof (BBFPage) * pageIndex); }
107+ const BBFSection* getSectionEntryView (uint8_t * sectionTable, int sectionIndex) { if (!this ->footerCache ) {return nullptr ;} if (!isSafe (this ->footerCache ->sectionCount , sectionIndex)) {return nullptr ;} return ((const BBFSection*)(sectionTable + sizeof (BBFSection) * sectionIndex)); }
108+ const BBFMeta* getMetaEntryView (uint8_t * metaTable, int metaIndex) { if (!this ->footerCache ) {return nullptr ;} if (!isSafe (this ->footerCache ->metaCount , metaIndex)) {return nullptr ;} return ((const BBFMeta*)(metaTable + sizeof (BBFMeta) * metaIndex)); }
109+ const BBFExpansion* getExpansionEntryView (uint8_t * expansionTable, int expansionIndex) { if (!this ->footerCache ) {return nullptr ;} if (!isSafe (this ->footerCache ->expansionCount , expansionIndex)) {return nullptr ;} return ((const BBFExpansion*)(expansionTable + sizeof (BBFExpansion) * expansionIndex)); }
110+
111+ // Gonna Copy the previous functions and make them accept const args cause we aren't modifying them
112+ const BBFAsset* getAssetEntryView (const uint8_t * assetTable, int assetIndex) { if (!this ->footerCache ) {return nullptr ;} if (!isSafe (this ->footerCache ->assetCount , assetIndex)) {return nullptr ;} return (const BBFAsset*)(assetTable + sizeof (BBFAsset) * assetIndex); }
113+ const BBFPage* getPageEntryView (const uint8_t * pageTable, int pageIndex) { if (!this ->footerCache ) {return nullptr ;} if (!isSafe (this ->footerCache ->pageCount , pageIndex)) {return nullptr ;} return (const BBFPage*)(pageTable + sizeof (BBFPage) * pageIndex); }
114+ const BBFSection* getSectionEntryView (const uint8_t * sectionTable, int sectionIndex) { if (!this ->footerCache ) {return nullptr ;} if (!isSafe (this ->footerCache ->sectionCount , sectionIndex)) {return nullptr ;} return ((const BBFSection*)(sectionTable + sizeof (BBFSection) * sectionIndex)); }
115+ const BBFMeta* getMetaEntryView (const uint8_t * metaTable, int metaIndex) { if (!this ->footerCache ) {return nullptr ;} if (!isSafe (this ->footerCache ->metaCount , metaIndex)) {return nullptr ;} return ((const BBFMeta*)(metaTable + sizeof (BBFMeta) * metaIndex)); }
116+ const BBFExpansion* getExpansionEntryView (const uint8_t * expansionTable, int expansionIndex) { if (!this ->footerCache ) {return nullptr ;} if (!isSafe (this ->footerCache ->expansionCount , expansionIndex)) {return nullptr ;} return ((const BBFExpansion*)(expansionTable + sizeof (BBFExpansion) * expansionIndex)); }
117+
118+
119+ // Get asset data
120+ const uint8_t * getAssetDataView (uint64_t fileOffset) { if (!isSafe (fileOffset)){return nullptr ;} return ((const uint8_t *)this ->fileBuffer + fileOffset); }
121+ // Get strings
122+ const char * getStringView (uint64_t strOffset);
123+
124+
125+ // Reader Utilities
126+ bool checkMagic (BBFHeader* pHeader);
127+
128+ // compute asset hashes (xx3-128)
129+ XXH128_hash_t computeAssetHash (const BBFAsset* assetView);
130+ XXH128_hash_t computeAssetHash (uint8_t * assetTableView, int assetIndex);
131+
132+ // compute index hash
133+ // uint64_t computeFooterHash(const uint8_t* indexStartPr);
134+
135+ // FILE* file;
136+
137+ private:
138+
139+ #ifdef _WIN32
140+ HANDLE hFile;
141+ HANDLE hMap;
142+ #else
143+ int fileDescriptor;
144+ #endif
145+
146+ bool isSafe (uint64_t offset, uint64_t size) const ;
147+ bool isSafe (uint64_t count, int index) const ;
148+ bool isSafe (uint64_t offset) const ;
149+
150+ char * getString (uint64_t stringOffset) { if (!isSafe (stringOffset)) {return nullptr ;} return (char *)stringOffset; };
151+
152+ uint8_t * fileBuffer;
153+ BBFFooter* footerCache;
154+ size_t fileSize;
155+
156+ };
157+
158+ #endif // BBFCODEC_H
0 commit comments