yEnc 0.0.28 → 0.0.29

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. data/lib/y_enc.rb +35 -3
  2. metadata +1 -1
@@ -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
- f = nil
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?
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yEnc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.28
4
+ version: 0.0.29
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: