Class: Unisec::Hexdump

Inherits:
Object
  • Object
show all
Defined in:
lib/unisec/hexdump.rb

Overview

Hexadecimal dump (hexdump) of all Unicode encodings.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(str) ⇒ Hexdump

Init the hexdump.

Examples:

hxd = Unisec::Hexdump.new('I ๐Ÿ’• Ruby ๐Ÿ’Ž')
hxd.utf8 # => "49 20 f0 9f 92 95 20 52 75 62 79 20 f0 9f 92 8e"
hxd.utf16be # => "0049 0020 d83d dc95 0020 0052 0075 0062 0079 0020 d83d dc8e"
hxd.utf32be # => "00000049 00000020 0001f495 00000020 00000052 00000075 00000062 00000079 00000020 0001f48e"

Parameters:

  • str (String)

    Input string to encode



35
36
37
38
39
40
41
# File 'lib/unisec/hexdump.rb', line 35

def initialize(str)
  @utf8 = Hexdump.utf8(str)
  @utf16be = Hexdump.utf16be(str)
  @utf16le = Hexdump.utf16le(str)
  @utf32be = Hexdump.utf32be(str)
  @utf32le = Hexdump.utf32le(str)
end

Instance Attribute Details

#utf16beString (readonly)

UTF-16BE hexdump

Returns:

  • (String)

    UTF-16BE hexdump



14
15
16
# File 'lib/unisec/hexdump.rb', line 14

def utf16be
  @utf16be
end

#utf16leString (readonly)

UTF-16LE hexdump

Returns:

  • (String)

    UTF-16LE hexdump



18
19
20
# File 'lib/unisec/hexdump.rb', line 18

def utf16le
  @utf16le
end

#utf32beString (readonly)

UTF-32BE hexdump

Returns:

  • (String)

    UTF-32BE hexdump



22
23
24
# File 'lib/unisec/hexdump.rb', line 22

def utf32be
  @utf32be
end

#utf32leString (readonly)

UTF-32LE hexdump

Returns:

  • (String)

    UTF-32LE hexdump



26
27
28
# File 'lib/unisec/hexdump.rb', line 26

def utf32le
  @utf32le
end

#utf8String (readonly)

UTF-8 hexdump

Returns:



10
11
12
# File 'lib/unisec/hexdump.rb', line 10

def utf8
  @utf8
end

Class Method Details

.display_reverse(hexbytes, enc, exact: true) ⇒ String

Display a CLI-friendly output summurizing the reverse hexdump search results

Examples:

puts Unisec::Hexdump.display_reverse('0d0a', 'utf16be', exact: true)
# เดŠ (U+0D0A) - 0d0a
puts Unisec::Hexdump.display_reverse('808080', 'utf8', exact: false)
# ๑€€€ (U+40000) - f1 80 80 80
# ๒€€€ (U+80000) - f2 80 80 80
# ๓€€€ (U+C0000) - f3 80 80 80
# ๔€€€ (U+100000) - f4 80 80 80

Parameters:

Returns:

  • (String)

    CLI-ready output



145
146
147
148
149
150
151
152
153
154
# File 'lib/unisec/hexdump.rb', line 145

def self.display_reverse(hexbytes, enc, exact: true)
  res = Unisec::Hexdump.reverse(hexbytes, enc, exact: exact)
  out = ''
  res.each do |char|
    cp = Utils::String.char2codepoint(char)
    hxd = Unisec::Hexdump.send(enc, char)
    out += "#{char.encode('UTF-8')} (#{cp}) - #{hxd}\n"
  end
  out
end

.reverse(hexbytes, enc, exact: true) ⇒ Array<String>

Search X byte(s) hexadecimal value in Y encoding, basically which characters will give this resulting encoded value

Examples:

Unisec::Hexdump.reverse('61', 'utf8') # => ["a"]
Unisec::Hexdump.reverse('a6', 'utf8', exact: true) # => []
Unisec::Hexdump.reverse('a6', 'utf8', exact: false) # => ["ยฆ",  "รฆ",  "ฤฆ",  "ลฆ",  "ฦฆ", "วฆ", โ€ฆ ]
Unisec::Hexdump.reverse('0d0a', 'utf16be', exact: true) # => ["\u0D0A"] (เดŠ)

Parameters:

  • hexbytes (String)

    Byte(s) in hexadecimal to search for

  • enc (String)

    The target encoding in which to search. It uses Unisec CLI argument values (utf8 utf16be utf16le utf32be utf32le).

  • exact (TrueClass|FalseClass) (defaults to: true)

    true (default) = exact search, false = "sub-string" search / the value is included in the encoded value

Returns:

  • (Array<String>)

    all matching source characters



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/unisec/hexdump.rb', line 98

def self.reverse(hexbytes, enc, exact: true)
  chars = []
  (0x000000..0x10FFFF).each do |i|
    char = i.chr(Unisec::Utils::Arguments.argenc2enc(enc, target: 'class'))
    encoded_value = Unisec::Hexdump.send(enc, char).delete(' ')
    if exact && encoded_value == hexbytes # exact match
      chars << char
      break
    elsif !exact && encoded_value.include?(hexbytes) # includes value
      chars << char
    end
  rescue RangeError # skip invalid code points for selected encoding
    next
  end
  chars
end

.utf16be(str) ⇒ String

Encode to UTF-16BE in hexdump format (spaced at every code unit = every 2 bytes)

Examples:

Unisec::Hexdump.utf16be('๐Ÿ‹') # => "d83d dc0b"

Parameters:

  • str (String)

    Input string to encode

Returns:

  • (String)

    hexdump (UTF-16BE encoded)



57
58
59
# File 'lib/unisec/hexdump.rb', line 57

def self.utf16be(str)
  str.encode('UTF-16BE').to_hex.scan(/.{4}/).join(' ')
end

.utf16le(str) ⇒ String

Encode to UTF-16LE in hexdump format (spaced at every code unit = every 2 bytes)

Examples:

Unisec::Hexdump.utf16le('๐Ÿ‹') # => "3dd8 0bdc"

Parameters:

  • str (String)

    Input string to encode

Returns:

  • (String)

    hexdump (UTF-16LE encoded)



66
67
68
# File 'lib/unisec/hexdump.rb', line 66

def self.utf16le(str)
  str.encode('UTF-16LE').to_hex.scan(/.{4}/).join(' ')
end

.utf32be(str) ⇒ String

Encode to UTF-32BE in hexdump format (spaced at every code unit = every 4 bytes)

Examples:

Unisec::Hexdump.utf32be('๐Ÿ‹') # => "0001f40b"

Parameters:

  • str (String)

    Input string to encode

Returns:

  • (String)

    hexdump (UTF-32BE encoded)



75
76
77
# File 'lib/unisec/hexdump.rb', line 75

def self.utf32be(str)
  str.encode('UTF-32BE').to_hex.scan(/.{8}/).join(' ')
end

.utf32le(str) ⇒ String

Encode to UTF-32LE in hexdump format (spaced at every code unit = every 4 bytes)

Examples:

Unisec::Hexdump.utf32le('๐Ÿ‹') # => "0bf40100"

Parameters:

  • str (String)

    Input string to encode

Returns:

  • (String)

    hexdump (UTF-32LE encoded)



84
85
86
# File 'lib/unisec/hexdump.rb', line 84

def self.utf32le(str)
  str.encode('UTF-32LE').to_hex.scan(/.{8}/).join(' ')
end

.utf8(str) ⇒ String

Encode to UTF-8 in hexdump format (spaced at every code unit = every byte)

Examples:

Unisec::Hexdump.utf8('๐Ÿ‹') # => "f0 9f 90 8b"

Parameters:

  • str (String)

    Input string to encode

Returns:

  • (String)

    hexdump (UTF-8 encoded)



48
49
50
# File 'lib/unisec/hexdump.rb', line 48

def self.utf8(str)
  str.encode('UTF-8').to_hex.scan(/.{2}/).join(' ')
end

Instance Method Details

#displayString

Display a CLI-friendly output summurizing the hexdump in all Unicode encodings

Examples:

puts Unisec::Hexdump.new('โ„ช').display # =>
# UTF-8: e2 84 aa
# UTF-16BE: 212a
# UTF-16LE: 2a21
# UTF-32BE: 0000212a
# UTF-32LE: 2a210000

Returns:

  • (String)

    CLI-ready output



124
125
126
127
128
129
130
# File 'lib/unisec/hexdump.rb', line 124

def display
  "UTF-8: #{@utf8}\n" \
    "UTF-16BE: #{@utf16be}\n" \
    "UTF-16LE: #{@utf16le}\n" \
    "UTF-32BE: #{@utf32be}\n" \
    "UTF-32LE: #{@utf32le}"
end