home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / Amiga / Workbench / Archivers / mpackPPC.lha / mpackPPC / src / codes.c < prev    next >
C/C++ Source or Header  |  1998-04-08  |  4KB  |  144 lines

  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. /*
  26. Copyright (c) 1991 Bell Communications Research, Inc. (Bellcore)
  27.  
  28. Permission to use, copy, modify, and distribute this material 
  29. for any purpose and without fee is hereby granted, provided 
  30. that the above copyright notice and this permission notice 
  31. appear in all copies, and that the name of Bellcore not be 
  32. used in advertising or publicity pertaining to this 
  33. material without the specific, prior written permission 
  34. of an authorized representative of Bellcore.  BELLCORE 
  35. MAKES NO REPRESENTATIONS ABOUT THE ACCURACY OR SUITABILITY 
  36. OF THIS MATERIAL FOR ANY PURPOSE.  IT IS PROVIDED "AS IS", 
  37. WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.  */
  38. #include <stdio.h>
  39. #include <string.h>
  40. #include <ctype.h>
  41. #include "xmalloc.h"
  42. #include "md5.h"
  43.  
  44. static char basis_64[] =
  45.    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  46.  
  47. int to64(infile, outfile, limit) 
  48. FILE *infile, *outfile;
  49. long limit;
  50. {
  51.     int c1, c2, c3, ct=0, written=0;
  52.  
  53.     if (limit && limit < 73) return 1;
  54.  
  55.     while ((c1 = getc(infile)) != EOF) {
  56.         c2 = getc(infile);
  57.         if (c2 == EOF) {
  58.             output64chunk(c1, 0, 0, 2, outfile);
  59.         } else {
  60.             c3 = getc(infile);
  61.             if (c3 == EOF) {
  62.                 output64chunk(c1, c2, 0, 1, outfile);
  63.             } else {
  64.                 output64chunk(c1, c2, c3, 0, outfile);
  65.             }
  66.         }
  67.         ct += 4;
  68.         if (ct > 71) {
  69.             putc('\n', outfile);
  70.         if (limit) {
  71.         limit -= ct + 1;
  72.         if (limit < 73) return 1;
  73.         }
  74.         written += 73;
  75.             ct = 0;
  76.         }
  77.     }
  78.     if (ct) {
  79.     putc('\n', outfile);
  80.     ct++;
  81.     }
  82.     return written + ct;
  83. }
  84.  
  85. output64chunk(c1, c2, c3, pads, outfile)
  86. FILE *outfile;
  87. {
  88.     putc(basis_64[c1>>2], outfile);
  89.     putc(basis_64[((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4)], outfile);
  90.     if (pads == 2) {
  91.         putc('=', outfile);
  92.         putc('=', outfile);
  93.     } else if (pads) {
  94.         putc(basis_64[((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6)], outfile);
  95.         putc('=', outfile);
  96.     } else {
  97.         putc(basis_64[((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6)], outfile);
  98.         putc(basis_64[c3 & 0x3F], outfile);
  99.     }
  100. }
  101.  
  102. char *md5contextTo64(context)
  103. MD5_CTX *context;
  104. {
  105.     unsigned char digest[18];
  106.     char encodedDigest[25];
  107.     int i;
  108.     char *p;
  109.  
  110.     MD5Final(digest, context);
  111.     digest[sizeof(digest)-1] = digest[sizeof(digest)-2] = 0;
  112.  
  113.     p = encodedDigest;
  114.     for (i=0; i < sizeof(digest); i+=3) {
  115.     *p++ = basis_64[digest[i]>>2];
  116.     *p++ = basis_64[((digest[i] & 0x3)<<4) | ((digest[i+1] & 0xF0)>>4)];
  117.     *p++ = basis_64[((digest[i+1] & 0xF)<<2) | ((digest[i+2] & 0xC0)>>6)];
  118.     *p++ = basis_64[digest[i+2] & 0x3F];
  119.     }
  120.     *p-- = '\0';
  121.     *p-- = '=';
  122.     *p-- = '=';
  123.     return strsave(encodedDigest);
  124. }    
  125.  
  126. char *md5digest(infile, len)
  127. FILE *infile;
  128. long *len;
  129. {
  130.     MD5_CTX context;
  131.     char buf[1000];
  132.     long length = 0;
  133.     int nbytes;
  134.     
  135.     MD5Init(&context);
  136.     while (nbytes = fread(buf, 1, sizeof(buf), infile)) {
  137.     length += nbytes;
  138.     MD5Update(&context, buf, nbytes);
  139.     }
  140.     rewind(infile);
  141.     if (len) *len = length;
  142.     return md5contextTo64(&context);
  143. }
  144.