git-zero-core 0.1.0

Allocation-free no_std Git objects, repositories, packs, zlib and delta primitives
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
//! Stream structural links into host-owned indexes without retaining a graph.
use crate::{Error, ObjectFormat, ObjectId, ObjectInfo, ObjectKind, Result, names::cmp_tree_names};

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct LinkLimits {
    pub max_body_bytes: u64,
    pub max_links: u64,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum LinkRelation {
    RootTree,
    Parent,
    TagTarget,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum LinkStatus<'name> {
    NeedInput,
    Link {
        id: ObjectId,
        kind: ObjectKind,
        relation: LinkRelation,
    },
    TreeEntry {
        id: ObjectId,
        mode: u32,
        name: &'name [u8],
        kind: Option<ObjectKind>,
        canonical_mode: bool,
        zero_padded: bool,
    },
    Finished,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct LinkProgress<'name> {
    pub consumed: usize,
    pub status: LinkStatus<'name>,
}

#[derive(Clone, Copy)]
enum Phase {
    Blob,
    CommitTree,
    Parents,
    Headers,
    TagObject,
    TagType,
    TagName,
    TagHeaders,
    Message,
    Mode,
    Name,
    Id,
    Done,
}

struct Names<'a> {
    previous: &'a mut [u8],
    current: &'a mut [u8],
}
struct State {
    format: ObjectFormat,
    info: ObjectInfo,
    limits: LinkLimits,
    phase: Phase,
    position: u64,
    links: u64,
    header: [u8; 80],
    header_len: usize,
    header_overflow: bool,
    target: ObjectId,
    mode: u32,
    mode_digits: u32,
    padded_mode: bool,
    previous_mode: u32,
    previous_len: usize,
    current_len: usize,
    has_previous: bool,
    id: [u8; 32],
    id_len: usize,
    failure: Option<Error>,
}

/// Scan one canonical object body across arbitrary chunks. Blobs/messages are
/// skipped without retaining them; commit parents and tag targets are emitted
/// with their expected kinds. Tree names borrow caller scratch until resumption.
/// Scratch is divided equally into current/previous names; other kinds need none.
///
/// This scans syntax and adjacent tree order, not complete graph validity.
/// Authenticate the body separately with `ObjectDigest`. The host MUST check
/// target existence/kinds and tree-name uniqueness (including file/directory
/// aliases separated in Git order), persist parent edges and enforce ref policy.
/// Gitlinks name external repositories and emit `kind: None`.
///
/// Historical noncanonical tree modes with recognized type bits, including
/// zero padding and old regular-file permissions, are preserved and flagged.
/// They are not silently rewritten or rejected as graph errors.
pub struct LinkDecoder<'scratch> {
    state: State,
    names: Names<'scratch>,
}

impl<'scratch> LinkDecoder<'scratch> {
    pub fn new(
        info: ObjectInfo,
        format: ObjectFormat,
        names: &'scratch mut [u8],
        limits: LinkLimits,
    ) -> Result<Self> {
        if info.size > limits.max_body_bytes {
            return Err(Error::LimitExceeded);
        }
        let midpoint = names.len() / 2;
        let (previous, current) = names[..midpoint * 2].split_at_mut(midpoint);
        let phase = match info.kind {
            ObjectKind::Blob => Phase::Blob,
            ObjectKind::Tree => Phase::Mode,
            ObjectKind::Commit => Phase::CommitTree,
            ObjectKind::Tag => Phase::TagObject,
        };
        Ok(Self {
            state: State {
                format,
                info,
                limits,
                phase,
                position: 0,
                links: 0,
                header: [0; 80],
                header_len: 0,
                header_overflow: false,
                target: format.zero_id(),
                mode: 0,
                mode_digits: 0,
                padded_mode: false,
                previous_mode: 0,
                previous_len: 0,
                current_len: 0,
                has_previous: false,
                id: [0; 32],
                id_len: 0,
                failure: None,
            },
            names: Names { previous, current },
        })
    }

    pub fn position(&self) -> u64 {
        self.state.position
    }

    /// Events may consume a prefix. Resubmit the suffix with the same EOF flag.
    /// Every error is terminal; all emitted metadata remains provisional.
    pub fn update<'name>(
        &'name mut self,
        input: &[u8],
        end_of_input: bool,
    ) -> Result<LinkProgress<'name>> {
        let result = self.state.advance(input, end_of_input, &mut self.names);
        if let Err(error) = result {
            self.state.failure = Some(error);
        }
        result
    }

    pub fn finish(&self) -> Result<()> {
        if let Some(error) = self.state.failure {
            return Err(error);
        }
        if matches!(self.state.phase, Phase::Done) {
            Ok(())
        } else {
            Err(Error::InvalidObject)
        }
    }
}

impl State {
    fn advance<'name>(
        &mut self,
        input: &[u8],
        eof: bool,
        names: &'name mut Names<'_>,
    ) -> Result<LinkProgress<'name>> {
        if let Some(error) = self.failure {
            return Err(error);
        }
        let mut consumed = 0;
        loop {
            if self.position == self.info.size {
                if consumed != input.len() {
                    return Err(Error::InvalidObject);
                }
                let status = self.end(eof)?;
                return Ok(LinkProgress { consumed, status });
            }
            if consumed == input.len() {
                if eof {
                    return Err(Error::InvalidObject);
                }
                return Ok(LinkProgress {
                    consumed,
                    status: LinkStatus::NeedInput,
                });
            }
            if matches!(self.phase, Phase::Blob | Phase::Message) {
                let count =
                    (self.info.size - self.position).min((input.len() - consumed) as u64) as usize;
                self.position += count as u64;
                consumed += count;
                continue;
            }
            let byte = input[consumed];
            consumed += 1;
            self.position += 1;
            match self.phase {
                Phase::Mode => self.mode_byte(byte)?,
                Phase::Name => self.name_byte(byte, names)?,
                Phase::Id => {
                    if let Some(id) = self.id_byte(byte, names)? {
                        return Ok(LinkProgress {
                            consumed,
                            status: LinkStatus::TreeEntry {
                                id,
                                mode: self.previous_mode,
                                name: &names.previous[..self.previous_len],
                                kind: tree_kind(self.previous_mode)?,
                                canonical_mode: !self.padded_mode
                                    && canonical_mode(self.previous_mode),
                                zero_padded: self.padded_mode,
                            },
                        });
                    }
                }
                _ => {
                    if let Some(status) = self.header_byte(byte)? {
                        return Ok(LinkProgress { consumed, status });
                    }
                }
            }
        }
    }

    fn name_byte(&mut self, byte: u8, names: &mut Names<'_>) -> Result<()> {
        if byte == 0 {
            self.phase = Phase::Id;
            self.id_len = 0;
            return Ok(());
        }
        *names
            .current
            .get_mut(self.current_len)
            .ok_or(Error::BufferTooSmall)? = byte;
        self.current_len += 1;
        Ok(())
    }

    fn id_byte(&mut self, byte: u8, names: &mut Names<'_>) -> Result<Option<ObjectId>> {
        self.id[self.id_len] = byte;
        self.id_len += 1;
        if self.id_len != self.format.id_length() {
            return Ok(None);
        }
        let id = ObjectId::from_bytes(self.format, &self.id[..self.id_len])?;
        if id.is_zero() {
            return Err(Error::InvalidObject);
        }
        self.tree_entry(names)?;
        Ok(Some(id))
    }

    fn header_byte(&mut self, byte: u8) -> Result<Option<LinkStatus<'static>>> {
        if byte == b'\n' {
            let status = self.line()?;
            self.header_len = 0;
            self.header_overflow = false;
            if status.is_some() {
                self.charge()?;
            }
            return Ok(status);
        }
        if byte == 0 {
            return Err(Error::InvalidObject);
        }
        if self.header_len == self.header.len() {
            self.header_overflow = true;
        } else {
            self.header[self.header_len] = byte;
            self.header_len += 1;
        }
        Ok(None)
    }

    fn end(&mut self, eof: bool) -> Result<LinkStatus<'static>> {
        if matches!(self.phase, Phase::Done) {
            return Ok(LinkStatus::Finished);
        }
        let complete = match self.phase {
            Phase::Blob | Phase::Message => true,
            Phase::Mode => self.mode_digits == 0,
            Phase::Headers | Phase::TagHeaders => self.header_len == 0 && !self.header_overflow,
            _ => false,
        };
        if !complete {
            return Err(Error::InvalidObject);
        }
        if !eof {
            return Ok(LinkStatus::NeedInput);
        }
        self.phase = Phase::Done;
        Ok(LinkStatus::Finished)
    }

    fn charge(&mut self) -> Result<()> {
        if self.links == self.limits.max_links {
            return Err(Error::LimitExceeded);
        }
        self.links += 1;
        Ok(())
    }

    fn mode_byte(&mut self, byte: u8) -> Result<()> {
        match byte {
            b'0'..=b'7' => {
                if self.mode_digits == 0 {
                    self.padded_mode = byte == b'0';
                }
                self.mode = self
                    .mode
                    .checked_mul(8)
                    .and_then(|mode| mode.checked_add(u32::from(byte - b'0')))
                    .ok_or(Error::InvalidObject)?;
                self.mode_digits = self.mode_digits.saturating_add(1);
            }
            b' ' if self.mode_digits != 0 => {
                tree_kind(self.mode)?;
                self.phase = Phase::Name;
            }
            _ => return Err(Error::InvalidObject),
        }
        Ok(())
    }

    fn tree_entry(&mut self, names: &mut Names<'_>) -> Result<()> {
        let name = &names.current[..self.current_len];
        if name.is_empty()
            || name == b"."
            || name == b".."
            || name.eq_ignore_ascii_case(b".git")
            || name.contains(&b'/')
        {
            return Err(Error::InvalidFileName);
        }
        if self.has_previous
            && cmp_tree_names(
                &names.previous[..self.previous_len],
                self.previous_mode & 0o170000 == 0o040000,
                name,
                self.mode & 0o170000 == 0o040000,
            ) != core::cmp::Ordering::Less
        {
            return Err(Error::UnsortedFiles);
        }
        self.charge()?;
        core::mem::swap(&mut names.previous, &mut names.current);
        self.previous_len = self.current_len;
        self.previous_mode = self.mode;
        self.has_previous = true;
        self.current_len = 0;
        self.mode = 0;
        self.mode_digits = 0;
        self.phase = Phase::Mode;
        Ok(())
    }

    fn line(&mut self) -> Result<Option<LinkStatus<'static>>> {
        let line = &self.header[..self.header_len];
        let edge = match self.phase {
            Phase::CommitTree => {
                let id = link(line, b"tree ", self.format, self.header_overflow)?;
                self.phase = Phase::Parents;
                Some(LinkStatus::Link {
                    id,
                    kind: ObjectKind::Tree,
                    relation: LinkRelation::RootTree,
                })
            }
            Phase::Parents if line.starts_with(b"parent ") => Some(LinkStatus::Link {
                id: link(line, b"parent ", self.format, self.header_overflow)?,
                kind: ObjectKind::Commit,
                relation: LinkRelation::Parent,
            }),
            Phase::TagObject => {
                self.target = link(line, b"object ", self.format, self.header_overflow)?;
                self.phase = Phase::TagType;
                None
            }
            Phase::TagType => {
                if self.header_overflow {
                    return Err(Error::InvalidObject);
                }
                let kind = ObjectKind::from_tag_header(line)?;
                self.phase = Phase::TagName;
                Some(LinkStatus::Link {
                    id: self.target,
                    kind,
                    relation: LinkRelation::TagTarget,
                })
            }
            Phase::TagName => {
                if !line.starts_with(b"tag ") {
                    return Err(Error::InvalidObject);
                }
                self.phase = Phase::TagHeaders;
                None
            }
            _ => {
                self.phase = if line.is_empty() {
                    Phase::Message
                } else if matches!(self.phase, Phase::TagHeaders) {
                    Phase::TagHeaders
                } else {
                    Phase::Headers
                };
                None
            }
        };
        Ok(edge)
    }
}

fn link(line: &[u8], prefix: &[u8], format: ObjectFormat, overflow: bool) -> Result<ObjectId> {
    if overflow {
        return Err(Error::InvalidObject);
    }
    let hex = line
        .strip_prefix(prefix)
        .filter(|hex| hex.len() == format.hex_length())
        .ok_or(Error::InvalidObject)?;
    ObjectId::from_hex(hex).map_err(|_| Error::InvalidObject)
}

fn tree_kind(mode: u32) -> Result<Option<ObjectKind>> {
    Ok(match mode & 0o170000 {
        0o040000 => Some(ObjectKind::Tree),
        0o100000 | 0o120000 => Some(ObjectKind::Blob),
        0o160000 => None,
        _ => return Err(Error::InvalidObject),
    })
}

fn canonical_mode(mode: u32) -> bool {
    matches!(mode, 0o040000 | 0o100644 | 0o100755 | 0o120000 | 0o160000)
}