/[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.1 - (hide annotations) (download)
Tue Aug 26 20:45:25 2003 UTC (20 years, 8 months ago) by edhill
Branch: MAIN
CVS Tags: checkpoint58l_post, checkpoint51k_post, checkpoint57t_post, checkpoint57o_post, checkpoint52l_pre, checkpoint52e_pre, hrcube4, checkpoint58e_post, mitgcm_mapl_00, checkpoint52n_post, checkpoint52j_post, checkpoint53d_post, checkpoint58u_post, checkpoint58w_post, checkpoint54a_pre, checkpoint51o_pre, checkpoint57m_post, checkpoint55c_post, checkpoint54e_post, checkpoint52e_post, checkpoint57s_post, checkpoint51n_pre, checkpoint54a_post, checkpoint53c_post, checkpoint57k_post, checkpoint55d_pre, checkpoint57d_post, checkpoint51l_post, checkpoint57g_post, checkpoint51q_post, checkpoint57b_post, checkpoint57c_pre, checkpoint51j_post, checkpoint58r_post, checkpoint55j_post, checkpoint56b_post, checkpoint57i_post, checkpoint57y_post, hrcube_1, checkpoint57e_post, branch-netcdf, checkpoint52d_pre, checkpoint52l_post, checkpoint55h_post, checkpoint58n_post, checkpoint53b_post, checkpoint58x_post, checkpoint52k_post, checkpoint52b_pre, checkpoint57g_pre, checkpoint54b_post, checkpoint53b_pre, checkpoint55b_post, checkpoint58t_post, checkpoint58h_post, checkpoint54d_post, checkpoint56c_post, checkpoint52m_post, checkpoint57y_pre, checkpoint55, checkpoint53a_post, checkpoint57f_pre, checkpoint57a_post, checkpoint54, checkpoint58q_post, checkpoint54f_post, checkpoint57v_post, checkpoint55g_post, checkpoint51o_post, checkpoint51p_post, checkpoint58j_post, checkpoint52a_pre, checkpoint59e, checkpoint59d, checkpoint59g, checkpoint59f, checkpoint59a, checkpoint55f_post, checkpoint59c, checkpoint59b, checkpoint59m, checkpoint59l, checkpoint59n, checkpoint59i, checkpoint59h, checkpoint59k, checkpoint59j, checkpoint57r_post, checkpoint59, checkpoint58, checkpoint57a_pre, checkpoint55i_post, checkpoint57, checkpoint56, checkpoint51i_post, checkpoint53, checkpoint52, checkpoint51f_post, checkpoint52d_post, eckpoint57e_pre, checkpoint51r_post, checkpoint52a_post, checkpoint57h_done, checkpoint58f_post, checkpoint52b_post, checkpoint53g_post, checkpoint52f_post, branchpoint-genmake2, checkpoint57x_post, checkpoint57n_post, checkpoint52c_post, checkpoint58d_post, checkpoint58c_post, checkpoint57w_post, checkpoint57p_post, checkpint57u_post, checkpoint57f_post, checkpoint58a_post, checkpoint51h_pre, checkpoint51l_pre, checkpoint58i_post, checkpoint57q_post, checkpoint51g_post, checkpoint58g_post, ecco_c52_e35, hrcube5, checkpoint58o_post, checkpoint57z_post, checkpoint57c_post, checkpoint58y_post, checkpoint55e_post, checkpoint58k_post, checkpoint52i_post, checkpoint52j_pre, checkpoint58v_post, checkpoint53f_post, checkpoint55a_post, checkpoint51t_post, checkpoint53d_pre, checkpoint54c_post, checkpoint58s_post, checkpoint58p_post, checkpoint51n_post, checkpoint57j_post, checkpoint58b_post, checkpoint57h_pre, checkpoint51i_pre, checkpoint58m_post, checkpoint57l_post, checkpoint52i_pre, checkpoint51u_post, checkpoint52h_pre, checkpoint52f_pre, checkpoint57h_post, hrcube_2, hrcube_3, checkpoint56a_post, checkpoint51m_post, checkpoint51s_post, checkpoint55d_post
Branch point for: branch-nonh, branch-genmake2, tg2-branch, checkpoint51n_branch, netcdf-sm0
File MIME type: text/plain
Initial check-in of the CMU "mpack" utility.  This allows us to (portably)
send MITgcm output as MIME-encoded email messages and will be used by the
"testreport" script.  The CMU license is basically an "AS-IS" statement.

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     char *digest, *appledigest;
47     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     if (p = strrchr(cleanfname, '.')) cleanfname = p+1;
61     #else
62     if (p = strrchr(cleanfname, '/')) cleanfname = p+1;
63     if (p = strrchr(cleanfname, '\\')) cleanfname = p+1;
64     #endif
65     if (p = strrchr(cleanfname, ':')) cleanfname = p+1;
66    
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     "Content-Disposition: inline; filename=\"%s\"\n",
193     cleanfname);
194     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     fprintf(outfile, "Content-Disposition: inline; filename=\"%s\"\n",
223     cleanfname);
224     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