/src/php-src/ext/uri/uriparser/src/UriSetScheme.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 "UriSetScheme.c" |
46 | | # undef URI_PASS_ANSI |
47 | | # endif |
48 | | # ifdef URI_ENABLE_UNICODE |
49 | | # define URI_PASS_UNICODE 1 |
50 | | # include "UriSetScheme.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 "UriCommon.h" |
64 | | # include "UriMemory.h" |
65 | | # endif |
66 | | |
67 | | # include <assert.h> |
68 | | |
69 | | # define URI_SET_DIGIT \ |
70 | 0 | _UT('0') : case _UT('1'): \ |
71 | 0 | case _UT('2'): \ |
72 | 0 | case _UT('3'): \ |
73 | 0 | case _UT('4'): \ |
74 | 0 | case _UT('5'): \ |
75 | 0 | case _UT('6'): \ |
76 | 0 | case _UT('7'): \ |
77 | 0 | case _UT('8'): \ |
78 | 0 | case _UT('9') |
79 | | |
80 | | # define URI_SET_HEX_LETTER_UPPER \ |
81 | 0 | _UT('A') : case _UT('B'): \ |
82 | 0 | case _UT('C'): \ |
83 | 0 | case _UT('D'): \ |
84 | 0 | case _UT('E'): \ |
85 | 0 | case _UT('F') |
86 | | |
87 | | # define URI_SET_HEX_LETTER_LOWER \ |
88 | 0 | _UT('a') : case _UT('b'): \ |
89 | 0 | case _UT('c'): \ |
90 | 0 | case _UT('d'): \ |
91 | 0 | case _UT('e'): \ |
92 | 0 | case _UT('f') |
93 | | |
94 | | # define URI_SET_HEXDIG \ |
95 | | URI_SET_DIGIT: \ |
96 | | case URI_SET_HEX_LETTER_UPPER: \ |
97 | | case URI_SET_HEX_LETTER_LOWER |
98 | | |
99 | | # define URI_SET_ALPHA \ |
100 | 0 | URI_SET_HEX_LETTER_UPPER: \ |
101 | 0 | case URI_SET_HEX_LETTER_LOWER: \ |
102 | 0 | case _UT('g'): \ |
103 | 0 | case _UT('G'): \ |
104 | 0 | case _UT('h'): \ |
105 | 0 | case _UT('H'): \ |
106 | 0 | case _UT('i'): \ |
107 | 0 | case _UT('I'): \ |
108 | 0 | case _UT('j'): \ |
109 | 0 | case _UT('J'): \ |
110 | 0 | case _UT('k'): \ |
111 | 0 | case _UT('K'): \ |
112 | 0 | case _UT('l'): \ |
113 | 0 | case _UT('L'): \ |
114 | 0 | case _UT('m'): \ |
115 | 0 | case _UT('M'): \ |
116 | 0 | case _UT('n'): \ |
117 | 0 | case _UT('N'): \ |
118 | 0 | case _UT('o'): \ |
119 | 0 | case _UT('O'): \ |
120 | 0 | case _UT('p'): \ |
121 | 0 | case _UT('P'): \ |
122 | 0 | case _UT('q'): \ |
123 | 0 | case _UT('Q'): \ |
124 | 0 | case _UT('r'): \ |
125 | 0 | case _UT('R'): \ |
126 | 0 | case _UT('s'): \ |
127 | 0 | case _UT('S'): \ |
128 | 0 | case _UT('t'): \ |
129 | 0 | case _UT('T'): \ |
130 | 0 | case _UT('u'): \ |
131 | 0 | case _UT('U'): \ |
132 | 0 | case _UT('v'): \ |
133 | 0 | case _UT('V'): \ |
134 | 0 | case _UT('w'): \ |
135 | 0 | case _UT('W'): \ |
136 | 0 | case _UT('x'): \ |
137 | 0 | case _UT('X'): \ |
138 | 0 | case _UT('y'): \ |
139 | 0 | case _UT('Y'): \ |
140 | 0 | case _UT('z'): \ |
141 | 0 | case _UT('Z') |
142 | | |
143 | 0 | UriBool URI_FUNC(IsWellFormedScheme)(const URI_CHAR * first, const URI_CHAR * afterLast) { |
144 | 0 | if ((first == NULL) || (afterLast == NULL)) { |
145 | 0 | return URI_FALSE; |
146 | 0 | } |
147 | | |
148 | | /* The related part of the grammar in RFC 3986 reads: |
149 | | * |
150 | | * scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." ) |
151 | | */ |
152 | 0 | if (first >= afterLast) { |
153 | 0 | return URI_FALSE; |
154 | 0 | } |
155 | | |
156 | 0 | switch (first[0]) { |
157 | 0 | case URI_SET_ALPHA: |
158 | 0 | break; |
159 | | |
160 | 0 | default: |
161 | 0 | return URI_FALSE; |
162 | 0 | } |
163 | | |
164 | 0 | first++; |
165 | |
|
166 | 0 | while (first < afterLast) { |
167 | 0 | switch (first[0]) { |
168 | 0 | case URI_SET_ALPHA: |
169 | 0 | case URI_SET_DIGIT: |
170 | 0 | case _UT('+'): |
171 | 0 | case _UT('-'): |
172 | 0 | case _UT('.'): |
173 | 0 | break; |
174 | | |
175 | 0 | default: |
176 | 0 | return URI_FALSE; |
177 | 0 | } |
178 | | |
179 | 0 | first++; |
180 | 0 | } |
181 | 0 | return URI_TRUE; |
182 | 0 | } Unexecuted instantiation: uriIsWellFormedSchemeA Unexecuted instantiation: uriIsWellFormedSchemeW |
183 | | |
184 | | int URI_FUNC(SetSchemeMm)(URI_TYPE(Uri) * uri, const URI_CHAR * first, |
185 | 0 | const URI_CHAR * afterLast, UriMemoryManager * memory) { |
186 | | /* Input validation (before making any changes) */ |
187 | 0 | if ((uri == NULL) || ((first == NULL) != (afterLast == NULL))) { |
188 | 0 | return URI_ERROR_NULL; |
189 | 0 | } |
190 | | |
191 | 0 | URI_CHECK_MEMORY_MANAGER(memory); /* may return */ |
192 | | |
193 | 0 | if ((first != NULL) |
194 | 0 | && (URI_FUNC(IsWellFormedScheme)(first, afterLast) == URI_FALSE)) { |
195 | 0 | return URI_ERROR_SYNTAX; |
196 | 0 | } |
197 | | |
198 | | /* Clear old value */ |
199 | 0 | if ((uri->owner == URI_TRUE) && (uri->scheme.first != uri->scheme.afterLast)) { |
200 | 0 | memory->free(memory, (URI_CHAR *)uri->scheme.first); |
201 | 0 | } |
202 | 0 | uri->scheme.first = NULL; |
203 | 0 | uri->scheme.afterLast = NULL; |
204 | | |
205 | | /* Already done setting? */ |
206 | 0 | if (first == NULL) { |
207 | | /* Yes, but disambiguate as needed */ |
208 | 0 | const UriBool success = URI_FUNC(FixPathNoScheme)(uri, memory); |
209 | 0 | return (success == URI_TRUE) ? URI_SUCCESS : URI_ERROR_MALLOC; |
210 | 0 | } |
211 | | |
212 | 0 | assert(first != NULL); |
213 | | |
214 | | /* Ensure owned */ |
215 | 0 | if (uri->owner == URI_FALSE) { |
216 | 0 | const int res = URI_FUNC(MakeOwnerMm)(uri, memory); |
217 | 0 | if (res != URI_SUCCESS) { |
218 | 0 | return res; |
219 | 0 | } |
220 | 0 | } |
221 | | |
222 | 0 | assert(uri->owner == URI_TRUE); |
223 | | |
224 | | /* Apply new value */ |
225 | 0 | URI_TYPE(TextRange) sourceRange; |
226 | 0 | sourceRange.first = first; |
227 | 0 | sourceRange.afterLast = afterLast; |
228 | |
|
229 | 0 | if (URI_FUNC(CopyRangeAsNeeded)(&uri->scheme, &sourceRange, memory) == URI_FALSE) { |
230 | 0 | return URI_ERROR_MALLOC; |
231 | 0 | } |
232 | | |
233 | 0 | return URI_SUCCESS; |
234 | 0 | } Unexecuted instantiation: uriSetSchemeMmA Unexecuted instantiation: uriSetSchemeMmW |
235 | | |
236 | | int URI_FUNC(SetScheme)(URI_TYPE(Uri) * uri, const URI_CHAR * first, |
237 | 0 | const URI_CHAR * afterLast) { |
238 | 0 | return URI_FUNC(SetSchemeMm)(uri, first, afterLast, NULL); |
239 | 0 | } Unexecuted instantiation: uriSetSchemeA Unexecuted instantiation: uriSetSchemeW |
240 | | |
241 | | #endif |