/src/PROJ/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 | 0 | { |
30 | 0 | out->str = NULL; |
31 | 0 | out->len = 0; |
32 | 0 | } |
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 | 0 | { |
45 | 0 | const char *s = *linep; |
46 | 0 | size_t len = 0; |
47 | 0 | DEBUGASSERT(linep && *linep && out && max && delim); |
48 | |
|
49 | 0 | curlx_str_init(out); |
50 | 0 | while(*s && (*s != delim)) { |
51 | 0 | s++; |
52 | 0 | if(++len > max) { |
53 | 0 | return STRE_BIG; |
54 | 0 | } |
55 | 0 | } |
56 | 0 | if(!len) |
57 | 0 | return STRE_SHORT; |
58 | 0 | out->str = *linep; |
59 | 0 | out->len = len; |
60 | 0 | *linep = s; /* point to the first byte after the word */ |
61 | 0 | return STRE_OK; |
62 | 0 | } |
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 | 0 | { |
77 | 0 | const char *s = *linep; |
78 | 0 | size_t len = 0; |
79 | 0 | DEBUGASSERT(linep && *linep && out && max); |
80 | |
|
81 | 0 | curlx_str_init(out); |
82 | 0 | while(*s && !ISNEWLINE(*s)) { |
83 | 0 | s++; |
84 | 0 | if(++len > max) |
85 | 0 | return STRE_BIG; |
86 | 0 | } |
87 | 0 | if(!len) |
88 | 0 | return STRE_SHORT; |
89 | 0 | out->str = *linep; |
90 | 0 | out->len = len; |
91 | 0 | *linep = s; /* point to the first byte after the word */ |
92 | 0 | return STRE_OK; |
93 | 0 | } |
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 | 0 | { |
126 | 0 | DEBUGASSERT(linep && *linep); |
127 | 0 | if(**linep != byte) |
128 | 0 | return STRE_BYTE; |
129 | 0 | (*linep)++; /* move over it */ |
130 | 0 | return STRE_OK; |
131 | 0 | } |
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 | 0 | (((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 | 0 | { |
159 | 0 | curl_off_t num = 0; |
160 | 0 | const char *p; |
161 | 0 | int m = (base == 10) ? '9' : /* the largest digit possible */ |
162 | 0 | (base == 16) ? 'f' : '7'; |
163 | 0 | DEBUGASSERT(linep && *linep && nump); |
164 | 0 | DEBUGASSERT((base == 8) || (base == 10) || (base == 16)); |
165 | 0 | DEBUGASSERT(max >= 0); /* mostly to catch SIZE_T_MAX, which is too large */ |
166 | 0 | *nump = 0; |
167 | 0 | p = *linep; |
168 | 0 | if(!valid_digit(*p, m)) |
169 | 0 | return STRE_NO_NUM; |
170 | 0 | 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 | 0 | else { |
180 | 0 | do { |
181 | 0 | int n = Curl_hexval(*p++); |
182 | 0 | if(num > ((max - n) / base)) |
183 | 0 | return STRE_OVERFLOW; |
184 | 0 | num = num * base + n; |
185 | 0 | } while(valid_digit(*p, m)); |
186 | 0 | } |
187 | 0 | *nump = num; |
188 | 0 | *linep = p; |
189 | 0 | return STRE_OK; |
190 | 0 | } |
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 | 0 | { |
196 | 0 | return str_num_base(linep, nump, max, 10); |
197 | 0 | } |
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 | 0 | { |
203 | 0 | return str_num_base(linep, nump, max, 16); |
204 | 0 | } |
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 | 0 | { |
210 | 0 | return str_num_base(linep, nump, max, 8); |
211 | 0 | } |
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 | 0 | { |
219 | 0 | curlx_str_passblanks(str); |
220 | 0 | return curlx_str_number(str, num, CURL_OFF_T_MAX); |
221 | 0 | } |
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 | 0 | { |
240 | 0 | size_t clen = check ? strlen(check) : 0; |
241 | 0 | return ((str->len == clen) && strncasecompare(str->str, check, clen)); |
242 | 0 | } |
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 | 0 | { |
259 | 0 | if(num <= str->len) { |
260 | 0 | str->str += num; |
261 | 0 | str->len -= num; |
262 | 0 | return STRE_OK; |
263 | 0 | } |
264 | 0 | return STRE_OVERFLOW; |
265 | 0 | } |
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 | 0 | { |
272 | 0 | const char *s = *linep; |
273 | 0 | size_t len; |
274 | 0 | DEBUGASSERT(linep && *linep); |
275 | |
|
276 | 0 | len = strcspn(s, reject); |
277 | 0 | if(len) { |
278 | 0 | out->str = s; |
279 | 0 | out->len = len; |
280 | 0 | *linep = &s[len]; |
281 | 0 | return STRE_OK; |
282 | 0 | } |
283 | 0 | curlx_str_init(out); |
284 | 0 | return STRE_SHORT; |
285 | 0 | } |
286 | | |
287 | | /* remove ISBLANK()s from both ends of the string */ |
288 | | void curlx_str_trimblanks(struct Curl_str *out) |
289 | 0 | { |
290 | 0 | while(out->len && ISBLANK(*out->str)) |
291 | 0 | curlx_str_nudge(out, 1); |
292 | | |
293 | | /* trim trailing spaces and tabs */ |
294 | 0 | while(out->len && ISBLANK(out->str[out->len - 1])) |
295 | 0 | out->len--; |
296 | 0 | } |
297 | | |
298 | | /* increase the pointer until it has moved over all blanks */ |
299 | | void curlx_str_passblanks(const char **linep) |
300 | 0 | { |
301 | 0 | while(ISBLANK(**linep)) |
302 | 0 | (*linep)++; /* move over it */ |
303 | 0 | } |