split off ydecoding stuff
This commit is contained in:
parent
cf34fe16c8
commit
7a09a4634e
2 changed files with 20 additions and 318 deletions
|
|
@ -1,6 +1,6 @@
|
|||
#################################
|
||||
#
|
||||
# $Dwarf: article.rb,v 1.59 2002/11/05 10:29:51 ward Exp $
|
||||
# $Dwarf: article.rb,v 1.60 2003/04/20 16:34:40 ward Exp $
|
||||
# $Source$
|
||||
#
|
||||
# article.rb
|
||||
|
|
@ -49,7 +49,6 @@ def initialize(nntpservers, groupname, newsrc="~/.newsrc")
|
|||
del_server(server)
|
||||
end
|
||||
}
|
||||
@ymap = {}
|
||||
end
|
||||
|
||||
def reconnect(server)
|
||||
|
|
@ -527,311 +526,6 @@ def save_cache(cachedir)
|
|||
end
|
||||
end
|
||||
|
||||
#######################################################################
|
||||
|
||||
def ydecode(data, outfile=nil)
|
||||
if @ymap.empty?
|
||||
(-106..255).each do |b|
|
||||
@ymap[b]=((b-42)%256)
|
||||
end
|
||||
end
|
||||
|
||||
case data.type.to_s
|
||||
when "Array"
|
||||
print "Calling _ydecode_array\n" if Debuglevel>0
|
||||
mode, filename, body = _ydecode_array(data)
|
||||
when "File", "Tempfile"
|
||||
unless outfile
|
||||
print "ydecode: need outfile\n"
|
||||
exit
|
||||
end
|
||||
print "Calling _ydecode_file\n" if Debuglevel>0
|
||||
mode, filename, body = _ydecode_file(data, outfile)
|
||||
else
|
||||
print "Funny stuff in ydecode. Data of type \"#{data.type.to_s}\"\n"
|
||||
end
|
||||
return mode, filename, body
|
||||
end
|
||||
|
||||
def _ydecode_line(line)
|
||||
i = 0
|
||||
ll = line.length
|
||||
ostr = ''
|
||||
while i < ll
|
||||
if line[i] == 0x3d
|
||||
i += 1
|
||||
line[i] -= 64
|
||||
end
|
||||
ostr << @ymap[line[i]]
|
||||
i += 1
|
||||
end
|
||||
return ostr, ostr.length
|
||||
end
|
||||
|
||||
def _ydecode_file(file, outfile)
|
||||
mode = 0600
|
||||
filename = "unknown"
|
||||
lines = file.pos
|
||||
file.pos = 0
|
||||
bytes = 0
|
||||
total = 0
|
||||
oldpartend = 0
|
||||
search_begin = false
|
||||
|
||||
while (! file.eof)
|
||||
line = file.gets
|
||||
print "line: #{line}" if Debuglevel > 0
|
||||
if line =~ /^\=ybegin\s+(.*line\=.*)/
|
||||
m = $1
|
||||
print "ybegin match; rest: #{m}\n" if Debuglevel > 0
|
||||
if m =~ /^\s*(part\=(\d+)\s+)?(total\=(\d+)\s+)?(line\=(\d+))(\s*size\=(\d+))(\s*name=(.*))\Z/
|
||||
part = $2.to_i
|
||||
total = $4.to_i
|
||||
linesize = $6.to_i
|
||||
totalsize = $8.to_i
|
||||
filename = $10
|
||||
print "found beginning"
|
||||
if part != nil
|
||||
print " of part #{part}"
|
||||
end
|
||||
if total != nil
|
||||
print " of #{total}"
|
||||
end
|
||||
print ", linesize = #{linesize}, size = #{totalsize}, filename = #{filename}\n"
|
||||
break
|
||||
else
|
||||
print "not a valid yenc begin line\n"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if file.eof
|
||||
print "Not yencoded!\n"
|
||||
return false
|
||||
end
|
||||
|
||||
print " ydecoding...\n"
|
||||
|
||||
while (! file.eof)
|
||||
print "at #{file.pos} need to go to #{lines}\n" if Debuglevel > 1
|
||||
line = file.gets
|
||||
line.chop!
|
||||
|
||||
if line =~ /^=yend\s+(.*)\Z/
|
||||
m = $1
|
||||
m =~ /(\s*size=(\d+)\s+)(\s*part=(\d+))?(\s+crc32=(\S+))?/
|
||||
size = $2.to_i
|
||||
part = $4.to_i
|
||||
crc = $6
|
||||
if size != bytes
|
||||
print "part size mismatch, is #{bytes}, should be #{size}\n"
|
||||
end
|
||||
if part == nil
|
||||
return mode, filename
|
||||
end
|
||||
total += bytes
|
||||
if total >= totalsize
|
||||
if total != totalsize
|
||||
print "total size mismatch, is #{total}, should be #{totalsize}\n"
|
||||
end
|
||||
return mode, filename
|
||||
end
|
||||
search_begin = true
|
||||
bytes = 0
|
||||
next
|
||||
end
|
||||
if search_begin && line =~ /^\=ybegin\s+(.*)\Z/
|
||||
m = $1
|
||||
search_begin = false
|
||||
if m =~ /^\s*(part\=(\d+)\s+)?(total\=(\d+)\s+)?(line\=(\d+))(\s*size\=(\d+))(\s*name=(.*))\Z/
|
||||
part = $2.to_i
|
||||
total = $4.to_i
|
||||
linesize = $6.to_i
|
||||
totalsize = $8.to_i
|
||||
filename = $10
|
||||
print "found beginning of part #{part}, linesize = #{linesize}, size = #{totalsize}, filename = #{filename}\n" if Debuglevel > 0
|
||||
end
|
||||
next
|
||||
end
|
||||
if search_begin == true
|
||||
next
|
||||
end
|
||||
if line =~ /^=ypart\s+(\s*begin=(\d+))(\s+end=(\d+))/
|
||||
b = $2
|
||||
e = $4
|
||||
print " next part begin #{b}, end #{e}\n"
|
||||
if b.to_i == oldpartend + 1
|
||||
oldpartend = e.to_i
|
||||
else
|
||||
raise PermError, "Parts not continuous! last end #{oldpartend}, begin #{b}"
|
||||
end
|
||||
next
|
||||
end
|
||||
|
||||
# This seems to be a common 'error' - maybe I misunderstand the spec or
|
||||
# something
|
||||
# if line.length != linesize
|
||||
# print "linesize mismatch, was #{line.length}, should be #{linesize}...\n"
|
||||
# end
|
||||
|
||||
# i = 0
|
||||
# ll = line.length
|
||||
# ostr = ''
|
||||
# while i < ll
|
||||
# if line[i] == 0x3d
|
||||
# i += 1
|
||||
# line[i] -= 64
|
||||
# end
|
||||
# ostr << ymap[line[i]]
|
||||
# i += 1
|
||||
# end
|
||||
ostr, ostrl = _ydecode_line(line)
|
||||
outfile << ostr
|
||||
bytes += ostrl
|
||||
end
|
||||
|
||||
print "No \"=yend\" found!!!\n"
|
||||
return mode, filename, outfile
|
||||
end
|
||||
|
||||
# toch maar een keer aparte class van maken... geld ook voor dit geneuzel
|
||||
def _ydecode_array(data)
|
||||
decode = ""
|
||||
mode = 0600
|
||||
filename = "unknown"
|
||||
c = 0
|
||||
lines = data.length
|
||||
bytes = 0
|
||||
percent = 0
|
||||
mark = lines/100
|
||||
|
||||
# ymap = {}
|
||||
# (-106..255).each do |b|
|
||||
# ymap[b]=((b-42)%256)
|
||||
# end
|
||||
|
||||
i = 0
|
||||
while (i < data.length)
|
||||
if data[i] =~ /^\=ybegin\s+(.*line\=.*)/
|
||||
m = $1
|
||||
print "ybegin match; rest: #{m}\n" if Debuglevel > 0
|
||||
if m =~ /^\s*(part\=(\d+)\s+)?(total\=(\d+)\s+)?(line\=(\d+))(\s*size\=(\d+))(\s*name=(.*))\Z/
|
||||
part = $2.to_i
|
||||
total = $4.to_i
|
||||
linesize = $6.to_i
|
||||
size = $8.to_i
|
||||
filename = $10
|
||||
print "found beginning, linesize = #{linesize}, size = #{size}, filename = #{filename}\n" if Debuglevel > 0
|
||||
i += 1
|
||||
break
|
||||
else
|
||||
print "not a valid yenc begin line\n"
|
||||
end
|
||||
end
|
||||
i += 1
|
||||
end
|
||||
|
||||
unless (i < data.length)
|
||||
print "Not yencoded!\n"
|
||||
return false
|
||||
end
|
||||
|
||||
print "ydecoding...\n"
|
||||
|
||||
while (i < data.length)
|
||||
print "at #{i} need to go to #{data.length}\r" if Debuglevel > 1
|
||||
line = data[i]
|
||||
i += 1
|
||||
if line =~ /^\=yend(\s+size=(\d+))(\s+crc32=(\S+))?/
|
||||
size = $2.to_i
|
||||
crc = $4
|
||||
if size != decode.length
|
||||
print "size mismatch, was #{decode.length}, should be #{size}\n"
|
||||
end
|
||||
dec = [ decode ]
|
||||
return mode, filename, dec
|
||||
end
|
||||
if line =~ /^\=ypart.*\Z/
|
||||
# ignore for now
|
||||
next
|
||||
end
|
||||
|
||||
# This seems to be a common 'error' - maybe I misunderstand the spec or
|
||||
# something
|
||||
# if line.length != linesize
|
||||
# print "#{i}: linesize mismatch, was #{line.length}, should be #{linesize}...\n"
|
||||
# end
|
||||
|
||||
# j = 0
|
||||
# ll = line.length
|
||||
# ostr = ''
|
||||
# while j < ll
|
||||
# if line[j] == 0x3d
|
||||
# j += 1
|
||||
# line[j] -= 64
|
||||
# end
|
||||
# ostr << ymap[line[j]]
|
||||
# j += 1
|
||||
# end
|
||||
# decode << ostr
|
||||
# bytes += ostr.length
|
||||
ostr, ostrl = _ydecode_line(line)
|
||||
decode << ostr
|
||||
bytes += ostrl
|
||||
|
||||
# special = 0
|
||||
# str = ""
|
||||
# line.each_byte { |b|
|
||||
# if special == 0
|
||||
# if b == 0x3d
|
||||
# special = 1
|
||||
# next
|
||||
# end
|
||||
# else
|
||||
# special = 0
|
||||
# b = (b - 64) % 256
|
||||
# end
|
||||
# str << ((b - 42) % 256).chr
|
||||
# }
|
||||
# decode << str
|
||||
end
|
||||
|
||||
print "${i}: no \"=yend\" found!!!\n"
|
||||
dec = [ decode ]
|
||||
return mode, filename, dec
|
||||
end
|
||||
|
||||
def ydecode_group(subj, tempdir=nil)
|
||||
group_subjects unless @grouped
|
||||
|
||||
body = get_group_body_first(subj)
|
||||
if body.to_s =~ /=ybegin/
|
||||
print "yencoded!\n" if Debuglevel > 0
|
||||
#if (file and outfile)
|
||||
if (tempdir != nil)
|
||||
file = Tempfile.new("#{tempdir}/riptmp")
|
||||
body.collect{|i| file.print "#{i}\n"}
|
||||
get_group_body_rest(subj, file)
|
||||
mode, filename, result = ydecode(file, outfile)
|
||||
else
|
||||
body.concat(get_group_body_rest(subj))
|
||||
mode, filename, result = ydecode(body)
|
||||
end
|
||||
return mode, filename, result
|
||||
else
|
||||
print "Not yencoded!\n" if Debuglevel > 0
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
def is_yencoded(data)
|
||||
if data.to_s =~ /=ybegin/
|
||||
return true
|
||||
else
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
###############################################################
|
||||
|
||||
# a bas64 decoder...
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue