hex_dump 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/examples/file_dump.rb +17 -0
- data/examples/img.png +0 -0
- data/lib/hex_dump.rb +79 -0
- metadata +50 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 061d35cd6d8ab4ca7e1ea1839883b63f4a5f539d
|
4
|
+
data.tar.gz: a9c19068b15f30fd9103c34ac6414201d459de5c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4286c9c73eeaea2bfea74a812bf5505b2fadbbf5c8ecb53737f0301a099d8b1c15c099d489a679932f233f7b268f1fc969ab5d7287c871df1b071bd2c23042bf
|
7
|
+
data.tar.gz: 0da6d609b65fa05f33354e5210e748f52fdcca600bfef0f82a91cdd32311bcf05cd6608a480ea4445da63dc9bdc031688221da825e06b240785b8c40d878276b
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# Hexadecimal dump of a binary (image) file.
|
2
|
+
# to run this file enter this on command line:
|
3
|
+
# $ ruby file_dump.rb
|
4
|
+
|
5
|
+
require 'hex_dump'
|
6
|
+
|
7
|
+
contents = File.read("img.png") # read file into a string
|
8
|
+
|
9
|
+
# display hex with default options (line_size:16 and hex_color:HexDump::BLACK)
|
10
|
+
HexDump.print contents
|
11
|
+
|
12
|
+
# display hex in color Yellow. (default line_size:16 will be used
|
13
|
+
# since it is not specified in options hash)
|
14
|
+
HexDump.print contents, options={hex_color: HexDump::CYAN}
|
15
|
+
|
16
|
+
# display hex with line width 24 and foreground color green
|
17
|
+
HexDump.print contents, options={line_size: 24, hex_color: HexDump::GREEN}
|
data/examples/img.png
ADDED
Binary file
|
data/lib/hex_dump.rb
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
# This module contains all the methods required for displaying
|
2
|
+
# the string/binary in hexadecimal.
|
3
|
+
# This has module functions which can be directly called in the code that
|
4
|
+
# requires this ruby file or the gem.
|
5
|
+
# The gem is at http://rubygems.org/gems/hex_dump and can be installed using
|
6
|
+
# $ gem install hex_dump
|
7
|
+
#
|
8
|
+
# @author Shivam Patel
|
9
|
+
|
10
|
+
module HexDump
|
11
|
+
|
12
|
+
# foreground color constants
|
13
|
+
BLACK = 30
|
14
|
+
RED = 31
|
15
|
+
GREEN = 32
|
16
|
+
YELLOW = 33
|
17
|
+
BLUE = 34
|
18
|
+
MAGENTA = 35
|
19
|
+
CYAN = 36
|
20
|
+
WHITE = 37
|
21
|
+
|
22
|
+
# Takes a buffer and displays it in hexadecimal in a format described by the options
|
23
|
+
# The optional options are :line_size and :hex_color
|
24
|
+
# :line_size sets the size (width) of the hex display. Can be 16(default), 24, 32 or 64
|
25
|
+
# :hex_color sets the color of the foreground text. Can be a number between 30 to 37 (see codes above)
|
26
|
+
# @param buffer [String] The actual binary data/string that is to be displayed in hex.
|
27
|
+
# @param options [Hash] This can be blank (in that case defaults options will be used).
|
28
|
+
# Otherwise a hash providing any of the settings that are to be changed.
|
29
|
+
# Eg. to change line witdh, pass options as { line_size: 32 }
|
30
|
+
# to change both line width and the foreground color, pass options as { line_size: 32, hex_color: 34 }
|
31
|
+
# @return [String] A string containing the entire hexdump.
|
32
|
+
def self.print(buffer = "", options = {})
|
33
|
+
# merge(and overwrite) whatsoever options were passed by user
|
34
|
+
options = {line_size: 16, hex_color: WHITE }.merge(options)
|
35
|
+
|
36
|
+
line_size = options[:line_size]
|
37
|
+
hex_color = options[:hex_color]
|
38
|
+
|
39
|
+
line_size = 16 unless [16, 24, 32, 64].include?(line_size) # prevent user providing arbit line_size
|
40
|
+
|
41
|
+
byte_number = 0
|
42
|
+
lines = ''
|
43
|
+
line_format = '%02x%02x ' * (line_size/2)
|
44
|
+
buffer_length = buffer.length
|
45
|
+
|
46
|
+
while byte_number < buffer_length
|
47
|
+
line_number = (sprintf '%08x', byte_number) + ": " # print byte number padded with zeros on left
|
48
|
+
|
49
|
+
if (buffer_length - byte_number) >= line_size
|
50
|
+
characters = buffer.byteslice(byte_number, line_size) # get a line size worth of characters
|
51
|
+
unpacked_chars = characters.unpack("C#{line_size}")
|
52
|
+
line_hex = send(:sprintf, line_format, *unpacked_chars )
|
53
|
+
else
|
54
|
+
len = buffer_length - byte_number
|
55
|
+
characters = buffer.byteslice(byte_number, len)
|
56
|
+
if len == 1
|
57
|
+
whitespace_pads = " " * ((line_size * 2.5) - 2) # this last line will only take 3 chars
|
58
|
+
line_format = "%02x#{whitespace_pads}"
|
59
|
+
else
|
60
|
+
whitespace_pads = " " * ((line_size * 2.5) - (len * 2 ) - len/2 )
|
61
|
+
line_format = '%02x%02x ' * (len/2) + '%02x' * (len % 2) + whitespace_pads
|
62
|
+
end
|
63
|
+
unpacked_chars = characters.unpack("C#{len}")
|
64
|
+
line_hex = send(:sprintf, line_format, *unpacked_chars )
|
65
|
+
|
66
|
+
line_size = len # for one last line, change the line_size
|
67
|
+
end
|
68
|
+
# replace all non printable chars with a '.' char
|
69
|
+
line_char = unpacked_chars.map{ |c| c.chr}.join.tr("^\x20-\x7E", '.')
|
70
|
+
|
71
|
+
line = line_number + "\e[#{hex_color}m" + line_hex + " " + line_char + "\e[0m"
|
72
|
+
byte_number += line_size
|
73
|
+
puts line
|
74
|
+
lines += line
|
75
|
+
end # end while
|
76
|
+
return lines # the entire hex dump
|
77
|
+
end # end def print
|
78
|
+
|
79
|
+
end #end module
|
metadata
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hex_dump
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Shivam Patel
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-01-17 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: This gem allows programmers to hexdump arbitary strings and binaries.
|
14
|
+
Helpful for your reverse engineering scripts. Has options like line_width and hex_colors
|
15
|
+
which can help customize the format in which the resultant hexadecimal dump of the
|
16
|
+
string or binary is displayed.
|
17
|
+
email: shivam@shivampatel.net
|
18
|
+
executables: []
|
19
|
+
extensions: []
|
20
|
+
extra_rdoc_files: []
|
21
|
+
files:
|
22
|
+
- examples/file_dump.rb
|
23
|
+
- examples/img.png
|
24
|
+
- lib/hex_dump.rb
|
25
|
+
homepage: https://github.com/shivampatel/hex_dump
|
26
|
+
licenses:
|
27
|
+
- MIT
|
28
|
+
metadata: {}
|
29
|
+
post_install_message:
|
30
|
+
rdoc_options: []
|
31
|
+
require_paths:
|
32
|
+
- lib
|
33
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - ">="
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
43
|
+
requirements: []
|
44
|
+
rubyforge_project:
|
45
|
+
rubygems_version: 2.2.1
|
46
|
+
signing_key:
|
47
|
+
specification_version: 4
|
48
|
+
summary: Hex Dump binaries and string from your ruby code
|
49
|
+
test_files: []
|
50
|
+
has_rdoc:
|