Class: GoogleSafeBrowsing::BinaryHelper
- Inherits:
-
Object
- Object
- GoogleSafeBrowsing::BinaryHelper
- 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
-
.four_as_hex(string) ⇒ String
Returns the first four bytes of
stringas hexidecimal. -
.hex_to_bin(hex) ⇒ String
Pack a Hex String into binary.
-
.read_bytes_as_hex(iter, count) ⇒ String
Reads
counterbyes from byte iteratoriterand returns the hex string represnetation. -
.read_bytes_from(iter, count) ⇒ String
Read
countbytes fromiterwithout unpacking the result. -
.unpack_add_chunk_num(bin) ⇒ String
Unpack string as big-endian network byte order.
-
.unpack_count(bin) ⇒ String
Unpack string as an unsigned integer; for count.
-
.unpack_host_key(bin) ⇒ String
Returns the first four bytes of
stringas hexidecimal; for host key.
Class Method Details
.four_as_hex(string) ⇒ String
Returns the first four bytes of string as hexidecimal
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
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
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
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
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
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
41 42 43 |
# File 'lib/google_safe_browsing/binary_helper.rb', line 41 def self.unpack_host_key(bin) bin.unpack('H8')[0] end |