Back to home page

MITgcm

 
 

    


File indexing completed on 2018-03-02 18:44:45 UTC

view on githubraw file Latest commit e768bd12 on 2008-02-26 17:05:00 UTC
ec6cf3b09d Ed H*0001 /* (C) Copyright 1993,1994 by Carnegie Mellon University
                0002  * All Rights Reserved.
                0003  *
                0004  * Permission to use, copy, modify, distribute, and sell this software
                0005  * and its documentation for any purpose is hereby granted without
                0006  * fee, provided that the above copyright notice appear in all copies
                0007  * and that both that copyright notice and this permission notice
                0008  * appear in supporting documentation, and that the name of Carnegie
                0009  * Mellon University not be used in advertising or publicity
                0010  * pertaining to distribution of the software without specific,
                0011  * written prior permission.  Carnegie Mellon University makes no
                0012  * representations about the suitability of this software for any
                0013  * purpose.  It is provided "as is" without express or implied
                0014  * warranty.
                0015  *
                0016  * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
                0017  * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
                0018  * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
                0019  * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                0020  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
                0021  * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
                0022  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
                0023  * SOFTWARE.
                0024  */
                0025 /*
                0026 Copyright (c) 1991 Bell Communications Research, Inc. (Bellcore)
                0027 
                0028 Permission to use, copy, modify, and distribute this material 
                0029 for any purpose and without fee is hereby granted, provided 
                0030 that the above copyright notice and this permission notice 
                0031 appear in all copies, and that the name of Bellcore not be 
                0032 used in advertising or publicity pertaining to this 
                0033 material without the specific, prior written permission 
                0034 of an authorized representative of Bellcore.  BELLCORE 
                0035 MAKES NO REPRESENTATIONS ABOUT THE ACCURACY OR SUITABILITY 
                0036 OF THIS MATERIAL FOR ANY PURPOSE.  IT IS PROVIDED "AS IS", 
                0037 WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.  */
                0038 #include <stdio.h>
                0039 #include <string.h>
                0040 #include <ctype.h>
                0041 #include "xmalloc.h"
                0042 #include "md5.h"
                0043 
                0044 void output64chunk(int c1, int c2, int c3, int pads, FILE *outfile);
                0045 static char basis_64[] =
                0046    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
                0047 
                0048 int to64(FILE *infile, FILE *outfile, long int limit)
                0049 {
                0050     int c1, c2, c3, ct=0, written=0;
                0051 
                0052     if (limit && limit < 73) return 1;
                0053 
                0054     while ((c1 = getc(infile)) != EOF) {
                0055         c2 = getc(infile);
                0056         if (c2 == EOF) {
                0057             output64chunk(c1, 0, 0, 2, outfile);
                0058         } else {
                0059             c3 = getc(infile);
                0060             if (c3 == EOF) {
                0061                 output64chunk(c1, c2, 0, 1, outfile);
                0062             } else {
                0063                 output64chunk(c1, c2, c3, 0, outfile);
                0064             }
                0065         }
                0066         ct += 4;
                0067         if (ct > 71) {
                0068             putc('\n', outfile);
                0069         if (limit) {
                0070         limit -= ct + 1;
                0071         if (limit < 73) return 1;
                0072         }
                0073         written += 73;
                0074             ct = 0;
                0075         }
                0076     }
                0077     if (ct) {
                0078     putc('\n', outfile);
                0079     ct++;
                0080     }
                0081     return written + ct;
                0082 }
                0083 
                0084 void output64chunk(int c1, int c2, int c3, int pads, FILE *outfile)
                0085 {
                0086     putc(basis_64[c1>>2], outfile);
                0087     putc(basis_64[((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4)], outfile);
                0088     if (pads == 2) {
                0089         putc('=', outfile);
                0090         putc('=', outfile);
                0091     } else if (pads) {
                0092         putc(basis_64[((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6)], outfile);
                0093         putc('=', outfile);
                0094     } else {
                0095         putc(basis_64[((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6)], outfile);
                0096         putc(basis_64[c3 & 0x3F], outfile);
                0097     }
                0098 }
                0099 
                0100 char *md5contextTo64(MD5_CTX *context)
                0101 {
                0102     unsigned char digest[18];
                0103     char encodedDigest[25];
                0104     int i;
                0105     char *p;
                0106 
                0107     MD5Final(digest, context);
                0108     digest[sizeof(digest)-1] = digest[sizeof(digest)-2] = 0;
                0109 
                0110     p = encodedDigest;
                0111     for (i=0; i < sizeof(digest); i+=3) {
                0112     *p++ = basis_64[digest[i]>>2];
                0113     *p++ = basis_64[((digest[i] & 0x3)<<4) | ((digest[i+1] & 0xF0)>>4)];
                0114     *p++ = basis_64[((digest[i+1] & 0xF)<<2) | ((digest[i+2] & 0xC0)>>6)];
                0115     *p++ = basis_64[digest[i+2] & 0x3F];
                0116     }
                0117     *p-- = '\0';
                0118     *p-- = '=';
                0119     *p-- = '=';
                0120     return strsave(encodedDigest);
                0121 }    
                0122 
                0123 char *md5digest(FILE *infile, long int *len)
                0124 {
                0125     MD5_CTX context;
                0126     char buf[1000];
                0127     long length = 0;
                0128     int nbytes;
                0129     
                0130     MD5Init(&context);
e768bd1221 Jean*0131     while ((nbytes = fread(buf, 1, sizeof(buf), infile))) {
ec6cf3b09d Ed H*0132     length += nbytes;
                0133     MD5Update(&context, buf, nbytes);
                0134     }
                0135     rewind(infile);
                0136     if (len) *len = length;
                0137     return md5contextTo64(&context);
                0138 }