1use crate::{ArchiveFormat, Block};
2
3pub const NAME_RANGE: std::ops::Range<usize> = 0..100;
6
7pub const MODE_RANGE: std::ops::Range<usize> = 100..108;
10
11pub const UID_RANGE: std::ops::Range<usize> = 108..116;
14
15pub const GID_RANGE: std::ops::Range<usize> = 116..124;
18
19pub const SIZE_RANGE: std::ops::Range<usize> = 124..136;
22
23pub const MTIME_RANGE: std::ops::Range<usize> = 136..148;
26
27pub const CHECKSUM_RANGE: std::ops::Range<usize> = 148..156;
30
31pub const TYPEFLAG_OFFSET: usize = 156;
34
35pub const LINK_NAME_RANGE: std::ops::Range<usize> = 157..257;
38
39pub const IDENTITY_RANGE: std::ops::Range<usize> = 257..265;
44
45pub const UNAME_RANGE: std::ops::Range<usize> = 265..297;
47
48pub const GNAME_RANGE: std::ops::Range<usize> = 297..329;
50
51pub const DEVMAJOR_RANGE: std::ops::Range<usize> = 329..337;
53
54pub const DEVMINOR_RANGE: std::ops::Range<usize> = 337..345;
56
57pub const PREFIX_RANGE: std::ops::Range<usize> = 345..500;
61
62pub const USTAR_IDENTITY: &[u8; 8] = b"ustar\x0000";
67
68pub const GNU_IDENTITY: &[u8; 8] = b"ustar \0";
72
73pub(crate) const XSTAR_MAGIC: &[u8; 4] = b"tar\0";
75
76const MAX_CHECKSUM: u64 = (504 * 255) + (8 * 32);
82const _: () = assert!(MAX_CHECKSUM < 0o777777);
83
84#[inline(always)]
87pub(crate) fn checksum(block: &Block) -> u64 {
88 let block_sum = block.iter().map(|byte| u32::from(*byte)).sum::<u32>();
91 let checksum_sum = block[CHECKSUM_RANGE]
92 .iter()
93 .map(|byte| u32::from(*byte))
94 .sum::<u32>();
95 u64::from(block_sum - checksum_sum + CHECKSUM_RANGE.len() as u32 * u32::from(b' '))
96}
97
98#[inline(always)]
99pub(crate) fn encode_checksum(block: &mut Block) {
100 let value = checksum(block);
101
102 debug_assert!(value <= MAX_CHECKSUM);
112 let _ = encode_octal_with_suffix(&mut block[CHECKSUM_RANGE], value, b"\0 ");
113}
114
115pub(crate) fn encode_octal(field: &mut [u8], value: u64) -> bool {
116 encode_octal_with_suffix(field, value, b"\0")
117}
118
119fn encode_octal_with_suffix(field: &mut [u8], value: u64, suffix: &[u8]) -> bool {
120 let Some(width) = field.len().checked_sub(suffix.len()) else {
121 return false;
122 };
123 if width == 0 {
124 return false;
125 }
126 field[width..].copy_from_slice(suffix);
127 encode_octal_digits(&mut field[..width], value)
128}
129
130fn encode_octal_digits(field: &mut [u8], mut value: u64) -> bool {
131 for byte in field.iter_mut().rev() {
132 *byte = b'0' + (value & 0o7) as u8;
133 value >>= 3;
134 }
135 value == 0
136}
137
138pub(crate) fn parse_octal(bytes: &[u8]) -> Option<u64> {
143 let mut value = 0_u64;
144 let mut has_digits = false;
145 let mut terminated = false;
146 for byte in bytes {
147 match *byte {
148 b'0'..=b'7' if !terminated => {
149 value = value.checked_mul(8)?.checked_add(u64::from(*byte - b'0'))?;
150 has_digits = true;
151 }
152 0 | b' ' => terminated = true,
153 _ => return None,
154 }
155 }
156 (has_digits && terminated).then_some(value)
157}
158
159pub(crate) fn is_all_nul(bytes: &[u8]) -> bool {
160 bytes.iter().all(|byte| *byte == 0)
161}
162
163#[derive(Clone, Copy, Debug, Eq, PartialEq)]
165pub(crate) enum NumericFormat {
166 Octal,
168 Gnu,
170}
171
172impl From<ArchiveFormat> for NumericFormat {
173 fn from(format: ArchiveFormat) -> Self {
174 match format {
175 ArchiveFormat::Pax => Self::Octal,
176 ArchiveFormat::Gnu => Self::Gnu,
177 }
178 }
179}
180
181impl NumericFormat {
182 pub(crate) fn parse(self, bytes: &[u8]) -> Option<u64> {
184 match self {
185 Self::Octal => parse_octal(bytes),
186 Self::Gnu => parse_gnu_number(bytes),
187 }
188 }
189}
190
191fn parse_gnu_number(bytes: &[u8]) -> Option<u64> {
198 match bytes.first()? {
199 0x80 => bytes[1..].iter().try_fold(0_u64, |value, byte| {
200 value.checked_mul(256)?.checked_add(u64::from(*byte))
201 }),
202 0xff => None,
205 _ => parse_octal(bytes),
206 }
207}
208
209#[cfg(test)]
210mod tests {
211 use super::*;
212
213 #[test]
214 fn encodes_octal_values_that_fit_the_field() {
215 let mut field = [0xff; 4];
216 assert!(encode_octal(&mut field, 0o17));
217 assert_eq!(&field, b"017\0");
218 assert_eq!(parse_octal(&field), Some(0o17));
219
220 assert!(encode_octal(&mut field, 0o777));
221 assert_eq!(&field, b"777\0");
222 assert!(!encode_octal(&mut field, 0o1000));
223 assert!(!encode_octal(&mut [], 0));
224 assert!(!encode_octal(&mut [0], 0));
225 }
226
227 #[test]
228 fn parses_strict_octal_fields() {
229 for (field, expected) in [
230 (&b"000017 "[..], Some(0o17)),
232 (&b"0000000000000000000000000000017 "[..], Some(0o17)),
233 (&b"17\0"[..], Some(0o17)),
235 (&b"17 "[..], Some(0o17)),
237 (&b"17 \0"[..], Some(0o17)),
239 (&b""[..], None),
241 (&b"\0"[..], None),
243 (&b" "[..], None),
244 (&b"17"[..], None),
246 (&b"18\0"[..], None),
248 (&b"1\0\x32"[..], None),
250 (&b"1\0\x31"[..], None),
252 (&b"1 1"[..], None),
253 (&[0x80, 0][..], None),
255 (&b"77777777777777777777777 "[..], None),
257 (&b"77777777777777777777777\0"[..], None),
258 ] {
259 assert_eq!(parse_octal(field), expected, "{field:?}");
260 }
261 }
262
263 #[test]
264 fn checksums_known_blocks() {
265 let zero_block = [0; crate::BLOCK_SIZE];
266 let mut x_typeflag_block = zero_block;
267 x_typeflag_block[TYPEFLAG_OFFSET] = b'x';
268 x_typeflag_block[CHECKSUM_RANGE].fill(0xff);
269 let maximum_block = [0xff; crate::BLOCK_SIZE];
270
271 for (name, mut block, expected) in [
272 ("zero block", zero_block, b"000400\0 "),
273 (
274 "x typeflag with junk checksum bytes",
275 x_typeflag_block,
276 b"000570\0 ",
277 ),
278 ("maximum block", maximum_block, b"373410\0 "),
279 ] {
280 assert_eq!(Some(checksum(&block)), parse_octal(expected), "{name}");
281 encode_checksum(&mut block);
282 assert_eq!(&block[CHECKSUM_RANGE], expected, "{name}");
283 }
284 }
285}