Internal: Resolves a physical offset/length into a `ChunkNode`. Parses the footer to determine if the chunk has children. # Arguments `offset`: Absolute byte offset in the file. `length`: Total length of the chunk including metadata.
(&self, offset: u64, length: u64)
| 785 | /// * `offset`: Absolute byte offset in the file. |
| 786 | /// * `length`: Total length of the chunk including metadata. |
| 787 | fn get_chunk(&self, offset: u64, length: u64) -> Result<ChunkNode<'_>> { |
| 788 | if offset + length > self.file_size { |
| 789 | return Err(ParcodeError::Format(format!( |
| 790 | "Chunk out of bounds: {} + {}", |
| 791 | offset, length |
| 792 | ))); |
| 793 | } |
| 794 | let chunk_end = usize::try_from(offset + length) |
| 795 | .map_err(|_| ParcodeError::Format("Chunk end exceeds address space".into()))?; |
| 796 | |
| 797 | let meta_byte = self |
| 798 | .source |
| 799 | .get(chunk_end - 1) |
| 800 | .ok_or_else(|| ParcodeError::Format("Failed to read chunk meta byte".into()))?; |
| 801 | let meta = MetaByte::from_byte(*meta_byte); |
| 802 | |
| 803 | let mut child_count = 0; |
| 804 | let mut payload_end = chunk_end - 1; |
| 805 | |
| 806 | if meta.is_chunkable() { |
| 807 | if length < 5 { |
| 808 | return Err(ParcodeError::Format("Chunk too small for metadata".into())); |
| 809 | } |
| 810 | |
| 811 | let count_start = chunk_end - 5; |
| 812 | let count_bytes = self |
| 813 | .source |
| 814 | .get(count_start..count_start + 4) |
| 815 | .ok_or_else(|| ParcodeError::Format("Failed to read child count".into()))?; |
| 816 | child_count = Self::read_u32(count_bytes)?; |
| 817 | |
| 818 | let footer_size = child_count as usize * ChildRef::SIZE; |
| 819 | let total_meta_size = 1 + 4 + footer_size; |
| 820 | |
| 821 | if length < total_meta_size as u64 { |
| 822 | return Err(ParcodeError::Format("Invalid footer size".into())); |
| 823 | } |
| 824 | payload_end = chunk_end - total_meta_size; |
| 825 | } |
| 826 | |
| 827 | Ok(ChunkNode { |
| 828 | reader: self, |
| 829 | offset, |
| 830 | length, |
| 831 | meta, |
| 832 | child_count, |
| 833 | payload_end_offset: offset + (payload_end as u64 - offset), |
| 834 | }) |
| 835 | } |
| 836 | } |
| 837 | |
| 838 | // --- CHUNK NODE API --- |
no test coverage detected