home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2083 / rad64.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-28  |  1.2 KB  |  90 lines

  1. /*
  2.  * Copyright 1989, 1990, John F. Haugh II
  3.  * All rights reserved.
  4.  *
  5.  * Use, duplication, and disclosure prohibited without
  6.  * the express written permission of the author.
  7.  */
  8.  
  9. #ifndef    lint
  10. static    char    _sccsid[] = "@(#)rad64.c    3.1    23:00:35    11/11/90";
  11. #endif
  12.  
  13. int    c64i (c)
  14. char    c;
  15. {
  16.     if (c == '.')
  17.         return (0);
  18.  
  19.     if (c == '/')
  20.         return (1);
  21.  
  22.     if (c >= '0' && c <= '9')
  23.         return (c - '0' + 2);
  24.  
  25.     if (c >= 'A' && c <= 'Z')
  26.         return (c - 'A' + 12);
  27.  
  28.     if (c >= 'a' && c <= 'z')
  29.         return (c - 'a' + 38);
  30.     else
  31.         return (-1);
  32. }
  33.  
  34. int    i64c (i)
  35. int    i;
  36. {
  37.     if (i < 0)
  38.         return ('.');
  39.     else if (i > 63)
  40.         return ('z');
  41.  
  42.     if (i == 0)
  43.         return ('.');
  44.  
  45.     if (i == 1)
  46.         return ('/');
  47.  
  48.     if (i >= 2 && i <= 11)
  49.         return ('0' - 2 + i);
  50.  
  51.     if (i >= 12 && i <= 37)
  52.         return ('A' - 12 + i);
  53.  
  54.     if (i >= 38 && i <= 63)
  55.         return ('a' - 38 + i);
  56.  
  57.     return ('\0');
  58. }
  59.  
  60. char    *l64a (l)
  61. long    l;
  62. {
  63.     static    char    buf[8];
  64.     int    i = 0;
  65.  
  66.     if (i < 0L)
  67.         return ((char *) 0);
  68.  
  69.     do {
  70.         buf[i++] = i64c ((int) (l % 64));
  71.         buf[i] = '\0';
  72.     } while (l /= 64L, l > 0 && i < 6);
  73.  
  74.     return (buf);
  75. }
  76.  
  77. long    a64l (s)
  78. char    *s;
  79. {
  80.     int    i;
  81.     long    value;
  82.     long    shift = 0;
  83.  
  84.     for (i = 0, value = 0L;i < 6 && *s;s++) {
  85.         value += (c64i (*s) << shift);
  86.         shift += 6;
  87.     }
  88.     return (value);
  89. }
  90.