1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2013, 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 */
34 GLOB_NO_MEM = CURLE_OUT_OF_MEMORY,
35 GLOB_ERROR = CURLE_URL_MALFORMAT
38 #define GLOBERROR(string, column, code) \
39 glob->error = string, glob->pos = column, code;
41 void glob_cleanup(URLGlob* glob);
43 static GlobCode glob_fixed(URLGlob *glob, char *fixed, size_t len)
45 URLPattern *pat = &glob->pattern[glob->size];
47 pat->content.Set.size = 1;
48 pat->content.Set.ptr_s = 0;
51 pat->content.Set.elements = malloc(sizeof(char*));
53 if(!pat->content.Set.elements)
54 return GLOBERROR("out of memory", 0, GLOB_NO_MEM);
56 pat->content.Set.elements[0] = malloc(len+1);
57 if(!pat->content.Set.elements[0])
58 return GLOBERROR("out of memory", 0, GLOB_NO_MEM);
60 memcpy(pat->content.Set.elements[0], fixed, len);
61 pat->content.Set.elements[0][len] = 0;
68 * Multiplies and checks for overflow.
70 static int multiply(unsigned long *amount, long with)
72 unsigned long sum = *amount * with;
73 if(sum/with != *amount)
74 return 1; /* didn't fit, bail out */
79 static GlobCode glob_set(URLGlob *glob, char **patternp,
80 size_t *posp, unsigned long *amount,
83 /* processes a set expression with the point behind the opening '{'
84 ','-separated elements are collected until the next closing '}'
88 char *buf = glob->glob_buffer;
89 char *pattern = *patternp;
90 char *opattern = pattern;
91 size_t opos = *posp-1;
93 pat = &glob->pattern[glob->size];
94 /* patterns 0,1,2,... correspond to size=1,3,5,... */
96 pat->content.Set.size = 0;
97 pat->content.Set.ptr_s = 0;
98 pat->content.Set.elements = NULL;
99 pat->globindex = globindex;
103 case '\0': /* URL ended while set was still open */
104 return GLOBERROR("unmatched brace", opos, GLOB_ERROR);
107 case '[': /* no nested expressions at this time */
108 return GLOBERROR("nested brace", *posp, GLOB_ERROR);
110 case '}': /* set element completed */
111 if(opattern == pattern)
112 return GLOBERROR("empty string within braces", *posp, GLOB_ERROR);
114 /* add 1 to size since it'll be incremented below */
115 if(multiply(amount, pat->content.Set.size+1))
116 return GLOBERROR("range overflow", 0, GLOB_ERROR);
122 if(pat->content.Set.elements) {
123 char **new_arr = realloc(pat->content.Set.elements,
124 (pat->content.Set.size + 1) * sizeof(char*));
126 return GLOBERROR("out of memory", 0, GLOB_NO_MEM);
128 pat->content.Set.elements = new_arr;
131 pat->content.Set.elements = malloc(sizeof(char*));
133 if(!pat->content.Set.elements)
134 return GLOBERROR("out of memory", 0, GLOB_NO_MEM);
136 pat->content.Set.elements[pat->content.Set.size] =
137 strdup(glob->glob_buffer);
138 if(!pat->content.Set.elements[pat->content.Set.size])
139 return GLOBERROR("out of memory", 0, GLOB_NO_MEM);
140 ++pat->content.Set.size;
142 if(*pattern == '}') {
143 pattern++; /* pass the closing brace */
148 buf = glob->glob_buffer;
153 case ']': /* illegal closing bracket */
154 return GLOBERROR("unexpected close bracket", *posp, GLOB_ERROR);
156 case '\\': /* escaped character, skip '\' */
161 /* intentional fallthrough */
163 *buf++ = *pattern++; /* copy character to set element */
168 *patternp = pattern; /* return with the new position */
172 static GlobCode glob_range(URLGlob *glob, char **patternp,
173 size_t *posp, unsigned long *amount,
176 /* processes a range expression with the point behind the opening '['
177 - char range: e.g. "a-z]", "B-Q]"
178 - num range: e.g. "0-9]", "17-2000]"
179 - num range with leading zeros: e.g. "001-999]"
180 expression is checked for well-formedness and collected until the next ']'
184 char *pattern = *patternp;
187 pat = &glob->pattern[glob->size];
188 pat->globindex = globindex;
190 if(ISALPHA(*pattern)) {
191 /* character range detected */
196 pat->type = UPTCharRange;
198 rc = sscanf(pattern, "%c-%c", &min_c, &max_c);
200 if((rc == 2) && (pattern[3] == ':')) {
204 lstep = strtoul(&pattern[3], &endp, 10);
205 if(errno || (*endp != ']'))
210 if(step > (max_c - min_c))
217 *posp += (pattern - *patternp);
219 if((rc != 2) || (min_c >= max_c) || ((max_c - min_c) > ('z' - 'a')) ||
221 /* the pattern is not well-formed */
222 return GLOBERROR("bad range", *posp, GLOB_ERROR);
224 /* if there was a ":[num]" thing, use that as step or else use 1 */
225 pat->content.CharRange.step = step;
226 pat->content.CharRange.ptr_c = pat->content.CharRange.min_c = min_c;
227 pat->content.CharRange.max_c = max_c;
229 if(multiply(amount, (pat->content.CharRange.max_c -
230 pat->content.CharRange.min_c + 1)))
231 return GLOBERROR("range overflow", *posp, GLOB_ERROR);
233 else if(ISDIGIT(*pattern)) {
234 /* numeric range detected */
236 unsigned long max_n = 0;
237 unsigned long step_n = 0;
240 pat->type = UPTNumRange;
241 pat->content.NumRange.padlength = 0;
243 if(*pattern == '0') {
244 /* leading zero specified, count them! */
248 ++pat->content.NumRange.padlength; /* padding length is set for all
249 instances of this pattern */
254 min_n = strtoul(pattern, &endp, 10);
255 if(errno || (endp == pattern))
263 max_n = strtoul(pattern, &endp, 10);
264 if(errno || (*endp == ':')) {
267 step_n = strtoul(pattern, &endp, 10);
269 /* over/underflow situation */
274 if(endp && (*endp == ']')) {
282 *posp += (pattern - *patternp);
284 if(!endp || (min_n > max_n) || (step_n > (max_n - min_n)))
285 /* the pattern is not well-formed */
286 return GLOBERROR("bad range", *posp, GLOB_ERROR);
288 /* typecasting to ints are fine here since we make sure above that we
289 are within 31 bits */
290 pat->content.NumRange.ptr_n = pat->content.NumRange.min_n = min_n;
291 pat->content.NumRange.max_n = max_n;
292 pat->content.NumRange.step = step_n;
294 if(multiply(amount, (pat->content.NumRange.max_n -
295 pat->content.NumRange.min_n + 1)))
296 return GLOBERROR("range overflow", *posp, GLOB_ERROR);
299 return GLOBERROR("bad range specification", *posp, GLOB_ERROR);
305 static GlobCode glob_parse(URLGlob *glob, char *pattern,
306 size_t pos, unsigned long *amount)
308 /* processes a literal string component of a URL
309 special characters '{' and '[' branch to set/range processing functions
311 GlobCode res = GLOB_OK;
312 int globindex = 0; /* count "actual" globs */
316 while(*pattern && !res) {
317 char *buf = glob->glob_buffer;
319 while(*pattern && *pattern != '{' && *pattern != '[') {
320 if(*pattern == '}' || *pattern == ']')
321 return GLOBERROR("unmatched close brace/bracket", pos, GLOB_ERROR);
323 /* only allow \ to escape known "special letters" */
324 if(*pattern == '\\' &&
325 (*(pattern+1) == '{' || *(pattern+1) == '[' ||
326 *(pattern+1) == '}' || *(pattern+1) == ']') ) {
328 /* escape character, skip '\' */
332 *buf++ = *pattern++; /* copy character to literal */
337 /* we got a literal string, add it as a single-item list */
339 res = glob_fixed(glob, glob->glob_buffer, sublen);
343 case '\0': /* done */
347 /* process set pattern */
350 res = glob_set(glob, &pattern, &pos, amount, globindex++);
354 /* process range pattern */
357 res = glob_range(glob, &pattern, &pos, amount, globindex++);
362 if(++glob->size > GLOB_PATTERN_NUM)
363 return GLOBERROR("too many globs", pos, GLOB_ERROR);
368 int glob_url(URLGlob** glob, char* url, unsigned long *urlnum, FILE *error)
371 * We can deal with any-size, just make a buffer with the same length
372 * as the specified URL!
374 URLGlob *glob_expand;
375 unsigned long amount = 0;
381 glob_buffer = malloc(strlen(url) + 1);
383 return CURLE_OUT_OF_MEMORY;
385 glob_expand = calloc(1, sizeof(URLGlob));
387 Curl_safefree(glob_buffer);
388 return CURLE_OUT_OF_MEMORY;
390 glob_expand->urllen = strlen(url);
391 glob_expand->glob_buffer = glob_buffer;
393 res = glob_parse(glob_expand, url, 1, &amount);
397 if(error && glob_expand->error) {
400 if(glob_expand->pos) {
401 snprintf(text, sizeof(text), "%s in column %zu", glob_expand->error,
406 t = glob_expand->error;
408 /* send error description to the error-stream */
409 fprintf(error, "curl: (%d) [globbing] %s\n", res, t);
411 /* it failed, we cleanup */
412 glob_cleanup(glob_expand);
421 void glob_cleanup(URLGlob* glob)
426 for(i = glob->size - 1; i < glob->size; --i) {
427 if((glob->pattern[i].type == UPTSet) &&
428 (glob->pattern[i].content.Set.elements)) {
429 for(elem = glob->pattern[i].content.Set.size - 1;
432 Curl_safefree(glob->pattern[i].content.Set.elements[elem]);
434 Curl_safefree(glob->pattern[i].content.Set.elements);
437 Curl_safefree(glob->glob_buffer);
441 int glob_next_url(char **globbed, URLGlob *glob)
447 size_t buflen = glob->urllen + 1;
448 char *buf = glob->glob_buffer;
457 /* implement a counter over the index ranges of all patterns,
458 starting with the rightmost pattern */
459 for(i = glob->size - 1; carry && (i < glob->size); --i) {
461 pat = &glob->pattern[i];
464 if((pat->content.Set.elements) &&
465 (++pat->content.Set.ptr_s == pat->content.Set.size)) {
466 pat->content.Set.ptr_s = 0;
471 pat->content.CharRange.ptr_c = (char)(pat->content.CharRange.step +
472 (int)((unsigned char)pat->content.CharRange.ptr_c));
473 if(pat->content.CharRange.ptr_c > pat->content.CharRange.max_c) {
474 pat->content.CharRange.ptr_c = pat->content.CharRange.min_c;
479 pat->content.NumRange.ptr_n += pat->content.NumRange.step;
480 if(pat->content.NumRange.ptr_n > pat->content.NumRange.max_n) {
481 pat->content.NumRange.ptr_n = pat->content.NumRange.min_n;
486 printf("internal error: invalid pattern type (%d)\n", (int)pat->type);
487 return CURLE_FAILED_INIT;
490 if(carry) { /* first pattern ptr has run into overflow, done! */
491 /* TODO: verify if this should actally return CURLE_OK. */
492 return CURLE_OK; /* CURLE_OK to match previous behavior */
496 for(j = 0; j < glob->size; ++j) {
497 pat = &glob->pattern[j];
500 if(pat->content.Set.elements) {
501 len = strlen(pat->content.Set.elements[pat->content.Set.ptr_s]);
502 snprintf(buf, buflen, "%s",
503 pat->content.Set.elements[pat->content.Set.ptr_s]);
509 *buf++ = pat->content.CharRange.ptr_c;
512 len = snprintf(buf, buflen, "%0*ld",
513 pat->content.NumRange.padlength,
514 pat->content.NumRange.ptr_n);
519 printf("internal error: invalid pattern type (%d)\n", (int)pat->type);
520 return CURLE_FAILED_INIT;
525 *globbed = strdup(glob->glob_buffer);
527 return CURLE_OUT_OF_MEMORY;
532 int glob_match_url(char **result, char *filename, URLGlob *glob)
537 char *appendthis = NULL;
538 size_t appendlen = 0;
539 size_t stringlen = 0;
543 /* We cannot use the glob_buffer for storage here since the filename may
544 * be longer than the URL we use. We allocate a good start size, then
545 * we need to realloc in case of need.
547 allocsize = strlen(filename) + 1; /* make it at least one byte to store the
549 target = malloc(allocsize);
551 return CURLE_OUT_OF_MEMORY;
554 if(*filename == '#' && ISDIGIT(filename[1])) {
556 char *ptr = filename;
557 unsigned long num = strtoul(&filename[1], &filename, 10);
558 URLPattern *pat =NULL;
560 if(num < glob->size) {
561 num--; /* make it zero based */
562 /* find the correct glob entry */
563 for(i=0; i<glob->size; i++) {
564 if(glob->pattern[i].globindex == (int)num) {
565 pat = &glob->pattern[i];
574 if(pat->content.Set.elements) {
575 appendthis = pat->content.Set.elements[pat->content.Set.ptr_s];
577 strlen(pat->content.Set.elements[pat->content.Set.ptr_s]);
581 numbuf[0] = pat->content.CharRange.ptr_c;
587 snprintf(numbuf, sizeof(numbuf), "%0*d",
588 pat->content.NumRange.padlength,
589 pat->content.NumRange.ptr_n);
591 appendlen = strlen(numbuf);
594 fprintf(stderr, "internal error: invalid pattern type (%d)\n",
596 Curl_safefree(target);
597 return CURLE_FAILED_INIT;
601 /* #[num] out of range, use the #[num] in the output */
603 appendthis = filename++;
608 appendthis = filename++;
611 if(appendlen + stringlen >= allocsize) {
613 /* we append a single byte to allow for the trailing byte to be appended
614 at the end of this function outside the while() loop */
615 allocsize = (appendlen + stringlen) * 2;
616 newstr = realloc(target, allocsize + 1);
618 Curl_safefree(target);
619 return CURLE_OUT_OF_MEMORY;
623 memcpy(&target[stringlen], appendthis, appendlen);
624 stringlen += appendlen;
626 target[stringlen]= '\0';