/src/php-src/ext/uri/uriparser/src/UriCopy.c
Line | Count | Source |
1 | | /* |
2 | | * uriparser - RFC 3986 URI parsing library |
3 | | * |
4 | | * Copyright (C) 2007, Weijia Song <songweijia@gmail.com> |
5 | | * Copyright (C) 2007, Sebastian Pipping <sebastian@pipping.org> |
6 | | * Copyright (C) 2025, Máté Kocsis <kocsismate@php.net> |
7 | | * All rights reserved. |
8 | | * |
9 | | * Redistribution and use in source and binary forms, with or without |
10 | | * modification, are permitted provided that the following conditions |
11 | | * are met: |
12 | | * |
13 | | * 1. Redistributions of source code must retain the above |
14 | | * copyright notice, this list of conditions and the following |
15 | | * disclaimer. |
16 | | * |
17 | | * 2. Redistributions in binary form must reproduce the above |
18 | | * copyright notice, this list of conditions and the following |
19 | | * disclaimer in the documentation and/or other materials |
20 | | * provided with the distribution. |
21 | | * |
22 | | * 3. Neither the name of the copyright holder nor the names of |
23 | | * its contributors may be used to endorse or promote products |
24 | | * derived from this software without specific prior written |
25 | | * permission. |
26 | | * |
27 | | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
28 | | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
29 | | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
30 | | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
31 | | * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
32 | | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
33 | | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
34 | | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
35 | | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
36 | | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
37 | | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
38 | | * OF THE POSSIBILITY OF SUCH DAMAGE. |
39 | | */ |
40 | | |
41 | | /** |
42 | | * @file UriCopy.c |
43 | | * Holds the RFC 3986 %URI normalization implementation. |
44 | | * NOTE: This source file includes itself twice. |
45 | | */ |
46 | | |
47 | | /* What encodings are enabled? */ |
48 | | #include <uriparser/UriDefsConfig.h> |
49 | | #if (!defined(URI_PASS_ANSI) && !defined(URI_PASS_UNICODE)) |
50 | | /* Include SELF twice */ |
51 | | # ifdef URI_ENABLE_ANSI |
52 | | # define URI_PASS_ANSI 1 |
53 | | # include "UriCopy.c" |
54 | | # undef URI_PASS_ANSI |
55 | | # endif |
56 | | # ifdef URI_ENABLE_UNICODE |
57 | | # define URI_PASS_UNICODE 1 |
58 | | # include "UriCopy.c" |
59 | | # undef URI_PASS_UNICODE |
60 | | # endif |
61 | | #else |
62 | | # ifdef URI_PASS_ANSI |
63 | | # include <uriparser/UriDefsAnsi.h> |
64 | | # else |
65 | | # include <uriparser/UriDefsUnicode.h> |
66 | | # include <wchar.h> |
67 | | # endif |
68 | | |
69 | | # ifndef URI_DOXYGEN |
70 | | # include <uriparser/Uri.h> |
71 | | # include "UriCommon.h" |
72 | | # include "UriMemory.h" |
73 | | # include "UriNormalize.h" |
74 | | # include "UriCopy.h" |
75 | | # endif |
76 | | |
77 | | static void URI_FUNC(PreventLeakageAfterCopy)(URI_TYPE(Uri) * uri, |
78 | | unsigned int revertMask, |
79 | 0 | UriMemoryManager * memory) { |
80 | 0 | URI_FUNC(PreventLeakage)(uri, revertMask, memory); |
81 | |
|
82 | 0 | if (uri->hostData.ip4 != NULL) { |
83 | 0 | memory->free(memory, uri->hostData.ip4); |
84 | 0 | uri->hostData.ip4 = NULL; |
85 | 0 | } else if (uri->hostData.ip6 != NULL) { |
86 | 0 | memory->free(memory, uri->hostData.ip6); |
87 | 0 | uri->hostData.ip6 = NULL; |
88 | 0 | } |
89 | |
|
90 | 0 | if (revertMask & URI_NORMALIZE_PORT) { |
91 | 0 | if (uri->portText.first != uri->portText.afterLast) { |
92 | 0 | memory->free(memory, (URI_CHAR *)uri->portText.first); |
93 | 0 | } |
94 | 0 | uri->portText.first = NULL; |
95 | 0 | uri->portText.afterLast = NULL; |
96 | 0 | } |
97 | 0 | } Unexecuted instantiation: UriCopy.c:uriPreventLeakageAfterCopyA Unexecuted instantiation: UriCopy.c:uriPreventLeakageAfterCopyW |
98 | | |
99 | | int URI_FUNC(CopyUriMm)(URI_TYPE(Uri) * destUri, const URI_TYPE(Uri) * sourceUri, |
100 | 0 | UriMemoryManager * memory) { |
101 | 0 | unsigned int revertMask = URI_NORMALIZED; |
102 | |
|
103 | 0 | if (sourceUri == NULL || destUri == NULL) { |
104 | 0 | return URI_ERROR_NULL; |
105 | 0 | } |
106 | | |
107 | 0 | URI_CHECK_MEMORY_MANAGER(memory); /* may return */ |
108 | | |
109 | 0 | URI_FUNC(ResetUri)(destUri); |
110 | |
|
111 | 0 | if (URI_FUNC(CopyRangeAsNeeded)(&destUri->scheme, &sourceUri->scheme, memory) |
112 | 0 | == URI_FALSE) { |
113 | 0 | return URI_ERROR_MALLOC; |
114 | 0 | } |
115 | | |
116 | 0 | revertMask |= URI_NORMALIZE_SCHEME; |
117 | |
|
118 | 0 | if (URI_FUNC(CopyRangeAsNeeded)(&destUri->userInfo, &sourceUri->userInfo, memory) |
119 | 0 | == URI_FALSE) { |
120 | 0 | URI_FUNC(PreventLeakageAfterCopy)(destUri, revertMask, memory); |
121 | 0 | return URI_ERROR_MALLOC; |
122 | 0 | } |
123 | | |
124 | 0 | revertMask |= URI_NORMALIZE_USER_INFO; |
125 | |
|
126 | 0 | if (URI_FUNC(CopyRangeAsNeeded)(&destUri->hostText, &sourceUri->hostText, memory) |
127 | 0 | == URI_FALSE) { |
128 | 0 | URI_FUNC(PreventLeakageAfterCopy)(destUri, revertMask, memory); |
129 | 0 | return URI_ERROR_MALLOC; |
130 | 0 | } |
131 | | |
132 | 0 | revertMask |= URI_NORMALIZE_HOST; |
133 | |
|
134 | 0 | if (sourceUri->hostData.ip4 == NULL) { |
135 | 0 | destUri->hostData.ip4 = NULL; |
136 | 0 | } else { |
137 | 0 | destUri->hostData.ip4 = memory->malloc(memory, sizeof(UriIp4)); |
138 | 0 | if (destUri->hostData.ip4 == NULL) { |
139 | 0 | URI_FUNC(PreventLeakageAfterCopy)(destUri, revertMask, memory); |
140 | 0 | return URI_ERROR_MALLOC; |
141 | 0 | } |
142 | 0 | *(destUri->hostData.ip4) = *(sourceUri->hostData.ip4); |
143 | 0 | } |
144 | | |
145 | 0 | if (sourceUri->hostData.ip6 == NULL) { |
146 | 0 | destUri->hostData.ip6 = NULL; |
147 | 0 | } else { |
148 | 0 | destUri->hostData.ip6 = memory->malloc(memory, sizeof(UriIp6)); |
149 | 0 | if (destUri->hostData.ip6 == NULL) { |
150 | 0 | URI_FUNC(PreventLeakageAfterCopy)(destUri, revertMask, memory); |
151 | 0 | return URI_ERROR_MALLOC; |
152 | 0 | } |
153 | 0 | *(destUri->hostData.ip6) = *(sourceUri->hostData.ip6); |
154 | 0 | } |
155 | | |
156 | 0 | if (sourceUri->hostData.ipFuture.first != NULL) { |
157 | 0 | destUri->hostData.ipFuture.first = destUri->hostText.first; |
158 | 0 | destUri->hostData.ipFuture.afterLast = destUri->hostText.afterLast; |
159 | 0 | } else if (URI_FUNC(CopyRangeAsNeeded)(&destUri->hostData.ipFuture, |
160 | 0 | &sourceUri->hostData.ipFuture, memory) |
161 | 0 | == URI_FALSE) { |
162 | 0 | URI_FUNC(PreventLeakageAfterCopy)(destUri, revertMask, memory); |
163 | 0 | return URI_ERROR_MALLOC; |
164 | 0 | } |
165 | | |
166 | 0 | if (URI_FUNC(CopyRangeAsNeeded)(&destUri->portText, &sourceUri->portText, memory) |
167 | 0 | == URI_FALSE) { |
168 | 0 | URI_FUNC(PreventLeakageAfterCopy)(destUri, revertMask, memory); |
169 | 0 | return URI_ERROR_MALLOC; |
170 | 0 | } |
171 | | |
172 | 0 | revertMask |= URI_NORMALIZE_PORT; |
173 | |
|
174 | 0 | destUri->pathHead = NULL; |
175 | 0 | destUri->pathTail = NULL; |
176 | |
|
177 | 0 | if (sourceUri->pathHead != NULL) { |
178 | 0 | URI_TYPE(PathSegment) * sourceWalker = sourceUri->pathHead; |
179 | 0 | URI_TYPE(PathSegment) * destPrev = NULL; |
180 | |
|
181 | 0 | while (sourceWalker != NULL) { |
182 | 0 | URI_TYPE(PathSegment) * destWalker = |
183 | 0 | memory->malloc(memory, sizeof(URI_TYPE(PathSegment))); |
184 | 0 | if (destWalker == NULL) { |
185 | 0 | URI_FUNC(PreventLeakageAfterCopy)(destUri, revertMask, memory); |
186 | 0 | return URI_ERROR_MALLOC; |
187 | 0 | } |
188 | | |
189 | 0 | destWalker->text.first = NULL; |
190 | 0 | destWalker->text.afterLast = NULL; |
191 | 0 | destWalker->next = NULL; |
192 | 0 | destWalker->reserved = NULL; |
193 | |
|
194 | 0 | if (destUri->pathHead == NULL) { |
195 | 0 | destUri->pathHead = destWalker; |
196 | 0 | revertMask |= URI_NORMALIZE_PATH; |
197 | 0 | } |
198 | |
|
199 | 0 | if (URI_FUNC(CopyRangeAsNeeded)(&destWalker->text, &sourceWalker->text, |
200 | 0 | memory) |
201 | 0 | == URI_FALSE) { |
202 | 0 | URI_FUNC(PreventLeakageAfterCopy)(destUri, revertMask, memory); |
203 | 0 | return URI_ERROR_MALLOC; |
204 | 0 | } |
205 | | |
206 | 0 | if (destPrev != NULL) { |
207 | 0 | destPrev->next = destWalker; |
208 | 0 | } |
209 | |
|
210 | 0 | destPrev = destWalker; |
211 | 0 | sourceWalker = sourceWalker->next; |
212 | |
|
213 | 0 | destUri->pathTail = destWalker; |
214 | 0 | } |
215 | 0 | } |
216 | | |
217 | 0 | if (URI_FUNC(CopyRangeAsNeeded)(&destUri->query, &sourceUri->query, memory) |
218 | 0 | == URI_FALSE) { |
219 | 0 | URI_FUNC(PreventLeakageAfterCopy)(destUri, revertMask, memory); |
220 | 0 | return URI_ERROR_MALLOC; |
221 | 0 | } |
222 | | |
223 | 0 | revertMask |= URI_NORMALIZE_QUERY; |
224 | |
|
225 | 0 | if (URI_FUNC(CopyRangeAsNeeded)(&destUri->fragment, &sourceUri->fragment, memory) |
226 | 0 | == URI_FALSE) { |
227 | 0 | URI_FUNC(PreventLeakageAfterCopy)(destUri, revertMask, memory); |
228 | 0 | return URI_ERROR_MALLOC; |
229 | 0 | } |
230 | | |
231 | 0 | destUri->absolutePath = sourceUri->absolutePath; |
232 | 0 | destUri->owner = URI_TRUE; |
233 | 0 | destUri->reserved = NULL; |
234 | |
|
235 | 0 | return URI_SUCCESS; |
236 | 0 | } Unexecuted instantiation: uriCopyUriMmA Unexecuted instantiation: uriCopyUriMmW |
237 | | |
238 | 0 | int URI_FUNC(CopyUri)(URI_TYPE(Uri) * destUri, const URI_TYPE(Uri) * sourceUri) { |
239 | 0 | return URI_FUNC(CopyUriMm)(destUri, sourceUri, NULL); |
240 | 0 | } Unexecuted instantiation: uriCopyUriA Unexecuted instantiation: uriCopyUriW |
241 | | |
242 | | #endif |