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 (7 years, 9 months ago) by misho
Branches: MAIN
CVS tags: www3_4, WWW3_3, HEAD
version 3.3

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

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