diff options
Diffstat (limited to 'lib/src')
-rw-r--r-- | lib/src/lib.rs | 19 |
1 files 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<u8> { 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<u8> { let find = |d: &[u8; 4]| -> Option<u8> { 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() } |