]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
uri-util: Added public function for parsing single percent encoding instances.
authorStephan Bosch <stephan@rename-it.nl>
Sun, 8 May 2016 20:56:59 +0000 (22:56 +0200)
committerGitLab <gitlab@git.dovecot.net>
Mon, 16 May 2016 07:36:48 +0000 (10:36 +0300)
src/lib/uri-util.c
src/lib/uri-util.h

index c16c2a953c9d6429a8c0ec6f015b4001ef29b6a1..d8d5491a1c5fe9b27a9e1a39ddb2a85567ff75be 100644 (file)
@@ -91,12 +91,17 @@ static inline int _decode_hex_digit(const unsigned char digit)
        return -1;
 }
 
-static int ATTR_NULL(3)
-uri_parse_pct_encoded(struct uri_parser *parser, const unsigned char **p,
-                     const unsigned char *pend, unsigned char *ch_r)
+static int
+uri_parse_pct_encoded_data(struct uri_parser *parser,
+                     const unsigned char **p, const unsigned char *pend,
+                     unsigned char *ch_r) ATTR_NULL(3)
 {
        int value;
 
+       if (**p != '%' || (pend != NULL && *p >= pend))
+               return 0;
+       *p += 1;
+
        if (**p == 0 || *(*p+1) == 0 || (pend != NULL && *p+1 >= pend)) {
                parser->error = "Unexpected URI boundary after '%'";
                return -1;
@@ -128,16 +133,20 @@ uri_parse_pct_encoded(struct uri_parser *parser, const unsigned char **p,
        return 1;       
 }
 
+int uri_parse_pct_encoded(struct uri_parser *parser,
+                     unsigned char *ch_r)
+{
+       return uri_parse_pct_encoded_data
+               (parser, &parser->cur, parser->end, ch_r);
+}
+
 static int
 uri_parse_unreserved_char(struct uri_parser *parser, unsigned char *ch_r)
 {
-       if (*parser->cur == '%') {
-               parser->cur++;
-               if (uri_parse_pct_encoded(parser, &parser->cur,
-                                         parser->end, ch_r) <= 0)
-                       return -1;
-               return 1;
-       }
+       int ret;
+
+       if ((ret=uri_parse_pct_encoded(parser, ch_r)) != 0)
+               return ret;
 
        if ((*parser->cur & 0x80) != 0)
                return 0;
@@ -160,7 +169,6 @@ int uri_parse_unreserved(struct uri_parser *parser, string_t *part)
 
                if ((ret = uri_parse_unreserved_char(parser, &ch)) < 0)
                        return -1;
-       
                if (ret == 0)
                        break;
 
@@ -178,6 +186,7 @@ bool uri_data_decode(struct uri_parser *parser, const char *data,
        const unsigned char *p = (const unsigned char *)data;
        const unsigned char *pend = (const unsigned char *)until;
        string_t *decoded;
+       int ret;
 
        if (pend == NULL) {
                /* NULL means unlimited; solely rely on '\0' */
@@ -194,11 +203,10 @@ bool uri_data_decode(struct uri_parser *parser, const char *data,
        while (p < pend && *p != '\0') {
                unsigned char ch;
 
-               if (*p == '%') {
-                       p++;
-                       if (uri_parse_pct_encoded(parser, &p, NULL, &ch) <= 0)
+               if ((ret=uri_parse_pct_encoded_data
+                       (parser, &p, NULL, &ch)) != 0) {
+                       if (ret < 0)
                                return FALSE;
-
                        str_append_c(decoded, ch);
                } else {
                        str_append_c(decoded, *p);
index a716a1a814e4256403df4969136af5471d17e499..02e1b39a0c1a97b71fb6bb5818558a71d88b54da 100644 (file)
@@ -25,6 +25,8 @@ struct uri_parser {
        string_t *tmpbuf;
 };
 
+int uri_parse_pct_encoded(struct uri_parser *parser,
+                     unsigned char *ch_r);
 int uri_parse_unreserved(struct uri_parser *parser, string_t *part);
 
 bool uri_data_decode(struct uri_parser *parser, const char *data,