diff --git a/Makefile b/Makefile index a870852..e6f82d7 100644 --- a/Makefile +++ b/Makefile @@ -1,12 +1,14 @@ -CFLAGS = -g -Wall -Wextra -Werror +CC = gcc +CFLAGS = -Og -g -Wall -Wextra -Werror -objects = main.o input.o trans.o +objects = main.o input.o output.o encode.o b64 : $(objects) - cc -o b64 $(objects) + $(CC) -o b64 $(objects) -main.o trans.o input.o : input.h -main.o trans.o : trans.h +main.o input.o : input.h +main.o encode.o : encode.h +main.o output.o : output.h .PHONY : clean clean : diff --git a/encode.c b/encode.c new file mode 100644 index 0000000..08d50f8 --- /dev/null +++ b/encode.c @@ -0,0 +1,107 @@ +#include "encode.h" + +#define PADDING '=' + +static +unsigned char b64toascii[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \ + "abcdefghijklmnopqrstuvwxyz" \ + "0123456789" \ + "+/"; + +static +unsigned char b64urltoascii[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \ + "abcdefghijklmnopqrstuvwxyz" \ + "0123456789" \ + "-_"; + +static +unsigned char asciitob64[] = { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 62, 0, 62, 0, 63, 52, 53, + 54, 55, 56, 57, 58, 59, 60, 61, 0, 0, + 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, + 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, + 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, + 25, 0, 0, 0, 0, 63, 0, 26, 27, 28, + 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, + 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, + 49, 50, 51 +}; + + +int +encode(unsigned char *op, int olen, unsigned char *sp, int url) +{ + int tmp; + unsigned char *table; + unsigned char *sbeg; + unsigned char *tend; + + table = (!url) ? b64toascii : b64urltoascii; + + sbeg = sp; + tend = op + olen - (olen % 3); + while (op < tend) { + *sp++ = table[(*op & ~3) >> 2]; + tmp = (*op++ & 3) << 4; + *sp++ = table[tmp + ((*op & ~15) >> 4)]; + tmp = (*op++ & 15) << 2; + *sp++ = table[tmp + ((*op & 192) >> 6)]; + *sp++ = table[*op++ & ~192]; + } + switch (olen % 3) { + case 2: + *sp++ = table[(*op & ~3) >> 2]; + tmp = (*op++ & 3) << 4; + *sp++ = table[tmp + ((*op & ~15) >> 4)]; + *sp++ = table[(*op & 15) << 2]; + *sp++ = PADDING; + break; + case 1: + *sp++ = table[(*op & ~3) >> 2]; + *sp++ = table[(*op & 3) << 4]; + *sp++ = PADDING; + *sp++ = PADDING; + break; + } + + return sp-sbeg; +} + +int +decode(unsigned char *sp, int slen, unsigned char *op) +{ + int tmp, b; + unsigned char *obeg; + unsigned char *qend; + + obeg = op; + qend = sp + slen - (slen % 4); + while (sp < qend) { + tmp = asciitob64[*sp++] << 2; + b = asciitob64[*sp++]; + *op++ = tmp + ((b & ~15) >> 4); + tmp = (b & 15) << 4; + b = asciitob64[*sp++]; + *op++ = tmp + ((b & ~3) >> 2); + *op++ = ((b & 3) << 6) + asciitob64[*sp++]; + } + switch (slen % 4) { + case 3: + tmp = asciitob64[*sp++] << 2; + b = asciitob64[*sp++]; + *op++ = tmp + ((b & ~15) >> 4); + tmp = (b & 15) << 4; + *op++ = tmp + ((asciitob64[*sp++] & ~3) >> 2); + break; + case 2: + tmp = asciitob64[*sp++] << 2; + *op++ = tmp + ((asciitob64[*sp++] & ~15) >> 4); + break; + } + + return op-obeg; +} diff --git a/encode.h b/encode.h new file mode 100644 index 0000000..7e6f217 --- /dev/null +++ b/encode.h @@ -0,0 +1,10 @@ +#ifndef ENCODE_H +#define ENCODE_H + +int +encode(unsigned char *op, int olen, unsigned char *sp, int url); + +int +decode(unsigned char *sp, int slen, unsigned char *op); + +#endif diff --git a/input.c b/input.c index 855445a..8e500b3 100644 --- a/input.c +++ b/input.c @@ -1,37 +1,29 @@ #include "input.h" -#define OBUFSIZE 4 -#define SBUFSIZE 5 +#define PADDING '=' -#define PADDING '=' - -unsigned char o[OBUFSIZE]; -unsigned char s[SBUFSIZE]; - -unsigned char * -getocts(FILE *fp, int *np) +int +readb(FILE *fp, unsigned char *s, int slen) { - int n, c; + int read, tread; + unsigned char *send, *sbeg; + unsigned char *l, *r; + + read = tread = 0; + sbeg = l = r = s; + while (tread < slen && (read = fread(l, sizeof(*l), slen-tread, fp))) { + tread += read; + send = s + tread; + for (r = l; r < send; ++r) + if (*r != '\n') + *l++ = *r; + tread -= r - l; + if (l > sbeg) { + for (--l; *l == PADDING; --l) + ; + ++l; + } + } - n = 0; - while (n < OBUFSIZE-1 && (c = fgetc(fp)) != EOF) - o[n++] = c; - *np = n; - - return (*np) ? o : NULL; -} - -unsigned char * -getsxts(FILE *fp, int *np) -{ - int n, p, c; - - n = p = 0; - while (n < SBUFSIZE-1 && (c = fgetc(fp)) != EOF && c != PADDING) - s[n++] = c; - while (n+p < SBUFSIZE-1) - s[n+p++] = PADDING; - *np = n; - - return (n) ? s : NULL; + return l-sbeg; } diff --git a/input.h b/input.h index 178f17b..901b2c4 100644 --- a/input.h +++ b/input.h @@ -1,15 +1,9 @@ -#ifndef HEADER_INPUT -#define HEADER_INPUT +#ifndef INPUT_H +#define INPUT_H #include -extern unsigned char o[]; -extern unsigned char s[]; - -unsigned char * -getocts(FILE *fp, int *np); - -unsigned char * -getsxts(FILE *fp, int *np); +int +readb(FILE *fp, unsigned char *s, int slen); #endif diff --git a/main.c b/main.c index b657c99..bc612d5 100644 --- a/main.c +++ b/main.c @@ -1,58 +1,80 @@ #include #include #include "input.h" -#include "trans.h" +#include "output.h" +#include "encode.h" + +#define OCTETBUF 1500 +#define SXTETBUF 2000 + +unsigned char obuf[OCTETBUF]; +unsigned char sbuf[SXTETBUF]; int main(int argc, char *argv[]) { - int c, n, dec, hlp; - char *prog = *argv; - unsigned char *b; - FILE *fp; + int c, n, last; + int dec, url, hlp, wrp; + FILE *in, *out; + char *prog; - dec = hlp = 0; + prog = *argv; + dec = url = hlp = wrp = 0; while (--argc > 0 && (*++argv)[0] == '-') while ((c = *++argv[0])) switch (c) { case 'd': dec = 1; break; + case 'u': + url = 1; + break; case 'h': hlp = 1; break; + case 'w': + wrp = 1; + break; default: fprintf(stderr, "%s: illegal option %c\n", prog, c); exit(EXIT_FAILURE); break; } if (hlp) { - fprintf(stdout, "Usage: %s -d -h file\n", prog); - } else if (argc != 1) { - if (dec) { - while ((b = getsxts(stdin, &n))) { - b = decode(b, &n); - fwrite(b, sizeof(*b), n, stdout); - } - } else { - while ((b = getocts(stdin, &n))) - printf("%s", encode(b, n)); - } + fprintf(stdout, "Usage: %s -duhw infile outfile\n", prog); } else { - if ((fp = fopen(*argv, "r")) == NULL) { + in = out = NULL; + if (argc >= 1 && (in = fopen(*argv, "r")) == NULL) { fprintf(stderr, "%s: can't open %s\n", prog, *argv); exit(EXIT_FAILURE); } - else if (dec) { - while ((b = getsxts(fp, &n))) { - b = decode(b, &n); - fwrite(b, sizeof(*b), n, stdout); - } - } else { - while ((b = getocts(fp, &n))) - printf("%s", encode(b, n)); + if (argc == 2 && (out = fopen(*(argv+1), "w")) == NULL) { + fprintf(stderr, "%s: can't open %s\n", prog, *(argv+1)); + exit(EXIT_FAILURE); } - fclose(fp); + if (dec) { + while ((n = readb((in) ? in : stdin, sbuf, SXTETBUF))) { + n = decode(sbuf, n, obuf); + fwrite(obuf, sizeof(*obuf), n, (out) ? out : stdout); + } + } else if (wrp) { + last = 0; + while ((n = fread(obuf, sizeof(*obuf), OCTETBUF, (in) ? in : stdin))) { + n = encode(obuf, n, sbuf, url); + last = printw((out) ? out : stdout, sbuf, n); + } + if (last != '\n') + fprintf((out) ? out : stdout, "\n"); + } else { + while ((n = fread(obuf, sizeof(*obuf), OCTETBUF, (in) ? in : stdin))) { + n = encode(obuf, n, sbuf, url); + fwrite(sbuf, sizeof(*sbuf), n, (out) ? out : stdout); + } + } + if (in) + fclose(in); + if (out) + fclose(out); } exit(EXIT_SUCCESS); } diff --git a/output.c b/output.c new file mode 100644 index 0000000..171c083 --- /dev/null +++ b/output.c @@ -0,0 +1,38 @@ +#include "output.h" + +#define WRAPCOL 76 + +char +printw(FILE *fp, unsigned char *s, int slen) +{ + static int col; + int c; + + c = '\n'; + if (col) { + if (slen >= WRAPCOL-col) { + fwrite(s, sizeof(*s), WRAPCOL-col, fp); + s += WRAPCOL-col; + slen -= WRAPCOL-col; + col = 0; + fputc('\n', fp); + } else { + fwrite(s, sizeof(*s), slen, fp); + s += slen; + col += slen; + slen -= slen; + c = *(s+slen-1); + } + } + for (; slen >= WRAPCOL; slen -= WRAPCOL, s += WRAPCOL) { + fwrite(s, sizeof(*s), WRAPCOL, fp); + fputc('\n', fp); + } + if (slen > 0) { + fwrite(s, sizeof(*s), slen, fp); + col += slen; + c = *(s+slen-1); + } + + return c; +} diff --git a/output.h b/output.h new file mode 100644 index 0000000..85736f2 --- /dev/null +++ b/output.h @@ -0,0 +1,9 @@ +#ifndef OUTPUT_H +#define OUTPUT_H + +#include + +char +printw(FILE *fp, unsigned char *s, int slen); + +#endif diff --git a/trans.c b/trans.c deleted file mode 100644 index c29c576..0000000 --- a/trans.c +++ /dev/null @@ -1,97 +0,0 @@ -#include "trans.h" - -#define PADDING '=' - -unsigned char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \ - "abcdefghijklmnopqrstuvwxyz" \ - "0123456789" \ - "+/"; - -unsigned char * -encode(unsigned char *op, int np) -{ - extern unsigned char s[]; - unsigned char *sp; - int n; - - sp = s; - switch (np) { - case 3: - *sp++ = b64[(*op & ~3) >> 2]; - n = (*op++ & 3) << 4; - *sp++ = b64[n + ((*op & ~15) >> 4)]; - n = (*op++ & 15) << 2; - *sp++ = b64[n + ((*op & 192) >> 6)]; - *sp = b64[*op & ~192]; - break; - case 2: - *sp++ = b64[(*op & ~3) >> 2]; - n = (*op++ & 3) << 4; - *sp++ = b64[n + ((*op & ~15) >> 4)]; - *sp++ = b64[(*op & 15) << 2]; - *sp = PADDING; - break; - case 1: - *sp++ = b64[(*op & ~3) >> 2]; - *sp++ = b64[(*op & 3) << 4]; - *sp++ = PADDING; - *sp = PADDING; - break; - } - - return s; -} - -unsigned char * -decode(unsigned char *sp, int *np) -{ - extern unsigned char o[]; - unsigned char *op; - int n, b, atob(int c); - - op = o; - switch (*np) { - case 4: - n = atob(*sp++) << 2; - b = atob(*sp++); - *op++ = n + ((b & ~15) >> 4); - - n = (b & 15) << 4; - b = atob(*sp++); - *op++ = n + ((b & ~3) >> 2); - - *op = ((b & 3) << 6) + atob(*sp); - break; - case 3: - n = atob(*sp++) << 2; - b = atob(*sp++); - *op++ = n + ((b & ~15) >> 4); - - n = (b & 15) << 4; - *op = n + ((atob(*sp) & ~3) >> 2); - break; - case 2: - n = atob(*sp++) << 2; - *op = n + ((atob(*sp) & ~15) >> 4); - break; - } - *np = op-o+1; - - return o; -} - -int atob(int c) -{ - if (c >= 'A' && c <= 'Z') - c -= 'A'; - else if (c >= 'a' && c <= 'z') - c = c - 'a' + 26; - else if (c >= '0' && c <= '9') - c = c - '0' + 26 * 2; - else if (c == '+') - c = 62; - else - c = 63; - - return c; -} diff --git a/trans.h b/trans.h deleted file mode 100644 index 845a2d2..0000000 --- a/trans.h +++ /dev/null @@ -1,12 +0,0 @@ -#ifndef HEADER_TRANS -#define HEADER_TRANS - -#include "input.h" - -unsigned char * -encode(unsigned char *op, int np); - -unsigned char * -decode(unsigned char *sp, int *np); - -#endif