Home | History | Annotate | Download | only in inet
      1 /*	$NetBSD: nsap_addr.c,v 1.2 2004/05/20 23:12:33 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 
     20 #include <sys/cdefs.h>
     21 #if defined(LIBC_SCCS) && !defined(lint)
     22 #if 0
     23 static const char rcsid[] = "Id: nsap_addr.c,v 1.2.206.1 2004/03/09 08:33:33 marka Exp";
     24 #else
     25 __RCSID("$NetBSD: nsap_addr.c,v 1.2 2004/05/20 23:12:33 christos Exp $");
     26 #endif
     27 #endif /* LIBC_SCCS and not lint */
     28 
     29 #include <sys/types.h>
     30 #include <sys/param.h>
     31 #include <sys/socket.h>
     32 
     33 #include <netinet/in.h>
     34 #include <arpa/inet.h>
     35 #include "arpa_nameser.h"
     36 
     37 #include <assert.h>
     38 #include <ctype.h>
     39 #ifdef ANDROID_CHANGES
     40 #include "resolv_private.h"
     41 #else
     42 #include <resolv.h>
     43 #endif
     44 
     45 static char
     46 xtob(int c) {
     47 	return (c - (((c >= '0') && (c <= '9')) ? '0' : '7'));
     48 }
     49 
     50 u_int
     51 inet_nsap_addr(const char *ascii, u_char *binary, int maxlen) {
     52 	u_char c, nib;
     53 	u_int len = 0;
     54 
     55 	assert(ascii != NULL);
     56 	assert(binary != NULL);
     57 
     58 	if (ascii[0] != '0' || (ascii[1] != 'x' && ascii[1] != 'X'))
     59 		return (0);
     60 	ascii += 2;
     61 
     62 	while ((c = *ascii++) != '\0' && len < (u_int)maxlen) {
     63 		if (c == '.' || c == '+' || c == '/')
     64 			continue;
     65 		if (!isascii(c))
     66 			return (0);
     67 		if (islower(c))
     68 			c = toupper(c);
     69 		if (isxdigit(c)) {
     70 			nib = xtob(c);
     71 			c = *ascii++;
     72 			if (c != '\0') {
     73 				c = toupper(c);
     74 				if (isxdigit(c)) {
     75 					*binary++ = (nib << 4) | xtob(c);
     76 					len++;
     77 				} else
     78 					return (0);
     79 			}
     80 			else
     81 				return (0);
     82 		}
     83 		else
     84 			return (0);
     85 	}
     86 	return (len);
     87 }
     88 
     89 char *
     90 inet_nsap_ntoa(int binlen, const u_char *binary, char *ascii) {
     91 	int nib;
     92 	int i;
     93 	static char tmpbuf[2+255*3];
     94 	char *start;
     95 
     96 	assert(binary != NULL);
     97 
     98 	if (ascii)
     99 		start = ascii;
    100 	else {
    101 		ascii = tmpbuf;
    102 		start = tmpbuf;
    103 	}
    104 
    105 	*ascii++ = '0';
    106 	*ascii++ = 'x';
    107 
    108 	if (binlen > 255)
    109 		binlen = 255;
    110 
    111 	for (i = 0; i < binlen; i++) {
    112 		nib = (u_int32_t)*binary >> 4;
    113 		*ascii++ = nib + (nib < 10 ? '0' : '7');
    114 		nib = *binary++ & 0x0f;
    115 		*ascii++ = nib + (nib < 10 ? '0' : '7');
    116 		if (((i % 2) == 0 && (i + 1) < binlen))
    117 			*ascii++ = '.';
    118 	}
    119 	*ascii = '\0';
    120 	return (start);
    121 }
    122