Module: BC3::Helper
Overview
Helper methods to be included to File and Folder.
Class Method Summary collapse
-
.crc32(c) ⇒ Object
Calculate the CRC32 for a string.
Instance Method Summary collapse
-
#fixnum2int16(int) ⇒ Object
Same as Helper#fixnum2int64, but as 2 bytes string.
-
#fixnum2int32(int) ⇒ Object
Same as Helper#fixnum2int64, but as 4 bytes string.
-
#fixnum2int64(int) ⇒ Object
Return a Number (e.g. AD epoch) as a binary string with 8 bytes in little endian order.
-
#shortstring2bcss(shortstring) ⇒ Object
Save a shortstring to bcss.
Class Method Details
.crc32(c) ⇒ Object
Calculate the CRC32 for a string.
Based on www.ruby-forum.com/topic/179452 (with minor ruby 1.9 adaptions).
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/bc3/helper.rb', line 70 def self.crc32(c) #Alternative solution: #~ require 'zlib' #~ return Zlib.crc32(c, 0) #Encoding of c should be binary or similar. n = c.length r = 0xFFFFFFFF c.each_byte do |i| r ^= i 8.times do if (r & 1)!=0 r = (r>>1) ^ 0xEDB88320 else r >>= 1 end end end r ^ 0xFFFFFFFF end |
Instance Method Details
#fixnum2int16(int) ⇒ Object
Same as Helper#fixnum2int64, but as 2 bytes string.
129 130 131 132 133 134 135 136 137 |
# File 'lib/bc3/helper.rb', line 129 def fixnum2int16( int ) bindata = ''.force_encoding('BINARY') ('%016b' % int).scan(/(\d{8})/).flatten.reverse.each{|b| bindata << b.to_i(2) } raise ArgumentError unless $~.post_match.empty? raise ArgumentError unless bindata.size == 2 #int was too big bindata end |
#fixnum2int32(int) ⇒ Object
Same as Helper#fixnum2int64, but as 4 bytes string.
116 117 118 119 120 121 122 123 124 |
# File 'lib/bc3/helper.rb', line 116 def fixnum2int32( int ) bindata = ''.force_encoding('BINARY') ('%032b' % int).scan(/(\d{8})/).flatten.reverse.each{|b| bindata << b.to_i(2) } raise ArgumentError unless $~.post_match.empty? raise ArgumentError unless bindata.size == 4 #int was too big bindata end |
#fixnum2int64(int) ⇒ Object
Return a Number (e.g. AD epoch) as a binary string with 8 bytes in little endian order.
With
String#<< (Integer)
the integer is a codepoint, the character is added. AD’s epoch isn’t a codepoint, but a binary data. So we need a little conversion, to get AD’s epoch (Bignum) as an bit sequence.
101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/bc3/helper.rb', line 101 def fixnum2int64( int ) bindata = ''.force_encoding('BINARY') #Ugly code, but it works ;-) #.reverse to get "little endian" ('%064b' % int).scan(/(\d{8})/).flatten.reverse.each{|b| bindata << b.to_i(2) } raise ArgumentError unless $~.post_match.empty? raise ArgumentError unless bindata.size == 8 #int was too big bindata end |
#shortstring2bcss(shortstring) ⇒ Object
Save a shortstring to bcss
ShortString:
Variable length single-byte character string (ANSI or UTF-8).
Not null terminated.
UTF-8: If set the snapshot was compressed on a system where the default
character encoding is UTF-8 (Linux, OS X). Filenames, paths, and link targets will all be stored as UTF-8. If this isn’t set the paths are stored using the original OS’s ANSI codepage (Windows). In that case any paths may be stored a second time as UTF-8 in extended headers.
153 154 155 156 157 158 |
# File 'lib/bc3/helper.rb', line 153 def shortstring2bcss(shortstring) bcss = "".force_encoding('BINARY') bcss << shortstring.bytesize bcss << shortstring.dup.force_encoding('binary') bcss end |