Changeset View
Changeset View
Standalone View
Standalone View
usr.bin/bintrans/uudecode.c
Show First 20 Lines • Show All 63 Lines • ▼ Show 20 Lines | |||||
#include <resolv.h> | #include <resolv.h> | ||||
#include <stdbool.h> | #include <stdbool.h> | ||||
#include <stdio.h> | #include <stdio.h> | ||||
#include <stdlib.h> | #include <stdlib.h> | ||||
#include <string.h> | #include <string.h> | ||||
#include <unistd.h> | #include <unistd.h> | ||||
extern int main_decode(int, char *[]); | extern int main_decode(int, char *[]); | ||||
extern int main_base64_decode(const char *); | extern int main_base64_decode(const char *, bool); | ||||
static const char *infile, *outfile; | static const char *infile, *outfile; | ||||
static FILE *infp, *outfp; | static FILE *infp, *outfp; | ||||
static bool base64, cflag, iflag, oflag, pflag, rflag, sflag; | static bool base64, cflag, iflag, oflag, pflag, rflag, sflag, nflag; | ||||
static void usage(void); | static void usage(void); | ||||
static int decode(void); | static int decode(void); | ||||
static int decode2(void); | static int decode2(void); | ||||
static int uu_decode(void); | static int uu_decode(void); | ||||
static int base64_decode(void); | static int base64_decode(void); | ||||
int | int | ||||
main_base64_decode(const char *in) | main_base64_decode(const char *in, bool ignore) | ||||
{ | { | ||||
base64 = 1; | base64 = 1; | ||||
rflag = 1; | rflag = 1; | ||||
nflag = ignore; | |||||
if (in != NULL) { | if (in != NULL) { | ||||
infile = in; | infile = in; | ||||
infp = fopen(infile, "r"); | infp = fopen(infile, "r"); | ||||
if (infp == NULL) | if (infp == NULL) | ||||
err(1, "%s", in); | err(1, "%s", in); | ||||
} else { | } else { | ||||
infile = "stdin"; | infile = "stdin"; | ||||
infp = stdin; | infp = stdin; | ||||
▲ Show 20 Lines • Show All 333 Lines • ▼ Show 20 Lines | #define OUT_OF_RANGE do { \ | ||||
} | } | ||||
} | } | ||||
static int | static int | ||||
base64_decode(void) | base64_decode(void) | ||||
{ | { | ||||
int n, count, count4; | int n, count, count4; | ||||
char inbuf[MAXPATHLEN + 1], *p; | char inbuf[MAXPATHLEN + 1], *p; | ||||
unsigned char outbuf[MAXPATHLEN * 4]; | unsigned char outbuf[MAXPATHLEN * 4], *o; | ||||
char leftover[MAXPATHLEN + 1]; | char leftover[MAXPATHLEN + 1]; | ||||
leftover[0] = '\0'; | leftover[0] = '\0'; | ||||
for (;;) { | for (;;) { | ||||
strcpy(inbuf, leftover); | strcpy(inbuf, leftover); | ||||
switch (get_line(inbuf + strlen(inbuf), | switch (get_line(inbuf + strlen(inbuf), | ||||
sizeof(inbuf) - strlen(inbuf))) { | sizeof(inbuf) - strlen(inbuf))) { | ||||
case 0: | case 0: | ||||
return (0); | return (0); | ||||
case 1: | case 1: | ||||
return (1); | return (1); | ||||
} | } | ||||
count = 0; | count = 0; | ||||
count4 = -1; | count4 = -1; | ||||
p = inbuf; | for (p = inbuf; *p != '\0'; p++) { | ||||
while (*p != '\0') { | |||||
/* | /* | ||||
* Base64 encoded strings have the following | * Base64 encoded strings have the following | ||||
* characters in them: A-Z, a-z, 0-9 and +, / and = | * characters in them: A-Z, a-z, 0-9 and +, / and = | ||||
*/ | */ | ||||
if (isalnum(*p) || *p == '+' || *p == '/' || *p == '=') | if (isalnum(*p) || *p == '+' || *p == '/' || *p == '=') | ||||
count++; | count++; | ||||
else if (nflag) | |||||
*p = ' '; | |||||
if (count % 4 == 0) | if (count % 4 == 0) | ||||
count4 = p - inbuf; | count4 = p - inbuf; | ||||
p++; | |||||
} | } | ||||
strcpy(leftover, inbuf + count4 + 1); | strcpy(leftover, inbuf + count4 + 1); | ||||
inbuf[count4 + 1] = 0; | inbuf[count4 + 1] = 0; | ||||
n = b64_pton(inbuf, outbuf, sizeof(outbuf)); | o = outbuf; | ||||
n = b64_pton_partial(inbuf, &o, sizeof(outbuf)); | |||||
if (n < 0) | fwrite(outbuf, 1, o - outbuf, outfp); | ||||
if (!nflag && n < 0) | |||||
break; | break; | ||||
fwrite(outbuf, 1, n, outfp); | |||||
} | } | ||||
return (checkend(inbuf, "====", "error decoding base64 input stream")); | return (!nflag && checkend(inbuf, "====", "error decoding base64 input stream")); | ||||
} | } | ||||
static void | static void | ||||
usage(void) | usage(void) | ||||
{ | { | ||||
(void)fprintf(stderr, | (void)fprintf(stderr, | ||||
"usage: uudecode [-cimprs] [file ...]\n" | "usage: uudecode [-cimprs] [file ...]\n" | ||||
" uudecode [-i] -o output_file [file]\n" | " uudecode [-i] -o output_file [file]\n" | ||||
" b64decode [-cimprs] [file ...]\n" | " b64decode [-cimprs] [file ...]\n" | ||||
" b64decode [-i] -o output_file [file]\n"); | " b64decode [-i] -o output_file [file]\n"); | ||||
exit(1); | exit(1); | ||||
} | } |