Compare commits

..

No commits in common. "main" and "v0.2" have entirely different histories.
main ... v0.2

8 changed files with 140 additions and 170 deletions

View file

@ -1,15 +1,14 @@
CC ::= gcc CFLAGS = -g -Wall -Wextra -Werror
CFLAGS ::= -Og -g -Wall -Wextra -Werror
objects ::= $(patsubst %.c, %.o, $(wildcard *.c)) objects = main.o input.o output.o encode.o
b64 : $(objects) b64 : $(objects)
$(CC) -o b64 $(objects) cc -o b64 $(objects)
main.o input.o : input.h main.o input.o : input.h
main.o encode.o : encode.h
main.o output.o : output.h main.o output.o : output.h
main.o encode.o : encode.h
.PHONY : clean .PHONY : clean
clean : clean :
-rm -f b64 $(objects) rm -f b64 $(objects)

154
encode.c
View file

@ -2,107 +2,99 @@
#define PADDING '=' #define PADDING '='
static unsigned char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
unsigned char b64toascii[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \ "abcdefghijklmnopqrstuvwxyz" \
"abcdefghijklmnopqrstuvwxyz" \ "0123456789" \
"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 b64u[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
"abcdefghijklmnopqrstuvwxyz" \
"0123456789" \
"-_";
int 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; int n;
unsigned char *sxtbegin; unsigned char *sb, *enc;
unsigned char *octleft;
int highorderbits;
table = (!urlencoded) ? b64toascii : b64urltoascii; sb = sp;
enc = (!url) ? b64 : b64u;
sxtbegin = sxtbuf; switch (olen) {
octleft = octbuf + octbufsize - (octbufsize % 3); case 3:
while (octbuf < octleft) { *sp++ = enc[(*op & ~3) >> 2];
*sxtbuf++ = table[(*octbuf & ~3) >> 2]; n = (*op++ & 3) << 4;
highorderbits = (*octbuf++ & 3) << 4; *sp++ = enc[n + ((*op & ~15) >> 4)];
*sxtbuf++ = table[highorderbits + ((*octbuf & ~15) >> 4)]; n = (*op++ & 15) << 2;
highorderbits = (*octbuf++ & 15) << 2; *sp++ = enc[n + ((*op & 192) >> 6)];
*sxtbuf++ = table[highorderbits + ((*octbuf & 192) >> 6)]; *sp = enc[*op & ~192];
*sxtbuf++ = table[*octbuf++ & ~192]; break;
}
switch (octbufsize % 3) {
case 2: case 2:
*sxtbuf++ = table[(*octbuf & ~3) >> 2]; *sp++ = enc[(*op & ~3) >> 2];
highorderbits = (*octbuf++ & 3) << 4; n = (*op++ & 3) << 4;
*sxtbuf++ = table[highorderbits + ((*octbuf & ~15) >> 4)]; *sp++ = enc[n + ((*op & ~15) >> 4)];
*sxtbuf++ = table[(*octbuf & 15) << 2]; *sp++ = enc[(*op & 15) << 2];
*sxtbuf++ = PADDING; *sp = PADDING;
break; break;
case 1: case 1:
*sxtbuf++ = table[(*octbuf & ~3) >> 2]; *sp++ = enc[(*op & ~3) >> 2];
*sxtbuf++ = table[(*octbuf & 3) << 4]; *sp++ = enc[(*op & 3) << 4];
*sxtbuf++ = PADDING; *sp++ = PADDING;
*sxtbuf++ = PADDING; *sp = PADDING;
break; break;
} }
return sxtbuf-sxtbegin; return sp-sb+1;
} }
int int
decode(unsigned char *sxtbuf, int sxtbufsize, unsigned char *octbuf) decode(unsigned char *sp, int slen, unsigned char *op, int url)
{ {
unsigned char *octbegin; int n, b, atob(int c, int url);
unsigned char *sxtleft; unsigned char *ob;
int highorderbits;
int loworderbits;
octbegin = octbuf; ob = op;
sxtleft = sxtbuf + sxtbufsize - (sxtbufsize % 4); switch (slen) {
while (sxtbuf < sxtleft) { case 4:
highorderbits = asciitob64[*sxtbuf++] << 2; n = atob(*sp++, url) << 2;
loworderbits = asciitob64[*sxtbuf++]; b = atob(*sp++, url);
*octbuf++ = highorderbits + ((loworderbits & ~15) >> 4); *op++ = n + ((b & ~15) >> 4);
highorderbits = (loworderbits & 15) << 4;
loworderbits = asciitob64[*sxtbuf++]; n = (b & 15) << 4;
*octbuf++ = highorderbits + ((loworderbits & ~3) >> 2); b = atob(*sp++, url);
*octbuf++ = ((loworderbits & 3) << 6) + asciitob64[*sxtbuf++]; *op++ = n + ((b & ~3) >> 2);
}
switch (sxtbufsize % 4) { *op = ((b & 3) << 6) + atob(*sp, url);
break;
case 3: case 3:
highorderbits = asciitob64[*sxtbuf++] << 2; n = atob(*sp++, url) << 2;
loworderbits = asciitob64[*sxtbuf++]; b = atob(*sp++, url);
*octbuf++ = highorderbits + ((loworderbits & ~15) >> 4); *op++ = n + ((b & ~15) >> 4);
highorderbits = (loworderbits & 15) << 4;
*octbuf++ = highorderbits + ((asciitob64[*sxtbuf++] & ~3) >> 2); n = (b & 15) << 4;
*op = n + ((atob(*sp, url) & ~3) >> 2);
break; break;
case 2: case 2:
highorderbits = asciitob64[*sxtbuf++] << 2; n = atob(*sp++, url) << 2;
*octbuf++ = highorderbits + ((asciitob64[*sxtbuf++] & ~15) >> 4); *op = n + ((atob(*sp, url) & ~15) >> 4);
break; 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;
} }

View file

@ -1,10 +1,10 @@
#ifndef ENCODE_H #ifndef HEADER_ENCODE
#define ENCODE_H #define HEADER_ENCODE
int int
encode(unsigned char *octbuf, int octbufsize, unsigned char *sxtbuf, int urlencoded); encode(unsigned char *op, int olen, unsigned char *sp, int url);
int int
decode(unsigned char *sxtbuf, int sxtbufsize, unsigned char *octbuf); decode(unsigned char *sp, int slen, unsigned char *op, int url);
#endif #endif

46
input.c
View file

@ -1,32 +1,30 @@
#include "input.h" #include "input.h"
#define PADDING '=' #define PADDING '='
int int
readb(FILE *fptr, unsigned char *buf, int bufsize) getocts(FILE *fp, unsigned char *o, int olen)
{ {
int read; int c, n;
int total;
unsigned char *begin;
unsigned char *end;
unsigned char *left;
unsigned char *right;
read = total = 0; n = 0;
begin = left = buf; while (n < olen-1 && (c = fgetc(fp)) != EOF)
while (total < bufsize && (read = fread(left, sizeof(*left), bufsize-total, fptr))) { o[n++] = c;
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;
}
return left-begin; 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;
} }

View file

@ -1,9 +1,12 @@
#ifndef INPUT_H #ifndef HEADER_INPUT
#define INPUT_H #define HEADER_INPUT
#include <stdio.h> #include <stdio.h>
int 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 #endif

41
main.c
View file

@ -4,21 +4,20 @@
#include "output.h" #include "output.h"
#include "encode.h" #include "encode.h"
#define OCTETBUF 1500 #define OBUFSIZE 4
#define SXTETBUF 2000 #define SBUFSIZE 5
unsigned char obuf[OCTETBUF]; unsigned char obuf[OBUFSIZE];
unsigned char sbuf[SXTETBUF]; unsigned char sbuf[SBUFSIZE];
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
int c, n, last; int c, n, l;
int dec, url, hlp, wrp; int dec, url, hlp, wrp;
char *prog = *argv;
FILE *in, *out; FILE *in, *out;
char *prog;
prog = *argv;
dec = url = hlp = wrp = 0; dec = url = hlp = wrp = 0;
while (--argc > 0 && (*++argv)[0] == '-') while (--argc > 0 && (*++argv)[0] == '-')
while ((c = *++argv[0])) while ((c = *++argv[0]))
@ -43,7 +42,6 @@ main(int argc, char *argv[])
if (hlp) { if (hlp) {
fprintf(stdout, "Usage: %s -duhw infile outfile\n", prog); fprintf(stdout, "Usage: %s -duhw infile outfile\n", prog);
} else { } else {
in = out = NULL;
if (argc >= 1 && (in = fopen(*argv, "r")) == NULL) { if (argc >= 1 && (in = fopen(*argv, "r")) == NULL) {
fprintf(stderr, "%s: can't open %s\n", prog, *argv); fprintf(stderr, "%s: can't open %s\n", prog, *argv);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
@ -53,23 +51,22 @@ main(int argc, char *argv[])
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if (dec) { if (dec) {
while ((n = readb((in) ? in : stdin, sbuf, SXTETBUF))) { while ((n = getsxts((argc >= 1) ? in : stdin, sbuf, SBUFSIZE))) {
n = decode(sbuf, n, obuf); n = decode(sbuf, n, obuf, url);
fwrite(obuf, sizeof(*obuf), n, (out) ? out : stdout); 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 { } else {
while ((n = fread(obuf, sizeof(*obuf), OCTETBUF, (in) ? in : stdin))) { while ((n = getocts((argc >= 1) ? in : stdin, obuf, OBUFSIZE))) {
n = encode(obuf, n, sbuf, url); if (wrp) {
fwrite(sbuf, sizeof(*sbuf), n, (out) ? out : stdout); 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) if (in)
fclose(in); fclose(in);

View file

@ -3,35 +3,16 @@
#define WRAPCOL 76 #define WRAPCOL 76
char 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; int c;
c = '\n'; while (slen--) {
if (column > 0) { fputc((c = *s++), fp);
if (bufsize >= WRAPCOL-column) { ++col;
fwrite(buf, sizeof(*buf), WRAPCOL-column, fptr); if (!(col %= WRAPCOL))
buf += WRAPCOL-column; fputc((c = '\n'), fp);
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);
} }
return c; return c;

View file

@ -1,9 +1,9 @@
#ifndef OUTPUT_H #ifndef HEADER_OUTPUT
#define OUTPUT_H #define HEADER_OUTPUT
#include <stdio.h> #include <stdio.h>
char char
printw(FILE *fptr, unsigned char *buf, int bufsize); printw(FILE *fp, unsigned char *s, int slen);
#endif #endif