2 Copyright (c) 1991 Bell Communications Research, Inc. (Bellcore)
4 Permission to use, copy, modify, and distribute this material
5 for any purpose and without fee is hereby granted, provided
6 that the above copyright notice and this permission notice
7 appear in all copies, and that the name of Bellcore not be
8 used in advertising or publicity pertaining to this
9 material without the specific, prior written permission
10 of an authorized representative of Bellcore. BELLCORE
11 MAKES NO REPRESENTATIONS ABOUT THE ACCURACY OR SUITABILITY
12 OF THIS MATERIAL FOR ANY PURPOSE. IT IS PROVIDED "AS IS",
13 WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
21 extern int main_quotedprintable(int, char *[]);
24 PendingBoundary(char *s, char **Boundaries, int *BoundaryCt)
29 if (s[0] != '-' || s[1] != '-')
32 for (i = 0; i < *BoundaryCt; ++i) {
33 len = strlen(Boundaries[i]);
34 if (strncmp(s, Boundaries[i], len) == 0) {
35 if (s[len] == '-' && s[len + 1] == '-')
43 #define basis_hex "0123456789ABCDEF"
44 static const char index_hex[128] = {
45 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
46 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
47 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
48 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1, -1,-1,-1,-1,
49 -1,10,11,12, 13,14,15,-1, -1,-1,-1,-1, -1,-1,-1,-1,
50 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
51 -1,10,11,12, 13,14,15,-1, -1,-1,-1,-1, -1,-1,-1,-1,
52 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1
55 /* The following version generated complaints on Solaris. */
56 /* #define hexchar(c) (((c) < 0 || (c) > 127) ? -1 : index_hex[(c)]) */
57 /* Since we're no longer ever calling it with anything signed, this should work: */
58 #define hexchar(c) (((c) > 127) ? -1 : index_hex[(c)])
61 toqp(FILE *infile, FILE *outfile)
63 int c, ct = 0, prevc = 255;
65 while ((c = getc(infile)) != EOF) {
66 if ((c < 32 && (c != '\n' && c != '\t'))
69 /* Following line is to avoid single periods alone on lines,
70 which messes up some dumb smtp implementations, sigh... */
71 || (ct == 0 && c == '.')) {
73 putc(basis_hex[c >> 4], outfile);
74 putc(basis_hex[c & 0xF], outfile);
76 prevc = 'A'; /* close enough */
77 } else if (c == '\n') {
78 if (prevc == ' ' || prevc == '\t') {
79 putc('=', outfile); /* soft & hard lines */
86 if (c == 'F' && prevc == '\n') {
87 /* HORRIBLE but clever hack suggested by MTR for sendmail-avoidance */
96 /* This is the case we are looking for */
97 fputs("=46rom", outfile);
100 fputs("From", outfile);
104 fputs("Fro", outfile);
108 fputs("Fr", outfile);
116 prevc = 'x'; /* close enough -- printable */
117 } else { /* END horrible hack */
137 fromqp(FILE *infile, FILE *outfile, char **boundaries, int *boundaryct)
140 bool sawnewline = true, neednewline = false;
141 /* The neednewline hack is necessary because the newline leading into
142 a multipart boundary is part of the boundary, not the data */
144 while ((c1 = getc(infile)) != EOF) {
145 if (sawnewline && boundaries && c1 == '-') {
150 fgets(Buf, sizeof(Buf), infile);
154 && PendingBoundary(Buf, boundaries, boundaryct)) {
157 /* Not a boundary, now we must treat THIS line as q-p, sigh */
162 for (s = (unsigned char *)Buf; *s; ++s) {
174 putc(c1 << 4 | c2, outfile);
195 putc(c1 << 4 | c2, outfile);
220 "usage: bintrans qp [-u] [-o outputfile] [file name]\n");
224 main_quotedprintable(int argc, char *argv[])
231 for (i = 1; i < argc; ++i) {
232 if (argv[i][0] == '-') {
233 switch (argv[i][1]) {
236 fprintf(stderr, "qp: -o requires a file name.\n");
239 fpo = fopen(argv[i], "w");
253 fp = fopen(argv[i], "r");
263 fromqp(fp, fpo, NULL, 0);