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, unsigned long *amount,
44 char *fixed, size_t len)
46 URLPattern *pat = &glob->pattern[glob->size];
48 pat->content.Set.size = 1;
49 pat->content.Set.ptr_s = 0;
54 pat->content.Set.elements = malloc(sizeof(char*));
56 if(!pat->content.Set.elements)
57 return GLOBERROR("out of memory", 0, GLOB_NO_MEM);
59 pat->content.Set.elements[0] = malloc(len+1);
60 if(!pat->content.Set.elements[0])
61 return GLOBERROR("out of memory", 0, GLOB_NO_MEM);
63 memcpy(pat->content.Set.elements[0], fixed, len);
64 pat->content.Set.elements[0][len] = 0;
71 * Multiplies and checks for overflow.
73 static int multiply(unsigned long *amount, long with)
75 unsigned long sum = *amount * with;
76 if(sum/with != *amount)
77 return 1; /* didn't fit, bail out */
82 static GlobCode glob_set(URLGlob *glob, char **patternp,
83 size_t *posp, unsigned long *amount,
86 /* processes a set expression with the point behind the opening '{'
87 ','-separated elements are collected until the next closing '}'
91 char *buf = glob->glob_buffer;
92 char *pattern = *patternp;
93 char *opattern = pattern;
94 size_t opos = *posp-1;
96 pat = &glob->pattern[glob->size];
97 /* patterns 0,1,2,... correspond to size=1,3,5,... */
99 pat->content.Set.size = 0;
100 pat->content.Set.ptr_s = 0;
101 pat->content.Set.elements = NULL;
102 pat->globindex = globindex;
106 case '\0': /* URL ended while set was still open */
107 return GLOBERROR("unmatched brace", opos, GLOB_ERROR);
110 case '[': /* no nested expressions at this time */
111 return GLOBERROR("nested brace", *posp, GLOB_ERROR);
113 case '}': /* set element completed */
114 if(opattern == pattern)
115 return GLOBERROR("empty string within braces", *posp, GLOB_ERROR);
117 /* add 1 to size since it'll be incremented below */
118 if(multiply(amount, pat->content.Set.size+1))
119 return GLOBERROR("range overflow", 0, GLOB_ERROR);
125 if(pat->content.Set.elements) {
126 char **new_arr = realloc(pat->content.Set.elements,
127 (pat->content.Set.size + 1) * sizeof(char*));
129 return GLOBERROR("out of memory", 0, GLOB_NO_MEM);
131 pat->content.Set.elements = new_arr;
134 pat->content.Set.elements = malloc(sizeof(char*));
136 if(!pat->content.Set.elements)
137 return GLOBERROR("out of memory", 0, GLOB_NO_MEM);
139 pat->content.Set.elements[pat->content.Set.size] =
140 strdup(glob->glob_buffer);
141 if(!pat->content.Set.elements[pat->content.Set.size])
142 return GLOBERROR("out of memory", 0, GLOB_NO_MEM);
143 ++pat->content.Set.size;
145 if(*pattern == '}') {
146 pattern++; /* pass the closing brace */
151 buf = glob->glob_buffer;
156 case ']': /* illegal closing bracket */
157 return GLOBERROR("unexpected close bracket", *posp, GLOB_ERROR);
159 case '\\': /* escaped character, skip '\' */
164 /* intentional fallthrough */
166 *buf++ = *pattern++; /* copy character to set element */
171 *patternp = pattern; /* return with the new position */
175 static GlobCode glob_range(URLGlob *glob, char **patternp,
176 size_t *posp, unsigned long *amount,
179 /* processes a range expression with the point behind the opening '['
180 - char range: e.g. "a-z]", "B-Q]"
181 - num range: e.g. "0-9]", "17-2000]"
182 - num range with leading zeros: e.g. "001-999]"
183 expression is checked for well-formedness and collected until the next ']'
187 char *pattern = *patternp;
190 pat = &glob->pattern[glob->size];
191 pat->globindex = globindex;
193 if(ISALPHA(*pattern)) {
194 /* character range detected */
199 pat->type = UPTCharRange;
201 rc = sscanf(pattern, "%c-%c", &min_c, &max_c);
203 if((rc == 2) && (pattern[3] == ':')) {
207 lstep = strtoul(&pattern[3], &endp, 10);
208 if(errno || (*endp != ']'))
213 if(step > (max_c - min_c))
220 *posp += (pattern - *patternp);
222 if((rc != 2) || (min_c >= max_c) || ((max_c - min_c) > ('z' - 'a')) ||
224 /* the pattern is not well-formed */
225 return GLOBERROR("bad range", *posp, GLOB_ERROR);
227 /* if there was a ":[num]" thing, use that as step or else use 1 */
228 pat->content.CharRange.step = step;
229 pat->content.CharRange.ptr_c = pat->content.CharRange.min_c = min_c;
230 pat->content.CharRange.max_c = max_c;
232 if(multiply(amount, (pat->content.CharRange.max_c -
233 pat->content.CharRange.min_c + 1)))
234 return GLOBERROR("range overflow", *posp, GLOB_ERROR);
236 else if(ISDIGIT(*pattern)) {
237 /* numeric range detected */
239 unsigned long max_n = 0;
240 unsigned long step_n = 0;
243 pat->type = UPTNumRange;
244 pat->content.NumRange.padlength = 0;
246 if(*pattern == '0') {
247 /* leading zero specified, count them! */
251 ++pat->content.NumRange.padlength; /* padding length is set for all
252 instances of this pattern */
257 min_n = strtoul(pattern, &endp, 10);
258 if(errno || (endp == pattern))
266 max_n = strtoul(pattern, &endp, 10);
267 if(errno || (*endp == ':')) {
270 step_n = strtoul(pattern, &endp, 10);
272 /* over/underflow situation */
277 if(endp && (*endp == ']')) {
285 *posp += (pattern - *patternp);
287 if(!endp || (min_n > max_n) || (step_n > (max_n - min_n)))
288 /* the pattern is not well-formed */
289 return GLOBERROR("bad range", *posp, GLOB_ERROR);
291 /* typecasting to ints are fine here since we make sure above that we
292 are within 31 bits */
293 pat->content.NumRange.ptr_n = pat->content.NumRange.min_n = min_n;
294 pat->content.NumRange.max_n = max_n;
295 pat->content.NumRange.step = step_n;
297 if(multiply(amount, (pat->content.NumRange.max_n -
298 pat->content.NumRange.min_n + 1)))
299 return GLOBERROR("range overflow", *posp, GLOB_ERROR);
302 return GLOBERROR("bad range specification", *posp, GLOB_ERROR);
308 static GlobCode glob_parse(URLGlob *glob, char *pattern,
309 size_t pos, unsigned long *amount)
311 /* processes a literal string component of a URL
312 special characters '{' and '[' branch to set/range processing functions
314 GlobCode res = GLOB_OK;
315 int globindex = 0; /* count "actual" globs */
317 while(*pattern && !res) {
318 char *buf = glob->glob_buffer;
320 while(*pattern && *pattern != '{' && *pattern != '[') {
321 if(*pattern == '}' || *pattern == ']')
322 return GLOBERROR("unmatched close brace/bracket", pos, GLOB_ERROR);
324 /* only allow \ to escape known "special letters" */
325 if(*pattern == '\\' &&
326 (*(pattern+1) == '{' || *(pattern+1) == '[' ||
327 *(pattern+1) == '}' || *(pattern+1) == ']') ) {
329 /* escape character, skip '\' */
333 *buf++ = *pattern++; /* copy character to literal */
338 /* we got a literal string, add it as a single-item list */
340 res = glob_fixed(glob, amount, glob->glob_buffer, sublen);
347 case '\0': /* done */
351 /* process set pattern */
354 res = glob_set(glob, &pattern, &pos, amount, globindex++);
358 /* process range pattern */
361 res = glob_range(glob, &pattern, &pos, amount, globindex++);
366 if(++glob->size > GLOB_PATTERN_NUM)
367 return GLOBERROR("too many globs", pos, GLOB_ERROR);
372 int glob_url(URLGlob** glob, char* url, unsigned long *urlnum, FILE *error)
375 * We can deal with any-size, just make a buffer with the same length
376 * as the specified URL!
378 URLGlob *glob_expand;
379 unsigned long amount = 0;
385 glob_buffer = malloc(strlen(url) + 1);
387 return CURLE_OUT_OF_MEMORY;
389 glob_expand = calloc(1, sizeof(URLGlob));
391 Curl_safefree(glob_buffer);
392 return CURLE_OUT_OF_MEMORY;
394 glob_expand->urllen = strlen(url);
395 glob_expand->glob_buffer = glob_buffer;
397 res = glob_parse(glob_expand, url, 1, &amount);
401 if(error && glob_expand->error) {
404 if(glob_expand->pos) {
405 snprintf(text, sizeof(text), "%s in column %zu", glob_expand->error,
410 t = glob_expand->error;
412 /* send error description to the error-stream */
413 fprintf(error, "curl: (%d) [globbing] %s\n", res, t);
415 /* it failed, we cleanup */
416 glob_cleanup(glob_expand);
425 void glob_cleanup(URLGlob* glob)
430 for(i = glob->size - 1; i < glob->size; --i) {
431 if((glob->pattern[i].type == UPTSet) &&
432 (glob->pattern[i].content.Set.elements)) {
433 for(elem = glob->pattern[i].content.Set.size - 1;
436 Curl_safefree(glob->pattern[i].content.Set.elements[elem]);
438 Curl_safefree(glob->pattern[i].content.Set.elements);
441 Curl_safefree(glob->glob_buffer);
445 int glob_next_url(char **globbed, URLGlob *glob)
451 size_t buflen = glob->urllen + 1;
452 char *buf = glob->glob_buffer;
461 /* implement a counter over the index ranges of all patterns,
462 starting with the rightmost pattern */
463 for(i = glob->size - 1; carry && (i < glob->size); --i) {
465 pat = &glob->pattern[i];
468 if((pat->content.Set.elements) &&
469 (++pat->content.Set.ptr_s == pat->content.Set.size)) {
470 pat->content.Set.ptr_s = 0;
475 pat->content.CharRange.ptr_c = (char)(pat->content.CharRange.step +
476 (int)((unsigned char)pat->content.CharRange.ptr_c));
477 if(pat->content.CharRange.ptr_c > pat->content.CharRange.max_c) {
478 pat->content.CharRange.ptr_c = pat->content.CharRange.min_c;
483 pat->content.NumRange.ptr_n += pat->content.NumRange.step;
484 if(pat->content.NumRange.ptr_n > pat->content.NumRange.max_n) {
485 pat->content.NumRange.ptr_n = pat->content.NumRange.min_n;
490 printf("internal error: invalid pattern type (%d)\n", (int)pat->type);
491 return CURLE_FAILED_INIT;
494 if(carry) { /* first pattern ptr has run into overflow, done! */
495 /* TODO: verify if this should actally return CURLE_OK. */
496 return CURLE_OK; /* CURLE_OK to match previous behavior */
500 for(j = 0; j < glob->size; ++j) {
501 pat = &glob->pattern[j];
504 if(pat->content.Set.elements) {
505 len = strlen(pat->content.Set.elements[pat->content.Set.ptr_s]);
506 snprintf(buf, buflen, "%s",
507 pat->content.Set.elements[pat->content.Set.ptr_s]);
513 *buf++ = pat->content.CharRange.ptr_c;
516 len = snprintf(buf, buflen, "%0*ld",
517 pat->content.NumRange.padlength,
518 pat->content.NumRange.ptr_n);
523 printf("internal error: invalid pattern type (%d)\n", (int)pat->type);
524 return CURLE_FAILED_INIT;
529 *globbed = strdup(glob->glob_buffer);
531 return CURLE_OUT_OF_MEMORY;
536 int glob_match_url(char **result, char *filename, URLGlob *glob)
541 char *appendthis = NULL;
542 size_t appendlen = 0;
543 size_t stringlen = 0;
547 /* We cannot use the glob_buffer for storage here since the filename may
548 * be longer than the URL we use. We allocate a good start size, then
549 * we need to realloc in case of need.
551 allocsize = strlen(filename) + 1; /* make it at least one byte to store the
553 target = malloc(allocsize);
555 return CURLE_OUT_OF_MEMORY;
558 if(*filename == '#' && ISDIGIT(filename[1])) {
560 char *ptr = filename;
561 unsigned long num = strtoul(&filename[1], &filename, 10);
562 URLPattern *pat =NULL;
564 if(num < glob->size) {
565 num--; /* make it zero based */
566 /* find the correct glob entry */
567 for(i=0; i<glob->size; i++) {
568 if(glob->pattern[i].globindex == (int)num) {
569 pat = &glob->pattern[i];
578 if(pat->content.Set.elements) {
579 appendthis = pat->content.Set.elements[pat->content.Set.ptr_s];
581 strlen(pat->content.Set.elements[pat->content.Set.ptr_s]);
585 numbuf[0] = pat->content.CharRange.ptr_c;
591 snprintf(numbuf, sizeof(numbuf), "%0*d",
592 pat->content.NumRange.padlength,
593 pat->content.NumRange.ptr_n);
595 appendlen = strlen(numbuf);
598 fprintf(stderr, "internal error: invalid pattern type (%d)\n",
600 Curl_safefree(target);
601 return CURLE_FAILED_INIT;
605 /* #[num] out of range, use the #[num] in the output */
607 appendthis = filename++;
612 appendthis = filename++;
615 if(appendlen + stringlen >= allocsize) {
617 /* we append a single byte to allow for the trailing byte to be appended
618 at the end of this function outside the while() loop */
619 allocsize = (appendlen + stringlen) * 2;
620 newstr = realloc(target, allocsize + 1);
622 Curl_safefree(target);
623 return CURLE_OUT_OF_MEMORY;
627 memcpy(&target[stringlen], appendthis, appendlen);
628 stringlen += appendlen;
630 target[stringlen]= '\0';