Index: usr.bin/bintrans/Makefile =================================================================== --- usr.bin/bintrans/Makefile +++ usr.bin/bintrans/Makefile @@ -4,7 +4,7 @@ .include PROG= bintrans -SRCS= bintrans.c uuencode.c uudecode.c quoted-printable.c +SRCS= bintrans.c uuencode.c uudecode.c qp.c MAN= bintrans.1 uuencode.format.5 LINKS+= ${BINDIR}/bintrans ${BINDIR}/uuencode LINKS+= ${BINDIR}/bintrans ${BINDIR}/b64encode Index: usr.bin/bintrans/qp.c =================================================================== --- /dev/null +++ usr.bin/bintrans/qp.c @@ -0,0 +1,192 @@ +/* + * Copyright (C) 2022 Baptiste Daroussin + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +#include +#include +#include +#include +#include + +extern int main_quotedprintable(int, char *[]); + +static int +hexval(int c) +{ + if ('0' <= c && c <= '9') return c - '0'; + return (10 + c - 'A'); +} + + +static int +decode_char(const char *s) +{ + return (16 * hexval(toupper(s[1])) + hexval(toupper(s[2]))); +} + + +static void +decode_quoted_printable(const char *body, FILE *fpo) +{ + while (*body) { + switch (*body) { + case '=': + if (strlen(body) < 2) { + fputc(*body, fpo); + break; + } + + if (body[1] == '\r' && body[2] == '\n') { + body += 2; + break; + } + if (body[1] == '\n') { + body++; + break; + } + if (strchr("0123456789ABCDEFabcdef", body[1]) == NULL) { + fputc(*body, fpo); + break; + } + if (strchr("0123456789ABCDEFabcdef", body[2]) == NULL) { + fputc(*body, fpo); + break; + } + fputc(decode_char(body), fpo); + body += 2; + break; + default: + fputc(*body, fpo); + break; + } + body++; + } +} + +static void +encode_quoted_printable(const char *body, FILE *fpo) +{ + char prev; + const char *end = body + strlen(body); + size_t linelen = 0; + + while (*body) { + if (linelen == 75) { + fputs("=\r\n", fpo); + linelen = 0; + } + if (!isascii(*body) || + (*body == '=') || + (*body == '.' && body + 1 < end && + (body[1] == '\n' || body[1] == '\r'))) { + fprintf(fpo, "=%02X", (unsigned char)*body); + linelen += 2; + prev = *body; + } else if (*body < 33 && *body != '\n') { + if ((*body == ' ' || *body == '\t') && + body+1 < end && + (body[1] != '\n' && body[1] != '\r')) { + fputc(*body, fpo); + prev = *body; + } else { + fprintf(fpo, "=%02X", (unsigned char)*body); + linelen+=2; + prev = '_'; + } + } else if (*body == '\n') { + if (prev == ' ' || prev == '\t') { + fputc('=', fpo); + } + fputc('\n', fpo); + linelen=0; + prev = 0; + } else { + fputc(*body, fpo); + prev = *body; + } + body++; + linelen++; + } +} + +static void +qp(FILE *fp, FILE *fpo, bool encode) +{ + char *line = NULL; + size_t linecap = 0; + ssize_t linelen; + void (*codec)(const char *line, FILE *f) = decode_quoted_printable; + if (encode) + codec = encode_quoted_printable; + + while ((linelen = getline(&line, &linecap, fp)) > 0) + codec(line, fpo); + free(line); +} + +static void +usage(void) +{ + fprintf(stderr, + "usage: bintrans qp [-u] [-o outputfile] [file name]\n"); +} + +int +main_quotedprintable(int argc, char *argv[]) +{ + int i; + bool encode = true; + FILE *fp = stdin; + FILE *fpo = stdout; + + for (i = 1; i < argc; ++i) { + if (argv[i][0] == '-') { + switch (argv[i][1]) { + case 'o': + if (++i >= argc) { + fprintf(stderr, "qp: -o requires a file name.\n"); + exit(EXIT_FAILURE); + } + fpo = fopen(argv[i], "w"); + if (fpo == NULL) { + perror(argv[i]); + exit(EXIT_FAILURE); + } + break; + case 'u': + encode = false; + break; + default: + usage(); + exit(EXIT_FAILURE); + } + } else { + fp = fopen(argv[i], "r"); + if (fp == NULL) { + perror(argv[i]); + exit(EXIT_FAILURE); + } + } + } + qp(fp, fpo, encode); + + return (EXIT_SUCCESS); +} Index: usr.bin/bintrans/quoted-printable.c =================================================================== --- usr.bin/bintrans/quoted-printable.c +++ /dev/null @@ -1,265 +0,0 @@ -/* -Copyright (c) 1991 Bell Communications Research, Inc. (Bellcore) - -Permission to use, copy, modify, and distribute this material -for any purpose and without fee is hereby granted, provided -that the above copyright notice and this permission notice -appear in all copies, and that the name of Bellcore not be -used in advertising or publicity pertaining to this -material without the specific, prior written permission -of an authorized representative of Bellcore. BELLCORE -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 -#include -#include -#include -#include - -extern int main_quotedprintable(int, char *[]); - -static int -PendingBoundary(char *s, char **Boundaries, int *BoundaryCt) -{ - int i; - size_t len; - - if (s[0] != '-' || s[1] != '-') - return (0); - - for (i = 0; i < *BoundaryCt; ++i) { - len = strlen(Boundaries[i]); - if (strncmp(s, Boundaries[i], len) == 0) { - if (s[len] == '-' && s[len + 1] == '-') - *BoundaryCt = i; - return (1); - } - } - return (0); -} - -#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, - 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1, -1,-1,-1,-1, - -1,10,11,12, 13,14,15,-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,10,11,12, 13,14,15,-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 -}; - -/* The following version generated complaints on Solaris. */ -/* #define hexchar(c) (((c) < 0 || (c) > 127) ? -1 : index_hex[(c)]) */ -/* Since we're no longer ever calling it with anything signed, this should work: */ -#define hexchar(c) (((c) > 127) ? -1 : index_hex[(c)]) - -static void -toqp(FILE *infile, FILE *outfile) -{ - int c, ct = 0, prevc = 255; - - while ((c = getc(infile)) != EOF) { - if ((c < 32 && (c != '\n' && c != '\t')) - || (c == '=') - || (c >= 127) - /* Following line is to avoid single periods alone on lines, - 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); - ct += 3; - prevc = 'A'; /* close enough */ - } else if (c == '\n') { - if (prevc == ' ' || prevc == '\t') { - putc('=', outfile); /* soft & hard lines */ - putc(c, outfile); - } - putc(c, outfile); - ct = 0; - prevc = c; - } else { - if (c == 'F' && prevc == '\n') { - /* HORRIBLE but clever hack suggested by MTR for sendmail-avoidance */ - c = getc(infile); - if (c == 'r') { - c = getc(infile); - if (c == 'o') { - c = getc(infile); - if (c == 'm') { - c = getc(infile); - if (c == ' ') { - /* This is the case we are looking for */ - fputs("=46rom", outfile); - ct += 6; - } else { - fputs("From", outfile); - ct += 4; - } - } else { - fputs("Fro", outfile); - ct += 3; - } - } else { - fputs("Fr", outfile); - ct += 2; - } - } else { - putc('F', outfile); - ++ct; - } - ungetc(c, infile); - prevc = 'x'; /* close enough -- printable */ - } else { /* END horrible hack */ - putc(c, outfile); - ++ct; - prevc = c; - } - } - if (ct > 72) { - putc('=', outfile); - putc('\n', outfile); - ct = 0; - prevc = '\n'; - } - } - if (ct) { - putc('=', outfile); - putc('\n', outfile); - } -} - -static void -fromqp(FILE *infile, FILE *outfile, char **boundaries, int *boundaryct) -{ - 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 == '-') { - char Buf[200]; - unsigned char *s; - - ungetc(c1, infile); - fgets(Buf, sizeof(Buf), infile); - if (boundaries - && 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 = false; - } - for (s = (unsigned char *)Buf; *s; ++s) { - if (*s == '=') { - if (*++s == 0) - break; - if (*s == '\n') { - /* ignore it */ - sawnewline = true; - } else { - c1 = hexchar(*s); - if (*++s == 0) - break; - c2 = hexchar(*s); - putc(c1 << 4 | c2, outfile); - } - } else { - putc(*s, outfile); - } - } - } else { - if (neednewline) { - putc('\n', outfile); - neednewline = false; - } - if (c1 == '=') { - sawnewline = false; - c1 = getc(infile); - if (c1 == '\n') { - /* ignore it */ - sawnewline = true; - } else { - c2 = getc(infile); - c1 = hexchar(c1); - c2 = hexchar(c2); - putc(c1 << 4 | c2, outfile); - if (c2 == '\n') - sawnewline = true; - } - } else { - if (c1 == '\n') { - sawnewline = true; - neednewline = true; - } else { - sawnewline = false; - putc(c1, outfile); - } - } - } - } - if (neednewline) { - putc('\n', outfile); - neednewline = false; - } -} - -static void -usage(void) -{ - fprintf(stderr, - "usage: bintrans qp [-u] [-o outputfile] [file name]\n"); -} - -int -main_quotedprintable(int argc, char *argv[]) -{ - int i; - bool encode = true; - FILE *fp = stdin; - FILE *fpo = stdout; - - for (i = 1; i < argc; ++i) { - if (argv[i][0] == '-') { - switch (argv[i][1]) { - case 'o': - if (++i >= argc) { - fprintf(stderr, "qp: -o requires a file name.\n"); - exit(EXIT_FAILURE); - } - fpo = fopen(argv[i], "w"); - if (fpo == NULL) { - perror(argv[i]); - exit(EXIT_FAILURE); - } - break; - case 'u': - encode = false; - break; - default: - usage(); - exit(EXIT_FAILURE); - } - } else { - fp = fopen(argv[i], "r"); - if (fp == NULL) { - perror(argv[i]); - exit(EXIT_FAILURE); - } - } - } - if (encode) - toqp(fp, fpo); - else - fromqp(fp, fpo, NULL, 0); - return (0); -} Index: usr.bin/bintrans/tests/Makefile =================================================================== --- usr.bin/bintrans/tests/Makefile +++ usr.bin/bintrans/tests/Makefile @@ -2,8 +2,10 @@ PACKAGE= tests +ATF_TESTS_SH+= bintrans_test TAP_TESTS_SH= legacy_test +${PACKAGE}FILES+= textqpenc textqpdec ${PACKAGE}FILES+= regress.base64.in regress.base64.out ${PACKAGE}FILES+= regress.in regress.out ${PACKAGE}FILES+= regress.sh Index: usr.bin/bintrans/tests/bintrans_test.sh =================================================================== --- /dev/null +++ usr.bin/bintrans/tests/bintrans_test.sh @@ -0,0 +1,36 @@ +atf_test_case encode_qp +encode_qp_body() +{ + atf_check -e empty -o file:"$(atf_get_srcdir)/textqpenc" bintrans qp $(atf_get_srcdir)/textqpdec + +} + +atf_test_case decode_qp +decode_qp_body() +{ + printf "=" > test + atf_check -e empty -o inline:"=" bintrans qp -u test + printf "=\ra" > test + atf_check -e empty -o inline:"=\ra" bintrans qp -u test + printf "=\r\na" > test + atf_check -e empty -o inline:"a" bintrans qp -u test + printf "This is a line" > test + atf_check -e empty -o inline:"This is a line" bintrans qp -u test + printf "This= is a line" > test + atf_check -e empty -o inline:"This= is a line" bintrans qp -u test + printf "This=2 is a line" > test + atf_check -e empty -o inline:"This=2 is a line" bintrans qp -u test + printf "This=23 is a line" > test + atf_check -e empty -o inline:"This# is a line" bintrans qp -u test + printf "This=3D is a line" > test + atf_check -e empty -o inline:"This= is a line" bintrans qp -u test + printf "This_ is a line" > test + atf_check -e empty -o inline:"This_ is a line" bintrans qp -u test + atf_check -e empty -o file:"$(atf_get_srcdir)/textqpdec" bintrans qp -u $(atf_get_srcdir)/textqpenc +} + +atf_init_test_cases() +{ + atf_add_test_case decode_qp + atf_add_test_case encode_qp +} Index: usr.bin/bintrans/tests/textqpdec =================================================================== --- /dev/null +++ usr.bin/bintrans/tests/textqpdec @@ -0,0 +1,2 @@ +J'interdis aux marchands de vanter trop leurs marchandises. Car ils se font vite pédagogues et t'enseignent comme but ce qui n'est par essence qu'un moyen, et te trompant ainsi sur la route à suivre les voilà bientôt qui te dégradent, car si leur musique est vulgaire ils te fabriquent pour te la vendre une âme vulgaire. + — Antoine de Saint-Exupéry, Citadelle (1948) Index: usr.bin/bintrans/tests/textqpenc =================================================================== --- /dev/null +++ usr.bin/bintrans/tests/textqpenc @@ -0,0 +1,6 @@ +J'interdis aux marchands de vanter trop leurs marchandises. Car ils se font= + vite p=C3=A9dagogues et t'enseignent comme but ce qui n'est par essence qu= +'un moyen, et te trompant ainsi sur la route =C3=A0 suivre les voil=C3=A0 b= +ient=C3=B4t qui te d=C3=A9gradent, car si leur musique est vulgaire ils te = +fabriquent pour te la vendre une =C3=A2me vulgaire=2E + =E2=80=94=E2=80=89Antoine de Saint-Exup=C3=A9ry, Citadelle (1948)