draugr.git


draugr

~ single ELF file analysis tool
git clone https://github.com/djnnvx/draugr.git
log | files | refs | archive | README

hexdump.odin (1160B)

      1 package analysis
      2 
      3 import "core:fmt"
      4 import "core:os"
      5 
      6 HexdumpConfig :: struct {
      7 	width       : int,
      8 	show_offset : bool,
      9 	show_ascii  : bool,
     10 }
     11 
     12 default_config :: proc() -> HexdumpConfig {
     13 	return HexdumpConfig{
     14 		width = 16,
     15 		show_offset = true,
     16 		show_ascii = true,
     17 	}
     18 }
     19 
     20 hexdump :: proc(data: []u8, offset: u64, out_path: string) -> string {
     21 	cfg := default_config()
     22 
     23 	f, err := open_out(out_path)
     24 	if err != "" {
     25 		return err
     26 	}
     27 	defer close_out(f, out_path)
     28 
     29 	for i := 0; i < len(data); i += cfg.width {
     30 		if cfg.show_offset {
     31 			fmt.fprintf(f, "%08x  ", offset + u64(i))
     32 		}
     33 		
     34 		for j := 0; j < cfg.width; j += 1 {
     35 			if i + j < len(data) {
     36 				fmt.fprintf(f, "%02x ", data[i + j])
     37 			} else {
     38 				fmt.fprintf(f, "   ")
     39 			}
     40 		}
     41 		
     42 		if cfg.show_ascii {
     43 			fmt.fprintf(f, " |")
     44 			for j := 0; j < cfg.width; j += 1 {
     45 				if i + j < len(data) {
     46 					c := data[i + j]
     47 					if c >= 32 {
     48 						if c <= 126 {
     49 							fmt.fprintf(f, "%c", c)
     50 						} else {
     51 							fmt.fprintf(f, ".")
     52 						}
     53 					} else {
     54 						fmt.fprintf(f, ".")
     55 					}
     56 				} else {
     57 					fmt.fprintf(f, " ")
     58 				}
     59 			}
     60 			fmt.fprintf(f, "|")
     61 		}
     62 		
     63 		fmt.fprintf(f, "\n")
     64 	}
     65 	
     66 	return ""
     67 }