home *** CD-ROM | disk | FTP | other *** search
/ Serving the Web / ServingTheWeb1995.disc1of1.iso / linux / slacksrce / d / libc / libc-4.6 / libc-4 / libc-linux / inet / nsap_addr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-28  |  1.6 KB  |  91 lines

  1. #if defined(LIBC_SCCS) && !defined(lint)
  2. static char rcsid[] = "$Id: nsap_addr.c,v 1.2 1994/06/26 04:00:18 vixie Exp $";
  3. #endif /* LIBC_SCCS and not lint */
  4.  
  5. #include "inetprivate.h"
  6.  
  7. #if !defined(isxdigit)    /* XXX - could be a function */
  8. static int
  9. isxdigit(c)
  10.     register int c;
  11. {
  12.     return ((c >= '0') && (c <= '9')) || ((c >= 'A') && (c <= 'F'));
  13. }
  14. #endif
  15.  
  16. static char
  17. xtob(c)
  18.     register int c;
  19. {
  20.     return (c - (((c >= '0') && (c <= '9')) ? '0' : '7'));
  21. }
  22.  
  23. u_int
  24. inet_nsap_addr(ascii, binary, maxlen)
  25.     const char *ascii;
  26.     u_char *binary;
  27.     int maxlen;
  28. {
  29.     register u_char c, nib;
  30.     u_char *start = binary;
  31.     u_int len = 0;
  32.  
  33.     while ((c = *ascii++) != '\0' && len < maxlen) {
  34.         if (c == '.' || c == '+' || c == '/')
  35.             continue;
  36.         if (!isascii(c))
  37.             return (0);
  38.         if (islower(c))
  39.             c = toupper(c);
  40.         if (isxdigit(c)) {
  41.             nib = xtob(c);
  42.             if (c = *ascii++) {
  43.                 c = toupper(c);
  44.                 if (isxdigit(c)) {
  45.                     *binary++ = (nib << 4) | xtob(c);
  46.                     len++;
  47.                 } else
  48.                     return (0);
  49.             }
  50.             else
  51.                 return (0);
  52.         }
  53.         else
  54.             return (0);
  55.     }
  56.     return (len);
  57. }
  58.  
  59. char *
  60. inet_nsap_ntoa(binlen, binary, ascii)
  61.     int binlen;
  62.     register const u_char *binary;
  63.     register char *ascii;
  64. {
  65.     register int nib;
  66.     int i;
  67.     static char tmpbuf[255*3];
  68.     char *start;
  69.  
  70.     if (ascii)
  71.         start = ascii;
  72.     else {
  73.         ascii = tmpbuf;
  74.         start = tmpbuf;
  75.     }
  76.  
  77.     if (binlen > 255)
  78.         binlen = 255;
  79.  
  80.     for (i = 0; i < binlen; i++) {
  81.         nib = *binary >> 4;
  82.         *ascii++ = nib + (nib < 10 ? '0' : '7');
  83.         nib = *binary++ & 0x0f;
  84.         *ascii++ = nib + (nib < 10 ? '0' : '7');
  85.         if (((i % 2) == 0 && (i + 1) < binlen))
  86.             *ascii++ = '.';
  87.     }
  88.     *ascii = '\0';
  89.     return (start);
  90. }
  91.