/tools/Ruby/lib/ruby/1.8/xmlrpc/base64.rb

http://github.com/agross/netopenspace · Ruby · 81 lines · 26 code · 13 blank · 42 comment · 0 complexity · ee89ac70fb31911013730ada1ce33ee2 MD5 · raw file

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