Coverage Report

Created: 2026-06-15 07:03

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