ripnews/encode/uuencode.rb

187 lines
4.3 KiB
Ruby
Raw Permalink Normal View History

2005-06-06 12:51:08 +00:00
# $Dwarf: uuencode.rb,v 1.7 2004/06/16 08:14:50 ward Exp $
2003-04-18 22:53:25 +00:00
# $Source$
2003-07-20 20:32:24 +00:00
#
# Copyright (c) 2002, 2003 Ward Wouts <ward@wouts.nl>
2003-04-18 22:53:25 +00:00
#
2003-07-20 20:32:24 +00:00
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
2003-04-18 22:53:25 +00:00
#
2003-07-20 20:32:24 +00:00
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2003-04-18 22:53:25 +00:00
#
require 'tempfile'
class UUEncode
class << self
2003-04-18 22:53:25 +00:00
Debuglevel = 0
def uudecode(data, outfile=nil)
2004-06-16 08:17:48 +00:00
case data.class.to_s
2003-04-18 22:53:25 +00:00
when "Array"
2008-02-06 15:41:22 +00:00
puts "Calling _uudecode_array" if Debuglevel>0
2003-04-18 22:53:25 +00:00
mode, filename, body = _uudecode_array(data)
when "File", "Tempfile"
unless outfile
2008-02-06 15:41:22 +00:00
puts "uudecode: need outfile"
2003-04-18 22:53:25 +00:00
exit
end
2008-02-06 15:41:22 +00:00
puts "Calling _uudecode_file" if Debuglevel>0
2003-04-18 22:53:25 +00:00
mode, filename, body = _uudecode_file(data, outfile)
else
2008-02-06 15:41:22 +00:00
puts "Funny stuff in uudecode. Data of class \"#{data.class.to_s}\""
2003-04-18 22:53:25 +00:00
end
return mode, filename, body
end
def _uudecode_file(file, outfile)
mode = 0600
filename = "unknown"
c = 0
2003-04-20 16:58:07 +00:00
lines = file.pos # horrible assumption FH is at end of file
2003-04-18 22:53:25 +00:00
percent = 0
mark = lines/100
2003-04-20 16:58:07 +00:00
file.pos = 0
2003-04-18 22:53:25 +00:00
while (! file.eof)
line = file.gets
print "line: #{line}" if Debuglevel > 0
if line =~ /^begin(.*)/
m = $1
2008-02-06 15:41:22 +00:00
puts "beginning matched; rest: #{m}" if Debuglevel > 0
2014-10-31 19:33:29 +00:00
if m.match(/^(\s+(\d+))?(\s+(.*?\S))?\s*\Z/)
2003-04-18 22:53:25 +00:00
mode = $2
filename = $4
2008-02-06 15:41:22 +00:00
puts "found beginning" if Debuglevel > 0
2003-04-18 22:53:25 +00:00
else
2008-02-06 15:41:22 +00:00
puts "mode, file set to defaults: #{m}"
2003-04-18 22:53:25 +00:00
end
break
end
end
if file.eof
2008-02-06 15:41:22 +00:00
puts "Not UUencoded!"
2003-04-18 22:53:25 +00:00
return false
end
2008-02-06 15:41:22 +00:00
puts "c: #{c} mark: #{mark} lines: #{lines}" if Debuglevel > 1
2003-04-18 22:53:25 +00:00
while (! file.eof)
if Debuglevel > 1
c = file.pos
if c > mark
2008-02-06 15:41:22 +00:00
puts "#{percent}%"
puts "c: #{c} mark: #{mark} lines: #{lines}" if Debuglevel > 1
2003-04-18 22:53:25 +00:00
percent += 1
mark = (lines/100)*(percent+1)
end
end
line = file.gets
print "line: #{line}" if Debuglevel > 1
return mode, filename if line =~ /^end/
next if line =~ /[a-z]/
next if line == nil
2014-10-31 19:33:29 +00:00
next unless ((((line[0].ord - 32) & 077) + 2) / 3).to_i == (line.length/4).to_i
line.unpack("u").each{|x| outfile.print x}
2003-04-18 22:53:25 +00:00
end
2008-02-06 15:41:22 +00:00
puts "No \"end\" found!!!"
2003-04-18 22:53:25 +00:00
#return mode, file, outfile
return false
end
# gaat volgens mij niet verder als er meerdere uuencoded blocks zijn...
# zal dan meerdere keren aangeroepen moeten worden, grmbl...
# tis getting a mess as we speak...
# toch maar een keer aparte class van maken...
def _uudecode_array(data)
decode = []
mode = 0600
filename = "unknown"
c = 0
lines = data.length
percent = 0
mark = lines/100
i = 0
while (i < data.length)
if data[i] =~ /^begin(.*)/
m = $1
2008-02-06 15:41:22 +00:00
puts "beginning matched; rest: #{m}" if Debuglevel > 0
2014-10-31 19:33:29 +00:00
if m.match(/^(\s+(\d+))?(\s+(.*?\S))?\s*\Z/)
2003-04-18 22:53:25 +00:00
mode = $2
filename = $4
2008-02-06 15:41:22 +00:00
puts "found beginning" if Debuglevel > 0
2003-04-18 22:53:25 +00:00
else
2008-02-06 15:41:22 +00:00
puts "mode, filename set to defaults: #{m}"
2003-04-18 22:53:25 +00:00
end
break
end
i += 1
end
unless (i < data.length)
2008-02-06 15:41:22 +00:00
puts "Not UUencoded!"
2003-04-18 22:53:25 +00:00
return false
end
while (i < data.length)
if Debuglevel > 1
if c > mark
2008-02-06 15:41:22 +00:00
puts "#{percent}%"
puts "c: #{c} mark: #{mark} lines: #{lines} i: #{i}" if Debuglevel > 1
2003-04-18 22:53:25 +00:00
percent += 1
mark = (lines/100)*(percent+1)
end
c += 1
end
line = data[i]
i += 1
return mode, filename, decode if line =~ /^end/
next if line =~ /[a-z]/
next if line == nil
2005-06-06 12:51:08 +00:00
begin
2014-10-31 19:33:29 +00:00
next unless ((((line[0].ord - 32) & 077) + 2) / 3).to_i == (line.length/4).to_i
2005-06-06 12:51:08 +00:00
rescue NoMethodError
return false
end
2003-04-20 18:02:51 +00:00
unless line.unpack("u").eql?([""])
decode.concat(line.unpack("u"))
end
2003-04-18 22:53:25 +00:00
end
2008-02-06 15:41:22 +00:00
puts "No \"end\" found!!!"
2003-04-18 22:53:25 +00:00
return false
end
def is_uuencoded(data)
if data.to_s =~ /begin\s+\d+?\s+.*?\S?\s*$/m
return true
2003-04-18 22:53:25 +00:00
else
return false
end
end
def get_filename(data)
2003-04-20 20:15:34 +00:00
i = 0
while i < data.length
line = data[i]
if line =~ /^begin(\s+(\d+))?(\s+(.*?\S))?\s*$/m
return $4
end
i += 1
2003-04-18 22:53:25 +00:00
end
return false
2003-04-18 22:53:25 +00:00
end
end # class
end