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
163pub(crate) fn parse_number(format: ArchiveFormat, bytes: &[u8]) -> Option<u64> {
168 match format {
169 ArchiveFormat::Pax => parse_octal(bytes),
170 ArchiveFormat::Gnu => parse_gnu_number(bytes),
171 }
172}
173
174fn parse_gnu_number(bytes: &[u8]) -> Option<u64> {
181 match bytes.first()? {
182 0x80 => bytes[1..].iter().try_fold(0_u64, |value, byte| {
183 value.checked_mul(256)?.checked_add(u64::from(*byte))
184 }),
185 0xff => None,
188 _ => parse_octal(bytes),
189 }
190}
191
192#[cfg(test)]
193mod tests {
194 use super::*;
195
196 #[test]
197 fn encodes_octal_values_that_fit_the_field() {
198 let mut field = [0xff; 4];
199 assert!(encode_octal(&mut field, 0o17));
200 assert_eq!(&field, b"017\0");
201 assert_eq!(parse_octal(&field), Some(0o17));
202
203 assert!(encode_octal(&mut field, 0o777));
204 assert_eq!(&field, b"777\0");
205 assert!(!encode_octal(&mut field, 0o1000));
206 assert!(!encode_octal(&mut [], 0));
207 assert!(!encode_octal(&mut [0], 0));
208 }
209
210 #[test]
211 fn parses_strict_octal_fields() {
212 for (field, expected) in [
213 (&b"000017 "[..], Some(0o17)),
215 (&b"0000000000000000000000000000017 "[..], Some(0o17)),
216 (&b"17\0"[..], Some(0o17)),
218 (&b"17 "[..], Some(0o17)),
220 (&b"17 \0"[..], Some(0o17)),
222 (&b""[..], None),
224 (&b"\0"[..], None),
226 (&b" "[..], None),
227 (&b"17"[..], None),
229 (&b"18\0"[..], None),
231 (&b"1\0\x32"[..], None),
233 (&b"1\0\x31"[..], None),
235 (&b"1 1"[..], None),
236 (&[0x80, 0][..], None),
238 (&b"77777777777777777777777 "[..], None),
240 (&b"77777777777777777777777\0"[..], None),
241 ] {
242 assert_eq!(parse_octal(field), expected, "{field:?}");
243 }
244 }
245
246 #[test]
247 fn checksums_known_blocks() {
248 let zero_block = [0; crate::BLOCK_SIZE];
249 let mut x_typeflag_block = zero_block;
250 x_typeflag_block[TYPEFLAG_OFFSET] = b'x';
251 x_typeflag_block[CHECKSUM_RANGE].fill(0xff);
252 let maximum_block = [0xff; crate::BLOCK_SIZE];
253
254 for (name, mut block, expected) in [
255 ("zero block", zero_block, b"000400\0 "),
256 (
257 "x typeflag with junk checksum bytes",
258 x_typeflag_block,
259 b"000570\0 ",
260 ),
261 ("maximum block", maximum_block, b"373410\0 "),
262 ] {
263 assert_eq!(Some(checksum(&block)), parse_octal(expected), "{name}");
264 encode_checksum(&mut block);
265 assert_eq!(&block[CHECKSUM_RANGE], expected, "{name}");
266 }
267 }
268}