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