team-ljyzipelo

공개
157줄4.3 KB
1use crate::{Entry, Failure, Kind};
2
3pub(crate) fn zero(bytes: &[u8]) -> bool {
4 bytes.iter().all(|byte| *byte == 0)
5}
6
7pub(crate) fn copy(value: &str) -> Result<String, Failure> {
8 let mut result = String::new();
9 result
10 .try_reserve_exact(value.len())
11 .map_err(|_| Failure::Allocation)?;
12 result.push_str(value);
13 Ok(result)
14}
15
16fn text(field: &[u8]) -> Result<&str, Failure> {
17 let end = field
18 .iter()
19 .position(|byte| *byte == 0)
20 .unwrap_or(field.len());
21 if !zero(&field[end..]) {
22 return Err(Failure::Integrity);
23 }
24 std::str::from_utf8(&field[..end]).map_err(|_| Failure::Integrity)
25}
26
27fn octal(field: &[u8], allow_empty: bool) -> Result<u64, Failure> {
28 let mut value = 0_u64;
29 let mut digits = 0;
30 let mut ended = false;
31 for &byte in field {
32 if byte == 0 || byte == b' ' {
33 if digits > 0 || byte == 0 {
34 ended = true;
35 }
36 continue;
37 }
38 if ended || !(b'0'..=b'7').contains(&byte) {
39 return Err(Failure::Integrity);
40 }
41 let digit = u64::from(byte - b'0');
42 if value > (i64::MAX as u64 - digit) / 8 {
43 return Err(Failure::Integrity);
44 }
45 value = value * 8 + digit;
46 digits += 1;
47 }
48 if digits == 0 && !allow_empty {
49 return Err(Failure::Integrity);
50 }
51 Ok(value)
52}
53
54pub(crate) fn parse(h: &[u8], index: usize) -> Result<Entry, Failure> {
55 let checksum = octal(&h[148..156], false)?;
56 let sum: u64 = h
57 .iter()
58 .enumerate()
59 .map(|(index, byte)| {
60 if (148..156).contains(&index) {
61 32
62 } else {
63 u64::from(*byte)
64 }
65 })
66 .sum();
67 if checksum != sum {
68 return Err(Failure::Integrity);
69 }
70 if &h[257..263] != b"ustar\0" || &h[263..265] != b"00" {
71 return Err(Failure::Unsupported);
72 }
73 let kind = match h[156] {
74 0 | b'0' => Kind::File,
75 b'5' => Kind::Directory,
76 _ => return Err(Failure::Unsupported),
77 };
78 for range in [100..108, 108..116, 116..124, 136..148] {
79 octal(&h[range], false)?;
80 }
81 let size = octal(&h[124..136], false)?;
82 if octal(&h[329..337], true)? != 0
83 || octal(&h[337..345], true)? != 0
84 || !zero(&h[157..257])
85 || !zero(&h[500..512])
86 {
87 return Err(Failure::Integrity);
88 }
89 text(&h[265..297])?;
90 text(&h[297..329])?;
91 let leaf = text(&h[..100])?;
92 let prefix = text(&h[345..500])?;
93 if leaf.is_empty() {
94 return Err(Failure::Integrity);
95 }
96 let mut name = String::new();
97 name.try_reserve_exact(prefix.len() + 1 + leaf.len())
98 .map_err(|_| Failure::Allocation)?;
99 if !prefix.is_empty() {
100 name.push_str(prefix);
101 name.push('/');
102 }
103 name.push_str(leaf);
104 if kind == Kind::Directory {
105 if name.ends_with('/') {
106 name.pop();
107 }
108 if size != 0 {
109 return Err(Failure::Integrity);
110 }
111 }
112 if name.is_empty()
113 || name.encode_utf16().count() > 4096
114 || name.contains(['\0', '\\'])
115 || name
116 .split('/')
117 .any(|part| part.is_empty() || part == "." || part == ".." || part.len() > 255)
118 {
119 return Err(Failure::UnsafePath);
120 }
121 // 순번은 정규화 없이 정확한 십진 문자열로 비교함.
122 let mut digits = [0_u8; 20];
123 let mut cursor = digits.len();
124 let mut number = index;
125 loop {
126 cursor -= 1;
127 digits[cursor] = b'0' + (number % 10) as u8;
128 number /= 10;
129 if number == 0 {
130 break;
131 }
132 }
133 let id = copy(std::str::from_utf8(&digits[cursor..]).map_err(|_| Failure::Integrity)?)?;
134 Ok(Entry {
135 id,
136 name,
137 kind,
138 size,
139 })
140}
141
142pub(crate) fn unique(entry: &Entry, previous: &[Entry]) -> Result<(), Failure> {
143 for other in previous {
144 if entry.name == other.name
145 || (other.kind == Kind::File && descendant(&entry.name, &other.name))
146 || (entry.kind == Kind::File && descendant(&other.name, &entry.name))
147 {
148 return Err(Failure::Conflict);
149 }
150 }
151 Ok(())
152}
153
154fn descendant(path: &str, ancestor: &str) -> bool {
155 path.strip_prefix(ancestor)
156 .is_some_and(|suffix| suffix.starts_with('/'))
157}