/src/open62541/deps/musl_inet_pton.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* musl as a whole is licensed under the following standard MIT license: |
2 | | * |
3 | | * ---------------------------------------------------------------------- |
4 | | * Copyright © 2005-2020 Rich Felker, et al. |
5 | | * |
6 | | * Permission is hereby granted, free of charge, to any person obtaining |
7 | | * a copy of this software and associated documentation files (the |
8 | | * "Software"), to deal in the Software without restriction, including |
9 | | * without limitation the rights to use, copy, modify, merge, publish, |
10 | | * distribute, sublicense, and/or sell copies of the Software, and to |
11 | | * permit persons to whom the Software is furnished to do so, subject to |
12 | | * the following conditions: |
13 | | * |
14 | | * The above copyright notice and this permission notice shall be |
15 | | * included in all copies or substantial portions of the Software. |
16 | | * |
17 | | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
18 | | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
19 | | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
20 | | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
21 | | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
22 | | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
23 | | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
24 | | *---------------------------------------------------------------------- |
25 | | */ |
26 | | |
27 | | #include "musl_inet_pton.h" |
28 | | #include <ctype.h> |
29 | | #include <errno.h> |
30 | | #include <string.h> |
31 | | |
32 | | static int hexval(unsigned c) |
33 | 0 | { |
34 | 0 | if (c-'0'<10) return c-'0'; |
35 | 0 | c |= 32; |
36 | 0 | if (c-'a'<6) return c-'a'+10; |
37 | 0 | return -1; |
38 | 0 | } |
39 | | |
40 | | int musl_inet_pton(int af, const char * UA_RESTRICT s, void * UA_RESTRICT a0) |
41 | 0 | { |
42 | 0 | uint16_t ip[8]; |
43 | 0 | unsigned char *a = (unsigned char *)a0; |
44 | 0 | int i, j, v, d, brk=-1, need_v4=0; |
45 | |
|
46 | 0 | if (af==AF_INET) { |
47 | 0 | for (i=0; i<4; i++) { |
48 | 0 | for (v=j=0; j<3 && isdigit(s[j]); j++) |
49 | 0 | v = 10*v + s[j]-'0'; |
50 | 0 | if (j==0 || (j>1 && s[0]=='0') || v>255) return 0; |
51 | 0 | a[i] = v; |
52 | 0 | if (s[j]==0 && i==3) return 1; |
53 | 0 | if (s[j]!='.') return 0; |
54 | 0 | s += j+1; |
55 | 0 | } |
56 | 0 | return 0; |
57 | 0 | } else if (af!=AF_INET6) { |
58 | 0 | errno = EAFNOSUPPORT; |
59 | 0 | return -1; |
60 | 0 | } |
61 | | |
62 | 0 | if (*s==':' && *++s!=':') return 0; |
63 | | |
64 | 0 | for (i=0; ; i++) { |
65 | 0 | if (s[0]==':' && brk<0) { |
66 | 0 | brk=i; |
67 | 0 | ip[i&7]=0; |
68 | 0 | if (!*++s) break; |
69 | 0 | if (i==7) return 0; |
70 | 0 | continue; |
71 | 0 | } |
72 | 0 | for (v=j=0; j<4 && (d=hexval(s[j]))>=0; j++) |
73 | 0 | v=16*v+d; |
74 | 0 | if (j==0) return 0; |
75 | 0 | ip[i&7] = v; |
76 | 0 | if (!s[j] && (brk>=0 || i==7)) break; |
77 | 0 | if (i==7) return 0; |
78 | 0 | if (s[j]!=':') { |
79 | 0 | if (s[j]!='.' || (i<6 && brk<0)) return 0; |
80 | 0 | need_v4=1; |
81 | 0 | i++; |
82 | 0 | ip[i&7]=0; |
83 | 0 | break; |
84 | 0 | } |
85 | 0 | s += j+1; |
86 | 0 | } |
87 | 0 | if (brk>=0) { |
88 | 0 | memmove(ip+brk+7-i, ip+brk, 2*(i+1-brk)); |
89 | 0 | for (j=0; j<7-i; j++) ip[brk+j] = 0; |
90 | 0 | } |
91 | 0 | for (j=0; j<8; j++) { |
92 | 0 | *a++ = ip[j]>>8; |
93 | 0 | *a++ = (unsigned char)ip[j]; |
94 | 0 | } |
95 | 0 | if (need_v4 && musl_inet_pton(AF_INET, (char *)(uintptr_t)s, a-4) <= 0) return 0; |
96 | 0 | return 1; |
97 | 0 | } |