1use std::convert::TryInto;
16
17pub const MAGIC: [u8; 4] = *b"RETE";
19
20pub const CURRENT_FORMAT_VERSION: u8 = 0x08;
34
35pub const LEGACY_FORMAT_VERSION: u8 = 0x05;
40
41pub const MIN_STABLE_READ_VERSION: u8 = 0x05;
50
51pub const HEADER_LEN: usize = 1024;
53
54const SECTION_DIR_OFFSET: usize = 64;
56const SECTION_ENTRY_LEN: usize = 24;
58pub const MAX_SECTIONS: usize = (HEADER_LEN - SECTION_DIR_OFFSET) / SECTION_ENTRY_LEN;
60
61pub const FLAG_HAS_QUADS: u8 = 0b0000_0001;
63
64pub const FLAG_TILE_SYNOPSIS: u8 = 0b0000_0010;
69
70pub const FLAG_HAS_QUOTED_TRIPLES: u8 = 0b0000_0100;
77
78pub const FLAG_QUAD_INDEX: u8 = 0b0000_1000;
87
88pub const FLAG_TEXT_INDEX_NAMED_GRAPHS: u8 = 0b0001_0000;
95
96pub const FLAG_SPARQL_TERM_ORDER: u8 = 0b0010_0000;
105
106pub const TEXT_INDEX_PARTIAL_DIGEST_LEN: u64 = 16;
111
112#[derive(Debug, Clone, Copy, PartialEq, Eq)]
114pub enum SectionKind {
115 Metadata,
117 Dictionary,
119 Index,
121 PyramidMeta,
123 NamedGraphs,
125 TextIndex,
127 TextIndexPartial,
141 BuildInfo,
148 LabelIndex,
154 Schema,
160 QuadIndex,
165 Unknown(u16),
168}
169
170impl SectionKind {
171 fn to_u16(self) -> u16 {
172 match self {
173 SectionKind::Metadata => 1,
174 SectionKind::Dictionary => 2,
175 SectionKind::Index => 3,
176 SectionKind::PyramidMeta => 4,
177 SectionKind::NamedGraphs => 5,
178 SectionKind::TextIndex => 6,
179 SectionKind::BuildInfo => 7,
180 SectionKind::LabelIndex => 8,
181 SectionKind::Schema => 9,
182 SectionKind::QuadIndex => 10,
183 SectionKind::TextIndexPartial => 11,
184 SectionKind::Unknown(k) => k,
185 }
186 }
187
188 fn from_u16(k: u16) -> Self {
189 match k {
190 1 => SectionKind::Metadata,
191 2 => SectionKind::Dictionary,
192 3 => SectionKind::Index,
193 4 => SectionKind::PyramidMeta,
194 5 => SectionKind::NamedGraphs,
195 6 => SectionKind::TextIndex,
196 7 => SectionKind::BuildInfo,
197 8 => SectionKind::LabelIndex,
198 9 => SectionKind::Schema,
199 10 => SectionKind::QuadIndex,
200 11 => SectionKind::TextIndexPartial,
201 other => SectionKind::Unknown(other),
202 }
203 }
204}
205
206#[derive(Debug, Clone, Copy, PartialEq, Eq)]
209pub struct Section {
210 pub kind: SectionKind,
211 pub flags: u16,
212 pub offset: u64,
213 pub length: u64,
214}
215
216#[derive(Debug, thiserror::Error)]
217#[non_exhaustive]
218pub enum HeaderError {
219 #[error("buffer too small: need {HEADER_LEN} bytes, got {0}")]
220 TooSmall(usize),
221 #[error("bad magic: expected RETE")]
222 BadMagic,
223 #[error(
224 "unsupported .rete format {found:#04x}; this Rete build reads {min:#04x}..={max:#04x}. Pre-1.0 files must be rebuilt from RDF source with `rete build`"
225 )]
226 UnsupportedVersion { found: u8, min: u8, max: u8 },
227 #[error("section count {0} overruns the header frame")]
228 BadSectionCount(usize),
229 #[error("unusable index permutation mask {mask:#04x}: {why}")]
230 BadPermMask { mask: u8, why: &'static str },
231 #[error("malformed 0x06 header: {0}")]
232 Malformed06(&'static str),
233}
234
235#[derive(Debug, Clone, PartialEq, Eq)]
239pub struct Header {
240 pub version: u8,
243 pub flags: u8,
244 pub metadata_offset: u64,
245 pub metadata_len: u64,
246 pub dictionary_offset: u64,
247 pub dictionary_len: u64,
248 pub root_dir_offset: u64,
249 pub root_dir_len: u64,
250 pub pyramid_meta_offset: u64,
251 pub pyramid_meta_len: u64,
252 pub dict_codec: u8,
253 pub block_codec: u8,
254 pub pyramid_levels: u16,
255 pub id_widths: u8,
257 pub quad_count: u64,
258 pub term_count: u64,
259 pub content_hash: [u8; 16],
261 pub named_graphs_offset: u64,
263 pub named_graphs_len: u64,
264 pub schema_meta_len: u32,
269 pub text_index_offset: u64,
272 pub text_index_len: u64,
273 pub text_index_partial: bool,
277 pub build_info_offset: u64,
280 pub build_info_len: u64,
281 pub perms: crate::index::PermSet,
293 pub extra_sections: Vec<Section>,
298}
299
300impl Header {
301 pub fn to_bytes(&self) -> [u8; HEADER_LEN] {
303 let mut b = [0u8; HEADER_LEN];
304 b[0..4].copy_from_slice(&MAGIC);
306 b[4] = self.version;
307 b[5] = self.flags;
308 b[6..8].copy_from_slice(&(HEADER_LEN as u16).to_le_bytes());
309 b[8..24].copy_from_slice(&self.content_hash);
310 b[24..32].copy_from_slice(&self.quad_count.to_le_bytes());
311 b[32..40].copy_from_slice(&self.term_count.to_le_bytes());
312 b[40..42].copy_from_slice(&self.pyramid_levels.to_le_bytes());
313 b[42] = self.dict_codec;
314 b[43] = self.block_codec;
315 b[46..50].copy_from_slice(&self.schema_meta_len.to_le_bytes());
317 b[50] = if self.perms == crate::index::PermSet::ALL {
320 0
321 } else {
322 self.perms.bits()
323 };
324 let entry = |kind, offset, length| Section {
331 kind,
332 flags: 0,
333 offset,
334 length,
335 };
336 let mut entries: Vec<Section> = vec![
337 entry(
338 SectionKind::Metadata,
339 self.metadata_offset,
340 self.metadata_len,
341 ),
342 entry(
343 SectionKind::Dictionary,
344 self.dictionary_offset,
345 self.dictionary_len,
346 ),
347 entry(SectionKind::Index, self.root_dir_offset, self.root_dir_len),
348 entry(
349 SectionKind::PyramidMeta,
350 self.pyramid_meta_offset,
351 self.pyramid_meta_len,
352 ),
353 entry(
354 SectionKind::NamedGraphs,
355 self.named_graphs_offset,
356 self.named_graphs_len,
357 ),
358 ];
359 if self.text_index_len > 0 {
360 entries.push(entry(
361 if self.text_index_partial {
362 SectionKind::TextIndexPartial
363 } else {
364 SectionKind::TextIndex
365 },
366 self.text_index_offset,
367 self.text_index_len,
368 ));
369 }
370 if self.build_info_len > 0 {
371 entries.push(entry(
372 SectionKind::BuildInfo,
373 self.build_info_offset,
374 self.build_info_len,
375 ));
376 }
377 entries.extend(self.extra_sections.iter().copied());
378 debug_assert!(
379 entries.len() <= MAX_SECTIONS,
380 "too many sections for a 1 KB header"
381 );
382 let n = entries.len().min(MAX_SECTIONS);
383 b[44..46].copy_from_slice(&(n as u16).to_le_bytes());
384 for (i, s) in entries.iter().take(n).enumerate() {
385 let p = SECTION_DIR_OFFSET + i * SECTION_ENTRY_LEN;
386 b[p..p + 2].copy_from_slice(&s.kind.to_u16().to_le_bytes());
387 b[p + 2..p + 4].copy_from_slice(&s.flags.to_le_bytes());
388 b[p + 8..p + 16].copy_from_slice(&s.offset.to_le_bytes());
390 b[p + 16..p + 24].copy_from_slice(&s.length.to_le_bytes());
391 }
392 b
393 }
394
395 pub fn from_bytes(b: &[u8]) -> Result<Self, HeaderError> {
397 if b.len() < HEADER_LEN {
398 return Err(HeaderError::TooSmall(b.len()));
399 }
400 if b[0..4] != MAGIC {
401 return Err(HeaderError::BadMagic);
402 }
403 if crate::v06::reads(b[4]) {
404 return crate::v06::Header06::from_bytes(b)
407 .map(|h| crate::v06::logical_header(&h))
408 .map_err(|e| match e {
409 crate::file::FileError::Container(m) => HeaderError::Malformed06(m),
410 _ => HeaderError::Malformed06("unreadable"),
411 });
412 }
413 if !(MIN_STABLE_READ_VERSION..=CURRENT_FORMAT_VERSION).contains(&b[4]) {
414 return Err(HeaderError::UnsupportedVersion {
415 found: b[4],
416 min: MIN_STABLE_READ_VERSION,
417 max: CURRENT_FORMAT_VERSION,
418 });
419 }
420 let u16_at = |o: usize| u16::from_le_bytes(b[o..o + 2].try_into().unwrap());
421 let u32_at = |o: usize| u32::from_le_bytes(b[o..o + 4].try_into().unwrap());
422 let u64_at = |o: usize| u64::from_le_bytes(b[o..o + 8].try_into().unwrap());
423
424 let section_count = u16_at(44) as usize;
425 if SECTION_DIR_OFFSET + section_count * SECTION_ENTRY_LEN > HEADER_LEN {
426 return Err(HeaderError::BadSectionCount(section_count));
427 }
428
429 let perms = if b[50] == 0 {
430 crate::index::PermSet::ALL
431 } else {
432 crate::index::PermSet::from_bits(b[50])
433 .map_err(|why| HeaderError::BadPermMask { mask: b[50], why })?
434 };
435
436 let mut h = Header {
437 version: b[4],
438 flags: b[5],
439 metadata_offset: 0,
440 metadata_len: 0,
441 dictionary_offset: 0,
442 dictionary_len: 0,
443 root_dir_offset: 0,
444 root_dir_len: 0,
445 pyramid_meta_offset: 0,
446 pyramid_meta_len: 0,
447 dict_codec: b[42],
448 block_codec: b[43],
449 pyramid_levels: u16_at(40),
450 id_widths: 0,
451 quad_count: u64_at(24),
452 term_count: u64_at(32),
453 content_hash: b[8..24].try_into().unwrap(),
454 named_graphs_offset: 0,
455 named_graphs_len: 0,
456 schema_meta_len: u32_at(46),
457 perms,
458 text_index_offset: 0,
459 text_index_len: 0,
460 text_index_partial: false,
461 build_info_offset: 0,
462 build_info_len: 0,
463 extra_sections: Vec::new(),
464 };
465 for i in 0..section_count {
466 let p = SECTION_DIR_OFFSET + i * SECTION_ENTRY_LEN;
467 let kind = SectionKind::from_u16(u16_at(p));
468 let offset = u64_at(p + 8);
469 let length = u64_at(p + 16);
470 match kind {
471 SectionKind::Metadata => {
472 h.metadata_offset = offset;
473 h.metadata_len = length;
474 }
475 SectionKind::Dictionary => {
476 h.dictionary_offset = offset;
477 h.dictionary_len = length;
478 }
479 SectionKind::Index => {
480 h.root_dir_offset = offset;
481 h.root_dir_len = length;
482 }
483 SectionKind::PyramidMeta => {
484 h.pyramid_meta_offset = offset;
485 h.pyramid_meta_len = length;
486 }
487 SectionKind::NamedGraphs => {
488 h.named_graphs_offset = offset;
489 h.named_graphs_len = length;
490 }
491 SectionKind::TextIndex => {
492 h.text_index_offset = offset;
493 h.text_index_len = length;
494 h.text_index_partial = false;
495 }
496 SectionKind::TextIndexPartial => {
497 h.text_index_offset = offset;
498 h.text_index_len = length;
499 h.text_index_partial = true;
500 }
501 SectionKind::BuildInfo => {
502 h.build_info_offset = offset;
503 h.build_info_len = length;
504 }
505 SectionKind::LabelIndex
506 | SectionKind::Schema
507 | SectionKind::QuadIndex
508 | SectionKind::Unknown(_) => h.extra_sections.push(Section {
509 kind,
510 flags: u16_at(p + 2),
511 offset,
512 length,
513 }),
514 }
515 }
516 Ok(h)
517 }
518
519 pub fn label_index_section(&self) -> Option<Section> {
521 self.section(SectionKind::LabelIndex)
522 .filter(|s| s.length > 0)
523 }
524
525 pub fn schema_section(&self) -> Option<Section> {
527 self.section(SectionKind::Schema).filter(|s| s.length > 0)
528 }
529
530 pub fn has_quads(&self) -> bool {
531 self.flags & FLAG_HAS_QUADS != 0
532 }
533
534 pub fn expected_file_len(&self) -> Option<u64> {
551 if crate::v06::reads(self.version) {
552 return Some(self.metadata_offset);
555 }
556 let mut end = HEADER_LEN as u64;
557 let named = [
558 (self.metadata_offset, self.metadata_len),
559 (self.dictionary_offset, self.dictionary_len),
560 (self.root_dir_offset, self.root_dir_len),
561 (self.pyramid_meta_offset, self.pyramid_meta_len),
562 (self.named_graphs_offset, self.named_graphs_len),
563 (self.text_index_offset, self.text_index_len),
564 (self.build_info_offset, self.build_info_len),
565 ];
566 let extras = self.extra_sections.iter().map(|s| (s.offset, s.length));
567 for (offset, length) in named.into_iter().chain(extras) {
568 if length == 0 {
569 continue; }
571 end = end.max(offset.checked_add(length)?);
572 }
573 end.checked_add(MAGIC.len() as u64)
574 }
575
576 pub fn has_quoted_triples(&self) -> bool {
578 self.flags & FLAG_HAS_QUOTED_TRIPLES != 0
579 }
580
581 pub fn term_order(&self) -> crate::term_order::TermOrder {
585 if self.version >= 0x08
586 || (self.version == 0x05 && self.flags & FLAG_SPARQL_TERM_ORDER != 0)
587 {
588 crate::term_order::TermOrder::Sparql
589 } else {
590 crate::term_order::TermOrder::Bytewise
591 }
592 }
593
594 pub fn has_tile_synopsis(&self) -> bool {
596 self.flags & FLAG_TILE_SYNOPSIS != 0
597 }
598
599 pub fn text_index_covers_named_graphs(&self) -> bool {
602 self.flags & FLAG_TEXT_INDEX_NAMED_GRAPHS != 0
603 }
604
605 pub fn text_index_payload(&self) -> (u64, u64) {
610 if self.text_index_partial {
611 (
612 self.text_index_offset
613 .saturating_add(TEXT_INDEX_PARTIAL_DIGEST_LEN),
614 self.text_index_len
615 .saturating_sub(TEXT_INDEX_PARTIAL_DIGEST_LEN),
616 )
617 } else {
618 (self.text_index_offset, self.text_index_len)
619 }
620 }
621
622 pub fn section(&self, kind: SectionKind) -> Option<Section> {
626 let (offset, length) = match kind {
627 SectionKind::Metadata => (self.metadata_offset, self.metadata_len),
628 SectionKind::Dictionary => (self.dictionary_offset, self.dictionary_len),
629 SectionKind::Index => (self.root_dir_offset, self.root_dir_len),
630 SectionKind::PyramidMeta => (self.pyramid_meta_offset, self.pyramid_meta_len),
631 SectionKind::NamedGraphs => (self.named_graphs_offset, self.named_graphs_len),
632 SectionKind::TextIndex if !self.text_index_partial => {
633 (self.text_index_offset, self.text_index_len)
634 }
635 SectionKind::TextIndexPartial if self.text_index_partial => {
636 (self.text_index_offset, self.text_index_len)
637 }
638 SectionKind::TextIndex | SectionKind::TextIndexPartial => (0, 0),
639 SectionKind::BuildInfo => (self.build_info_offset, self.build_info_len),
640 SectionKind::LabelIndex
641 | SectionKind::Schema
642 | SectionKind::QuadIndex
643 | SectionKind::Unknown(_) => {
644 return self.extra_sections.iter().find(|s| s.kind == kind).copied()
645 }
646 };
647 Some(Section {
648 kind,
649 flags: 0,
650 offset,
651 length,
652 })
653 }
654
655 pub fn with_section(mut self, kind: SectionKind, offset: u64, length: u64) -> Self {
659 match kind {
660 SectionKind::Metadata => {
661 self.metadata_offset = offset;
662 self.metadata_len = length;
663 }
664 SectionKind::Dictionary => {
665 self.dictionary_offset = offset;
666 self.dictionary_len = length;
667 }
668 SectionKind::Index => {
669 self.root_dir_offset = offset;
670 self.root_dir_len = length;
671 }
672 SectionKind::PyramidMeta => {
673 self.pyramid_meta_offset = offset;
674 self.pyramid_meta_len = length;
675 }
676 SectionKind::NamedGraphs => {
677 self.named_graphs_offset = offset;
678 self.named_graphs_len = length;
679 }
680 SectionKind::TextIndex => {
681 self.text_index_offset = offset;
682 self.text_index_len = length;
683 self.text_index_partial = false;
684 }
685 SectionKind::TextIndexPartial => {
686 self.text_index_offset = offset;
687 self.text_index_len = length;
688 self.text_index_partial = true;
689 }
690 SectionKind::BuildInfo => {
691 self.build_info_offset = offset;
692 self.build_info_len = length;
693 }
694 SectionKind::LabelIndex | SectionKind::Schema | SectionKind::QuadIndex => {
695 match self.extra_sections.iter_mut().find(|s| s.kind == kind) {
697 Some(s) => {
698 s.offset = offset;
699 s.length = length;
700 }
701 None => self.extra_sections.push(Section {
702 kind,
703 flags: 0,
704 offset,
705 length,
706 }),
707 }
708 }
709 SectionKind::Unknown(_) => self.extra_sections.push(Section {
710 kind,
711 flags: 0,
712 offset,
713 length,
714 }),
715 }
716 self
717 }
718
719 pub fn has_quad_index(&self) -> bool {
721 self.flags & FLAG_QUAD_INDEX != 0
722 }
723
724 pub fn quad_index_section(&self) -> Option<Section> {
726 self.section(SectionKind::QuadIndex)
727 .filter(|s| s.length > 0)
728 }
729
730 pub fn index_container_sections(&self) -> usize {
733 self.perms.len() + usize::from(self.has_quad_index())
734 }
735}
736
737#[cfg(test)]
738mod tests {
739 use super::*;
740
741 fn sample() -> Header {
742 Header {
743 version: LEGACY_FORMAT_VERSION,
744 flags: FLAG_HAS_QUADS,
745 metadata_offset: 1024,
746 metadata_len: 42,
747 dictionary_offset: 1066,
748 dictionary_len: 2048,
749 root_dir_offset: 3114,
750 root_dir_len: 256,
751 pyramid_meta_offset: 3370,
752 pyramid_meta_len: 64,
753 dict_codec: 1,
754 block_codec: 2,
755 pyramid_levels: 3,
756 id_widths: 0,
757 perms: crate::index::PermSet::ALL,
758 quad_count: 5,
759 term_count: 9,
760 content_hash: [7u8; 16],
761 named_graphs_offset: 3434,
762 named_graphs_len: 48,
763 schema_meta_len: 99,
764 text_index_offset: 0,
765 text_index_len: 0,
766 text_index_partial: false,
767 build_info_offset: 0,
768 build_info_len: 0,
769 extra_sections: Vec::new(),
770 }
771 }
772
773 #[test]
774 fn round_trip() {
775 let h = sample();
776 let bytes = h.to_bytes();
777 assert_eq!(bytes.len(), HEADER_LEN);
778 assert_eq!(&bytes[0..4], b"RETE");
779 let back = Header::from_bytes(&bytes).unwrap();
780 assert_eq!(h, back);
781 assert!(back.has_quads());
782 }
783
784 #[test]
785 fn byte_layout_matches_spec() {
786 let h = Header {
788 content_hash: [0xCC; 16],
789 quad_count: 0x99,
790 term_count: 0xAA,
791 pyramid_levels: 0xABCD,
792 id_widths: 0,
793 dict_codec: 0xA1,
794 block_codec: 0xA2,
795 schema_meta_len: 0xD00D,
796 metadata_offset: 0x11,
797 metadata_len: 0x22,
798 dictionary_offset: 0x33,
799 dictionary_len: 0x44,
800 ..sample()
801 };
802 let b = h.to_bytes();
803 let u16_at = |o: usize| u16::from_le_bytes(b[o..o + 2].try_into().unwrap());
804 let u64_at = |o: usize| u64::from_le_bytes(b[o..o + 8].try_into().unwrap());
805
806 assert_eq!(&b[0..4], b"RETE");
808 assert_eq!(b[4], LEGACY_FORMAT_VERSION);
809 assert_eq!(b[5], FLAG_HAS_QUADS);
810 assert_eq!(u16_at(6), HEADER_LEN as u16);
811 assert_eq!(&b[8..24], &[0xCC; 16]); assert_eq!(u64_at(24), 0x99); assert_eq!(u64_at(32), 0xAA); assert_eq!(u16_at(40), 0xABCD); assert_eq!(b[42], 0xA1); assert_eq!(b[43], 0xA2); assert_eq!(u16_at(44), 5); assert_eq!(u32::from_le_bytes(b[46..50].try_into().unwrap()), 0xD00D); assert_eq!(u16_at(64), 1); assert_eq!(u64_at(72), 0x11); assert_eq!(u64_at(80), 0x22); assert_eq!(u16_at(88), 2);
825 assert_eq!(u64_at(96), 0x33);
826 assert_eq!(u64_at(104), 0x44);
827 assert_eq!(b.len(), HEADER_LEN);
828 }
829
830 #[test]
831 fn expected_file_len_is_last_section_end_plus_footer() {
832 assert_eq!(sample().expected_file_len(), Some(3482 + 4));
834
835 let mut h = sample();
838 h.text_index_offset = 1 << 40;
839 h.text_index_len = 0;
840 assert_eq!(h.expected_file_len(), Some(3482 + 4));
841
842 let mut e = sample();
844 e.metadata_len = 0;
845 e.dictionary_len = 0;
846 e.root_dir_len = 0;
847 e.pyramid_meta_len = 0;
848 e.named_graphs_len = 0;
849 assert_eq!(e.expected_file_len(), Some(1024 + 4));
850
851 let mut c = sample();
854 c.named_graphs_offset = u64::MAX - 8;
855 c.named_graphs_len = 64;
856 assert_eq!(c.expected_file_len(), None);
857
858 let f = sample().with_section(SectionKind::Unknown(99), 1 << 20, 512);
860 assert_eq!(f.expected_file_len(), Some((1 << 20) + 512 + 4));
861 }
862
863 #[test]
864 fn rejects_bad_magic() {
865 let mut bytes = [0u8; HEADER_LEN];
866 bytes[4] = CURRENT_FORMAT_VERSION;
867 assert!(matches!(
868 Header::from_bytes(&bytes),
869 Err(HeaderError::BadMagic)
870 ));
871 }
872
873 #[test]
874 fn stable_reader_accepts_v1_baseline_and_rejects_pre_v1() {
875 let current = sample().to_bytes();
876 assert_eq!(current[4], 0x05);
877 assert_eq!(Header::from_bytes(¤t).unwrap().version, 0x05);
878
879 for old in 0x01..=0x04 {
880 let mut bytes = current;
881 bytes[4] = old;
882 let error = Header::from_bytes(&bytes).unwrap_err();
883 assert!(matches!(
884 &error,
885 HeaderError::UnsupportedVersion {
886 found,
887 min: 0x05,
888 max: 0x08
889 } if *found == old
890 ));
891 assert!(error
892 .to_string()
893 .contains("Pre-1.0 files must be rebuilt from RDF source with `rete build`"));
894 }
895
896 for unsupported in [0x00, 0x09, 0xff] {
897 let mut bytes = current;
898 bytes[4] = unsupported;
899 assert!(matches!(
900 Header::from_bytes(&bytes),
901 Err(HeaderError::UnsupportedVersion {
902 found,
903 min: 0x05,
904 max: 0x08
905 }) if found == unsupported
906 ));
907 }
908 }
909
910 #[test]
911 fn rejects_overrunning_section_count() {
912 let mut bad = sample().to_bytes();
913 bad[44..46].copy_from_slice(&9999u16.to_le_bytes());
914 assert!(matches!(
915 Header::from_bytes(&bad),
916 Err(HeaderError::BadSectionCount(9999))
917 ));
918 }
919
920 #[test]
921 fn unknown_section_survives_round_trip() {
922 let h = sample().with_section(SectionKind::Unknown(99), 4096, 512);
925 let back = Header::from_bytes(&h.to_bytes()).unwrap();
926 assert_eq!(back.extra_sections.len(), 1);
927 let s = back.section(SectionKind::Unknown(99)).unwrap();
928 assert_eq!((s.offset, s.length), (4096, 512));
929 let dict = back.section(SectionKind::Dictionary).unwrap();
931 assert_eq!(dict.offset, h.dictionary_offset);
932 assert_eq!(h, back);
933 }
934
935 #[test]
936 fn build_info_section_round_trips_and_is_optional() {
937 let plain = sample().to_bytes();
940 assert_eq!(u16::from_le_bytes(plain[44..46].try_into().unwrap()), 5);
941 assert_eq!(sample().section(SectionKind::BuildInfo).unwrap().length, 0);
942
943 let h = sample().with_section(SectionKind::BuildInfo, 1066, 300);
945 let bytes = h.to_bytes();
946 assert_eq!(u16::from_le_bytes(bytes[44..46].try_into().unwrap()), 6);
947 let back = Header::from_bytes(&bytes).unwrap();
948 assert_eq!(h, back);
949 let s = back.section(SectionKind::BuildInfo).unwrap();
950 assert_eq!((s.offset, s.length), (1066, 300));
951 assert!(back.extra_sections.is_empty(), "BuildInfo is a known kind");
952 let far = sample().with_section(SectionKind::BuildInfo, 1 << 21, 128);
954 assert_eq!(far.expected_file_len(), Some((1 << 21) + 128 + 4));
955 }
956
957 #[test]
958 fn label_index_and_schema_sections_are_known_extras() {
959 assert!(sample().label_index_section().is_none());
962 assert!(sample().schema_section().is_none());
963
964 let h = sample()
965 .with_section(SectionKind::LabelIndex, 8000, 300)
966 .with_section(SectionKind::Schema, 8300, 40);
967 let bytes = h.to_bytes();
968 assert_eq!(u16::from_le_bytes(bytes[44..46].try_into().unwrap()), 7);
969 assert_eq!(
971 u16::from_le_bytes(bytes[64 + 5 * 24..][..2].try_into().unwrap()),
972 8
973 );
974 assert_eq!(
975 u16::from_le_bytes(bytes[64 + 6 * 24..][..2].try_into().unwrap()),
976 9
977 );
978 let back = Header::from_bytes(&bytes).unwrap();
979 assert_eq!(h, back);
980 assert_eq!(back.extra_sections.len(), 2, "stored with the extras");
981 let l = back.label_index_section().unwrap();
982 assert_eq!((l.offset, l.length), (8000, 300));
983 let s = back.schema_section().unwrap();
984 assert_eq!((s.offset, s.length), (8300, 40));
985 assert_eq!(back.expected_file_len(), Some(8340 + 4));
986
987 let again = back.with_section(SectionKind::Schema, 9000, 10);
989 assert_eq!(again.extra_sections.len(), 2);
990 assert_eq!(again.schema_section().unwrap().offset, 9000);
991
992 let empty = sample().with_section(SectionKind::LabelIndex, 0, 0);
994 assert!(empty.label_index_section().is_none());
995 }
996
997 #[test]
998 fn quad_index_flag_and_section_round_trip() {
999 let plain = sample();
1001 assert!(!plain.has_quad_index());
1002 assert!(plain.quad_index_section().is_none());
1003 assert_eq!(plain.index_container_sections(), 6);
1004
1005 let mut h = sample().with_section(SectionKind::QuadIndex, 7000, 900);
1008 h.flags |= FLAG_QUAD_INDEX;
1009 h.named_graphs_offset = 0;
1010 h.named_graphs_len = 0;
1011 let bytes = h.to_bytes();
1012 assert_eq!(bytes[5] & 0b1000, 0b1000, "flag bit 3");
1013 let n = u16::from_le_bytes(bytes[44..46].try_into().unwrap()) as usize;
1014 let kinds: Vec<u16> = (0..n)
1015 .map(|i| {
1016 let p = SECTION_DIR_OFFSET + i * SECTION_ENTRY_LEN;
1017 u16::from_le_bytes(bytes[p..p + 2].try_into().unwrap())
1018 })
1019 .collect();
1020 assert!(kinds.contains(&10), "a kind-10 directory entry: {kinds:?}");
1021 let back = Header::from_bytes(&bytes).unwrap();
1022 assert_eq!(back, h);
1023 assert!(back.has_quad_index());
1024 let s = back.quad_index_section().unwrap();
1025 assert_eq!(
1026 (s.kind, s.offset, s.length),
1027 (SectionKind::QuadIndex, 7000, 900)
1028 );
1029 assert_eq!(back.index_container_sections(), 7);
1030 assert_eq!(back.expected_file_len(), Some(7000 + 900 + 4));
1031 let mut lean = back.clone();
1033 lean.perms = crate::index::PermSet::CORE;
1034 assert_eq!(lean.index_container_sections(), 4);
1035 let again = back.with_section(SectionKind::QuadIndex, 8000, 10);
1037 assert_eq!(again.extra_sections.len(), 1);
1038 assert_eq!(again.quad_index_section().unwrap().offset, 8000);
1039 }
1040
1041 #[test]
1042 fn text_index_section_round_trips_and_is_optional() {
1043 assert_eq!(
1046 u16::from_le_bytes(sample().to_bytes()[44..46].try_into().unwrap()),
1047 5
1048 );
1049 assert!(sample().section(SectionKind::TextIndex).unwrap().length == 0);
1050
1051 let h = sample().with_section(SectionKind::TextIndex, 5000, 4096);
1053 let bytes = h.to_bytes();
1054 assert_eq!(u16::from_le_bytes(bytes[44..46].try_into().unwrap()), 6);
1055 let back = Header::from_bytes(&bytes).unwrap();
1056 assert_eq!(h, back);
1057 let s = back.section(SectionKind::TextIndex).unwrap();
1058 assert_eq!((s.offset, s.length), (5000, 4096));
1059 assert!(back.extra_sections.is_empty(), "TextIndex is a known kind");
1060 assert!(!back.text_index_partial);
1061 }
1062
1063 #[test]
1067 fn partial_text_index_is_kind_11_and_invisible_as_kind_6() {
1068 let h = sample().with_section(SectionKind::TextIndexPartial, 5000, 4096);
1069 assert!(h.text_index_partial);
1070 let bytes = h.to_bytes();
1071 assert_eq!(u16::from_le_bytes(bytes[44..46].try_into().unwrap()), 6);
1072 let p = SECTION_DIR_OFFSET + 5 * SECTION_ENTRY_LEN;
1074 assert_eq!(u16::from_le_bytes(bytes[p..p + 2].try_into().unwrap()), 11);
1075 let back = Header::from_bytes(&bytes).unwrap();
1076 assert_eq!(h, back);
1077 assert!(back.text_index_partial);
1078 assert_eq!((back.text_index_offset, back.text_index_len), (5000, 4096));
1079 let s = back.section(SectionKind::TextIndexPartial).unwrap();
1080 assert_eq!((s.offset, s.length), (5000, 4096));
1081 assert_eq!(back.section(SectionKind::TextIndex).unwrap().length, 0);
1082 assert!(back.extra_sections.is_empty(), "kind 11 is known here");
1083
1084 let mut old_view = bytes;
1088 old_view[p..p + 2].copy_from_slice(&0x7777u16.to_le_bytes());
1089 let old = Header::from_bytes(&old_view).unwrap();
1090 assert_eq!(old.text_index_len, 0);
1091 assert_eq!(old.extra_sections.len(), 1);
1092 }
1093}