aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLeah Neukirchen <[email protected]>2025-02-28 16:55:44 +0100
committerLeah Neukirchen <[email protected]>2025-02-28 17:34:35 +0100
commitc3ad60d2b13d868d6da25c2587e915908c7066f4 (patch)
tree13064c2ffd5603a3311f7fda04b3fa40f68d3f59
parentcd6fdc3e03b0143f0e2a4d7537f58e595cf7cc0b (diff)
b3sum: add tag support for --check
-rw-r--r--b3sum/src/main.rs36
1 files changed, 36 insertions, 0 deletions
diff --git a/b3sum/src/main.rs b/b3sum/src/main.rs
index b10cff8..8d63688 100644
--- a/b3sum/src/main.rs
+++ b/b3sum/src/main.rs
@@ -347,6 +347,42 @@ fn parse_check_line(mut line: &str) -> Result<ParsedCheckLine> {
is_escaped = true;
line = &line[1..];
}
+
+ if line.starts_with("BLAKE3 (") {
+ match line.rsplit_once(')') {
+ Some((file, hash_hex)) => {
+ ensure!(hash_hex.starts_with(" = "), "Invalid tagged line");
+ let file_string = file[8..].to_string();
+ let file_path_string = if is_escaped {
+ // If we detected a backslash at the start of the line earlier, now we
+ // need to unescape backslashes and newlines.
+ unescape(&file_string)?
+ } else {
+ file_string.clone().into()
+ };
+ // Decode the hash hex.
+ let mut hash_bytes = [0; blake3::OUT_LEN];
+ let mut hex_chars = hash_hex[3..].chars();
+ let hash_hex_len = 2 * blake3::OUT_LEN;
+ ensure!(hash_hex.len() - 3 == hash_hex_len, "Wrong hash length");
+ for byte in &mut hash_bytes {
+ let high_char = hex_chars.next().unwrap();
+ let low_char = hex_chars.next().unwrap();
+ *byte = 16 * hex_half_byte(high_char)? + hex_half_byte(low_char)?;
+ }
+ let expected_hash: blake3::Hash = hash_bytes.into();
+ check_for_invalid_characters(&file_path_string)?;
+ return Ok(ParsedCheckLine {
+ file_string,
+ is_escaped,
+ file_path: file_path_string.into(),
+ expected_hash,
+ });
+ }
+ None => ensure!(false, "Invalid tagged line"),
+ }
+ }
+
// The front of the line must be a hash of the usual length, followed by
// two spaces. The hex characters in the hash must be lowercase for now,
// though we could support uppercase too if we wanted to.