Coverage Report

Created: 2026-10-15 06:19

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
17.7M
{
28
17.7M
  out->str = NULL;
29
17.7M
  out->len = 0;
30
17.7M
}
31
32
void curlx_str_assign(struct Curl_str *out, const char *str, size_t len)
33
60.7k
{
34
60.7k
  out->str = str;
35
60.7k
  out->len = len;
36
60.7k
}
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
17.5M
{
52
17.5M
  const char *s;
53
17.5M
  size_t len = 0;
54
17.5M
  DEBUGASSERT(linep);
55
17.5M
  DEBUGASSERT(*linep);
56
17.5M
  DEBUGASSERT(out);
57
17.5M
  DEBUGASSERT(delim);
58
17.5M
  s = *linep;
59
60
17.5M
  curlx_str_init(out);
61
1.35G
  while(*s && (*s != delim)) {
62
1.33G
    s++;
63
1.33G
    if(++len > max) {
64
3.78k
      return STRE_BIG;
65
3.78k
    }
66
1.33G
  }
67
17.5M
  if(!len)
68
22.2k
    return STRE_SHORT;
69
17.5M
  out->str = *linep;
70
17.5M
  out->len = len;
71
17.5M
  *linep = s; /* point to the first byte after the word */
72
17.5M
  return STRE_OK;
73
17.5M
}
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
21.2k
{
79
21.2k
  return curlx_str_until(linep, out, max, ' ');
80
21.2k
}
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
167k
{
87
167k
  const char *s = *linep;
88
167k
  size_t len = 0;
89
167k
  DEBUGASSERT(linep && *linep && out && max);
90
91
167k
  curlx_str_init(out);
92
7.72M
  while(*s && !ISNEWLINE(*s)) {
93
7.55M
    s++;
94
7.55M
    if(++len > max)
95
0
      return STRE_BIG;
96
7.55M
  }
97
167k
  if(!len)
98
1.94k
    return STRE_SHORT;
99
165k
  out->str = *linep;
100
165k
  out->len = len;
101
165k
  *linep = s; /* point to the first byte after the word */
102
165k
  return STRE_OK;
103
167k
}
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
0
{
110
0
  const char *s = *linep;
111
0
  size_t len = 0;
112
0
  DEBUGASSERT(linep && *linep && out && max);
113
114
0
  curlx_str_init(out);
115
0
  if(*s != '\"')
116
0
    return STRE_BEGQUOTE;
117
0
  s++;
118
0
  while(*s && (*s != '\"')) {
119
0
    if(*s == '\\' && s[1]) {
120
0
      s++;
121
0
      if(++len > max)
122
0
        return STRE_BIG;
123
0
    }
124
0
    s++;
125
0
    if(++len > max)
126
0
      return STRE_BIG;
127
0
  }
128
0
  if(*s != '\"')
129
0
    return STRE_ENDQUOTE;
130
0
  out->str = (*linep) + 1;
131
0
  out->len = len;
132
0
  *linep = s + 1;
133
0
  return STRE_OK;
134
0
}
135
136
/* Advance over a single character.
137
   return non-zero on error */
138
int curlx_str_single(const char **linep, char byte)
139
35.1M
{
140
35.1M
  DEBUGASSERT(linep && *linep);
141
35.1M
  if(**linep != byte)
142
16.5M
    return STRE_BYTE;
143
18.6M
  (*linep)++; /* move over it */
144
18.6M
  return STRE_OK;
145
35.1M
}
146
147
/* Advance over a single space.
148
   return non-zero on error */
149
int curlx_str_singlespace(const char **linep)
150
21.2k
{
151
21.2k
  return curlx_str_single(linep, ' ');
152
21.2k
}
153
154
/* given an ASCII character and max ascii, return TRUE if valid */
155
#define valid_digit(x, m) \
156
7.14M
  (((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
2.89M
{
173
2.89M
  curl_off_t num = 0;
174
2.89M
  const char *p;
175
2.89M
  int m = (base == 10) ? '9' :   /* the largest digit possible */
176
2.89M
    (base == 16) ? 'f' : '7';
177
2.89M
  DEBUGASSERT(linep && *linep && nump);
178
2.89M
  DEBUGASSERT((base == 8) || (base == 10) || (base == 16));
179
2.89M
  DEBUGASSERT(max >= 0); /* mostly to catch SIZE_MAX, which is too large */
180
2.89M
  *nump = 0;
181
2.89M
  p = *linep;
182
2.89M
  if(!valid_digit(*p, m))
183
1.32M
    return STRE_NO_NUM;
184
1.56M
  if(max < base) {
185
    /* special-case low max scenario because check needs to be different */
186
0
    do {
187
0
      int n = curlx_hexval(*p++);
188
0
      num = (num * base) + n;
189
0
      if(num > max)
190
0
        return STRE_OVERFLOW;
191
0
    } while(valid_digit(*p, m));
192
0
  }
193
1.56M
  else {
194
1.56M
    curl_off_t cutoff = (max - (base - 1)) / base;
195
4.25M
    while(valid_digit(*p, m)) {
196
2.68M
      if(num <= cutoff)
197
2.68M
        num = (num * base) + curlx_hexval(*p++);
198
1.56k
      else {
199
1.56k
        int n = curlx_hexval(*p++);
200
1.56k
        if(num > ((max - n) / base))
201
1.31k
          return STRE_OVERFLOW;
202
250
        num = (num * base) + n;
203
250
      }
204
2.68M
    }
205
1.56M
  }
206
1.56M
  *nump = num;
207
1.56M
  *linep = p;
208
1.56M
  return STRE_OK;
209
1.56M
}
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
2.48M
{
215
2.48M
  return str_num_base(linep, nump, max, 10);
216
2.48M
}
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
754
{
222
754
  return str_num_base(linep, nump, max, 16);
223
754
}
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
408k
{
229
408k
  return str_num_base(linep, nump, max, 8);
230
408k
}
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
5.37k
{
238
5.37k
  curlx_str_passblanks(str);
239
5.37k
  return curlx_str_number(str, num, CURL_OFF_T_MAX);
240
5.37k
}
241
242
/* CR or LF
243
   return non-zero on error */
244
int curlx_str_newline(const char **linep)
245
4.93k
{
246
4.93k
  DEBUGASSERT(linep && *linep);
247
4.93k
  if(ISNEWLINE(**linep)) {
248
4.92k
    (*linep)++;
249
4.92k
    return STRE_OK; /* yessir */
250
4.92k
  }
251
9
  return STRE_NEWLINE;
252
4.93k
}
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
119M
{
259
119M
  size_t clen = check ? strlen(check) : 0;
260
119M
  return ((str->len == clen) && curl_strnequal(str->str, check, clen));
261
119M
}
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
133k
{
267
133k
  if(check) {
268
133k
    size_t clen = strlen(check);
269
133k
    return ((str->len == clen) && !strncmp(str->str, check, clen));
270
133k
  }
271
0
  return !!str->len;
272
133k
}
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
19.0k
{
278
19.0k
  if(num <= str->len) {
279
19.0k
    str->str += num;
280
19.0k
    str->len -= num;
281
19.0k
    return STRE_OK;
282
19.0k
  }
283
0
  return STRE_OVERFLOW;
284
19.0k
}
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
155k
{
291
155k
  const char *s = *linep;
292
155k
  size_t len;
293
155k
  DEBUGASSERT(linep && *linep);
294
295
155k
  len = strcspn(s, reject);
296
155k
  if(len) {
297
134k
    out->str = s;
298
134k
    out->len = len;
299
134k
    *linep = &s[len];
300
134k
    return STRE_OK;
301
134k
  }
302
21.0k
  curlx_str_init(out);
303
21.0k
  return STRE_SHORT;
304
155k
}
305
306
/* remove ISBLANK()s from both ends of the string */
307
void curlx_str_trimblanks(struct Curl_str *out)
308
487k
{
309
505k
  while(out->len && ISBLANK(*out->str))
310
18.0k
    curlx_str_nudge(out, 1);
311
312
  /* trim trailing spaces and tabs */
313
495k
  while(out->len && ISBLANK(out->str[out->len - 1]))
314
7.97k
    out->len--;
315
487k
}
316
317
/* increase the pointer until it has moved over all blanks */
318
void curlx_str_passblanks(const char **linep)
319
34.6M
{
320
35.3M
  while(ISBLANK(**linep))
321
765k
    (*linep)++; /* move over it */
322
34.6M
}