From: Urban Wallasch Date: Wed, 18 Jul 2018 13:02:30 +0000 (+0200) Subject: * initial commit X-Git-Url: https://git.packet-gain.de/?a=commitdiff_plain;h=3302c4eb76099cecbc6c55a5b7c87e61354050dd;p=base4.git * initial commit --- 3302c4eb76099cecbc6c55a5b7c87e61354050dd diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..162ad05 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.o +base2 diff --git a/base2.c b/base2.c new file mode 100644 index 0000000..20d3a9a --- /dev/null +++ b/base2.c @@ -0,0 +1,58 @@ +#include +#include +#include +#include + +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 index 0000000..ca69251 --- /dev/null +++ b/build.sh @@ -0,0 +1 @@ +gcc -Wall -Wextra -std=c99 -pedantic -o base2 base2.c