File:  [ELWIX - Embedded LightWeight unIX -] / libaitwww / example / test_base64.c
Revision 1.3: download - view: text, annotated - select for diffs - revision graph
Thu May 30 09:25:35 2013 UTC (11 years, 1 month ago) by misho
Branches: MAIN
CVS tags: www3_3, www3_2, www3_1, WWW3_2, WWW3_1, WWW3_0, HEAD
version 3.0

    1: #include <stdio.h>
    2: #include <string.h>
    3: #include <unistd.h>
    4: #include <fcntl.h>
    5: #include <sys/limits.h>
    6: #include <elwix.h>
    7: #include <aitwww.h>
    8: 
    9: 
   10: int
   11: main(int argc, char **argv)
   12: {
   13: 	int rlen, fd = STDIN_FILENO;
   14: 	ait_val_t *e, *d, s, src;
   15: 
   16: 	if (argc > 1) {
   17: 		fd = open(argv[1], O_RDONLY);
   18: 		if (fd == -1) {
   19: 			perror("open");
   20: 			return 1;
   21: 		}
   22: 		AIT_SET_STRSIZ(&src, lseek(fd, 0, SEEK_END));
   23: 		lseek(fd, 0, SEEK_SET);
   24: 	} else
   25: 		AIT_SET_STRSIZ(&src, USHRT_MAX + 1);
   26: 	rlen = read(fd, AIT_GET_STR(&src), AIT_LEN(&src));
   27: 	if (rlen == -1) {
   28: 		perror("read");
   29: 		if (fd > 2)
   30: 			close(fd);
   31: 		return 2;
   32: 	} else {
   33: 		AIT_INIT_VAL2(&s, string);
   34: 		AIT_SET_STRLCPY(&s, AIT_GET_STR(&src), rlen);
   35: 		AIT_FREE_VAL(&src);
   36: 	}
   37: 	if (fd > 2)
   38: 		close(fd);
   39: 
   40: 	e = www_b64encode(&s);
   41: 	if (!e) {
   42: 		ELIBERR(www);
   43: 		return 3;
   44: 	}
   45: 	printf("\n\n--- encode base64 len=%u ---\n%s\n", AIT_LEN(e), AIT_GET_STR(e));
   46: 
   47: 	d = www_b64decode(e);
   48: 	if (!d) {
   49: 		ELIBERR(www);
   50: 		ait_freeVar(&e);
   51: 		return 4;
   52: 	}
   53: 	printf("\n\n--- decode base64 len=%u ---\n%s\n", AIT_LEN(d), AIT_GET_STR(d));
   54: 
   55: 	if ((rlen = ait_cmpVar(&s, d)) < 0)
   56: 		printf("\n\n>>> FAIL = -1\n");
   57: 	else if (!rlen)
   58: 		printf("\n\n>>> PASS = 0\n");
   59: 	else
   60: 		printf("\n\n>>> FAIL = 1\n");
   61: 
   62: 	ait_freeVar(&d);
   63: 	ait_freeVar(&e);
   64: 	AIT_FREE_VAL(&s);
   65: 	return 0;
   66: }

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