/[MITgcm]/MITgcm/tools/mpack-1.6/encode.c
ViewVC logotype

Annotation of /MITgcm/tools/mpack-1.6/encode.c

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.3 - (hide annotations) (download)
Sat Feb 23 03:30:03 2008 UTC (16 years, 2 months ago) by jmc
Branch: MAIN
Changes since 1.2: +9 -13 lines
File MIME type: text/plain
Was working nicly before checked-in ; but because of modification-time
changes (due to the check-in), dependencies have changed, and is now
failling when trying to run Automake/Autoconf.
=> go back to previous version (hopefully should works).

1 edhill 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 <string.h>
27    
28     extern char *magic_look(FILE *infile);
29     extern char *os_genid(void);
30     extern FILE *os_createnewfile(char *fname);
31     extern char *md5digest(FILE *infile, long int *len);
32    
33     #define NUMREFERENCES 4
34    
35     /*
36     * Encode a file into one or more MIME messages, each
37     * no larger than 'maxsize'. A 'maxsize' of zero means no size limit.
38     * If 'applefile' is non-null, it is the first part of a multipart/appledouble
39     * pair.
40     */
41     int encode(FILE *infile, FILE *applefile, char *fname, FILE *descfile, char *subject, char *headers, long int maxsize, char *typeoverride, char *outfname)
42     {
43     char *type;
44     FILE *outfile;
45     char *cleanfname, *p;
46 jmc 1.3 char *digest, *appledigest;
47 edhill 1.1 long filesize, l, written;
48     int thispart, numparts = 1;
49     int wrotefiletype = 0;
50     char *multipartid, *msgid, *referenceid[NUMREFERENCES];
51     char buf[1024];
52     int i;
53    
54     /* Clean up fname for printing */
55     cleanfname = fname;
56     #ifdef __riscos
57     /* This filename-cleaning knowledge will probably
58     * be moved to the os layer in a future version.
59     */
60 jmc 1.3 if (p = strrchr(cleanfname, '.')) cleanfname = p+1;
61 edhill 1.1 #else
62 jmc 1.3 if (p = strrchr(cleanfname, '/')) cleanfname = p+1;
63     if (p = strrchr(cleanfname, '\\')) cleanfname = p+1;
64 edhill 1.1 #endif
65 jmc 1.3 if (p = strrchr(cleanfname, ':')) cleanfname = p+1;
66 edhill 1.1
67     /* Find file type */
68     if (typeoverride) {
69     type = typeoverride;
70     }
71     else {
72     type = magic_look(infile);
73     }
74    
75     /* Compute MD5 digests */
76     digest = md5digest(infile, &filesize);
77     if (applefile) {
78     appledigest = md5digest(applefile, &l);
79     filesize += l;
80     }
81    
82     /* See if we have to do multipart */
83     if (maxsize) {
84     filesize = (filesize / 54) * 73; /* Allow for base64 expansion */
85    
86     /* Add in size of desc file */
87     if (descfile) {
88     free(md5digest(descfile, &l)); /* XXX */
89     filesize += l;
90     }
91    
92     numparts = (filesize-1000)/maxsize + 1;
93     if (numparts < 1) numparts = 1;
94     }
95    
96     multipartid = os_genid();
97     for (i=0; i<NUMREFERENCES; i++) {
98     referenceid[i] = 0;
99     }
100    
101     for (thispart=1; thispart <= numparts; thispart++) {
102     written = 0;
103    
104     /* Open output file */
105     if (numparts == 1) {
106     outfile = os_createnewfile(outfname);
107     }
108     else {
109     #ifdef __riscos
110     /* Arrgh, riscos uses '.' as directory separator */
111     sprintf(buf, "%s/%02d", outfname, thispart);
112     #else
113     sprintf(buf, "%s.%02d", outfname, thispart);
114     #endif
115     outfile = os_createnewfile(buf);
116     }
117     if (!outfile) {
118     os_perror(buf);
119     return 1;
120     }
121    
122     msgid = os_genid();
123     fprintf(outfile, "Message-ID: <%s>\n", msgid);
124     fprintf(outfile, "Mime-Version: 1.0\n");
125     if (headers) fputs(headers, outfile);
126     if (numparts > 1) {
127     fprintf(outfile, "Subject: %s (%02d/%02d)\n", subject,
128     thispart, numparts);
129     if (thispart == 1) {
130     referenceid[0] = msgid;
131     }
132     else {
133     /* Put out References: header pointing to previous parts */
134     fprintf(outfile, "References: <%s>\n", referenceid[0]);
135     for (i=1; i<NUMREFERENCES; i++) {
136     if (referenceid[i]) fprintf(outfile, "\t <%s>\n",
137     referenceid[i]);
138     }
139     for (i=2; i<NUMREFERENCES; i++) {
140     referenceid[i-1] = referenceid[i];
141     }
142     referenceid[NUMREFERENCES-1] = msgid;
143     }
144     fprintf(outfile,
145     "Content-Type: message/partial; number=%d; total=%d;\n",
146     thispart, numparts);
147     fprintf(outfile, "\t id=\"%s\"\n", multipartid);
148     fprintf(outfile, "\n");
149     }
150    
151     if (thispart == 1) {
152     if (numparts > 1) {
153     fprintf(outfile, "Message-ID: <%s>\n", multipartid);
154     fprintf(outfile, "MIME-Version: 1.0\n");
155     }
156     fprintf(outfile, "Subject: %s\n", subject);
157     fprintf(outfile,
158     "Content-Type: multipart/mixed; boundary=\"-\"\n");
159     fprintf(outfile,
160     "\nThis is a MIME encoded message. Decode it with \"munpack\"\n");
161     fprintf(outfile,
162     "or any other MIME reading software. Mpack/munpack is available\n");
163     fprintf(outfile,
164     "via anonymous FTP in ftp.andrew.cmu.edu:pub/mpack/\n");
165     written = 300;
166    
167     /* Spit out description section */
168     if (descfile) {
169     fprintf(outfile, "---\n\n");
170     while (fgets(buf, sizeof(buf), descfile)) {
171     /* Strip multiple leading dashes as they may become MIME
172     * boundaries
173     */
174     p = buf;
175     if (*p == '-') {
176     while (p[1] == '-') p++;
177     }
178    
179     fputs(p, outfile);
180     written += strlen(p);
181     }
182     fprintf(outfile, "\n");
183     }
184    
185     fprintf(outfile, "---\n");
186    
187     if (applefile) {
188     fprintf(outfile,
189     "Content-Type: multipart/appledouble; boundary=\"=\"; name=\"%s\"\n",
190     cleanfname);
191     fprintf(outfile,
192 jmc 1.3 "Content-Disposition: inline; filename=\"%s\"\n",
193     cleanfname);
194 edhill 1.1 fprintf(outfile, "\n\n--=\n");
195     fprintf(outfile, "Content-Type: application/applefile\n");
196     fprintf(outfile, "Content-Transfer-Encoding: base64\n");
197     fprintf(outfile, "Content-MD5: %s\n\n", appledigest);
198     free(appledigest);
199     written += 100;
200     }
201    
202     }
203    
204     if (applefile && !feof(applefile)) {
205     if (written == maxsize) written--; /* avoid a nasty fencepost error */
206     written += to64(applefile, outfile,
207     (thispart == numparts) ? 0 : (maxsize-written));
208    
209     if (!feof(applefile)) {
210     fclose(outfile);
211     continue;
212     }
213    
214     fprintf(outfile, "\n--=\n");
215     }
216    
217    
218     if (!wrotefiletype++) {
219     fprintf(outfile, "Content-Type: %s; name=\"%s\"\n", type,
220     cleanfname);
221     fprintf(outfile, "Content-Transfer-Encoding: base64\n");
222 jmc 1.3 fprintf(outfile, "Content-Disposition: inline; filename=\"%s\"\n",
223     cleanfname);
224 edhill 1.1 fprintf(outfile, "Content-MD5: %s\n\n", digest);
225     free(digest);
226     written += 80;
227     }
228    
229     if (written == maxsize) written--; /* avoid a nasty fencepost error */
230    
231     written += to64(infile, outfile,
232     (thispart == numparts) ? 0 : (maxsize-written));
233    
234     if (thispart == numparts) {
235     if (applefile) fprintf(outfile, "\n--=--\n");
236     fprintf(outfile, "\n-----\n");
237     }
238    
239     fclose(outfile);
240     }
241    
242     return 0;
243     }

  ViewVC Help
Powered by ViewVC 1.1.22