Compare commits

..

No commits in common. "710606626181a454ecfd5515a05830a4663ae47a" and "59bd416dc7f5fbe135d125480b6b9ac993ead780" have entirely different histories.

6 changed files with 92 additions and 96 deletions

View file

@ -33,76 +33,75 @@ unsigned char asciitob64[] = {
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 tmp;
unsigned char *table; unsigned char *table;
unsigned char *sxtbegin; unsigned char *sbeg;
unsigned char *octleft; unsigned char *tend;
int highorderbits;
table = (!urlencoded) ? b64toascii : b64urltoascii; table = (!url) ? b64toascii : b64urltoascii;
sxtbegin = sxtbuf; sbeg = sp;
octleft = octbuf + octbufsize - (octbufsize % 3); tend = op + olen - (olen % 3);
while (octbuf < octleft) { while (op < tend) {
*sxtbuf++ = table[(*octbuf & ~3) >> 2]; *sp++ = table[(*op & ~3) >> 2];
highorderbits = (*octbuf++ & 3) << 4; tmp = (*op++ & 3) << 4;
*sxtbuf++ = table[highorderbits + ((*octbuf & ~15) >> 4)]; *sp++ = table[tmp + ((*op & ~15) >> 4)];
highorderbits = (*octbuf++ & 15) << 2; tmp = (*op++ & 15) << 2;
*sxtbuf++ = table[highorderbits + ((*octbuf & 192) >> 6)]; *sp++ = table[tmp + ((*op & 192) >> 6)];
*sxtbuf++ = table[*octbuf++ & ~192]; *sp++ = table[*op++ & ~192];
} }
switch (octbufsize % 3) { switch (olen % 3) {
case 2: case 2:
*sxtbuf++ = table[(*octbuf & ~3) >> 2]; *sp++ = table[(*op & ~3) >> 2];
highorderbits = (*octbuf++ & 3) << 4; tmp = (*op++ & 3) << 4;
*sxtbuf++ = table[highorderbits + ((*octbuf & ~15) >> 4)]; *sp++ = table[tmp + ((*op & ~15) >> 4)];
*sxtbuf++ = table[(*octbuf & 15) << 2]; *sp++ = table[(*op & 15) << 2];
*sxtbuf++ = PADDING; *sp++ = PADDING;
break; break;
case 1: case 1:
*sxtbuf++ = table[(*octbuf & ~3) >> 2]; *sp++ = table[(*op & ~3) >> 2];
*sxtbuf++ = table[(*octbuf & 3) << 4]; *sp++ = table[(*op & 3) << 4];
*sxtbuf++ = PADDING; *sp++ = PADDING;
*sxtbuf++ = PADDING; *sp++ = PADDING;
break; break;
} }
return sxtbuf-sxtbegin; return sp-sbeg;
} }
int int
decode(unsigned char *sxtbuf, int sxtbufsize, unsigned char *octbuf) decode(unsigned char *sp, int slen, unsigned char *op)
{ {
unsigned char *octbegin; int tmp, b;
unsigned char *sxtleft; unsigned char *obeg;
int highorderbits; unsigned char *qend;
int loworderbits;
octbegin = octbuf; obeg = op;
sxtleft = sxtbuf + sxtbufsize - (sxtbufsize % 4); qend = sp + slen - (slen % 4);
while (sxtbuf < sxtleft) { while (sp < qend) {
highorderbits = asciitob64[*sxtbuf++] << 2; tmp = asciitob64[*sp++] << 2;
loworderbits = asciitob64[*sxtbuf++]; b = asciitob64[*sp++];
*octbuf++ = highorderbits + ((loworderbits & ~15) >> 4); *op++ = tmp + ((b & ~15) >> 4);
highorderbits = (loworderbits & 15) << 4; tmp = (b & 15) << 4;
loworderbits = asciitob64[*sxtbuf++]; b = asciitob64[*sp++];
*octbuf++ = highorderbits + ((loworderbits & ~3) >> 2); *op++ = tmp + ((b & ~3) >> 2);
*octbuf++ = ((loworderbits & 3) << 6) + asciitob64[*sxtbuf++]; *op++ = ((b & 3) << 6) + asciitob64[*sp++];
} }
switch (sxtbufsize % 4) { switch (slen % 4) {
case 3: case 3:
highorderbits = asciitob64[*sxtbuf++] << 2; tmp = asciitob64[*sp++] << 2;
loworderbits = asciitob64[*sxtbuf++]; b = asciitob64[*sp++];
*octbuf++ = highorderbits + ((loworderbits & ~15) >> 4); *op++ = tmp + ((b & ~15) >> 4);
highorderbits = (loworderbits & 15) << 4; tmp = (b & 15) << 4;
*octbuf++ = highorderbits + ((asciitob64[*sxtbuf++] & ~3) >> 2); *op++ = tmp + ((asciitob64[*sp++] & ~3) >> 2);
break; break;
case 2: case 2:
highorderbits = asciitob64[*sxtbuf++] << 2; tmp = asciitob64[*sp++] << 2;
*octbuf++ = highorderbits + ((asciitob64[*sxtbuf++] & ~15) >> 4); *op++ = tmp + ((asciitob64[*sp++] & ~15) >> 4);
break; break;
} }
return octbuf-octbegin; return op-obeg;
} }

View file

@ -2,9 +2,9 @@
#define ENCODE_H #define ENCODE_H
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);
#endif #endif

39
input.c
View file

@ -3,30 +3,27 @@
#define PADDING '=' #define PADDING '='
int int
readb(FILE *fptr, unsigned char *buf, int bufsize) readb(FILE *fp, unsigned char *s, int slen)
{ {
int read; int read, tread;
int total; unsigned char *send, *sbeg;
unsigned char *begin; unsigned char *l, *r;
unsigned char *end;
unsigned char *left;
unsigned char *right;
read = total = 0; read = tread = 0;
begin = left = buf; sbeg = l = r = s;
while (total < bufsize && (read = fread(left, sizeof(*left), bufsize-total, fptr))) { while (tread < slen && (read = fread(l, sizeof(*l), slen-tread, fp))) {
total += read; tread += read;
end = buf + total; send = s + tread;
for (right = left; right < end; ++right) for (r = l; r < send; ++r)
if (*right != '\n') if (*r != '\n')
*left++ = *right; *l++ = *r;
total -= right - left; tread -= r - l;
} if (l > sbeg) {
if (left > begin) { for (--l; *l == PADDING; --l)
for (--left; *left == PADDING; --left)
; ;
++left; ++l;
}
} }
return left-begin; return l-sbeg;
} }

View file

@ -4,6 +4,6 @@
#include <stdio.h> #include <stdio.h>
int int
readb(FILE *fptr, unsigned char *buf, int bufsize); readb(FILE *fp, unsigned char *s, int slen);
#endif #endif

View file

@ -3,35 +3,35 @@
#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'; c = '\n';
if (column > 0) { if (col) {
if (bufsize >= WRAPCOL-column) { if (slen >= WRAPCOL-col) {
fwrite(buf, sizeof(*buf), WRAPCOL-column, fptr); fwrite(s, sizeof(*s), WRAPCOL-col, fp);
buf += WRAPCOL-column; s += WRAPCOL-col;
bufsize -= WRAPCOL-column; slen -= WRAPCOL-col;
column = 0; col = 0;
fputc('\n', fptr); fputc('\n', fp);
} else { } else {
fwrite(buf, sizeof(*buf), bufsize, fptr); fwrite(s, sizeof(*s), slen, fp);
column += bufsize; s += slen;
buf += bufsize; col += slen;
bufsize -= bufsize; slen -= slen;
c = *(buf+bufsize-1); c = *(s+slen-1);
} }
} }
for (; bufsize >= WRAPCOL; bufsize -= WRAPCOL, buf += WRAPCOL) { for (; slen >= WRAPCOL; slen -= WRAPCOL, s += WRAPCOL) {
fwrite(buf, sizeof(*buf), WRAPCOL, fptr); fwrite(s, sizeof(*s), WRAPCOL, fp);
fputc('\n', fptr); fputc('\n', fp);
} }
if (bufsize > 0) { if (slen > 0) {
fwrite(buf, sizeof(*buf), bufsize, fptr); fwrite(s, sizeof(*s), slen, fp);
column += bufsize; col += slen;
c = *(buf+bufsize-1); c = *(s+slen-1);
} }
return c; return c;

View file

@ -4,6 +4,6 @@
#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