File: encode.c

package info (click to toggle)
mpack 1.6-8
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 1,144 kB
  • ctags: 1,184
  • sloc: ansic: 8,785; sh: 330; makefile: 75; perl: 25
file content (247 lines) | stat: -rw-r--r-- 7,348 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/* (C) Copyright 1993,1994 by Carnegie Mellon University
 * All Rights Reserved.
 *
 * Permission to use, copy, modify, distribute, and sell this software
 * and its documentation for any purpose is hereby granted without
 * fee, provided that the above copyright notice appear in all copies
 * and that both that copyright notice and this permission notice
 * appear in supporting documentation, and that the name of Carnegie
 * Mellon University not be used in advertising or publicity
 * pertaining to distribution of the software without specific,
 * written prior permission.  Carnegie Mellon University makes no
 * representations about the suitability of this software for any
 * purpose.  It is provided "as is" without express or implied
 * warranty.
 *
 * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
 * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
 * SOFTWARE.
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

extern char *magic_look(FILE *infile);
extern char *os_genid(void);
extern FILE *os_createnewfile(char *fname);
extern char *md5digest(FILE *infile, long int *len);
extern void os_perror(char *str);
extern int to64(FILE *infile, FILE *outfile, long int limit);

#define NUMREFERENCES 4
int attachment;

/*
 * Encode a file into one or more MIME messages, each
 * no larger than 'maxsize'.  A 'maxsize' of zero means no size limit.
 * If 'applefile' is non-null, it is the first part of a multipart/appledouble
 * pair.
 */
int encode(FILE *infile, FILE *applefile, char *fname, FILE *descfile, char *subject, char *headers, long int maxsize, char *typeoverride, char *outfname)
{
    char *type;
    FILE *outfile;
    char *cleanfname, *p;
    char *digest, *appledigest = NULL;
    long filesize, l, written;
    int thispart, numparts = 1;
    int wrotefiletype = 0;
    char *multipartid, *msgid, *referenceid[NUMREFERENCES];
    char buf[1024];
    int i;

    /* Clean up fname for printing */
    cleanfname = fname;
#ifdef __riscos
    /* This filename-cleaning knowledge will probably
     * be moved to the os layer in a future version.
     */
    if ((p = strrchr(cleanfname, '.'))) cleanfname = p+1;
#else
    if ((p = strrchr(cleanfname, '/'))) cleanfname = p+1;
    if ((p = strrchr(cleanfname, '\\'))) cleanfname = p+1;
#endif
    if ((p = strrchr(cleanfname, ':'))) cleanfname = p+1;

    /* Find file type */
    if (typeoverride) {
	type = typeoverride;
    }
    else {
	type = magic_look(infile);
    }

    /* Compute MD5 digests */
    digest = md5digest(infile, &filesize);
    if (applefile) {
	appledigest = md5digest(applefile, &l);
	filesize += l;
    }

    /* See if we have to do multipart */
    if (maxsize) {
	filesize = (filesize / 54) * 73; /* Allow for base64 expansion */

	/* Add in size of desc file */
	if (descfile) {
	    free(md5digest(descfile, &l)); 	/* XXX */
	    filesize += l;
	}

	numparts = (filesize-1000)/maxsize + 1;
	if (numparts < 1) numparts = 1;
    }

    multipartid = os_genid();
    for (i=0; i<NUMREFERENCES; i++) {
	referenceid[i] = 0;
    }

    for (thispart=1; thispart <= numparts; thispart++) {
	written = 0;

	/* Open output file */
	if (numparts == 1) {
	    outfile = os_createnewfile(outfname);
	}
	else {
#ifdef __riscos
	    /* Arrgh, riscos uses '.' as directory separator */
	    sprintf(buf, "%s/%02d", outfname, thispart);
#else
	    sprintf(buf, "%s.%02d", outfname, thispart);
#endif
	    outfile = os_createnewfile(buf);
	}
	if (!outfile) {
	    os_perror(buf);
            return 1;
        }
	
	msgid = os_genid();
	fprintf(outfile, "Message-ID: <%s>\n", msgid);
	fprintf(outfile, "Mime-Version: 1.0\n");
	if (headers) fputs(headers, outfile);
	if (numparts > 1) {
	    fprintf(outfile, "Subject: %s (%02d/%02d)\n", subject,
		    thispart, numparts);
	    if (thispart == 1) {
		referenceid[0] = msgid;
	    }
	    else {
		/* Put out References: header pointing to previous parts */
		fprintf(outfile, "References: <%s>\n", referenceid[0]);
		for (i=1; i<NUMREFERENCES; i++) {
		    if (referenceid[i]) fprintf(outfile, "\t <%s>\n",
						referenceid[i]);
		}
		for (i=2; i<NUMREFERENCES; i++) {
		    referenceid[i-1] = referenceid[i];
		}
		referenceid[NUMREFERENCES-1] = msgid;
	    }
	    fprintf(outfile,
		    "Content-Type: message/partial; number=%d; total=%d;\n",
		    thispart, numparts);
	    fprintf(outfile, "\t id=\"%s\"\n", multipartid);
	    fprintf(outfile, "\n");
	}

	if (thispart == 1) {
	    if (numparts > 1) {
		fprintf(outfile, "Message-ID: <%s>\n", multipartid);
		fprintf(outfile, "MIME-Version: 1.0\n");
	    }
	    fprintf(outfile, "Subject: %s\n", subject);
	    fprintf(outfile,
		    "Content-Type: multipart/mixed; boundary=\"-\"\n");
	    fprintf(outfile,
"\nThis is a MIME encoded message.  Decode it with \"munpack\"\n");
	    fprintf(outfile,
"or any other MIME reading software.  Mpack/munpack is available\n");
	    fprintf(outfile,
"via anonymous FTP in ftp.andrew.cmu.edu:pub/mpack/\n");
	    written = 300;

	    /* Spit out description section */
	    if (descfile) {
		fprintf(outfile, "---\n\n");
		while (fgets(buf, sizeof(buf), descfile)) {
		    /* Strip multiple leading dashes as they may become MIME
		     * boundaries
		     */
		    p = buf;
		    if (*p == '-') {
			while (p[1] == '-') p++;
		    }

		    fputs(p, outfile);
		    written += strlen(p);
		}
		fprintf(outfile, "\n");
	    }
    
	    fprintf(outfile, "---\n");

	    if (applefile) {
		fprintf(outfile,
	"Content-Type: multipart/appledouble; boundary=\"=\"; name=\"%s\"\n",
			cleanfname);
		fprintf(outfile,
			"Content-Disposition: %s; filename=\"%s\"\n",
		        attachment ? "attachment" : "inline", cleanfname);
		fprintf(outfile, "\n\n--=\n");
		fprintf(outfile, "Content-Type: application/applefile\n");
		fprintf(outfile, "Content-Transfer-Encoding: base64\n");
		fprintf(outfile, "Content-MD5: %s\n\n", appledigest);
		free(appledigest);
		written += 100;
	    }

	}

	if (applefile && !feof(applefile)) {
	    if (written == maxsize) written--; /* avoid a nasty fencepost error */
	    written += to64(applefile, outfile,
			    (thispart == numparts) ? 0 : (maxsize-written));

	    if (!feof(applefile)) {
		fclose(outfile);
		continue;
	    }

	    fprintf(outfile, "\n--=\n");
	}


	if (!wrotefiletype++) {
	    fprintf(outfile, "Content-Type: %s; name=\"%s\"\n", type,
		    cleanfname);
	    fprintf(outfile, "Content-Transfer-Encoding: base64\n");
	    fprintf(outfile, "Content-Disposition: %s; filename=\"%s\"\n",
		    attachment ? "attachment" : "inline", cleanfname);
	    fprintf(outfile, "Content-MD5: %s\n\n", digest);
	    free(digest);
	    written += 80;
	}

	if (written == maxsize) written--; /* avoid a nasty fencepost error */

	written += to64(infile, outfile,
			(thispart == numparts) ? 0 : (maxsize-written));

	if (thispart == numparts) {
	    if (applefile) fprintf(outfile, "\n--=--\n");
	    fprintf(outfile, "\n-----\n");
	}
	
	fclose(outfile);    
    }

    return 0;
}