summaryrefslogtreecommitdiffstats
path: root/lib/base64.rb
blob: d7461d82e7bf39b8c174b99ea1eba3ff36cf5775 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
require "kconv"

def decode64(str)
  str.unpack("m")[0]
end

def decode_b(str)
  str.gsub!(/=\?ISO-2022-JP\?B\?([!->@-~]+)\?=/i) {
    decode64($1)
  }
  str = Kconv::toeuc(str)
  str.gsub!(/=\?SHIFT_JIS\?B\?([!->@-~]+)\?=/i) {
    decode64($1)
  }
  str = Kconv::toeuc(str)
  str.gsub!(/\n/, ' ') 
  str.gsub!(/\0/, '')
  str
end

def encode64(bin)
  [bin].pack("m")
end

def b64encode(bin, len = 60)
  encode64(bin).scan(/.{1,#{len}}/o) do
    print $&, "\n"
  end
end