libarchive_oxide-core 0.1.0

Frozen trait algebra and sans-IO core: Transform/Filter/Format/EntryReader/EntryWriter. no_std + alloc.
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
// SPDX-FileCopyrightText: 2026 libarchive_oxide contributors
//
// SPDX-License-Identifier: MIT OR Apache-2.0

//! cpio format (SVR4 "newc"/"crc" and POSIX "odc").
//!
//! **Orthogonality proof, realized**: adding a new format is just adding a type that implements
//! the same [`EntryReader`], with no change to the existing traits or the tar implementation.
//!
//! Supports the two ASCII header variants used in practice: `newc`/`crc` (SVR4, 8-hex-digit
//! fields, 4-byte-aligned) and `odc` (POSIX, 6/11-octal-digit fields, unaligned). The legacy
//! binary format is out of P4 scope. The archive ends at the `TRAILER!!!` entry.

use alloc::borrow::Cow;

use crate::error::{Error, Result};
use crate::format::{
    ArchiveFormat, Detection, Entry, EntryDataSink, EntryReader, EntrySink, EntryWriter, SliceData,
};
use crate::io::Sink;
use crate::meta::{EntryKind, EntryMeta, Timestamp};

const NEWC_MAGIC: &[u8] = b"070701";
const NEWC_CRC_MAGIC: &[u8] = b"070702";
const ODC_MAGIC: &[u8] = b"070707";
const NEWC_HEADER: usize = 110;
const ODC_HEADER: usize = 76;
const TRAILER: &[u8] = b"TRAILER!!!";

/// Legacy binary format magic (both host byte orders).
const BIN_MAGIC_LE: [u8; 2] = [0xc7, 0x71];
const BIN_MAGIC_BE: [u8; 2] = [0x71, 0xc7];

/// The cpio format detection anchor (zero-sized type).
#[derive(Debug, Clone, Copy, Default)]
pub struct Cpio;

impl ArchiveFormat for Cpio {
    const NAME: &'static str = "cpio";

    fn sniff(prefix: &[u8]) -> Detection {
        if prefix.len() < 2 {
            return Detection::NeedMore;
        }
        let head2 = [prefix[0], prefix[1]];
        if head2 == BIN_MAGIC_LE || head2 == BIN_MAGIC_BE {
            return Detection::Match;
        }
        if prefix.len() < 6 {
            return Detection::NeedMore;
        }
        let head6 = &prefix[..6];
        if head6 == NEWC_MAGIC || head6 == NEWC_CRC_MAGIC || head6 == ODC_MAGIC {
            Detection::Match
        } else {
            Detection::NoMatch
        }
    }
}

/// Which ASCII header layout an entry uses.
#[derive(Clone, Copy)]
enum Variant {
    /// SVR4: 8-hex-digit fields, name and data padded to 4 bytes.
    Newc,
    /// POSIX: 6/11-octal-digit fields, no padding.
    Odc,
}

/// The parsed, format-independent view of a cpio header.
struct Header {
    mode: u64,
    uid: u64,
    gid: u64,
    mtime: u64,
    filesize: usize,
    name_start: usize,
    name_len: usize, // excludes the trailing NUL
    data_start: usize,
    next_pos: usize,
}

/// cpio streaming reader (over an in-memory slice).
#[derive(Debug)]
pub struct CpioReader<'a> {
    data: &'a [u8],
    pos: usize,
    payload: SliceData<'a>,
    ended: bool,
}

impl<'a> CpioReader<'a> {
    /// Builds a reader from the entire post-filter archive byte slice.
    #[must_use]
    pub fn new(data: &'a [u8]) -> Self {
        Self {
            data,
            pos: 0,
            payload: SliceData::default(),
            ended: false,
        }
    }

    /// Parses the header at `self.pos`, returning the format-independent [`Header`].
    fn parse_header(data: &[u8], pos: usize) -> Result<Header> {
        let magic = data
            .get(pos..pos + 6)
            .ok_or(Error::Malformed("cpio: truncated header"))?;
        let variant = match magic {
            NEWC_MAGIC | NEWC_CRC_MAGIC => Variant::Newc,
            ODC_MAGIC => Variant::Odc,
            _ => return Err(Error::Malformed("cpio: bad magic")),
        };

        match variant {
            Variant::Newc => {
                let f = |i: usize| newc_field(data, pos, i);
                let mode = f(1)?;
                let uid = f(2)?;
                let gid = f(3)?;
                let mtime = f(5)?;
                let filesize = usize_of(f(6)?)?;
                let name_len = usize_of(f(11)?)?;
                let name_start = pos + NEWC_HEADER;
                let data_start = round4(add(name_start, name_len)?)?;
                let next_pos = add(data_start, round4(filesize)?)?;
                Ok(Header {
                    mode,
                    uid,
                    gid,
                    mtime,
                    filesize,
                    name_start,
                    name_len: name_len.saturating_sub(1),
                    data_start,
                    next_pos,
                })
            },
            Variant::Odc => {
                let mode = odc_field(data, pos, 18, 6)?;
                let uid = odc_field(data, pos, 24, 6)?;
                let gid = odc_field(data, pos, 30, 6)?;
                let mtime = odc_field(data, pos, 48, 11)?;
                let name_len = usize_of(odc_field(data, pos, 59, 6)?)?;
                let filesize = usize_of(odc_field(data, pos, 65, 11)?)?;
                let name_start = pos + ODC_HEADER;
                let data_start = add(name_start, name_len)?;
                let next_pos = add(data_start, filesize)?;
                Ok(Header {
                    mode,
                    uid,
                    gid,
                    mtime,
                    filesize,
                    name_start,
                    name_len: name_len.saturating_sub(1),
                    data_start,
                    next_pos,
                })
            },
        }
    }
}

impl<'a> EntryReader for CpioReader<'a> {
    type Data = SliceData<'a>;

    fn next_entry(&mut self) -> Result<Option<Entry<'_, SliceData<'a>>>> {
        if self.ended {
            return Ok(None);
        }
        let data = self.data;

        let header = Self::parse_header(data, self.pos)?;
        let name = data
            .get(header.name_start..)
            .and_then(|s| s.get(..header.name_len))
            .ok_or(Error::Malformed("cpio: truncated name"))?;

        if name == TRAILER {
            self.ended = true;
            return Ok(None);
        }

        let kind = kind_from_mode(header.mode);
        let body = data
            .get(header.data_start..)
            .and_then(|s| s.get(..header.filesize))
            .ok_or(Error::Malformed("cpio: truncated data"))?;
        let link_target = matches!(kind, EntryKind::Symlink).then(|| Cow::Borrowed(body));

        let meta = EntryMeta {
            kind,
            path: Cow::Borrowed(name),
            mode: u32::try_from(header.mode & 0o7777).unwrap_or(0),
            uid: header.uid,
            gid: header.gid,
            mtime: Some(Timestamp {
                secs: i64::try_from(header.mtime).unwrap_or(i64::MAX),
                nanos: 0,
            }),
            size: header.filesize as u64,
            link_target,
            pax: crate::meta::PaxMap::new(),
        };

        self.payload = SliceData::new(data, header.data_start, header.filesize);
        self.pos = header.next_pos;
        Ok(Some(Entry::new(meta, &mut self.payload)))
    }
}

/// cpio "newc" streaming writer — the dual of [`CpioReader`]. Emits SVR4 headers with 4-byte
/// alignment and closes with the `TRAILER!!!` marker.
#[derive(Debug)]
pub struct CpioWriter<W: Sink> {
    sink: W,
    ino: u32,
    remaining: u64,
    data_pad: usize,
    open: bool,
}

impl<W: Sink> CpioWriter<W> {
    /// Builds a writer over a byte sink.
    pub fn new(sink: W) -> Self {
        Self {
            sink,
            ino: 0,
            remaining: 0,
            data_pad: 0,
            open: false,
        }
    }

    /// Consumes the writer and returns the underlying sink.
    pub fn into_inner(self) -> W {
        self.sink
    }

    /// Emits a newc header, the name, and its 4-byte padding.
    fn emit_header(&mut self, name: &[u8], f: &NewcFields) -> Result<()> {
        let filesize = u32::try_from(f.filesize)
            .map_err(|_| Error::Unsupported("cpio: file too large for newc"))?;
        let namesize = u32::try_from(name.len() + 1)
            .map_err(|_| Error::Unsupported("cpio: name too long for newc"))?;
        let fields = [
            self.ino,
            lo32(f.mode),
            lo32(f.uid),
            lo32(f.gid),
            f.nlink,
            lo32(f.mtime),
            filesize,
            0,
            0,
            0,
            0,
            namesize,
            0,
        ];
        let mut h = [0u8; NEWC_HEADER];
        h[..6].copy_from_slice(NEWC_MAGIC);
        for (i, &val) in fields.iter().enumerate() {
            let off = 6 + i * 8;
            put_hex8(&mut h[off..off + 8], val);
        }
        self.sink.write_all(&h)?;
        self.sink.write_all(name)?;
        self.sink.write_all(&[0u8])?;
        let after = NEWC_HEADER + name.len() + 1;
        write_zeros(&mut self.sink, round4(after)? - after)?;
        self.ino = self.ino.wrapping_add(1);
        Ok(())
    }
}

impl<W: Sink> EntryWriter for CpioWriter<W> {
    type Sink = Self;

    fn start_entry(&mut self, meta: &EntryMeta<'_>) -> Result<EntrySink<'_, Self>> {
        if self.open {
            return Err(Error::InvalidState("cpio: previous entry not closed"));
        }
        let mode = mode_bits(meta.kind) | u64::from(meta.mode & 0o7777);
        let nlink = if matches!(meta.kind, EntryKind::Dir) {
            2
        } else {
            1
        };
        let mtime = meta
            .mtime
            .map_or(0, |t| u64::try_from(t.secs.max(0)).unwrap_or(0));
        self.emit_header(
            &meta.path,
            &NewcFields {
                mode,
                uid: meta.uid,
                gid: meta.gid,
                mtime,
                filesize: meta.size,
                nlink,
            },
        )?;
        self.remaining = meta.size;
        let size = usize_of(meta.size)?;
        self.data_pad = round4(size)? - size;
        self.open = true;
        Ok(EntrySink::new(self))
    }

    fn finish(&mut self) -> Result<()> {
        if self.open {
            return Err(Error::InvalidState("cpio: entry open at finish"));
        }
        self.emit_header(
            TRAILER,
            &NewcFields {
                mode: 0,
                uid: 0,
                gid: 0,
                mtime: 0,
                filesize: 0,
                nlink: 1,
            },
        )
    }
}

/// The numeric fields of a newc header (grouped to keep `emit_header` small).
struct NewcFields {
    mode: u64,
    uid: u64,
    gid: u64,
    mtime: u64,
    filesize: u64,
    nlink: u32,
}

impl<W: Sink> EntryDataSink for CpioWriter<W> {
    fn write_chunk(&mut self, data: &[u8]) -> Result<()> {
        if data.len() as u64 > self.remaining {
            return Err(Error::InvalidState("cpio: payload exceeds declared size"));
        }
        self.sink.write_all(data)?;
        self.remaining -= data.len() as u64;
        Ok(())
    }

    fn close(&mut self) -> Result<()> {
        if self.remaining != 0 {
            return Err(Error::InvalidState(
                "cpio: payload shorter than declared size",
            ));
        }
        write_zeros(&mut self.sink, self.data_pad)?;
        self.data_pad = 0;
        self.open = false;
        Ok(())
    }
}

/// Maps a typed kind to its `S_IFMT` bits for writing.
fn mode_bits(kind: EntryKind) -> u64 {
    match kind {
        EntryKind::Dir => S_IFDIR,
        EntryKind::Symlink => S_IFLNK,
        EntryKind::Char => S_IFCHR,
        EntryKind::Block => S_IFBLK,
        EntryKind::Fifo => S_IFIFO,
        EntryKind::Socket => S_IFSOCK,
        _ => S_IFREG,
    }
}

/// The low 32 bits of `v` (newc fields are 32-bit).
fn lo32(v: u64) -> u32 {
    u32::try_from(v & 0xFFFF_FFFF).unwrap_or(0)
}

/// Writes `val` as 8 uppercase hex digits into `field`.
fn put_hex8(field: &mut [u8], val: u32) {
    let mut v = val;
    for slot in field.iter_mut().rev() {
        let d = u8::try_from(v & 0xf).unwrap_or(0);
        *slot = if d < 10 { b'0' + d } else { b'A' + (d - 10) };
        v >>= 4;
    }
}

/// Writes `count` zero bytes to the sink.
fn write_zeros<W: Sink>(sink: &mut W, count: usize) -> Result<()> {
    const ZEROS: [u8; 512] = [0; 512];
    let mut left = count;
    while left > 0 {
        let n = left.min(512);
        sink.write_all(&ZEROS[..n])?;
        left -= n;
    }
    Ok(())
}

/// Reads the `i`-th 8-hex-digit field of a newc header (0-based, after the 6-byte magic).
fn newc_field(data: &[u8], pos: usize, i: usize) -> Result<u64> {
    let start = pos + 6 + i * 8;
    let field = data
        .get(start..start + 8)
        .ok_or(Error::Malformed("cpio: truncated newc field"))?;
    parse_radix(field, 16)
}

/// Reads an octal field of the given width at `pos + off` in an odc header.
fn odc_field(data: &[u8], pos: usize, off: usize, width: usize) -> Result<u64> {
    let start = pos + off;
    let field = data
        .get(start..start + width)
        .ok_or(Error::Malformed("cpio: truncated odc field"))?;
    parse_radix(field, 8)
}

/// Parses ASCII digits in base 8 or 16 into a `u64`. Spaces and NULs are ignored.
fn parse_radix(field: &[u8], radix: u32) -> Result<u64> {
    let mut val: u64 = 0;
    for &b in field {
        if b == b' ' || b == 0 {
            continue;
        }
        let digit = u64::from(
            (b as char)
                .to_digit(radix)
                .ok_or(Error::Malformed("cpio: invalid digit"))?,
        );
        val = val
            .checked_mul(u64::from(radix))
            .and_then(|v| v.checked_add(digit))
            .ok_or(Error::Malformed("cpio: numeric overflow"))?;
    }
    Ok(val)
}

// File-type bits (`S_IFMT`) of a UNIX mode.
const S_IFMT: u64 = 0o170_000;
const S_IFREG: u64 = 0o100_000;
const S_IFDIR: u64 = 0o040_000;
const S_IFCHR: u64 = 0o020_000;
const S_IFBLK: u64 = 0o060_000;
const S_IFIFO: u64 = 0o010_000;
const S_IFLNK: u64 = 0o120_000;
const S_IFSOCK: u64 = 0o140_000;

/// Maps the `S_IFMT` bits of `mode` to a typed [`EntryKind`].
fn kind_from_mode(mode: u64) -> EntryKind {
    match mode & S_IFMT {
        S_IFDIR => EntryKind::Dir,
        S_IFLNK => EntryKind::Symlink,
        S_IFCHR => EntryKind::Char,
        S_IFBLK => EntryKind::Block,
        S_IFIFO => EntryKind::Fifo,
        S_IFSOCK => EntryKind::Socket,
        _ => EntryKind::File,
    }
}

/// Rounds up to the next 4-byte boundary (newc alignment). Errors on overflow (32-bit targets).
fn round4(n: usize) -> Result<usize> {
    Ok(add(n, 3)? & !3)
}

/// Checked `usize` addition, mapped to a malformed-archive error on overflow.
fn add(a: usize, b: usize) -> Result<usize> {
    a.checked_add(b)
        .ok_or(Error::Malformed("cpio: size overflow"))
}

/// `u64` to `usize`, rejecting oversized values on 32-bit targets.
fn usize_of(v: u64) -> Result<usize> {
    usize::try_from(v).map_err(|_| Error::LimitExceeded("cpio: size exceeds usize"))
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
mod tests {
    use super::*;

    #[test]
    fn radix_parsing() {
        assert_eq!(parse_radix(b"000000ff", 16).unwrap(), 255);
        assert_eq!(parse_radix(b"000644", 8).unwrap(), 0o644);
    }

    #[test]
    fn round4_alignment() {
        assert_eq!(round4(0).unwrap(), 0);
        assert_eq!(round4(1).unwrap(), 4);
        assert_eq!(round4(110).unwrap(), 112);
        assert!(round4(usize::MAX).is_err());
    }
}