-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSPEC.typ
More file actions
1239 lines (890 loc) · 40.9 KB
/
SPEC.typ
File metadata and controls
1239 lines (890 loc) · 40.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Box Archive Format Specification
// Typst source
#set document(
title: "Box Archive Format Specification",
author: "Brendan Molloy",
)
#set page(
paper: "a4",
margin: (x: 2.5cm, y: 2.5cm),
numbering: "1",
)
#set heading(numbering: "1.1")
#show link: underline
#show raw.where(block: false): box.with(
fill: luma(240),
inset: (x: 3pt, y: 0pt),
outset: (y: 3pt),
radius: 2pt,
)
#show raw.where(block: true): block.with(
fill: luma(240),
inset: 10pt,
radius: 4pt,
width: 100%,
)
// Title
#block(inset: (y: 2em))[
#text(size: 24pt, weight: "bold")[Box Archive Format Specification]
*Version:* 0.2.0 \
*Status:* Draft
]
#outline(indent: auto)
= Introduction <sec:introduction>
The Box format is an open archive format designed for storing files with support for compression, checksums, extended attributes, and memory-mapped access. It provides platform-independent path encoding and efficient metadata storage through string interning.
== Design Goals <sec:design-goals>
- *Platform Independence:* Paths are encoded in a platform-neutral format that can be safely represented on all major operating systems.
- *Compression:* Multiple compression algorithms are supported, selectable per-file.
- *Integrity:* Optional Blake3 checksums for content verification.
- *Efficiency:* String interning for attribute keys reduces metadata size. Memory mapping enables zero-copy file access for uncompressed files. Block-level compression enables efficient random access for large compressed files.
- *Extensibility:* The attribute system allows arbitrary metadata without format changes.
== File Extension <sec:file-extension>
Box archives SHOULD use the `.box` file extension.
= Terminology <sec:terminology>
The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in #link("https://www.rfc-editor.org/rfc/rfc2119")[RFC 2119].
*Archive:* A Box format file containing zero or more entries.
*Entry:* A file, directory, or symbolic link stored in the archive.
*Record:* The metadata structure describing an entry.
*Trailer:* The metadata section at the end of the archive containing all records and attributes.
*Chunked File:* A file stored with block-level compression, enabling random access without decompressing the entire file.
= File Structure Overview <sec:file-structure>
A Box archive consists of five sections laid out sequentially:
#figure(
table(
columns: (auto, 1fr, auto),
align: (right, center, left),
stroke: 0.5pt,
inset: 8pt,
fill: (x, y) => if x == 1 { (rgb("#e8f4e8"), rgb("#fff8e8"), rgb("#e8f0ff"), rgb("#f0e8ff"), rgb("#ffe8f0")).at(y) } else { none },
[Offset 0x00 →], [*Header*], [32 bytes, fixed size],
[Offset 0x20 →], [*Data Section*], [Variable size],
[header.trailer →], [*Trailer*], [Variable size (BoxMetadata)],
[], [*Path FST*], [Variable size],
[EOF →], [*Block FST*], [Variable size (optional)],
),
caption: [Box archive structure],
) <fig:archive-structure>
The Header is always 32 bytes and located at offset 0. The Header contains a pointer to the Trailer, which stores record metadata. File content data is stored between the Header and Trailer. The Path FST follows the Trailer and provides path-to-record mapping (see @sec:path-fst). The Block FST, if present, follows the Path FST and provides logical-to-physical offset mapping for chunked files (see @sec:block-fst). Both FSTs are prefixed with a u64 length (little-endian), enabling efficient seeking past the Path FST to detect the Block FST's presence.
All multi-byte integers in the Box format are stored in *little-endian* byte order unless otherwise specified.
= Data Types <sec:data-types>
== Fixed-Size Integers <sec:fixed-integers>
#table(
columns: (auto, auto, 1fr),
align: (left, left, left),
table.header([*Type*], [*Size*], [*Description*]),
[`u8`], [1 byte], [Unsigned 8-bit integer],
[`u32`], [4 bytes], [Unsigned 32-bit integer, little-endian],
[`u64`], [8 bytes], [Unsigned 64-bit integer, little-endian],
[`i64`], [8 bytes], [Signed 64-bit integer, little-endian],
)
== Vu64 Encoding (FastVint) <sec:vu64>
Variable-size unsigned 64-bit integers (Vu64) are encoded using FastVint, a prefix-based variable-length encoding where the number of leading zeros in the first byte determines the total byte count.
*Length Determination:*
The number of leading zero bits in the first byte, plus one, gives the total byte count:
```
1xxx_xxxx → 1 byte (7 data bits)
01xx_xxxx → 2 bytes (14 data bits: 6 + 8)
001x_xxxx → 3 bytes (21 data bits: 5 + 16)
0001_xxxx → 4 bytes (28 data bits: 4 + 24)
0000_1xxx → 5 bytes (35 data bits: 3 + 32)
0000_01xx → 6 bytes (42 data bits: 2 + 40)
0000_001x → 7 bytes (49 data bits: 1 + 48)
0000_0001 → 8 bytes (56 data bits: 0 + 56)
0000_0000 → 9 bytes (64 data bits: 0 + 64)
```
*Offset-Based Encoding:*
FastVint uses offset-based encoding for denser packing. Each length tier has a base offset:
#table(
columns: (auto, auto, auto),
align: (left, left, left),
table.header([*Bytes*], [*Value Range*], [*Offset*]),
[1], [0 – 127], [0],
[2], [128 – 16,511], [128],
[3], [16,512 – 2,113,663], [16,512],
[4], [2,113,664 – 270,549,119], [2,113,664],
[5+], [...], [(continues exponentially)],
[9], [(full u64 range)], [72,624,976,668,147,840],
)
*Data Byte Order:*
After the prefix byte, remaining data bytes are stored in *big-endian* order.
*Key Properties:*
- Length is known from the first byte alone (no continuation scanning required)
- Maximum 9 bytes for `u64` values
- This encoding does NOT enforce minimal representations; the same value MAY have multiple valid encodings
- Decoders MUST accept all valid encodings of a value, including non-minimal representations
== String <sec:string>
Strings are encoded as:
```
[Vu64: byte length]
[UTF-8 bytes]
```
All strings MUST be valid UTF-8. Implementations MUST reject invalid UTF-8 sequences.
== Vec\<T\> <sec:vec>
Vectors (arrays) are encoded as:
```
[Vu64: element count]
[T encoding] * count
```
The following vector types are used in the format:
#table(
columns: (auto, auto, 1fr),
align: (left, left, left),
table.header([*Type*], [*Usage*], [*Element Encoding*]),
[`Vec<Record>`], [`BoxMetadata.records`], [Record (see @sec:record-types)],
[`Vec<AttrKey>`], [`attr_keys`], [Type tag + String (see @sec:attr-keys)],
)
== Vec\<u8\> (Byte Array) <sec:vec-u8>
Byte arrays are encoded identically to strings:
```
[Vu64: byte length]
[raw bytes]
```
== RecordIndex <sec:record-index>
A RecordIndex is a 1-based index into the records array, encoded as Vu64:
```
[Vu64: index value]
```
Index values MUST be non-zero. An index value of `n` refers to `records[n-1]`.
== AttrMap <sec:attrmap>
Attribute maps are encoded as:
```
[u64: byte count of remaining section (not including this field)]
[Vu64: entry count]
For each entry:
[Vu64: key index into attr_keys]
[Vec<u8>: value]
```
The leading byte count allows implementations to skip the entire attribute map without parsing individual entries.
= Header <sec:header>
The Header is located at byte offset 0 and is exactly 32 bytes.
== Header Structure <sec:header-structure>
#table(
columns: (auto, auto, auto, 1fr),
align: (left, left, left, left),
table.header([*Offset*], [*Size*], [*Field*], [*Description*]),
[`0x00`], [4], [`magic`], [Magic bytes: `0xFF 0x42 0x4F 0x58` (`\xFFBOX`)],
[`0x04`], [1], [`version`], [Format version number],
[`0x05`], [1], [`flags`], [Feature flags (see @sec:flags)],
[`0x06`], [2], [`reserved1`], [Reserved bytes],
[`0x08`], [4], [`alignment`], [Data alignment boundary (`u32`)],
[`0x0C`], [4], [`reserved2`], [Reserved bytes],
[`0x10`], [8], [`trailer`], [Byte offset to trailer (`u64`)],
[`0x18`], [8], [`reserved3`], [Reserved bytes],
)
*Total Size:* 32 bytes
== Magic Bytes <sec:magic>
The magic bytes MUST be exactly `0xFF 0x42 0x4F 0x58` (the byte `0xFF` followed by ASCII `BOX`).
The leading `0xFF` byte is invalid UTF-8, causing any attempt to parse the file as a text string to fail immediately at byte 0.
== Version <sec:version>
The current format version is `0x01` (1). Implementations SHOULD reject archives with unknown major versions.
== Flags <sec:flags>
The flags byte is a bitfield:
#table(
columns: (auto, auto, 1fr),
align: (left, left, left),
table.header([*Bit*], [*Name*], [*Description*]),
[0], [`EXTERNAL_SYMLINKS`], [Archive contains external symlinks (see @sec:external-symlink)],
[1], [`ALLOW_ESCAPES`], [Archive paths may contain `..` components],
[2-7], [Reserved], [Must be 0],
)
== Alignment <sec:alignment>
The `alignment` field specifies the byte boundary to which file data is aligned. Valid values are powers of 2 from 1 to 65536. A value of 0 indicates no alignment (equivalent to 1).
When alignment is specified, padding bytes (value `0x00`) are inserted before file data to ensure each file's data starts at an offset that is a multiple of the alignment value.
Common alignment values:
- `0` or `1`: No alignment (most compact)
- `4096`: Page alignment (optimal for memory mapping)
- `512`: Sector alignment
== Reserved Fields <sec:reserved-fields>
All reserved fields (`reserved1`, `reserved2`, `reserved3`) MUST be set to zero when writing and MUST be ignored when reading to allow for future format extensions.
== Binary Layout <sec:binary-layout>
#figure(
{
set text(font: "Courier New", size: 9pt)
table(
columns: (auto,) + (1.2em,) * 16,
align: center + horizon,
stroke: 0.5pt,
fill: (x, y) => if y == 0 { rgb("#e8e8e8") } else { white },
[*Off*], [*00*], [*01*], [*02*], [*03*], [*04*], [*05*], [*06*], [*07*], [*08*], [*09*], [*0A*], [*0B*], [*0C*], [*0D*], [*0E*], [*0F*],
[0x00], [FF], [42], [4F], [58], [VV], [XX], [RR], [RR], [AA], [AA], [AA], [AA], [RR], [RR], [RR], [RR],
[0x10], [TT], [TT], [TT], [TT], [TT], [TT], [TT], [TT], [RR], [RR], [RR], [RR], [RR], [RR], [RR], [RR],
)
},
caption: [Header binary layout],
) <fig:header-binary>
#text(size: 10pt)[
*Legend:* `FF 42 4F 58` = magic, `VV` = version, `XX` = flags, `RR` = reserved, `AA` = alignment, `TT` = trailer offset
]
= Data Section <sec:data-section>
The Data Section immediately follows the Header and contains the actual file content.
== Layout <sec:data-layout>
File data is stored contiguously, with optional padding between files when alignment is enabled. The Data Section starts immediately after the Header. When alignment is disabled, this is offset `0x20` (32 bytes). When alignment is enabled, padding bytes (`0x00`) are inserted so that the first file's data begins at an aligned offset. The Data Section extends to the trailer offset.
#figure(
table(
columns: (auto, 1fr),
align: (right, center),
stroke: 0.5pt,
inset: 8pt,
[Offset 0x20 →], [\[Padding\] _(if alignment > 1)_],
[], [*File 1 Data*],
[], [\[Padding\] _(if alignment > 1)_],
[], [*File 2 Data*],
[], [\[Padding\]],
[], [...],
[header.trailer →], [],
),
caption: [Data section layout],
) <fig:data-layout>
== Alignment Padding <sec:alignment-padding>
When `header.alignment > 1`, padding bytes (`0x00`) are inserted before file data such that:
```
file_data_offset % alignment == 0
```
The amount of padding before each file is:
```
padding = (alignment - (current_offset % alignment)) % alignment
```
= Trailer (Metadata) <sec:trailer>
The Trailer contains all archive metadata encoded as `BoxMetadata`.
== BoxMetadata Structure <sec:boxmetadata>
```
[Vec<AttrKey>: attr_keys]
[AttrMap: attrs]
[Vu64: dictionary_length]
[bytes: dictionary]
[Vec<Record>: records]
```
Fields are encoded in the order shown, using the data type encodings from @sec:data-types. The dictionary field is encoded as a Vu64 length followed by raw bytes; length=0 means no dictionary is present.
== Attribute Key Table (attr_keys) <sec:attr-keys>
The attribute key table provides string interning for attribute keys. Each entry consists of:
```
[u8: type tag]
[String: key name]
```
The type tag indicates the expected value type for this attribute key. See @sec:attr-value-types for type tag values.
Keys are referenced by their 0-based index in this table.
== Attribute Value Types <sec:attr-value-types>
*Type Tag Values:*
#table(
columns: (auto, auto, 1fr),
align: (left, left, left),
table.header([*Value*], [*Type*], [*Description*]),
[0], [Bytes], [Raw bytes, no interpretation],
[1], [String], [UTF-8 string],
[2], [Json], [UTF-8 JSON],
[3], [U8], [Single byte (u8)],
[4], [Vi32], [Zigzag-encoded i32],
[5], [Vu32], [FastVint-encoded u32],
[6], [Vi64], [Zigzag-encoded i64],
[7], [Vu64], [FastVint-encoded u64],
[8], [U128], [Fixed 16 bytes (128 bits)],
[9], [U256], [Fixed 32 bytes (256 bits)],
[10], [DateTime], [Minutes since Box epoch (2026-01-01 00:00:00 UTC), zigzag-encoded i64],
[11-255], [Reserved], [Reserved for future use],
)
When serializing: keys are written in symbol order (0, 1, 2, ...).
When deserializing: strings are assigned symbols in the order read. The type tag determines how the attribute value is interpreted.
== Archive Attributes (attrs) <sec:archive-attrs>
The `attrs` field stores archive-level attributes as an `AttrMap`. Common archive attributes include creation tools, comments, or custom metadata.
== Dictionary <sec:dictionary>
The `dictionary` field contains a Zstd dictionary used for compression. When present (non-empty), all records compressed with Zstd MUST use this dictionary for both compression and decompression. The dictionary is ignored for other compression methods (Stored, XZ).
Dictionary training is typically performed at archive creation time using `ZDICT_trainFromBuffer()` or equivalent. A recommended dictionary size is 32KB-128KB, trained on representative samples from the archive contents totaling approximately 100× the dictionary size.
When `dictionary` is empty, Zstd compression operates without a dictionary.
The dictionary provides two benefits:
1. Improved compression ratio, especially for small files with shared patterns
2. More predictable compression ratios, which aids in estimating compressed block sizes for chunked files
= Record Types <sec:record-types>
Records describe entries in the archive. Each record begins with a 1-byte type/compression identifier.
== Type/Compression Byte <sec:type-compression>
The first byte of each record encodes both the record type and compression method:
```
[type_compression: u8]
```
*Layout:*
```
Bits 0-3: Record type
Bits 4-7: Compression method
```
*Record Type Values:*
#table(
columns: (auto, auto, 1fr),
align: (left, left, left),
table.header([*Value*], [*Type*], [*Description*]),
[`0x01`], [Directory], [Directory entry],
[`0x02`], [File], [Regular file],
[`0x03`], [Symlink], [Internal symbolic link],
[`0x0A`], [Chunked File], [File with block-level compression],
[`0x0B`], [External Symlink], [Symbolic link to external target],
)
*Compression Values:*
#table(
columns: (auto, auto, 1fr),
align: (left, left, left),
table.header([*Value*], [*Method*], [*Description*]),
[`0x00`], [Stored], [No compression],
[`0x10`], [Zstd], [Zstandard compression],
[`0x20`], [XZ], [XZ/LZMA2 compression],
)
*Combined Examples:*
#table(
columns: (auto, 1fr),
align: (left, left),
table.header([*Byte*], [*Meaning*]),
[`0x01`], [Directory (compression ignored)],
[`0x02`], [File, stored (uncompressed)],
[`0x12`], [File, Zstd compressed],
[`0x22`], [File, XZ compressed],
[`0x1A`], [Chunked file, Zstd compressed blocks],
)
== Directory Record (Type 0x01) <sec:directory-record>
Directories have no associated data in the Data Section.
*Structure:*
```
[type_compression: u8] // Always 0x01
[String: name]
[AttrMap: attrs]
```
*Binary Layout:*
#block(inset: (left: 1em))[
#set text(font: "Courier New", size: 10pt)
#table(
columns: (auto, 1fr),
stroke: none,
inset: 3pt,
[`00`], [type_compression (directory)],
[...], [String: name],
[...], [AttrMap: attrs],
)
]
== File Record (Type 0x02) <sec:file-record>
Files store their content in the Data Section.
*Structure:*
```
[type_compression: u8]
[length: u64] // Compressed length in data section
[decompressed_length: u64] // Original file size
[data: u64] // Byte offset in archive
[String: name]
[AttrMap: attrs]
```
*Binary Layout:*
#block(inset: (left: 1em))[
#set text(font: "Courier New", size: 10pt)
#table(
columns: (auto, 1fr),
stroke: none,
inset: 3pt,
[`TT`], [type_compression],
[`LL LL LL LL LL LL LL LL`], [length (u64)],
[`DD DD DD DD DD DD DD DD`], [decompressed_length (u64)],
[`OO OO OO OO OO OO OO OO`], [data offset (u64)],
[...], [String: name],
[...], [AttrMap: attrs],
)
]
When `compression == Stored`, `length == decompressed_length`.
== Symlink Record (Type 0x03) <sec:symlink-record>
Symbolic links point to other entries within the archive.
*Structure:*
```
[type_compression: u8] // Always 0x03
[String: name]
[Vu64: target (RecordIndex)]
[AttrMap: attrs]
```
*Constraints:*
- Target MUST be a valid RecordIndex pointing to an existing record
- Circular symlinks are not permitted
- The target record MUST NOT be another symlink
*Binary Layout:*
#block(inset: (left: 1em))[
#set text(font: "Courier New", size: 10pt)
#table(
columns: (auto, 1fr),
stroke: none,
inset: 3pt,
[`03`], [type_compression (symlink)],
[...], [String: name],
[...], [Vu64: target (RecordIndex)],
[...], [AttrMap: attrs],
)
]
== External Symlink Record (Type 0x0B) <sec:external-symlink>
External symlinks point to paths outside the archive. This record type is only valid when the `EXTERNAL_SYMLINKS` flag is set in the header.
*Structure:*
```
[type_compression: u8] // Always 0x0B
[String: name]
[String: target]
[AttrMap: attrs]
```
*Target Path Format:*
The target path uses BoxPath encoding (see @sec:path-encoding) with one relaxation: `..` components are permitted to allow the symlink to escape the archive root. The path is stored with `\x1F` (Unit Separator) as the component separator.
*Constraints:*
- The header MUST have the `EXTERNAL_SYMLINKS` flag (bit 0) set
- Target is a relative path from the symlink's location
- Implementations MUST validate `..` does not escape the extraction directory without explicit user consent
*Binary Layout:*
#block(inset: (left: 1em))[
#set text(font: "Courier New", size: 10pt)
#table(
columns: (auto, 1fr),
stroke: none,
inset: 3pt,
[`0B`], [type_compression (external symlink)],
[...], [String: name],
[...], [String: target],
[...], [AttrMap: attrs],
)
]
*Extraction Behavior:*
When extracting an external symlink record, implementations:
1. Convert the stored target path from BoxPath format (`\x1F` separators) to platform path format
2. Create a symbolic link at the record's location using the converted relative target path
*Security Considerations:*
External symlinks can point to arbitrary locations outside the extraction directory. This presents security risks:
- Implementations MUST require explicit user consent before extracting archives containing external symlinks.
- This feature is opt-in at both archive creation time (setting the header flag) and extraction time.
- Extraction tools SHOULD warn users when processing archives with external symlinks.
== Chunked File Record (Type 0x0A) <sec:chunked-file>
Chunked files store content in fixed-size blocks, each independently compressed. This enables random access to large compressed files without decompressing the entire file.
*Structure:*
```
[type_compression: u8] // 0x0A | compression_method
[block_size: u32] // Size of each block before compression
[length: u64] // Total compressed length
[decompressed_length: u64] // Total original file size
[data: u64] // Byte offset to first block
[String: name]
[AttrMap: attrs]
```
*Binary Layout:*
#block(inset: (left: 1em))[
#set text(font: "Courier New", size: 10pt)
#table(
columns: (auto, 1fr),
stroke: none,
inset: 3pt,
[`TT`], [type_compression],
[`BB BB BB BB`], [block_size (u32)],
[`LL LL LL LL LL LL LL LL`], [length (u64)],
[`DD DD DD DD DD DD DD DD`], [decompressed_length (u64)],
[`OO OO OO OO OO OO OO OO`], [data offset (u64)],
[...], [String: name],
[...], [AttrMap: attrs],
)
]
*Decompression:*
To read bytes at logical offset `O` within a chunked file, use the Block FST lookup procedure described in @sec:block-lookup.
*Recommended Block Size:*
A block size of 2MB (2,097,152 bytes) is RECOMMENDED. This aligns with common hugepage sizes and provides a good balance between compression ratio and random access granularity.
= Path Encoding <sec:path-encoding>
Paths in Box archives use a platform-independent encoding called BoxPath.
== BoxPath Format <sec:boxpath>
BoxPath paths are sequences of components separated by `U+001F` UNIT SEPARATOR (hex `\x1F`).
*Properties:*
- Paths are always relative (no leading separator)
- No trailing separator
- Empty components are not permitted
- Components `.` and `..` are not permitted
- Components cannot contain platform path separators (`/` or `\`)
- Components cannot contain null bytes
== Separator Choice <sec:separator-choice>
The `U+001F` separator was chosen because:
1. It forces explicit handling - developers cannot be lazy and treat paths as platform-native strings
2. It avoids confusion with URL scheme prefixes (`file://`, `http://`) that would occur with `/`
3. It is truly platform-agnostic - neither Unix nor Windows "owns" this separator
4. It is illegal in filenames on all major platforms, preventing ambiguity
== Platform Conversion <sec:platform-conversion>
When extracting archives, implementations MUST convert BoxPath paths to platform-native paths:
#table(
columns: (auto, auto),
align: (left, left),
table.header([*Platform*], [*Separator*]),
[Unix/Linux/macOS], [`/`],
[Windows], [`\`],
)
*Example:*
```
BoxPath: foo\x1Fbar\x1Fbaz.txt
Unix: foo/bar/baz.txt
Windows: foo\bar\baz.txt
```
== Unicode Normalization <sec:unicode-normalization>
All paths MUST be stored in NFC (Canonical Decomposition, followed by Canonical Composition) normalized form. Implementations MUST normalize paths before storage and comparison.
= Compression <sec:compression>
Box supports multiple compression methods, selectable per-file.
== Supported Methods <sec:compression-methods>
#table(
columns: (auto, auto, auto, 1fr),
align: (left, left, left, left),
table.header([*Value*], [*Name*], [*Library*], [*Notes*]),
[`0x00`], [Stored], [None], [No compression],
[`0x10`], [Zstd], [zstd], [Recommended default],
[`0x20`], [XZ], [liblzma], [High compression ratio],
)
== Zstd Configuration <sec:zstd-config>
When using Zstd:
- Compression level: Implementations SHOULD allow user-configurable levels (1-22)
- Default level: 3 (good balance of speed and ratio)
- Dictionary: If `BoxMetadata.dictionary` is non-empty, it MUST be used
== XZ Configuration <sec:xz-config>
When using XZ:
- Preset: Implementations SHOULD use preset 6 by default
- Integrity check: CRC64 is RECOMMENDED
== Stored (Uncompressed) <sec:stored>
When compression is `Stored` (`0x00`):
- `length` equals `decompressed_length`
- Data is stored verbatim
- Useful for already-compressed files (images, videos) or when memory mapping is desired
== Dictionary Usage <sec:dictionary-usage>
When a dictionary is present in `BoxMetadata.dictionary` (@sec:dictionary), it MUST be used for all Zstd-compressed content in the archive, including:
- Whole-file compressed files
- Individual blocks in chunked files
The dictionary is loaded once when opening the archive and reused for all Zstd decompression operations. The dictionary has no effect on Stored or XZ-compressed content.
= Checksums <sec:checksums>
Box archives support content checksums for integrity verification.
== Supported Algorithms <sec:checksum-algorithms>
#table(
columns: (auto, auto, auto),
align: (left, left, left),
table.header([*Name*], [*Attribute*], [*Output Size*]),
[Blake3], [`blake3`], [32 bytes],
)
== Checksum Storage <sec:checksum-storage>
Checksums are stored as record attributes:
```
attrs["blake3"] = <32-byte hash>
```
The checksum is computed over the *decompressed* content.
== Verification <sec:checksum-verification>
Implementations SHOULD verify checksums when:
1. Extracting files (if checksums are present)
2. Explicitly requested by the user
Verification failures MUST be reported to the user. Implementations MAY offer options to proceed despite failures.
= Standard Attributes <sec:standard-attrs>
This section defines standard attribute keys that implementations SHOULD recognize.
== Timestamps <sec:timestamps>
#table(
columns: (auto, auto, 1fr),
align: (left, left, left),
table.header([*Attribute*], [*Type*], [*Description*]),
[`created`], [Vi64], [Creation time (minutes since Box epoch)],
[`modified`], [Vi64], [Modification time (minutes since Box epoch)],
[`accessed`], [Vi64], [Access time (minutes since Box epoch)],
)
*Box Epoch:* 2026-01-01 00:00:00 UTC
Timestamps are OPTIONAL and stored as signed variable-length integers (Vi64, zigzag encoded) representing minutes since the Box epoch. The signed format supports dates before 2026. Minute precision is sufficient for typical archive use cases.
== Sub-Minute Precision <sec:sub-minute>
For applications requiring higher precision:
#table(
columns: (auto, auto, 1fr),
align: (left, left, left),
table.header([*Attribute*], [*Type*], [*Description*]),
[`created.seconds`], [U8], [Additional seconds (0-59)],
[`modified.seconds`], [U8], [Additional seconds (0-59)],
[`accessed.seconds`], [U8], [Additional seconds (0-59)],
[`created.nanoseconds`], [Vu64], [Sub-minute precision in nanoseconds (0-59,999,999,999)],
[`modified.nanoseconds`], [Vu64], [Sub-minute precision in nanoseconds (0-59,999,999,999)],
[`accessed.nanoseconds`], [Vu64], [Sub-minute precision in nanoseconds (0-59,999,999,999)],
)
Seconds and nanoseconds attributes are OPTIONAL. If present, they provide additional precision beyond the minute-level timestamp. If `nanoseconds` is present, these take precedence over the `seconds` attribute.
== Unix Permissions <sec:unix-permissions>
#table(
columns: (auto, auto, 1fr),
align: (left, left, left),
table.header([*Attribute*], [*Type*], [*Extraction Default*]),
[`unix.mode`], [Vu32], [0o100644 (files), 0o40755 (dirs)],
[`unix.uid`], [Vu32], [Archive attr, then current user],
[`unix.gid`], [Vu32], [Archive attr, then current user],
)
*unix.mode Storage:*
The mode value includes the file type bits in the standard Unix format:
- Regular file: `0o100000` + permissions
- Directory: `0o040000` + permissions
- Symlink: `0o120000` + permissions
*Extraction Behavior:*
If `unix.mode` is not present:
- Files: `0o100644` (rw-r--r--)
- Directories: `0o040755` (rwxr-xr-x)
- Symlinks: `0o120777` (rwxrwxrwx)
If `unix.uid`/`unix.gid` are not present, implementations SHOULD:
1. Check archive-level `unix.uid`/`unix.gid` attributes
2. Fall back to current user's UID/GID
== Attribute Encoding <sec:attr-encoding>
Attribute values are stored as raw bytes (`Vec<u8>`). The type tag stored with the attribute key (see @sec:attr-keys) determines how to interpret the value.
*Standard Attribute Types:*
#table(
columns: (1fr, auto, 1fr),
align: (left, left, left),
table.header([*Attribute*], [*Type Tag*], [*Encoding*]),
[`created`, `modified`, `accessed`], [DateTime (10)], [Zigzag-encoded i64 (minutes since Box epoch)],
[`created.seconds`, `modified.seconds`, `accessed.seconds`], [U8 (3)], [Single byte (0-59)],
[`created.nanoseconds`, `modified.nanoseconds`, `accessed.nanoseconds`], [Vu64 (7)], [FastVint-encoded u64],
[`unix.mode`, `unix.uid`, `unix.gid`], [Vu32 (5)], [FastVint-encoded u32],
[`blake3`], [U256 (9)], [Fixed 32 bytes],
)
Applications MAY define custom attributes with any type tag.
= Implementation Guidance <sec:implementation-guidance>
This section provides non-normative guidance for implementers.
== Memory Mapping <sec:memory-mapping>
Box archives are designed to support memory-mapped access:
1. Use appropriate `alignment` (e.g., 4096 for page alignment)
2. Store files with `compression = Stored` for direct access
3. The FST structure enables O(m) path lookups where m is path length
== Streaming Creation <sec:streaming-creation>
Archives can be created in a streaming fashion:
1. Write header with placeholder trailer offset
2. Write file data sequentially, recording offsets
3. Write trailer with all records
4. Build and write Path FST (and Block FST if needed)
5. Seek back and update header with trailer offset
== Large File Handling <sec:large-file-handling>
For files larger than available memory:
1. Use chunked file records for random access
2. Stream decompression for sequential access
3. Consider block size trade-offs (smaller = more overhead, larger = more memory)
== Error Recovery <sec:error-recovery>
The trailer-at-end design means truncated archives lose metadata. Implementations MAY:
1. Store periodic checkpoints during creation
2. Implement recovery tools that scan for record boundaries
3. Use file system features (copy-on-write, journaling) for durability
= Path FST <sec:path-fst>
The Path FST (Finite State Transducer) provides efficient path-to-record mapping.
== Overview <sec:fst-overview>
The FST maps BoxPath strings to RecordIndex values. It supports:
- O(m) exact lookups where m is path length
- Prefix queries (enumerate all paths under a directory)
- Memory-efficient storage through prefix sharing
== FST Layout <sec:fst-layout>
The Path FST is located immediately after the Trailer:
#figure(
table(
columns: (auto, 1fr, auto),
align: (right, center, left),
stroke: 0.5pt,
inset: 8pt,
[trailer_end →], [*Length (u64)*], [8 bytes],
[], [*Header*], [24 bytes],
[], [*Node Index*], [node_count × 8 bytes],
[], [*Hot Section*], [Variable size (compact node headers)],
[], [*Cold Section*], [Variable size (edge data)],
),
caption: [FST section layout],
) <fig:fst-layout>
The length prefix encodes the total size of the FST data (Header through Cold Section, not including the length prefix itself).
The Block FST (if present) uses the same structure (length prefix followed by FST data) and follows immediately after the Path FST.
== FST Header <sec:fst-header>
The FST Header is 24 bytes and located at the start of the FST section.
#table(
columns: (auto, auto, auto, 1fr),
align: (left, left, left, left),
table.header([*Offset*], [*Size*], [*Field*], [*Description*]),
[`0x00`], [4], [`magic`], [Magic bytes: `BFST` (0x42 0x46 0x53 0x54)],
[`0x04`], [1], [`version`], [Format version (currently 1)],
[`0x05`], [1], [`flags`], [Reserved flags (must be 0)],
[`0x06`], [2], [`reserved`], [Reserved bytes (must be 0)],
[`0x08`], [4], [`node_count`], [Total number of nodes (`u32`)],
[`0x0C`], [8], [`entry_count`], [Number of entries indexed (`u64`)],
[`0x14`], [4], [`cold_offset`], [Cold section start offset (`u32`)],
)
The Hot Section starts at offset `24 + node_count × 8` (immediately after the Node Index). The root node is always node 0.
Implementations MUST reject FST data where magic bytes do not match or version is unsupported.
== Node Index <sec:node-index>
The Node Index is an array of `node_count` entries, with each entry being 8 bytes.
*Entry Format:*
#table(
columns: (auto, auto, auto, 1fr),
align: (left, left, left, left),
table.header([*Offset*], [*Size*], [*Field*], [*Description*]),
[`0x00`], [4], [`hot_offset`], [Offset within Hot Section (`u32`)],
[`0x04`], [4], [`cold_offset`], [Offset within Cold Section (`u32`)],
)
Offsets are relative to the start of their respective sections. Node IDs are indices into this array (node 0 = first entry, node 1 = second entry, etc.).
== Hot Section <sec:hot-section>
The Hot Section contains compact node headers optimized for cache efficiency. Each node's hot data has the following structure:
#block(inset: (left: 1em))[
#set text(font: "Courier New", size: 10pt)
#table(
columns: (auto, 1fr),
stroke: none,
inset: 3pt,
[...], [flags: u8],
[...], [edge_count: Vu64],
[...], [lookup_data: variable],
[...], [offsets: edge_count × u16],
)
]
*Flags Byte:*
#table(
columns: (auto, auto, 1fr),