Use a byte buffer for the password

Introduces a "UnicodeError" to express improperly encoded
passwords.
This commit is contained in:
Florian Gilcher 2016-03-08 11:29:16 +01:00
parent f601c49e28
commit c5fd3b1145
2 changed files with 9 additions and 4 deletions

View File

@ -16,6 +16,8 @@ pub enum TerminalError {
StdinError, StdinError,
/// Failed to parse number. /// Failed to parse number.
ParseError, ParseError,
/// Failed to read unicode encoded data.
UnicodeError
} }
impl TerminalError { impl TerminalError {
@ -27,6 +29,7 @@ impl TerminalError {
TerminalError::StdoutError => "Failed to write to stdout.", TerminalError::StdoutError => "Failed to write to stdout.",
TerminalError::StdinError => "Failed to read from stdin.", TerminalError::StdinError => "Failed to read from stdin.",
TerminalError::ParseError => "Failed to parse number.", TerminalError::ParseError => "Failed to parse number.",
TerminalError::UnicodeError => "Failed to read unicode encoded data.",
} }
} }
} }

View File

@ -13,17 +13,19 @@ pub trait ReadExt {
impl<R: Read> ReadExt for R { impl<R: Read> ReadExt for R {
fn read_passwd<W: Write>(&mut self, writer: &mut W) -> Result<Option<String>, TerminalError> { fn read_passwd<W: Write>(&mut self, writer: &mut W) -> Result<Option<String>, TerminalError> {
let _raw = try!(writer.into_raw_mode()); let _raw = try!(writer.into_raw_mode());
let mut string = String::with_capacity(30); let mut passbuf = Vec::with_capacity(30);
for c in self.bytes() { for c in self.bytes() {
match c { match c {
Err(_) => return Err(TerminalError::StdinError), Err(_) => return Err(TerminalError::StdinError),
Ok(0) | Ok(3) | Ok(4) => return Ok(None), Ok(0) | Ok(3) | Ok(4) => return Ok(None),
Ok(b'\n') | Ok(b'\r') => return Ok(Some(string)), Ok(b'\n') | Ok(b'\r') => break,
Ok(c) => string.push(c as char), Ok(c) => passbuf.push(c),
} }
} }
Ok(Some(string)) let passwd = try!(String::from_utf8(passbuf).map_err(|_| TerminalError::UnicodeError ));
Ok(Some(passwd))
} }
} }