sim: Use byteorder for multi-byte values
Instead of manually expanding multi-byte encoding, use the byteorder
crate which has its own extension methods to do this. This both makes
the code a bit clearer, and also makes it clear that these encodings are
specific to little endian platforms.
Signed-off-by: David Brown <david.brown@linaro.org>
diff --git a/sim/Cargo.lock b/sim/Cargo.lock
index a51f757..314b5f3 100644
--- a/sim/Cargo.lock
+++ b/sim/Cargo.lock
@@ -17,7 +17,7 @@
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"block-cipher-trait 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)",
- "byteorder 1.2.7 (registry+https://github.com/rust-lang/crates.io-index)",
+ "byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)",
"opaque-debug 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
@@ -81,7 +81,7 @@
version = "0.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
- "byteorder 1.2.7 (registry+https://github.com/rust-lang/crates.io-index)",
+ "byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)",
"safemem 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
@@ -104,6 +104,7 @@
dependencies = [
"aes-ctr 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"base64 0.9.3 (registry+https://github.com/rust-lang/crates.io-index)",
+ "byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)",
"docopt 0.8.3 (registry+https://github.com/rust-lang/crates.io-index)",
"env_logger 0.5.13 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.46 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -120,7 +121,7 @@
[[package]]
name = "byteorder"
-version = "1.2.7"
+version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
@@ -562,7 +563,7 @@
"checksum base64 0.9.3 (registry+https://github.com/rust-lang/crates.io-index)" = "489d6c0ed21b11d038c31b6ceccca973e65d73ba3bd8ecb9a2babf5546164643"
"checksum bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "228047a76f468627ca71776ecdebd732a3423081fcf5125585bcd7c49886ce12"
"checksum block-cipher-trait 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "1c924d49bd09e7c06003acda26cd9742e796e34282ec6c1189404dee0c1f4774"
-"checksum byteorder 1.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = "94f88df23a25417badc922ab0f5716cc1330e87f71ddd9203b3a3ccd9cedf75d"
+"checksum byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a7c3dd8985a7111efc5c80b44e23ecdd8c007de8ade3b96595387e812b957cf5"
"checksum cc 1.0.28 (registry+https://github.com/rust-lang/crates.io-index)" = "bb4a8b715cb4597106ea87c7c84b2f1d452c7492033765df7f32651e66fcf749"
"checksum cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "082bb9b28e00d3c9d39cc03e64ce4cea0f1bb9b3fde493f0cbc008472d22bdf4"
"checksum ctr 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "28912c12ae9ba20d6971168379d1482a4ce17f4855f23218ffb53ddc91fbe69b"
diff --git a/sim/Cargo.toml b/sim/Cargo.toml
index 28c8dc0..82ae932 100644
--- a/sim/Cargo.toml
+++ b/sim/Cargo.toml
@@ -19,6 +19,7 @@
multiimage = ["mcuboot-sys/multiimage"]
[dependencies]
+byteorder = "1.3"
libc = "0.2.0"
rand = "0.4.0"
docopt = "0.8"
diff --git a/sim/src/tlv.rs b/sim/src/tlv.rs
index 1af8618..f78c507 100644
--- a/sim/src/tlv.rs
+++ b/sim/src/tlv.rs
@@ -8,6 +8,9 @@
//! Because of this header, we have to make two passes. The first pass will compute the size of
//! the TLV, and the second pass will build the data for the TLV.
+use byteorder::{
+ LittleEndian, WriteBytesExt,
+};
use pem;
use base64;
use ring::{digest, rand};
@@ -200,8 +203,7 @@
let size = self.get_size();
result.push(0x07);
result.push(0x69);
- result.push((size & 0xFF) as u8);
- result.push(((size >> 8) & 0xFF) as u8);
+ result.write_u16::<LittleEndian>(size).unwrap();
if self.kinds.contains(&TlvKinds::SHA256) {
let hash = digest::digest(&digest::SHA256, &self.payload);
@@ -259,8 +261,7 @@
result.push(TlvKinds::RSA3072 as u8);
}
result.push(0);
- result.push((signature.len() & 0xFF) as u8);
- result.push(((signature.len() >> 8) & 0xFF) as u8);
+ result.write_u16::<LittleEndian>(signature.len() as u16).unwrap();
result.extend_from_slice(&signature);
}
@@ -295,8 +296,7 @@
signature[1] += 1;
}
- result.push((signature.len() & 0xFF) as u8);
- result.push(((signature.len() >> 8) & 0xFF) as u8);
+ result.write_u16::<LittleEndian>(signature.len() as u16).unwrap();
result.extend_from_slice(signature.as_ref());
}
@@ -327,8 +327,7 @@
result.push(0);
let signature = signature.as_ref().to_vec();
- result.push((signature.len() & 0xFF) as u8);
- result.push(((signature.len() >> 8) & 0xFF) as u8);
+ result.write_u16::<LittleEndian>(signature.len() as u16).unwrap();
result.extend_from_slice(signature.as_ref());
}