Re: bug fix incremental base64 encoding (glib)



* Sebastian Siewior | 2008-06-18 17:37:29 [+0200]:

>Bug available in Revision 7055 @
>http://svn.gnome.org/svn/glib/trunk/glib/
>
>If the previous incremental encoding saved two bytes, the followed-up
>incremental just one and we than call the finish routine than state
>looks like '01-XX-YY-00' where XX is the actual saved c1 and YY and c2
>saved from the last previous (not last) incremental encoding. Since c2
>is not zero now, we get a bogus result.
>
>Signed-off-by Sebastian Siewior <sebastian breakpoint cc>

The attached file is a testcase.

Regards,
Sebastian
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <glib-2.0/glib.h>

static const unsigned char pattern[] = {
	0xab, 0xcd, 0xef, 0x12,
};

static void G_GNUC_NORETURN die(const char *str)
{
	fprintf(stderr, str);
	exit(1);
}

static size_t base64_get_size_encoded(size_t len)
{
	/* no newlines */
	return len * 4 / 3 + 6;
}
#define all_in_one_size (sizeof(pattern))
static int steps[] = {
	2, 2, 0
};

int main(int argc, char *argv[])
{
	int ret, state = 0, save = 0;
	size_t size;
	int pos;
	char *all_in_one;
	char *inc_base64;
	int ipos, i;

	size = base64_get_size_encoded(sizeof(pattern));
	all_in_one = malloc(size);
	if (!all_in_one)
		die("-ENOMEM\n");

	memset(all_in_one, 0, size);

	inc_base64 = malloc(size);
	if (!inc_base64)
		die("-ENOMEM\n");

	memset(inc_base64, 0, size);

	all_in_one = g_base64_encode(pattern, all_in_one_size);

	i = 0;
	pos = 0;
	while (steps[i])
		pos += steps[i++];

	if (pos != sizeof(pattern))
		die("pos != sizeof\n");

	pos = 0;
	ipos = 0;
	i = 0;

	while (steps[i]) {

		pos += g_base64_encode_step(pattern + ipos, steps[i], FALSE, inc_base64 + pos, &state, &save);
		ipos += steps[i];
		i++;
	}

	printf("save: %08x\n", save);

	/* save &= 0xffff; */
	g_base64_encode_close(FALSE, inc_base64 + pos, &state, &save);

	ret = strcmp(all_in_one, inc_base64);
	if (!ret)
		printf("Equal\n");
	else
		printf("Different\nCorrect:\n%s\nWrong:\n%s\n",
				all_in_one, inc_base64);
	return 0;
}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]