home *** CD-ROM | disk | FTP | other *** search
/ Atari FTP / ATARI_FTP_0693.zip / ATARI_FTP_0693 / Mint / mntlib32.zoo / a64l.c < prev    next >
C/C++ Source or Header  |  1993-02-13  |  4KB  |  162 lines

  1. /****************************************************************/
  2. /* Module name:   a64l.c                                        */
  3. /* Library name:  mintlibs for Atari ST                         */
  4. /* Author:        Hildo Biersma (boender@dutiws.twi.tudelft.nl) */
  5. /* Date:          January 11, 1993                              */
  6. /* Revision:      1 (first attempt)                             */
  7. /*                2 ++entropy Made compatible with K&R compilers*/
  8. /****************************************************************/
  9.  
  10. /* FIXME: maybe use ERANGE or EDOM instead of EBADARG */
  11.  
  12. /*
  13. NAME
  14.     a64l, l64a - convert between long integer and base-64 ASCII string
  15.  
  16. SYNOPSIS
  17.     #include <support.h>
  18.     long a64l(const char *s);
  19.     char *l64a(long l);
  20.  
  21. DESCRIPTION
  22.     These functions are used to maintain numbers stored in base-64
  23.     ASCII characters. This is a notation by which long integers
  24.     can be represented by up to six characters; each character
  25.     represents a "digit" in a radix-64 notation.
  26.     
  27.     The characters used to represent "digits" are . for 0, / for 1,
  28.     0 through 9 for 2-11, A through Z for 12-37, and a through z
  29.     for 38-63.
  30.     
  31.     a64l takes a pointer to a null-terminated base-64 representation
  32.     and returns a corresponding long value. If the string pointed to
  33.     by s contains more than six characters, a64l will use the first
  34.     six. a64l scans the character string from left to right, decoding
  35.     each character as a 6 bit radix-64 number. If the string contains
  36.     illegal characters, -1 is returned and errno is set to EBADARG.
  37.     
  38.     l64a takes a long argument and returns a pointer to the
  39.     corresponding base-64 representation. If the argument is 0, a64l
  40.     returns a pointer to a null string. If the argument is smaller
  41.     than zero, a pointer to a null string is returned and errno is
  42.     set to EBADARG.
  43.  
  44. CAVEATS
  45.     The value returned by l64a is a pointer into a static buffer,
  46.     the contents of which are overwritten by each call.
  47.     
  48.     The value returned by a64l may be incorrect if the value
  49.     is too large; for that reason, only strings that resulted
  50.     from a call to l64a should be used to call a64l.
  51.  
  52.     Maybe these calls should unsigned long values, but longs are
  53.     used here to retain compatibility with UN*X System V.
  54.  
  55. AUTHOR
  56.     Hildo Biersma, with the help of a UN*X System V man page.
  57. */
  58.  
  59. #include <errno.h>
  60. extern int errno;
  61.  
  62. #ifndef _COMPILER_H
  63. #include <compiler.h>
  64. #endif
  65.  
  66. /* Local function prototypes */
  67. static int a64i __PROTO((char c));  /* base-64 char to int, -1 on error */
  68. static char i64a __PROTO((int i));  /* integer to base-64 char, 0x7F on error */
  69.  
  70. /* base-64 char to int, -1 on error */
  71. static int a64i(c)
  72.   char c;
  73. {
  74.   int retval = c;
  75.  
  76.   if ((c < '.') || (c > 'z'))
  77.   {
  78.     errno = EBADARG;
  79.     return(-1);
  80.   }
  81.   retval -= '.';
  82.   if (c > '9')
  83.     retval -= 'A' - '9' - 1;
  84.   if (c > 'Z')
  85.     retval -= 'a' - 'Z' - 1;
  86.   if (retval > 63)
  87.   {
  88.     errno = EBADARG;
  89.     return(-1);
  90.   }
  91.   return(retval);
  92. } /* End of a64i() */
  93.  
  94. /* base-64 string to long */
  95. long a64l(s)
  96.   const char *s;
  97. {
  98.   long retval = 0;
  99.   int counter = 0;
  100.   const char *ptr = s;
  101.  
  102.   while ((counter++ < 6) && (*ptr != 0x00))
  103.   {
  104.     int val;
  105.     
  106.     if ((val = a64i(*ptr++)) == -1)
  107.       return(-1); /* errno was set by a64i() */
  108.     retval <<= 6;
  109.     retval += val;
  110.   }
  111.   return(retval);
  112. } /* End of a64l() */
  113.  
  114. /* integer to base-64 char, 0x7F on error */
  115. static char i64a(i)
  116.   int i;
  117. {
  118.   char retval = (char)i;
  119.   
  120.   if ((i < 0) || (i > 63))
  121.   {
  122.     errno = EBADARG;
  123.     return(0x7F);
  124.   }
  125.   retval += '.';
  126.   if (i > 11)
  127.     retval += 'A' - '9' - 1;
  128.   if (i > 37)
  129.     retval += 'a' - 'Z' - 1;
  130.   return(retval);
  131. } /* End of i64a() */
  132.  
  133. /* long to base-64 string */
  134. char *l64a(l)
  135.   long l;
  136. {
  137.   static char retval[7];
  138.   char buffer[7], *ptr1 = buffer, *ptr2 = retval;
  139.   int counter = 0;
  140.  
  141.   if (l < 0)
  142.   {
  143.     errno = EBADARG;
  144.     return("");
  145.   }
  146.   if (l == 0)
  147.     return("");
  148.   while ((counter++ < 6) && (l > 0))
  149.   {
  150.     char val;
  151.     
  152.     if ((val = i64a((char)(l & 0x3F))) == 0x7F)
  153.       return(""); /* errno was set by i64a() */
  154.     *ptr1++ = val;
  155.     l >>= 6;
  156.   }
  157.   while (ptr1 > buffer)
  158.     *ptr2++ = *(--ptr1);
  159.   *ptr2 = 0x00;
  160.   return(retval);
  161. } /* End of l64a() */
  162.