Coverage Report

Created: 2026-01-17 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 "strparse.h"
25
26
void curlx_str_init(struct Curl_str *out)
27
11.0k
{
28
11.0k
  out->str = NULL;
29
11.0k
  out->len = 0;
30
11.0k
}
31
32
void curlx_str_assign(struct Curl_str *out, const char *str, size_t len)
33
0
{
34
0
  out->str = str;
35
0
  out->len = len;
36
0
}
37
38
/* Get a word until the first DELIM or end of string. At least one byte long.
39
   return non-zero on error */
40
int curlx_str_until(const char **linep, struct Curl_str *out,
41
                    const size_t max, char delim)
42
4.30k
{
43
4.30k
  const char *s = *linep;
44
4.30k
  size_t len = 0;
45
4.30k
  DEBUGASSERT(linep && *linep && out && max && delim);
46
47
4.30k
  curlx_str_init(out);
48
15.1k
  while(*s && (*s != delim)) {
49
10.8k
    s++;
50
10.8k
    if(++len > max) {
51
28
      return STRE_BIG;
52
28
    }
53
10.8k
  }
54
4.27k
  if(!len)
55
8
    return STRE_SHORT;
56
4.26k
  out->str = *linep;
57
4.26k
  out->len = len;
58
4.26k
  *linep = s; /* point to the first byte after the word */
59
4.26k
  return STRE_OK;
60
4.27k
}
61
62
/* Get a word until the first space or end of string. At least one byte long.
63
   return non-zero on error */
64
int curlx_str_word(const char **linep, struct Curl_str *out, const size_t max)
65
4.07k
{
66
4.07k
  return curlx_str_until(linep, out, max, ' ');
67
4.07k
}
68
69
/* Get a word until a newline byte or end of string. At least one byte long.
70
   return non-zero on error */
71
int curlx_str_untilnl(const char **linep, struct Curl_str *out,
72
                      const size_t max)
73
0
{
74
0
  const char *s = *linep;
75
0
  size_t len = 0;
76
0
  DEBUGASSERT(linep && *linep && out && max);
77
78
0
  curlx_str_init(out);
79
0
  while(*s && !ISNEWLINE(*s)) {
80
0
    s++;
81
0
    if(++len > max)
82
0
      return STRE_BIG;
83
0
  }
84
0
  if(!len)
85
0
    return STRE_SHORT;
86
0
  out->str = *linep;
87
0
  out->len = len;
88
0
  *linep = s; /* point to the first byte after the word */
89
0
  return STRE_OK;
90
0
}
91
92
/* Get a "quoted" word. Escaped quotes are supported.
93
   return non-zero on error */
94
int curlx_str_quotedword(const char **linep, struct Curl_str *out,
95
                         const size_t max)
96
0
{
97
0
  const char *s = *linep;
98
0
  size_t len = 0;
99
0
  DEBUGASSERT(linep && *linep && out && max);
100
101
0
  curlx_str_init(out);
102
0
  if(*s != '\"')
103
0
    return STRE_BEGQUOTE;
104
0
  s++;
105
0
  while(*s && (*s != '\"')) {
106
0
    if(*s == '\\' && s[1]) {
107
0
      s++;
108
0
      if(++len > max)
109
0
        return STRE_BIG;
110
0
    }
111
0
    s++;
112
0
    if(++len > max)
113
0
      return STRE_BIG;
114
0
  }
115
0
  if(*s != '\"')
116
0
    return STRE_ENDQUOTE;
117
0
  out->str = (*linep) + 1;
118
0
  out->len = len;
119
0
  *linep = s + 1;
120
0
  return STRE_OK;
121
0
}
122
123
/* Advance over a single character.
124
   return non-zero on error */
125
int curlx_str_single(const char **linep, char byte)
126
49.3k
{
127
49.3k
  DEBUGASSERT(linep && *linep);
128
49.3k
  if(**linep != byte)
129
13.1k
    return STRE_BYTE;
130
36.2k
  (*linep)++; /* move over it */
131
36.2k
  return STRE_OK;
132
49.3k
}
133
134
/* Advance over a single space.
135
   return non-zero on error */
136
int curlx_str_singlespace(const char **linep)
137
4.07k
{
138
4.07k
  return curlx_str_single(linep, ' ');
139
4.07k
}
140
141
/* given an ASCII character and max ascii, return TRUE if valid */
142
#define valid_digit(x, m) \
143
58.5k
  (((x) >= '0') && ((x) <= m) && curlx_hexasciitable[(x) - '0'])
144
145
/* We use 16 for the zero index (and the necessary bitwise AND in the loop)
146
   to be able to have a non-zero value there to make valid_digit() able to
147
   use the info */
148
const unsigned char curlx_hexasciitable[] = {
149
  16, 1, 2, 3, 4, 5, 6, 7, 8, 9, /* 0x30: 0 - 9 */
150
  0, 0, 0, 0, 0, 0, 0,
151
  10, 11, 12, 13, 14, 15,        /* 0x41: A - F */
152
  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,
153
  10, 11, 12, 13, 14, 15         /* 0x61: a - f */
154
};
155
156
/* no support for 0x prefix nor leading spaces */
157
static int str_num_base(const char **linep, curl_off_t *nump, curl_off_t max,
158
                        int base) /* 8, 10 or 16, nothing else */
159
15.3k
{
160
15.3k
  curl_off_t num = 0;
161
15.3k
  const char *p;
162
15.3k
  int m = (base == 10) ? '9' :   /* the largest digit possible */
163
15.3k
    (base == 16) ? 'f' : '7';
164
15.3k
  DEBUGASSERT(linep && *linep && nump);
165
15.3k
  DEBUGASSERT((base == 8) || (base == 10) || (base == 16));
166
15.3k
  DEBUGASSERT(max >= 0); /* mostly to catch SIZE_MAX, which is too large */
167
15.3k
  *nump = 0;
168
15.3k
  p = *linep;
169
15.3k
  if(!valid_digit(*p, m))
170
205
    return STRE_NO_NUM;
171
15.1k
  if(max < base) {
172
    /* special-case low max scenario because check needs to be different */
173
0
    do {
174
0
      int n = curlx_hexval(*p++);
175
0
      num = num * base + n;
176
0
      if(num > max)
177
0
        return STRE_OVERFLOW;
178
0
    } while(valid_digit(*p, m));
179
0
  }
180
15.1k
  else {
181
43.6k
    do {
182
43.6k
      int n = curlx_hexval(*p++);
183
43.6k
      if(num > ((max - n) / base))
184
362
        return STRE_OVERFLOW;
185
43.2k
      num = num * base + n;
186
43.2k
    } while(valid_digit(*p, m));
187
15.1k
  }
188
14.7k
  *nump = num;
189
14.7k
  *linep = p;
190
14.7k
  return STRE_OK;
191
15.1k
}
192
193
/* Get an unsigned decimal number with no leading space or minus. Leading
194
   zeroes are accepted. return non-zero on error */
195
int curlx_str_number(const char **linep, curl_off_t *nump, curl_off_t max)
196
15.3k
{
197
15.3k
  return str_num_base(linep, nump, max, 10);
198
15.3k
}
199
200
/* Get an unsigned hexadecimal number with no leading space or minus and no
201
   "0x" support. Leading zeroes are accepted. return non-zero on error */
202
int curlx_str_hex(const char **linep, curl_off_t *nump, curl_off_t max)
203
0
{
204
0
  return str_num_base(linep, nump, max, 16);
205
0
}
206
207
/* Get an unsigned octal number with no leading space or minus and no "0"
208
   prefix support. Leading zeroes are accepted. return non-zero on error */
209
int curlx_str_octal(const char **linep, curl_off_t *nump, curl_off_t max)
210
0
{
211
0
  return str_num_base(linep, nump, max, 8);
212
0
}
213
214
/*
215
 * Parse a positive number up to 63-bit number written in ASCII. Skip leading
216
 * blanks. No support for prefixes.
217
 */
218
int curlx_str_numblanks(const char **str, curl_off_t *num)
219
0
{
220
0
  curlx_str_passblanks(str);
221
0
  return curlx_str_number(str, num, CURL_OFF_T_MAX);
222
0
}
223
224
/* CR or LF
225
   return non-zero on error */
226
int curlx_str_newline(const char **linep)
227
0
{
228
0
  DEBUGASSERT(linep && *linep);
229
0
  if(ISNEWLINE(**linep)) {
230
0
    (*linep)++;
231
0
    return STRE_OK; /* yessir */
232
0
  }
233
0
  return STRE_NEWLINE;
234
0
}
235
236
#ifndef WITHOUT_LIBCURL
237
/* case insensitive compare that the parsed string matches the given string.
238
   Returns non-zero on match. */
239
int curlx_str_casecompare(struct Curl_str *str, const char *check)
240
66.2k
{
241
66.2k
  size_t clen = check ? strlen(check) : 0;
242
66.2k
  return ((str->len == clen) && curl_strnequal(str->str, check, clen));
243
66.2k
}
244
#endif
245
246
/* case sensitive string compare. Returns non-zero on match. */
247
int curlx_str_cmp(struct Curl_str *str, const char *check)
248
0
{
249
0
  if(check) {
250
0
    size_t clen = strlen(check);
251
0
    return ((str->len == clen) && !strncmp(str->str, check, clen));
252
0
  }
253
0
  return !!(str->len);
254
0
}
255
256
/* Trim off 'num' number of bytes from the beginning (left side) of the
257
   string. If 'num' is larger than the string, return error. */
258
int curlx_str_nudge(struct Curl_str *str, size_t num)
259
2.31k
{
260
2.31k
  if(num <= str->len) {
261
2.31k
    str->str += num;
262
2.31k
    str->len -= num;
263
2.31k
    return STRE_OK;
264
2.31k
  }
265
0
  return STRE_OVERFLOW;
266
2.31k
}
267
268
/* Get the following character sequence that consists only of bytes not
269
   present in the 'reject' string. Like strcspn(). */
270
int curlx_str_cspn(const char **linep, struct Curl_str *out,
271
                   const char *reject)
272
37.5k
{
273
37.5k
  const char *s = *linep;
274
37.5k
  size_t len;
275
37.5k
  DEBUGASSERT(linep && *linep);
276
277
37.5k
  len = strcspn(s, reject);
278
37.5k
  if(len) {
279
34.3k
    out->str = s;
280
34.3k
    out->len = len;
281
34.3k
    *linep = &s[len];
282
34.3k
    return STRE_OK;
283
34.3k
  }
284
3.10k
  curlx_str_init(out);
285
3.10k
  return STRE_SHORT;
286
37.5k
}
287
288
/* remove ISBLANK()s from both ends of the string */
289
void curlx_str_trimblanks(struct Curl_str *out)
290
34.3k
{
291
36.7k
  while(out->len && ISBLANK(*out->str))
292
2.31k
    curlx_str_nudge(out, 1);
293
294
  /* trim trailing spaces and tabs */
295
35.3k
  while(out->len && ISBLANK(out->str[out->len - 1]))
296
908
    out->len--;
297
34.3k
}
298
299
/* increase the pointer until it has moved over all blanks */
300
void curlx_str_passblanks(const char **linep)
301
4.07k
{
302
4.07k
  while(ISBLANK(**linep))
303
0
    (*linep)++; /* move over it */
304
4.07k
}