Skip to content

Commit

Permalink
Add simple utility and some extra test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
dirkx committed Apr 2, 2021
1 parent 8088c71 commit 14cb92c
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
*.core
test
*.d
base64
19 changes: 15 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
TARGET_EXEC ?= test
TARGET_LIB ?= libbase64.a
TARGET_TEST ?= test
TARGET_EXEC ?= base45
TARGET_LIB ?= libbase45.a

SRC_DIRS ?= .

Expand All @@ -13,10 +14,20 @@ INC_FLAGS :=

CPPFLAGS ?= $(INC_FLAGS) -MMD -MP

tests: all
for i in 1 7 13 10 103 1002 10009 100007 1000001 0; do \
openssl rand $$i > $$TMPDIR/x; \
cat $$TMPDIR/x | ./base45 | ./base45 -d > $$TMPDIR/y; \
diff $$TMPDIR/x $$TMPDIR/y || exit 1; \
done; \
rm $$TMPDIR/x $$TMPDIR/y

all: $(TARGET_EXEC) $(TARGET_LIB)
./$(TARGET_EXEC)

$(TARGET_EXEC): $(OBJS_TEST) $(TARGET_LIB)
$(TARGET_EXEC): $(SRCS)
$(CC) $(SRCS) -DBASE45_UTIL -o $@ $(LDFLAGS)

$(TARGET_TEST): $(OBJS_TEST) $(TARGET_LIB)
$(CC) $(OBJS_TEST) -o $@ $(LDFLAGS) -lbase64 -L.

$(TARGET_LIB): $(OBJS)
Expand Down
71 changes: 70 additions & 1 deletion base45.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@
11 B 23 N 35 Z
*/

static const char BASE45_CHARSET[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:";

static char _C2I[256] = {
255,255,255,255, 255,255,255,255, 255,255,255,255, 255,255,255,255,
255,255,255,255, 255,255,255,255, 255,255,255,255, 255,255,255,255,
36, 255,255,255, 37, 38,255,255, 255,255, 39, 40, 255, 41, 42, 43,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,255,255, 255,255,255,255,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 44,255, 255,255,255,255,

255, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, /* uppercase */
25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 35, 255,255,255,255,
Expand Down Expand Up @@ -134,4 +135,72 @@ int base45_decode(unsigned char * dst, size_t * _max_dst_len, const char * src,
return 0;
}

#ifdef BASE45_UTIL
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char ** argv) {
FILE * in = stdin;
FILE * out = stdout;
int decode = 0;
int at = 1;

if (argc > 1 && argv[at][0] == '-') {
if (argv[at][1] == 'd')
decode = 1;
else {
fprintf(stderr,"Syntax: %s [-d] [infile [outfile]]\n", argv[0]);
exit(1);
};
at++; argc--;
};
if (argc > 1) {
if (NULL == (in = fopen(argv[at],"r"))) {
perror("Cannot open input file for reading:");
exit(1);
};
at++; argc--;
};
if (argc > 1) {
if (NULL == (out = fopen(argv[at],"w"))) {
perror("Cannot open out file for writing:");
exit(1);
};
at++; argc--;
};

#ifdef VALIDATE
for(int i = 0; i < 45; i++) assert(i == _C2I[BASE45_CHARSET[i]]);
#endif

while(!feof(in)) {
unsigned char buff[ 3 * 1024 ]; // multiple chosen to allow continuation.
unsigned char outbuf[ 3 * 3 * 1024 ];
size_t olen = sizeof(outbuf);
size_t len = fread(buff, 1, 3 * 1024, in);

buff[len] = 0;

if (len) {
int e;
if (decode)
e = base45_decode(outbuf, &olen, (char *) buff, len);
else
e = base45_encode((char *)outbuf, &olen, buff, len);

if (e) {
fprintf(stderr,"base45 %s failed\n", decode ? "decode" : "encode");
exit(1);
};

if (olen)
fwrite(outbuf, 1, olen, out);
};
}
fclose(out);

return(0);
};
#endif


0 comments on commit 14cb92c

Please sign in to comment.