Coverage Report

Created: 2026-02-26 06:33

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
10.8k
{
28
10.8k
  out->str = NULL;
29
10.8k
  out->len = 0;
30
10.8k
}
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.36k
{
43
4.36k
  const char *s = *linep;
44
4.36k
  size_t len = 0;
45
4.36k
  DEBUGASSERT(linep && *linep && out && max && delim);
46
47
4.36k
  curlx_str_init(out);
48
14.4k
  while(*s && (*s != delim)) {
49
10.1k
    s++;
50
10.1k
    if(++len > max) {
51
29
      return STRE_BIG;
52
29
    }
53
10.1k
  }
54
4.33k
  if(!len)
55
13
    return STRE_SHORT;
56
4.32k
  out->str = *linep;
57
4.32k
  out->len = len;
58
4.32k
  *linep = s; /* point to the first byte after the word */
59
4.32k
  return STRE_OK;
60
4.33k
}
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.13k
{
66
4.13k
  return curlx_str_until(linep, out, max, ' ');
67
4.13k
}
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.8k
{
127
49.8k
  DEBUGASSERT(linep && *linep);
128
49.8k
  if(**linep != byte)
129
13.1k
    return STRE_BYTE;
130
36.7k
  (*linep)++; /* move over it */
131
36.7k
  return STRE_OK;
132
49.8k
}
133
134
/* Advance over a single space.
135
   return non-zero on error */
136
int curlx_str_singlespace(const char **linep)
137
4.13k
{
138
4.13k
  return curlx_str_single(linep, ' ');
139
4.13k
}
140
141
/* given an ASCII character and max ascii, return TRUE if valid */
142
#define valid_digit(x, m) \
143
61.0k
  (((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
16.0k
{
160
16.0k
  curl_off_t num = 0;
161
16.0k
  const char *p;
162
16.0k
  int m = (base == 10) ? '9' :   /* the largest digit possible */
163
16.0k
    (base == 16) ? 'f' : '7';
164
16.0k
  DEBUGASSERT(linep && *linep && nump);
165
16.0k
  DEBUGASSERT((base == 8) || (base == 10) || (base == 16));
166
16.0k
  DEBUGASSERT(max >= 0); /* mostly to catch SIZE_MAX, which is too large */
167
16.0k
  *nump = 0;
168
16.0k
  p = *linep;
169
16.0k
  if(!valid_digit(*p, m))
170
197
    return STRE_NO_NUM;
171
15.8k
  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.8k
  else {
181
45.3k
    do {
182
45.3k
      int n = curlx_hexval(*p++);
183
45.3k
      if(num > ((max - n) / base))
184
346
        return STRE_OVERFLOW;
185
45.0k
      num = (num * base) + n;
186
45.0k
    } while(valid_digit(*p, m));
187
15.8k
  }
188
15.5k
  *nump = num;
189
15.5k
  *linep = p;
190
15.5k
  return STRE_OK;
191
15.8k
}
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
16.0k
{
197
16.0k
  return str_num_base(linep, nump, max, 10);
198
16.0k
}
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
67.4k
{
241
67.4k
  size_t clen = check ? strlen(check) : 0;
242
67.4k
  return ((str->len == clen) && curl_strnequal(str->str, check, clen));
243
67.4k
}
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
1.69k
{
260
1.69k
  if(num <= str->len) {
261
1.69k
    str->str += num;
262
1.69k
    str->len -= num;
263
1.69k
    return STRE_OK;
264
1.69k
  }
265
0
  return STRE_OVERFLOW;
266
1.69k
}
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.9k
{
273
37.9k
  const char *s = *linep;
274
37.9k
  size_t len;
275
37.9k
  DEBUGASSERT(linep && *linep);
276
277
37.9k
  len = strcspn(s, reject);
278
37.9k
  if(len) {
279
35.0k
    out->str = s;
280
35.0k
    out->len = len;
281
35.0k
    *linep = &s[len];
282
35.0k
    return STRE_OK;
283
35.0k
  }
284
2.89k
  curlx_str_init(out);
285
2.89k
  return STRE_SHORT;
286
37.9k
}
287
288
/* remove ISBLANK()s from both ends of the string */
289
void curlx_str_trimblanks(struct Curl_str *out)
290
35.0k
{
291
36.7k
  while(out->len && ISBLANK(*out->str))
292
1.69k
    curlx_str_nudge(out, 1);
293
294
  /* trim trailing spaces and tabs */
295
35.9k
  while(out->len && ISBLANK(out->str[out->len - 1]))
296
919
    out->len--;
297
35.0k
}
298
299
/* increase the pointer until it has moved over all blanks */
300
void curlx_str_passblanks(const char **linep)
301
4.13k
{
302
4.13k
  while(ISBLANK(**linep))
303
0
    (*linep)++; /* move over it */
304
4.13k
}