source: rtems-libbsd/freebsd/lib/libc/inet/nsap_addr.c

6-freebsd-12
Last change on this file was 18fa92c, checked in by Sebastian Huber <sebastian.huber@…>, on 08/20/18 at 13:53:03

Update to FreeBSD head 2018-02-01

Git mirror commit d079ae0442af8fa3cfd6d7ede190d04e64a2c0d4.

Update #3472.

  • Property mode set to 100644
File size: 2.9 KB
Line 
1#include <machine/rtems-bsd-user-space.h>
2
3/*-
4 * SPDX-License-Identifier: ISC
5 *
6 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
7 * Copyright (c) 1996-1999 by Internet Software Consortium.
8 *
9 * Permission to use, copy, modify, and distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice appear in all copies.
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
19 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 */
21
22#if defined(LIBC_SCCS) && !defined(lint)
23static const char rcsid[] = "$Id: nsap_addr.c,v 1.5 2005/07/28 06:51:48 marka Exp $";
24#endif /* LIBC_SCCS and not lint */
25#include <sys/cdefs.h>
26__FBSDID("$FreeBSD$");
27
28#include "port_before.h"
29
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 <ctype.h>
38#include <resolv.h>
39#include <resolv_mt.h>
40
41#include "port_after.h"
42
43static char
44xtob(int c) {
45        return (c - (((c >= '0') && (c <= '9')) ? '0' : '7'));
46}
47
48u_int
49inet_nsap_addr(const char *ascii, u_char *binary, int maxlen) {
50        u_char c, nib;
51        u_int len = 0;
52
53        if (ascii[0] != '0' || (ascii[1] != 'x' && ascii[1] != 'X'))
54                return (0);
55        ascii += 2;
56
57        while ((c = *ascii++) != '\0' && len < (u_int)maxlen) {
58                if (c == '.' || c == '+' || c == '/')
59                        continue;
60                if (!isascii(c))
61                        return (0);
62                if (islower(c))
63                        c = toupper(c);
64                if (isxdigit(c)) {
65                        nib = xtob(c);
66                        c = *ascii++;
67                        if (c != '\0') {
68                                c = toupper(c);
69                                if (isxdigit(c)) {
70                                        *binary++ = (nib << 4) | xtob(c);
71                                        len++;
72                                } else
73                                        return (0);
74                        }
75                        else
76                                return (0);
77                }
78                else
79                        return (0);
80        }
81        return (len);
82}
83
84char *
85inet_nsap_ntoa(int binlen, const u_char *binary, char *ascii) {
86        int nib;
87        int i;
88        char *tmpbuf = inet_nsap_ntoa_tmpbuf;
89        char *start;
90
91        if (ascii)
92                start = ascii;
93        else {
94                ascii = tmpbuf;
95                start = tmpbuf;
96        }
97
98        *ascii++ = '0';
99        *ascii++ = 'x';
100
101        if (binlen > 255)
102                binlen = 255;
103
104        for (i = 0; i < binlen; i++) {
105                nib = *binary >> 4;
106                *ascii++ = nib + (nib < 10 ? '0' : '7');
107                nib = *binary++ & 0x0f;
108                *ascii++ = nib + (nib < 10 ? '0' : '7');
109                if (((i % 2) == 0 && (i + 1) < binlen))
110                        *ascii++ = '.';
111        }
112        *ascii = '\0';
113        return (start);
114}
115
116#ifndef __rtems__
117/*
118 * Weak aliases for applications that use certain private entry points,
119 * and fail to include <arpa/inet.h>.
120 */
121#undef inet_nsap_addr
122__weak_reference(__inet_nsap_addr, inet_nsap_addr);
123#undef inet_nsap_ntoa
124__weak_reference(__inet_nsap_ntoa, inet_nsap_ntoa);
125#endif /* __rtems__ */
126
127/*! \file */
Note: See TracBrowser for help on using the repository browser.