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 ::= -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)

142
encode.c
View file

@ -2,107 +2,99 @@
#define PADDING '='
static
unsigned char b64toascii[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
unsigned char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
"abcdefghijklmnopqrstuvwxyz" \
"0123456789" \
"+/";
static
unsigned char b64urltoascii[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
unsigned char b64u[] = "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 *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;
}

View file

@ -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

42
input.c
View file

@ -3,30 +3,28 @@
#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;
int c, n;
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;
n = 0;
while (n < olen-1 && (c = fgetc(fp)) != EOF)
o[n++] = c;
return n;
}
return left-begin;
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
#define INPUT_H
#ifndef HEADER_INPUT
#define HEADER_INPUT
#include <stdio.h>
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

39
main.c
View file

@ -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,24 +51,23 @@ 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))) {
while ((n = getocts((argc >= 1) ? in : stdin, obuf, OBUFSIZE))) {
if (wrp) {
n = encode(obuf, n, sbuf, url);
fwrite(sbuf, sizeof(*sbuf), n, (out) ? out : stdout);
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);
if (out)

View file

@ -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;

View file

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