Class: FastLib

Inherits:
Object
  • Object
show all
Defined in:
lib/fastlib.rb

Overview

The FastLib class implements the meat of the FASTLIB archive format

Constant Summary collapse

VERSION =
"0.0.5"
FLAG_COMPRESS =
0x01
FLAG_ENCRYPT =
0x02
@@cache =
{}
@@has_zlib =
false

Class Method Summary collapse

Class Method Details

.cacheObject



324
325
326
# File 'lib/fastlib.rb', line 324

def self.cache
	@@cache
end

.decrypt_12345600(data) ⇒ Object



320
321
322
# File 'lib/fastlib.rb', line 320

def self.decrypt_12345600(data)
	encrypt_12345600(data)
end

.dump(lib, flag, 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.



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/fastlib.rb', line 252

def self.dump(lib, flag, bdir, *dirs)
	head = ""
	data = ""
	hidx = 0
	didx = 0
	
	bdir = bdir.gsub(/\/$/, '')
	brex = /^#{Regexp.escape(bdir)}\//
	
	@@cache[lib] = {
		:fastlib_flags => flag.to_i(16)
	}
	
	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, flag.to_i(16) ].pack("NN") )
		fd.write( head )
		fd.write( data )
	end	
end

.encrypt_12345600(data) ⇒ Object

This is a stub crypto handler that performs a basic XOR operation against a fixed one byte key



316
317
318
# File 'lib/fastlib.rb', line 316

def self.encrypt_12345600(data)
	data.unpack("C*").map{ |c| c ^ 0x90 }.pack("C*")
end

.fastlib_filter_decode(lib, buff) ⇒ Object

This method provides compression and encryption capabilities for the fastlib archive format.



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
# File 'lib/fastlib.rb', line 189

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.



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/fastlib.rb', line 219

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 archive provides a way to list the contents of an archive file, returning the names only in sorted order.



299
300
301
302
# File 'lib/fastlib.rb', line 299

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



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/fastlib.rb', line 132

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( @@cache[lib][:fastlib_flags], 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



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/fastlib.rb', line 155

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 called on the loaded is required to expand __FILE__ and other inline dynamic constants to map to the correct location.



308
309
310
# File 'lib/fastlib.rb', line 308

def self.post_process(lib, name, data)
	data.gsub('__FILE__', "'#{ ::File.expand_path(::File.join(::File.dirname(lib), name)) }'")
end

.versionObject

This method returns the version of the fastlib library



122
123
124
# File 'lib/fastlib.rb', line 122

def self.version
	VERSION
end