base64: Fix g_base64_decode_step ()
authorOgnyan Tonchev <ognyan@axis.com>
Thu, 28 Feb 2013 17:27:14 +0000 (18:27 +0100)
committerColin Walters <walters@verbum.org>
Tue, 5 Mar 2013 15:55:29 +0000 (10:55 -0500)
Do not produce invalid data if there was padding character in the
previous sequence.

https://bugzilla.gnome.org/show_bug.cgi?id=694843

glib/gbase64.c
glib/tests/base64.c

index d0ab0ea..bcc59a6 100644 (file)
@@ -344,8 +344,18 @@ g_base64_decode_step (const gchar  *in,
   /* convert 4 base64 bytes to 3 normal bytes */
   v=*save;
   i=*state;
-  inptr = (const guchar *)in;
+
   last[0] = last[1] = 0;
+
+  /* we use the sign in the state to determine if we got a padding character
+     in the previous sequence */
+  if (i < 0)
+    {
+      i = -i;
+      last[0] = '=';
+    }
+
+  inptr = (const guchar *)in;
   while (inptr < inend)
     {
       c = *inptr++;
@@ -369,7 +379,7 @@ g_base64_decode_step (const gchar  *in,
     }
 
   *save = v;
-  *state = i;
+  *state = last[0] == '=' ? -i : i;
 
   return outptr - out;
 }
index 0d8ce92..6704c44 100644 (file)
@@ -408,14 +408,12 @@ main (int argc, char *argv[])
   g_test_add_func ("/base64/decode-inplace", test_base64_decode_inplace);
   g_test_add_func ("/base64/encode-decode", test_base64_encode_decode);
 
-  /*
   g_test_add_data_func ("/base64/incremental/smallblock/1", GINT_TO_POINTER(1),
                         test_base64_decode_smallblock);
   g_test_add_data_func ("/base64/incremental/smallblock/2", GINT_TO_POINTER(2),
                         test_base64_decode_smallblock);
   g_test_add_data_func ("/base64/incremental/smallblock/3", GINT_TO_POINTER(3),
                         test_base64_decode_smallblock);
-  */
   g_test_add_data_func ("/base64/incremental/smallblock/4", GINT_TO_POINTER(4),
                         test_base64_decode_smallblock);