dump-binary
[1/8] from: jvargas::whywire::net at: 30-Apr-2004 12:04
I have being working with binaries a lot lately so I created this
tool that lets you examine the contents of a binary in a more
explicit way that you using probe.
Please critique my code so that I can improve it.
Thank you, and enjoy -- Jaime
dump-binary: func ['buffer /local v b t c r pad pl len chars tmp][
chars: charset [#"a" - #"z" #"A" - #"Z" #"0" - #"9" #"-"]
either binary! = type? b: get buffer [
t: to-integer divide len: length? b 16
r: remainder len 16
pl: either odd? r [40 - (5 * (r - 1) / 2)][40 - (5 * r / 2)]
pad: head insert/dup copy {} #" " pl
print [buffer "=="]
for j 0 t 1 [
prin head insert/dup tmp: to-string j "0" 4 - length? tmp
prin ":^-"
; first print the hex format
c: either j = t [r][16]
repeat i c [
prin lowercase at to-hex pick b (j * 16 + i) 7
if equal? i // 2 0 [prin " "]
]
if all [r <> 0 j = t] [prin pad]
either j <> t [prin "^-|^- "][if r <> 0 [prin "^-|^- "]]
; now print the ascii character
repeat i c [
prin either find chars v: to-char pick b (j * 16 + i) [v][#"."]
]
prin newline
]
][
make error! reform [buffer "must be a binary!"]
]
]
[2/8] from: hallvard:ystad:oops-as:no at: 30-Apr-2004 20:19
This is brilliant, Jaime. Do you mind if I include it in prob.r, my visual probe tool?
I can put a note with your name on it on every face where it is used if you like...
Regards,
HY
Dixit Jaime Vargas (18.04 30.04.2004):
[3/8] from: greggirwin:mindspring at: 30-Apr-2004 13:13
Jaime,
*Very* nice! Thanks for posting it!
-- Gregg
[4/8] from: atruter:labyrinth:au at: 1-May-2004 9:28
Hi Jaime,
> Please critique my code so that I can improve it.
First, let me echo other folks comments ... well done, I hope this ends up
(in some shape or form) as part of the main REBOL code much like
'dump-face, 'dump-obj and 'dump-pane.
Now, for some general observations (all opinion, so ignore as you see fit!
;) ).
1. I'd name the function 'dump-bin (to be consistant with 'dump-obj and
the use of *-dir function names)
2. I'd add function help, something like:
dump-bin: func [
"Print ASCII info for entire binary. (for debugging)"
bin [binary!]
/local v t c r pad pl len chars tmp
][
...
3. As above, I'd use bin instead of 'buffer and b, and explicity require a
binary! (This simplifies the code by letting you remove "either binary! type? b: get
buffer" and the matching error block).
4. Ditch the 'print [buffer "=="]' due to above change
These small changes make the function more generic, enabling you to do the
following:
dump-bin my-binary
dump-bin to-binary "abc"
dump-bin read/binary %my-binary.exe
Regards,
Ashley
[5/8] from: jvargas:whywire at: 1-May-2004 20:50
Thank you all for the input. Below is a new version of dump-bin that
includes Ashley's suggestions. Hallvard you got my permission to
include in prob.r.
Greg, I think your suggestion that probe should produce output
similar to dump-bin by default is great. I hope Carl goes for it.
Finally, I hope to be posting other code snippets soon. I am working
on a ssh scheme for rebol versions that support encryption. I am
thinking about releasing it to the public, but first I need to figure
out
the licensing terms.
Cheers, Jaime
-- The best way to predict the future is to invent it -- Alan Kay
dump-bin: func [
"Print ASCII info for entire binary. (for debugging)"
bin [binary!]
/local v t c r pad pl len chars tmp
][
chars: charset [#"a" - #"z" #"A" - #"Z" #"0" - #"9" #"-"]
t: to-integer divide len: length? bin 16
r: remainder len 16
pl: either odd? r [40 - (5 * (r - 1) / 2)][40 - (5 * r / 2)]
pad: head insert/dup copy {} #" " pl
for j 0 t 1 [
prin head insert/dup tmp: to-string j "0" 4 - length? tmp
prin ":^-"
; first print the hex format
c: either j = t [r][16]
repeat i c [
prin lowercase at to-hex pick bin (j * 16 + i) 7
if equal? i // 2 0 [prin " "]
]
if all [r <> 0 j = t] [prin pad]
either j <> t [prin "^-|^- "][if r <> 0 [prin "^-|^- "]]
; now print the ascii character
repeat i c [
prin either find chars v: to-char pick bin (j * 16 + i) [v][#"."]
]
prin newline
]
]
[6/8] from: andreas:bolka:gmx at: 2-May-2004 14:41
Sunday, May 2, 2004, 2:50:44 AM, Jaime wrote:
> dump-bin: func [
> "Print ASCII info for entire binary. (for debugging)"
> bin [binary!]
> /local v t c r pad pl len chars tmp
> ][
> ..
I just have to metoo: Jaime, that's great! I've been working with
something similar for years, but your dump-bin is just sooo much
better!
I hope this will make it's way into REBOL/Core in one form or another.
--
Best regards,
Andreas
[7/8] from: didec:tiscali at: 3-May-2004 16:49
Re: dump-binary
Nice job Jaime.
I like it.
Maybe, it would be nice to have a refinment to make the function "interactive". Almost
to print by page/line waiting for key to continue (like the 'more "command" does in many
shell).
But may be more than that, allowing to go backward/forward...
Or simply a refinment to specify the beginning of the dump and another for the length.
I think debugging things like RebDB records need this king of options.
DideC
[8/8] from: hallvard:ystad:oops-as:no at: 3-May-2004 17:40
Dixit Didec (16.49 03.05.2004):
>Maybe, it would be nice to have a refinment to make the function "interactive". Almost
to print by page/line waiting for key to continue (like the 'more "command" does in many
shell).
>But may be more than that, allowing to go backward/forward...
Hm. Sounds more like a job for /view. Like a little editor or something.
>Or simply a refinment to specify the beginning of the dump and another for the length.
I think debugging things like RebDB records need this king of options.
King of options, yes, indeed!
HY