File:  [ELWIX - Embedded LightWeight unIX -] / libaitwww / example / test_base64.c
Revision 1.4: download - view: text, annotated - select for diffs - revision graph
Wed Sep 14 15:12:22 2016 UTC (8 years, 1 month ago) by misho
Branches: MAIN
CVS tags: www3_4, WWW3_3, HEAD
version 3.3

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <elwix.h>
#include <aitwww.h>


int
main(int argc, char **argv)
{
	int rlen, fd = STDIN_FILENO;
	ait_val_t *e, *d, s, src;

	if (argc > 1) {
		fd = open(argv[1], O_RDONLY);
		if (fd == -1) {
			perror("open");
			return 1;
		}
		AIT_SET_STRSIZ(&src, lseek(fd, 0, SEEK_END));
		lseek(fd, 0, SEEK_SET);
	} else
		AIT_SET_STRSIZ(&src, USHRT_MAX + 1);
	rlen = read(fd, AIT_GET_STR(&src), AIT_LEN(&src));
	if (rlen == -1) {
		perror("read");
		if (fd > 2)
			close(fd);
		return 2;
	} else {
		AIT_INIT_VAL2(&s, string);
		AIT_SET_STRLCPY(&s, AIT_GET_STR(&src), rlen);
		AIT_FREE_VAL(&src);
	}
	if (fd > 2)
		close(fd);

	e = www_b64encode(&s);
	if (!e) {
		ELIBERR(www);
		return 3;
	}
	printf("\n\n--- encode base64 len=%u ---\n%s\n", AIT_LEN(e), AIT_GET_STR(e));

	d = www_b64decode(e);
	if (!d) {
		ELIBERR(www);
		ait_freeVar(&e);
		return 4;
	}
	printf("\n\n--- decode base64 len=%u ---\n%s\n", AIT_LEN(d), AIT_GET_STR(d));

	if ((rlen = ait_cmpVar(&s, d)) < 0)
		printf("\n\n>>> FAIL = -1\n");
	else if (!rlen)
		printf("\n\n>>> PASS = 0\n");
	else
		printf("\n\n>>> FAIL = 1\n");

	ait_freeVar(&d);
	ait_freeVar(&e);
	AIT_FREE_VAL(&s);
	return 0;
}

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>