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"
|
||||
|
||||
#define OBUFSIZE 4
|
||||
#define SBUFSIZE 5
|
||||
|
||||
#define PADDING '='
|
||||
|
||||
unsigned char o[OBUFSIZE];
|
||||
unsigned char s[SBUFSIZE];
|
||||
|
||||
unsigned char *
|
||||
getocts(FILE *fp, int *np)
|
||||
int
|
||||
getocts(FILE *fp, unsigned char *o, int olen)
|
||||
{
|
||||
int n, c;
|
||||
int c, n;
|
||||
|
||||
n = 0;
|
||||
while (n < OBUFSIZE-1 && (c = fgetc(fp)) != EOF)
|
||||
while (n < olen-1 && (c = fgetc(fp)) != EOF)
|
||||
o[n++] = c;
|
||||
*np = n;
|
||||
|
||||
return (*np) ? o : NULL;
|
||||
return n;
|
||||
}
|
||||
|
||||
unsigned char *
|
||||
getsxts(FILE *fp, int *np)
|
||||
int
|
||||
getsxts(FILE *fp, unsigned char *s, int slen)
|
||||
{
|
||||
int n, p, c;
|
||||
int c, n, pad;
|
||||
|
||||
n = p = 0;
|
||||
while (n < SBUFSIZE-1 && (c = fgetc(fp)) != EOF && c != PADDING)
|
||||
n = pad = 0;
|
||||
while (n < slen-1 && (c = fgetc(fp)) != EOF && c != PADDING)
|
||||
s[n++] = c;
|
||||
while (n+p < SBUFSIZE-1)
|
||||
s[n+p++] = PADDING;
|
||||
*np = n;
|
||||
while (n+pad < slen-1)
|
||||
s[n+pad++] = PADDING;
|
||||
|
||||
return (n) ? s : NULL;
|
||||
return n;
|
||||
}
|
||||
|
|
11
input.h
11
input.h
|
@ -3,13 +3,10 @@
|
|||
|
||||
#include <stdio.h>
|
||||
|
||||
extern unsigned char o[];
|
||||
extern unsigned char s[];
|
||||
int
|
||||
getocts(FILE *fp, unsigned char *o, int olen);
|
||||
|
||||
unsigned char *
|
||||
getocts(FILE *fp, int *np);
|
||||
|
||||
unsigned char *
|
||||
getsxts(FILE *fp, int *np);
|
||||
int
|
||||
getsxts(FILE *fp, unsigned char *s, int slen);
|
||||
|
||||
#endif
|
||||
|
|
31
main.c
31
main.c
|
@ -3,12 +3,17 @@
|
|||
#include "input.h"
|
||||
#include "trans.h"
|
||||
|
||||
#define OBUFSIZE 4
|
||||
#define SBUFSIZE 5
|
||||
|
||||
unsigned char obuf[OBUFSIZE];
|
||||
unsigned char sbuf[SBUFSIZE];
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
int c, n, dec, hlp;
|
||||
char *prog = *argv;
|
||||
unsigned char *b;
|
||||
FILE *fp;
|
||||
|
||||
dec = hlp = 0;
|
||||
|
@ -30,13 +35,15 @@ main(int argc, char *argv[])
|
|||
fprintf(stdout, "Usage: %s -d -h file\n", prog);
|
||||
} else if (argc != 1) {
|
||||
if (dec) {
|
||||
while ((b = getsxts(stdin, &n))) {
|
||||
b = decode(b, &n);
|
||||
fwrite(b, sizeof(*b), n, stdout);
|
||||
while ((n = getsxts(stdin, sbuf, SBUFSIZE))) {
|
||||
n = decode(sbuf, n, obuf);
|
||||
fwrite(obuf, sizeof(*obuf), n, stdout);
|
||||
}
|
||||
} else {
|
||||
while ((b = getocts(stdin, &n)))
|
||||
printf("%s", encode(b, n));
|
||||
while ((n = getocts(stdin, obuf, OBUFSIZE))) {
|
||||
encode(obuf, n, sbuf);
|
||||
printf("%s", sbuf);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if ((fp = fopen(*argv, "r")) == NULL) {
|
||||
|
@ -44,13 +51,15 @@ main(int argc, char *argv[])
|
|||
exit(EXIT_FAILURE);
|
||||
}
|
||||
else if (dec) {
|
||||
while ((b = getsxts(fp, &n))) {
|
||||
b = decode(b, &n);
|
||||
fwrite(b, sizeof(*b), n, stdout);
|
||||
while ((n = getsxts(fp, sbuf, SBUFSIZE))) {
|
||||
n = decode(sbuf, n, obuf);
|
||||
fwrite(obuf, sizeof(*obuf), n, stdout);
|
||||
}
|
||||
} else {
|
||||
while ((b = getocts(fp, &n)))
|
||||
printf("%s", encode(b, n));
|
||||
while ((n = getocts(fp, obuf, OBUFSIZE))) {
|
||||
encode(obuf, n, sbuf);
|
||||
printf("%s", sbuf);
|
||||
}
|
||||
}
|
||||
fclose(fp);
|
||||
}
|
||||
|
|
27
trans.c
27
trans.c
|
@ -7,15 +7,14 @@ unsigned char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
|
|||
"0123456789" \
|
||||
"+/";
|
||||
|
||||
unsigned char *
|
||||
encode(unsigned char *op, int np)
|
||||
int
|
||||
encode(unsigned char *op, int olen, unsigned char *sp)
|
||||
{
|
||||
extern unsigned char s[];
|
||||
unsigned char *sp;
|
||||
int n;
|
||||
unsigned char *sb;
|
||||
|
||||
sp = s;
|
||||
switch (np) {
|
||||
sb = sp;
|
||||
switch (olen) {
|
||||
case 3:
|
||||
*sp++ = b64[(*op & ~3) >> 2];
|
||||
n = (*op++ & 3) << 4;
|
||||
|
@ -39,18 +38,17 @@ encode(unsigned char *op, int np)
|
|||
break;
|
||||
}
|
||||
|
||||
return s;
|
||||
return sp-sb+1;
|
||||
}
|
||||
|
||||
unsigned char *
|
||||
decode(unsigned char *sp, int *np)
|
||||
int
|
||||
decode(unsigned char *sp, int slen, unsigned char *op)
|
||||
{
|
||||
extern unsigned char o[];
|
||||
unsigned char *op;
|
||||
int n, b, atob(int c);
|
||||
unsigned char *ob;
|
||||
|
||||
op = o;
|
||||
switch (*np) {
|
||||
ob = op;
|
||||
switch (slen) {
|
||||
case 4:
|
||||
n = atob(*sp++) << 2;
|
||||
b = atob(*sp++);
|
||||
|
@ -75,9 +73,8 @@ decode(unsigned char *sp, int *np)
|
|||
*op = n + ((atob(*sp) & ~15) >> 4);
|
||||
break;
|
||||
}
|
||||
*np = op-o+1;
|
||||
|
||||
return o;
|
||||
return op-ob+1;
|
||||
}
|
||||
|
||||
int atob(int c)
|
||||
|
|
10
trans.h
10
trans.h
|
@ -1,12 +1,10 @@
|
|||
#ifndef HEADER_TRANS
|
||||
#define HEADER_TRANS
|
||||
|
||||
#include "input.h"
|
||||
int
|
||||
encode(unsigned char *op, int olen, unsigned char *sp);
|
||||
|
||||
unsigned char *
|
||||
encode(unsigned char *op, int np);
|
||||
|
||||
unsigned char *
|
||||
decode(unsigned char *sp, int *np);
|
||||
int
|
||||
decode(unsigned char *sp, int slen, unsigned char *op);
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue