mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-02-02 17:26:17 +01:00
Don't refuse to encode a single byte. (Milan Crha)
2007-11-23 Matthias Clasen <mclasen@redhat.com> * glib/gbase64.c (g_base64_encode): Don't refuse to encode a single byte. (Milan Crha) * tests/base64-test.c: Test encoding short strings. svn path=/trunk/; revision=5919
This commit is contained in:
parent
39ad6fbd02
commit
78c06eafe1
@ -1,3 +1,10 @@
|
||||
2007-11-23 Matthias Clasen <mclasen@redhat.com>
|
||||
|
||||
* glib/gbase64.c (g_base64_encode): Don't refuse to encode
|
||||
a single byte. (Milan Crha)
|
||||
|
||||
* tests/base64-test.c: Test encoding short strings.
|
||||
|
||||
2007-11-23 Matthias Clasen <mclasen@redhat.com>
|
||||
|
||||
* glib/gi18n-lib.h:
|
||||
|
@ -231,16 +231,14 @@ g_base64_encode (const guchar *data,
|
||||
gint save = 0;
|
||||
|
||||
g_return_val_if_fail (data != NULL, NULL);
|
||||
g_return_val_if_fail (len > 1, NULL);
|
||||
g_return_val_if_fail (len > 0, NULL);
|
||||
|
||||
/* We can use a smaller limit here, since we know the saved state is 0 */
|
||||
out = g_malloc (len * 4 / 3 + 4);
|
||||
outlen = g_base64_encode_step (data, len, FALSE, out, &state, &save);
|
||||
outlen += g_base64_encode_close (FALSE,
|
||||
out + outlen,
|
||||
&state,
|
||||
&save);
|
||||
outlen += g_base64_encode_close (FALSE, out + outlen, &state, &save);
|
||||
out[outlen] = '\0';
|
||||
|
||||
return (gchar *) out;
|
||||
}
|
||||
|
||||
|
@ -83,24 +83,24 @@ test_incremental (gboolean line_break,
|
||||
}
|
||||
|
||||
static void
|
||||
test_full (void)
|
||||
test_full (gint length)
|
||||
{
|
||||
char *text;
|
||||
guchar *data2;
|
||||
gsize len;
|
||||
|
||||
text = g_base64_encode (data, DATA_SIZE);
|
||||
text = g_base64_encode (data, length);
|
||||
data2 = g_base64_decode (text, &len);
|
||||
g_free (text);
|
||||
|
||||
if (len != DATA_SIZE)
|
||||
if (len != length)
|
||||
{
|
||||
g_print ("Wrong decoded length: got %d, expected %d\n",
|
||||
len, DATA_SIZE);
|
||||
len, length);
|
||||
exit (1);
|
||||
}
|
||||
|
||||
if (memcmp (data, data2, DATA_SIZE) != 0)
|
||||
if (memcmp (data, data2, length) != 0)
|
||||
{
|
||||
g_print ("Wrong decoded base64 data\n");
|
||||
exit (1);
|
||||
@ -116,7 +116,10 @@ main (int argc, char *argv[])
|
||||
for (i = 0; i < DATA_SIZE; i++)
|
||||
data[i] = (guchar)i;
|
||||
|
||||
test_full ();
|
||||
test_full (DATA_SIZE);
|
||||
test_full (1);
|
||||
test_full (2);
|
||||
test_full (3);
|
||||
|
||||
test_incremental (FALSE, DATA_SIZE);
|
||||
test_incremental (TRUE, DATA_SIZE);
|
||||
@ -127,5 +130,9 @@ main (int argc, char *argv[])
|
||||
test_incremental (FALSE, DATA_SIZE - 2);
|
||||
test_incremental (TRUE, DATA_SIZE - 2);
|
||||
|
||||
test_incremental (FALSE, 1);
|
||||
test_incremental (FALSE, 2);
|
||||
test_incremental (FALSE, 3);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user