SHARE
TWEET

Untitled

a guest Aug 25th, 2019 63 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. use std::io::{Read, Seek, SeekFrom};
  2. use std::fs::File;
  3. use std::path::Path;
  4.  
  5. use std::mem;
  6.  
  7. fn read_four_bytes<P: AsRef<Path>>(path: P) -> [u8; 4] {
  8.     let mut file = File::open(path).expect("File IO Failure");
  9.     let mut value = [0u8; 4];
  10.     file.read_exact(&mut value).expect("Unable to read 4 bytes");
  11.     value
  12. }
  13.  
  14. fn read_float_at_40<P: AsRef<Path>>(path: P) -> f32 {
  15.     let mut file = File::open(path).expect("File IO Failure");
  16.     let mut value = [0u8; 4];
  17.     file.seek(SeekFrom::Start(40)).expect("Unable to seek to 40 bytes");
  18.     file.read_exact(&mut value).expect("Unable to read 4 bytes");
  19.     unsafe { mem::transmute::<[u8; 4], f32>(value) }
  20. }
  21.  
  22. fn main() {
  23.     let header = read_four_bytes("/proc/self/exe");
  24.     assert_eq!(header, [0x7F, b'E', b'L', b'F']);
  25.     println!("{:?}", header);
  26.    
  27.     let random_float = read_float_at_40("/proc/self/exe");
  28.     println!("{:?}", random_float);
  29. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top