diff options
author | Justus Winter <justus@sequoia-pgp.org> | 2018-10-10 17:53:18 +0200 |
---|---|---|
committer | Justus Winter <justus@sequoia-pgp.org> | 2018-10-10 17:55:17 +0200 |
commit | 2973ed3e5ac1c1d74a7ccc2d17a886a0fa498a31 (patch) | |
tree | 9c1ee16399f766fae945dc9f70ac799a9edc815c /openpgp/src/conversions.rs | |
parent | 36220a4b1316816bba15dffda47a3d63fdd29712 (diff) |
openpgp: Move conversion functions.
Diffstat (limited to 'openpgp/src/conversions.rs')
-rw-r--r-- | openpgp/src/conversions.rs | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/openpgp/src/conversions.rs b/openpgp/src/conversions.rs index b7dbd891..77236fbd 100644 --- a/openpgp/src/conversions.rs +++ b/openpgp/src/conversions.rs @@ -140,8 +140,34 @@ pub(crate) fn from_hex(hex: &str, pretty: bool) -> Result<Vec<u8>> { Ok(bytes) } +pub(crate) fn read_be_u64(b: &[u8]) -> u64 { + assert_eq!(b.len(), 8); + ((b[0] as u64) << 56) as u64 + | ((b[1] as u64) << 48) + | ((b[2] as u64) << 40) + | ((b[3] as u64) << 32) + | ((b[4] as u64) << 24) + | ((b[5] as u64) << 16) + | ((b[6] as u64) << 8) + | ((b[7] as u64) << 0) +} + +pub(crate) fn write_be_u64(b: &mut [u8], n: u64) { + assert_eq!(b.len(), 8); + b[0] = (n >> 56) as u8; + b[1] = (n >> 48) as u8; + b[2] = (n >> 40) as u8; + b[3] = (n >> 32) as u8; + b[4] = (n >> 24) as u8; + b[5] = (n >> 16) as u8; + b[6] = (n >> 8) as u8; + b[7] = (n >> 0) as u8; +} + #[cfg(test)] mod test { + use super::*; + #[test] fn from_hex() { use super::from_hex as fh; @@ -201,4 +227,12 @@ mod test { data == super::from_hex(&hex, true).unwrap() } } + + quickcheck! { + fn be_u64_roundtrip(n: u64) -> bool { + let mut b = [0; 8]; + write_be_u64(&mut b, n); + n == read_be_u64(&b) + } + } } |