Compare commits
3 commits
59bd416dc7
...
7106066261
Author | SHA1 | Date | |
---|---|---|---|
|
7106066261 | ||
|
44187d5618 | ||
|
99c88949ac |
6 changed files with 96 additions and 92 deletions
97
encode.c
97
encode.c
|
@ -33,75 +33,76 @@ unsigned char asciitob64[] = {
|
||||||
|
|
||||||
|
|
||||||
int
|
int
|
||||||
encode(unsigned char *op, int olen, unsigned char *sp, int url)
|
encode(unsigned char *octbuf, int octbufsize, unsigned char *sxtbuf, int urlencoded)
|
||||||
{
|
{
|
||||||
int tmp;
|
|
||||||
unsigned char *table;
|
unsigned char *table;
|
||||||
unsigned char *sbeg;
|
unsigned char *sxtbegin;
|
||||||
unsigned char *tend;
|
unsigned char *octleft;
|
||||||
|
int highorderbits;
|
||||||
|
|
||||||
table = (!url) ? b64toascii : b64urltoascii;
|
table = (!urlencoded) ? b64toascii : b64urltoascii;
|
||||||
|
|
||||||
sbeg = sp;
|
sxtbegin = sxtbuf;
|
||||||
tend = op + olen - (olen % 3);
|
octleft = octbuf + octbufsize - (octbufsize % 3);
|
||||||
while (op < tend) {
|
while (octbuf < octleft) {
|
||||||
*sp++ = table[(*op & ~3) >> 2];
|
*sxtbuf++ = table[(*octbuf & ~3) >> 2];
|
||||||
tmp = (*op++ & 3) << 4;
|
highorderbits = (*octbuf++ & 3) << 4;
|
||||||
*sp++ = table[tmp + ((*op & ~15) >> 4)];
|
*sxtbuf++ = table[highorderbits + ((*octbuf & ~15) >> 4)];
|
||||||
tmp = (*op++ & 15) << 2;
|
highorderbits = (*octbuf++ & 15) << 2;
|
||||||
*sp++ = table[tmp + ((*op & 192) >> 6)];
|
*sxtbuf++ = table[highorderbits + ((*octbuf & 192) >> 6)];
|
||||||
*sp++ = table[*op++ & ~192];
|
*sxtbuf++ = table[*octbuf++ & ~192];
|
||||||
}
|
}
|
||||||
switch (olen % 3) {
|
switch (octbufsize % 3) {
|
||||||
case 2:
|
case 2:
|
||||||
*sp++ = table[(*op & ~3) >> 2];
|
*sxtbuf++ = table[(*octbuf & ~3) >> 2];
|
||||||
tmp = (*op++ & 3) << 4;
|
highorderbits = (*octbuf++ & 3) << 4;
|
||||||
*sp++ = table[tmp + ((*op & ~15) >> 4)];
|
*sxtbuf++ = table[highorderbits + ((*octbuf & ~15) >> 4)];
|
||||||
*sp++ = table[(*op & 15) << 2];
|
*sxtbuf++ = table[(*octbuf & 15) << 2];
|
||||||
*sp++ = PADDING;
|
*sxtbuf++ = PADDING;
|
||||||
break;
|
break;
|
||||||
case 1:
|
case 1:
|
||||||
*sp++ = table[(*op & ~3) >> 2];
|
*sxtbuf++ = table[(*octbuf & ~3) >> 2];
|
||||||
*sp++ = table[(*op & 3) << 4];
|
*sxtbuf++ = table[(*octbuf & 3) << 4];
|
||||||
*sp++ = PADDING;
|
*sxtbuf++ = PADDING;
|
||||||
*sp++ = PADDING;
|
*sxtbuf++ = PADDING;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return sp-sbeg;
|
return sxtbuf-sxtbegin;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
decode(unsigned char *sp, int slen, unsigned char *op)
|
decode(unsigned char *sxtbuf, int sxtbufsize, unsigned char *octbuf)
|
||||||
{
|
{
|
||||||
int tmp, b;
|
unsigned char *octbegin;
|
||||||
unsigned char *obeg;
|
unsigned char *sxtleft;
|
||||||
unsigned char *qend;
|
int highorderbits;
|
||||||
|
int loworderbits;
|
||||||
|
|
||||||
obeg = op;
|
octbegin = octbuf;
|
||||||
qend = sp + slen - (slen % 4);
|
sxtleft = sxtbuf + sxtbufsize - (sxtbufsize % 4);
|
||||||
while (sp < qend) {
|
while (sxtbuf < sxtleft) {
|
||||||
tmp = asciitob64[*sp++] << 2;
|
highorderbits = asciitob64[*sxtbuf++] << 2;
|
||||||
b = asciitob64[*sp++];
|
loworderbits = asciitob64[*sxtbuf++];
|
||||||
*op++ = tmp + ((b & ~15) >> 4);
|
*octbuf++ = highorderbits + ((loworderbits & ~15) >> 4);
|
||||||
tmp = (b & 15) << 4;
|
highorderbits = (loworderbits & 15) << 4;
|
||||||
b = asciitob64[*sp++];
|
loworderbits = asciitob64[*sxtbuf++];
|
||||||
*op++ = tmp + ((b & ~3) >> 2);
|
*octbuf++ = highorderbits + ((loworderbits & ~3) >> 2);
|
||||||
*op++ = ((b & 3) << 6) + asciitob64[*sp++];
|
*octbuf++ = ((loworderbits & 3) << 6) + asciitob64[*sxtbuf++];
|
||||||
}
|
}
|
||||||
switch (slen % 4) {
|
switch (sxtbufsize % 4) {
|
||||||
case 3:
|
case 3:
|
||||||
tmp = asciitob64[*sp++] << 2;
|
highorderbits = asciitob64[*sxtbuf++] << 2;
|
||||||
b = asciitob64[*sp++];
|
loworderbits = asciitob64[*sxtbuf++];
|
||||||
*op++ = tmp + ((b & ~15) >> 4);
|
*octbuf++ = highorderbits + ((loworderbits & ~15) >> 4);
|
||||||
tmp = (b & 15) << 4;
|
highorderbits = (loworderbits & 15) << 4;
|
||||||
*op++ = tmp + ((asciitob64[*sp++] & ~3) >> 2);
|
*octbuf++ = highorderbits + ((asciitob64[*sxtbuf++] & ~3) >> 2);
|
||||||
break;
|
break;
|
||||||
case 2:
|
case 2:
|
||||||
tmp = asciitob64[*sp++] << 2;
|
highorderbits = asciitob64[*sxtbuf++] << 2;
|
||||||
*op++ = tmp + ((asciitob64[*sp++] & ~15) >> 4);
|
*octbuf++ = highorderbits + ((asciitob64[*sxtbuf++] & ~15) >> 4);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return op-obeg;
|
return octbuf-octbegin;
|
||||||
}
|
}
|
||||||
|
|
4
encode.h
4
encode.h
|
@ -2,9 +2,9 @@
|
||||||
#define ENCODE_H
|
#define ENCODE_H
|
||||||
|
|
||||||
int
|
int
|
||||||
encode(unsigned char *op, int olen, unsigned char *sp, int url);
|
encode(unsigned char *octbuf, int octbufsize, unsigned char *sxtbuf, int urlencoded);
|
||||||
|
|
||||||
int
|
int
|
||||||
decode(unsigned char *sp, int slen, unsigned char *op);
|
decode(unsigned char *sxtbuf, int sxtbufsize, unsigned char *octbuf);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
41
input.c
41
input.c
|
@ -3,27 +3,30 @@
|
||||||
#define PADDING '='
|
#define PADDING '='
|
||||||
|
|
||||||
int
|
int
|
||||||
readb(FILE *fp, unsigned char *s, int slen)
|
readb(FILE *fptr, unsigned char *buf, int bufsize)
|
||||||
{
|
{
|
||||||
int read, tread;
|
int read;
|
||||||
unsigned char *send, *sbeg;
|
int total;
|
||||||
unsigned char *l, *r;
|
unsigned char *begin;
|
||||||
|
unsigned char *end;
|
||||||
|
unsigned char *left;
|
||||||
|
unsigned char *right;
|
||||||
|
|
||||||
read = tread = 0;
|
read = total = 0;
|
||||||
sbeg = l = r = s;
|
begin = left = buf;
|
||||||
while (tread < slen && (read = fread(l, sizeof(*l), slen-tread, fp))) {
|
while (total < bufsize && (read = fread(left, sizeof(*left), bufsize-total, fptr))) {
|
||||||
tread += read;
|
total += read;
|
||||||
send = s + tread;
|
end = buf + total;
|
||||||
for (r = l; r < send; ++r)
|
for (right = left; right < end; ++right)
|
||||||
if (*r != '\n')
|
if (*right != '\n')
|
||||||
*l++ = *r;
|
*left++ = *right;
|
||||||
tread -= r - l;
|
total -= right - left;
|
||||||
if (l > sbeg) {
|
}
|
||||||
for (--l; *l == PADDING; --l)
|
if (left > begin) {
|
||||||
;
|
for (--left; *left == PADDING; --left)
|
||||||
++l;
|
;
|
||||||
}
|
++left;
|
||||||
}
|
}
|
||||||
|
|
||||||
return l-sbeg;
|
return left-begin;
|
||||||
}
|
}
|
||||||
|
|
2
input.h
2
input.h
|
@ -4,6 +4,6 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
int
|
int
|
||||||
readb(FILE *fp, unsigned char *s, int slen);
|
readb(FILE *fptr, unsigned char *buf, int bufsize);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
42
output.c
42
output.c
|
@ -3,35 +3,35 @@
|
||||||
#define WRAPCOL 76
|
#define WRAPCOL 76
|
||||||
|
|
||||||
char
|
char
|
||||||
printw(FILE *fp, unsigned char *s, int slen)
|
printw(FILE *fptr, unsigned char *buf, int bufsize)
|
||||||
{
|
{
|
||||||
static int col;
|
static int column;
|
||||||
int c;
|
int c;
|
||||||
|
|
||||||
c = '\n';
|
c = '\n';
|
||||||
if (col) {
|
if (column > 0) {
|
||||||
if (slen >= WRAPCOL-col) {
|
if (bufsize >= WRAPCOL-column) {
|
||||||
fwrite(s, sizeof(*s), WRAPCOL-col, fp);
|
fwrite(buf, sizeof(*buf), WRAPCOL-column, fptr);
|
||||||
s += WRAPCOL-col;
|
buf += WRAPCOL-column;
|
||||||
slen -= WRAPCOL-col;
|
bufsize -= WRAPCOL-column;
|
||||||
col = 0;
|
column = 0;
|
||||||
fputc('\n', fp);
|
fputc('\n', fptr);
|
||||||
} else {
|
} else {
|
||||||
fwrite(s, sizeof(*s), slen, fp);
|
fwrite(buf, sizeof(*buf), bufsize, fptr);
|
||||||
s += slen;
|
column += bufsize;
|
||||||
col += slen;
|
buf += bufsize;
|
||||||
slen -= slen;
|
bufsize -= bufsize;
|
||||||
c = *(s+slen-1);
|
c = *(buf+bufsize-1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (; slen >= WRAPCOL; slen -= WRAPCOL, s += WRAPCOL) {
|
for (; bufsize >= WRAPCOL; bufsize -= WRAPCOL, buf += WRAPCOL) {
|
||||||
fwrite(s, sizeof(*s), WRAPCOL, fp);
|
fwrite(buf, sizeof(*buf), WRAPCOL, fptr);
|
||||||
fputc('\n', fp);
|
fputc('\n', fptr);
|
||||||
}
|
}
|
||||||
if (slen > 0) {
|
if (bufsize > 0) {
|
||||||
fwrite(s, sizeof(*s), slen, fp);
|
fwrite(buf, sizeof(*buf), bufsize, fptr);
|
||||||
col += slen;
|
column += bufsize;
|
||||||
c = *(s+slen-1);
|
c = *(buf+bufsize-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
return c;
|
return c;
|
||||||
|
|
2
output.h
2
output.h
|
@ -4,6 +4,6 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
char
|
char
|
||||||
printw(FILE *fp, unsigned char *s, int slen);
|
printw(FILE *fptr, unsigned char *buf, int bufsize);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue