2007-03-17 10:49:09 +01:00
|
|
|
#include "config.h"
|
|
|
|
|
2006-04-04 15:03:23 +02:00
|
|
|
#include <glib.h>
|
|
|
|
#include <string.h>
|
2007-03-17 10:49:09 +01:00
|
|
|
#ifdef HAVE_UNISTD_H
|
2006-04-04 15:03:23 +02:00
|
|
|
#include <unistd.h>
|
2007-03-17 10:49:09 +01:00
|
|
|
#endif
|
2006-04-04 15:03:23 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#define DATA_SIZE 1024
|
|
|
|
#define BLOCK_SIZE 32
|
|
|
|
#define NUM_BLOCKS 32
|
|
|
|
static guchar data[DATA_SIZE];
|
|
|
|
|
|
|
|
static void
|
2006-05-03 17:27:40 +02:00
|
|
|
test_incremental (gboolean line_break,
|
|
|
|
gint length)
|
2006-04-04 15:03:23 +02:00
|
|
|
{
|
|
|
|
char *p;
|
2006-05-03 17:27:40 +02:00
|
|
|
gsize len, decoded_len, max, input_len, block_size;
|
2006-04-04 15:03:23 +02:00
|
|
|
int state, save;
|
|
|
|
guint decoder_save;
|
2006-05-03 14:23:44 +02:00
|
|
|
char *text;
|
|
|
|
guchar *data2;
|
|
|
|
|
2006-05-03 17:27:40 +02:00
|
|
|
data2 = g_malloc (length);
|
2007-11-24 17:24:30 +01:00
|
|
|
text = g_malloc (length * 4);
|
2006-04-04 15:03:23 +02:00
|
|
|
|
|
|
|
len = 0;
|
|
|
|
state = 0;
|
|
|
|
save = 0;
|
2006-05-03 17:27:40 +02:00
|
|
|
input_len = 0;
|
|
|
|
while (input_len < length)
|
|
|
|
{
|
|
|
|
block_size = MIN (BLOCK_SIZE, length - input_len);
|
|
|
|
len += g_base64_encode_step (data + input_len, block_size,
|
|
|
|
line_break, text + len, &state, &save);
|
|
|
|
input_len += block_size;
|
|
|
|
}
|
2006-04-04 15:03:23 +02:00
|
|
|
len += g_base64_encode_close (line_break, text + len, &state, &save);
|
|
|
|
|
|
|
|
if (line_break)
|
2006-05-03 17:27:40 +02:00
|
|
|
max = length * 4 / 3 + length * 4 / (3 * 72) + 7;
|
2006-04-04 15:03:23 +02:00
|
|
|
else
|
2006-05-03 17:27:40 +02:00
|
|
|
max = length * 4 / 3 + 6;
|
2006-04-04 15:03:23 +02:00
|
|
|
if (len > max)
|
|
|
|
{
|
2006-05-03 17:27:40 +02:00
|
|
|
g_print ("Too long encoded length: got %d, expected max %d\n",
|
2006-04-04 15:03:23 +02:00
|
|
|
len, max);
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
decoded_len = 0;
|
|
|
|
state = 0;
|
|
|
|
decoder_save = 0;
|
|
|
|
p = text;
|
|
|
|
while (len > 0)
|
|
|
|
{
|
2006-05-03 17:27:40 +02:00
|
|
|
int chunk_len = MIN (BLOCK_SIZE, len);
|
2006-04-04 15:03:23 +02:00
|
|
|
decoded_len += g_base64_decode_step (p,
|
|
|
|
chunk_len,
|
|
|
|
data2 + decoded_len,
|
|
|
|
&state, &decoder_save);
|
|
|
|
p += chunk_len;
|
|
|
|
len -= chunk_len;
|
|
|
|
}
|
2006-05-03 17:27:40 +02:00
|
|
|
|
|
|
|
if (decoded_len != length)
|
2006-04-04 15:03:23 +02:00
|
|
|
{
|
|
|
|
g_print ("Wrong decoded length: got %d, expected %d\n",
|
2006-05-03 17:27:40 +02:00
|
|
|
decoded_len, length);
|
2006-04-04 15:03:23 +02:00
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
2006-05-03 17:27:40 +02:00
|
|
|
if (memcmp (data, data2, length) != 0)
|
2006-04-04 15:03:23 +02:00
|
|
|
{
|
|
|
|
g_print ("Wrong decoded base64 data\n");
|
|
|
|
exit (1);
|
|
|
|
}
|
2006-05-03 14:23:44 +02:00
|
|
|
|
|
|
|
g_free (text);
|
|
|
|
g_free (data2);
|
2006-04-04 15:03:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2007-11-23 18:58:00 +01:00
|
|
|
test_full (gint length)
|
2006-04-04 15:03:23 +02:00
|
|
|
{
|
|
|
|
char *text;
|
|
|
|
guchar *data2;
|
|
|
|
gsize len;
|
|
|
|
|
2007-11-23 18:58:00 +01:00
|
|
|
text = g_base64_encode (data, length);
|
2006-04-04 15:03:23 +02:00
|
|
|
data2 = g_base64_decode (text, &len);
|
|
|
|
g_free (text);
|
|
|
|
|
2007-11-23 18:58:00 +01:00
|
|
|
if (len != length)
|
2006-04-04 15:03:23 +02:00
|
|
|
{
|
|
|
|
g_print ("Wrong decoded length: got %d, expected %d\n",
|
2007-11-23 18:58:00 +01:00
|
|
|
len, length);
|
2006-04-04 15:03:23 +02:00
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
2007-11-23 18:58:00 +01:00
|
|
|
if (memcmp (data, data2, length) != 0)
|
2006-04-04 15:03:23 +02:00
|
|
|
{
|
|
|
|
g_print ("Wrong decoded base64 data\n");
|
|
|
|
exit (1);
|
|
|
|
}
|
2006-05-03 14:23:44 +02:00
|
|
|
|
|
|
|
g_free (data2);
|
2006-04-04 15:03:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main (int argc, char *argv[])
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < DATA_SIZE; i++)
|
|
|
|
data[i] = (guchar)i;
|
|
|
|
|
2007-11-23 18:58:00 +01:00
|
|
|
test_full (DATA_SIZE);
|
|
|
|
test_full (1);
|
|
|
|
test_full (2);
|
|
|
|
test_full (3);
|
2006-05-03 17:27:40 +02:00
|
|
|
|
|
|
|
test_incremental (FALSE, DATA_SIZE);
|
|
|
|
test_incremental (TRUE, DATA_SIZE);
|
|
|
|
|
|
|
|
test_incremental (FALSE, DATA_SIZE - 1);
|
|
|
|
test_incremental (TRUE, DATA_SIZE - 1);
|
|
|
|
|
|
|
|
test_incremental (FALSE, DATA_SIZE - 2);
|
|
|
|
test_incremental (TRUE, DATA_SIZE - 2);
|
|
|
|
|
2007-11-23 18:58:00 +01:00
|
|
|
test_incremental (FALSE, 1);
|
|
|
|
test_incremental (FALSE, 2);
|
|
|
|
test_incremental (FALSE, 3);
|
|
|
|
|
2006-04-04 15:03:23 +02:00
|
|
|
return 0;
|
|
|
|
}
|