Skip to content

Commit e768e98

Browse files
committed
Add format specification
1 parent 9d31abf commit e768e98

1 file changed

Lines changed: 390 additions & 0 deletions

File tree

SPECNOTE.txt

Lines changed: 390 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,390 @@
1+
File: SPECNOTE.TXT
2+
Title: BBF File Specification
3+
Version: 3.0
4+
Revised: 1/27/26
5+
Author: EF1500
6+
7+
1.0 Introduction
8+
----------------
9+
10+
Bound Book Format is a binary container format designed for the
11+
storage, deduplication, and paginated access of media assets.
12+
13+
Bound Book Format (BBF) files support arbitrary metadata,
14+
hierarchical sectioning and linearization (referred to as petrification).
15+
16+
17+
2.0 Notations
18+
-------------
19+
Usage of the term "MUST" or "SHALL" indicate required elements.
20+
21+
"MUST NOT" or "SHALL NOT" indicate elements prohibited from use.
22+
23+
"SHOULD" indicates a RECCOMENDED element.
24+
25+
"SHOULD NOT" indicates and element NOT RECCOMENDED element.
26+
27+
"MAY" indicates an OPTIONAL element.
28+
29+
3.0 BBF Files
30+
-------------
31+
32+
3.1 All multi-byte integer values MUST be stored in Little-Endian format unless otherwise specified.
33+
34+
3.2 Alignment and Reaming
35+
BBF files utilize a power-of-two alignment system. The alignment is stored
36+
as an exponent (n), where the boundary is 2^n bytes.
37+
38+
Any implemented writer MUST prohibit the value of n from exceeding 16, or prompt
39+
the user for confirmation.
40+
41+
Any implemented reader MUST inform the user if the value of n exceeds 16.
42+
43+
If the VARIABLE REAM SIZE flag is set, assets smaller than 2^(ream size) bytes
44+
SHOULD be aligned to an 8-byte boundary to reduce internal fragmentation.
45+
46+
3.3 Layout (Default)
47+
The default BBF file layout is constructed in the following manner:
48+
49+
[BBF Header]
50+
[Asset Data (Aligned)]
51+
[Asset Table]
52+
[Page Table]
53+
[Section Table]
54+
[Metadata Table]
55+
[Expansion Table] (Optional)
56+
[String Pool]
57+
[BBF Footer]
58+
59+
3.4 Layout (Petrified)
60+
Petrified BBF files move the data tables and footer to immedieately
61+
follow the header. Readers SHOULD parse the entire file structure
62+
with one initial block read, provided the index region is less than
63+
sixteen megabytes in size. For larger index sizes, readers SHOULD
64+
verify the footer hash via streaming before allocating memory.
65+
66+
[BBF Header]
67+
[Asset Table]
68+
[Page Table]
69+
[Section Table]
70+
[Metadata Table]
71+
[Expansion Table] (Optional)
72+
[String Table]
73+
[BBF Footer]
74+
[Asset Data (Aligned)]
75+
76+
4.0 BBF Data Structures
77+
-----------------------
78+
79+
Unless otherwise specified
80+
- All integer fields are unsigned
81+
- All reserved fields MUST be written as zero.
82+
- All offsets are absolute file offsets, starting from the beginning of the file.
83+
84+
4.1 BBF Header
85+
The header MUST appear at offset 0 and MUST be 64 bytes in BBF v3.
86+
Readers MAY use header length to validate header size.
87+
88+
Offset Size Description
89+
0 4 bytes Magic Number (0x42424633, "BBF3")
90+
4 2 bytes Format Major Version
91+
6 2 bytes Header Length (Total size of this header)
92+
8 4 bytes Header flags (see 4.1.1)
93+
12 1 byte Alignment exponent, 2^(alignment) bytes
94+
13 1 byte Ream threshold exponent, 2^(ream threshold) bytes
95+
14 2 bytes Reserved. MUST be zero.
96+
16 8 bytes BBF Footer offset.
97+
24 40 bytes Reserved. MUST be zero.
98+
99+
Any implemented reader MUST:
100+
- Validate Magic
101+
- Validate Version == 3
102+
- Use footerOffset to locate the footer.
103+
104+
4.1.1 Header Flags
105+
106+
headerFlags is a 32-bit field with the following flag values
107+
108+
Bit / Mask Name
109+
0x00000001 PETRIFICATION_FLAG
110+
If set, the file uses a Petrified layout (see 3.4)
111+
0x00000002 VARIABLE_REAM_SIZE_FLAG
112+
If set, assets smaller than 2^(ream threshold) bytes SHOULD
113+
be aligned to an 8-byte boundary. Assets equal or larger MUST
114+
be aligned to 2^(alignment) bytes.
115+
116+
All other bits are reserved and MUST be written as zero.
117+
118+
4.2 BBF Footer
119+
120+
The footer must appear at the offset specified by footerOffset in
121+
the header (see 4.1).
122+
123+
The footer provides necessary offsets and counts necessary to locate and
124+
parse the index region.
125+
126+
Offset Size Description
127+
0 8 bytes Offset into the asset table
128+
8 8 bytes Offset into the page table
129+
16 8 bytes Offset into the section table
130+
24 8 bytes Offset into the metadata table
131+
32 8 bytes Offset into the expansion table, or 0
132+
40 8 bytes Offset into string pool region
133+
48 8 bytes Total size of the string pool region in bytes
134+
135+
56 8 bytes Number of asset entries in the asset table
136+
64 8 bytes Number of page entries in the page table
137+
72 8 bytes Number of section entries in the section table
138+
80 8 bytes Number of metadata entries in the metadata table
139+
88 8 bytes Number of entries in the expansion table
140+
141+
96 4 bytes Footer Flags (reserved; MUST be 0)
142+
100 1 byte Footer Length (total length of this structure)
143+
101 3 bytes Padding (MUST be 0)
144+
104 8 bytes XXH3-64 Hash of the Index Region (see 4.2.1)
145+
112 144 bytes Reserved. MUST be set to 0.
146+
147+
Any implemented reader MUST perform the following:
148+
- Utilize the provided offsets to locate each table
149+
- Igore expansion offset if it is 0.
150+
151+
Any implemented writer MUST perform the following:
152+
- set footer length equal to the size of the footer structure, for
153+
BBFv3, the footer is 256 bytes.
154+
155+
Any implemented reader SHOULD inform the user if the footer suggests
156+
more than one million assets are present.
157+
158+
4.2.1 Index Hash (footer hash)
159+
160+
BBF's footer hash is XXH3-64 computed over the serialized bytes of the
161+
index region, excluding the asset data region.
162+
163+
The Index region consists of the Asset Table, Page Table, Section Table,
164+
Metadata Table, and String Pool. It does NOT include the footer itself.
165+
166+
Should a writer include expansion entries, a writer MUST include this
167+
region as well.
168+
169+
Readers MAY validate the footer hash. If validation fails, readers SHOULD
170+
notify the user of a corrupted footer or close the file at once.
171+
172+
NOTE: XXH3-64 is not a cryptographic hash. Writers intending to implement
173+
functionality for cryptographic hashes MAY use the provided expansion
174+
table.
175+
176+
4.3 Asset Table (BBFAsset)
177+
178+
Each stored asset entry describes a unique payload. Assets are deduplicated
179+
using by using XXH3-128 hashes. Multiple pages MAY reference the same asset
180+
index.
181+
182+
Offset Size Description
183+
0 8 bytes Offset to the file data (absolute file offset)
184+
8 16 bytes XXH3-128 Hash (stored as Little-Endian 128-bit integer)
185+
24 8 bytes Size of the file in bytes
186+
32 4 bytes Asset flags (Reserved, writers MUST set to 0)
187+
36 2 bytes Reserved Value (Padding). MUST be zero.
188+
38 1 byte Asset type (See 4.3.1)
189+
39 9 bytes Reserved (Padding). MUST be zero.
190+
191+
Any implemented reader MUST:
192+
- Seek to the offset given in the table to retrieve the file data.
193+
- Read the 128-bit hash as two 64-bit Little-Endian integers if
194+
native 128-bit types are unavailable.
195+
- Ensure all reserved and padding bytes are zero.
196+
197+
Any implemented writer MUST:
198+
- Set reserved bytes to 0
199+
- (Optional) Verify byte payloads if hashes match to guarantee zero collisions.
200+
201+
Any implemented writer MAY use the expansion table (See section 4.7) to add
202+
cryptographic hashes or additional security features.
203+
204+
Asset hashes are chiefly for deduplication and local verification purposes.
205+
Asset hashes MUST NOT be used for secure integrity checks, or be treated as
206+
cryptographically secure. Those self-hosting with exposed endpoints SHOULD NOT
207+
use XXH3-128 hashes for local verification purposes. Individuals self-hosting
208+
with no exposed endpoints MAY use the built-in XXH3-128 asset hashes for local
209+
verification purposes.
210+
211+
4.3.1 Asset Type (BBFMediaType)
212+
213+
Media type is an 8 bit identifier.
214+
215+
Current defined values are as follows, with identifiers given in
216+
hexadecimal.
217+
218+
Identifier Media Type
219+
0x00 UNKNOWN
220+
0x01 AVIF
221+
0x02 PNG
222+
0x03 WEBP
223+
0x04 JXL (JPEG-XL)
224+
0x05 BMP
225+
0x06 (Reserved)
226+
0x07 GIF
227+
0x08 TIFF
228+
0x09 JPG
229+
230+
Values 0x10 through 0xFF are reserved for user defined values. Any implemented
231+
reader MAY support this feature or treat unknown types as UNKNOWN.
232+
233+
Any implemented writer SHOULD allow for user-defined values, or treat unknown types
234+
as UNKNOWN.
235+
236+
4.4 Page Table (BBFPage)
237+
238+
The page table serves as the table of contents for readers, and provides
239+
the logical reading order of a BBF file. Each entry references an asset by
240+
index into the Asset Table.
241+
242+
Offset Size Description
243+
0 8 bytes Asset Index
244+
8 4 bytes Flags (reserved, writers MUST set to 0)
245+
12 4 bytes Reserved, writers MUST set to zero
246+
247+
4.5 Section Table (BBFSection)
248+
249+
The section table defines logical groupings of pages. Sections MAY be
250+
nested.
251+
252+
Offset Size Description
253+
0 8 bytes Offset into string pool for section title
254+
8 8 bytes First page index in this section
255+
16 8 bytes Offset into string pool of parent,
256+
or (0xFFFFFFFFFFFFFFFF for none)
257+
24 8 bytes Reserved. MUST be set to 0.
258+
259+
4.6 Metadata Table (BBFMeta)
260+
261+
Metadata entries define arbitrary key-value pairs associated with
262+
the BBF file. Metadata MAY optionally be associated with a parent
263+
identifier.
264+
265+
Offset Size Description
266+
0 8 bytes Offset into string pool for metadata key
267+
8 8 bytes Offset into string pool for metadata value
268+
16 8 bytes Offset into string pool for parent identifier,
269+
or 0xFFFFFFFFFFFFFFFF if none
270+
24 8 bytes Reserved. MUST be set to zero
271+
272+
Any implemented reader SHOULD:
273+
- Interpret keys and values as UTF-8 encoded strings
274+
- Treat parent offset value 0xFFFFFFFFFFFFFFFF as "no parent"
275+
276+
Any implemented writer MUST:
277+
- Set all reserved bytes to zero
278+
279+
4.7 Expansion Table (Optional)
280+
281+
The Expansion Table is reserved for future extensions to the BBF
282+
format. In BBF v3, this region is OPTIONAL and not yet standardized.
283+
284+
If no expansion data is present, the expansion offset in the footer
285+
MUST be set to 0, and its table size is 0.
286+
287+
Readers MUST ignore the expansion table if the expansion offset is 0.
288+
Readers SHOULD ignore unknown expansion formats.
289+
290+
4.8 String Pool
291+
292+
The string pool is a contiguous region of UTF-8 encoded strings.
293+
All string references within BBF structures are offsets into this
294+
pool.
295+
296+
Offsets into the string pool are absolute file offsets.
297+
298+
Writers MUST store strings as null-terminated UTF-8 sequences.
299+
Readers MUST enforce a maximum string length of 2048 characters in
300+
the string pool.
301+
302+
Strings in the string pool are for metadata and sectioning purposes
303+
only. Expansion entries which include text data MUST NOT include
304+
offsets to this region.
305+
306+
All string offsets MUST be located in the region defined by
307+
(string pool offset + string pool size).
308+
309+
Any implemented reader MUST:
310+
- Validate that string offsets fall within the string pool region
311+
- Treat invalid offsets as file corruption
312+
- Ensure null terminators exist within the bounds of the
313+
string pool (string pool offset + string pool size).
314+
- Ensure null terminators exist within the max string length of the
315+
current string pool offset.
316+
- Ensure (string pool offset + string pool size) does not
317+
result in an integer overflow.
318+
319+
5.0 Asset Data Region
320+
---------------------
321+
322+
The Asset Data Region contains the raw byte payloads of stored assets.
323+
324+
Each asset's data location and size is defined by the Asset Table.
325+
Assets MAY be stored in any order, but SHOULD be stored contiguously
326+
to reduce fragmentation.
327+
328+
In non-petrified files, asset data MUST appear immediately after the
329+
header.
330+
331+
In petrified files, asset data MUST appear after the footer.
332+
333+
Asset data SHOULD be aligned according to the alignment rules
334+
described in Section 3.2.
335+
336+
6.0 Petrification
337+
---------------------------
338+
339+
Petrification is the process of reorganizing a BBF file such that
340+
all index structures appear contiguously at the front of the file.
341+
342+
Petrified BBF files are optimized for:
343+
- Single-read parsing
344+
- Streaming and prefetching
345+
346+
When petrified:
347+
- All table offsets in the footer MUST reflect their new locations
348+
- Asset offsets MUST be updated to account for the relocated data
349+
region
350+
- The PETRIFICATION_FLAG MUST be set in the header
351+
352+
Readers MUST support both petrified and non-petrified layouts.
353+
354+
Any implemented reader MUST:
355+
- Verify offsets do not exceed the file's bounds.
356+
- Support both petrified and non-petrified layouts.
357+
358+
Any implemented reader SHOULD:
359+
- Verify the footer hash before allocating resources
360+
361+
7.0 Conformance Requirements
362+
----------------------------
363+
364+
7.1 Writer Conformance
365+
366+
A conforming BBF v3 writer MUST:
367+
- Write a valid BBF header at offset 0
368+
- Populate all required tables
369+
- Write a valid footer and set footer offset correctly
370+
- Set all reserved fields to zero
371+
- Use little-endian encoding for all multi-byte values
372+
- Correctly compute the footer hash
373+
374+
7.2 Reader Conformance
375+
376+
A conforming BBF v3 reader MUST:
377+
- Validate the magic number and version
378+
- Locate and parse the footer using footer offset
379+
- Use footer-provided offsets and counts to parse tables
380+
- Support both petrified and non-petrified files
381+
- Verify that (Table Entry Count * Entry Size) <= Filesize
382+
before allocating memory for any table.
383+
- Verify that (Table Entry Count * Entry Size + Table offset)
384+
does not overflow before allocating memory.
385+
- Verify that Asset Offset + Asset Size <= Filesize before
386+
reading asset data.
387+
388+
A conforming BBF v3 reader SHOULD:
389+
- Reject or inform users if files contain nonzero data in
390+
reserved or padding fields.

0 commit comments

Comments
 (0)