Watt-32 tcp/ip  2.2 dev-rel.10
nsapaddr.c
Go to the documentation of this file.
1 
5 /* Copyright (c) 1996 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 INTERNET SOFTWARE CONSORTIUM DISCLAIMS
12  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
13  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
14  * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
15  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
16  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
17  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
18  * SOFTWARE.
19  */
20 
21 #include "socket.h"
22 
23 #if defined(USE_BSD_API) || defined(USE_BIND)
24 
25 static __inline char xtob (int c)
26 {
27  return (c - (((c >= '0') && (c <= '9')) ? '0' : '7'));
28 }
29 
30 u_int W32_CALL inet_nsap_addr (const char *ascii, u_char *binary, int maxlen)
31 {
32  u_char c, nib;
33  int len = 0;
34 
35  while ((c = *ascii++) != '\0' && len < maxlen)
36  {
37  if (c == '.' || c == '+' || c == '/')
38  continue;
39 
40  c = toupper (c);
41  if (!isxdigit(c))
42  return (0);
43 
44  nib = xtob (c);
45  if ((c = *ascii++) == '\0')
46  return (0);
47 
48  c = toupper (c);
49  if (!isxdigit(c))
50  return (0);
51 
52  *binary++ = (nib << 4) | xtob(c);
53  len++;
54  }
55  return (u_int)len;
56 }
57 
58 char * W32_CALL inet_nsap_ntoa (int binlen, const u_char *binary, char *ascii)
59 {
60  static char tmpbuf[255*3+1];
61  char *start;
62  int i;
63 
64  if (ascii)
65  start = ascii;
66  else
67  {
68  ascii = tmpbuf;
69  start = tmpbuf;
70  }
71 
72  if (binlen > 255)
73  binlen = 255;
74 
75  for (i = 0; i < binlen; i++)
76  {
77  int nib = *binary >> 4;
78 
79  *ascii++ = nib + (nib < 10 ? '0' : '7');
80  nib = *binary++ & 0x0f;
81  *ascii++ = nib + (nib < 10 ? '0' : '7');
82  if (((i % 2) == 0 && (i + 1) < binlen))
83  *ascii++ = '.';
84  }
85  *ascii = '\0';
86  return (start);
87 }
88 
89 #endif