/src/php-src/ext/uri/uriparser/src/UriSetHostIp6.c
Line | Count | Source |
1 | | /* |
2 | | * uriparser - RFC 3986 URI parsing library |
3 | | * |
4 | | * Copyright (C) 2025, Sebastian Pipping <sebastian@pipping.org> |
5 | | * All rights reserved. |
6 | | * |
7 | | * Redistribution and use in source and binary forms, with or without |
8 | | * modification, are permitted provided that the following conditions |
9 | | * are met: |
10 | | * |
11 | | * 1. Redistributions of source code must retain the above |
12 | | * copyright notice, this list of conditions and the following |
13 | | * disclaimer. |
14 | | * |
15 | | * 2. Redistributions in binary form must reproduce the above |
16 | | * copyright notice, this list of conditions and the following |
17 | | * disclaimer in the documentation and/or other materials |
18 | | * provided with the distribution. |
19 | | * |
20 | | * 3. Neither the name of the copyright holder nor the names of |
21 | | * its contributors may be used to endorse or promote products |
22 | | * derived from this software without specific prior written |
23 | | * permission. |
24 | | * |
25 | | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
26 | | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
27 | | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
28 | | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
29 | | * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
30 | | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
31 | | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
32 | | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
33 | | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
34 | | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
35 | | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
36 | | * OF THE POSSIBILITY OF SUCH DAMAGE. |
37 | | */ |
38 | | |
39 | | /* What encodings are enabled? */ |
40 | | #include <uriparser/UriDefsConfig.h> |
41 | | #if (!defined(URI_PASS_ANSI) && !defined(URI_PASS_UNICODE)) |
42 | | /* Include SELF twice */ |
43 | | # ifdef URI_ENABLE_ANSI |
44 | | # define URI_PASS_ANSI 1 |
45 | | # include "UriSetHostIp6.c" |
46 | | # undef URI_PASS_ANSI |
47 | | # endif |
48 | | # ifdef URI_ENABLE_UNICODE |
49 | | # define URI_PASS_UNICODE 1 |
50 | | # include "UriSetHostIp6.c" |
51 | | # undef URI_PASS_UNICODE |
52 | | # endif |
53 | | #else |
54 | | # ifdef URI_PASS_ANSI |
55 | | # include <uriparser/UriDefsAnsi.h> |
56 | | # else |
57 | | # include <uriparser/UriDefsUnicode.h> |
58 | | # include <wchar.h> |
59 | | # endif |
60 | | |
61 | | # ifndef URI_DOXYGEN |
62 | | # include <uriparser/Uri.h> |
63 | | # include "UriMemory.h" |
64 | | # include "UriSetHostBase.h" |
65 | | # include "UriSetHostCommon.h" |
66 | | # endif |
67 | | |
68 | | # include <assert.h> |
69 | | # include <string.h> /* for memcpy */ |
70 | | |
71 | | # define URI_MAX_IP6_LEN \ |
72 | 0 | (8 * 4 + 7 * 1) /* i.e. 8 full quads plus 7 colon separators \ |
73 | | */ |
74 | | |
75 | | int URI_FUNC(ParseIpSixAddressMm)(UriIp6 * output, const URI_CHAR * first, |
76 | 0 | const URI_CHAR * afterLast, UriMemoryManager * memory) { |
77 | | /* NOTE: output is allowed to be NULL */ |
78 | 0 | if ((first == NULL) || (afterLast == NULL)) { |
79 | 0 | return URI_ERROR_NULL; |
80 | 0 | } |
81 | | |
82 | 0 | URI_CHECK_MEMORY_MANAGER(memory); /* may return */ |
83 | | |
84 | | /* Are we dealing with potential IPvFuture input? */ |
85 | 0 | if (first < afterLast) { |
86 | 0 | switch (first[0]) { |
87 | 0 | case _UT('v'): |
88 | 0 | case _UT('V'): |
89 | 0 | return URI_ERROR_SYNTAX; |
90 | 0 | default: |
91 | 0 | break; |
92 | 0 | } |
93 | 0 | } |
94 | | |
95 | | /* Are we dealing with IPv6 input? */ |
96 | | /* Assemble "//[..]" input wrap for upcoming parse as a URI |
97 | | * NOTE: If the input contains closing "]" on its own, the resulting |
98 | | * string will not be valid URI syntax, and hence there is |
99 | | * no risk of false positives from "bracket injection". */ |
100 | 0 | URI_CHAR candidate[3 + URI_MAX_IP6_LEN + 1 + 1] = _UT("//["); |
101 | 0 | const size_t inputLenChars = (afterLast - first); |
102 | | |
103 | | /* Detect overflow */ |
104 | 0 | if (inputLenChars > URI_MAX_IP6_LEN) { |
105 | 0 | return URI_ERROR_SYNTAX; |
106 | 0 | } |
107 | | |
108 | 0 | memcpy(candidate + 3, first, inputLenChars * sizeof(URI_CHAR)); |
109 | 0 | memcpy(candidate + 3 + inputLenChars, _UT("]"), |
110 | 0 | 2 * sizeof(URI_CHAR)); /* includes zero terminator */ |
111 | | |
112 | | /* Parse as an RFC 3986 URI */ |
113 | 0 | const size_t candidateLenChars = 3 + inputLenChars + 1; |
114 | 0 | URI_TYPE(Uri) uri; |
115 | 0 | const int res = URI_FUNC(ParseSingleUriExMm)( |
116 | 0 | &uri, candidate, candidate + candidateLenChars, NULL, memory); |
117 | |
|
118 | 0 | assert((res == URI_SUCCESS) || (res == URI_ERROR_SYNTAX) |
119 | 0 | || (res == URI_ERROR_MALLOC)); |
120 | |
|
121 | 0 | if (res == URI_SUCCESS) { |
122 | 0 | assert(uri.hostData.ip6 != NULL); |
123 | |
|
124 | 0 | if (output != NULL) { |
125 | 0 | memcpy(output->data, uri.hostData.ip6->data, sizeof(output->data)); |
126 | 0 | } |
127 | |
|
128 | 0 | URI_FUNC(FreeUriMembersMm)(&uri, memory); |
129 | 0 | } |
130 | |
|
131 | 0 | return res; |
132 | 0 | } Unexecuted instantiation: uriParseIpSixAddressMmA Unexecuted instantiation: uriParseIpSixAddressMmW |
133 | | |
134 | | int URI_FUNC(ParseIpSixAddress)(UriIp6 * output, const URI_CHAR * first, |
135 | 0 | const URI_CHAR * afterLast) { |
136 | 0 | return URI_FUNC(ParseIpSixAddressMm)(output, first, afterLast, NULL); |
137 | 0 | } Unexecuted instantiation: uriParseIpSixAddressA Unexecuted instantiation: uriParseIpSixAddressW |
138 | | |
139 | | int URI_FUNC(IsWellFormedHostIp6Mm)(const URI_CHAR * first, const URI_CHAR * afterLast, |
140 | 0 | UriMemoryManager * memory) { |
141 | 0 | return URI_FUNC(ParseIpSixAddressMm)(NULL, first, afterLast, memory); |
142 | 0 | } Unexecuted instantiation: uriIsWellFormedHostIp6MmA Unexecuted instantiation: uriIsWellFormedHostIp6MmW |
143 | | |
144 | 0 | int URI_FUNC(IsWellFormedHostIp6)(const URI_CHAR * first, const URI_CHAR * afterLast) { |
145 | 0 | return URI_FUNC(IsWellFormedHostIp6Mm)(first, afterLast, NULL); |
146 | 0 | } Unexecuted instantiation: uriIsWellFormedHostIp6A Unexecuted instantiation: uriIsWellFormedHostIp6W |
147 | | |
148 | | int URI_FUNC(SetHostIp6Mm)(URI_TYPE(Uri) * uri, const URI_CHAR * first, |
149 | 0 | const URI_CHAR * afterLast, UriMemoryManager * memory) { |
150 | 0 | return URI_FUNC(InternalSetHostMm)(uri, URI_HOST_TYPE_IP6, first, afterLast, memory); |
151 | 0 | } Unexecuted instantiation: uriSetHostIp6MmA Unexecuted instantiation: uriSetHostIp6MmW |
152 | | |
153 | | int URI_FUNC(SetHostIp6)(URI_TYPE(Uri) * uri, const URI_CHAR * first, |
154 | 0 | const URI_CHAR * afterLast) { |
155 | 0 | return URI_FUNC(SetHostIp6Mm)(uri, first, afterLast, NULL); |
156 | 0 | } Unexecuted instantiation: uriSetHostIp6A Unexecuted instantiation: uriSetHostIp6W |
157 | | |
158 | | #endif |