MCPcopy Create free account
hub / github.com/atomicdotdev/atomic / decode

Method decode

atomic-core/src/pristine/ref_mapping.rs:202–294  ·  view source on GitHub ↗

Decode with strict bounds checking; unknown versions fail closed, the record must end exactly at the layout end (no trailing bytes), and a v1 row decodes with the v2 export state unbound.

(bytes: &[u8])

Source from the content-addressed store, hash-verified

200 /// record must end exactly at the layout end (no trailing bytes), and a
201 /// v1 row decodes with the v2 export state unbound.
202 pub fn decode(bytes: &[u8]) -> PristineResult<Self> {
203 let &version = bytes.first().ok_or(decode_error())?;
204 // CB-10A review R7: version 0 never existed as a written layout — a
205 // zero first byte is corruption (or a zeroed region), not a
206 // decodable record.
207 if version == 0 || version > REF_MAPPING_VERSION {
208 return Err(PristineError::Serialization {
209 message: format!(
210 "ref mapping record version {version} is not supported (this build supports {REF_MAPPING_VERSION})"
211 ),
212 });
213 }
214 let mut cursor = 1;
215 let mut take = |count: usize| -> Result<&[u8], PristineError> {
216 if cursor + count > bytes.len() {
217 return Err(decode_error());
218 }
219 let slice = &bytes[cursor..cursor + count];
220 cursor += count;
221 Ok(slice)
222 };
223 let view_id = u64::from_le_bytes(take(8)?.try_into().expect("fixed-size u64"));
224 let view_name = take_string(&mut take)?;
225 let scope = take(1)?[0];
226 if ViewScope::from_u8(scope).is_none() {
227 return Err(decode_error());
228 }
229 let local_ref = take_option_ref(&mut take)?;
230 let remote = match take(1)?[0] {
231 0 => None,
232 1 => {
233 let remote_name = take_string(&mut take)?;
234 let remote_ref = take_string(&mut take)?;
235 Some((remote_name, remote_ref))
236 }
237 _ => return Err(decode_error()),
238 };
239 let last_observed_local = take_option_ref(&mut take)?;
240 let last_observed_remote = take_option_ref(&mut take)?;
241 let last_exported = take_option_ref(&mut take)?;
242 let last_observed_atomic = take_option_ref(&mut take)?;
243 let status = RefSyncStatus::from_u8(take(1)?[0]).ok_or(decode_error())?;
244 let last_exported_state = if version >= 2 {
245 take_option_ref(&mut take)?
246 } else {
247 None
248 };
249 if cursor != bytes.len() {
250 // Trailing bytes mean the record is not the encoding this build
251 // wrote: refuse rather than silently ignoring appended data.
252 return Err(decode_error());
253 }
254 // CB-10A review R7: the decoded strings are validated with the same
255 // rules the encoder enforces, so a corrupted-but-well-formed row is
256 // rejected at the boundary instead of flowing into reconciliation.
257 if view_name.is_empty() || view_name.bytes().any(|b| b < 0x20 || b == 0x7f) {
258 return Err(decode_error());
259 }

Callers

nothing calls this directly

Calls 10

decode_errorFunction · 0.85
take_stringFunction · 0.85
take_option_refFunction · 0.85
validate_ref_nameFunction · 0.85
validate_oidFunction · 0.85
validate_stateFunction · 0.85
is_noneMethod · 0.80
lenMethod · 0.45
is_emptyMethod · 0.45
bytesMethod · 0.45

Tested by

no test coverage detected