change function definitions
Buffers are no longer wired into any input or translation functions. They are supplied as arguments through main.
This commit is contained in:
parent
9b1fe95a8e
commit
848c3749d2
5 changed files with 53 additions and 60 deletions
34
input.c
34
input.c
|
@ -1,37 +1,29 @@
|
||||||
#include "input.h"
|
#include "input.h"
|
||||||
|
|
||||||
#define OBUFSIZE 4
|
|
||||||
#define SBUFSIZE 5
|
|
||||||
|
|
||||||
#define PADDING '='
|
#define PADDING '='
|
||||||
|
|
||||||
unsigned char o[OBUFSIZE];
|
int
|
||||||
unsigned char s[SBUFSIZE];
|
getocts(FILE *fp, unsigned char *o, int olen)
|
||||||
|
|
||||||
unsigned char *
|
|
||||||
getocts(FILE *fp, int *np)
|
|
||||||
{
|
{
|
||||||
int n, c;
|
int c, n;
|
||||||
|
|
||||||
n = 0;
|
n = 0;
|
||||||
while (n < OBUFSIZE-1 && (c = fgetc(fp)) != EOF)
|
while (n < olen-1 && (c = fgetc(fp)) != EOF)
|
||||||
o[n++] = c;
|
o[n++] = c;
|
||||||
*np = n;
|
|
||||||
|
|
||||||
return (*np) ? o : NULL;
|
return n;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned char *
|
int
|
||||||
getsxts(FILE *fp, int *np)
|
getsxts(FILE *fp, unsigned char *s, int slen)
|
||||||
{
|
{
|
||||||
int n, p, c;
|
int c, n, pad;
|
||||||
|
|
||||||
n = p = 0;
|
n = pad = 0;
|
||||||
while (n < SBUFSIZE-1 && (c = fgetc(fp)) != EOF && c != PADDING)
|
while (n < slen-1 && (c = fgetc(fp)) != EOF && c != PADDING)
|
||||||
s[n++] = c;
|
s[n++] = c;
|
||||||
while (n+p < SBUFSIZE-1)
|
while (n+pad < slen-1)
|
||||||
s[n+p++] = PADDING;
|
s[n+pad++] = PADDING;
|
||||||
*np = n;
|
|
||||||
|
|
||||||
return (n) ? s : NULL;
|
return n;
|
||||||
}
|
}
|
||||||
|
|
11
input.h
11
input.h
|
@ -3,13 +3,10 @@
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
extern unsigned char o[];
|
int
|
||||||
extern unsigned char s[];
|
getocts(FILE *fp, unsigned char *o, int olen);
|
||||||
|
|
||||||
unsigned char *
|
int
|
||||||
getocts(FILE *fp, int *np);
|
getsxts(FILE *fp, unsigned char *s, int slen);
|
||||||
|
|
||||||
unsigned char *
|
|
||||||
getsxts(FILE *fp, int *np);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
31
main.c
31
main.c
|
@ -3,12 +3,17 @@
|
||||||
#include "input.h"
|
#include "input.h"
|
||||||
#include "trans.h"
|
#include "trans.h"
|
||||||
|
|
||||||
|
#define OBUFSIZE 4
|
||||||
|
#define SBUFSIZE 5
|
||||||
|
|
||||||
|
unsigned char obuf[OBUFSIZE];
|
||||||
|
unsigned char sbuf[SBUFSIZE];
|
||||||
|
|
||||||
int
|
int
|
||||||
main(int argc, char *argv[])
|
main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
int c, n, dec, hlp;
|
int c, n, dec, hlp;
|
||||||
char *prog = *argv;
|
char *prog = *argv;
|
||||||
unsigned char *b;
|
|
||||||
FILE *fp;
|
FILE *fp;
|
||||||
|
|
||||||
dec = hlp = 0;
|
dec = hlp = 0;
|
||||||
|
@ -30,13 +35,15 @@ main(int argc, char *argv[])
|
||||||
fprintf(stdout, "Usage: %s -d -h file\n", prog);
|
fprintf(stdout, "Usage: %s -d -h file\n", prog);
|
||||||
} else if (argc != 1) {
|
} else if (argc != 1) {
|
||||||
if (dec) {
|
if (dec) {
|
||||||
while ((b = getsxts(stdin, &n))) {
|
while ((n = getsxts(stdin, sbuf, SBUFSIZE))) {
|
||||||
b = decode(b, &n);
|
n = decode(sbuf, n, obuf);
|
||||||
fwrite(b, sizeof(*b), n, stdout);
|
fwrite(obuf, sizeof(*obuf), n, stdout);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
while ((b = getocts(stdin, &n)))
|
while ((n = getocts(stdin, obuf, OBUFSIZE))) {
|
||||||
printf("%s", encode(b, n));
|
encode(obuf, n, sbuf);
|
||||||
|
printf("%s", sbuf);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if ((fp = fopen(*argv, "r")) == NULL) {
|
if ((fp = fopen(*argv, "r")) == NULL) {
|
||||||
|
@ -44,13 +51,15 @@ main(int argc, char *argv[])
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
else if (dec) {
|
else if (dec) {
|
||||||
while ((b = getsxts(fp, &n))) {
|
while ((n = getsxts(fp, sbuf, SBUFSIZE))) {
|
||||||
b = decode(b, &n);
|
n = decode(sbuf, n, obuf);
|
||||||
fwrite(b, sizeof(*b), n, stdout);
|
fwrite(obuf, sizeof(*obuf), n, stdout);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
while ((b = getocts(fp, &n)))
|
while ((n = getocts(fp, obuf, OBUFSIZE))) {
|
||||||
printf("%s", encode(b, n));
|
encode(obuf, n, sbuf);
|
||||||
|
printf("%s", sbuf);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
}
|
}
|
||||||
|
|
27
trans.c
27
trans.c
|
@ -7,15 +7,14 @@ unsigned char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
|
||||||
"0123456789" \
|
"0123456789" \
|
||||||
"+/";
|
"+/";
|
||||||
|
|
||||||
unsigned char *
|
int
|
||||||
encode(unsigned char *op, int np)
|
encode(unsigned char *op, int olen, unsigned char *sp)
|
||||||
{
|
{
|
||||||
extern unsigned char s[];
|
|
||||||
unsigned char *sp;
|
|
||||||
int n;
|
int n;
|
||||||
|
unsigned char *sb;
|
||||||
|
|
||||||
sp = s;
|
sb = sp;
|
||||||
switch (np) {
|
switch (olen) {
|
||||||
case 3:
|
case 3:
|
||||||
*sp++ = b64[(*op & ~3) >> 2];
|
*sp++ = b64[(*op & ~3) >> 2];
|
||||||
n = (*op++ & 3) << 4;
|
n = (*op++ & 3) << 4;
|
||||||
|
@ -39,18 +38,17 @@ encode(unsigned char *op, int np)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return s;
|
return sp-sb+1;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned char *
|
int
|
||||||
decode(unsigned char *sp, int *np)
|
decode(unsigned char *sp, int slen, unsigned char *op)
|
||||||
{
|
{
|
||||||
extern unsigned char o[];
|
|
||||||
unsigned char *op;
|
|
||||||
int n, b, atob(int c);
|
int n, b, atob(int c);
|
||||||
|
unsigned char *ob;
|
||||||
|
|
||||||
op = o;
|
ob = op;
|
||||||
switch (*np) {
|
switch (slen) {
|
||||||
case 4:
|
case 4:
|
||||||
n = atob(*sp++) << 2;
|
n = atob(*sp++) << 2;
|
||||||
b = atob(*sp++);
|
b = atob(*sp++);
|
||||||
|
@ -75,9 +73,8 @@ decode(unsigned char *sp, int *np)
|
||||||
*op = n + ((atob(*sp) & ~15) >> 4);
|
*op = n + ((atob(*sp) & ~15) >> 4);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
*np = op-o+1;
|
|
||||||
|
|
||||||
return o;
|
return op-ob+1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int atob(int c)
|
int atob(int c)
|
||||||
|
|
10
trans.h
10
trans.h
|
@ -1,12 +1,10 @@
|
||||||
#ifndef HEADER_TRANS
|
#ifndef HEADER_TRANS
|
||||||
#define HEADER_TRANS
|
#define HEADER_TRANS
|
||||||
|
|
||||||
#include "input.h"
|
int
|
||||||
|
encode(unsigned char *op, int olen, unsigned char *sp);
|
||||||
|
|
||||||
unsigned char *
|
int
|
||||||
encode(unsigned char *op, int np);
|
decode(unsigned char *sp, int slen, unsigned char *op);
|
||||||
|
|
||||||
unsigned char *
|
|
||||||
decode(unsigned char *sp, int *np);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue