revault_lockbox_api 0.0.1

reVault lockbox API to create and manage lockboxes
Documentation
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
use crate::compression::validate_compression_frame_lengths;
use crate::constants::{
    DEFAULT_DIRECTORY_PERMISSIONS, DEFAULT_FILE_PERMISSIONS, DEFAULT_SYMLINK_PERMISSIONS,
};
use crate::file_chunk::{CompressionFrameSegment, FileChunk};
use crate::node_kind::NodeKind;
use crate::security::validate_permissions;
use crate::toc_entry::TocEntry;
use crate::{Error, LockboxPath, Result};

const PATH_RESTART_INTERVAL: usize = 128;
const ENTRY_FLAG_DELETED: u8 = 0x01;
const ENTRY_FLAG_SYMLINK: u8 = 0x02;
const ENTRY_FLAG_CUSTOM_PERMISSIONS: u8 = 0x04;
const ENTRY_FLAG_DIRECTORY: u8 = 0x08;

#[cfg(test)]
pub(crate) fn encode_toc(
    toc_entries: &std::collections::BTreeMap<LockboxPath, TocEntry>,
) -> Vec<u8> {
    encode_toc_entries(toc_entries.values())
}

pub(crate) fn encode_toc_entries<'a>(entries: impl IntoIterator<Item = &'a TocEntry>) -> Vec<u8> {
    let entries = entries.into_iter().collect::<Vec<_>>();
    let descriptors = frame_descriptors(&entries);
    let mut out = Vec::new();
    put_varint(entries.len() as u64, &mut out);
    put_varint(descriptors.len() as u64, &mut out);
    for descriptor in &descriptors {
        encode_frame_descriptor(descriptor, &mut out);
    }
    let mut previous_path = "";
    for (index, entry) in entries.iter().enumerate() {
        let default_permissions = default_permissions(entry.node_kind);
        let mut flags = 0u8;
        if entry.deleted {
            flags |= ENTRY_FLAG_DELETED;
        }
        if entry.node_kind == NodeKind::Symlink {
            flags |= ENTRY_FLAG_SYMLINK;
        }
        if entry.node_kind == NodeKind::Directory {
            flags |= ENTRY_FLAG_DIRECTORY;
        }
        if entry.permissions != default_permissions {
            flags |= ENTRY_FLAG_CUSTOM_PERMISSIONS;
        }

        out.push(flags);
        encode_path(entry.path.as_str(), previous_path, index, &mut out);
        put_varint(entry.len, &mut out);
        put_varint(entry.record_offset, &mut out);
        put_varint(entry.record_len, &mut out);
        put_varint(entry.record_object_id, &mut out);
        if flags & ENTRY_FLAG_CUSTOM_PERMISSIONS != 0 {
            put_varint(entry.permissions as u64, &mut out);
        }
        put_varint(entry.chunks.len() as u64, &mut out);
        for chunk in &entry.chunks {
            let stored_path = if chunk.stored_path == entry.path.as_str() {
                ""
            } else {
                chunk.stored_path.as_str()
            };
            put_varint(stored_path.len() as u64, &mut out);
            out.extend_from_slice(stored_path.as_bytes());
            put_varint(chunk.file_offset, &mut out);
            put_varint(chunk.len, &mut out);
            put_varint(chunk.compression_frame_offset, &mut out);
            let descriptor_index = frame_descriptor_index(&descriptors, chunk).unwrap_or(0);
            put_varint(descriptor_index as u64, &mut out);
        }
        previous_path = entry.path.as_str();
    }
    out
}

pub(crate) fn encoded_toc_entries_len(entries: &[TocEntry]) -> usize {
    let descriptors = frame_descriptors(&entries.iter().collect::<Vec<_>>());
    encoded_toc_count_len(entries.len())
        + encoded_toc_count_len(descriptors.len())
        + descriptors
            .iter()
            .map(encoded_descriptor_len)
            .sum::<usize>()
        + encoded_entries_with_descriptors_len(entries, &descriptors)
}

pub(crate) fn encoded_toc_count_len(count: usize) -> usize {
    varint_len(count as u64)
}

#[derive(Debug, Default)]
pub(crate) struct TocEntriesLenEstimator {
    count: usize,
    descriptors: Vec<FrameDescriptor>,
    descriptor_len: usize,
    entries_len: usize,
    previous_path: String,
}

impl TocEntriesLenEstimator {
    pub(crate) fn new() -> Self {
        Self::default()
    }

    pub(crate) fn count(&self) -> usize {
        self.count
    }

    pub(crate) fn clear(&mut self) {
        self.count = 0;
        self.descriptors.clear();
        self.descriptor_len = 0;
        self.entries_len = 0;
        self.previous_path.clear();
    }

    pub(crate) fn push(&mut self, entry: &TocEntry) {
        for chunk in &entry.chunks {
            if frame_descriptor_index(&self.descriptors, chunk).is_none() {
                let descriptor = FrameDescriptor::from_chunk(chunk);
                self.descriptor_len += encoded_descriptor_len(&descriptor);
                self.descriptors.push(descriptor);
            }
        }
        self.entries_len += encoded_toc_entry_len(
            entry,
            Some(&self.previous_path),
            self.count,
            &self.descriptors,
        );
        self.previous_path.clear();
        self.previous_path.push_str(entry.path.as_str());
        self.count += 1;
    }

    pub(crate) fn encoded_len(&self) -> usize {
        encoded_toc_count_len(self.count)
            + encoded_toc_count_len(self.descriptors.len())
            + self.descriptor_len
            + self.entries_len
    }
}

fn encoded_entries_with_descriptors_len(
    entries: &[TocEntry],
    descriptors: &[FrameDescriptor],
) -> usize {
    entries
        .iter()
        .enumerate()
        .map(|(index, entry)| {
            let previous_path = index
                .checked_sub(1)
                .and_then(|previous| entries.get(previous))
                .map(|entry| entry.path.as_str());
            encoded_toc_entry_len(entry, previous_path, index, descriptors)
        })
        .sum::<usize>()
}

fn encoded_toc_entry_len(
    entry: &TocEntry,
    previous_path: Option<&str>,
    index: usize,
    descriptors: &[FrameDescriptor],
) -> usize {
    let default_permissions = default_permissions(entry.node_kind);
    1 + encoded_path_len(entry.path.as_str(), previous_path.unwrap_or(""), index)
        + varint_len(entry.len)
        + varint_len(entry.record_offset)
        + varint_len(entry.record_len)
        + varint_len(entry.record_object_id)
        + if entry.permissions == default_permissions {
            0
        } else {
            varint_len(entry.permissions as u64)
        }
        + varint_len(entry.chunks.len() as u64)
        + entry
            .chunks
            .iter()
            .map(|chunk| encoded_chunk_len(entry, chunk, descriptors))
            .sum::<usize>()
}

#[cfg(test)]
pub(crate) fn decode_toc(
    payload: &[u8],
) -> Result<std::collections::BTreeMap<LockboxPath, TocEntry>> {
    Ok(decode_toc_entries(payload)?
        .into_iter()
        .map(|entry| (entry.path.clone(), entry))
        .collect())
}

pub(crate) fn decode_toc_entries(payload: &[u8]) -> Result<Vec<TocEntry>> {
    let mut offset = 0;
    let count =
        usize::try_from(take_varint(payload, &mut offset)?).map_err(|_| Error::CorruptRecord)?;
    if count > payload.len().saturating_sub(offset) {
        return Err(Error::CorruptRecord);
    }
    let descriptor_count =
        usize::try_from(take_varint(payload, &mut offset)?).map_err(|_| Error::CorruptRecord)?;
    if descriptor_count > payload.len().saturating_sub(offset) {
        return Err(Error::CorruptRecord);
    }
    let mut descriptors = Vec::with_capacity(descriptor_count);
    for _ in 0..descriptor_count {
        descriptors.push(decode_frame_descriptor(payload, &mut offset)?);
    }

    let mut entries = Vec::with_capacity(count);
    let mut previous_path = String::new();
    for index in 0..count {
        if offset >= payload.len() {
            return Err(Error::CorruptRecord);
        }
        let flags = payload[offset];
        offset += 1;
        if flags
            & !(ENTRY_FLAG_DELETED
                | ENTRY_FLAG_SYMLINK
                | ENTRY_FLAG_CUSTOM_PERMISSIONS
                | ENTRY_FLAG_DIRECTORY)
            != 0
        {
            return Err(Error::CorruptRecord);
        }
        if flags & ENTRY_FLAG_SYMLINK != 0 && flags & ENTRY_FLAG_DIRECTORY != 0 {
            return Err(Error::CorruptRecord);
        }

        let path = decode_path(payload, &mut offset, &previous_path, index)?;
        let path = LockboxPath::from_stored(&path, false)?;
        let next_previous_path = path.as_str().to_string();
        let len = take_varint(payload, &mut offset)?;
        let record_offset = take_varint(payload, &mut offset)?;
        let record_len = take_varint(payload, &mut offset)?;
        let record_object_id = take_varint(payload, &mut offset)?;
        let deleted = flags & ENTRY_FLAG_DELETED != 0;
        let node_kind = NodeKind::from_u8(if flags & ENTRY_FLAG_DIRECTORY != 0 {
            3
        } else if flags & ENTRY_FLAG_SYMLINK != 0 {
            2
        } else {
            1
        })?;
        let permissions = if flags & ENTRY_FLAG_CUSTOM_PERMISSIONS == 0 {
            default_permissions(node_kind)
        } else {
            u32::try_from(take_varint(payload, &mut offset)?).map_err(|_| Error::CorruptRecord)?
        };
        let permissions = validate_permissions(permissions)?;
        let chunk_count = usize::try_from(take_varint(payload, &mut offset)?)
            .map_err(|_| Error::CorruptRecord)?;
        if chunk_count > payload.len().saturating_sub(offset) {
            return Err(Error::CorruptRecord);
        }
        let mut chunks = Vec::with_capacity(chunk_count);
        for _ in 0..chunk_count {
            let stored_path_len = usize::try_from(take_varint(payload, &mut offset)?)
                .map_err(|_| Error::CorruptRecord)?;
            if stored_path_len > payload.len().saturating_sub(offset) {
                return Err(Error::CorruptRecord);
            }
            let stored_path = if stored_path_len == 0 {
                path.clone()
            } else {
                let stored_path =
                    String::from_utf8(payload[offset..offset + stored_path_len].to_vec())
                        .map_err(|_| Error::CorruptRecord)?;
                LockboxPath::from_stored(&stored_path, false)?
            };
            offset += stored_path_len;
            let file_offset = take_varint(payload, &mut offset)?;
            let chunk_len = take_varint(payload, &mut offset)?;
            let compression_frame_offset = take_varint(payload, &mut offset)?;
            let descriptor_index = usize::try_from(take_varint(payload, &mut offset)?)
                .map_err(|_| Error::CorruptRecord)?;
            let descriptor = descriptors
                .get(descriptor_index)
                .ok_or(Error::CorruptRecord)?;
            chunks.push(FileChunk {
                stored_path,
                file_offset,
                len: chunk_len,
                compression_frame_offset,
                compression_frame_len: descriptor.compression_frame_len,
                compressed_len: descriptor.compressed_len,
                compression: descriptor.compression,
                compression_frame_id: descriptor.compression_frame_id,
                compression_frame_digest: descriptor.compression_frame_digest,
                segments: descriptor.segments.clone(),
            });
        }
        match node_kind {
            NodeKind::Symlink if record_offset == 0 || record_len == 0 || record_object_id == 0 => {
                return Err(Error::CorruptRecord);
            }
            NodeKind::Symlink if !chunks.is_empty() => return Err(Error::CorruptRecord),
            NodeKind::Directory
                if len != 0
                    || record_offset != 0
                    || record_len != 0
                    || record_object_id != 0
                    || !chunks.is_empty() =>
            {
                return Err(Error::CorruptRecord);
            }
            _ => {}
        }
        entries.push(TocEntry {
            path,
            len,
            record_offset,
            record_len,
            record_object_id,
            deleted,
            node_kind,
            permissions,
            chunks,
        });
        previous_path = next_previous_path;
    }
    if offset != payload.len() {
        return Err(Error::CorruptRecord);
    }
    Ok(entries)
}

fn encode_path(path: &str, previous_path: &str, index: usize, out: &mut Vec<u8>) {
    if path_restart(index) {
        put_varint(path.len() as u64, out);
        out.extend_from_slice(path.as_bytes());
        return;
    }

    let prefix_len = common_prefix_len(previous_path, path);
    let suffix = &path.as_bytes()[prefix_len..];
    put_varint(prefix_len as u64, out);
    put_varint(suffix.len() as u64, out);
    out.extend_from_slice(suffix);
}

fn decode_path(
    payload: &[u8],
    offset: &mut usize,
    previous_path: &str,
    index: usize,
) -> Result<String> {
    if path_restart(index) {
        let path_len =
            usize::try_from(take_varint(payload, offset)?).map_err(|_| Error::CorruptRecord)?;
        if path_len > payload.len().saturating_sub(*offset) {
            return Err(Error::CorruptRecord);
        }
        let path = String::from_utf8(payload[*offset..*offset + path_len].to_vec())
            .map_err(|_| Error::CorruptRecord)?;
        *offset += path_len;
        return Ok(path);
    }

    let prefix_len =
        usize::try_from(take_varint(payload, offset)?).map_err(|_| Error::CorruptRecord)?;
    let suffix_len =
        usize::try_from(take_varint(payload, offset)?).map_err(|_| Error::CorruptRecord)?;
    if prefix_len > previous_path.len()
        || !previous_path.is_char_boundary(prefix_len)
        || suffix_len > payload.len().saturating_sub(*offset)
    {
        return Err(Error::CorruptRecord);
    }
    let mut path = Vec::with_capacity(prefix_len + suffix_len);
    path.extend_from_slice(&previous_path.as_bytes()[..prefix_len]);
    path.extend_from_slice(&payload[*offset..*offset + suffix_len]);
    *offset += suffix_len;
    String::from_utf8(path).map_err(|_| Error::CorruptRecord)
}

fn encoded_path_len(path: &str, previous_path: &str, index: usize) -> usize {
    if path_restart(index) {
        return varint_len(path.len() as u64) + path.len();
    }
    let prefix_len = common_prefix_len(previous_path, path);
    let suffix_len = path.len() - prefix_len;
    varint_len(prefix_len as u64) + varint_len(suffix_len as u64) + suffix_len
}

#[derive(Debug, Clone, PartialEq, Eq)]
struct FrameDescriptor {
    compression_frame_id: u64,
    compression: u8,
    compression_frame_len: u64,
    compressed_len: u64,
    compression_frame_digest: [u8; 32],
    segments: Vec<CompressionFrameSegment>,
}

impl FrameDescriptor {
    fn from_chunk(chunk: &FileChunk) -> Self {
        Self {
            compression_frame_id: chunk.compression_frame_id,
            compression: chunk.compression,
            compression_frame_len: chunk.compression_frame_len,
            compressed_len: chunk.compressed_len,
            compression_frame_digest: chunk.compression_frame_digest,
            segments: chunk.segments.clone(),
        }
    }

    fn matches_chunk(&self, chunk: &FileChunk) -> bool {
        self.compression_frame_id == chunk.compression_frame_id
            && self.compression == chunk.compression
            && self.compression_frame_len == chunk.compression_frame_len
            && self.compressed_len == chunk.compressed_len
            && self.compression_frame_digest == chunk.compression_frame_digest
            && self.segments == chunk.segments
    }
}

fn frame_descriptors(entries: &[&TocEntry]) -> Vec<FrameDescriptor> {
    let mut descriptors = Vec::new();
    for entry in entries {
        for chunk in &entry.chunks {
            if frame_descriptor_index(&descriptors, chunk).is_none() {
                descriptors.push(FrameDescriptor::from_chunk(chunk));
            }
        }
    }
    descriptors
}

fn frame_descriptor_index(descriptors: &[FrameDescriptor], chunk: &FileChunk) -> Option<usize> {
    descriptors
        .iter()
        .position(|descriptor| descriptor.matches_chunk(chunk))
}

fn encode_frame_descriptor(descriptor: &FrameDescriptor, out: &mut Vec<u8>) {
    put_varint(descriptor.compression_frame_id, out);
    put_varint(descriptor.compression as u64, out);
    put_varint(descriptor.compression_frame_len, out);
    put_varint(descriptor.compressed_len, out);
    out.extend_from_slice(&descriptor.compression_frame_digest);
    put_varint(descriptor.segments.len() as u64, out);
    for segment in &descriptor.segments {
        put_varint(segment.page_offset, out);
        put_varint(segment.page_len, out);
        put_varint(segment.object_id, out);
        put_varint(segment.segment_offset, out);
        put_varint(segment.segment_len, out);
    }
}

fn decode_frame_descriptor(payload: &[u8], offset: &mut usize) -> Result<FrameDescriptor> {
    let compression_frame_id = take_varint(payload, offset)?;
    let compression =
        u8::try_from(take_varint(payload, offset)?).map_err(|_| Error::CorruptRecord)?;
    let compression_frame_len = take_varint(payload, offset)?;
    let compressed_len = take_varint(payload, offset)?;
    validate_compression_frame_lengths(compression_frame_len, compressed_len)?;
    if *offset + 32 > payload.len() {
        return Err(Error::CorruptRecord);
    }
    let mut compression_frame_digest = [0u8; 32];
    compression_frame_digest.copy_from_slice(&payload[*offset..*offset + 32]);
    *offset += 32;
    let segment_count =
        usize::try_from(take_varint(payload, offset)?).map_err(|_| Error::CorruptRecord)?;
    if segment_count > payload.len().saturating_sub(*offset) {
        return Err(Error::CorruptRecord);
    }
    let mut segments = Vec::with_capacity(segment_count);
    for _ in 0..segment_count {
        let page_offset = take_varint(payload, offset)?;
        let page_len = take_varint(payload, offset)?;
        let object_id = take_varint(payload, offset)?;
        let segment_offset = take_varint(payload, offset)?;
        let segment_len = take_varint(payload, offset)?;
        if segment_offset
            .checked_add(segment_len)
            .is_none_or(|end| end > compressed_len)
        {
            return Err(Error::CorruptRecord);
        }
        segments.push(CompressionFrameSegment {
            page_offset,
            page_len,
            object_id,
            segment_offset,
            segment_len,
        });
    }
    Ok(FrameDescriptor {
        compression_frame_id,
        compression,
        compression_frame_len,
        compressed_len,
        compression_frame_digest,
        segments,
    })
}

fn encoded_descriptor_len(descriptor: &FrameDescriptor) -> usize {
    varint_len(descriptor.compression_frame_id)
        + varint_len(descriptor.compression as u64)
        + varint_len(descriptor.compression_frame_len)
        + varint_len(descriptor.compressed_len)
        + 32
        + varint_len(descriptor.segments.len() as u64)
        + descriptor
            .segments
            .iter()
            .map(encoded_segment_len)
            .sum::<usize>()
}

fn encoded_chunk_len(
    entry: &TocEntry,
    chunk: &FileChunk,
    descriptors: &[FrameDescriptor],
) -> usize {
    let stored_path_len = if chunk.stored_path == entry.path.as_str() {
        0
    } else {
        chunk.stored_path.len()
    };
    varint_len(stored_path_len as u64)
        + stored_path_len
        + varint_len(chunk.file_offset)
        + varint_len(chunk.len)
        + varint_len(chunk.compression_frame_offset)
        + varint_len(frame_descriptor_index(descriptors, chunk).unwrap_or(0) as u64)
}

fn encoded_segment_len(segment: &CompressionFrameSegment) -> usize {
    varint_len(segment.page_offset)
        + varint_len(segment.page_len)
        + varint_len(segment.object_id)
        + varint_len(segment.segment_offset)
        + varint_len(segment.segment_len)
}

fn path_restart(index: usize) -> bool {
    index.is_multiple_of(PATH_RESTART_INTERVAL)
}

fn common_prefix_len(previous: &str, current: &str) -> usize {
    let mut len = previous
        .as_bytes()
        .iter()
        .zip(current.as_bytes())
        .take_while(|(left, right)| left == right)
        .count();
    while len > 0 && !current.is_char_boundary(len) {
        len -= 1;
    }
    len
}

fn default_permissions(node_kind: NodeKind) -> u32 {
    match node_kind {
        NodeKind::File => DEFAULT_FILE_PERMISSIONS,
        NodeKind::Symlink => DEFAULT_SYMLINK_PERMISSIONS,
        NodeKind::Directory => DEFAULT_DIRECTORY_PERMISSIONS,
    }
}

fn put_varint(mut value: u64, out: &mut Vec<u8>) {
    while value >= 0x80 {
        out.push((value as u8) | 0x80);
        value >>= 7;
    }
    out.push(value as u8);
}

fn take_varint(payload: &[u8], cursor: &mut usize) -> Result<u64> {
    let mut value = 0u64;
    let mut shift = 0u32;
    for _ in 0..10 {
        if *cursor >= payload.len() {
            return Err(Error::CorruptRecord);
        }
        let byte = payload[*cursor];
        *cursor += 1;
        value |= u64::from(byte & 0x7f) << shift;
        if byte & 0x80 == 0 {
            return Ok(value);
        }
        shift += 7;
    }
    Err(Error::CorruptRecord)
}

pub(crate) fn varint_len(mut value: u64) -> usize {
    let mut len = 1;
    while value >= 0x80 {
        len += 1;
        value >>= 7;
    }
    len
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::collections::BTreeMap;

    #[test]
    fn decoded_toc_rejects_tampered_host_paths() {
        let mut toc_entries = BTreeMap::new();
        let invalid_path = LockboxPath::from_unchecked_for_test("/C:/Users/target.txt");
        toc_entries.insert(
            invalid_path.clone(),
            TocEntry {
                path: invalid_path,
                len: 1,
                record_offset: 64,
                record_len: 64,
                record_object_id: 1,
                deleted: false,
                node_kind: NodeKind::File,
                permissions: DEFAULT_FILE_PERMISSIONS,
                chunks: Vec::new(),
            },
        );
        let payload = encode_toc(&toc_entries);
        assert!(matches!(decode_toc(&payload), Err(Error::InvalidPath(_))));
    }

    #[test]
    fn decoded_toc_rejects_impossible_entry_count_before_allocating() {
        let payload = u64::MAX.to_le_bytes();

        assert!(matches!(
            decode_toc_entries(&payload),
            Err(Error::CorruptRecord)
        ));
    }

    #[test]
    fn decoded_toc_rejects_symlink_without_object_reference() {
        let payload = encoded_symlink_entry("/links/current", 0, 0, 0);

        assert!(matches!(
            decode_toc_entries(&payload),
            Err(Error::CorruptRecord)
        ));
    }

    #[test]
    fn toc_entries_round_trip_front_coded_paths_with_restarts() {
        let entries = (0..260)
            .map(|i| TocEntry {
                path: LockboxPath::new(format!("/docs/chapter-{i:03}/index.txt")).unwrap(),
                len: 25_600,
                record_offset: 96 + i * 1024,
                record_len: 1024,
                record_object_id: i + 1,
                deleted: false,
                node_kind: NodeKind::File,
                permissions: DEFAULT_FILE_PERMISSIONS,
                chunks: Vec::new(),
            })
            .collect::<Vec<_>>();

        let payload = encode_toc_entries(entries.iter());
        assert_eq!(payload.len(), encoded_toc_entries_len(&entries));
        let decoded = decode_toc_entries(&payload).unwrap();

        assert_eq!(decoded.len(), entries.len());
        for (decoded, expected) in decoded.iter().zip(entries.iter()) {
            assert_eq!(decoded.path, expected.path);
            assert_eq!(decoded.len, expected.len);
            assert_eq!(decoded.record_offset, expected.record_offset);
            assert_eq!(decoded.record_len, expected.record_len);
            assert_eq!(decoded.record_object_id, expected.record_object_id);
        }
    }

    #[test]
    fn front_coding_handles_unicode_boundaries() {
        let entries = [
            TocEntry {
                path: LockboxPath::new("/docs/cafe\u{301}/a.txt").unwrap(),
                len: 1,
                record_offset: 64,
                record_len: 64,
                record_object_id: 1,
                deleted: false,
                node_kind: NodeKind::File,
                permissions: DEFAULT_FILE_PERMISSIONS,
                chunks: Vec::new(),
            },
            TocEntry {
                path: LockboxPath::new("/docs/cafe\u{301}/b.txt").unwrap(),
                len: 1,
                record_offset: 64,
                record_len: 64,
                record_object_id: 1,
                deleted: false,
                node_kind: NodeKind::File,
                permissions: DEFAULT_FILE_PERMISSIONS,
                chunks: Vec::new(),
            },
        ];

        let payload = encode_toc_entries(entries.iter());
        let decoded = decode_toc_entries(&payload).unwrap();

        assert_eq!(decoded[0].path, entries[0].path);
        assert_eq!(decoded[1].path, entries[1].path);
    }

    #[test]
    fn toc_entries_share_frame_descriptors_across_chunks() {
        let entries = (0..4)
            .map(|i| TocEntry {
                path: LockboxPath::new(format!("/tree/file-{i}.bin")).unwrap(),
                len: 128,
                record_offset: 4096,
                record_len: 4096,
                record_object_id: 10,
                deleted: false,
                node_kind: NodeKind::File,
                permissions: DEFAULT_FILE_PERMISSIONS,
                chunks: vec![shared_frame_chunk(i)],
            })
            .collect::<Vec<_>>();

        let payload = encode_toc_entries(entries.iter());
        assert_eq!(payload.len(), encoded_toc_entries_len(&entries));
        let decoded = decode_toc_entries(&payload).unwrap();

        assert_eq!(decoded.len(), entries.len());
        for (decoded, expected) in decoded.iter().zip(entries.iter()) {
            assert_eq!(decoded.path, expected.path);
            assert_eq!(decoded.len, expected.len);
            assert_eq!(decoded.chunks, expected.chunks);
        }
        assert!(
            payload.len()
                < entries
                    .iter()
                    .map(|entry| 64 + entry.path.len())
                    .sum::<usize>()
        );
    }

    #[test]
    fn toc_decode_rejects_invalid_frame_descriptor_index() {
        let entry = TocEntry {
            path: LockboxPath::new("/tree/file.bin").unwrap(),
            len: 128,
            record_offset: 4096,
            record_len: 4096,
            record_object_id: 10,
            deleted: false,
            node_kind: NodeKind::File,
            permissions: DEFAULT_FILE_PERMISSIONS,
            chunks: vec![shared_frame_chunk(0)],
        };
        let mut payload = encode_toc_entries([&entry]);
        let last = payload.last_mut().unwrap();
        *last = last.saturating_add(1);

        assert!(matches!(
            decode_toc_entries(&payload),
            Err(Error::CorruptRecord)
        ));
    }

    #[test]
    fn toc_decode_rejects_compressed_len_larger_than_frame_len() {
        let mut entry = TocEntry {
            path: LockboxPath::new("/tree/file.bin").unwrap(),
            len: 128,
            record_offset: 4096,
            record_len: 4096,
            record_object_id: 10,
            deleted: false,
            node_kind: NodeKind::File,
            permissions: DEFAULT_FILE_PERMISSIONS,
            chunks: vec![shared_frame_chunk(0)],
        };
        entry.chunks[0].compression_frame_len = 4;
        entry.chunks[0].compressed_len = 5;

        assert!(matches!(
            decode_toc_entries(&encode_toc_entries([&entry])),
            Err(Error::CorruptRecord)
        ));
    }

    #[test]
    fn toc_decode_rejects_segment_outside_compressed_frame() {
        let mut entry = TocEntry {
            path: LockboxPath::new("/tree/file.bin").unwrap(),
            len: 128,
            record_offset: 4096,
            record_len: 4096,
            record_object_id: 10,
            deleted: false,
            node_kind: NodeKind::File,
            permissions: DEFAULT_FILE_PERMISSIONS,
            chunks: vec![shared_frame_chunk(0)],
        };
        entry.chunks[0].segments[0].segment_offset = 41;
        entry.chunks[0].segments[0].segment_len = 2;

        assert!(matches!(
            decode_toc_entries(&encode_toc_entries([&entry])),
            Err(Error::CorruptRecord)
        ));
    }

    fn encoded_symlink_entry(
        path: &str,
        record_offset: u64,
        record_len: u64,
        record_object_id: u64,
    ) -> Vec<u8> {
        encode_toc_entries([&TocEntry {
            path: LockboxPath::new(path).unwrap(),
            len: 0,
            record_offset,
            record_len,
            record_object_id,
            deleted: false,
            node_kind: NodeKind::Symlink,
            permissions: DEFAULT_SYMLINK_PERMISSIONS,
            chunks: Vec::new(),
        }])
    }

    fn shared_frame_chunk(index: u64) -> FileChunk {
        FileChunk {
            stored_path: LockboxPath::new(format!("/tree/file-{index}.bin")).unwrap(),
            file_offset: 0,
            len: 128,
            compression_frame_offset: index * 128,
            compression_frame_len: 512,
            compressed_len: 42,
            compression: 1,
            compression_frame_id: 7,
            compression_frame_digest: [9; 32],
            segments: vec![CompressionFrameSegment {
                page_offset: 4096,
                page_len: 4096,
                object_id: 10,
                segment_offset: 0,
                segment_len: 42,
            }],
        }
    }
}