Coverage Report

Created: 2026-01-17 06:34

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
732k
{
28
732k
  out->str = NULL;
29
732k
  out->len = 0;
30
732k
}
31
32
void curlx_str_assign(struct Curl_str *out, const char *str, size_t len)
33
13.7k
{
34
13.7k
  out->str = str;
35
13.7k
  out->len = len;
36
13.7k
}
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
413k
{
43
413k
  const char *s = *linep;
44
413k
  size_t len = 0;
45
413k
  DEBUGASSERT(linep && *linep && out && max && delim);
46
47
413k
  curlx_str_init(out);
48
9.56M
  while(*s && (*s != delim)) {
49
9.14M
    s++;
50
9.14M
    if(++len > max) {
51
572
      return STRE_BIG;
52
572
    }
53
9.14M
  }
54
412k
  if(!len)
55
22.6k
    return STRE_SHORT;
56
389k
  out->str = *linep;
57
389k
  out->len = len;
58
389k
  *linep = s; /* point to the first byte after the word */
59
389k
  return STRE_OK;
60
412k
}
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
171k
{
66
171k
  return curlx_str_until(linep, out, max, ' ');
67
171k
}
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
95.2k
{
74
95.2k
  const char *s = *linep;
75
95.2k
  size_t len = 0;
76
95.2k
  DEBUGASSERT(linep && *linep && out && max);
77
78
95.2k
  curlx_str_init(out);
79
9.70M
  while(*s && !ISNEWLINE(*s)) {
80
9.61M
    s++;
81
9.61M
    if(++len > max)
82
2
      return STRE_BIG;
83
9.61M
  }
84
95.2k
  if(!len)
85
6.31k
    return STRE_SHORT;
86
88.9k
  out->str = *linep;
87
88.9k
  out->len = len;
88
88.9k
  *linep = s; /* point to the first byte after the word */
89
88.9k
  return STRE_OK;
90
95.2k
}
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
6.30k
{
97
6.30k
  const char *s = *linep;
98
6.30k
  size_t len = 0;
99
6.30k
  DEBUGASSERT(linep && *linep && out && max);
100
101
6.30k
  curlx_str_init(out);
102
6.30k
  if(*s != '\"')
103
13
    return STRE_BEGQUOTE;
104
6.28k
  s++;
105
137k
  while(*s && (*s != '\"')) {
106
131k
    if(*s == '\\' && s[1]) {
107
996
      s++;
108
996
      if(++len > max)
109
0
        return STRE_BIG;
110
996
    }
111
131k
    s++;
112
131k
    if(++len > max)
113
0
      return STRE_BIG;
114
131k
  }
115
6.28k
  if(*s != '\"')
116
1.61k
    return STRE_ENDQUOTE;
117
4.67k
  out->str = (*linep) + 1;
118
4.67k
  out->len = len;
119
4.67k
  *linep = s + 1;
120
4.67k
  return STRE_OK;
121
6.28k
}
122
123
/* Advance over a single character.
124
   return non-zero on error */
125
int curlx_str_single(const char **linep, char byte)
126
4.66M
{
127
4.66M
  DEBUGASSERT(linep && *linep);
128
4.66M
  if(**linep != byte)
129
3.71M
    return STRE_BYTE;
130
949k
  (*linep)++; /* move over it */
131
949k
  return STRE_OK;
132
4.66M
}
133
134
/* Advance over a single space.
135
   return non-zero on error */
136
int curlx_str_singlespace(const char **linep)
137
171k
{
138
171k
  return curlx_str_single(linep, ' ');
139
171k
}
140
141
/* given an ASCII character and max ascii, return TRUE if valid */
142
#define valid_digit(x, m) \
143
28.1M
  (((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
14.4M
{
160
14.4M
  curl_off_t num = 0;
161
14.4M
  const char *p;
162
14.4M
  int m = (base == 10) ? '9' :   /* the largest digit possible */
163
14.4M
    (base == 16) ? 'f' : '7';
164
14.4M
  DEBUGASSERT(linep && *linep && nump);
165
14.4M
  DEBUGASSERT((base == 8) || (base == 10) || (base == 16));
166
14.4M
  DEBUGASSERT(max >= 0); /* mostly to catch SIZE_MAX, which is too large */
167
14.4M
  *nump = 0;
168
14.4M
  p = *linep;
169
14.4M
  if(!valid_digit(*p, m))
170
7.55M
    return STRE_NO_NUM;
171
6.87M
  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
6.87M
  else {
181
13.7M
    do {
182
13.7M
      int n = curlx_hexval(*p++);
183
13.7M
      if(num > ((max - n) / base))
184
7.23k
        return STRE_OVERFLOW;
185
13.7M
      num = num * base + n;
186
13.7M
    } while(valid_digit(*p, m));
187
6.87M
  }
188
6.86M
  *nump = num;
189
6.86M
  *linep = p;
190
6.86M
  return STRE_OK;
191
6.87M
}
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
14.3M
{
197
14.3M
  return str_num_base(linep, nump, max, 10);
198
14.3M
}
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
2.55k
{
204
2.55k
  return str_num_base(linep, nump, max, 16);
205
2.55k
}
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
69.1k
{
211
69.1k
  return str_num_base(linep, nump, max, 8);
212
69.1k
}
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
6.87k
{
220
6.87k
  curlx_str_passblanks(str);
221
6.87k
  return curlx_str_number(str, num, CURL_OFF_T_MAX);
222
6.87k
}
223
224
/* CR or LF
225
   return non-zero on error */
226
int curlx_str_newline(const char **linep)
227
2.74k
{
228
2.74k
  DEBUGASSERT(linep && *linep);
229
2.74k
  if(ISNEWLINE(**linep)) {
230
2.71k
    (*linep)++;
231
2.71k
    return STRE_OK; /* yessir */
232
2.71k
  }
233
28
  return STRE_NEWLINE;
234
2.74k
}
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
1.52M
{
241
1.52M
  size_t clen = check ? strlen(check) : 0;
242
1.52M
  return ((str->len == clen) && curl_strnequal(str->str, check, clen));
243
1.52M
}
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
47.6k
{
249
47.6k
  if(check) {
250
47.6k
    size_t clen = strlen(check);
251
47.6k
    return ((str->len == clen) && !strncmp(str->str, check, clen));
252
47.6k
  }
253
0
  return !!(str->len);
254
47.6k
}
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
114k
{
260
114k
  if(num <= str->len) {
261
114k
    str->str += num;
262
114k
    str->len -= num;
263
114k
    return STRE_OK;
264
114k
  }
265
0
  return STRE_OVERFLOW;
266
114k
}
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
849k
{
273
849k
  const char *s = *linep;
274
849k
  size_t len;
275
849k
  DEBUGASSERT(linep && *linep);
276
277
849k
  len = strcspn(s, reject);
278
849k
  if(len) {
279
757k
    out->str = s;
280
757k
    out->len = len;
281
757k
    *linep = &s[len];
282
757k
    return STRE_OK;
283
757k
  }
284
92.1k
  curlx_str_init(out);
285
92.1k
  return STRE_SHORT;
286
849k
}
287
288
/* remove ISBLANK()s from both ends of the string */
289
void curlx_str_trimblanks(struct Curl_str *out)
290
781k
{
291
895k
  while(out->len && ISBLANK(*out->str))
292
113k
    curlx_str_nudge(out, 1);
293
294
  /* trim trailing spaces and tabs */
295
812k
  while(out->len && ISBLANK(out->str[out->len - 1]))
296
31.2k
    out->len--;
297
781k
}
298
299
/* increase the pointer until it has moved over all blanks */
300
void curlx_str_passblanks(const char **linep)
301
942k
{
302
1.06M
  while(ISBLANK(**linep))
303
122k
    (*linep)++; /* move over it */
304
942k
}