1#
2# xmlrpc/base64.rb
3# Copyright (C) 2001, 2002, 2003 by Michael Neumann (mneumann@ntecs.de)
4#
5# Released under the same term of license as Ruby.
6
7module XMLRPC # :nodoc:
8
9# This class is necessary for 'xmlrpc4r' to determine that a string should
10# be transmitted base64-encoded and not as a raw-string.
11#
12# You can use XMLRPC::Base64 on the client and server-side as a
13# parameter and/or return-value.
14class Base64
15
16  # Creates a new XMLRPC::Base64 instance with string +str+ as the
17  # internal string. When +state+ is +:dec+ it assumes that the
18  # string +str+ is not in base64 format (perhaps already decoded),
19  # otherwise if +state+ is +:enc+ it decodes +str+
20  # and stores it as the internal string.
21  def initialize(str, state = :dec)
22    case state
23    when :enc
24      @str = Base64.decode(str)
25    when :dec
26      @str = str
27    else
28      raise ArgumentError, "wrong argument; either :enc or :dec"
29    end
30  end
31
32  # Returns the decoded internal string.
33  def decoded
34    @str
35  end
36
37  # Returns the base64 encoded internal string.
38  def encoded
39    Base64.encode(@str)
40  end
41
42
43  # Decodes string +str+ with base64 and returns that value.
44  def Base64.decode(str)
45    str.gsub(/\s+/, "").unpack("m")[0]
46  end
47
48  # Encodes string +str+ with base64 and returns that value.
49  def Base64.encode(str)
50    [str].pack("m")
51  end
52
53end
54
55
56end # module XMLRPC
57
58
59=begin
60= History
61    $Id: base64.rb 36958 2012-09-13 02:22:10Z zzak $
62=end
63