Coverage Report

Created: 2025-12-14 06:23

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
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
0
{
33
0
  out->str = NULL;
34
0
  out->len = 0;
35
0
}
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
0
{
48
0
  const char *s = *linep;
49
0
  size_t len = 0;
50
0
  DEBUGASSERT(linep && *linep && out && max && delim);
51
52
0
  curlx_str_init(out);
53
0
  while(*s && (*s != delim)) {
54
0
    s++;
55
0
    if(++len > max) {
56
0
      return STRE_BIG;
57
0
    }
58
0
  }
59
0
  if(!len)
60
0
    return STRE_SHORT;
61
0
  out->str = *linep;
62
0
  out->len = len;
63
0
  *linep = s; /* point to the first byte after the word */
64
0
  return STRE_OK;
65
0
}
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, const size_t max)
70
0
{
71
0
  return curlx_str_until(linep, out, max, ' ');
72
0
}
73
74
/* Get a word until a newline byte or end of string. At least one byte long.
75
   return non-zero on error */
76
int curlx_str_untilnl(const char **linep, struct Curl_str *out,
77
                      const size_t max)
78
0
{
79
0
  const char *s = *linep;
80
0
  size_t len = 0;
81
0
  DEBUGASSERT(linep && *linep && out && max);
82
83
0
  curlx_str_init(out);
84
0
  while(*s && !ISNEWLINE(*s)) {
85
0
    s++;
86
0
    if(++len > max)
87
0
      return STRE_BIG;
88
0
  }
89
0
  if(!len)
90
0
    return STRE_SHORT;
91
0
  out->str = *linep;
92
0
  out->len = len;
93
0
  *linep = s; /* point to the first byte after the word */
94
0
  return STRE_OK;
95
0
}
96
97
/* Get a "quoted" word. No escaping possible.
98
   return non-zero on error */
99
int curlx_str_quotedword(const char **linep, struct Curl_str *out,
100
                         const size_t max)
101
0
{
102
0
  const char *s = *linep;
103
0
  size_t len = 0;
104
0
  DEBUGASSERT(linep && *linep && out && max);
105
106
0
  curlx_str_init(out);
107
0
  if(*s != '\"')
108
0
    return STRE_BEGQUOTE;
109
0
  s++;
110
0
  while(*s && (*s != '\"')) {
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
8.34k
  (((x) >= '0') && ((x) <= m) && Curl_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 Curl_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
3.05k
{
160
3.05k
  curl_off_t num = 0;
161
3.05k
  const char *p;
162
3.05k
  int m = (base == 10) ? '9' :   /* the largest digit possible */
163
3.05k
    (base == 16) ? 'f' : '7';
164
3.05k
  DEBUGASSERT(linep && *linep && nump);
165
3.05k
  DEBUGASSERT((base == 8) || (base == 10) || (base == 16));
166
3.05k
  DEBUGASSERT(max >= 0); /* mostly to catch SIZE_MAX, which is too large */
167
3.05k
  *nump = 0;
168
3.05k
  p = *linep;
169
3.05k
  if(!valid_digit(*p, m))
170
1.09k
    return STRE_NO_NUM;
171
1.96k
  if(max < base) {
172
    /* special-case low max scenario because check needs to be different */
173
0
    do {
174
0
      int n = Curl_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.96k
  else {
181
5.32k
    do {
182
5.32k
      int n = Curl_hexval(*p++);
183
5.32k
      if(num > ((max - n) / base))
184
34
        return STRE_OVERFLOW;
185
5.28k
      num = num * base + n;
186
5.28k
    } while(valid_digit(*p, m));
187
1.96k
  }
188
1.93k
  *nump = num;
189
1.93k
  *linep = p;
190
1.93k
  return STRE_OK;
191
1.96k
}
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.39k
{
197
2.39k
  return str_num_base(linep, nump, max, 10);
198
2.39k
}
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
240
{
204
240
  return str_num_base(linep, nump, max, 16);
205
240
}
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
423
{
211
423
  return str_num_base(linep, nump, max, 8);
212
423
}
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
}