Page MenuHomeFreeBSD

D36314.id109729.diff
No OneTemporary

D36314.id109729.diff

Index: usr.bin/bintrans/Makefile
===================================================================
--- usr.bin/bintrans/Makefile
+++ usr.bin/bintrans/Makefile
@@ -4,7 +4,7 @@
.include <src.opts.mk>
PROG= bintrans
-SRCS= bintrans.c uuencode.c uudecode.c quoted-printable.c
+SRCS= bintrans.c uuencode.c uudecode.c quoted-printable.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,93 @@
+/*
+ * Copyright (C) 2022 Baptiste Daroussin <bapt@FreeBSD.org>
+ *
+ * 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 <ctype.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+
+extern void fromqp(FILE *in, FILE *out);
+
+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++;
+ }
+}
+
+void
+fromqp(FILE *fp, FILE *fpo)
+{
+ char *line = NULL;
+ size_t linecap = 0;
+ ssize_t linelen;
+
+ while ((linelen = getline(&line, &linecap, fp)) > 0)
+ decode_quoted_printable(line, fpo);
+ free(line);
+}
Index: usr.bin/bintrans/quoted-printable.c
===================================================================
--- usr.bin/bintrans/quoted-printable.c
+++ usr.bin/bintrans/quoted-printable.c
@@ -20,26 +20,6 @@
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,
@@ -133,85 +113,7 @@
}
}
-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;
- }
-}
+extern void fromqp(FILE *infile, FILE *outfile);
static void
usage(void)
@@ -260,6 +162,6 @@
if (encode)
toqp(fp, fpo);
else
- fromqp(fp, fpo, NULL, 0);
+ fromqp(fp, fpo);
return (0);
}
Index: usr.bin/bintrans/tests/Makefile
===================================================================
--- usr.bin/bintrans/tests/Makefile
+++ usr.bin/bintrans/tests/Makefile
@@ -2,6 +2,7 @@
PACKAGE= tests
+ATF_TESTS_SH+= bintrans_test
TAP_TESTS_SH= legacy_test
${PACKAGE}FILES+= regress.base64.in regress.base64.out
Index: usr.bin/bintrans/tests/bintrans_test.sh
===================================================================
--- /dev/null
+++ usr.bin/bintrans/tests/bintrans_test.sh
@@ -0,0 +1,29 @@
+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
+ printf "J'interdis aux marchands de vanter trop leurs marchandises. Car ils se font=\r\n vite p=C3=A9dagogues et t'enseignent comme but ce qui n'est par essence qu=\r\n'un moyen, et te trompant ainsi sur la route =C3=A0 suivre les voil=C3=\r\n=A0 bient=C3=B4t qui te d=C3=A9gradent, car si leur musique est vulgaire il=\r\ns te fabriquent pour te la vendre une =C3=A2me vulgaire." > test
+ atf_check -e empty -o inline:"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." bintrans qp -u test
+}
+
+atf_init_test_cases()
+{
+ atf_add_test_case decode_qp
+}

File Metadata

Mime Type
text/plain
Expires
Thu, Jan 16, 9:45 PM (12 h, 5 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
15829496
Default Alt Text
D36314.id109729.diff (7 KB)

Event Timeline