Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.06 KB | None | 0 0
  1. use std::{io, fs, env};
  2. use ansi_term::Colour::RGB;
  3.  
  4. #[derive(Debug)]
  5. struct Hex {
  6. content: Vec<u8>,
  7. }
  8.  
  9. impl Hex {
  10. fn new(file: &str) -> io::Result<Self> { //check documentation for reference
  11. let content = fs::read(file)?;
  12.  
  13. Ok(Self {
  14. content,
  15. })
  16. }
  17.  
  18. fn show(&self) {
  19. println!("╔══════════╦═════════════════════════════════════════════════╦══════════════════╗");
  20. println!("║ Offset ║ 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F ║ Content ║");
  21. println!("╠══════════╬═════════════════════════════════════════════════╬══════════════════╣");
  22.  
  23. let mut offset = 0;
  24. let mut hexdump = self.content.iter();
  25.  
  26. let match_color = |byte, src: String| {
  27. match byte {
  28. 0 => print!("{}", RGB(200, 200, 200).paint(src)),
  29. 1..=31 => print!("{}", RGB(255, 150, 0).paint(src)),
  30. 32..=126 => print!("{}", RGB(0, 200, 100).paint(src)),
  31. 127..=160 => print!("{}", RGB(100, 0, 100).paint(src)),
  32. _ => print!("{}", RGB(50, 100, 100).paint(src)),
  33. }
  34. };
  35.  
  36. self.content.chunks(16).for_each(|chunk| {
  37. print!("║ {:08X} ║ ", offset);
  38.  
  39. for _ in 0..16 {
  40. match hexdump.next() {
  41. Some(b) => {
  42. let byte = format!("{:02X}", b);
  43. match_color(*b, byte);
  44. },
  45. _ => print!(" "),
  46. }
  47.  
  48. print!(" ");
  49. }
  50.  
  51. print!("║ ");
  52.  
  53. chunk.iter().for_each(|b| {
  54. let src = match b {
  55. 0..=31 | 127..=160 => ".".to_string(),
  56. _ => (*b as char).to_string(),
  57. };
  58.  
  59. match_color(*b, src);
  60. });
  61.  
  62. println!(" ║");
  63.  
  64. offset += 0x10;
  65. });
  66.  
  67. print!("╚══════════╩═══════════════════════════════════════════════════════╩═══════════╝")
  68. }
  69. }
  70.  
  71. /* fn help() {
  72. println!("A CLI hex viewer.\n");
  73. println!("USAGE:");
  74. println!(" hexview.exe [COMMAND]\n");
  75. println!("COMMANDS:");
  76. println!(" view\t View the raw bytes.");
  77. println!(" help\t Show this help output.")
  78. } */
  79.  
  80. fn main() {
  81. let args: Vec<String> = env::args().collect();
  82.  
  83. match args.get(1) {
  84. Some(p) => {
  85. let hex = Hex::new(p);
  86.  
  87. if let Ok(c) = hex {
  88. c.show();
  89. }
  90. },
  91. _ => println!("Help command is WIP, enter a file as argument"),
  92. }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement