From bbad68da2a4a67a2d3d04cd5dc48d848a210d16d Mon Sep 17 00:00:00 2001 From: networkException Date: Tue, 23 Jan 2024 03:23:12 +0100 Subject: adfjafjsdhijdofjdesudhhikjujksjsdaljdffsdihjkdjidlrjdjlakahjkljedflsladjojjakhuslhdedjeilulsoksedlajolhekksjkjsskfkifjhwhkd asdeeaasphkekiesljojokjjolpjfseefapjklwifdehkhhsdswidjsjkafjdfljkihsrojedlojdjfadhasljlsrlljfssikfhjksaurhaskjdjoajjkojjdlaakhhjkksedsdhkhaifsaidkhjdjjadhhjrwpskkhjfsjidldsdkfsdsjjkshakaasklaskjjeklkskjtsfsaskdawkuasfsledajjlatakaasdldskffjpiaadhaskjasddsjfjdskdisfwpekdejdthsdudjdsshdhhsoarjlkjhkahhpihhaokjdtfjfhkukhaelshsokusksiadahjrhfsdkaskhjslhdjdwssohfsfjhsdjjsfaksphfhkawjkufsdllikpkjljusdajjddsjkpdjkjsedswurahidkijdlkakaaslsaskkhidjiakhhsrdjjrfkjthaslssipshadaasoajjfhkjdsihkhhsofdskfleljhuduajfhwjkaijfhdjfeuwrhhskpfidsdhkhhsfsuidkhskjewkuaikhljkkhedhsslhdjdhssdufjfjhskjsjlwkjlhlhdhhsriusdsjsosrsdkjjkfkjksejdshawkdsdjkjkpfsfakhdhhidjisdlojkutskplskshjkkijopkjkethkfdahdr --- lib/src/lib.rs | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/lib/src/lib.rs b/lib/src/lib.rs index a8d48b6..b561f13 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs @@ -51,15 +51,23 @@ pub fn decode(buf: &[u8]) -> Vec { let mut out = Vec::new(); for pair in buf.chunks(4) { - out.push((decode_sym(pair[0]) << 6) - + (decode_sym(pair[1]) << 4) - + (decode_sym(pair[2]) << 2) - + decode_sym(pair[3])); + if pair.len() != 4 { + continue; + } + + match (decode_sym(pair[0]), decode_sym(pair[1]), decode_sym(pair[2]), decode_sym(pair[3])) { + (Some(zero), Some(one), Some(two), Some(three)) => out.push((zero << 6) + (one << 4) + (two << 2) + three), + (_, _, _, _) => { + // U+FFFD � REPLACEMENT CHARACTER + out.push(0xFF); + out.push(0xFD); + } + } } out } -pub fn decode_sym(sym: u8) -> u8 { +pub fn decode_sym(sym: u8) -> Option { let find = |d: &[u8; 4]| -> Option { d.iter().enumerate().find(|s| s.1 == &sym).map(|s| s.0 as u8) }; @@ -67,7 +75,6 @@ pub fn decode_sym(sym: u8) -> u8 { .or_else(|| find(&HOME_LEFT)) .or_else(|| find(&ABOVE_RIGHT)) .or_else(|| find(&ABOVE_LEFT)) - .unwrap() } -- cgit v1.2.3