Class: YEnc

Inherits:
Object
  • Object
show all
Defined in:
lib/y_enc.rb

Overview

yEnc

This gem allows you to decode and encode files using the yenc standard.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filepath, outputpath) ⇒ YEnc

Returns a new instance of YEnc.



11
12
13
14
# File 'lib/y_enc.rb', line 11

def initialize filepath, outputpath
  @filepath = filepath
  @outputpath = outputpath
end

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



9
10
11
# File 'lib/y_enc.rb', line 9

def filename
  @filename
end

#filepathObject (readonly)

Returns the value of attribute filepath.



9
10
11
# File 'lib/y_enc.rb', line 9

def filepath
  @filepath
end

#filesizeObject (readonly)

Returns the value of attribute filesize.



9
10
11
# File 'lib/y_enc.rb', line 9

def filesize
  @filesize
end

#lineObject (readonly)

Returns the value of attribute line.



9
10
11
# File 'lib/y_enc.rb', line 9

def line
  @line
end

#outputpathObject (readonly)

Returns the value of attribute outputpath.



9
10
11
# File 'lib/y_enc.rb', line 9

def outputpath
  @outputpath
end

Instance Method Details

#crc32Object



16
17
18
# File 'lib/y_enc.rb', line 16

def crc32
  @crc32.upcase.strip
end

#decodeObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/y_enc.rb', line 68

def decode
  if is_yenc?
          #Continue decoding
    begin_read = false

    File.open(@filepath, 'r').each_line do |line|

      if begin_read
        if line.start_with?("=yend")
          breakdown_endline line
          begin_read=false
          break #stop looking through the file we are done
        end
        #end of reading lines

        #puts "LINE COUNT: #{line.length}"
        #Decode and write to binary file
        esc = false
        line.each_byte do |c|
          next if c == 13 or c == 10

          if c == 61 and not esc #escape character hit goto the next one
            esc = true
            next
          else
            if esc
              esc = false
              c = c - 64
            end

            if c.between?(0,41)
              decoded = c + 214
            else
              decoded = c - 42
            end
          end
          @new_file.putc decoded
        end

      else
        if line.start_with?("=ybegin") #This is the begin size
          breakdown_header line
          begin_read = true
          next
        end
      end
    end
  else
    false
  end
  @new_file.close
end

#encodeObject

method only encodes given file and returns yenc encoded string; nothing more, nothing less Author: Tadeus Dobrovolskij



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/y_enc.rb', line 46

def encode
  sio = StringIO.new("","w:ASCII-8BIT")
  special = { 0 => nil, 10 => nil, 13 => nil, 61 => nil }
  File.open(@filepath,'rb') do |b|
    until b.eof?
      buffer = b.read(128)
      buffer.each_byte do |byte|
        char_to_write = (byte + 42) % 256
        if special.has_key?(char_to_write)
          sio.putc '='
          char_to_write = (char_to_write + 64) % 256
        end
        sio.putc char_to_write
      end
      sio.puts "\n"
    end
  end
  result = sio.string
  sio.close
  return result
end

#encode_to_file(outputfilename) ⇒ Object

Encode file into a yenc text file



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/y_enc.rb', line 21

def encode_to_file outputfilename
  outputfile = File.new(@outputpath + outputfilename, "w")
  outputfile.puts "=ybegin size=#{File.size?(@filepath)} line=128 name=#{File.basename @filepath}\n"
  special = { 0 => nil, 10 => nil, 13 => nil, 61 => nil }
  File.open(@filepath,'rb') do |f|
    until f.eof?
      #Read in 128 bytes at a time
      buffer = f.read(128)
      buffer.each_byte do |byte|
        char_to_write = (byte + 42) % 256
        if special.has_key?(char_to_write)
          outputfile.putc '='
          char_to_write = (char_to_write + 64) % 256
        end
        outputfile.putc char_to_write
      end
      outputfile.puts "\n"
    end
  end
  outputfile.puts "=yend size=312860 crc32=#{file_crc32(@filepath).upcase}\n"
  outputfile.close
end

#file_crc32(filepath) ⇒ Object

Get the CRC32 for a file



128
129
130
131
132
# File 'lib/y_enc.rb', line 128

def file_crc32 filepath
  f = nil
  File.open( filepath, "rb") { |h| f = h.read }
  Zlib.crc32(f,0).to_s(16)
end

#pass_crc32?Boolean

Does this pass the crc32 check

Returns:

  • (Boolean)


122
123
124
125
# File 'lib/y_enc.rb', line 122

def pass_crc32?
  crc32 = file_crc32 @outputpath + @filename
  crc32.eql?(@crc32.downcase.strip)
end