roken: add rkbase64 noinst program
This will be useful in tests.
This commit is contained in:
@@ -20,7 +20,7 @@ if HAVE_DBHEADER
|
||||
AM_CPPFLAGS += -I$(DBHEADER)
|
||||
endif
|
||||
|
||||
noinst_PROGRAMS = snprintf-test resolve-test rkpty test-detach test-auxval rkvis
|
||||
noinst_PROGRAMS = snprintf-test resolve-test rkpty test-detach test-auxval rkvis rkbase64
|
||||
|
||||
CHECK_LOCAL = snprintf-test resolve-test rkpty make-roken
|
||||
|
||||
@@ -60,6 +60,9 @@ test_getuserinfo_LDADD = libtest.la $(LDADD)
|
||||
rkvis_SOURCES = vis.c
|
||||
rkvis_CPPFLAGS = -DTEST
|
||||
|
||||
rkbase64_SOURCES = base64.c
|
||||
rkbase64_CPPFLAGS = -DTEST
|
||||
|
||||
test_detach_SOURCES = test-detach.c
|
||||
|
||||
rkpty_LDADD = $(LIB_openpty) $(LDADD)
|
||||
|
@@ -37,7 +37,13 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <limits.h>
|
||||
#ifdef TEST
|
||||
#include <stdio.h>
|
||||
#include <getarg.h>
|
||||
#include <err.h>
|
||||
#endif
|
||||
#include "base64.h"
|
||||
#include "roken.h"
|
||||
|
||||
static const char base64_chars[] =
|
||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||
@@ -148,3 +154,105 @@ rk_base64_decode(const char *str, void *data)
|
||||
}
|
||||
return q - (unsigned char *) data;
|
||||
}
|
||||
|
||||
#ifdef TEST
|
||||
static int decode_flag;
|
||||
static int help_flag;
|
||||
|
||||
/*
|
||||
* The short options are compatible with a subset of the FreeBSD contrib
|
||||
* vis(1). Heimdal additions have long option names only.
|
||||
*/
|
||||
static struct getargs args[] = {
|
||||
{ "decode", 'd', arg_flag, &decode_flag, "Decode", NULL },
|
||||
{ "help", 'h', arg_flag, &help_flag, "Print help message", NULL },
|
||||
};
|
||||
static size_t num_args = sizeof(args)/sizeof(args[0]);
|
||||
|
||||
int
|
||||
main(int argc, char **argv)
|
||||
{
|
||||
unsigned char *buf = NULL;
|
||||
size_t buflen = 0;
|
||||
size_t bufsz = 0;
|
||||
int goptind = 0;
|
||||
int ret;
|
||||
|
||||
setprogname("rkbase64");
|
||||
if (getarg(args, num_args, argc, argv, &goptind) || help_flag) {
|
||||
arg_printusage(args, num_args, NULL, "FILE | -");
|
||||
return help_flag ? 0 : 1;
|
||||
}
|
||||
|
||||
argc -= goptind;
|
||||
argv += goptind;
|
||||
|
||||
if (help_flag)
|
||||
return arg_printusage(args, num_args, NULL, "FILE | -- -"), 0;
|
||||
if (argc != 1)
|
||||
return arg_printusage(args, num_args, NULL, "FILE | -- -"), 1;
|
||||
|
||||
if (strcmp(argv[0], "-") == 0) {
|
||||
unsigned char *tmp;
|
||||
unsigned char d[4096];
|
||||
size_t bytes;
|
||||
|
||||
while (!feof(stdin) && !ferror(stdin)) {
|
||||
bytes = fread(d, 1, sizeof(d), stdin);
|
||||
if (bytes == 0)
|
||||
continue;
|
||||
if (buflen + bytes > bufsz) {
|
||||
if ((tmp = realloc(buf, bufsz + (bufsz >> 2) + sizeof(d))) == NULL)
|
||||
err(1, "Could not read stdin");
|
||||
buf = tmp;
|
||||
bufsz = bufsz + (bufsz >> 2) + sizeof(d);
|
||||
}
|
||||
memcpy(buf + buflen, d, bytes);
|
||||
buflen += bytes;
|
||||
}
|
||||
if (ferror(stdin))
|
||||
err(1, "Could not read stdin");
|
||||
} else {
|
||||
void *d;
|
||||
if ((ret = rk_undumpdata(argv[0], &d, &bufsz)))
|
||||
err(1, "Could not read %s", argv[0]);
|
||||
buflen = bufsz;
|
||||
buf = d;
|
||||
}
|
||||
|
||||
if (decode_flag) {
|
||||
unsigned char *d;
|
||||
|
||||
if (buflen == bufsz) {
|
||||
unsigned char *tmp;
|
||||
|
||||
if ((tmp = realloc(buf, bufsz + 1)) == NULL)
|
||||
err(1, "Could not decode data");
|
||||
buf = tmp;
|
||||
bufsz++;
|
||||
}
|
||||
buf[buflen] = '\0';
|
||||
|
||||
if ((d = malloc(buflen * 3 / 4 + 4)) == NULL)
|
||||
err(1, "Could not decode data");
|
||||
|
||||
if ((ret = rk_base64_decode((const char *)buf, d)) < 0)
|
||||
err(1, "Could not decode data");
|
||||
if (fwrite(d, ret, 1, stdout) != 1)
|
||||
err(1, "Could not write decoded data");
|
||||
free(d);
|
||||
} else {
|
||||
char *e;
|
||||
|
||||
if ((ret = rk_base64_encode(buf, buflen, &e)) < 0)
|
||||
err(1, "Could not encode data");
|
||||
if (fwrite(e, ret, 1, stdout) != 1)
|
||||
err(1, "Could not write decoded data");
|
||||
free(e);
|
||||
if (fwrite("\n", 1, 1, stdout) != 1)
|
||||
err(1, "Could not write decoded data");
|
||||
}
|
||||
free(buf);
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
Reference in New Issue
Block a user