--- libaitwww/example/test_base64.c 2012/09/17 13:05:47 1.1 +++ libaitwww/example/test_base64.c 2012/09/20 14:19:45 1.2 @@ -0,0 +1,66 @@ +#include +#include +#include +#include +#include +#include +#include + + +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) { + ioLIBERR(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) { + ioLIBERR(www); + io_freeVar(&e); + return 4; + } + printf("\n\n--- decode base64 len=%u ---\n%s\n", AIT_LEN(d), AIT_GET_STR(d)); + + if ((rlen = io_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"); + + io_freeVar(&d); + io_freeVar(&e); + AIT_FREE_VAL(&s); + return 0; +}