Coverage Report

Created: 2025-07-11 06:57

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