mirror of
https://github.com/YGGverse/aquatic.git
synced 2026-03-31 17:55:36 +00:00
access list: improve info hash string parsing
This commit is contained in:
parent
6cfa220097
commit
ea52317b49
1 changed files with 12 additions and 11 deletions
|
|
@ -36,23 +36,24 @@ pub struct AccessList(HashSet<[u8; 20]>);
|
||||||
|
|
||||||
impl AccessList {
|
impl AccessList {
|
||||||
fn parse_line_to_info_hash(line: String) -> anyhow::Result<[u8; 20]> {
|
fn parse_line_to_info_hash(line: String) -> anyhow::Result<[u8; 20]> {
|
||||||
let mut count = 0usize;
|
if line.len() != 20 {
|
||||||
|
return Err(anyhow::anyhow!("Info hash is not 20 bytes long: {}", line));
|
||||||
|
}
|
||||||
|
|
||||||
let mut bytes = [0u8; 20];
|
let mut bytes = [0u8; 20];
|
||||||
|
|
||||||
for (byte, c) in bytes.iter_mut().zip(line.chars()) {
|
for (byte, c) in bytes.iter_mut().zip(line.chars()) {
|
||||||
|
if c as u32 > 255 {
|
||||||
|
return Err(anyhow::anyhow!(
|
||||||
|
"Info hash character is not ASCII: {:#?}",
|
||||||
|
c
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
*byte = c as u8;
|
*byte = c as u8;
|
||||||
count += 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if count == 20 {
|
Ok(bytes)
|
||||||
Ok(bytes)
|
|
||||||
} else {
|
|
||||||
Err(anyhow::anyhow!(
|
|
||||||
"Info hash length only {} bytes: {}",
|
|
||||||
count,
|
|
||||||
line
|
|
||||||
))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update_from_path(&mut self, path: &PathBuf) -> anyhow::Result<()> {
|
pub fn update_from_path(&mut self, path: &PathBuf) -> anyhow::Result<()> {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue