Class: FastLib
- Inherits:
-
Object
- Object
- FastLib
- Defined in:
- lib/fastlib.rb
Overview
The FastLib class implements the meat of the FASTLIB archive format
Constant Summary collapse
- VERSION =
"0.0.8"
- FLAG_COMPRESS =
0x01
- FLAG_ENCRYPT =
0x02
- @@cache =
{}
- @@has_zlib =
false
Class Method Summary collapse
-
.cache ⇒ Object
Expose the cache to callers.
-
.create(lib, flags, bdir, *dirs) ⇒ Object
This method provides a way to create a FASTLIB archive programatically, the key arguments are the name of the destination archive, the base directory that should be excluded from the archived path, and finally the list of specific files and directories to include in the archive.
- .decrypt_00000000(data) ⇒ Object
- .decrypt_12345600(data) ⇒ Object
- .encrypt_00000000(data) ⇒ Object
-
.encrypt_12345600(data) ⇒ Object
This is a stub crypto handler that performs a basic XOR operation against a fixed one byte key.
-
.fastlib_filter_decode(lib, buff) ⇒ Object
This method provides compression and encryption capabilities for the fastlib archive format.
-
.fastlib_filter_encode(lib, buff) ⇒ Object
This method provides compression and encryption capabilities for the fastlib archive format.
-
.list(lib) ⇒ Object
This method provides a way to list the contents of an archive file, returning the names only in sorted order.
-
.load(lib, name, noprocess = false) ⇒ Object
This method loads content from a specific archive file by name.
-
.load_cache(lib) ⇒ Object
This method caches the file list and offsets within the archive.
-
.post_process(lib, name, data) ⇒ Object
This method is expands __FILE__ sequences and other inline dynamic constants to map to the correct location.
-
.version ⇒ Object
This method returns the version of the fastlib library.
Class Method Details
.cache ⇒ Object
Expose the cache to callers
273 274 275 |
# File 'lib/fastlib.rb', line 273 def self.cache @@cache end |
.create(lib, flags, bdir, *dirs) ⇒ Object
This method provides a way to create a FASTLIB archive programatically, the key arguments are the name of the destination archive, the base directory that should be excluded from the archived path, and finally the list of specific files and directories to include in the archive.
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/fastlib.rb', line 182 def self.create(lib, flags, bdir, *dirs) head = "" data = "" hidx = 0 didx = 0 bdir = bdir.gsub(/\/$/, '') brex = /^#{Regexp.escape(bdir)}\// if flags.kind_of?(::String) if flags =~ /^0x/ flags = flags.to_i(16) else flags = flags.to_i end end @@cache[lib] = { :fastlib_flags => flags } dirs.each do |dir| ::Find.find(dir).each do |path| next if not ::File.file?(path) name = fastlib_filter_encode( lib, path.sub( brex, "" ) ) buff = "" ::File.open(path, "rb") do |fd| buff = fastlib_filter_encode(lib, fd.read(fd.stat.size)) end head << [ name.length, didx, buff.length, ::File.stat(path).mtime.utc.to_i ].pack("NNNN") head << name hidx = hidx + 16 + name.length data << buff didx = didx + buff.length end end head << [0,0,0].pack("NNN") ::File.open(lib, "wb") do |fd| fd.write("FAST") fd.write( [ head.length, flags ].pack("NN") ) fd.write( head ) fd.write( data ) end end |
.decrypt_00000000(data) ⇒ Object
266 267 268 |
# File 'lib/fastlib.rb', line 266 def self.decrypt_00000000(data) encrypt_00000000(data) end |
.decrypt_12345600(data) ⇒ Object
258 259 260 |
# File 'lib/fastlib.rb', line 258 def self.decrypt_12345600(data) encrypt_00000000(data) end |
.encrypt_00000000(data) ⇒ Object
262 263 264 |
# File 'lib/fastlib.rb', line 262 def self.encrypt_00000000(data) data.unpack("C*").map{ |c| c ^ 0x90 }.pack("C*") end |
.encrypt_12345600(data) ⇒ Object
This is a stub crypto handler that performs a basic XOR operation against a fixed one byte key. The two usable IDs are 12345600 and 00000000
254 255 256 |
# File 'lib/fastlib.rb', line 254 def self.encrypt_12345600(data) encrypt_00000000(data) end |
.fastlib_filter_decode(lib, buff) ⇒ Object
This method provides compression and encryption capabilities for the fastlib archive format.
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/fastlib.rb', line 119 def self.fastlib_filter_decode(lib, buff) if (@@cache[lib][:fastlib_flags] & FLAG_ENCRYPT) != 0 @@cache[lib][:fastlib_decrypt] ||= ::Proc.new do |data| stub = "decrypt_%.8x" % ( @@cache[lib][:fastlib_flags] & 0xfffffff0 ) FastLib.send(stub, data) end buff = @@cache[lib][:fastlib_decrypt].call( buff ) end if (@@cache[lib][:fastlib_flags] & FLAG_COMPRESS) != 0 if not @@has_zlib raise ::RuntimeError, "zlib is required to open this archive" end z = Zlib::Inflate.new buff = z.inflate(buff) buff << z.finish z.close end buff end |
.fastlib_filter_encode(lib, buff) ⇒ Object
This method provides compression and encryption capabilities for the fastlib archive format.
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/fastlib.rb', line 149 def self.fastlib_filter_encode(lib, buff) if (@@cache[lib][:fastlib_flags] & FLAG_COMPRESS) != 0 if not @@has_zlib raise ::RuntimeError, "zlib is required to open this archive" end z = Zlib::Deflate.new buff = z.deflate(buff) buff << z.finish z.close end if (@@cache[lib][:fastlib_flags] & FLAG_ENCRYPT) != 0 @@cache[lib][:fastlib_encrypt] ||= ::Proc.new do |data| stub = "encrypt_%.8x" % ( @@cache[lib][:fastlib_flags] & 0xfffffff0 ) FastLib.send(stub, data) end buff = @@cache[lib][:fastlib_encrypt].call( buff ) end buff end |
.list(lib) ⇒ Object
This method provides a way to list the contents of an archive file, returning the names only in sorted order.
236 237 238 239 |
# File 'lib/fastlib.rb', line 236 def self.list(lib) load_cache(lib) ( @@cache[lib] || {} ).keys.map{|x| x.to_s }.sort.select{ |x| @@cache[lib][x] } end |
.load(lib, name, noprocess = false) ⇒ Object
This method loads content from a specific archive file by name. If the noprocess argument is set to true, the contents will not be expanded to include workarounds for things such as __FILE__. This is useful when loading raw binary data where these strings may occur
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/fastlib.rb', line 62 def self.load(lib, name, noprocess=false) data = "" load_cache(lib) return if not ( @@cache[lib] and @@cache[lib][name] ) ::File.open(lib, "rb") do |fd| fd.seek( @@cache[lib][:fastlib_header][0] + @@cache[lib][:fastlib_header][1] + @@cache[lib][name][0] ) data = fastlib_filter_decode( lib, fd.read(@@cache[lib][name][1] )) end # Return the contents in raw or processed form noprocess ? data : post_process(lib, name, data) end |
.load_cache(lib) ⇒ Object
This method caches the file list and offsets within the archive
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/fastlib.rb', line 85 def self.load_cache(lib) return if @@cache[lib] @@cache[lib] = {} return if not ::File.exists?(lib) ::File.open(lib, 'rb') do |fd| dict = {} head = fd.read(4) return if head != "FAST" hlen = fd.read(4).unpack("N")[0] flag = fd.read(4).unpack("N")[0] @@cache[lib][:fastlib_header] = [12, hlen, fd.stat.mtime.utc.to_i ] @@cache[lib][:fastlib_flags] = flag nlen, doff, dlen, tims = fd.read(16).unpack("N*") while nlen > 0 name = fastlib_filter_decode( lib, fd.read(nlen) ) dict[name] = [doff, dlen, tims] nlen, doff, dlen, tims = fd.read(16).unpack("N*") end @@cache[lib].merge!(dict) end end |
.post_process(lib, name, data) ⇒ Object
This method is expands __FILE__ sequences and other inline dynamic constants to map to the correct location.
245 246 247 |
# File 'lib/fastlib.rb', line 245 def self.post_process(lib, name, data) data.gsub('__FILE__', "'#{ ::File.(::File.join(::File.dirname(lib), name)) }'") end |
.version ⇒ Object
This method returns the version of the fastlib library
52 53 54 |
# File 'lib/fastlib.rb', line 52 def self.version VERSION end |