bintrans: adjust mmencode.c to style(9)

This commit is contained in:
Piotr Pawel Stefaniak 2022-04-15 15:00:36 +02:00
parent 9d68da4ee8
commit ff2b1ffbb0

View File

@ -12,33 +12,34 @@ MAKES NO REPRESENTATIONS ABOUT THE ACCURACY OR SUITABILITY
OF THIS MATERIAL FOR ANY PURPOSE. IT IS PROVIDED "AS IS",
WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
*/
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int PendingBoundary(s, Boundaries, BoundaryCt)
char *s;
char **Boundaries;
int *BoundaryCt;
static int
PendingBoundary(char *s, char **Boundaries, int *BoundaryCt)
{
int i, len;
int i;
size_t len;
if (s[0] != '-' || s[1] != '-') return(0);
if (s[0] != '-' || s[1] != '-')
return (0);
for (i=0; i < *BoundaryCt; ++i) {
for (i = 0; i < *BoundaryCt; ++i) {
len = strlen(Boundaries[i]);
if (!strncmp(s, Boundaries[i], len)) {
if (s[len] == '-' && s[len+1] == '-') *BoundaryCt = i;
return(1);
if (strncmp(s, Boundaries[i], len) == 0) {
if (s[len] == '-' && s[len + 1] == '-')
*BoundaryCt = i;
return (1);
}
}
return(0);
return (0);
}
static char basis_hex[] = "0123456789ABCDEF";
static char index_hex[128] = {
#define basis_hex "0123456789ABCDEF"
static const char index_hex[128] = {
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
@ -54,10 +55,11 @@ static char index_hex[128] = {
/* Since we're no longer ever calling it with anything signed, this should work: */
#define hexchar(c) (((c) > 127) ? -1 : index_hex[(c)])
void toqp(infile, outfile)
FILE *infile, *outfile;
static void
toqp(FILE *infile, FILE *outfile)
{
int c, ct=0, prevc=255;
int c, ct = 0, prevc = 255;
while ((c = getc(infile)) != EOF) {
if ((c < 32 && (c != '\n' && c != '\t'))
|| (c == '=')
@ -66,8 +68,8 @@ FILE *infile, *outfile;
which messes up some dumb smtp implementations, sigh... */
|| (ct == 0 && c == '.')) {
putc('=', outfile);
putc(basis_hex[c>>4], outfile);
putc(basis_hex[c&0xF], outfile);
putc(basis_hex[c >> 4], outfile);
putc(basis_hex[c & 0xF], outfile);
ct += 3;
prevc = 'A'; /* close enough */
} else if (c == '\n') {
@ -129,45 +131,45 @@ FILE *infile, *outfile;
}
}
void fromqp(infile, outfile, boundaries, boundaryct)
FILE *infile, *outfile;
char **boundaries;
int *boundaryct;
static void
fromqp(FILE *infile, FILE *outfile, char **boundaries, int *boundaryct)
{
unsigned int c1, c2;
int sawnewline = 1, neednewline = 0;
int c1, c2;
bool sawnewline = true, neednewline = false;
/* The neednewline hack is necessary because the newline leading into
a multipart boundary is part of the boundary, not the data */
while ((c1 = getc(infile)) != EOF) {
if (sawnewline && boundaries && (c1 == '-')) {
if (sawnewline && boundaries && c1 == '-') {
char Buf[200];
unsigned char *s;
ungetc(c1, infile);
fgets(Buf, sizeof(Buf), infile);
if (boundaries
&& (Buf[0] == '-')
&& (Buf[1] == '-')
&& Buf[0] == '-'
&& Buf[1] == '-'
&& PendingBoundary(Buf, boundaries, boundaryct)) {
return;
}
/* Not a boundary, now we must treat THIS line as q-p, sigh */
if (neednewline) {
putc('\n', outfile);
neednewline = 0;
neednewline = false;
}
for (s=(unsigned char *) Buf; *s; ++s) {
for (s = (unsigned char *)Buf; *s; ++s) {
if (*s == '=') {
if (!*++s) break;
if (*++s == 0)
break;
if (*s == '\n') {
/* ignore it */
sawnewline = 1;
sawnewline = true;
} else {
c1 = hexchar(*s);
if (!*++s) break;
if (*++s == 0)
break;
c2 = hexchar(*s);
putc(c1<<4 | c2, outfile);
putc(c1 << 4 | c2, outfile);
}
} else {
putc(*s, outfile);
@ -176,27 +178,28 @@ int *boundaryct;
} else {
if (neednewline) {
putc('\n', outfile);
neednewline = 0;
neednewline = false;
}
if (c1 == '=') {
sawnewline = 0;
sawnewline = false;
c1 = getc(infile);
if (c1 == '\n') {
/* ignore it */
sawnewline = 1;
sawnewline = true;
} else {
c2 = getc(infile);
c1 = hexchar(c1);
c2 = hexchar(c2);
putc(c1<<4 | c2, outfile);
if (c2 == '\n') sawnewline = 1;
putc(c1 << 4 | c2, outfile);
if (c2 == '\n')
sawnewline = true;
}
} else {
if (c1 == '\n') {
sawnewline = 1;
neednewline = 1;
sawnewline = true;
neednewline = true;
} else {
sawnewline = 0;
sawnewline = false;
putc(c1, outfile);
}
}
@ -204,51 +207,51 @@ int *boundaryct;
}
if (neednewline) {
putc('\n', outfile);
neednewline = 0;
neednewline = false;
}
}
#define QP 2 /* quoted-printable */
int main(argc, argv)
int argc;
char **argv;
int
main(int argc, char *argv[])
{
int encode = 1, i;
int i;
bool encode = true;
FILE *fp = stdin;
FILE *fpo = stdout;
for (i=1; i<argc; ++i) {
for (i = 1; i < argc; ++i) {
if (argv[i][0] == '-') {
switch (argv[i][1]) {
case 'o':
if (++i >= argc) {
fprintf(stderr, "mimencode: -o requires a file name.\n");
exit(-1);
exit(EXIT_FAILURE);
}
fpo = fopen(argv[i], "w");
if (!fpo) {
if (fpo == NULL) {
perror(argv[i]);
exit(-1);
exit(EXIT_FAILURE);
}
break;
case 'u':
encode = 0;
encode = false;
break;
default:
fprintf(stderr,
"Usage: mmencode [-u] [-o outputfile] [file name]\n");
exit(-1);
exit(EXIT_FAILURE);
}
} else {
fp = fopen(argv[i], "r");
if (!fp) {
if (fp == NULL) {
perror(argv[i]);
exit(-1);
exit(EXIT_FAILURE);
}
}
}
if (encode) toqp(fp, fpo); else fromqp(fp, fpo, NULL, 0);
return(0);
if (encode)
toqp(fp, fpo);
else
fromqp(fp, fpo, NULL, 0);
return (0);
}