From: Urban Wallasch Date: Sat, 21 Jul 2018 08:30:32 +0000 (+0200) Subject: * Renamed to base4, to match this version's actual functionality. X-Git-Url: https://git.packet-gain.de/?a=commitdiff_plain;ds=sidebyside;p=base4.git * Renamed to base4, to match this version's actual functionality. --- diff --git a/.gitignore b/.gitignore index 162ad05..550b72a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ *.o -base2 +base4 diff --git a/base2.c b/base2.c deleted file mode 100644 index b7db8a7..0000000 --- a/base2.c +++ /dev/null @@ -1,58 +0,0 @@ -#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", 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/base4.c b/base4.c new file mode 100644 index 0000000..31c418d --- /dev/null +++ b/base4.c @@ -0,0 +1,58 @@ +#include +#include +#include +#include + +static char *cd = "\t\n\r "; + +static int b4encode( 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 b4decode( 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", 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 ? b4encode( stdin, stdout ) : b4decode( stdin, stdout ); + exit( res ? EXIT_FAILURE : EXIT_SUCCESS ); +} diff --git a/build.sh b/build.sh index ca69251..a3fea44 100755 --- a/build.sh +++ b/build.sh @@ -1 +1 @@ -gcc -Wall -Wextra -std=c99 -pedantic -o base2 base2.c +gcc -Wall -Wextra -std=c99 -pedantic -o base4 base4.c