Compare commits
14 commits
Author | SHA1 | Date | |
---|---|---|---|
|
7106066261 | ||
|
44187d5618 | ||
|
99c88949ac | ||
|
59bd416dc7 | ||
|
06b8c2a046 | ||
|
aacf2324c7 | ||
|
debab62506 | ||
|
0a4a98390d | ||
|
f0577d8ea0 | ||
|
3194981825 | ||
|
4e8bb86ddd | ||
|
733a013ee7 | ||
|
fa5095133a | ||
|
b76c50a91b |
8 changed files with 170 additions and 140 deletions
11
Makefile
11
Makefile
|
@ -1,14 +1,15 @@
|
||||||
CFLAGS = -g -Wall -Wextra -Werror
|
CC ::= gcc
|
||||||
|
CFLAGS ::= -Og -g -Wall -Wextra -Werror
|
||||||
|
|
||||||
objects = main.o input.o output.o encode.o
|
objects ::= $(patsubst %.c, %.o, $(wildcard *.c))
|
||||||
|
|
||||||
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 output.o : output.h
|
|
||||||
main.o encode.o : encode.h
|
main.o encode.o : encode.h
|
||||||
|
main.o output.o : output.h
|
||||||
|
|
||||||
.PHONY : clean
|
.PHONY : clean
|
||||||
clean :
|
clean :
|
||||||
rm -f b64 $(objects)
|
-rm -f b64 $(objects)
|
||||||
|
|
154
encode.c
154
encode.c
|
@ -2,99 +2,107 @@
|
||||||
|
|
||||||
#define PADDING '='
|
#define PADDING '='
|
||||||
|
|
||||||
unsigned char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
|
static
|
||||||
"abcdefghijklmnopqrstuvwxyz" \
|
unsigned char b64toascii[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
|
||||||
"0123456789" \
|
"abcdefghijklmnopqrstuvwxyz" \
|
||||||
"+/";
|
"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 *op, int olen, unsigned char *sp, int url)
|
encode(unsigned char *octbuf, int octbufsize, unsigned char *sxtbuf, int urlencoded)
|
||||||
{
|
{
|
||||||
int n;
|
unsigned char *table;
|
||||||
unsigned char *sb, *enc;
|
unsigned char *sxtbegin;
|
||||||
|
unsigned char *octleft;
|
||||||
|
int highorderbits;
|
||||||
|
|
||||||
sb = sp;
|
table = (!urlencoded) ? b64toascii : b64urltoascii;
|
||||||
enc = (!url) ? b64 : b64u;
|
|
||||||
switch (olen) {
|
sxtbegin = sxtbuf;
|
||||||
case 3:
|
octleft = octbuf + octbufsize - (octbufsize % 3);
|
||||||
*sp++ = enc[(*op & ~3) >> 2];
|
while (octbuf < octleft) {
|
||||||
n = (*op++ & 3) << 4;
|
*sxtbuf++ = table[(*octbuf & ~3) >> 2];
|
||||||
*sp++ = enc[n + ((*op & ~15) >> 4)];
|
highorderbits = (*octbuf++ & 3) << 4;
|
||||||
n = (*op++ & 15) << 2;
|
*sxtbuf++ = table[highorderbits + ((*octbuf & ~15) >> 4)];
|
||||||
*sp++ = enc[n + ((*op & 192) >> 6)];
|
highorderbits = (*octbuf++ & 15) << 2;
|
||||||
*sp = enc[*op & ~192];
|
*sxtbuf++ = table[highorderbits + ((*octbuf & 192) >> 6)];
|
||||||
break;
|
*sxtbuf++ = table[*octbuf++ & ~192];
|
||||||
|
}
|
||||||
|
switch (octbufsize % 3) {
|
||||||
case 2:
|
case 2:
|
||||||
*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)];
|
||||||
*sp++ = enc[(*op & 15) << 2];
|
*sxtbuf++ = table[(*octbuf & 15) << 2];
|
||||||
*sp = PADDING;
|
*sxtbuf++ = PADDING;
|
||||||
break;
|
break;
|
||||||
case 1:
|
case 1:
|
||||||
*sp++ = enc[(*op & ~3) >> 2];
|
*sxtbuf++ = table[(*octbuf & ~3) >> 2];
|
||||||
*sp++ = enc[(*op & 3) << 4];
|
*sxtbuf++ = table[(*octbuf & 3) << 4];
|
||||||
*sp++ = PADDING;
|
*sxtbuf++ = PADDING;
|
||||||
*sp = PADDING;
|
*sxtbuf++ = PADDING;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return sp-sb+1;
|
return sxtbuf-sxtbegin;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
decode(unsigned char *sp, int slen, unsigned char *op, int url)
|
decode(unsigned char *sxtbuf, int sxtbufsize, unsigned char *octbuf)
|
||||||
{
|
{
|
||||||
int n, b, atob(int c, int url);
|
unsigned char *octbegin;
|
||||||
unsigned char *ob;
|
unsigned char *sxtleft;
|
||||||
|
int highorderbits;
|
||||||
|
int loworderbits;
|
||||||
|
|
||||||
ob = op;
|
octbegin = octbuf;
|
||||||
switch (slen) {
|
sxtleft = sxtbuf + sxtbufsize - (sxtbufsize % 4);
|
||||||
case 4:
|
while (sxtbuf < sxtleft) {
|
||||||
n = atob(*sp++, url) << 2;
|
highorderbits = asciitob64[*sxtbuf++] << 2;
|
||||||
b = atob(*sp++, url);
|
loworderbits = asciitob64[*sxtbuf++];
|
||||||
*op++ = n + ((b & ~15) >> 4);
|
*octbuf++ = highorderbits + ((loworderbits & ~15) >> 4);
|
||||||
|
highorderbits = (loworderbits & 15) << 4;
|
||||||
n = (b & 15) << 4;
|
loworderbits = asciitob64[*sxtbuf++];
|
||||||
b = atob(*sp++, url);
|
*octbuf++ = highorderbits + ((loworderbits & ~3) >> 2);
|
||||||
*op++ = n + ((b & ~3) >> 2);
|
*octbuf++ = ((loworderbits & 3) << 6) + asciitob64[*sxtbuf++];
|
||||||
|
}
|
||||||
*op = ((b & 3) << 6) + atob(*sp, url);
|
switch (sxtbufsize % 4) {
|
||||||
break;
|
|
||||||
case 3:
|
case 3:
|
||||||
n = atob(*sp++, url) << 2;
|
highorderbits = asciitob64[*sxtbuf++] << 2;
|
||||||
b = atob(*sp++, url);
|
loworderbits = asciitob64[*sxtbuf++];
|
||||||
*op++ = n + ((b & ~15) >> 4);
|
*octbuf++ = highorderbits + ((loworderbits & ~15) >> 4);
|
||||||
|
highorderbits = (loworderbits & 15) << 4;
|
||||||
n = (b & 15) << 4;
|
*octbuf++ = highorderbits + ((asciitob64[*sxtbuf++] & ~3) >> 2);
|
||||||
*op = n + ((atob(*sp, url) & ~3) >> 2);
|
|
||||||
break;
|
break;
|
||||||
case 2:
|
case 2:
|
||||||
n = atob(*sp++, url) << 2;
|
highorderbits = asciitob64[*sxtbuf++] << 2;
|
||||||
*op = n + ((atob(*sp, url) & ~15) >> 4);
|
*octbuf++ = highorderbits + ((asciitob64[*sxtbuf++] & ~15) >> 4);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return op-ob+1;
|
return octbuf-octbegin;
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
8
encode.h
8
encode.h
|
@ -1,10 +1,10 @@
|
||||||
#ifndef HEADER_ENCODE
|
#ifndef ENCODE_H
|
||||||
#define HEADER_ENCODE
|
#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, int url);
|
decode(unsigned char *sxtbuf, int sxtbufsize, unsigned char *octbuf);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
48
input.c
48
input.c
|
@ -1,30 +1,32 @@
|
||||||
#include "input.h"
|
#include "input.h"
|
||||||
|
|
||||||
#define PADDING '='
|
#define PADDING '='
|
||||||
|
|
||||||
int
|
int
|
||||||
getocts(FILE *fp, unsigned char *o, int olen)
|
readb(FILE *fptr, unsigned char *buf, int bufsize)
|
||||||
{
|
{
|
||||||
int c, n;
|
int read;
|
||||||
|
int total;
|
||||||
|
unsigned char *begin;
|
||||||
|
unsigned char *end;
|
||||||
|
unsigned char *left;
|
||||||
|
unsigned char *right;
|
||||||
|
|
||||||
|
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;
|
return left-begin;
|
||||||
while (n < olen-1 && (c = fgetc(fp)) != EOF)
|
|
||||||
o[n++] = c;
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
9
input.h
9
input.h
|
@ -1,12 +1,9 @@
|
||||||
#ifndef HEADER_INPUT
|
#ifndef INPUT_H
|
||||||
#define HEADER_INPUT
|
#define INPUT_H
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
int
|
int
|
||||||
getocts(FILE *fp, unsigned char *o, int olen);
|
readb(FILE *fptr, unsigned char *buf, int bufsize);
|
||||||
|
|
||||||
int
|
|
||||||
getsxts(FILE *fp, unsigned char *s, int slen);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
41
main.c
41
main.c
|
@ -4,20 +4,21 @@
|
||||||
#include "output.h"
|
#include "output.h"
|
||||||
#include "encode.h"
|
#include "encode.h"
|
||||||
|
|
||||||
#define OBUFSIZE 4
|
#define OCTETBUF 1500
|
||||||
#define SBUFSIZE 5
|
#define SXTETBUF 2000
|
||||||
|
|
||||||
unsigned char obuf[OBUFSIZE];
|
unsigned char obuf[OCTETBUF];
|
||||||
unsigned char sbuf[SBUFSIZE];
|
unsigned char sbuf[SXTETBUF];
|
||||||
|
|
||||||
int
|
int
|
||||||
main(int argc, char *argv[])
|
main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
int c, n, l;
|
int c, n, last;
|
||||||
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]))
|
||||||
|
@ -42,6 +43,7 @@ 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);
|
||||||
|
@ -51,22 +53,23 @@ main(int argc, char *argv[])
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
if (dec) {
|
if (dec) {
|
||||||
while ((n = getsxts((argc >= 1) ? in : stdin, sbuf, SBUFSIZE))) {
|
while ((n = readb((in) ? in : stdin, sbuf, SXTETBUF))) {
|
||||||
n = decode(sbuf, n, obuf, url);
|
n = decode(sbuf, n, obuf);
|
||||||
fwrite(obuf, sizeof(*obuf), n, (argc == 2) ? out : stdout);
|
fwrite(obuf, sizeof(*obuf), n, (out) ? 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 = getocts((argc >= 1) ? in : stdin, obuf, OBUFSIZE))) {
|
while ((n = fread(obuf, sizeof(*obuf), OCTETBUF, (in) ? in : stdin))) {
|
||||||
if (wrp) {
|
n = encode(obuf, n, sbuf, url);
|
||||||
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)
|
if (in)
|
||||||
fclose(in);
|
fclose(in);
|
||||||
|
|
33
output.c
33
output.c
|
@ -3,16 +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;
|
||||||
|
|
||||||
while (slen--) {
|
c = '\n';
|
||||||
fputc((c = *s++), fp);
|
if (column > 0) {
|
||||||
++col;
|
if (bufsize >= WRAPCOL-column) {
|
||||||
if (!(col %= WRAPCOL))
|
fwrite(buf, sizeof(*buf), WRAPCOL-column, fptr);
|
||||||
fputc((c = '\n'), fp);
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
return c;
|
return c;
|
||||||
|
|
6
output.h
6
output.h
|
@ -1,9 +1,9 @@
|
||||||
#ifndef HEADER_OUTPUT
|
#ifndef OUTPUT_H
|
||||||
#define HEADER_OUTPUT
|
#define OUTPUT_H
|
||||||
|
|
||||||
#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