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
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
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 }