mirror of
https://gitea.tendokyu.moe/ppc/amnet.git
synced 2026-09-22 22:28:22 +03:00
make access/idm parsing more strict
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
pub (crate) fn format_hex_bytes(bytes: &[u8]) -> String {
|
||||
pub fn format_hex_bytes(bytes: &[u8]) -> String {
|
||||
bytes.chunks(2)
|
||||
.map(|chunk| chunk.iter().map(|b| format!("{:02X}", b)).collect::<String>())
|
||||
.collect::<Vec<_>>()
|
||||
@@ -6,7 +6,12 @@ pub (crate) fn format_hex_bytes(bytes: &[u8]) -> String {
|
||||
}
|
||||
|
||||
pub (crate) fn parse_access_code(code: &String) -> Option<[u8; 10]> {
|
||||
let matrix_code = format!("{:0>20}", code.replace(" ", ""));
|
||||
let matrix_code = code.replace(" ", "");
|
||||
|
||||
if code.chars().any(|c| !c.is_ascii_hexdigit() && !c.is_whitespace()) && matrix_code.len() != 20 {
|
||||
return None;
|
||||
}
|
||||
|
||||
let mut bytes: [u8; 10] = [0; 10];
|
||||
|
||||
for i in 0..bytes.len() {
|
||||
@@ -20,15 +25,14 @@ pub (crate) fn parse_access_code(code: &String) -> Option<[u8; 10]> {
|
||||
}
|
||||
|
||||
pub (crate) fn parse_idm_hex(idm_hex: &String) -> Option<[u8; 8]> {
|
||||
if idm_hex.len() > 16 || !idm_hex.chars().all(|c| c.is_ascii_hexdigit()) {
|
||||
if idm_hex.len() != 16 || idm_hex.chars().any(|c| !c.is_ascii_hexdigit()) {
|
||||
return None;
|
||||
}
|
||||
|
||||
let padded_idm = format!("{:0>16}", idm_hex);
|
||||
let mut idm_bytes: [u8; 8] = [0; 8];
|
||||
|
||||
for i in 0..idm_bytes.len() {
|
||||
let slice_str = &padded_idm[i*2..i*2+2];
|
||||
let slice_str = &idm_hex[i*2..i*2+2];
|
||||
let slice_val = u8::from_str_radix(slice_str, 16).ok()?;
|
||||
|
||||
idm_bytes[i] = slice_val;
|
||||
|
||||
Reference in New Issue
Block a user