Coverage Report

Created: 2025-09-27 06:26

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
62
63
#ifndef URI_DOXYGEN
64
# include <uriparser/Uri.h>
65
# include "UriCommon.h"
66
# include "UriMemory.h"
67
#endif
68
69
70
71
#include <assert.h>
72
73
74
75
#define URI_SET_DIGIT \
76
0
       _UT('0'): \
77
0
  case _UT('1'): \
78
0
  case _UT('2'): \
79
0
  case _UT('3'): \
80
0
  case _UT('4'): \
81
0
  case _UT('5'): \
82
0
  case _UT('6'): \
83
0
  case _UT('7'): \
84
0
  case _UT('8'): \
85
0
  case _UT('9')
86
87
88
89
#define URI_SET_HEX_LETTER_UPPER \
90
0
       _UT('A'): \
91
0
  case _UT('B'): \
92
0
  case _UT('C'): \
93
0
  case _UT('D'): \
94
0
  case _UT('E'): \
95
0
  case _UT('F')
96
97
98
99
#define URI_SET_HEX_LETTER_LOWER \
100
0
       _UT('a'): \
101
0
  case _UT('b'): \
102
0
  case _UT('c'): \
103
0
  case _UT('d'): \
104
0
  case _UT('e'): \
105
0
  case _UT('f')
106
107
108
109
#define URI_SET_HEXDIG \
110
0
  URI_SET_DIGIT: \
111
0
  case URI_SET_HEX_LETTER_UPPER: \
112
0
  case URI_SET_HEX_LETTER_LOWER
113
114
115
116
#define URI_SET_ALPHA \
117
0
  URI_SET_HEX_LETTER_UPPER: \
118
0
  case URI_SET_HEX_LETTER_LOWER: \
119
0
  case _UT('g'): \
120
0
  case _UT('G'): \
121
0
  case _UT('h'): \
122
0
  case _UT('H'): \
123
0
  case _UT('i'): \
124
0
  case _UT('I'): \
125
0
  case _UT('j'): \
126
0
  case _UT('J'): \
127
0
  case _UT('k'): \
128
0
  case _UT('K'): \
129
0
  case _UT('l'): \
130
0
  case _UT('L'): \
131
0
  case _UT('m'): \
132
0
  case _UT('M'): \
133
0
  case _UT('n'): \
134
0
  case _UT('N'): \
135
0
  case _UT('o'): \
136
0
  case _UT('O'): \
137
0
  case _UT('p'): \
138
0
  case _UT('P'): \
139
0
  case _UT('q'): \
140
0
  case _UT('Q'): \
141
0
  case _UT('r'): \
142
0
  case _UT('R'): \
143
0
  case _UT('s'): \
144
0
  case _UT('S'): \
145
0
  case _UT('t'): \
146
0
  case _UT('T'): \
147
0
  case _UT('u'): \
148
0
  case _UT('U'): \
149
0
  case _UT('v'): \
150
0
  case _UT('V'): \
151
0
  case _UT('w'): \
152
0
  case _UT('W'): \
153
0
  case _UT('x'): \
154
0
  case _UT('X'): \
155
0
  case _UT('y'): \
156
0
  case _UT('Y'): \
157
0
  case _UT('z'): \
158
0
  case _UT('Z')
159
160
161
162
#define URI_SET_SUB_DELIMS \
163
0
       _UT('!'): \
164
0
  case _UT('$'): \
165
0
  case _UT('&'): \
166
0
  case _UT('\''): \
167
0
  case _UT('('): \
168
0
  case _UT(')'): \
169
0
  case _UT('*'): \
170
0
  case _UT('+'): \
171
0
  case _UT(','): \
172
0
  case _UT(';'): \
173
0
  case _UT('=')
174
175
176
177
#define URI_SET_UNRESERVED \
178
0
  URI_SET_ALPHA: \
179
0
  case URI_SET_DIGIT: \
180
0
  case _UT('-'): \
181
0
  case _UT('.'): \
182
0
  case _UT('_'): \
183
0
  case _UT('~')
184
185
186
187
0
UriBool URI_FUNC(IsWellFormedFragment)(const URI_CHAR * first, const URI_CHAR * afterLast) {
188
0
  if ((first == NULL) || (afterLast == NULL)) {
189
0
    return URI_FALSE;
190
0
  }
191
192
  /* The related part of the grammar in RFC 3986 reads:
193
   *
194
   *   fragment      = *( pchar / "/" / "?" )
195
   *   pchar         = unreserved / pct-encoded / sub-delims / ":" / "@"
196
   */
197
0
  while (first < afterLast) {
198
0
    switch (first[0]) {
199
0
      case URI_SET_UNRESERVED:
200
0
        break;
201
202
      /* pct-encoded */
203
0
      case _UT('%'):
204
0
        if (afterLast - first < 3) {
205
0
          return URI_FALSE;
206
0
        }
207
0
        switch (first[1]) {
208
0
          case URI_SET_HEXDIG:
209
0
            break;
210
0
          default:
211
0
            return URI_FALSE;
212
0
        }
213
0
        switch (first[2]) {
214
0
          case URI_SET_HEXDIG:
215
0
            break;
216
0
          default:
217
0
            return URI_FALSE;
218
0
        }
219
0
        first += 2;
220
0
        break;
221
222
0
      case URI_SET_SUB_DELIMS:
223
0
        break;
224
225
      /* ":" / "@" and "/" / "?" */
226
0
      case _UT(':'):
227
0
      case _UT('@'):
228
0
      case _UT('/'):
229
0
      case _UT('?'):
230
0
        break;
231
232
0
      default:
233
0
        return URI_FALSE;
234
0
    }
235
236
0
    first++;
237
0
  }
238
0
  return URI_TRUE;
239
0
}
Unexecuted instantiation: uriIsWellFormedFragmentA
Unexecuted instantiation: uriIsWellFormedFragmentW
240
241
242
243
int URI_FUNC(SetFragmentMm)(URI_TYPE(Uri) * uri,
244
    const URI_CHAR * first,
245
    const URI_CHAR * afterLast,
246
0
    UriMemoryManager * memory) {
247
  /* Input validation (before making any changes) */
248
0
  if ((uri == NULL) || ((first == NULL) != (afterLast == NULL))) {
249
0
    return URI_ERROR_NULL;
250
0
  }
251
252
0
  URI_CHECK_MEMORY_MANAGER(memory);  /* may return */
253
254
0
  if ((first != NULL) && (URI_FUNC(IsWellFormedFragment)(first, afterLast) == URI_FALSE)) {
255
0
    return URI_ERROR_SYNTAX;
256
0
  }
257
258
  /* Clear old value */
259
0
  if ((uri->owner == URI_TRUE) && (uri->fragment.first != uri->fragment.afterLast)) {
260
0
    memory->free(memory, (URI_CHAR *)uri->fragment.first);
261
0
  }
262
0
  uri->fragment.first = NULL;
263
0
  uri->fragment.afterLast = NULL;
264
265
  /* Already done? */
266
0
  if (first == NULL) {
267
0
    return URI_SUCCESS;
268
0
  }
269
270
0
  assert(first != NULL);
271
272
  /* Ensure owned */
273
0
  if (uri->owner == URI_FALSE) {
274
0
    const int res = URI_FUNC(MakeOwnerMm)(uri, memory);
275
0
    if (res != URI_SUCCESS) {
276
0
      return res;
277
0
    }
278
0
  }
279
280
0
  assert(uri->owner == URI_TRUE);
281
282
  /* Apply new value */
283
0
  {
284
0
    URI_TYPE(TextRange) sourceRange;
285
0
    sourceRange.first = first;
286
0
    sourceRange.afterLast = afterLast;
287
288
0
    if (URI_FUNC(CopyRangeAsNeeded)(&uri->fragment, &sourceRange, memory) == URI_FALSE) {
289
0
      return URI_ERROR_MALLOC;
290
0
    }
291
0
  }
292
293
0
  return URI_SUCCESS;
294
0
}
Unexecuted instantiation: uriSetFragmentMmA
Unexecuted instantiation: uriSetFragmentMmW
295
296
297
298
int URI_FUNC(SetFragment)(URI_TYPE(Uri) * uri,
299
    const URI_CHAR * first,
300
0
    const URI_CHAR * afterLast) {
301
0
  return URI_FUNC(SetFragmentMm)(uri, first, afterLast, NULL);
302
0
}
Unexecuted instantiation: uriSetFragmentA
Unexecuted instantiation: uriSetFragmentW
303
304
305
306
#endif