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