| 1 |
/*************************************************************************** |
| 2 |
* _ _ ____ _ |
| 3 |
* Project ___| | | | _ \| | |
| 4 |
* / __| | | | |_) | | |
| 5 |
* | (__| |_| | _ <| |___ |
| 6 |
* \___|\___/|_| \_\_____| |
| 7 |
* |
| 8 |
* Copyright (C) Daniel Stenberg, <[email protected]>, 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 |
{ |
| 30 |
out->str = NULL; |
| 31 |
out->len = 0; |
| 32 |
} |
| 33 |
|
| 34 |
void curlx_str_assign(struct Curl_str *out, const char *str, size_t len) |
| 35 |
{ |
| 36 |
out->str = str; |
| 37 |
out->len = len; |
| 38 |
} |
| 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 |
{ |
| 45 |
const char *s = *linep; |
| 46 |
size_t len = 0; |
| 47 |
DEBUGASSERT(linep && *linep && out && max && delim); |
| 48 |
|
| 49 |
curlx_str_init(out); |
| 50 |
while(*s && (*s != delim)) { |
| 51 |
s++; |
| 52 |
if(++len > max) { |
| 53 |
return STRE_BIG; |
| 54 |
} |
| 55 |
} |
| 56 |
if(!len) |
| 57 |
return STRE_SHORT; |
| 58 |
out->str = *linep; |
| 59 |
out->len = len; |
| 60 |
*linep = s; /* point to the first byte after the word */ |
| 61 |
return STRE_OK; |
| 62 |
} |
| 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 |
{ |
| 69 |
return curlx_str_until(linep, out, max, ' '); |
| 70 |
} |
| 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 |
{ |
| 77 |
const char *s = *linep; |
| 78 |
size_t len = 0; |
| 79 |
DEBUGASSERT(linep && *linep && out && max); |
| 80 |
|
| 81 |
curlx_str_init(out); |
| 82 |
while(*s && !ISNEWLINE(*s)) { |
| 83 |
s++; |
| 84 |
if(++len > max) |
| 85 |
return STRE_BIG; |
| 86 |
} |
| 87 |
if(!len) |
| 88 |
return STRE_SHORT; |
| 89 |
out->str = *linep; |
| 90 |
out->len = len; |
| 91 |
*linep = s; /* point to the first byte after the word */ |
| 92 |
return STRE_OK; |
| 93 |
} |
| 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 |
{ |
| 101 |
const char *s = *linep; |
| 102 |
size_t len = 0; |
| 103 |
DEBUGASSERT(linep && *linep && out && max); |
| 104 |
|
| 105 |
curlx_str_init(out); |
| 106 |
if(*s != '\"') |
| 107 |
return STRE_BEGQUOTE; |
| 108 |
s++; |
| 109 |
while(*s && (*s != '\"')) { |
| 110 |
s++; |
| 111 |
if(++len > max) |
| 112 |
return STRE_BIG; |
| 113 |
} |
| 114 |
if(*s != '\"') |
| 115 |
return STRE_ENDQUOTE; |
| 116 |
out->str = (*linep) + 1; |
| 117 |
out->len = len; |
| 118 |
*linep = s + 1; |
| 119 |
return STRE_OK; |
| 120 |
} |
| 121 |
|
| 122 |
/* Advance over a single character. |
| 123 |
return non-zero on error */ |
| 124 |
int curlx_str_single(const char **linep, char byte) |
| 125 |
{ |
| 126 |
DEBUGASSERT(linep && *linep); |
| 127 |
if(**linep != byte) |
| 128 |
return STRE_BYTE; |
| 129 |
(*linep)++; /* move over it */ |
| 130 |
return STRE_OK; |
| 131 |
} |
| 132 |
|
| 133 |
/* Advance over a single space. |
| 134 |
return non-zero on error */ |
| 135 |
int curlx_str_singlespace(const char **linep) |
| 136 |
{ |
| 137 |
return curlx_str_single(linep, ' '); |
| 138 |
} |
| 139 |
|
| 140 |
/* given an ASCII character and max ascii, return TRUE if valid */ |
| 141 |
#define valid_digit(x,m) \ |
| 142 |
(((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 |
{ |
| 159 |
curl_off_t num = 0; |
| 160 |
const char *p; |
| 161 |
int m = (base == 10) ? '9' : /* the largest digit possible */ |
| 162 |
(base == 16) ? 'f' : '7'; |
| 163 |
DEBUGASSERT(linep && *linep && nump); |
| 164 |
DEBUGASSERT((base == 8) || (base == 10) || (base == 16)); |
| 165 |
DEBUGASSERT(max >= 0); /* mostly to catch SIZE_T_MAX, which is too large */ |
| 166 |
*nump = 0; |
| 167 |
p = *linep; |
| 168 |
if(!valid_digit(*p, m)) |
| 169 |
return STRE_NO_NUM; |
| 170 |
if(max < base) { |
| 171 |
/* special-case low max scenario because check needs to be different */ |
| 172 |
do { |
| 173 |
int n = Curl_hexval(*p++); |
| 174 |
num = num * base + n; |
| 175 |
if(num > max) |
| 176 |
return STRE_OVERFLOW; |
| 177 |
} while(valid_digit(*p, m)); |
| 178 |
} |
| 179 |
else { |
| 180 |
do { |
| 181 |
int n = Curl_hexval(*p++); |
| 182 |
if(num > ((max - n) / base)) |
| 183 |
return STRE_OVERFLOW; |
| 184 |
num = num * base + n; |
| 185 |
} while(valid_digit(*p, m)); |
| 186 |
} |
| 187 |
*nump = num; |
| 188 |
*linep = p; |
| 189 |
return STRE_OK; |
| 190 |
} |
| 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 |
{ |
| 196 |
return str_num_base(linep, nump, max, 10); |
| 197 |
} |
| 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 |
{ |
| 203 |
return str_num_base(linep, nump, max, 16); |
| 204 |
} |
| 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 |
{ |
| 210 |
return str_num_base(linep, nump, max, 8); |
| 211 |
} |
| 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 |
{ |
| 219 |
curlx_str_passblanks(str); |
| 220 |
return curlx_str_number(str, num, CURL_OFF_T_MAX); |
| 221 |
} |
| 222 |
|
| 223 |
/* CR or LF |
| 224 |
return non-zero on error */ |
| 225 |
int curlx_str_newline(const char **linep) |
| 226 |
{ |
| 227 |
DEBUGASSERT(linep && *linep); |
| 228 |
if(ISNEWLINE(**linep)) { |
| 229 |
(*linep)++; |
| 230 |
return STRE_OK; /* yessir */ |
| 231 |
} |
| 232 |
return STRE_NEWLINE; |
| 233 |
} |
| 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 |
{ |
| 240 |
size_t clen = check ? strlen(check) : 0; |
| 241 |
return ((str->len == clen) && strncasecompare(str->str, check, clen)); |
| 242 |
} |
| 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 |
{ |
| 248 |
if(check) { |
| 249 |
size_t clen = strlen(check); |
| 250 |
return ((str->len == clen) && !strncmp(str->str, check, clen)); |
| 251 |
} |
| 252 |
return !!(str->len); |
| 253 |
} |
| 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 |
{ |
| 259 |
if(num <= str->len) { |
| 260 |
str->str += num; |
| 261 |
str->len -= num; |
| 262 |
return STRE_OK; |
| 263 |
} |
| 264 |
return STRE_OVERFLOW; |
| 265 |
} |
| 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 |
{ |
| 272 |
const char *s = *linep; |
| 273 |
size_t len; |
| 274 |
DEBUGASSERT(linep && *linep); |
| 275 |
|
| 276 |
len = strcspn(s, reject); |
| 277 |
if(len) { |
| 278 |
out->str = s; |
| 279 |
out->len = len; |
| 280 |
*linep = &s[len]; |
| 281 |
return STRE_OK; |
| 282 |
} |
| 283 |
curlx_str_init(out); |
| 284 |
return STRE_SHORT; |
| 285 |
} |
| 286 |
|
| 287 |
/* remove ISBLANK()s from both ends of the string */ |
| 288 |
void curlx_str_trimblanks(struct Curl_str *out) |
| 289 |
{ |
| 290 |
while(out->len && ISBLANK(*out->str)) |
| 291 |
curlx_str_nudge(out, 1); |
| 292 |
|
| 293 |
/* trim trailing spaces and tabs */ |
| 294 |
while(out->len && ISBLANK(out->str[out->len - 1])) |
| 295 |
out->len--; |
| 296 |
} |
| 297 |
|
| 298 |
/* increase the pointer until it has moved over all blanks */ |
| 299 |
void curlx_str_passblanks(const char **linep) |
| 300 |
{ |
| 301 |
while(ISBLANK(**linep)) |
| 302 |
(*linep)++; /* move over it */ |
| 303 |
} |