* initial commit
authorUrban Wallasch <urban.wallasch@freenet.de>
Wed, 18 Jul 2018 13:02:30 +0000 (15:02 +0200)
committerUrban Wallasch <urban.wallasch@freenet.de>
Wed, 18 Jul 2018 13:02:30 +0000 (15:02 +0200)
.gitignore [new file with mode: 0644]
base2.c [new file with mode: 0644]
build.sh [new file with mode: 0755]

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..162ad05
--- /dev/null
@@ -0,0 +1,2 @@
+*.o
+base2
diff --git a/base2.c b/base2.c
new file mode 100644 (file)
index 0000000..20d3a9a
--- /dev/null
+++ b/base2.c
@@ -0,0 +1,58 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+
+static char *cd = "\t\n\r ";
+
+static int b2encode( FILE *inf, FILE *outf ) {
+    int c;
+    while ( EOF != ( c = fgetc( inf ) ) ) {
+        for ( int i = 6; i >= 0; i -= 2 )
+            fputc( cd[c >> i & 3], outf );
+    }
+    return ferror( inf );
+}
+
+static int b2decode( FILE *inf, FILE *outf ) {
+    char c;
+    unsigned d = 0;
+    int i = 0;
+    char *p;
+    while ( EOF != ( c = fgetc( inf ) ) ) {
+        if ( NULL == ( p = strchr( cd, c ) ) )
+            return EILSEQ;
+        d = d << 2 | ( p - cd );
+        if ( 4 == ++i ) {
+            fputc( d, outf );
+            i = d = 0;
+        }
+    }
+    return i ? -i : ferror( inf );
+}
+
+void usemsg( const char *n ) {
+    fprintf( stderr, "Usage:\n%s [-c] [-d] [-t ABCD]", n );
+    exit( EXIT_FAILURE );
+}
+
+int main( int argc, char *argv[] ) {
+    int res = 0, encode = 1;
+
+    for ( int oi = 1; oi < argc; ++oi ) {
+        if ( 0 == strcmp( argv[oi], "-c" ) )
+            encode = 1;
+        else if ( 0 == strcmp( argv[oi], "-d" ) )
+            encode = 0;
+        else if ( 0 == strcmp( argv[oi], "-t" ) ) {
+            ++oi;
+            if ( oi >= argc || strlen( argv[oi] ) < 4 )
+                usemsg( argv[0] );
+            cd = argv[oi];
+        }
+        else
+            usemsg( argv[0] );
+    }
+    res = encode ? b2encode( stdin, stdout ) : b2decode( stdin, stdout );
+    exit( res ? EXIT_FAILURE : EXIT_SUCCESS );
+}
diff --git a/build.sh b/build.sh
new file mode 100755 (executable)
index 0000000..ca69251
--- /dev/null
+++ b/build.sh
@@ -0,0 +1 @@
+gcc -Wall -Wextra -std=c99 -pedantic -o base2 base2.c