* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
- * Copyright (C) 1998 - 2013, Daniel Stenberg, <daniel@haxx.se>, et al.
+ * Copyright (C) 1998 - 2014, Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
#include "memdebug.h" /* keep this as LAST include */
-typedef enum {
- GLOB_OK,
- GLOB_NO_MEM = CURLE_OUT_OF_MEMORY,
- GLOB_ERROR = CURLE_URL_MALFORMAT
-} GlobCode;
-
#define GLOBERROR(string, column, code) \
- glob->error = string, glob->pos = column, code;
+ glob->error = string, glob->pos = column, code
void glob_cleanup(URLGlob* glob);
-static GlobCode glob_fixed(URLGlob *glob, unsigned long *amount)
+static CURLcode glob_fixed(URLGlob *glob, char *fixed, size_t len)
{
URLPattern *pat = &glob->pattern[glob->size];
pat->type = UPTSet;
pat->content.Set.ptr_s = 0;
pat->globindex = -1;
- (*amount)++;
-
pat->content.Set.elements = malloc(sizeof(char*));
if(!pat->content.Set.elements)
- return GLOBERROR("out of memory", 0, GLOB_NO_MEM);
+ return GLOBERROR("out of memory", 0, CURLE_OUT_OF_MEMORY);
- pat->content.Set.elements[0] = strdup(glob->glob_buffer);
+ pat->content.Set.elements[0] = malloc(len+1);
if(!pat->content.Set.elements[0])
- return GLOBERROR("out of memory", 0, GLOB_NO_MEM);
+ return GLOBERROR("out of memory", 0, CURLE_OUT_OF_MEMORY);
+
+ memcpy(pat->content.Set.elements[0], fixed, len);
+ pat->content.Set.elements[0][len] = 0;
- return GLOB_OK;
+ return CURLE_OK;
}
/* multiply
return 0;
}
-static GlobCode glob_set(URLGlob *glob, char **patternp,
+static CURLcode glob_set(URLGlob *glob, char **patternp,
size_t *posp, unsigned long *amount,
int globindex)
{
while(!done) {
switch (*pattern) {
case '\0': /* URL ended while set was still open */
- return GLOBERROR("unmatched brace", opos, GLOB_ERROR);
+ return GLOBERROR("unmatched brace", opos, CURLE_URL_MALFORMAT);
case '{':
case '[': /* no nested expressions at this time */
- return GLOBERROR("nested brace", *posp, GLOB_ERROR);
+ return GLOBERROR("nested brace", *posp, CURLE_URL_MALFORMAT);
case '}': /* set element completed */
if(opattern == pattern)
- return GLOBERROR("empty string within braces", *posp, GLOB_ERROR);
+ return GLOBERROR("empty string within braces", *posp,
+ CURLE_URL_MALFORMAT);
/* add 1 to size since it'll be incremented below */
if(multiply(amount, pat->content.Set.size+1))
- return GLOBERROR("range overflow", 0, GLOB_ERROR);
+ return GLOBERROR("range overflow", 0, CURLE_URL_MALFORMAT);
/* fall-through */
case ',':
char **new_arr = realloc(pat->content.Set.elements,
(pat->content.Set.size + 1) * sizeof(char*));
if(!new_arr)
- return GLOBERROR("out of memory", 0, GLOB_NO_MEM);
+ return GLOBERROR("out of memory", 0, CURLE_OUT_OF_MEMORY);
pat->content.Set.elements = new_arr;
}
pat->content.Set.elements = malloc(sizeof(char*));
if(!pat->content.Set.elements)
- return GLOBERROR("out of memory", 0, GLOB_NO_MEM);
+ return GLOBERROR("out of memory", 0, CURLE_OUT_OF_MEMORY);
pat->content.Set.elements[pat->content.Set.size] =
strdup(glob->glob_buffer);
if(!pat->content.Set.elements[pat->content.Set.size])
- return GLOBERROR("out of memory", 0, GLOB_NO_MEM);
+ return GLOBERROR("out of memory", 0, CURLE_OUT_OF_MEMORY);
++pat->content.Set.size;
if(*pattern == '}') {
break;
case ']': /* illegal closing bracket */
- return GLOBERROR("unexpected close bracket", *posp, GLOB_ERROR);
+ return GLOBERROR("unexpected close bracket", *posp, CURLE_URL_MALFORMAT);
case '\\': /* escaped character, skip '\' */
if(pattern[1]) {
}
*patternp = pattern; /* return with the new position */
- return GLOB_OK;
+ return CURLE_OK;
}
-static GlobCode glob_range(URLGlob *glob, char **patternp,
+static CURLcode glob_range(URLGlob *glob, char **patternp,
size_t *posp, unsigned long *amount,
int globindex)
{
}
}
else
- pattern+=3;
+ pattern += 4;
*posp += (pattern - *patternp);
if((rc != 2) || (min_c >= max_c) || ((max_c - min_c) > ('z' - 'a')) ||
(step < 0) )
/* the pattern is not well-formed */
- return GLOBERROR("bad range", *posp, GLOB_ERROR);
+ return GLOBERROR("bad range", *posp, CURLE_URL_MALFORMAT);
/* if there was a ":[num]" thing, use that as step or else use 1 */
pat->content.CharRange.step = step;
if(multiply(amount, (pat->content.CharRange.max_c -
pat->content.CharRange.min_c + 1)))
- return GLOBERROR("range overflow", *posp, GLOB_ERROR);
+ return GLOBERROR("range overflow", *posp, CURLE_URL_MALFORMAT);
}
else if(ISDIGIT(*pattern)) {
/* numeric range detected */
if(!endp || (min_n > max_n) || (step_n > (max_n - min_n)))
/* the pattern is not well-formed */
- return GLOBERROR("bad range", *posp, GLOB_ERROR);
+ return GLOBERROR("bad range", *posp, CURLE_URL_MALFORMAT);
/* typecasting to ints are fine here since we make sure above that we
are within 31 bits */
if(multiply(amount, (pat->content.NumRange.max_n -
pat->content.NumRange.min_n + 1)))
- return GLOBERROR("range overflow", *posp, GLOB_ERROR);
+ return GLOBERROR("range overflow", *posp, CURLE_URL_MALFORMAT);
}
else
- return GLOBERROR("bad range specification", *posp, GLOB_ERROR);
+ return GLOBERROR("bad range specification", *posp, CURLE_URL_MALFORMAT);
*patternp = pattern;
- return GLOB_OK;
+ return CURLE_OK;
}
-static GlobCode glob_parse(URLGlob *glob, char *pattern,
+static bool peek_ipv6(const char *str, size_t *skip)
+{
+ /*
+ * Scan for a potential IPv6 literal.
+ * - Valid globs contain a hyphen and <= 1 colon.
+ * - IPv6 literals contain no hyphens and >= 2 colons.
+ */
+ size_t i = 0;
+ size_t colons = 0;
+ if(str[i++] != '[') {
+ return FALSE;
+ }
+ for(;;) {
+ const char c = str[i++];
+ if(ISALNUM(c) || c == '.' || c == '%') {
+ /* ok */
+ }
+ else if(c == ':') {
+ colons++;
+ }
+ else if(c == ']') {
+ *skip = i;
+ return colons >= 2 ? TRUE : FALSE;
+ }
+ else {
+ return FALSE;
+ }
+ }
+}
+
+static CURLcode glob_parse(URLGlob *glob, char *pattern,
size_t pos, unsigned long *amount)
{
/* processes a literal string component of a URL
special characters '{' and '[' branch to set/range processing functions
*/
- char* buf = glob->glob_buffer;
- GlobCode res = GLOB_OK;
+ CURLcode res = CURLE_OK;
int globindex = 0; /* count "actual" globs */
+ *amount = 1;
+
while(*pattern && !res) {
- int sublen = 0;
- while(*pattern && *pattern != '{' && *pattern != '[') {
+ char *buf = glob->glob_buffer;
+ size_t sublen = 0;
+ while(*pattern && *pattern != '{') {
+ if(*pattern == '[') {
+ /* Skip over potential IPv6 literals. */
+ size_t skip;
+ if(peek_ipv6(pattern, &skip)) {
+ memcpy(buf, pattern, skip);
+ buf += skip;
+ pattern += skip;
+ sublen += skip;
+ continue;
+ }
+ break;
+ }
if(*pattern == '}' || *pattern == ']')
- return GLOBERROR("unmatched close brace/bracket", pos, GLOB_ERROR);
+ return GLOBERROR("unmatched close brace/bracket", pos,
+ CURLE_URL_MALFORMAT);
/* only allow \ to escape known "special letters" */
if(*pattern == '\\' &&
if(sublen) {
/* we got a literal string, add it as a single-item list */
*buf = '\0';
- res = glob_fixed(glob, amount);
+ res = glob_fixed(glob, glob->glob_buffer, sublen);
}
else {
- if(!*amount)
- *amount = 1;
-
switch (*pattern) {
case '\0': /* done */
break;
}
if(++glob->size > GLOB_PATTERN_NUM)
- return GLOBERROR("too many globs", pos, GLOB_ERROR);
+ return GLOBERROR("too many globs", pos, CURLE_URL_MALFORMAT);
}
return res;
}
-int glob_url(URLGlob** glob, char* url, unsigned long *urlnum, FILE *error)
+CURLcode glob_url(URLGlob** glob, char* url, unsigned long *urlnum,
+ FILE *error)
{
/*
* We can deal with any-size, just make a buffer with the same length
URLGlob *glob_expand;
unsigned long amount = 0;
char *glob_buffer;
- GlobCode res;
+ CURLcode res;
*glob = NULL;
size_t i;
int elem;
- for(i = glob->size - 1; i < glob->size; --i) {
+ for(i = 0; i < glob->size; i++) {
if((glob->pattern[i].type == UPTSet) &&
(glob->pattern[i].content.Set.elements)) {
for(elem = glob->pattern[i].content.Set.size - 1;
Curl_safefree(glob);
}
-int glob_next_url(char **globbed, URLGlob *glob)
+CURLcode glob_next_url(char **globbed, URLGlob *glob)
{
URLPattern *pat;
size_t i;
- size_t j;
size_t len;
size_t buflen = glob->urllen + 1;
char *buf = glob->glob_buffer;
else {
bool carry = TRUE;
- /* implement a counter over the index ranges of all patterns,
- starting with the rightmost pattern */
- for(i = glob->size - 1; carry && (i < glob->size); --i) {
+ /* implement a counter over the index ranges of all patterns, starting
+ with the rightmost pattern */
+ for(i = 0; carry && (i < glob->size); i++) {
carry = FALSE;
- pat = &glob->pattern[i];
+ pat = &glob->pattern[glob->size - 1 - i];
switch (pat->type) {
case UPTSet:
if((pat->content.Set.elements) &&
}
break;
case UPTCharRange:
- pat->content.CharRange.ptr_c = (char)(pat->content.CharRange.step +
- (int)((unsigned char)pat->content.CharRange.ptr_c));
+ pat->content.CharRange.ptr_c =
+ (char)(pat->content.CharRange.step +
+ (int)((unsigned char)pat->content.CharRange.ptr_c));
if(pat->content.CharRange.ptr_c > pat->content.CharRange.max_c) {
pat->content.CharRange.ptr_c = pat->content.CharRange.min_c;
carry = TRUE;
}
}
- for(j = 0; j < glob->size; ++j) {
- pat = &glob->pattern[j];
+ for(i = 0; i < glob->size; ++i) {
+ pat = &glob->pattern[i];
switch(pat->type) {
case UPTSet:
if(pat->content.Set.elements) {
return CURLE_OK;
}
-int glob_match_url(char **result, char *filename, URLGlob *glob)
+CURLcode glob_match_url(char **result, char *filename, URLGlob *glob)
{
char *target;
size_t allocsize;