ic_host_artifacts/artifact/
mod.rs1use sha2::{Digest, Sha256};
9use std::{
10 fmt,
11 io::{self, Read},
12 str::FromStr,
13};
14
15mod copy;
16#[cfg(feature = "gzip")]
17mod gzip;
18mod hashing;
19mod matching;
20#[cfg(test)]
21mod tests;
22mod writer;
23
24pub use copy::{CopyError, copy_reader};
25#[cfg(feature = "gzip")]
26pub use gzip::{GzipError, decode_gzip, encode_gzip};
27pub use hashing::HashingWriter;
28pub use matching::MatchingWriter;
29pub use writer::{BoundedWriter, WriterError};
30
31#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
33pub struct Sha256Digest([u8; 32]);
34
35impl Sha256Digest {
36 #[must_use]
38 pub const fn from_bytes(bytes: [u8; 32]) -> Self {
39 Self(bytes)
40 }
41
42 #[must_use]
44 pub const fn as_bytes(&self) -> &[u8; 32] {
45 &self.0
46 }
47
48 #[must_use]
50 pub fn compute(bytes: &[u8]) -> Self {
51 Self(Sha256::digest(bytes).into())
52 }
53}
54
55impl fmt::Display for Sha256Digest {
56 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
57 for byte in self.0 {
58 write!(f, "{byte:02x}")?;
59 }
60 Ok(())
61 }
62}
63
64#[derive(Clone, Copy, Debug, Eq, PartialEq)]
66pub enum DigestParseError {
67 Length {
69 actual: usize,
71 },
72 Digit {
74 offset: usize,
76 },
77}
78
79impl fmt::Display for DigestParseError {
80 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
81 match self {
82 Self::Length { actual } => {
83 write!(f, "SHA-256 requires 64 hex bytes, received {actual}")
84 }
85 Self::Digit { offset } => write!(f, "invalid lowercase SHA-256 digit at byte {offset}"),
86 }
87 }
88}
89impl std::error::Error for DigestParseError {}
90
91impl FromStr for Sha256Digest {
92 type Err = DigestParseError;
93
94 fn from_str(text: &str) -> Result<Self, Self::Err> {
95 if text.len() != 64 {
96 return Err(DigestParseError::Length { actual: text.len() });
97 }
98 let mut bytes = [0; 32];
99 for (offset, digit) in text.bytes().enumerate() {
100 let nibble = match digit {
101 b'0'..=b'9' => digit - b'0',
102 b'a'..=b'f' => digit - b'a' + 10,
103 _ => return Err(DigestParseError::Digit { offset }),
104 };
105 bytes[offset / 2] |= nibble << if offset % 2 == 0 { 4 } else { 0 };
106 }
107 Ok(Self(bytes))
108 }
109}
110
111#[derive(Clone, Copy, Debug, Eq, PartialEq)]
113pub struct ArtifactIdentity {
114 pub bytes: u64,
116 pub sha256: Sha256Digest,
118}
119
120#[derive(Debug)]
122pub enum ArtifactError {
123 Io(io::Error),
125 NotRegularFile,
127 LimitExceeded {
129 limit: u64,
131 },
132 DigestMismatch {
134 expected: Sha256Digest,
136 actual: ArtifactIdentity,
138 },
139 Allocation(std::collections::TryReserveError),
141}
142
143impl fmt::Display for ArtifactError {
144 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
145 match self {
146 Self::Io(source) => write!(f, "artifact read failed: {source}"),
147 Self::NotRegularFile => f.write_str("artifact path is not a regular file"),
148 Self::LimitExceeded { limit } => write!(f, "artifact exceeds {limit} bytes"),
149 Self::DigestMismatch { expected, actual } => write!(
150 f,
151 "artifact SHA-256 is {}, expected {expected}",
152 actual.sha256
153 ),
154 Self::Allocation(source) => write!(f, "artifact allocation failed: {source}"),
155 }
156 }
157}
158impl std::error::Error for ArtifactError {
159 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
160 match self {
161 Self::Io(source) => Some(source),
162 Self::Allocation(source) => Some(source),
163 _ => None,
164 }
165 }
166}
167impl From<io::Error> for ArtifactError {
168 fn from(source: io::Error) -> Self {
169 Self::Io(source)
170 }
171}
172
173pub fn hash_reader(
183 mut reader: impl Read,
184 max_bytes: u64,
185) -> Result<ArtifactIdentity, ArtifactError> {
186 let mut hasher = Sha256::new();
187 let bytes = visit_reader::<ArtifactError>(&mut reader, max_bytes, |chunk| {
188 hasher.update(chunk);
189 Ok(())
190 })?;
191 Ok(ArtifactIdentity {
192 bytes,
193 sha256: Sha256Digest(hasher.finalize().into()),
194 })
195}
196
197pub fn verify_reader(
202 reader: impl Read,
203 max_bytes: u64,
204 expected: Sha256Digest,
205) -> Result<ArtifactIdentity, ArtifactError> {
206 let actual = hash_reader(reader, max_bytes)?;
207 if actual.sha256 != expected {
208 return Err(ArtifactError::DigestMismatch { expected, actual });
209 }
210 Ok(actual)
211}
212
213pub fn read_reader(mut reader: impl Read, max_bytes: usize) -> Result<Vec<u8>, ArtifactError> {
222 let mut bytes = Vec::new();
223 visit_reader::<ArtifactError>(&mut reader, max_bytes as u64, |chunk| {
224 bytes
225 .try_reserve_exact(chunk.len())
226 .map_err(ArtifactError::Allocation)?;
227 bytes.extend_from_slice(chunk);
228 Ok(())
229 })?;
230 Ok(bytes)
231}
232
233fn visit_reader<E: From<ArtifactError>>(
234 reader: &mut impl Read,
235 limit: u64,
236 mut visit: impl FnMut(&[u8]) -> Result<(), E>,
237) -> Result<u64, E> {
238 let mut bytes = 0_u64;
239 let mut buffer = [0_u8; 16 * 1024];
240 loop {
241 let remaining = usize::try_from(limit - bytes).unwrap_or(usize::MAX);
242 let allowance = remaining.saturating_add(1).min(buffer.len());
243 let count = match reader.read(&mut buffer[..allowance]) {
244 Ok(count) => count,
245 Err(source) if source.kind() == io::ErrorKind::Interrupted => continue,
246 Err(source) => return Err(ArtifactError::Io(source).into()),
247 };
248 if count > allowance {
249 return Err(ArtifactError::Io(io::Error::new(
250 io::ErrorKind::InvalidData,
251 "reader returned more bytes than its buffer can hold",
252 ))
253 .into());
254 }
255 if count == 0 {
256 return Ok(bytes);
257 }
258 if count as u64 > limit - bytes {
259 return Err(ArtifactError::LimitExceeded { limit }.into());
260 }
261 visit(&buffer[..count])?;
262 bytes += count as u64;
263 }
264}