P
Patrick Hurley
I have need to distribute applications to customers. The customers
have every right to the source code, but when it is installed we do
not want the code to be easily visible. We are not worried about super
hackers (or even moderately strong Ruby programmers , we just do
not want our raw source code on the system calling out to be played
with by the end users.
To this end, I have developed this very simple script, which I plan on
further refining. I know there are many rough edges, but I wanted to
see if anyone else was interested in the code (or had any other
suggestions), before I went any further.
require "zlib"
@init = <<ZLIB_REQUIRE
module Kernel
alias default_require require
@@zlib_requires = {}
def zlib_requires=(zr)
@@zlib_requires = zr
end
def require(path)
return false if $".include?(path)
if @@zlib_requires.has_key(path)
eval(Zlib::Inflate.inflate(@@zlib_requires[path]))
$" << path
else
default_require path
end
end
end
ZLIB_REQUIRE
@bootstrap = <<BOOTSTRAP
require "zlib"
File.open(__FILE__, "rb") do |data|
# skip to the end
line = data.gets until line =~ /^__END__$/
f = Marshal.load(data)
eval(Zlib::Inflate.inflate(f[:init]))
Kernel.zlib_requires = f
eval(Zlib::Inflate.inflate(f[:main]))
end
__END__
BOOTSTRAP
def findrb(fname)
$:.each do |path|
fp = File.join(path, fname)
return fp if File.exists? fp
end
false
end
def compress(script)
files = {}
load(script) rescue nil
$".each do |fname|
if (File.extname(fname) == ".rb") && (fp = findrb(fname))
data = IO.read(fp)
files[fname] = Zlib:eflate.deflate(data, Zlib::BEST_COMPRESSION)
end
end
files[:main] = Zlib:eflate.deflate(IO.read(script), Zlib::BEST_COMPRESSION)
files[:init] = Zlib:eflate.deflate(@init, Zlib::BEST_COMPRESSION)
File.open("compress-#{script}", "wb") do |io|
io.print @bootstrap
io.write(Marshal.dump(files))
end
# write header data - fname,
end
compress(ARGV[0])
have every right to the source code, but when it is installed we do
not want the code to be easily visible. We are not worried about super
hackers (or even moderately strong Ruby programmers , we just do
not want our raw source code on the system calling out to be played
with by the end users.
To this end, I have developed this very simple script, which I plan on
further refining. I know there are many rough edges, but I wanted to
see if anyone else was interested in the code (or had any other
suggestions), before I went any further.
require "zlib"
@init = <<ZLIB_REQUIRE
module Kernel
alias default_require require
@@zlib_requires = {}
def zlib_requires=(zr)
@@zlib_requires = zr
end
def require(path)
return false if $".include?(path)
if @@zlib_requires.has_key(path)
eval(Zlib::Inflate.inflate(@@zlib_requires[path]))
$" << path
else
default_require path
end
end
end
ZLIB_REQUIRE
@bootstrap = <<BOOTSTRAP
require "zlib"
File.open(__FILE__, "rb") do |data|
# skip to the end
line = data.gets until line =~ /^__END__$/
f = Marshal.load(data)
eval(Zlib::Inflate.inflate(f[:init]))
Kernel.zlib_requires = f
eval(Zlib::Inflate.inflate(f[:main]))
end
__END__
BOOTSTRAP
def findrb(fname)
$:.each do |path|
fp = File.join(path, fname)
return fp if File.exists? fp
end
false
end
def compress(script)
files = {}
load(script) rescue nil
$".each do |fname|
if (File.extname(fname) == ".rb") && (fp = findrb(fname))
data = IO.read(fp)
files[fname] = Zlib:eflate.deflate(data, Zlib::BEST_COMPRESSION)
end
end
files[:main] = Zlib:eflate.deflate(IO.read(script), Zlib::BEST_COMPRESSION)
files[:init] = Zlib:eflate.deflate(@init, Zlib::BEST_COMPRESSION)
File.open("compress-#{script}", "wb") do |io|
io.print @bootstrap
io.write(Marshal.dump(files))
end
# write header data - fname,
end
compress(ARGV[0])