]> git.proxmox.com Git - mirror_edk2.git/blame - StdLib/BsdSocketLib/nsap_addr.c
UefiCpuPkg/MicrocodeUpdateDxe: Fix coding style issues in INF file
[mirror_edk2.git] / StdLib / BsdSocketLib / nsap_addr.c
CommitLineData
d7ce7006 1/*\r
2 * Copyright (c) 1996, 1998 by Internet Software Consortium.\r
3 *\r
4 * Permission to use, copy, modify, and distribute this software for any\r
5 * purpose with or without fee is hereby granted, provided that the above\r
6 * copyright notice and this permission notice appear in all copies.\r
7 *\r
8 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS\r
9 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES\r
10 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE\r
11 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL\r
12 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR\r
13 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS\r
14 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS\r
15 * SOFTWARE.\r
16 */\r
17\r
d7ce7006 18#include <sys/types.h>\r
19#include <sys/param.h>\r
20#include <sys/socket.h>\r
21#include <netinet/in.h>\r
22#include <arpa/inet.h>\r
23#include <arpa/nameser.h>\r
24#include <ctype.h>\r
25#include <resolv.h>\r
26\r
27static char\r
28xtob(\r
29 register int c\r
30 )\r
31{\r
32 return (char)(c - (((c >= '0') && (c <= '9')) ? '0' : '7'));\r
33}\r
34\r
35u_int\r
36inet_nsap_addr(\r
37 const char *ascii,\r
38 u_char *binary,\r
39 int maxlen\r
40 )\r
41{\r
42 u_char c, nib;\r
43 u_int len = 0;\r
44\r
45 while ((c = *ascii++) != '\0' && len < (u_int)maxlen) {\r
46 if (c == '.' || c == '+' || c == '/')\r
47 continue;\r
48 if (!isascii(c))\r
49 return (0);\r
50 if (islower(c))\r
51 c = (u_char)( toupper(c));\r
52 if (isxdigit(c)) {\r
53 nib = xtob(c);\r
54 c = *ascii++;\r
55 if (c != '\0') {\r
56 c = (u_char)( toupper(c));\r
57 if (isxdigit(c)) {\r
58 *binary++ = (nib << 4) | xtob(c);\r
59 len++;\r
60 } else\r
61 return (0);\r
62 }\r
63 else\r
64 return (0);\r
65 }\r
66 else\r
67 return (0);\r
68 }\r
69 return (len);\r
70}\r
71\r
72char *\r
73inet_nsap_ntoa(\r
74 int binlen,\r
75 register const u_char *binary,\r
76 register char *ascii\r
77 )\r
78{\r
79 register int nib;\r
80 int i;\r
81 static char tmpbuf[255*3];\r
82 char *start;\r
83\r
84 if (ascii)\r
85 start = ascii;\r
86 else {\r
87 ascii = tmpbuf;\r
88 start = tmpbuf;\r
89 }\r
90\r
91 if (binlen > 255)\r
92 binlen = 255;\r
93\r
94 for (i = 0; i < binlen; i++) {\r
95 nib = *binary >> 4;\r
96 *ascii++ = (char)( nib + (nib < 10 ? '0' : '7'));\r
97 nib = *binary++ & 0x0f;\r
98 *ascii++ = (char)( nib + (nib < 10 ? '0' : '7'));\r
99 if (((i % 2) == 0 && (i + 1) < binlen))\r
100 *ascii++ = '.';\r
101 }\r
102 *ascii = '\0';\r
103 return (start);\r
104}\r