Read the next section, decompressing its payload. Returns `None` if all sections have been read. The returned [`ReadSection`] contains the decompressed payload and metadata. The compressed bytes of hashed sections are fed through the blake3 hasher for later verification. # Errors - [`FormatError::InvalidSectionType`] if the section type is unknown. - [`FormatError::Decompress`] if zstd decompr
(&mut self)
| 111 | /// } |
| 112 | /// ``` |
| 113 | pub fn next_section(&mut self) -> FormatResult<Option<ReadSection>> { |
| 114 | // Ensure we have a peeked header (reads from source if needed) |
| 115 | if self.peeked.is_none() && self.peek_section_type()?.is_none() { |
| 116 | return Ok(None); |
| 117 | } |
| 118 | |
| 119 | let peeked = self.peeked.take().unwrap(); |
| 120 | |
| 121 | let bounded_schema = self.file_header.bounded_sections(); |
| 122 | let limit = super::super::semantic::wire_limit(); |
| 123 | if bounded_schema && peeked.compressed_len as usize > limit { |
| 124 | return Err(FormatError::InvalidChangeMetadata { |
| 125 | reason: "compressed schema-4 section exceeds limit".into(), |
| 126 | }); |
| 127 | } |
| 128 | |
| 129 | // Read the compressed payload |
| 130 | let mut compressed = vec![0u8; peeked.compressed_len as usize]; |
| 131 | self.reader.read_exact(&mut compressed)?; |
| 132 | |
| 133 | // Feed through hasher if this is a hashed section |
| 134 | if peeked.section_type.is_hashed() { |
| 135 | self.hasher.update(&peeked.header_bytes); |
| 136 | self.hasher.update(&compressed); |
| 137 | } |
| 138 | |
| 139 | // Decompress |
| 140 | let decompressed = if peeked.compressed_len == 0 { |
| 141 | Vec::new() |
| 142 | } else if bounded_schema { |
| 143 | let mut decoded = Vec::new(); |
| 144 | zstd::stream::read::Decoder::new(compressed.as_slice())? |
| 145 | .take(limit as u64 + 1) |
| 146 | .read_to_end(&mut decoded)?; |
| 147 | if decoded.len() > limit { |
| 148 | return Err(FormatError::InvalidChangeMetadata { |
| 149 | reason: "decompressed schema-4 section exceeds limit".into(), |
| 150 | }); |
| 151 | } |
| 152 | decoded |
| 153 | } else { |
| 154 | zstd::decode_all(&compressed[..]).map_err(|e| FormatError::Decompress(e.to_string()))? |
| 155 | }; |
| 156 | |
| 157 | // Build content chunk info if applicable |
| 158 | let content_chunk_info = peeked.chunk_header.map(|ch| ContentChunkInfo { |
| 159 | chunk_index: ch.chunk_index, |
| 160 | chunk_hash: ch.chunk_hash, |
| 161 | uncompressed_len: ch.uncompressed_len, |
| 162 | }); |
| 163 | |
| 164 | // Update stats |
| 165 | let header_size = peeked.header_bytes.len() as u64; |
| 166 | self.stats.sections_read += 1; |
| 167 | self.stats.total_compressed += peeked.compressed_len as u64; |
| 168 | self.stats.total_decompressed += decompressed.len() as u64; |
| 169 | self.stats.total_bytes_read += header_size + peeked.compressed_len as u64; |
| 170 | self.remaining_sections = self.remaining_sections.saturating_sub(1); |