From 99c88949ac60ced9b0b6d23392d1dc433b3ab437 Mon Sep 17 00:00:00 2001 From: Eric Date: Thu, 12 Jun 2025 11:32:47 +0200 Subject: [PATCH 1/3] increase readability of input.{c,h} --- input.c | 41 ++++++++++++++++++++++------------------- input.h | 2 +- 2 files changed, 23 insertions(+), 20 deletions(-) diff --git a/input.c b/input.c index 8e500b3..cb0a890 100644 --- a/input.c +++ b/input.c @@ -3,27 +3,30 @@ #define PADDING '=' int -readb(FILE *fp, unsigned char *s, int slen) +readb(FILE *fptr, unsigned char *buf, int bufsize) { - int read, tread; - unsigned char *send, *sbeg; - unsigned char *l, *r; + int read; + int total; + unsigned char *begin; + unsigned char *end; + unsigned char *left; + unsigned char *right; - read = tread = 0; - sbeg = l = r = s; - while (tread < slen && (read = fread(l, sizeof(*l), slen-tread, fp))) { - tread += read; - send = s + tread; - for (r = l; r < send; ++r) - if (*r != '\n') - *l++ = *r; - tread -= r - l; - if (l > sbeg) { - for (--l; *l == PADDING; --l) - ; - ++l; - } + 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; } - return l-sbeg; + return left-begin; } diff --git a/input.h b/input.h index 901b2c4..57bb5de 100644 --- a/input.h +++ b/input.h @@ -4,6 +4,6 @@ #include int -readb(FILE *fp, unsigned char *s, int slen); +readb(FILE *fptr, unsigned char *buf, int bufsize); #endif From 44187d56181d70552c41894a28253286cadfe456 Mon Sep 17 00:00:00 2001 From: Eric Date: Thu, 12 Jun 2025 23:53:00 +0200 Subject: [PATCH 2/3] increase readability of output.{c,h} --- output.c | 42 +++++++++++++++++++++--------------------- output.h | 2 +- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/output.c b/output.c index 171c083..f671883 100644 --- a/output.c +++ b/output.c @@ -3,35 +3,35 @@ #define WRAPCOL 76 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; c = '\n'; - if (col) { - if (slen >= WRAPCOL-col) { - fwrite(s, sizeof(*s), WRAPCOL-col, fp); - s += WRAPCOL-col; - slen -= WRAPCOL-col; - col = 0; - fputc('\n', fp); + 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(s, sizeof(*s), slen, fp); - s += slen; - col += slen; - slen -= slen; - c = *(s+slen-1); + fwrite(buf, sizeof(*buf), bufsize, fptr); + column += bufsize; + buf += bufsize; + bufsize -= bufsize; + c = *(buf+bufsize-1); } } - for (; slen >= WRAPCOL; slen -= WRAPCOL, s += WRAPCOL) { - fwrite(s, sizeof(*s), WRAPCOL, fp); - fputc('\n', fp); + for (; bufsize >= WRAPCOL; bufsize -= WRAPCOL, buf += WRAPCOL) { + fwrite(buf, sizeof(*buf), WRAPCOL, fptr); + fputc('\n', fptr); } - if (slen > 0) { - fwrite(s, sizeof(*s), slen, fp); - col += slen; - c = *(s+slen-1); + if (bufsize > 0) { + fwrite(buf, sizeof(*buf), bufsize, fptr); + column += bufsize; + c = *(buf+bufsize-1); } return c; diff --git a/output.h b/output.h index 85736f2..48feaa8 100644 --- a/output.h +++ b/output.h @@ -4,6 +4,6 @@ #include char -printw(FILE *fp, unsigned char *s, int slen); +printw(FILE *fptr, unsigned char *buf, int bufsize); #endif From 710606626181a454ecfd5515a05830a4663ae47a Mon Sep 17 00:00:00 2001 From: Eric Date: Fri, 13 Jun 2025 01:03:14 +0200 Subject: [PATCH 3/3] increase readability of encode.{c,h} --- encode.c | 97 ++++++++++++++++++++++++++++---------------------------- encode.h | 4 +-- 2 files changed, 51 insertions(+), 50 deletions(-) diff --git a/encode.c b/encode.c index 08d50f8..aa5f7f2 100644 --- a/encode.c +++ b/encode.c @@ -33,75 +33,76 @@ unsigned char asciitob64[] = { 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 *sbeg; - unsigned char *tend; + unsigned char *sxtbegin; + unsigned char *octleft; + int highorderbits; - table = (!url) ? b64toascii : b64urltoascii; + table = (!urlencoded) ? b64toascii : b64urltoascii; - sbeg = sp; - tend = op + olen - (olen % 3); - while (op < tend) { - *sp++ = table[(*op & ~3) >> 2]; - tmp = (*op++ & 3) << 4; - *sp++ = table[tmp + ((*op & ~15) >> 4)]; - tmp = (*op++ & 15) << 2; - *sp++ = table[tmp + ((*op & 192) >> 6)]; - *sp++ = table[*op++ & ~192]; + 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 (olen % 3) { + switch (octbufsize % 3) { case 2: - *sp++ = table[(*op & ~3) >> 2]; - tmp = (*op++ & 3) << 4; - *sp++ = table[tmp + ((*op & ~15) >> 4)]; - *sp++ = table[(*op & 15) << 2]; - *sp++ = PADDING; + *sxtbuf++ = table[(*octbuf & ~3) >> 2]; + highorderbits = (*octbuf++ & 3) << 4; + *sxtbuf++ = table[highorderbits + ((*octbuf & ~15) >> 4)]; + *sxtbuf++ = table[(*octbuf & 15) << 2]; + *sxtbuf++ = PADDING; break; case 1: - *sp++ = table[(*op & ~3) >> 2]; - *sp++ = table[(*op & 3) << 4]; - *sp++ = PADDING; - *sp++ = PADDING; + *sxtbuf++ = table[(*octbuf & ~3) >> 2]; + *sxtbuf++ = table[(*octbuf & 3) << 4]; + *sxtbuf++ = PADDING; + *sxtbuf++ = PADDING; break; } - return sp-sbeg; + return sxtbuf-sxtbegin; } int -decode(unsigned char *sp, int slen, unsigned char *op) +decode(unsigned char *sxtbuf, int sxtbufsize, unsigned char *octbuf) { - int tmp, b; - unsigned char *obeg; - unsigned char *qend; + unsigned char *octbegin; + unsigned char *sxtleft; + int highorderbits; + int loworderbits; - obeg = op; - qend = sp + slen - (slen % 4); - while (sp < qend) { - tmp = asciitob64[*sp++] << 2; - b = asciitob64[*sp++]; - *op++ = tmp + ((b & ~15) >> 4); - tmp = (b & 15) << 4; - b = asciitob64[*sp++]; - *op++ = tmp + ((b & ~3) >> 2); - *op++ = ((b & 3) << 6) + asciitob64[*sp++]; + 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 (slen % 4) { + switch (sxtbufsize % 4) { case 3: - tmp = asciitob64[*sp++] << 2; - b = asciitob64[*sp++]; - *op++ = tmp + ((b & ~15) >> 4); - tmp = (b & 15) << 4; - *op++ = tmp + ((asciitob64[*sp++] & ~3) >> 2); + highorderbits = asciitob64[*sxtbuf++] << 2; + loworderbits = asciitob64[*sxtbuf++]; + *octbuf++ = highorderbits + ((loworderbits & ~15) >> 4); + highorderbits = (loworderbits & 15) << 4; + *octbuf++ = highorderbits + ((asciitob64[*sxtbuf++] & ~3) >> 2); break; case 2: - tmp = asciitob64[*sp++] << 2; - *op++ = tmp + ((asciitob64[*sp++] & ~15) >> 4); + highorderbits = asciitob64[*sxtbuf++] << 2; + *octbuf++ = highorderbits + ((asciitob64[*sxtbuf++] & ~15) >> 4); break; } - return op-obeg; + return octbuf-octbegin; } diff --git a/encode.h b/encode.h index 7e6f217..5c48d1a 100644 --- a/encode.h +++ b/encode.h @@ -2,9 +2,9 @@ #define ENCODE_H int -encode(unsigned char *op, int olen, unsigned char *sp, int url); +encode(unsigned char *octbuf, int octbufsize, unsigned char *sxtbuf, int urlencoded); int -decode(unsigned char *sp, int slen, unsigned char *op); +decode(unsigned char *sxtbuf, int sxtbufsize, unsigned char *octbuf); #endif