From 4a9dab94400cb00ae1e28485ddc64d46cf27ed3c Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Wed, 29 Jan 2020 14:04:06 +0100 Subject: Implement From<&NK_status> for RawConfig This makes it easier to parse only the config part of the NK_status struct and avoids code duplication in the upcoming get_config refactoring. --- src/config.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'src/config.rs') diff --git a/src/config.rs b/src/config.rs index cb678d7..9b9de3c 100644 --- a/src/config.rs +++ b/src/config.rs @@ -83,6 +83,17 @@ impl convert::TryFrom for RawConfig { } } +impl From<&nitrokey_sys::NK_status> for RawConfig { + fn from(status: &nitrokey_sys::NK_status) -> Self { + Self { + numlock: status.config_numlock, + capslock: status.config_capslock, + scrollock: status.config_scrolllock, + user_password: status.otp_user_password, + } + } +} + impl From<[u8; 5]> for RawConfig { fn from(data: [u8; 5]) -> Self { RawConfig { -- cgit v1.2.1 From c1f48ce6c614586042db8891d2eebf19d2212ce4 Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Wed, 29 Jan 2020 13:52:19 +0100 Subject: Use NK_get_status to implement Device::get_config MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit libnitrokey’s NK_read_config function returns a pointer to an array that has been allocated using new[]. We would have to delete this pointer using delete[], but we only have access to free. Therefore this patch modifies the Device::get_config function to call NK_get_status instead of NK_read_config. This also makes the code more safe as we get the data as a struct instead of an array. It does not add much overhead as NK_read_config also executes the GET_STATUS command on the Nitrokey device. --- src/config.rs | 11 ----------- 1 file changed, 11 deletions(-) (limited to 'src/config.rs') diff --git a/src/config.rs b/src/config.rs index 9b9de3c..120a51b 100644 --- a/src/config.rs +++ b/src/config.rs @@ -94,17 +94,6 @@ impl From<&nitrokey_sys::NK_status> for RawConfig { } } -impl From<[u8; 5]> for RawConfig { - fn from(data: [u8; 5]) -> Self { - RawConfig { - numlock: data[0], - capslock: data[1], - scrollock: data[2], - user_password: data[3] != 0, - } - } -} - impl Into for RawConfig { fn into(self) -> Config { Config { -- cgit v1.2.1