Repository navigation
Expand file tree
/
Copy pathdump.lisp
More file actions
93 lines (75 loc) · 2.38 KB
/
Copy pathdump.lisp
File metadata and controls
93 lines (75 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
;; dump.lisp - Simple utility to dump file contents.
;;
;; Show the contents as hex bytes, and also ASCII
;; characters when they are printable.
;;
;; Example usage:
;;
;; ./dump ./dump
;;
;; Load our argument-parsing library
(require arg-parser)
(defun dump (str)
"Dump an allocated region of memory to STDOUT as hex and ASCII characters."
;; remove header from the allocated length
(let ((len (- (alen str) 24))
(i 0) ;; index
(hex "") ;; hex version of output
(ch "") ;; character version of output
(c nil)) ;; current character
;; For each byte
(while (< i len)
(set! c (strat str i))
;; hex will have the hex bytes of the characters.
(set! hex (strcat (strcat hex (dec2hex c)) " "))
;; ch will have the character bytes.
;; Need to mask out non-printable characters
(if (and (>= c 32) (<= c 127))
(set! ch (strcat ch (string (chr c))))
(set! ch (strcat ch ".")))
;; end of group? Then print and reset.
(when (= (% i 16) 15)
(print hex)
(print " ")
(print ch)
(print "\n")
(set! ch "")
(set! hex ""))
;; Next loop
(set! i (+ 1 i)))
;; Print the remainder of any contents, for the case when
;; the output didn't end up aligned.
(while (<= (strlen hex) (* 5 16)) ;; "0x00 " = 5 bytes
(set! hex (strcat hex " ")))
(print hex)
(print "")
(print ch)
(print "\n")
(newline)))
(defun dump_file (name)
(let ((handle (fopen name "r")) ; open
(data (fread handle)) ; read
(discard (fclose handle))) ; close
(if data
(dump data)
(do
(println "Error reading " name)
(exit 1)))))
(defun help ()
(println "Usage: dump file1 file2 .. fileN")
(exit 0))
(defun main (args)
;; parse arguments
(let ((parser (arg-parser:new (cdr args)))
(files (parser :files)))
;; process flags.
(map (lambda (arg)
(cond
((or (= arg "--help") (= arg "-help")) (help))
((or (= arg "-h") (= arg "-?")) (help))
(t (do (println "Unknown argument: " arg "\n") (help)))))
(parser :flags))
;; If we have files process them, otherwise show help
(if (> (length files) 0)
(map (lambda (file) (dump_file file)) files)
(help))))