Class: YEnc
- Inherits:
-
Object
- Object
- YEnc
- 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
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
-
#filepath ⇒ Object
readonly
Returns the value of attribute filepath.
-
#filesize ⇒ Object
readonly
Returns the value of attribute filesize.
-
#line ⇒ Object
readonly
Returns the value of attribute line.
-
#outputpath ⇒ Object
readonly
Returns the value of attribute outputpath.
Instance Method Summary collapse
- #crc32 ⇒ Object
- #decode ⇒ Object
-
#encode_to_file(outputfilename) ⇒ Object
Encode file into a yenc text file.
-
#file_crc32(filepath) ⇒ Object
Get the CRC32 for a file.
-
#initialize(filepath, outputpath) ⇒ YEnc
constructor
A new instance of YEnc.
-
#pass_crc32? ⇒ Boolean
Does this pass the crc32 check.
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
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
9 10 11 |
# File 'lib/y_enc.rb', line 9 def filename @filename end |
#filepath ⇒ Object (readonly)
Returns the value of attribute filepath.
9 10 11 |
# File 'lib/y_enc.rb', line 9 def filepath @filepath end |
#filesize ⇒ Object (readonly)
Returns the value of attribute filesize.
9 10 11 |
# File 'lib/y_enc.rb', line 9 def filesize @filesize end |
#line ⇒ Object (readonly)
Returns the value of attribute line.
9 10 11 |
# File 'lib/y_enc.rb', line 9 def line @line end |
#outputpath ⇒ Object (readonly)
Returns the value of attribute outputpath.
9 10 11 |
# File 'lib/y_enc.rb', line 9 def outputpath @outputpath end |
Instance Method Details
#crc32 ⇒ Object
16 17 18 |
# File 'lib/y_enc.rb', line 16 def crc32 @crc32.upcase.strip end |
#decode ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 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 |
# File 'lib/y_enc.rb', line 43 def decode if is_yenc? #Continue decoding begin_read = false File.open(@filepath, 'r').each_line do |line| if line.include?("=ybegin") #This is the begin size breakdown_header line begin_read = true next end if line.include?("=yend") breakdown_endline line begin_read=false break #stop looking through the file we are done end #end of reading lines if begin_read #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 end end @new_file.close else false end 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 |
# 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" 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 [0, 10, 13, 61].include?(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
102 103 104 105 106 |
# File 'lib/y_enc.rb', line 102 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
96 97 98 99 |
# File 'lib/y_enc.rb', line 96 def pass_crc32? crc32 = file_crc32 @outputpath + @filename crc32.eql?(@crc32.downcase.strip) end |