[前][次][番号順一覧][スレッド一覧]

ruby-changes:14501

From: knu <ko1@a...>
Date: Mon, 18 Jan 2010 03:43:25 +0900 (JST)
Subject: [ruby-changes:14501] Ruby:r26337 (ruby_1_8): * lib/base64.rb (Base64#{strict_encode64,strict_decode64,urlsafe_encode64,

knu	2010-01-18 03:43:04 +0900 (Mon, 18 Jan 2010)

  New Revision: 26337

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=26337

  Log:
    * lib/base64.rb (Base64#{strict_encode64,strict_decode64,urlsafe_encode64,
      urlsafe_decode64): New methods backported from 1.9.

  Added directories:
    branches/ruby_1_8/test/base64/
  Added files:
    branches/ruby_1_8/test/base64/test_base64.rb
  Modified files:
    branches/ruby_1_8/ChangeLog
    branches/ruby_1_8/NEWS
    branches/ruby_1_8/lib/base64.rb

Index: ruby_1_8/NEWS
===================================================================
--- ruby_1_8/NEWS	(revision 26336)
+++ ruby_1_8/NEWS	(revision 26337)
@@ -128,6 +128,15 @@
 
     New methods.
 
+* base64
+
+  * Base64#strict_encode64
+  * Base64#strict_decode64
+  * Base64#urlsafe_encode64
+  * Base64#urlsafe_decode64
+
+    New methods.
+
 * dbm
 
   DBM#key
Index: ruby_1_8/ChangeLog
===================================================================
--- ruby_1_8/ChangeLog	(revision 26336)
+++ ruby_1_8/ChangeLog	(revision 26337)
@@ -1,3 +1,8 @@
+Mon Jan 18 03:39:05 2010  Akinori MUSHA  <knu@i...>
+
+	* lib/base64.rb (Base64#{strict_encode64,strict_decode64,urlsafe_encode64,
+	  urlsafe_decode64): New methods backported from 1.9.
+
 Sun Jan 17 19:24:25 2010  Nobuyoshi Nakada  <nobu@r...>
 
 	* math.c (domain_check): check errno first.
Index: ruby_1_8/lib/base64.rb
===================================================================
--- ruby_1_8/lib/base64.rb	(revision 26336)
+++ ruby_1_8/lib/base64.rb	(revision 26337)
@@ -97,6 +97,40 @@
     [bin].pack("m")
   end
 
+  # Returns the Base64-encoded version of +bin+.
+  # This method complies with RFC 4648.
+  # No line feeds are added.
+  def strict_encode64(bin)
+    [bin].pack((len = bin.bytesize) > 45 ? "m#{len+2}" : "m").chomp
+  end
+
+  # Returns the Base64-decoded version of +str+.
+  # This method complies with RFC 4648.
+  # ArgumentError is raised if +str+ is incorrectly padded or contains
+  # non-alphabet characters.  Note that CR or LF are also rejected.
+  def strict_decode64(str)
+    return str.unpack("m").first if str.bytesize % 4 == 0 &&
+      str.match(%r{\A[A-Za-z0-9+/]*([A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?\z}) &&
+      (!$1 || $1 == $1.unpack('m').pack('m').chomp)
+    raise ArgumentError, 'invalid base64'
+  end
+
+  # Returns the Base64-encoded version of +bin+.
+  # This method complies with ``Base 64 Encoding with URL and Filename Safe
+  # Alphabet'' in RFC 4648.
+  # The alphabet uses '-' instead of '+' and '_' instead of '/'.
+  def urlsafe_encode64(bin)
+    strict_encode64(bin).tr("+/", "-_")
+  end
+
+  # Returns the Base64-decoded version of +str+.
+  # This method complies with ``Base 64 Encoding with URL and Filename Safe
+  # Alphabet'' in RFC 4648.
+  # The alphabet uses '-' instead of '+' and '_' instead of '/'.
+  def urlsafe_decode64(str)
+    strict_decode64(str.tr("-_", "+/"))
+  end
+
   # _Prints_ the Base64 encoded version of +bin+ (a +String+) in lines of
   # +len+ (default 60) characters.
   #
Index: ruby_1_8/test/base64/test_base64.rb
===================================================================
--- ruby_1_8/test/base64/test_base64.rb	(revision 0)
+++ ruby_1_8/test/base64/test_base64.rb	(revision 26337)
@@ -0,0 +1,99 @@
+require "test/unit"
+require "base64"
+
+class TestBase64 < Test::Unit::TestCase
+  def test_sample
+    assert_equal("U2VuZCByZWluZm9yY2VtZW50cw==\n", Base64.encode64('Send reinforcements'))
+    assert_equal('Send reinforcements', Base64.decode64("U2VuZCByZWluZm9yY2VtZW50cw==\n"))
+    assert_equal(
+      "Tm93IGlzIHRoZSB0aW1lIGZvciBhbGwgZ29vZCBjb2RlcnMKdG8gbGVhcm4g\nUnVieQ==\n",
+      Base64.encode64("Now is the time for all good coders\nto learn Ruby"))
+    assert_equal(
+      "Now is the time for all good coders\nto learn Ruby",
+      Base64.decode64("Tm93IGlzIHRoZSB0aW1lIGZvciBhbGwgZ29vZCBjb2RlcnMKdG8gbGVhcm4g\nUnVieQ==\n"))
+    assert_equal(
+      "VGhpcyBpcyBsaW5lIG9uZQpUaGlzIGlzIGxpbmUgdHdvClRoaXMgaXMgbGlu\nZSB0aHJlZQpBbmQgc28gb24uLi4K\n",
+      Base64.encode64("This is line one\nThis is line two\nThis is line three\nAnd so on...\n"))
+    assert_equal(
+      "This is line one\nThis is line two\nThis is line three\nAnd so on...\n",
+      Base64.decode64("VGhpcyBpcyBsaW5lIG9uZQpUaGlzIGlzIGxpbmUgdHdvClRoaXMgaXMgbGluZSB0aHJlZQpBbmQgc28gb24uLi4K"))
+  end
+
+  def test_encode64
+    assert_equal("", Base64.encode64(""))
+    assert_equal("AA==\n", Base64.encode64("\0"))
+    assert_equal("AAA=\n", Base64.encode64("\0\0"))
+    assert_equal("AAAA\n", Base64.encode64("\0\0\0"))
+    assert_equal("/w==\n", Base64.encode64("\377"))
+    assert_equal("//8=\n", Base64.encode64("\377\377"))
+    assert_equal("////\n", Base64.encode64("\377\377\377"))
+    assert_equal("/+8=\n", Base64.encode64("\xff\xef"))
+  end
+
+  def test_decode64
+    assert_equal("", Base64.decode64(""))
+    assert_equal("\0", Base64.decode64("AA==\n"))
+    assert_equal("\0\0", Base64.decode64("AAA=\n"))
+    assert_equal("\0\0\0", Base64.decode64("AAAA\n"))
+    assert_equal("\377", Base64.decode64("/w==\n"))
+    assert_equal("\377\377", Base64.decode64("//8=\n"))
+    assert_equal("\377\377\377", Base64.decode64("////\n"))
+    assert_equal("\xff\xef", Base64.decode64("/+8=\n"))
+  end
+
+  def test_strict_encode64
+    assert_equal("", Base64.strict_encode64(""))
+    assert_equal("AA==", Base64.strict_encode64("\0"))
+    assert_equal("AAA=", Base64.strict_encode64("\0\0"))
+    assert_equal("AAAA", Base64.strict_encode64("\0\0\0"))
+    assert_equal("/w==", Base64.strict_encode64("\377"))
+    assert_equal("//8=", Base64.strict_encode64("\377\377"))
+    assert_equal("////", Base64.strict_encode64("\377\377\377"))
+    assert_equal("/+8=", Base64.strict_encode64("\xff\xef"))
+  end
+
+  def test_strict_decode64
+    assert_equal("", Base64.strict_decode64(""))
+    assert_equal("\0", Base64.strict_decode64("AA=="))
+    assert_equal("\0\0", Base64.strict_decode64("AAA="))
+    assert_equal("\0\0\0", Base64.strict_decode64("AAAA"))
+    assert_equal("\377", Base64.strict_decode64("/w=="))
+    assert_equal("\377\377", Base64.strict_decode64("//8="))
+    assert_equal("\377\377\377", Base64.strict_decode64("////"))
+    assert_equal("\xff\xef", Base64.strict_decode64("/+8="))
+
+    assert_raise(ArgumentError) { Base64.strict_decode64("^") }
+    assert_raise(ArgumentError) { Base64.strict_decode64("A") }
+    assert_raise(ArgumentError) { Base64.strict_decode64("A^") }
+    assert_raise(ArgumentError) { Base64.strict_decode64("AA") }
+    assert_raise(ArgumentError) { Base64.strict_decode64("AA=") }
+    assert_raise(ArgumentError) { Base64.strict_decode64("AA===") }
+    assert_raise(ArgumentError) { Base64.strict_decode64("AA=x") }
+    assert_raise(ArgumentError) { Base64.strict_decode64("AAA") }
+    assert_raise(ArgumentError) { Base64.strict_decode64("AAA^") }
+    assert_raise(ArgumentError) { Base64.strict_decode64("AB==") }
+    assert_raise(ArgumentError) { Base64.strict_decode64("AAB=") }
+  end
+
+  def test_urlsafe_encode64
+    assert_equal("", Base64.urlsafe_encode64(""))
+    assert_equal("AA==", Base64.urlsafe_encode64("\0"))
+    assert_equal("AAA=", Base64.urlsafe_encode64("\0\0"))
+    assert_equal("AAAA", Base64.urlsafe_encode64("\0\0\0"))
+    assert_equal("_w==", Base64.urlsafe_encode64("\377"))
+    assert_equal("__8=", Base64.urlsafe_encode64("\377\377"))
+    assert_equal("____", Base64.urlsafe_encode64("\377\377\377"))
+    assert_equal("_-8=", Base64.urlsafe_encode64("\xff\xef"))
+  end
+
+  def test_urlsafe_decode64
+    assert_equal("", Base64.urlsafe_decode64(""))
+    assert_equal("\0", Base64.urlsafe_decode64("AA=="))
+    assert_equal("\0\0", Base64.urlsafe_decode64("AAA="))
+    assert_equal("\0\0\0", Base64.urlsafe_decode64("AAAA"))
+    assert_equal("\377", Base64.urlsafe_decode64("_w=="))
+    assert_equal("\377\377", Base64.urlsafe_decode64("__8="))
+    assert_equal("\377\377\377", Base64.urlsafe_decode64("____"))
+    assert_equal("\xff\xef", Base64.urlsafe_decode64("_+8="))
+  end
+end

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]