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
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
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
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
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
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
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
36 37 38 |
# File 'lib/google_safe_browsing/binary_helper.rb', line 36 def self.unpack_host_key(bin) bin.unpack('H8')[0] end |