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.7"
FLAG_COMPRESS =
0x01
FLAG_ENCRYPT =
0x02
@@cache =
{}
@@has_zlib =
false

Class Method Summary collapse

Class Method Details

.cacheObject

Expose the cache to callers



274
275
276
# File 'lib/fastlib.rb', line 274

def self.cache
	@@cache
end

.decrypt_00000000(data) ⇒ Object



267
268
269
# File 'lib/fastlib.rb', line 267

def self.decrypt_00000000(data)
	encrypt_00000000(data)
end

.decrypt_12345600(data) ⇒ Object



259
260
261
# File 'lib/fastlib.rb', line 259

def self.decrypt_12345600(data)
	encrypt_00000000(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.



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

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_00000000(data) ⇒ Object



263
264
265
# File 'lib/fastlib.rb', line 263

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



255
256
257
# File 'lib/fastlib.rb', line 255

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.



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

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.



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

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.



237
238
239
240
# File 'lib/fastlib.rb', line 237

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



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/fastlib.rb', line 70

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



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/fastlib.rb', line 93

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.



246
247
248
# File 'lib/fastlib.rb', line 246

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



60
61
62
# File 'lib/fastlib.rb', line 60

def self.version
	VERSION
end