Ruby Compressor (little long)

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::Deflate.deflate(data, Zlib::BEST_COMPRESSION)
end
end

files[:main] = Zlib::Deflate.deflate(IO.read(script),
Zlib::BEST_COMPRESSION)
files[:init] = Zlib::Deflate.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])

I havent personally used this to distribute an application, but it
sounds like it’ll fit the bill:

http://www.erikveen.dds.nl/allinoneruby/index.html

On 8/16/06, Scott [email protected] wrote:

hackers (or even moderately strong Ruby programmers :-), we just do
@init = <<ZLIB_REQUIRE

END
def compress(script)
files[:main] = Zlib::Deflate.deflate(IO.read(script), Zlib::BEST_COMPRESSION)
compress(ARGV[0])

I have used this and rubyscript2exe they are wonderful, but serve a
slightly different purpose. I just want to hide the code a little bit.
In our particular case installing Ruby is not a real problem.

Thanks
pth

This:
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

Can be changed to:
f = Marshal.load(DATA.read)
eval(Zlib::Inflate.inflate(f[:init]))
Kernel.zlib_requires = f
eval(Zlib::Inflate.inflate(f[:main]))
end
END

On 8/17/06, Joey [email protected] wrote:

Can be changed to:
f = Marshal.load(DATA.read)
eval(Zlib::Inflate.inflate(f[:init]))
Kernel.zlib_requires = f
eval(Zlib::Inflate.inflate(f[:main]))
end
END

Thanks, I had tried that – plus a DATA.binmode (without which it did
not work on Windows at all), but I still had a few issues, it failed
in odd ways – I did not spend a lot of time tracking it down as I
knew that reopening the file would work – I will check it again and
see if I can make it work or give more information on the failure.

pth

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs