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



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

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



61
62
63
# File 'lib/google_safe_browsing/binary_helper.rb', line 61

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



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

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`



25
26
27
28
29
30
31
# File 'lib/google_safe_browsing/binary_helper.rb', line 25

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
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



53
54
55
# File 'lib/google_safe_browsing/binary_helper.rb', line 53

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



44
45
46
47
# File 'lib/google_safe_browsing/binary_helper.rb', line 44

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



36
37
38
# File 'lib/google_safe_browsing/binary_helper.rb', line 36

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