/src/php-src/ext/uri/uriparser/src/UriSetHostIpFuture.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 "UriSetHostIpFuture.c" |
46 | | # undef URI_PASS_ANSI |
47 | | # endif |
48 | | # ifdef URI_ENABLE_UNICODE |
49 | | # define URI_PASS_UNICODE 1 |
50 | | # include "UriSetHostIpFuture.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 | | |
62 | | |
63 | | #ifndef URI_DOXYGEN |
64 | | # include <uriparser/Uri.h> |
65 | | # include "UriMemory.h" |
66 | | # include "UriSetHostBase.h" |
67 | | # include "UriSetHostCommon.h" |
68 | | #endif |
69 | | |
70 | | |
71 | | |
72 | | #include <assert.h> |
73 | | #include <string.h> /* for memcpy */ |
74 | | |
75 | | |
76 | | |
77 | 0 | int URI_FUNC(IsWellFormedHostIpFutureMm)(const URI_CHAR * first, const URI_CHAR * afterLast, UriMemoryManager * memory) { |
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 IPv6 input? */ |
85 | 0 | if (first < afterLast) { |
86 | 0 | switch (first[0]) { |
87 | 0 | case _UT('v'): |
88 | 0 | case _UT('V'): |
89 | 0 | break; |
90 | 0 | default: |
91 | 0 | return URI_ERROR_SYNTAX; |
92 | 0 | } |
93 | 0 | } |
94 | | |
95 | | /* Are we dealing with IPvFuture input? */ |
96 | 0 | { |
97 | | /* Assemble "//[..]" input wrap for upcoming parse as a URI |
98 | | * NOTE: If the input contains closing "]" on its own, the resulting |
99 | | * string will not be valid URI syntax, and hence there is |
100 | | * no risk of false positives from "bracket injection". */ |
101 | 0 | const size_t inputLenChars = (afterLast - first); |
102 | 0 | const size_t MAX_SIZE_T = (size_t)-1; |
103 | | |
104 | | /* Detect overflow */ |
105 | 0 | if (MAX_SIZE_T - inputLenChars < 3 + 1 + 1) { |
106 | 0 | return URI_ERROR_MALLOC; |
107 | 0 | } |
108 | | |
109 | 0 | { |
110 | 0 | const size_t candidateLenChars = 3 + inputLenChars + 1; |
111 | | |
112 | | /* Detect overflow */ |
113 | 0 | if (MAX_SIZE_T / sizeof(URI_CHAR) < candidateLenChars + 1) { |
114 | 0 | return URI_ERROR_MALLOC; |
115 | 0 | } |
116 | | |
117 | 0 | { |
118 | 0 | URI_CHAR * const candidate = memory->malloc(memory, (candidateLenChars + 1) * sizeof(URI_CHAR)); |
119 | |
|
120 | 0 | if (candidate == NULL) { |
121 | 0 | return URI_ERROR_MALLOC; |
122 | 0 | } |
123 | | |
124 | 0 | memcpy(candidate, _UT("//["), 3 * sizeof(URI_CHAR)); |
125 | 0 | memcpy(candidate + 3, first, inputLenChars * sizeof(URI_CHAR)); |
126 | 0 | memcpy(candidate + 3 + inputLenChars, _UT("]"), 2 * sizeof(URI_CHAR)); /* includes zero terminator */ |
127 | | |
128 | | /* Parse as an RFC 3986 URI */ |
129 | 0 | { |
130 | 0 | URI_TYPE(Uri) uri; |
131 | 0 | const int res = URI_FUNC(ParseSingleUriExMm)(&uri, candidate, candidate + candidateLenChars, NULL, memory); |
132 | |
|
133 | 0 | assert((res == URI_SUCCESS) || (res == URI_ERROR_SYNTAX) || (res == URI_ERROR_MALLOC)); |
134 | |
|
135 | 0 | if (res == URI_SUCCESS) { |
136 | 0 | assert(uri.hostData.ipFuture.first != NULL); |
137 | 0 | URI_FUNC(FreeUriMembersMm)(&uri, memory); |
138 | 0 | } |
139 | |
|
140 | 0 | memory->free(memory, candidate); |
141 | |
|
142 | 0 | return res; |
143 | 0 | } |
144 | 0 | } |
145 | 0 | } |
146 | 0 | } |
147 | 0 | } Unexecuted instantiation: uriIsWellFormedHostIpFutureMmA Unexecuted instantiation: uriIsWellFormedHostIpFutureMmW |
148 | | |
149 | | |
150 | | |
151 | 0 | int URI_FUNC(IsWellFormedHostIpFuture)(const URI_CHAR * first, const URI_CHAR * afterLast) { |
152 | 0 | return URI_FUNC(IsWellFormedHostIpFutureMm)(first, afterLast, NULL); |
153 | 0 | } Unexecuted instantiation: uriIsWellFormedHostIpFutureA Unexecuted instantiation: uriIsWellFormedHostIpFutureW |
154 | | |
155 | | |
156 | | |
157 | | int URI_FUNC(SetHostIpFutureMm)(URI_TYPE(Uri) * uri, |
158 | | const URI_CHAR * first, |
159 | | const URI_CHAR * afterLast, |
160 | 0 | UriMemoryManager * memory) { |
161 | 0 | return URI_FUNC(InternalSetHostMm)(uri, URI_HOST_TYPE_IPFUTURE, first, afterLast, memory); |
162 | 0 | } Unexecuted instantiation: uriSetHostIpFutureMmA Unexecuted instantiation: uriSetHostIpFutureMmW |
163 | | |
164 | | |
165 | | |
166 | | int URI_FUNC(SetHostIpFuture)(URI_TYPE(Uri) * uri, |
167 | | const URI_CHAR * first, |
168 | 0 | const URI_CHAR * afterLast) { |
169 | 0 | return URI_FUNC(SetHostIpFutureMm)(uri, first, afterLast, NULL); |
170 | 0 | } Unexecuted instantiation: uriSetHostIpFutureA Unexecuted instantiation: uriSetHostIpFutureW |
171 | | |
172 | | |
173 | | |
174 | | #endif |