mirror of
https://github.com/YGGverse/aquatic.git
synced 2026-04-02 18:55:32 +00:00
aquatic_ws: deserialize 20 byte strings: improve code
This commit is contained in:
parent
761952513f
commit
9030944c02
1 changed files with 16 additions and 16 deletions
|
|
@ -20,29 +20,29 @@ impl<'de> Visitor<'de> for TwentyByteVisitor {
|
||||||
// var infoHash = 'abcd..'; // 40 hexadecimals
|
// var infoHash = 'abcd..'; // 40 hexadecimals
|
||||||
// Buffer.from(infoHash, 'hex').toString('binary');
|
// Buffer.from(infoHash, 'hex').toString('binary');
|
||||||
// ```
|
// ```
|
||||||
// which produces a UTF16 string with each char having only the low
|
// which seemingly produces a UTF16 string with each char having only
|
||||||
// byte set. Here, we extract it by casting to u8.
|
// the "low byte" set. Here, we extract it by casting it to an u8.
|
||||||
|
|
||||||
let mut arr = [0u8; 20];
|
let mut arr = [0u8; 20];
|
||||||
let mut max_i = 0;
|
let mut char_iter = value.chars();
|
||||||
|
|
||||||
for (i, (a, c)) in arr.iter_mut().zip(value.chars()).enumerate(){
|
for a in arr.iter_mut(){
|
||||||
|
if let Some(c) = char_iter.next(){
|
||||||
if c as u32 > 255 {
|
if c as u32 > 255 {
|
||||||
return Err(E::custom(format!(
|
return Err(E::custom(format!(
|
||||||
"character not in single byte range: {}",
|
"character not in single byte range: {:#?}",
|
||||||
c
|
c
|
||||||
)))
|
)));
|
||||||
}
|
|
||||||
*a = c as u8;
|
|
||||||
max_i = i;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if max_i == 19 {
|
*a = c as u8;
|
||||||
Ok(arr)
|
|
||||||
} else {
|
} else {
|
||||||
Err(E::custom(format!("not 20 bytes: {}", value)))
|
return Err(E::custom(format!("not 20 bytes: {:#?}", value)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Ok(arr)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue