File: hexdump.rb

package info (click to toggle)
ruby-httpclient 2.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,724 kB
  • sloc: ruby: 9,544; makefile: 10; sh: 2
file content (50 lines) | stat: -rw-r--r-- 1,275 bytes parent folder | download
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
# encoding: binary

# This was written by Arai-san and published at
# http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-list/31987


module HexDump
  # str must be in BINARY encoding in 1.9
  def encode(str)
    offset = 0
    result = []
    while raw = str.slice(offset, 16) and raw.length > 0
      # data field
      data = ''.dup
      for v in raw.unpack('N* a*')
        if v.kind_of? Integer
          data << sprintf("%08x ", v)
        else
          v.each_byte {|c| data << sprintf("%02x", c) }
        end
      end
      # text field
      text = raw.tr("\000-\037\177-\377", ".")
      result << sprintf("%08x  %-36s  %s", offset, data, text)
      offset += 16
      # omit duplicate line
      if /^(#{regex_quote_n(raw)})+/n =~ str[offset .. -1]
        result << sprintf("%08x  ...", offset)
        offset += $&.length
        # should print at the end
        if offset == str.length
          result << sprintf("%08x  %-36s  %s", offset-16, data, text)
        end
      end
    end
    result
  end
  module_function :encode

  if RUBY_VERSION >= "1.9"
    # raw must be in BINARY encoding in 1.9
    def self.regex_quote_n(raw)
      Regexp.quote(raw)
    end
  else
    def self.regex_quote_n(raw)
      Regexp.quote(raw, 'n')
    end
  end
end