From: Joey Date: 2006-08-17T16:20:19+09:00 Subject: Re: Ruby Compressor (little long) ------=_Part_10682_12940343.1155799216986 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline 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, Patrick Hurley wrote: > > 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 = < 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 = < 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]) > > ------=_Part_10682_12940343.1155799216986--