1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2014, Daniel Stenberg, <daniel@haxx.se>, et al.
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 http://curl.haxx.se/docs/copyright.html.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
22 #include "tool_setup.h"
24 #define _MPRINTF_REPLACE /* we want curl-functions instead of native ones */
25 #include <curl/mprintf.h>
27 #include "tool_urlglob.h"
30 #include "memdebug.h" /* keep this as LAST include */
32 #define GLOBERROR(string, column, code) \
33 glob->error = string, glob->pos = column, code
35 void glob_cleanup(URLGlob* glob);
37 static CURLcode glob_fixed(URLGlob *glob, char *fixed, size_t len)
39 URLPattern *pat = &glob->pattern[glob->size];
41 pat->content.Set.size = 1;
42 pat->content.Set.ptr_s = 0;
45 pat->content.Set.elements = malloc(sizeof(char*));
47 if(!pat->content.Set.elements)
48 return GLOBERROR("out of memory", 0, CURLE_OUT_OF_MEMORY);
50 pat->content.Set.elements[0] = malloc(len+1);
51 if(!pat->content.Set.elements[0])
52 return GLOBERROR("out of memory", 0, CURLE_OUT_OF_MEMORY);
54 memcpy(pat->content.Set.elements[0], fixed, len);
55 pat->content.Set.elements[0][len] = 0;
62 * Multiplies and checks for overflow.
64 static int multiply(unsigned long *amount, long with)
66 unsigned long sum = *amount * with;
67 if(sum/with != *amount)
68 return 1; /* didn't fit, bail out */
73 static CURLcode glob_set(URLGlob *glob, char **patternp,
74 size_t *posp, unsigned long *amount,
77 /* processes a set expression with the point behind the opening '{'
78 ','-separated elements are collected until the next closing '}'
82 char *buf = glob->glob_buffer;
83 char *pattern = *patternp;
84 char *opattern = pattern;
85 size_t opos = *posp-1;
87 pat = &glob->pattern[glob->size];
88 /* patterns 0,1,2,... correspond to size=1,3,5,... */
90 pat->content.Set.size = 0;
91 pat->content.Set.ptr_s = 0;
92 pat->content.Set.elements = NULL;
93 pat->globindex = globindex;
97 case '\0': /* URL ended while set was still open */
98 return GLOBERROR("unmatched brace", opos, CURLE_URL_MALFORMAT);
101 case '[': /* no nested expressions at this time */
102 return GLOBERROR("nested brace", *posp, CURLE_URL_MALFORMAT);
104 case '}': /* set element completed */
105 if(opattern == pattern)
106 return GLOBERROR("empty string within braces", *posp,
107 CURLE_URL_MALFORMAT);
109 /* add 1 to size since it'll be incremented below */
110 if(multiply(amount, pat->content.Set.size+1))
111 return GLOBERROR("range overflow", 0, CURLE_URL_MALFORMAT);
117 if(pat->content.Set.elements) {
118 char **new_arr = realloc(pat->content.Set.elements,
119 (pat->content.Set.size + 1) * sizeof(char*));
121 return GLOBERROR("out of memory", 0, CURLE_OUT_OF_MEMORY);
123 pat->content.Set.elements = new_arr;
126 pat->content.Set.elements = malloc(sizeof(char*));
128 if(!pat->content.Set.elements)
129 return GLOBERROR("out of memory", 0, CURLE_OUT_OF_MEMORY);
131 pat->content.Set.elements[pat->content.Set.size] =
132 strdup(glob->glob_buffer);
133 if(!pat->content.Set.elements[pat->content.Set.size])
134 return GLOBERROR("out of memory", 0, CURLE_OUT_OF_MEMORY);
135 ++pat->content.Set.size;
137 if(*pattern == '}') {
138 pattern++; /* pass the closing brace */
143 buf = glob->glob_buffer;
148 case ']': /* illegal closing bracket */
149 return GLOBERROR("unexpected close bracket", *posp, CURLE_URL_MALFORMAT);
151 case '\\': /* escaped character, skip '\' */
156 /* intentional fallthrough */
158 *buf++ = *pattern++; /* copy character to set element */
163 *patternp = pattern; /* return with the new position */
167 static CURLcode glob_range(URLGlob *glob, char **patternp,
168 size_t *posp, unsigned long *amount,
171 /* processes a range expression with the point behind the opening '['
172 - char range: e.g. "a-z]", "B-Q]"
173 - num range: e.g. "0-9]", "17-2000]"
174 - num range with leading zeros: e.g. "001-999]"
175 expression is checked for well-formedness and collected until the next ']'
179 char *pattern = *patternp;
182 pat = &glob->pattern[glob->size];
183 pat->globindex = globindex;
185 if(ISALPHA(*pattern)) {
186 /* character range detected */
191 pat->type = UPTCharRange;
193 rc = sscanf(pattern, "%c-%c", &min_c, &max_c);
195 if((rc == 2) && (pattern[3] == ':')) {
199 lstep = strtoul(&pattern[3], &endp, 10);
200 if(errno || (*endp != ']'))
205 if(step > (max_c - min_c))
212 *posp += (pattern - *patternp);
214 if((rc != 2) || (min_c >= max_c) || ((max_c - min_c) > ('z' - 'a')) ||
216 /* the pattern is not well-formed */
217 return GLOBERROR("bad range", *posp, CURLE_URL_MALFORMAT);
219 /* if there was a ":[num]" thing, use that as step or else use 1 */
220 pat->content.CharRange.step = step;
221 pat->content.CharRange.ptr_c = pat->content.CharRange.min_c = min_c;
222 pat->content.CharRange.max_c = max_c;
224 if(multiply(amount, (pat->content.CharRange.max_c -
225 pat->content.CharRange.min_c + 1)))
226 return GLOBERROR("range overflow", *posp, CURLE_URL_MALFORMAT);
228 else if(ISDIGIT(*pattern)) {
229 /* numeric range detected */
231 unsigned long max_n = 0;
232 unsigned long step_n = 0;
235 pat->type = UPTNumRange;
236 pat->content.NumRange.padlength = 0;
238 if(*pattern == '0') {
239 /* leading zero specified, count them! */
243 ++pat->content.NumRange.padlength; /* padding length is set for all
244 instances of this pattern */
249 min_n = strtoul(pattern, &endp, 10);
250 if(errno || (endp == pattern))
258 max_n = strtoul(pattern, &endp, 10);
259 if(errno || (*endp == ':')) {
262 step_n = strtoul(pattern, &endp, 10);
264 /* over/underflow situation */
269 if(endp && (*endp == ']')) {
277 *posp += (pattern - *patternp);
279 if(!endp || (min_n > max_n) || (step_n > (max_n - min_n)))
280 /* the pattern is not well-formed */
281 return GLOBERROR("bad range", *posp, CURLE_URL_MALFORMAT);
283 /* typecasting to ints are fine here since we make sure above that we
284 are within 31 bits */
285 pat->content.NumRange.ptr_n = pat->content.NumRange.min_n = min_n;
286 pat->content.NumRange.max_n = max_n;
287 pat->content.NumRange.step = step_n;
289 if(multiply(amount, (pat->content.NumRange.max_n -
290 pat->content.NumRange.min_n + 1)))
291 return GLOBERROR("range overflow", *posp, CURLE_URL_MALFORMAT);
294 return GLOBERROR("bad range specification", *posp, CURLE_URL_MALFORMAT);
300 static bool peek_ipv6(const char *str, size_t *skip)
303 * Scan for a potential IPv6 literal.
304 * - Valid globs contain a hyphen and <= 1 colon.
305 * - IPv6 literals contain no hyphens and >= 2 colons.
309 if(str[i++] != '[') {
313 const char c = str[i++];
314 if(ISALNUM(c) || c == '.' || c == '%') {
322 return colons >= 2 ? TRUE : FALSE;
330 static CURLcode glob_parse(URLGlob *glob, char *pattern,
331 size_t pos, unsigned long *amount)
333 /* processes a literal string component of a URL
334 special characters '{' and '[' branch to set/range processing functions
336 CURLcode res = CURLE_OK;
337 int globindex = 0; /* count "actual" globs */
341 while(*pattern && !res) {
342 char *buf = glob->glob_buffer;
344 while(*pattern && *pattern != '{') {
345 if(*pattern == '[') {
346 /* Skip over potential IPv6 literals. */
348 if(peek_ipv6(pattern, &skip)) {
349 memcpy(buf, pattern, skip);
357 if(*pattern == '}' || *pattern == ']')
358 return GLOBERROR("unmatched close brace/bracket", pos,
359 CURLE_URL_MALFORMAT);
361 /* only allow \ to escape known "special letters" */
362 if(*pattern == '\\' &&
363 (*(pattern+1) == '{' || *(pattern+1) == '[' ||
364 *(pattern+1) == '}' || *(pattern+1) == ']') ) {
366 /* escape character, skip '\' */
370 *buf++ = *pattern++; /* copy character to literal */
375 /* we got a literal string, add it as a single-item list */
377 res = glob_fixed(glob, glob->glob_buffer, sublen);
381 case '\0': /* done */
385 /* process set pattern */
388 res = glob_set(glob, &pattern, &pos, amount, globindex++);
392 /* process range pattern */
395 res = glob_range(glob, &pattern, &pos, amount, globindex++);
400 if(++glob->size > GLOB_PATTERN_NUM)
401 return GLOBERROR("too many globs", pos, CURLE_URL_MALFORMAT);
406 CURLcode glob_url(URLGlob** glob, char* url, unsigned long *urlnum,
410 * We can deal with any-size, just make a buffer with the same length
411 * as the specified URL!
413 URLGlob *glob_expand;
414 unsigned long amount = 0;
420 glob_buffer = malloc(strlen(url) + 1);
422 return CURLE_OUT_OF_MEMORY;
424 glob_expand = calloc(1, sizeof(URLGlob));
426 Curl_safefree(glob_buffer);
427 return CURLE_OUT_OF_MEMORY;
429 glob_expand->urllen = strlen(url);
430 glob_expand->glob_buffer = glob_buffer;
432 res = glob_parse(glob_expand, url, 1, &amount);
436 if(error && glob_expand->error) {
439 if(glob_expand->pos) {
440 snprintf(text, sizeof(text), "%s in column %zu", glob_expand->error,
445 t = glob_expand->error;
447 /* send error description to the error-stream */
448 fprintf(error, "curl: (%d) [globbing] %s\n", res, t);
450 /* it failed, we cleanup */
451 glob_cleanup(glob_expand);
460 void glob_cleanup(URLGlob* glob)
465 for(i = 0; i < glob->size; i++) {
466 if((glob->pattern[i].type == UPTSet) &&
467 (glob->pattern[i].content.Set.elements)) {
468 for(elem = glob->pattern[i].content.Set.size - 1;
471 Curl_safefree(glob->pattern[i].content.Set.elements[elem]);
473 Curl_safefree(glob->pattern[i].content.Set.elements);
476 Curl_safefree(glob->glob_buffer);
480 CURLcode glob_next_url(char **globbed, URLGlob *glob)
485 size_t buflen = glob->urllen + 1;
486 char *buf = glob->glob_buffer;
495 /* implement a counter over the index ranges of all patterns, starting
496 with the rightmost pattern */
497 for(i = 0; carry && (i < glob->size); i++) {
499 pat = &glob->pattern[glob->size - 1 - i];
502 if((pat->content.Set.elements) &&
503 (++pat->content.Set.ptr_s == pat->content.Set.size)) {
504 pat->content.Set.ptr_s = 0;
509 pat->content.CharRange.ptr_c =
510 (char)(pat->content.CharRange.step +
511 (int)((unsigned char)pat->content.CharRange.ptr_c));
512 if(pat->content.CharRange.ptr_c > pat->content.CharRange.max_c) {
513 pat->content.CharRange.ptr_c = pat->content.CharRange.min_c;
518 pat->content.NumRange.ptr_n += pat->content.NumRange.step;
519 if(pat->content.NumRange.ptr_n > pat->content.NumRange.max_n) {
520 pat->content.NumRange.ptr_n = pat->content.NumRange.min_n;
525 printf("internal error: invalid pattern type (%d)\n", (int)pat->type);
526 return CURLE_FAILED_INIT;
529 if(carry) { /* first pattern ptr has run into overflow, done! */
530 /* TODO: verify if this should actally return CURLE_OK. */
531 return CURLE_OK; /* CURLE_OK to match previous behavior */
535 for(i = 0; i < glob->size; ++i) {
536 pat = &glob->pattern[i];
539 if(pat->content.Set.elements) {
540 len = strlen(pat->content.Set.elements[pat->content.Set.ptr_s]);
541 snprintf(buf, buflen, "%s",
542 pat->content.Set.elements[pat->content.Set.ptr_s]);
548 *buf++ = pat->content.CharRange.ptr_c;
551 len = snprintf(buf, buflen, "%0*ld",
552 pat->content.NumRange.padlength,
553 pat->content.NumRange.ptr_n);
558 printf("internal error: invalid pattern type (%d)\n", (int)pat->type);
559 return CURLE_FAILED_INIT;
564 *globbed = strdup(glob->glob_buffer);
566 return CURLE_OUT_OF_MEMORY;
571 CURLcode glob_match_url(char **result, char *filename, URLGlob *glob)
576 char *appendthis = NULL;
577 size_t appendlen = 0;
578 size_t stringlen = 0;
582 /* We cannot use the glob_buffer for storage here since the filename may
583 * be longer than the URL we use. We allocate a good start size, then
584 * we need to realloc in case of need.
586 allocsize = strlen(filename) + 1; /* make it at least one byte to store the
588 target = malloc(allocsize);
590 return CURLE_OUT_OF_MEMORY;
593 if(*filename == '#' && ISDIGIT(filename[1])) {
595 char *ptr = filename;
596 unsigned long num = strtoul(&filename[1], &filename, 10);
597 URLPattern *pat =NULL;
599 if(num < glob->size) {
600 num--; /* make it zero based */
601 /* find the correct glob entry */
602 for(i=0; i<glob->size; i++) {
603 if(glob->pattern[i].globindex == (int)num) {
604 pat = &glob->pattern[i];
613 if(pat->content.Set.elements) {
614 appendthis = pat->content.Set.elements[pat->content.Set.ptr_s];
616 strlen(pat->content.Set.elements[pat->content.Set.ptr_s]);
620 numbuf[0] = pat->content.CharRange.ptr_c;
626 snprintf(numbuf, sizeof(numbuf), "%0*d",
627 pat->content.NumRange.padlength,
628 pat->content.NumRange.ptr_n);
630 appendlen = strlen(numbuf);
633 fprintf(stderr, "internal error: invalid pattern type (%d)\n",
635 Curl_safefree(target);
636 return CURLE_FAILED_INIT;
640 /* #[num] out of range, use the #[num] in the output */
642 appendthis = filename++;
647 appendthis = filename++;
650 if(appendlen + stringlen >= allocsize) {
652 /* we append a single byte to allow for the trailing byte to be appended
653 at the end of this function outside the while() loop */
654 allocsize = (appendlen + stringlen) * 2;
655 newstr = realloc(target, allocsize + 1);
657 Curl_safefree(target);
658 return CURLE_OUT_OF_MEMORY;
662 memcpy(&target[stringlen], appendthis, appendlen);
663 stringlen += appendlen;
665 target[stringlen]= '\0';