diff --git a/Makefile b/Makefile index 1053c2c..ff2a3f4 100644 --- a/Makefile +++ b/Makefile @@ -1,15 +1,14 @@ -CC ::= gcc -CFLAGS ::= -Og -g -Wall -Wextra -Werror +CFLAGS = -g -Wall -Wextra -Werror -objects ::= $(patsubst %.c, %.o, $(wildcard *.c)) +objects = main.o input.o output.o encode.o b64 : $(objects) - $(CC) -o b64 $(objects) + cc -o b64 $(objects) main.o input.o : input.h -main.o encode.o : encode.h main.o output.o : output.h +main.o encode.o : encode.h .PHONY : clean clean : - -rm -f b64 $(objects) + rm -f b64 $(objects) diff --git a/encode.c b/encode.c index aa5f7f2..cee611f 100644 --- a/encode.c +++ b/encode.c @@ -2,107 +2,99 @@ #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 -}; +unsigned char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \ + "abcdefghijklmnopqrstuvwxyz" \ + "0123456789" \ + "+/"; +unsigned char b64u[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \ + "abcdefghijklmnopqrstuvwxyz" \ + "0123456789" \ + "-_"; int -encode(unsigned char *octbuf, int octbufsize, unsigned char *sxtbuf, int urlencoded) +encode(unsigned char *op, int olen, unsigned char *sp, int url) { - unsigned char *table; - unsigned char *sxtbegin; - unsigned char *octleft; - int highorderbits; + int n; + unsigned char *sb, *enc; - table = (!urlencoded) ? b64toascii : b64urltoascii; - - sxtbegin = sxtbuf; - octleft = octbuf + octbufsize - (octbufsize % 3); - while (octbuf < octleft) { - *sxtbuf++ = table[(*octbuf & ~3) >> 2]; - highorderbits = (*octbuf++ & 3) << 4; - *sxtbuf++ = table[highorderbits + ((*octbuf & ~15) >> 4)]; - highorderbits = (*octbuf++ & 15) << 2; - *sxtbuf++ = table[highorderbits + ((*octbuf & 192) >> 6)]; - *sxtbuf++ = table[*octbuf++ & ~192]; - } - switch (octbufsize % 3) { + sb = sp; + enc = (!url) ? b64 : b64u; + switch (olen) { + case 3: + *sp++ = enc[(*op & ~3) >> 2]; + n = (*op++ & 3) << 4; + *sp++ = enc[n + ((*op & ~15) >> 4)]; + n = (*op++ & 15) << 2; + *sp++ = enc[n + ((*op & 192) >> 6)]; + *sp = enc[*op & ~192]; + break; case 2: - *sxtbuf++ = table[(*octbuf & ~3) >> 2]; - highorderbits = (*octbuf++ & 3) << 4; - *sxtbuf++ = table[highorderbits + ((*octbuf & ~15) >> 4)]; - *sxtbuf++ = table[(*octbuf & 15) << 2]; - *sxtbuf++ = PADDING; + *sp++ = enc[(*op & ~3) >> 2]; + n = (*op++ & 3) << 4; + *sp++ = enc[n + ((*op & ~15) >> 4)]; + *sp++ = enc[(*op & 15) << 2]; + *sp = PADDING; break; case 1: - *sxtbuf++ = table[(*octbuf & ~3) >> 2]; - *sxtbuf++ = table[(*octbuf & 3) << 4]; - *sxtbuf++ = PADDING; - *sxtbuf++ = PADDING; + *sp++ = enc[(*op & ~3) >> 2]; + *sp++ = enc[(*op & 3) << 4]; + *sp++ = PADDING; + *sp = PADDING; break; } - return sxtbuf-sxtbegin; + return sp-sb+1; } int -decode(unsigned char *sxtbuf, int sxtbufsize, unsigned char *octbuf) +decode(unsigned char *sp, int slen, unsigned char *op, int url) { - unsigned char *octbegin; - unsigned char *sxtleft; - int highorderbits; - int loworderbits; + int n, b, atob(int c, int url); + unsigned char *ob; - octbegin = octbuf; - sxtleft = sxtbuf + sxtbufsize - (sxtbufsize % 4); - while (sxtbuf < sxtleft) { - highorderbits = asciitob64[*sxtbuf++] << 2; - loworderbits = asciitob64[*sxtbuf++]; - *octbuf++ = highorderbits + ((loworderbits & ~15) >> 4); - highorderbits = (loworderbits & 15) << 4; - loworderbits = asciitob64[*sxtbuf++]; - *octbuf++ = highorderbits + ((loworderbits & ~3) >> 2); - *octbuf++ = ((loworderbits & 3) << 6) + asciitob64[*sxtbuf++]; - } - switch (sxtbufsize % 4) { + ob = op; + switch (slen) { + case 4: + n = atob(*sp++, url) << 2; + b = atob(*sp++, url); + *op++ = n + ((b & ~15) >> 4); + + n = (b & 15) << 4; + b = atob(*sp++, url); + *op++ = n + ((b & ~3) >> 2); + + *op = ((b & 3) << 6) + atob(*sp, url); + break; case 3: - highorderbits = asciitob64[*sxtbuf++] << 2; - loworderbits = asciitob64[*sxtbuf++]; - *octbuf++ = highorderbits + ((loworderbits & ~15) >> 4); - highorderbits = (loworderbits & 15) << 4; - *octbuf++ = highorderbits + ((asciitob64[*sxtbuf++] & ~3) >> 2); + n = atob(*sp++, url) << 2; + b = atob(*sp++, url); + *op++ = n + ((b & ~15) >> 4); + + n = (b & 15) << 4; + *op = n + ((atob(*sp, url) & ~3) >> 2); break; case 2: - highorderbits = asciitob64[*sxtbuf++] << 2; - *octbuf++ = highorderbits + ((asciitob64[*sxtbuf++] & ~15) >> 4); + n = atob(*sp++, url) << 2; + *op = n + ((atob(*sp, url) & ~15) >> 4); break; } - return octbuf-octbegin; + return op-ob+1; +} + +int atob(int c, int url) +{ + 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 == ((!url) ? '+' : '-')) + c = 62; + else + c = 63; + + return c; } diff --git a/encode.h b/encode.h index 5c48d1a..2ea285a 100644 --- a/encode.h +++ b/encode.h @@ -1,10 +1,10 @@ -#ifndef ENCODE_H -#define ENCODE_H +#ifndef HEADER_ENCODE +#define HEADER_ENCODE int -encode(unsigned char *octbuf, int octbufsize, unsigned char *sxtbuf, int urlencoded); +encode(unsigned char *op, int olen, unsigned char *sp, int url); int -decode(unsigned char *sxtbuf, int sxtbufsize, unsigned char *octbuf); +decode(unsigned char *sp, int slen, unsigned char *op, int url); #endif diff --git a/input.c b/input.c index cb0a890..cfe80b1 100644 --- a/input.c +++ b/input.c @@ -1,32 +1,30 @@ #include "input.h" -#define PADDING '=' +#define PADDING '=' int -readb(FILE *fptr, unsigned char *buf, int bufsize) +getocts(FILE *fp, unsigned char *o, int olen) { - int read; - int total; - unsigned char *begin; - unsigned char *end; - unsigned char *left; - unsigned char *right; - - read = total = 0; - begin = left = buf; - while (total < bufsize && (read = fread(left, sizeof(*left), bufsize-total, fptr))) { - total += read; - end = buf + total; - for (right = left; right < end; ++right) - if (*right != '\n') - *left++ = *right; - total -= right - left; - } - if (left > begin) { - for (--left; *left == PADDING; --left) - ; - ++left; - } + int c, n; - return left-begin; + n = 0; + while (n < olen-1 && (c = fgetc(fp)) != EOF) + o[n++] = c; + + return n; +} + +int +getsxts(FILE *fp, unsigned char *s, int slen) +{ + int c, n, pad; + + n = pad = 0; + while (n < slen-1 && (c = fgetc(fp)) != EOF && c != PADDING) + if (c != '\n') + s[n++] = c; + while (n+pad < slen-1) + s[n+pad++] = PADDING; + + return n; } diff --git a/input.h b/input.h index 57bb5de..42bd4a6 100644 --- a/input.h +++ b/input.h @@ -1,9 +1,12 @@ -#ifndef INPUT_H -#define INPUT_H +#ifndef HEADER_INPUT +#define HEADER_INPUT #include int -readb(FILE *fptr, unsigned char *buf, int bufsize); +getocts(FILE *fp, unsigned char *o, int olen); + +int +getsxts(FILE *fp, unsigned char *s, int slen); #endif diff --git a/main.c b/main.c index 54098a2..0cf51bc 100644 --- a/main.c +++ b/main.c @@ -4,21 +4,20 @@ #include "output.h" #include "encode.h" -#define OCTETBUF 1500 -#define SXTETBUF 2000 +#define OBUFSIZE 4 +#define SBUFSIZE 5 -unsigned char obuf[OCTETBUF]; -unsigned char sbuf[SXTETBUF]; +unsigned char obuf[OBUFSIZE]; +unsigned char sbuf[SBUFSIZE]; int main(int argc, char *argv[]) { - int c, n, last; + int c, n, l; int dec, url, hlp, wrp; + char *prog = *argv; FILE *in, *out; - char *prog; - prog = *argv; dec = url = hlp = wrp = 0; while (--argc > 0 && (*++argv)[0] == '-') while ((c = *++argv[0])) @@ -43,7 +42,6 @@ main(int argc, char *argv[]) if (hlp) { fprintf(stdout, "Usage: %s -duhw infile outfile\n", prog); } else { - in = out = NULL; if (argc >= 1 && (in = fopen(*argv, "r")) == NULL) { fprintf(stderr, "%s: can't open %s\n", prog, *argv); exit(EXIT_FAILURE); @@ -53,23 +51,22 @@ main(int argc, char *argv[]) exit(EXIT_FAILURE); } if (dec) { - while ((n = readb((in) ? in : stdin, sbuf, SXTETBUF))) { - n = decode(sbuf, n, obuf); - fwrite(obuf, sizeof(*obuf), n, (out) ? out : stdout); + while ((n = getsxts((argc >= 1) ? in : stdin, sbuf, SBUFSIZE))) { + n = decode(sbuf, n, obuf, url); + fwrite(obuf, sizeof(*obuf), n, (argc == 2) ? out : stdout); } - } else if (wrp) { - last = '\n'; - 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); + while ((n = getocts((argc >= 1) ? in : stdin, obuf, OBUFSIZE))) { + if (wrp) { + n = encode(obuf, n, sbuf, url); + l = printw((argc == 2) ? out : stdout, sbuf, n); + } else { + encode(obuf, n, sbuf, url); + fprintf((argc == 2) ? out : stdout, "%s", sbuf); + } } + if (wrp && l != '\n') + fprintf((argc == 2) ? out : stdout, "\n"); } if (in) fclose(in); diff --git a/output.c b/output.c index f671883..d5c1a3a 100644 --- a/output.c +++ b/output.c @@ -3,35 +3,16 @@ #define WRAPCOL 76 char -printw(FILE *fptr, unsigned char *buf, int bufsize) +printw(FILE *fp, unsigned char *s, int slen) { - static int column; + static int col; int c; - c = '\n'; - if (column > 0) { - if (bufsize >= WRAPCOL-column) { - fwrite(buf, sizeof(*buf), WRAPCOL-column, fptr); - buf += WRAPCOL-column; - bufsize -= WRAPCOL-column; - column = 0; - fputc('\n', fptr); - } else { - fwrite(buf, sizeof(*buf), bufsize, fptr); - column += bufsize; - buf += bufsize; - bufsize -= bufsize; - c = *(buf+bufsize-1); - } - } - for (; bufsize >= WRAPCOL; bufsize -= WRAPCOL, buf += WRAPCOL) { - fwrite(buf, sizeof(*buf), WRAPCOL, fptr); - fputc('\n', fptr); - } - if (bufsize > 0) { - fwrite(buf, sizeof(*buf), bufsize, fptr); - column += bufsize; - c = *(buf+bufsize-1); + while (slen--) { + fputc((c = *s++), fp); + ++col; + if (!(col %= WRAPCOL)) + fputc((c = '\n'), fp); } return c; diff --git a/output.h b/output.h index 48feaa8..8135e20 100644 --- a/output.h +++ b/output.h @@ -1,9 +1,9 @@ -#ifndef OUTPUT_H -#define OUTPUT_H +#ifndef HEADER_OUTPUT +#define HEADER_OUTPUT #include char -printw(FILE *fptr, unsigned char *buf, int bufsize); +printw(FILE *fp, unsigned char *s, int slen); #endif