Issue #5277: Fix quote counting when parsing RFC 2231 encoded parameters.
diff --git a/Lib/email/message.py b/Lib/email/message.py
index 6adb3f6..993a1ac 100644
--- a/Lib/email/message.py
+++ b/Lib/email/message.py
@@ -62,7 +62,7 @@
     while s[:1] == ';':
         s = s[1:]
         end = s.find(';')
-        while end > 0 and s.count('"', 0, end) % 2:
+        while end > 0 and (s.count('"', 0, end) - s.count('\\"', 0, end)) % 2:
             end = s.find(';', end + 1)
         if end < 0:
             end = len(s)
diff --git a/Lib/email/test/test_email.py b/Lib/email/test/test_email.py
index 36b308f..22eddd5 100644
--- a/Lib/email/test/test_email.py
+++ b/Lib/email/test/test_email.py
@@ -351,6 +351,14 @@
         self.assertEqual(msg.get_param('name', unquote=False),
                          '"Jim&amp;&amp;Jill"')
 
+    def test_get_param_with_quotes(self):
+        msg = email.message_from_string(
+            'Content-Type: foo; bar*0="baz\\"foobar"; bar*1="\\"baz"')
+        self.assertEqual(msg.get_param('bar'), 'baz"foobar"baz')
+        msg = email.message_from_string(
+            "Content-Type: foo; bar*0=\"baz\\\"foobar\"; bar*1=\"\\\"baz\"")
+        self.assertEqual(msg.get_param('bar'), 'baz"foobar"baz')
+
     def test_has_key(self):
         msg = email.message_from_string('Header: exists')
         self.assertTrue(msg.has_key('header'))
diff --git a/Misc/ACKS b/Misc/ACKS
index 2a031e7..9eeb9bb 100644
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -162,6 +162,7 @@
 Steve Cousins
 Alex Coventry
 Matthew Dixon Cowles
+Ryan Coyner
 Christopher A. Craig
 Laura Creighton
 Simon Cross
@@ -667,6 +668,7 @@
 Rich Salz
 Kevin Samborn
 Ilya Sandler
+Mark Sapiro
 Ty Sarna
 Ben Sayer
 Michael Scharf
diff --git a/Misc/NEWS b/Misc/NEWS
index d2b907e..3ea6fff 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -83,6 +83,8 @@
 Library
 -------
 
+- Issue #5277: Fix quote counting when parsing RFC 2231 encoded parameters.
+
 - Issue #8321: Give access to OpenSSL version numbers from the `ssl` module,
   using the new attributes `ssl.OPENSSL_VERSION`, `ssl.OPENSSL_VERSION_INFO`
   and `ssl.OPENSSL_VERSION_NUMBER`.