[BACK]Return to nsap_addr.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / lib / libc / inet

Annotation of src/lib/libc/inet/nsap_addr.c, Revision 1.2

1.1       christos    1: /*     $NetBSD: nsap_addr.c,v 1.1.1.1 2004/05/17 23:44:44 christos Exp $       */
                      2:
                      3: /*
                      4:  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
                      5:  * Copyright (c) 1996-1999 by Internet Software Consortium.
                      6:  *
                      7:  * Permission to use, copy, modify, and distribute this software for any
                      8:  * purpose with or without fee is hereby granted, provided that the above
                      9:  * copyright notice and this permission notice appear in all copies.
                     10:  *
                     11:  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
                     12:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     13:  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
                     14:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     15:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     16:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
                     17:  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     18:  */
                     19:
1.2     ! christos   20: #include <sys/cdefs.h>
1.1       christos   21: #if defined(LIBC_SCCS) && !defined(lint)
1.2     ! christos   22: #if 0
1.1       christos   23: static const char rcsid[] = "Id: nsap_addr.c,v 1.2.206.1 2004/03/09 08:33:33 marka Exp";
1.2     ! christos   24: #else
        !            25: __RCSID("$NetBSD$");
        !            26: #endif
1.1       christos   27: #endif /* LIBC_SCCS and not lint */
                     28:
                     29: #include "port_before.h"
                     30:
1.2     ! christos   31: #include "namespace.h"
1.1       christos   32: #include <sys/types.h>
                     33: #include <sys/param.h>
                     34: #include <sys/socket.h>
                     35:
                     36: #include <netinet/in.h>
                     37: #include <arpa/inet.h>
                     38: #include <arpa/nameser.h>
                     39:
1.2     ! christos   40: #include <assert.h>
1.1       christos   41: #include <ctype.h>
                     42: #include <resolv.h>
                     43:
                     44: #include "port_after.h"
                     45:
1.2     ! christos   46: #ifdef __weak_alias
        !            47: __weak_alias(inet_nsap_addr,_inet_nsap_addr)
        !            48: __weak_alias(inet_nsap_ntoa,_inet_nsap_ntoa)
        !            49: #endif
        !            50:
1.1       christos   51: static char
                     52: xtob(int c) {
                     53:        return (c - (((c >= '0') && (c <= '9')) ? '0' : '7'));
                     54: }
                     55:
                     56: u_int
                     57: inet_nsap_addr(const char *ascii, u_char *binary, int maxlen) {
                     58:        u_char c, nib;
                     59:        u_int len = 0;
                     60:
1.2     ! christos   61:        _DIAGASSERT(ascii != NULL);
        !            62:        _DIAGASSERT(binary != NULL);
        !            63:
1.1       christos   64:        if (ascii[0] != '0' || (ascii[1] != 'x' && ascii[1] != 'X'))
                     65:                return (0);
                     66:        ascii += 2;
                     67:
                     68:        while ((c = *ascii++) != '\0' && len < (u_int)maxlen) {
                     69:                if (c == '.' || c == '+' || c == '/')
                     70:                        continue;
                     71:                if (!isascii(c))
                     72:                        return (0);
                     73:                if (islower(c))
                     74:                        c = toupper(c);
                     75:                if (isxdigit(c)) {
                     76:                        nib = xtob(c);
                     77:                        c = *ascii++;
                     78:                        if (c != '\0') {
                     79:                                c = toupper(c);
                     80:                                if (isxdigit(c)) {
                     81:                                        *binary++ = (nib << 4) | xtob(c);
                     82:                                        len++;
                     83:                                } else
                     84:                                        return (0);
                     85:                        }
                     86:                        else
                     87:                                return (0);
                     88:                }
                     89:                else
                     90:                        return (0);
                     91:        }
                     92:        return (len);
                     93: }
                     94:
                     95: char *
                     96: inet_nsap_ntoa(int binlen, const u_char *binary, char *ascii) {
                     97:        int nib;
                     98:        int i;
                     99:        static char tmpbuf[2+255*3];
                    100:        char *start;
                    101:
1.2     ! christos  102:        _DIAGASSERT(binary != NULL);
        !           103:
1.1       christos  104:        if (ascii)
                    105:                start = ascii;
                    106:        else {
                    107:                ascii = tmpbuf;
                    108:                start = tmpbuf;
                    109:        }
                    110:
                    111:        *ascii++ = '0';
                    112:        *ascii++ = 'x';
                    113:
                    114:        if (binlen > 255)
                    115:                binlen = 255;
                    116:
                    117:        for (i = 0; i < binlen; i++) {
1.2     ! christos  118:                nib = (u_int32_t)*binary >> 4;
1.1       christos  119:                *ascii++ = nib + (nib < 10 ? '0' : '7');
                    120:                nib = *binary++ & 0x0f;
                    121:                *ascii++ = nib + (nib < 10 ? '0' : '7');
                    122:                if (((i % 2) == 0 && (i + 1) < binlen))
                    123:                        *ascii++ = '.';
                    124:        }
                    125:        *ascii = '\0';
                    126:        return (start);
                    127: }

CVSweb <webmaster@jp.NetBSD.org>