/src/php-src/ext/uri/uriparser/src/UriSetPort.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 "UriSetPort.c" |
46 | | # undef URI_PASS_ANSI |
47 | | # endif |
48 | | # ifdef URI_ENABLE_UNICODE |
49 | | # define URI_PASS_UNICODE 1 |
50 | | # include "UriSetPort.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 | 0 | UriBool URI_FUNC(IsWellFormedPort)(const URI_CHAR * first, const URI_CHAR * afterLast) { |
81 | 0 | if ((first == NULL) || (afterLast == NULL)) { |
82 | 0 | return URI_FALSE; |
83 | 0 | } |
84 | | |
85 | | /* NOTE: Grammar reads "port = *DIGIT" which includes the empty string. */ |
86 | 0 | while (first < afterLast) { |
87 | 0 | switch (first[0]) { |
88 | 0 | case URI_SET_DIGIT: |
89 | 0 | break; |
90 | 0 | default: |
91 | 0 | return URI_FALSE; |
92 | 0 | } |
93 | 0 | first++; |
94 | 0 | } |
95 | 0 | return URI_TRUE; |
96 | 0 | } Unexecuted instantiation: uriIsWellFormedPortA Unexecuted instantiation: uriIsWellFormedPortW |
97 | | |
98 | | int URI_FUNC(SetPortTextMm)(URI_TYPE(Uri) * uri, const URI_CHAR * first, |
99 | 0 | const URI_CHAR * afterLast, UriMemoryManager * memory) { |
100 | | /* Input validation (before making any changes) */ |
101 | 0 | if ((uri == NULL) || ((first == NULL) != (afterLast == NULL))) { |
102 | 0 | return URI_ERROR_NULL; |
103 | 0 | } |
104 | | |
105 | 0 | URI_CHECK_MEMORY_MANAGER(memory); /* may return */ |
106 | | |
107 | | /* The RFC 3986 grammar reads: |
108 | | * authority = [ userinfo "@" ] host [ ":" port ] |
109 | | * So no port without a host. */ |
110 | 0 | if ((first != NULL) && (URI_FUNC(HasHost)(uri) == URI_FALSE)) { |
111 | 0 | return URI_ERROR_SETPORT_HOST_NOT_SET; |
112 | 0 | } |
113 | | |
114 | 0 | if ((first != NULL) && (URI_FUNC(IsWellFormedPort)(first, afterLast) == URI_FALSE)) { |
115 | 0 | return URI_ERROR_SYNTAX; |
116 | 0 | } |
117 | | |
118 | | /* Clear old value */ |
119 | 0 | if ((uri->owner == URI_TRUE) && (uri->portText.first != uri->portText.afterLast)) { |
120 | 0 | memory->free(memory, (URI_CHAR *)uri->portText.first); |
121 | 0 | } |
122 | 0 | uri->portText.first = NULL; |
123 | 0 | uri->portText.afterLast = NULL; |
124 | | |
125 | | /* Already done? */ |
126 | 0 | if (first == NULL) { |
127 | 0 | return URI_SUCCESS; |
128 | 0 | } |
129 | | |
130 | 0 | assert(first != NULL); |
131 | | |
132 | | /* Ensure owned */ |
133 | 0 | if (uri->owner == URI_FALSE) { |
134 | 0 | const int res = URI_FUNC(MakeOwnerMm)(uri, memory); |
135 | 0 | if (res != URI_SUCCESS) { |
136 | 0 | return res; |
137 | 0 | } |
138 | 0 | } |
139 | | |
140 | 0 | assert(uri->owner == URI_TRUE); |
141 | | |
142 | | /* Apply new value */ |
143 | 0 | URI_TYPE(TextRange) sourceRange; |
144 | 0 | sourceRange.first = first; |
145 | 0 | sourceRange.afterLast = afterLast; |
146 | |
|
147 | 0 | if (URI_FUNC(CopyRangeAsNeeded)(&uri->portText, &sourceRange, memory) == URI_FALSE) { |
148 | 0 | return URI_ERROR_MALLOC; |
149 | 0 | } |
150 | | |
151 | 0 | return URI_SUCCESS; |
152 | 0 | } Unexecuted instantiation: uriSetPortTextMmA Unexecuted instantiation: uriSetPortTextMmW |
153 | | |
154 | | int URI_FUNC(SetPortText)(URI_TYPE(Uri) * uri, const URI_CHAR * first, |
155 | 0 | const URI_CHAR * afterLast) { |
156 | 0 | return URI_FUNC(SetPortTextMm)(uri, first, afterLast, NULL); |
157 | 0 | } Unexecuted instantiation: uriSetPortTextA Unexecuted instantiation: uriSetPortTextW |
158 | | |
159 | | #endif |