acl/samples/base64/main.c

86 lines
1.5 KiB
C
Raw Normal View History

2014-11-19 00:25:21 +08:00
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include "lib_acl.h"
static void usage(const char *prog)
{
printf("usage: %s -e string [-d base64_string, -h help]\n", prog);
}
static void b64_encode(const char *ptr)
{
ACL_VSTRING *str = acl_vstring_alloc(1);
acl_vstring_base64_encode(str, ptr, strlen(ptr));
printf(">>>encode result:%s\n", acl_vstring_str(str));
acl_vstring_free(str);
}
static void b64_decode(const char *ptr)
{
ACL_VSTRING *str = acl_vstring_alloc(1);
const char *p;
#define DECODE(b,x,l) { \
if (acl_vstring_base64_decode((b),(x),(l)) == 0) { \
printf("bad base64 encoded string: %s\r\n", (x)); \
exit (1); \
} \
}
DECODE(str, ptr, strlen(ptr));
p = acl_vstring_str(str);
printf(">>>decode result:|%s|\n>>>orignal str: {%s}, len: %d\n", p, ptr, (int) ACL_VSTRING_LEN(str));
b64_encode(p);
printf("len: %d, %d\n", (int) ACL_VSTRING_LEN(str), (int) strlen(p));
acl_vstring_free(str);
}
int main(int argc, char *argv[])
{
char ch;
if (argc == 1) {
usage(argv[0]);
exit (1);
}
while ((ch = getopt(argc, argv, "e:d:h")) > 0) {
switch (ch) {
case 'e':
b64_encode(optarg);
break;
case 'd':
b64_decode(optarg);
break;
case 'h':
usage(argv[0]);
exit (0);
default:
usage(argv[0]);
exit (1);
}
}
{
char* p = "<EFBFBD><EFBFBD>";
char buf[32];
strcpy(buf, p);
p = buf;
p++;
*p = 0;
b64_encode(buf);
strcpy(buf, "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
p = buf;
p++;
b64_encode(p);
}
exit (0);
}