Coverage Report

Created: 2026-10-15 06:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl/lib/curlx/strparse.c
Line
Count
Source
1
/***************************************************************************
2
 *                                  _   _ ____  _
3
 *  Project                     ___| | | |  _ \| |
4
 *                             / __| | | | |_) | |
5
 *                            | (__| |_| |  _ <| |___
6
 *                             \___|\___/|_| \_\_____|
7
 *
8
 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9
 *
10
 * This software is licensed as described in the file COPYING, which
11
 * you should have received as part of this distribution. The terms
12
 * are also available at https://curl.se/docs/copyright.html.
13
 *
14
 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15
 * copies of the Software, and permit persons to whom the Software is
16
 * furnished to do so, under the terms of the COPYING file.
17
 *
18
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19
 * KIND, either express or implied.
20
 *
21
 * SPDX-License-Identifier: curl
22
 *
23
 ***************************************************************************/
24
#include "curlx/strparse.h"
25
26
void curlx_str_init(struct Curl_str *out)
27
22.4M
{
28
22.4M
  out->str = NULL;
29
22.4M
  out->len = 0;
30
22.4M
}
31
32
void curlx_str_assign(struct Curl_str *out, const char *str, size_t len)
33
1.80M
{
34
1.80M
  out->str = str;
35
1.80M
  out->len = len;
36
1.80M
}
37
38
/* remove bytes from the end of the string, never remove more bytes than what
39
   the string holds! */
40
void curlx_str_trim(struct Curl_str *out, size_t len)
41
0
{
42
0
  DEBUGASSERT(out);
43
0
  DEBUGASSERT(out->len >= len);
44
0
  out->len -= len;
45
0
}
46
47
/* Get a word until the first DELIM or end of string. At least one byte long.
48
   return non-zero on error. If 'max' is zero, it will always return error. */
49
int curlx_str_until(const char **linep, struct Curl_str *out,
50
                    const size_t max, char delim)
51
20.6M
{
52
20.6M
  const char *s;
53
20.6M
  size_t len = 0;
54
20.6M
  DEBUGASSERT(linep);
55
20.6M
  DEBUGASSERT(*linep);
56
20.6M
  DEBUGASSERT(out);
57
20.6M
  DEBUGASSERT(delim);
58
20.6M
  s = *linep;
59
60
20.6M
  curlx_str_init(out);
61
1.46G
  while(*s && (*s != delim)) {
62
1.44G
    s++;
63
1.44G
    if(++len > max) {
64
50.4k
      return STRE_BIG;
65
50.4k
    }
66
1.44G
  }
67
20.6M
  if(!len)
68
182k
    return STRE_SHORT;
69
20.4M
  out->str = *linep;
70
20.4M
  out->len = len;
71
20.4M
  *linep = s; /* point to the first byte after the word */
72
20.4M
  return STRE_OK;
73
20.6M
}
74
75
/* Get a word until the first space or end of string. At least one byte long.
76
   return non-zero on error */
77
int curlx_str_word(const char **linep, struct Curl_str *out, const size_t max)
78
679k
{
79
679k
  return curlx_str_until(linep, out, max, ' ');
80
679k
}
81
82
/* Get a word until a newline byte or end of string. At least one byte long.
83
   return non-zero on error */
84
int curlx_str_untilnl(const char **linep, struct Curl_str *out,
85
                      const size_t max)
86
1.05M
{
87
1.05M
  const char *s = *linep;
88
1.05M
  size_t len = 0;
89
1.05M
  DEBUGASSERT(linep && *linep && out && max);
90
91
1.05M
  curlx_str_init(out);
92
77.0M
  while(*s && !ISNEWLINE(*s)) {
93
76.0M
    s++;
94
76.0M
    if(++len > max)
95
0
      return STRE_BIG;
96
76.0M
  }
97
1.05M
  if(!len)
98
39.9k
    return STRE_SHORT;
99
1.01M
  out->str = *linep;
100
1.01M
  out->len = len;
101
1.01M
  *linep = s; /* point to the first byte after the word */
102
1.01M
  return STRE_OK;
103
1.05M
}
104
105
/* Get a "quoted" word. Escaped quotes are supported.
106
   return non-zero on error */
107
int curlx_str_quotedword(const char **linep, struct Curl_str *out,
108
                         const size_t max)
109
30.5k
{
110
30.5k
  const char *s = *linep;
111
30.5k
  size_t len = 0;
112
30.5k
  DEBUGASSERT(linep && *linep && out && max);
113
114
30.5k
  curlx_str_init(out);
115
30.5k
  if(*s != '\"')
116
2.43k
    return STRE_BEGQUOTE;
117
28.0k
  s++;
118
4.42M
  while(*s && (*s != '\"')) {
119
4.39M
    if(*s == '\\' && s[1]) {
120
7.17k
      s++;
121
7.17k
      if(++len > max)
122
23
        return STRE_BIG;
123
7.17k
    }
124
4.39M
    s++;
125
4.39M
    if(++len > max)
126
321
      return STRE_BIG;
127
4.39M
  }
128
27.7k
  if(*s != '\"')
129
8.01k
    return STRE_ENDQUOTE;
130
19.7k
  out->str = (*linep) + 1;
131
19.7k
  out->len = len;
132
19.7k
  *linep = s + 1;
133
19.7k
  return STRE_OK;
134
27.7k
}
135
136
/* Advance over a single character.
137
   return non-zero on error */
138
int curlx_str_single(const char **linep, char byte)
139
69.2M
{
140
69.2M
  DEBUGASSERT(linep && *linep);
141
69.2M
  if(**linep != byte)
142
46.7M
    return STRE_BYTE;
143
22.4M
  (*linep)++; /* move over it */
144
22.4M
  return STRE_OK;
145
69.2M
}
146
147
/* Advance over a single space.
148
   return non-zero on error */
149
int curlx_str_singlespace(const char **linep)
150
683k
{
151
683k
  return curlx_str_single(linep, ' ');
152
683k
}
153
154
/* given an ASCII character and max ascii, return TRUE if valid */
155
#define valid_digit(x, m) \
156
255M
  (((x) >= '0') && ((x) <= (m)) && curlx_hexasciitable[(x) - '0'])
157
158
/* We use 16 for the zero index (and the necessary bitwise AND in the loop)
159
   to be able to have a non-zero value there to make valid_digit() able to
160
   use the info */
161
const unsigned char curlx_hexasciitable[] = {
162
  16, 1, 2, 3, 4, 5, 6, 7, 8, 9, /* 0x30: 0 - 9 */
163
  0, 0, 0, 0, 0, 0, 0,
164
  10, 11, 12, 13, 14, 15,        /* 0x41: A - F */
165
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
166
  10, 11, 12, 13, 14, 15         /* 0x61: a - f */
167
};
168
169
/* no support for 0x prefix nor leading spaces */
170
static int str_num_base(const char **linep, curl_off_t *nump, curl_off_t max,
171
                        int base) /* 8, 10 or 16, nothing else */
172
78.9M
{
173
78.9M
  curl_off_t num = 0;
174
78.9M
  const char *p;
175
78.9M
  int m = (base == 10) ? '9' :   /* the largest digit possible */
176
78.9M
    (base == 16) ? 'f' : '7';
177
78.9M
  DEBUGASSERT(linep && *linep && nump);
178
78.9M
  DEBUGASSERT((base == 8) || (base == 10) || (base == 16));
179
78.9M
  DEBUGASSERT(max >= 0); /* mostly to catch SIZE_MAX, which is too large */
180
78.9M
  *nump = 0;
181
78.9M
  p = *linep;
182
78.9M
  if(!valid_digit(*p, m))
183
19.7M
    return STRE_NO_NUM;
184
59.1M
  if(max < base) {
185
    /* special-case low max scenario because check needs to be different */
186
3.59k
    do {
187
3.59k
      int n = curlx_hexval(*p++);
188
3.59k
      num = (num * base) + n;
189
3.59k
      if(num > max)
190
406
        return STRE_OVERFLOW;
191
3.59k
    } while(valid_digit(*p, m));
192
3.30k
  }
193
59.1M
  else {
194
59.1M
    curl_off_t cutoff = (max - (base - 1)) / base;
195
176M
    while(valid_digit(*p, m)) {
196
117M
      if(num <= cutoff)
197
117M
        num = (num * base) + curlx_hexval(*p++);
198
44.7k
      else {
199
44.7k
        int n = curlx_hexval(*p++);
200
44.7k
        if(num > ((max - n) / base))
201
40.1k
          return STRE_OVERFLOW;
202
4.62k
        num = (num * base) + n;
203
4.62k
      }
204
117M
    }
205
59.1M
  }
206
59.0M
  *nump = num;
207
59.0M
  *linep = p;
208
59.0M
  return STRE_OK;
209
59.1M
}
210
211
/* Get an unsigned decimal number with no leading space or minus. Leading
212
   zeroes are accepted. return non-zero on error */
213
int curlx_str_number(const char **linep, curl_off_t *nump, curl_off_t max)
214
77.6M
{
215
77.6M
  return str_num_base(linep, nump, max, 10);
216
77.6M
}
217
218
/* Get an unsigned hexadecimal number with no leading space or minus and no
219
   "0x" support. Leading zeroes are accepted. return non-zero on error */
220
int curlx_str_hex(const char **linep, curl_off_t *nump, curl_off_t max)
221
10.1k
{
222
10.1k
  return str_num_base(linep, nump, max, 16);
223
10.1k
}
224
225
/* Get an unsigned octal number with no leading space or minus and no "0"
226
   prefix support. Leading zeroes are accepted. return non-zero on error */
227
int curlx_str_octal(const char **linep, curl_off_t *nump, curl_off_t max)
228
1.27M
{
229
1.27M
  return str_num_base(linep, nump, max, 8);
230
1.27M
}
231
232
/*
233
 * Parse a positive number up to 63-bit number written in ASCII. Skip leading
234
 * blanks. No support for prefixes.
235
 */
236
int curlx_str_numblanks(const char **str, curl_off_t *num)
237
111k
{
238
111k
  curlx_str_passblanks(str);
239
111k
  return curlx_str_number(str, num, CURL_OFF_T_MAX);
240
111k
}
241
242
/* CR or LF
243
   return non-zero on error */
244
int curlx_str_newline(const char **linep)
245
100k
{
246
100k
  DEBUGASSERT(linep && *linep);
247
100k
  if(ISNEWLINE(**linep)) {
248
100k
    (*linep)++;
249
100k
    return STRE_OK; /* yessir */
250
100k
  }
251
233
  return STRE_NEWLINE;
252
100k
}
253
254
#ifndef WITHOUT_LIBCURL
255
/* case insensitive compare that the parsed string matches the given string.
256
   Returns non-zero on match. */
257
int curlx_str_casecompare(struct Curl_str *str, const char *check)
258
127M
{
259
127M
  size_t clen = check ? strlen(check) : 0;
260
127M
  return ((str->len == clen) && curl_strnequal(str->str, check, clen));
261
127M
}
262
#endif
263
264
/* case sensitive string compare. Returns non-zero on match. */
265
int curlx_str_cmp(struct Curl_str *str, const char *check)
266
340k
{
267
340k
  if(check) {
268
340k
    size_t clen = strlen(check);
269
340k
    return ((str->len == clen) && !strncmp(str->str, check, clen));
270
340k
  }
271
0
  return !!str->len;
272
340k
}
273
274
/* Trim off 'num' number of bytes from the beginning (left side) of the
275
   string. If 'num' is larger than the string, return error. */
276
int curlx_str_nudge(struct Curl_str *str, size_t num)
277
1.33M
{
278
1.33M
  if(num <= str->len) {
279
1.33M
    str->str += num;
280
1.33M
    str->len -= num;
281
1.33M
    return STRE_OK;
282
1.33M
  }
283
0
  return STRE_OVERFLOW;
284
1.33M
}
285
286
/* Get the following character sequence that consists only of bytes not
287
   present in the 'reject' string. Like strcspn(). */
288
int curlx_str_cspn(const char **linep, struct Curl_str *out,
289
                   const char *reject)
290
2.61M
{
291
2.61M
  const char *s = *linep;
292
2.61M
  size_t len;
293
2.61M
  DEBUGASSERT(linep && *linep);
294
295
2.61M
  len = strcspn(s, reject);
296
2.61M
  if(len) {
297
2.30M
    out->str = s;
298
2.30M
    out->len = len;
299
2.30M
    *linep = &s[len];
300
2.30M
    return STRE_OK;
301
2.30M
  }
302
308k
  curlx_str_init(out);
303
308k
  return STRE_SHORT;
304
2.61M
}
305
306
/* remove ISBLANK()s from both ends of the string */
307
void curlx_str_trimblanks(struct Curl_str *out)
308
4.11M
{
309
5.43M
  while(out->len && ISBLANK(*out->str))
310
1.32M
    curlx_str_nudge(out, 1);
311
312
  /* trim trailing spaces and tabs */
313
4.27M
  while(out->len && ISBLANK(out->str[out->len - 1]))
314
163k
    out->len--;
315
4.11M
}
316
317
/* increase the pointer until it has moved over all blanks */
318
void curlx_str_passblanks(const char **linep)
319
38.6M
{
320
40.3M
  while(ISBLANK(**linep))
321
1.64M
    (*linep)++; /* move over it */
322
38.6M
}