Coverage Report

Created: 2026-10-22 06:50

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