home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 3 / 3348 / rad64.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-05-17  |  1.6 KB  |  109 lines

  1. /*
  2.  * Copyright 1989, 1990, John F. Haugh II
  3.  * All rights reserved.
  4.  *
  5.  * Permission is granted to copy and create derivative works for any
  6.  * non-commercial purpose, provided this copyright notice is preserved
  7.  * in all copies of source code, or included in human readable form
  8.  * and conspicuously displayed on all copies of object code or
  9.  * distribution media.
  10.  */
  11.  
  12. #ifndef    lint
  13. static    char    sccsid[] = "@(#)rad64.c    3.2    19:44:25    12/10/90";
  14. #endif
  15.  
  16. /*
  17.  * c64i - convert a radix 64 character to an integer
  18.  */
  19.  
  20. int    c64i (c)
  21. char    c;
  22. {
  23.     if (c == '.')
  24.         return (0);
  25.  
  26.     if (c == '/')
  27.         return (1);
  28.  
  29.     if (c >= '0' && c <= '9')
  30.         return (c - '0' + 2);
  31.  
  32.     if (c >= 'A' && c <= 'Z')
  33.         return (c - 'A' + 12);
  34.  
  35.     if (c >= 'a' && c <= 'z')
  36.         return (c - 'a' + 38);
  37.     else
  38.         return (-1);
  39. }
  40.  
  41. /*
  42.  * i64c - convert an integer to a radix 64 character
  43.  */
  44.  
  45. int    i64c (i)
  46. int    i;
  47. {
  48.     if (i < 0)
  49.         return ('.');
  50.     else if (i > 63)
  51.         return ('z');
  52.  
  53.     if (i == 0)
  54.         return ('.');
  55.  
  56.     if (i == 1)
  57.         return ('/');
  58.  
  59.     if (i >= 2 && i <= 11)
  60.         return ('0' - 2 + i);
  61.  
  62.     if (i >= 12 && i <= 37)
  63.         return ('A' - 12 + i);
  64.  
  65.     if (i >= 38 && i <= 63)
  66.         return ('a' - 38 + i);
  67.  
  68.     return ('\0');
  69. }
  70.  
  71. /*
  72.  * l64a - convert a long to a string of radix 64 characters
  73.  */
  74.  
  75. char    *l64a (l)
  76. long    l;
  77. {
  78.     static    char    buf[8];
  79.     int    i = 0;
  80.  
  81.     if (i < 0L)
  82.         return ((char *) 0);
  83.  
  84.     do {
  85.         buf[i++] = i64c ((int) (l % 64));
  86.         buf[i] = '\0';
  87.     } while (l /= 64L, l > 0 && i < 6);
  88.  
  89.     return (buf);
  90. }
  91.  
  92. /*
  93.  * a64l - convert a radix 64 string to a long integer
  94.  */
  95.  
  96. long    a64l (s)
  97. char    *s;
  98. {
  99.     int    i;
  100.     long    value;
  101.     long    shift = 0;
  102.  
  103.     for (i = 0, value = 0L;i < 6 && *s;s++) {
  104.         value += (c64i (*s) << shift);
  105.         shift += 6;
  106.     }
  107.     return (value);
  108. }
  109.