/[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.4 - (hide annotations) (download)
Tue Feb 26 17:05:00 2008 UTC (16 years, 2 months ago) by jmc
Branch: MAIN
CVS Tags: checkpoint64y, checkpoint64x, checkpoint64z, checkpoint64q, checkpoint64p, checkpoint64s, checkpoint64r, checkpoint64u, checkpoint64t, checkpoint64w, checkpoint64v, checkpoint64i, checkpoint64h, checkpoint64k, checkpoint64j, checkpoint64m, checkpoint64l, checkpoint64o, checkpoint64n, checkpoint64a, checkpoint64c, checkpoint64b, checkpoint64e, checkpoint64d, checkpoint64g, checkpoint64f, checkpoint63p, checkpoint63q, checkpoint63r, checkpoint63s, checkpoint63l, checkpoint63m, checkpoint63n, checkpoint63o, checkpoint63h, checkpoint63i, checkpoint63j, checkpoint63k, checkpoint63d, checkpoint63e, checkpoint63f, checkpoint63g, checkpoint63a, checkpoint63b, checkpoint63c, checkpoint64, checkpoint65, checkpoint60, checkpoint61, checkpoint62, checkpoint63, checkpoint66g, checkpoint66f, checkpoint66e, checkpoint66d, checkpoint66c, checkpoint66b, checkpoint66a, checkpoint66o, checkpoint66n, checkpoint66m, checkpoint66l, checkpoint66k, checkpoint66j, checkpoint66i, checkpoint66h, checkpoint65z, checkpoint65x, checkpoint65y, checkpoint65r, checkpoint65s, checkpoint65p, checkpoint65q, checkpoint65v, checkpoint65w, checkpoint65t, checkpoint65u, checkpoint65j, checkpoint65k, checkpoint65h, checkpoint65i, checkpoint65n, checkpoint65o, checkpoint65l, checkpoint65m, checkpoint65b, checkpoint65c, checkpoint65a, checkpoint65f, checkpoint65g, checkpoint65d, checkpoint65e, checkpoint59q, checkpoint59p, checkpoint59r, checkpoint59o, checkpoint62c, checkpoint62b, checkpoint62a, checkpoint62g, checkpoint62f, checkpoint62e, checkpoint62d, checkpoint62k, checkpoint62j, checkpoint62i, checkpoint62h, checkpoint62o, checkpoint62n, checkpoint62m, checkpoint62l, checkpoint62s, checkpoint62r, checkpoint62q, checkpoint62p, checkpoint62w, checkpoint62v, checkpoint62u, checkpoint62t, checkpoint62z, checkpoint62y, checkpoint62x, checkpoint61f, checkpoint61g, checkpoint61d, checkpoint61e, checkpoint61b, checkpoint61c, checkpoint61a, checkpoint61n, checkpoint61o, checkpoint61l, checkpoint61m, checkpoint61j, checkpoint61k, checkpoint61h, checkpoint61i, checkpoint61v, checkpoint61w, checkpoint61t, checkpoint61u, checkpoint61r, checkpoint61s, checkpoint61p, checkpoint61q, checkpoint61z, checkpoint61x, checkpoint61y, HEAD
Changes since 1.3: +13 -9 lines
File MIME type: text/plain
put again new version (1.6-4, see MITgcm_contrib/mpack_src) of mpack sources.
among others:
 - fix MD5 code on some 64 bit platforms.
 - fix lot of compilation warnings.

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 jmc 1.4 #include <stdlib.h>
27 edhill 1.1 #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 jmc 1.4 extern void os_perror(char *str);
34     extern int to64(FILE *infile, FILE *outfile, long int limit);
35 edhill 1.1
36     #define NUMREFERENCES 4
37 jmc 1.4 int attachment;
38 edhill 1.1
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 jmc 1.4 char *digest, *appledigest = NULL;
51 edhill 1.1 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 jmc 1.4 if ((p = strrchr(cleanfname, '.'))) cleanfname = p+1;
65 edhill 1.1 #else
66 jmc 1.4 if ((p = strrchr(cleanfname, '/'))) cleanfname = p+1;
67     if ((p = strrchr(cleanfname, '\\'))) cleanfname = p+1;
68 edhill 1.1 #endif
69 jmc 1.4 if ((p = strrchr(cleanfname, ':'))) cleanfname = p+1;
70 edhill 1.1
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 jmc 1.4 "Content-Disposition: %s; filename=\"%s\"\n",
197     attachment ? "attachment" : "inline", cleanfname);
198 edhill 1.1 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 jmc 1.4 fprintf(outfile, "Content-Disposition: %s; filename=\"%s\"\n",
227     attachment ? "attachment" : "inline", cleanfname);
228 edhill 1.1 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     }

  ViewVC Help
Powered by ViewVC 1.1.22