| 1 |
jmc |
1.1 |
/* (C) Copyright 1993,1994 by Carnegie Mellon University |
| 2 |
|
|
* All Rights Reserved. |
| 3 |
|
|
* |
| 4 |
|
|
* Permission to use, copy, modify, distribute, and sell this software |
| 5 |
|
|
* and its documentation for any purpose is hereby granted without |
| 6 |
|
|
* fee, provided that the above copyright notice appear in all copies |
| 7 |
|
|
* and that both that copyright notice and this permission notice |
| 8 |
|
|
* appear in supporting documentation, and that the name of Carnegie |
| 9 |
|
|
* Mellon University not be used in advertising or publicity |
| 10 |
|
|
* pertaining to distribution of the software without specific, |
| 11 |
|
|
* written prior permission. Carnegie Mellon University makes no |
| 12 |
|
|
* representations about the suitability of this software for any |
| 13 |
|
|
* purpose. It is provided "as is" without express or implied |
| 14 |
|
|
* warranty. |
| 15 |
|
|
* |
| 16 |
|
|
* CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO |
| 17 |
|
|
* THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY |
| 18 |
|
|
* AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE |
| 19 |
|
|
* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 20 |
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN |
| 21 |
|
|
* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING |
| 22 |
|
|
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS |
| 23 |
|
|
* SOFTWARE. |
| 24 |
|
|
*/ |
| 25 |
|
|
#include <stdio.h> |
| 26 |
|
|
#include <stdlib.h> |
| 27 |
|
|
#include <string.h> |
| 28 |
|
|
|
| 29 |
|
|
extern char *magic_look(FILE *infile); |
| 30 |
|
|
extern char *os_genid(void); |
| 31 |
|
|
extern FILE *os_createnewfile(char *fname); |
| 32 |
|
|
extern char *md5digest(FILE *infile, long int *len); |
| 33 |
|
|
extern void os_perror(char *str); |
| 34 |
|
|
extern int to64(FILE *infile, FILE *outfile, long int limit); |
| 35 |
|
|
|
| 36 |
|
|
#define NUMREFERENCES 4 |
| 37 |
|
|
int attachment; |
| 38 |
|
|
|
| 39 |
|
|
/* |
| 40 |
|
|
* Encode a file into one or more MIME messages, each |
| 41 |
|
|
* no larger than 'maxsize'. A 'maxsize' of zero means no size limit. |
| 42 |
|
|
* If 'applefile' is non-null, it is the first part of a multipart/appledouble |
| 43 |
|
|
* pair. |
| 44 |
|
|
*/ |
| 45 |
|
|
int encode(FILE *infile, FILE *applefile, char *fname, FILE *descfile, char *subject, char *headers, long int maxsize, char *typeoverride, char *outfname) |
| 46 |
|
|
{ |
| 47 |
|
|
char *type; |
| 48 |
|
|
FILE *outfile; |
| 49 |
|
|
char *cleanfname, *p; |
| 50 |
|
|
char *digest, *appledigest = NULL; |
| 51 |
|
|
long filesize, l, written; |
| 52 |
|
|
int thispart, numparts = 1; |
| 53 |
|
|
int wrotefiletype = 0; |
| 54 |
|
|
char *multipartid, *msgid, *referenceid[NUMREFERENCES]; |
| 55 |
|
|
char buf[1024]; |
| 56 |
|
|
int i; |
| 57 |
|
|
|
| 58 |
|
|
/* Clean up fname for printing */ |
| 59 |
|
|
cleanfname = fname; |
| 60 |
|
|
#ifdef __riscos |
| 61 |
|
|
/* This filename-cleaning knowledge will probably |
| 62 |
|
|
* be moved to the os layer in a future version. |
| 63 |
|
|
*/ |
| 64 |
|
|
if ((p = strrchr(cleanfname, '.'))) cleanfname = p+1; |
| 65 |
|
|
#else |
| 66 |
|
|
if ((p = strrchr(cleanfname, '/'))) cleanfname = p+1; |
| 67 |
|
|
if ((p = strrchr(cleanfname, '\\'))) cleanfname = p+1; |
| 68 |
|
|
#endif |
| 69 |
|
|
if ((p = strrchr(cleanfname, ':'))) cleanfname = p+1; |
| 70 |
|
|
|
| 71 |
|
|
/* Find file type */ |
| 72 |
|
|
if (typeoverride) { |
| 73 |
|
|
type = typeoverride; |
| 74 |
|
|
} |
| 75 |
|
|
else { |
| 76 |
|
|
type = magic_look(infile); |
| 77 |
|
|
} |
| 78 |
|
|
|
| 79 |
|
|
/* Compute MD5 digests */ |
| 80 |
|
|
digest = md5digest(infile, &filesize); |
| 81 |
|
|
if (applefile) { |
| 82 |
|
|
appledigest = md5digest(applefile, &l); |
| 83 |
|
|
filesize += l; |
| 84 |
|
|
} |
| 85 |
|
|
|
| 86 |
|
|
/* See if we have to do multipart */ |
| 87 |
|
|
if (maxsize) { |
| 88 |
|
|
filesize = (filesize / 54) * 73; /* Allow for base64 expansion */ |
| 89 |
|
|
|
| 90 |
|
|
/* Add in size of desc file */ |
| 91 |
|
|
if (descfile) { |
| 92 |
|
|
free(md5digest(descfile, &l)); /* XXX */ |
| 93 |
|
|
filesize += l; |
| 94 |
|
|
} |
| 95 |
|
|
|
| 96 |
|
|
numparts = (filesize-1000)/maxsize + 1; |
| 97 |
|
|
if (numparts < 1) numparts = 1; |
| 98 |
|
|
} |
| 99 |
|
|
|
| 100 |
|
|
multipartid = os_genid(); |
| 101 |
|
|
for (i=0; i<NUMREFERENCES; i++) { |
| 102 |
|
|
referenceid[i] = 0; |
| 103 |
|
|
} |
| 104 |
|
|
|
| 105 |
|
|
for (thispart=1; thispart <= numparts; thispart++) { |
| 106 |
|
|
written = 0; |
| 107 |
|
|
|
| 108 |
|
|
/* Open output file */ |
| 109 |
|
|
if (numparts == 1) { |
| 110 |
|
|
outfile = os_createnewfile(outfname); |
| 111 |
|
|
} |
| 112 |
|
|
else { |
| 113 |
|
|
#ifdef __riscos |
| 114 |
|
|
/* Arrgh, riscos uses '.' as directory separator */ |
| 115 |
|
|
sprintf(buf, "%s/%02d", outfname, thispart); |
| 116 |
|
|
#else |
| 117 |
|
|
sprintf(buf, "%s.%02d", outfname, thispart); |
| 118 |
|
|
#endif |
| 119 |
|
|
outfile = os_createnewfile(buf); |
| 120 |
|
|
} |
| 121 |
|
|
if (!outfile) { |
| 122 |
|
|
os_perror(buf); |
| 123 |
|
|
return 1; |
| 124 |
|
|
} |
| 125 |
|
|
|
| 126 |
|
|
msgid = os_genid(); |
| 127 |
|
|
fprintf(outfile, "Message-ID: <%s>\n", msgid); |
| 128 |
|
|
fprintf(outfile, "Mime-Version: 1.0\n"); |
| 129 |
|
|
if (headers) fputs(headers, outfile); |
| 130 |
|
|
if (numparts > 1) { |
| 131 |
|
|
fprintf(outfile, "Subject: %s (%02d/%02d)\n", subject, |
| 132 |
|
|
thispart, numparts); |
| 133 |
|
|
if (thispart == 1) { |
| 134 |
|
|
referenceid[0] = msgid; |
| 135 |
|
|
} |
| 136 |
|
|
else { |
| 137 |
|
|
/* Put out References: header pointing to previous parts */ |
| 138 |
|
|
fprintf(outfile, "References: <%s>\n", referenceid[0]); |
| 139 |
|
|
for (i=1; i<NUMREFERENCES; i++) { |
| 140 |
|
|
if (referenceid[i]) fprintf(outfile, "\t <%s>\n", |
| 141 |
|
|
referenceid[i]); |
| 142 |
|
|
} |
| 143 |
|
|
for (i=2; i<NUMREFERENCES; i++) { |
| 144 |
|
|
referenceid[i-1] = referenceid[i]; |
| 145 |
|
|
} |
| 146 |
|
|
referenceid[NUMREFERENCES-1] = msgid; |
| 147 |
|
|
} |
| 148 |
|
|
fprintf(outfile, |
| 149 |
|
|
"Content-Type: message/partial; number=%d; total=%d;\n", |
| 150 |
|
|
thispart, numparts); |
| 151 |
|
|
fprintf(outfile, "\t id=\"%s\"\n", multipartid); |
| 152 |
|
|
fprintf(outfile, "\n"); |
| 153 |
|
|
} |
| 154 |
|
|
|
| 155 |
|
|
if (thispart == 1) { |
| 156 |
|
|
if (numparts > 1) { |
| 157 |
|
|
fprintf(outfile, "Message-ID: <%s>\n", multipartid); |
| 158 |
|
|
fprintf(outfile, "MIME-Version: 1.0\n"); |
| 159 |
|
|
} |
| 160 |
|
|
fprintf(outfile, "Subject: %s\n", subject); |
| 161 |
|
|
fprintf(outfile, |
| 162 |
|
|
"Content-Type: multipart/mixed; boundary=\"-\"\n"); |
| 163 |
|
|
fprintf(outfile, |
| 164 |
|
|
"\nThis is a MIME encoded message. Decode it with \"munpack\"\n"); |
| 165 |
|
|
fprintf(outfile, |
| 166 |
|
|
"or any other MIME reading software. Mpack/munpack is available\n"); |
| 167 |
|
|
fprintf(outfile, |
| 168 |
|
|
"via anonymous FTP in ftp.andrew.cmu.edu:pub/mpack/\n"); |
| 169 |
|
|
written = 300; |
| 170 |
|
|
|
| 171 |
|
|
/* Spit out description section */ |
| 172 |
|
|
if (descfile) { |
| 173 |
|
|
fprintf(outfile, "---\n\n"); |
| 174 |
|
|
while (fgets(buf, sizeof(buf), descfile)) { |
| 175 |
|
|
/* Strip multiple leading dashes as they may become MIME |
| 176 |
|
|
* boundaries |
| 177 |
|
|
*/ |
| 178 |
|
|
p = buf; |
| 179 |
|
|
if (*p == '-') { |
| 180 |
|
|
while (p[1] == '-') p++; |
| 181 |
|
|
} |
| 182 |
|
|
|
| 183 |
|
|
fputs(p, outfile); |
| 184 |
|
|
written += strlen(p); |
| 185 |
|
|
} |
| 186 |
|
|
fprintf(outfile, "\n"); |
| 187 |
|
|
} |
| 188 |
|
|
|
| 189 |
|
|
fprintf(outfile, "---\n"); |
| 190 |
|
|
|
| 191 |
|
|
if (applefile) { |
| 192 |
|
|
fprintf(outfile, |
| 193 |
|
|
"Content-Type: multipart/appledouble; boundary=\"=\"; name=\"%s\"\n", |
| 194 |
|
|
cleanfname); |
| 195 |
|
|
fprintf(outfile, |
| 196 |
|
|
"Content-Disposition: %s; filename=\"%s\"\n", |
| 197 |
|
|
attachment ? "attachment" : "inline", cleanfname); |
| 198 |
|
|
fprintf(outfile, "\n\n--=\n"); |
| 199 |
|
|
fprintf(outfile, "Content-Type: application/applefile\n"); |
| 200 |
|
|
fprintf(outfile, "Content-Transfer-Encoding: base64\n"); |
| 201 |
|
|
fprintf(outfile, "Content-MD5: %s\n\n", appledigest); |
| 202 |
|
|
free(appledigest); |
| 203 |
|
|
written += 100; |
| 204 |
|
|
} |
| 205 |
|
|
|
| 206 |
|
|
} |
| 207 |
|
|
|
| 208 |
|
|
if (applefile && !feof(applefile)) { |
| 209 |
|
|
if (written == maxsize) written--; /* avoid a nasty fencepost error */ |
| 210 |
|
|
written += to64(applefile, outfile, |
| 211 |
|
|
(thispart == numparts) ? 0 : (maxsize-written)); |
| 212 |
|
|
|
| 213 |
|
|
if (!feof(applefile)) { |
| 214 |
|
|
fclose(outfile); |
| 215 |
|
|
continue; |
| 216 |
|
|
} |
| 217 |
|
|
|
| 218 |
|
|
fprintf(outfile, "\n--=\n"); |
| 219 |
|
|
} |
| 220 |
|
|
|
| 221 |
|
|
|
| 222 |
|
|
if (!wrotefiletype++) { |
| 223 |
|
|
fprintf(outfile, "Content-Type: %s; name=\"%s\"\n", type, |
| 224 |
|
|
cleanfname); |
| 225 |
|
|
fprintf(outfile, "Content-Transfer-Encoding: base64\n"); |
| 226 |
|
|
fprintf(outfile, "Content-Disposition: %s; filename=\"%s\"\n", |
| 227 |
|
|
attachment ? "attachment" : "inline", cleanfname); |
| 228 |
|
|
fprintf(outfile, "Content-MD5: %s\n\n", digest); |
| 229 |
|
|
free(digest); |
| 230 |
|
|
written += 80; |
| 231 |
|
|
} |
| 232 |
|
|
|
| 233 |
|
|
if (written == maxsize) written--; /* avoid a nasty fencepost error */ |
| 234 |
|
|
|
| 235 |
|
|
written += to64(infile, outfile, |
| 236 |
|
|
(thispart == numparts) ? 0 : (maxsize-written)); |
| 237 |
|
|
|
| 238 |
|
|
if (thispart == numparts) { |
| 239 |
|
|
if (applefile) fprintf(outfile, "\n--=--\n"); |
| 240 |
|
|
fprintf(outfile, "\n-----\n"); |
| 241 |
|
|
} |
| 242 |
|
|
|
| 243 |
|
|
fclose(outfile); |
| 244 |
|
|
} |
| 245 |
|
|
|
| 246 |
|
|
return 0; |
| 247 |
|
|
} |