Coverage Report

Created: 2025-06-09 07:43

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