Class: GoogleSafeBrowsing::BinaryHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/google_safe_browsing/binary_helper.rb

Overview

Helper methods for working with binary encoded data from Forwarding URLs

Class Method Summary collapse

Class Method Details

.four_as_hex(string) ⇒ String

Returns the first four bytes of string as hexidecimal

Parameters:

  • string (String)

    to unpack the first four bytes as hex

Returns:

  • (String)

    hexidecimal string



18
19
20
# File 'lib/google_safe_browsing/binary_helper.rb', line 18

def self.four_as_hex(string)
  string.unpack('H8')[0]
end

.hex_to_bin(hex) ⇒ String

Pack a Hex String into binary

Parameters:

  • hex (String)

    string to encode

Returns:

  • (String)

    encoded string



66
67
68
# File 'lib/google_safe_browsing/binary_helper.rb', line 66

def self.hex_to_bin(hex)
  [hex].pack('H*')
end

.read_bytes_as_hex(iter, count) ⇒ String

Reads counter byes from byte iterator iter and returns the hex string represnetation

Parameters:

  • iter (ByteIterator)

    byte iterator already at correct position

  • count (Integer)

    number of bytes to read

Returns:

  • (String)

    hexidecimal string



10
11
12
# File 'lib/google_safe_browsing/binary_helper.rb', line 10

def self.read_bytes_as_hex(iter, count)
  read_bytes_from(iter, count).unpack("H#{count * 2}")[0]
end

.read_bytes_from(iter, count) ⇒ String

Read count bytes from iter without unpacking the result

Parameters:

  • iter (ByteIterator)

    byte iterator already at correct position

  • count (Integer)

    number of bytes to read

Returns:

  • (String)

    not unpacked string from iter



27
28
29
30
31
32
33
34
35
36
# File 'lib/google_safe_browsing/binary_helper.rb', line 27

def self.read_bytes_from(iter, count)
  iter = iter.to_enum if iter.is_a?(Array)

  ret = ''
  count.to_i.times { ret << iter.next }
  ret
   #rescue
   #  puts "Tried to read past chunk iterator++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
   #  return nil
end

.unpack_add_chunk_num(bin) ⇒ String

Unpack string as big-endian network byte order

Parameters:

  • bin (String)

    string to unpack

Returns:

  • (String)

    unpacked string



58
59
60
# File 'lib/google_safe_browsing/binary_helper.rb', line 58

def self.unpack_add_chunk_num(bin)
  bin.unpack('N')[0]
end

.unpack_count(bin) ⇒ String

Unpack string as an unsigned integer; for count

Parameters:

  • bin (String)

    string to unpack

Returns:

  • (String)

    unpacked string



49
50
51
52
# File 'lib/google_safe_browsing/binary_helper.rb', line 49

def self.unpack_count(bin)
  # this may not be correct
  bin.unpack('U')[0]
end

.unpack_host_key(bin) ⇒ String

Returns the first four bytes of string as hexidecimal; for host key

Parameters:

  • bin (String)

    string to unpack

Returns:

  • (String)

    unpacked string



41
42
43
# File 'lib/google_safe_browsing/binary_helper.rb', line 41

def self.unpack_host_key(bin)
  bin.unpack('H8')[0]
end