yEnc 0.0.28 → 0.0.29
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/y_enc.rb +35 -3
- metadata +1 -1
data/lib/y_enc.rb
CHANGED
@@ -1,5 +1,9 @@
|
|
1
1
|
require 'zlib'
|
2
2
|
|
3
|
+
# yEnc
|
4
|
+
#
|
5
|
+
# This gem allows you to decode and encode files using the yenc standard.
|
6
|
+
|
3
7
|
class YEnc
|
4
8
|
|
5
9
|
attr_reader :filepath, :outputpath, :filename, :filesize, :line
|
@@ -13,6 +17,29 @@ class YEnc
|
|
13
17
|
@crc32.upcase.strip
|
14
18
|
end
|
15
19
|
|
20
|
+
# Encode file into a yenc text file
|
21
|
+
def encode_to_file outputfilename
|
22
|
+
outputfile = File.new(@outputpath + outputfilename, "w")
|
23
|
+
outputfile.puts "=ybegin size=#{File.size?(@filepath)} line=128 name=#{File.basename @filepath}\n"
|
24
|
+
File.open(@filepath,'rb') do |f|
|
25
|
+
until f.eof?
|
26
|
+
#Read in 128 bytes at a time
|
27
|
+
buffer = f.read(128)
|
28
|
+
buffer.each_byte do |byte|
|
29
|
+
char_to_write = (byte + 42) % 256
|
30
|
+
if [0, 10, 13, 61].include?(char_to_write)
|
31
|
+
outputfile.putc '='
|
32
|
+
char_to_write = (char_to_write + 64) % 256
|
33
|
+
end
|
34
|
+
outputfile.putc char_to_write
|
35
|
+
end
|
36
|
+
outputfile.puts "\n"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
outputfile.puts "=yend size=312860 crc32=#{file_crc32(@filepath).upcase}\n"
|
40
|
+
outputfile.close
|
41
|
+
end
|
42
|
+
|
16
43
|
def decode
|
17
44
|
if is_yenc?
|
18
45
|
#Continue decoding
|
@@ -67,12 +94,17 @@ class YEnc
|
|
67
94
|
|
68
95
|
#Does this pass the crc32 check
|
69
96
|
def pass_crc32?
|
70
|
-
|
71
|
-
File.open(@outputpath + @filename, "rb") { |h| f = h.read }
|
72
|
-
crc32 = Zlib.crc32(f,0).to_s(16)
|
97
|
+
crc32 = file_crc32 @outputpath + @filename
|
73
98
|
crc32.eql?(@crc32.downcase.strip)
|
74
99
|
end
|
75
100
|
|
101
|
+
# Get the CRC32 for a file
|
102
|
+
def file_crc32 filepath
|
103
|
+
f = nil
|
104
|
+
File.open( filepath, "rb") { |h| f = h.read }
|
105
|
+
Zlib.crc32(f,0).to_s(16)
|
106
|
+
end
|
107
|
+
|
76
108
|
private
|
77
109
|
|
78
110
|
def is_yenc?
|