Remove dependency on base64 - close #177

This commit is contained in:
josh committed 2025-06-08 02:48:03 -04:00
1 parent 1a280a8994
commit 18a2a075c1
4 files changed
+51 -20

No files matched your search

+45 -10
View File
@@ -1,7 +1,10 @@
#!/usr/bin/env ruby
require "fileutils"
require "base64"
require "digest/md5"
require "fileutils"
require "stringio"
require "zlib"
if File.read("lib/rscons/version.rb") =~ /VERSION = "(.+)"/
VERSION = $1
@@ -68,11 +71,15 @@ license = File.read("LICENSE.txt").gsub(/^(.*?)$/) do |line|
end
end
require "zlib"
require "base64"
compressed_script = Zlib::Deflate.deflate(stripped.join)
hash = Digest::MD5.hexdigest(compressed_script)
encoded_compressed_script = Base64.encode64(compressed_script).gsub("\n", "")
hash = Digest::MD5.hexdigest(encoded_compressed_script)
encoded_compressed_script_io = StringIO.new(encoded_compressed_script)
commented_encoded_compressed_script = ""
until encoded_compressed_script_io.eof?
line = encoded_compressed_script_io.read(64)
commented_encoded_compressed_script += "##{line}\n"
end
FileUtils.rm_rf(DIST)
FileUtils.mkdir_p(DIST)
@@ -82,25 +89,53 @@ File.open("#{DIST}/#{PROG_NAME}", "wb", 0755) do |fh|
#{license}
BASE64CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
def base64_decode(s)
out = ""
v = 0
bits = 0
s.each_char do |c|
if cv = BASE64CHARS.index(c)
v = (v << 6) | cv
bits += 6
elsif c == "="
break
end
if bits >= 8
out += (v >> (bits - 8)).chr
v &= 0xFFFFFFFF >> (32 - (bits - 8))
bits -= 8
end
end
out
end
script = File.join(File.dirname(__FILE__), ".rscons-#{VERSION}-#{hash}.rb")
unless File.exist?(script)
if File.read(__FILE__, mode: "rb") =~ /^#==>(.*)/
if File.read(__FILE__, mode: "rb") =~ /^#==>(.*)/m
require "zlib"
require "base64"
encoded_compressed = $1
unescaped_compressed = Base64.decode64(encoded_compressed)
inflated = Zlib::Inflate.inflate(unescaped_compressed)
compressed = base64_decode(encoded_compressed)
if ENV["rscons_dist_specs"]
require "digest/md5"
if Digest::MD5.hexdigest(compressed) != "#{hash}"
raise "Hash mismatch when decompressing rscons executable"
end
end
inflated = Zlib::Inflate.inflate(compressed)
File.open(script, "wb") do |fh|
fh.write(inflated)
end
else
raise "Could not decompress."
raise "Error expanding rscons executable"
end
end
load script
if __FILE__ == $0
Rscons::Cli.new.run(ARGV)
end
#==>#{encoded_compressed_script}
#==>
#{commented_encoded_compressed_script}
EOF
end