hexd 0.1.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.
Files changed (5) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE.md +21 -0
  3. data/README.md +22 -0
  4. data/lib/hexd.rb +107 -0
  5. metadata +46 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8809de6eedaeedd7f06545bbe01e035af7366cbb
4
+ data.tar.gz: a8c3f9802373b0d9a45a118b6e8ef56780a2b518
5
+ SHA512:
6
+ metadata.gz: acfe15604edca263fe1c014616c940683bcb378ae834fe931018995081882d94b032a390268a66a4a8f587d2388ce5a4b190a21d331a6da130b5181bd25ce114
7
+ data.tar.gz: fdf71e6fc0a8afae496e3f0aa4f7d5690352204cbe1013243ddfd19976ed961c5fd2c50397159c91aa0d1f75b883fa5a7f2c89b9db5ebb00575cf1270ec15455
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) arcsum42@gmail.com
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,22 @@
1
+ # hexd
2
+
3
+ a simple hex dump tool
4
+
5
+ ## usage
6
+
7
+ as an executable:
8
+
9
+ hexd README.md
10
+ cat README.md | hexd
11
+
12
+ as a library:
13
+
14
+ ``` ruby
15
+ require 'hexd'
16
+
17
+ File.open('README.md') do |file|
18
+ Hexd::Dump.new(file).each do |line|
19
+ puts line
20
+ end
21
+ end
22
+ ```
@@ -0,0 +1,107 @@
1
+ module Hexd
2
+ BASE = 16
3
+ BYTE_SIZE = 8
4
+
5
+ class Dump
6
+ include Enumerable
7
+
8
+ BYTES_PER_LINE = 16
9
+
10
+ def initialize(input)
11
+ @input = input
12
+ end
13
+
14
+ def each
15
+ addr = 0
16
+ while (line = @input.read(BYTES_PER_LINE))
17
+ yield LineFormatter.new(addr, line).to_s
18
+ addr += BYTES_PER_LINE
19
+ end
20
+ @input.rewind if @input.is_a?(File)
21
+ nil
22
+ end
23
+
24
+ private
25
+
26
+ class LineFormatter
27
+ ASCII_COL_IDX = 60
28
+ COL_SIZE = 8
29
+
30
+ def initialize(addr, line)
31
+ @addr, @line = addr, line
32
+ end
33
+
34
+ def ascii_column
35
+ AsciiFormatter.new(@line).to_s
36
+ end
37
+
38
+ def bytes_column_one
39
+ @line.bytes[0...COL_SIZE].map do |b|
40
+ ByteFormatter.new(b).to_s << ' '
41
+ end.join
42
+ end
43
+
44
+ def bytes_column_two
45
+ nbytes = @line.bytes.size
46
+
47
+ if nbytes < COL_SIZE
48
+ ''
49
+ else
50
+ @line.bytes[COL_SIZE...nbytes].map do |b|
51
+ ByteFormatter.new(b).to_s << ' '
52
+ end.join
53
+ end
54
+ end
55
+
56
+ def offset_column
57
+ OffsetFormatter.new(@addr).to_s
58
+ end
59
+
60
+ def to_s
61
+ str = offset_column << ' ' << bytes_column_one << ' ' << bytes_column_two
62
+ str = str.ljust(ASCII_COL_IDX) << ascii_column
63
+ end
64
+ end
65
+
66
+ class OffsetFormatter
67
+ def initialize(off)
68
+ @off = off
69
+ end
70
+
71
+ def to_s
72
+ @off.to_s(BASE).rjust(BYTE_SIZE, '0')
73
+ end
74
+ end
75
+
76
+ class ByteFormatter
77
+ def initialize(byte)
78
+ @byte = byte
79
+ end
80
+
81
+ def to_s
82
+ @byte.to_s(BASE).rjust(2, '0')
83
+ end
84
+ end
85
+
86
+ class AsciiFormatter
87
+ def initialize(ascii)
88
+ @ascii = ascii
89
+ end
90
+
91
+ def printable_char?(c)
92
+ c.ord >= ' '.ord && c.ord <= '~'.ord
93
+ end
94
+
95
+ def to_s
96
+ str = '|'
97
+
98
+ @ascii.each_char do |c|
99
+ symbol = (printable_char?(c) ? c : '.')
100
+ str << symbol
101
+ end
102
+
103
+ str << '|'
104
+ end
105
+ end
106
+ end
107
+ end
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hexd
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - John Doe
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-06-26 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: a simple hex dump tool
14
+ email: arcsum42@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - README.md
20
+ - LICENSE.md
21
+ - lib/hexd.rb
22
+ homepage: https://github.com/arcsum/hexd
23
+ licenses:
24
+ - MIT
25
+ metadata: {}
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - '>='
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - '>='
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubyforge_project:
42
+ rubygems_version: 2.0.0
43
+ signing_key:
44
+ specification_version: 4
45
+ summary: a simple hex dump tool
46
+ test_files: []