The functions encode and decode are now capable of processing larger buffers with less conditional logic. Input and output functions need to follow suit to make better use of this next. Encode without line wrapping already makes use of these improvements, because input and output are now entirely handled by <stdio.h> functions.
80 lines
2.3 KiB
C
80 lines
2.3 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include "input.h"
|
|
#include "output.h"
|
|
#include "encode.h"
|
|
|
|
#define OCTETBUF 300
|
|
#define SXTETBUF 400
|
|
|
|
unsigned char obuf[OCTETBUF];
|
|
unsigned char sbuf[SXTETBUF];
|
|
|
|
int
|
|
main(int argc, char *argv[])
|
|
{
|
|
int c, n, last;
|
|
int dec, url, hlp, wrp;
|
|
FILE *in, *out;
|
|
char *prog;
|
|
|
|
prog = *argv;
|
|
dec = url = hlp = wrp = 0;
|
|
while (--argc > 0 && (*++argv)[0] == '-')
|
|
while ((c = *++argv[0]))
|
|
switch (c) {
|
|
case 'd':
|
|
dec = 1;
|
|
break;
|
|
case 'u':
|
|
url = 1;
|
|
break;
|
|
case 'h':
|
|
hlp = 1;
|
|
break;
|
|
case 'w':
|
|
wrp = 1;
|
|
break;
|
|
default:
|
|
fprintf(stderr, "%s: illegal option %c\n", prog, c);
|
|
exit(EXIT_FAILURE);
|
|
break;
|
|
}
|
|
if (hlp) {
|
|
fprintf(stdout, "Usage: %s -duhw infile outfile\n", prog);
|
|
} else {
|
|
in = out = NULL;
|
|
if (argc >= 1 && (in = fopen(*argv, "r")) == NULL) {
|
|
fprintf(stderr, "%s: can't open %s\n", prog, *argv);
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
if (argc == 2 && (out = fopen(*(argv+1), "w")) == NULL) {
|
|
fprintf(stderr, "%s: can't open %s\n", prog, *(argv+1));
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
if (dec) {
|
|
while ((n = getsxts((in) ? in : stdin, sbuf))) {
|
|
n = decode(sbuf, n, obuf, url);
|
|
fwrite(obuf, sizeof(*obuf), n, (out) ? out : stdout);
|
|
}
|
|
} else if (wrp) {
|
|
last = 0;
|
|
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 {
|
|
while ((n = fread(obuf, sizeof(*obuf), OCTETBUF, (in) ? in : stdin))) {
|
|
n = encode(obuf, n, sbuf, url);
|
|
fwrite(sbuf, sizeof(*sbuf), n, (out) ? out : stdout);
|
|
}
|
|
}
|
|
if (in)
|
|
fclose(in);
|
|
if (out)
|
|
fclose(out);
|
|
}
|
|
exit(EXIT_SUCCESS);
|
|
}
|