This repository has been archived by the owner on May 18, 2021. It is now read-only.
/
fastlib.rb
executable file
·116 lines (91 loc) · 3.23 KB
/
fastlib.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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
114
115
116
#!/usr/bin/env ruby
$:.unshift(File.expand_path(File.join(File.dirname(__FILE__), "..", "lib")))
require 'fastlib'
require 'optparse'
require 'ostruct'
options = OpenStruct.new(action: nil, archive: nil, required: nil, included: nil, compress: nil, encrypt: nil, verbose: nil)
option_parser = OptionParser.new do |opts|
opts.banner = "Usage: #{$0} <action> [options] <arguments>"
opts.separator "Create or list the contents of a fastlib archive"
opts.separator ""
opts.separator "Options"
opts.on("-c", "--compress", "Compresses the archive with Zlib ") do
options.compress = true
end
opts.on("-e", "--encrypt 0x<Algorithm ID>", "Encrypt the archive with the specified algorithm ID (0x00 for XOR)") do |opt_encrypt|
if opt_encrypt =~ /^0x/
options.encrypt = opt_encrypt.to_i(16)
else
options.encrypt = opt_encrypt.to_i
end
if options.encrypt > 0xffffff
$stderr.puts "Error: Algorithm IDs must be between 0 and 0xffffff"
exit(1)
end
end
opts.on("-r", "--require library", "Require the specified library to support custom encryption methods") do |opt_require|
options.required ||= []
options.required << opt_require
end
opts.on("-I", "--include library_path", "Search the specified path for required libraries") do |opt_include|
$:.append opt_include
end
opts.on("-V", "--version", "Show the FastLib version") do
$stderr.puts "FastLib v#{FastLib::VERSION}"
exit(0)
end
opts.on("-h", "--help", "Show this message.") do
$stderr.puts opts
exit(1)
end
end
option_parser.parse!(ARGV)
# Load any libraries required to support custom encryption providers
if options.required
options.required.each {|lname| require(lname) }
end
if options.encrypt
# Verify that our 24-bit algorithm ID maps to a FastLib namespace method
# The user may need to specify -r provider to load the right library first
unless FastLib.respond_to?(sprintf("encrypt_%.8x", (options.encrypt<<8) ))
$stderr.puts "Error: Unknown algorithm ID, you may need to specify the provider library with -r"
exit(1)
end
end
options.action = ARGV.shift
options.archive = ARGV.shift
unless options.archive
$stderr.puts "Error: No archive file has been specified"
exit(1)
end
case options.action
when 'create'
source_path = ARGV.shift
unless source_path
$stderr.puts "Error: No source directory has been specified"
exit(1)
end
source_path = File.expand_path(source_path)
source_files = Dir["#{source_path}/*"]
create_flags = 0
if options.compress
create_flags |= FastLib::FLAG_COMPRESS
end
if options.encrypt
create_flags |= (options.encrypt<<8)
create_flags |= FastLib::FLAG_ENCRYPT
end
FastLib.create(options.archive, create_flags, source_path, *source_files)
when 'list'
$stdout.puts "Library: #{options.archive}"
$stdout.puts "====================================================="
FastLib.list(options.archive).each do |name|
fsize = FastLib.cache[options.archive][name][1]
ftime = ::Time.at(FastLib.cache[options.archive][name][2]).strftime("%Y-%m-%d %H:%M:%S")
$stdout.puts sprintf("%9d\t%20s\t%s\n", fsize, ftime, name)
end
$stdout.puts ""
else
$stderr.puts "Error: Unknown action '#{options.action}'"
exit(1)
end