Coverage Report

Created: 2025-11-16 06:23

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/php-src/ext/uri/uriparser/src/UriSetFragment.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 "UriSetFragment.c"
46
#    undef URI_PASS_ANSI
47
#  endif
48
#  ifdef URI_ENABLE_UNICODE
49
#    define URI_PASS_UNICODE 1
50
#    include "UriSetFragment.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
0
  URI_SET_DIGIT: \
96
0
  case URI_SET_HEX_LETTER_UPPER: \
97
0
  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
#  define URI_SET_SUB_DELIMS \
144
0
  _UT('!') : case _UT('$'): \
145
0
  case _UT('&'): \
146
0
  case _UT('\''): \
147
0
  case _UT('('): \
148
0
  case _UT(')'): \
149
0
  case _UT('*'): \
150
0
  case _UT('+'): \
151
0
  case _UT(','): \
152
0
  case _UT(';'): \
153
0
  case _UT('=')
154
155
#  define URI_SET_UNRESERVED \
156
0
  URI_SET_ALPHA: \
157
0
  case URI_SET_DIGIT: \
158
0
  case _UT('-'): \
159
0
  case _UT('.'): \
160
0
  case _UT('_'): \
161
0
  case _UT('~')
162
163
UriBool URI_FUNC(IsWellFormedFragment)(const URI_CHAR * first,
164
0
                                       const URI_CHAR * afterLast) {
165
0
    if ((first == NULL) || (afterLast == NULL)) {
166
0
        return URI_FALSE;
167
0
    }
168
169
    /* The related part of the grammar in RFC 3986 reads:
170
     *
171
     *   fragment      = *( pchar / "/" / "?" )
172
     *   pchar         = unreserved / pct-encoded / sub-delims / ":" / "@"
173
     */
174
0
    while (first < afterLast) {
175
0
        switch (first[0]) {
176
0
        case URI_SET_UNRESERVED:
177
0
            break;
178
179
        /* pct-encoded */
180
0
        case _UT('%'):
181
0
            if (afterLast - first < 3) {
182
0
                return URI_FALSE;
183
0
            }
184
0
            switch (first[1]) {
185
0
            case URI_SET_HEXDIG:
186
0
                break;
187
0
            default:
188
0
                return URI_FALSE;
189
0
            }
190
0
            switch (first[2]) {
191
0
            case URI_SET_HEXDIG:
192
0
                break;
193
0
            default:
194
0
                return URI_FALSE;
195
0
            }
196
0
            first += 2;
197
0
            break;
198
199
0
        case URI_SET_SUB_DELIMS:
200
0
            break;
201
202
        /* ":" / "@" and "/" / "?" */
203
0
        case _UT(':'):
204
0
        case _UT('@'):
205
0
        case _UT('/'):
206
0
        case _UT('?'):
207
0
            break;
208
209
0
        default:
210
0
            return URI_FALSE;
211
0
        }
212
213
0
        first++;
214
0
    }
215
0
    return URI_TRUE;
216
0
}
Unexecuted instantiation: uriIsWellFormedFragmentA
Unexecuted instantiation: uriIsWellFormedFragmentW
217
218
int URI_FUNC(SetFragmentMm)(URI_TYPE(Uri) * uri, const URI_CHAR * first,
219
0
                            const URI_CHAR * afterLast, UriMemoryManager * memory) {
220
    /* Input validation (before making any changes) */
221
0
    if ((uri == NULL) || ((first == NULL) != (afterLast == NULL))) {
222
0
        return URI_ERROR_NULL;
223
0
    }
224
225
0
    URI_CHECK_MEMORY_MANAGER(memory); /* may return */
226
227
0
    if ((first != NULL)
228
0
        && (URI_FUNC(IsWellFormedFragment)(first, afterLast) == URI_FALSE)) {
229
0
        return URI_ERROR_SYNTAX;
230
0
    }
231
232
    /* Clear old value */
233
0
    if ((uri->owner == URI_TRUE) && (uri->fragment.first != uri->fragment.afterLast)) {
234
0
        memory->free(memory, (URI_CHAR *)uri->fragment.first);
235
0
    }
236
0
    uri->fragment.first = NULL;
237
0
    uri->fragment.afterLast = NULL;
238
239
    /* Already done? */
240
0
    if (first == NULL) {
241
0
        return URI_SUCCESS;
242
0
    }
243
244
0
    assert(first != NULL);
245
246
    /* Ensure owned */
247
0
    if (uri->owner == URI_FALSE) {
248
0
        const int res = URI_FUNC(MakeOwnerMm)(uri, memory);
249
0
        if (res != URI_SUCCESS) {
250
0
            return res;
251
0
        }
252
0
    }
253
254
0
    assert(uri->owner == URI_TRUE);
255
256
    /* Apply new value */
257
0
    URI_TYPE(TextRange) sourceRange;
258
0
    sourceRange.first = first;
259
0
    sourceRange.afterLast = afterLast;
260
261
0
    if (URI_FUNC(CopyRangeAsNeeded)(&uri->fragment, &sourceRange, memory) == URI_FALSE) {
262
0
        return URI_ERROR_MALLOC;
263
0
    }
264
265
0
    return URI_SUCCESS;
266
0
}
Unexecuted instantiation: uriSetFragmentMmA
Unexecuted instantiation: uriSetFragmentMmW
267
268
int URI_FUNC(SetFragment)(URI_TYPE(Uri) * uri, const URI_CHAR * first,
269
0
                          const URI_CHAR * afterLast) {
270
0
    return URI_FUNC(SetFragmentMm)(uri, first, afterLast, NULL);
271
0
}
Unexecuted instantiation: uriSetFragmentA
Unexecuted instantiation: uriSetFragmentW
272
273
#endif