]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib: Reformat uri-util.c.
authorStephan Bosch <stephan.bosch@open-xchange.com>
Thu, 3 Dec 2020 20:12:56 +0000 (21:12 +0100)
committerAki Tuomi <aki.tuomi@open-xchange.com>
Mon, 17 Jan 2022 11:52:09 +0000 (13:52 +0200)
src/lib/uri-util.c

index 498bc88e4829f00d09613ad11e6268371845f186..4e73d6d7648c713da3e3c4aff86af096d9bed34a 100644 (file)
 #define URI_MAX_SCHEME_NAME_LEN 64
 
 /* Character lookup table
- *
- * unreserved    = ALPHA / DIGIT / "-" / "." / "_" / "~"     [bit0]
- * sub-delims    = "!" / "$" / "&" / "'" / "(" / ")"
- *               / "*" / "+" / "," / ";" / "="               [bit1]
- * gen-delims    = ":" / "/" / "?" / "#" / "[" / "]" / "@"   [bit2]
- * pchar         = unreserved / sub-delims / ":" / "@"       [bit0|bit1|bit3]
- * 'pfchar'      = unreserved / sub-delims / ":" / "@" / "/" 
- *                                                      [bit0|bit1|bit3|bit5]
- * 'uchar'       = unreserved / sub-delims / ":"             [bit0|bit1|bit4]
- * 'qchar'       = pchar / "/" / "?"               [bit0|bit1|bit3|bit5|bit6]
- *
+
+   unreserved    = ALPHA / DIGIT / "-" / "." / "_" / "~"     [bit0]
+   sub-delims    = "!" / "$" / "&" / "'" / "(" / ")"
+                 / "*" / "+" / "," / ";" / "="               [bit1]
+   gen-delims    = ":" / "/" / "?" / "#" / "[" / "]" / "@"   [bit2]
+   pchar         = unreserved / sub-delims / ":" / "@"       [bit0|bit1|bit3]
+   'pfchar'      = unreserved / sub-delims / ":" / "@" / "/"
+                                                        [bit0|bit1|bit3|bit5]
+   'uchar'       = unreserved / sub-delims / ":"             [bit0|bit1|bit4]
+   'qchar'       = pchar / "/" / "?"               [bit0|bit1|bit3|bit5|bit6]
  */
 
 #define CHAR_MASK_UNRESERVED (1<<0)
@@ -133,8 +132,8 @@ static inline int _decode_hex_digit(const unsigned char digit)
 
 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)
+                          const unsigned char **p, const unsigned char *pend,
+                          unsigned char *ch_r) ATTR_NULL(3)
 {
        int value;
 
@@ -147,18 +146,23 @@ uri_parse_pct_encoded_data(struct uri_parser *parser,
                return -1;
        }
 
-       if ((value = _decode_hex_digit(**p)) < 0) {
-               parser->error = p_strdup_printf(parser->pool,
+       value = _decode_hex_digit(**p);
+       if (value < 0) {
+               parser->error = p_strdup_printf(
+                       parser->pool,
                        "Expecting hex digit after '%%', but found '%c'", **p);
                return -1;
        }
-       
+
        *ch_r = (value & 0x0f) << 4;
        *p += 1;
-       
-       if ((value = _decode_hex_digit(**p)) < 0) {
-               parser->error = p_strdup_printf(parser->pool,
-                       "Expecting hex digit after '%%%c', but found '%c'",     *((*p)-1), **p);
+
+       value = _decode_hex_digit(**p);
+       if (value < 0) {
+               parser->error = p_strdup_printf(
+                       parser->pool,
+                       "Expecting hex digit after '%%%c', but found '%c'",
+                       *((*p)-1), **p);
                return -1;
        }
 
@@ -170,14 +174,13 @@ uri_parse_pct_encoded_data(struct uri_parser *parser,
                        "Percent encoding is not allowed to encode NUL character";
                return -1;
        }
-       return 1;       
+       return 1;
 }
 
-int uri_parse_pct_encoded(struct uri_parser *parser,
-                     unsigned char *ch_r)
+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);
+       return uri_parse_pct_encoded_data(parser, &parser->cur, parser->end,
+                                         ch_r);
 }
 
 static int
@@ -190,7 +193,7 @@ uri_parse_unreserved_char(struct uri_parser *parser, unsigned char *ch_r)
                *ch_r = *parser->cur;
                parser->cur++;
                return 1;
-       }                       
+       }
        return 0;
 }
 
@@ -202,7 +205,8 @@ int uri_parse_unreserved(struct uri_parser *parser, string_t *part)
                int ret;
                unsigned char ch = 0;
 
-               if ((ret = uri_parse_unreserved_char(parser, &ch)) < 0)
+               ret = uri_parse_unreserved_char(parser, &ch);
+               if (ret < 0)
                        return -1;
                if (ret == 0)
                        break;
@@ -223,10 +227,11 @@ int uri_parse_unreserved_pct(struct uri_parser *parser, string_t *part)
                int ret;
                unsigned char ch = 0;
 
-               if ((ret=uri_parse_pct_encoded(parser, &ch)) < 0)
+               ret = uri_parse_pct_encoded(parser, &ch);
+               if (ret < 0)
                        return -1;
                else if (ret == 0 &&
-                       (ret=uri_parse_unreserved_char(parser, &ch)) < 0)
+                       (ret = uri_parse_unreserved_char(parser, &ch)) < 0)
                        return -1;
                if (ret == 0)
                        break;
@@ -251,19 +256,19 @@ bool uri_data_decode(struct uri_parser *parser, const char *data,
                /* NULL means unlimited; solely rely on '\0' */
                pend = (const unsigned char *)SIZE_MAX;
        }
-       
+
        if (p >= pend || *p == '\0') {
                if (decoded_r != NULL)
                        *decoded_r = "";
                return TRUE;
        }
-       
+
        decoded = uri_parser_get_tmpbuf(parser, 256);
        while (p < pend && *p != '\0') {
                unsigned char ch;
 
-               if ((ret=uri_parse_pct_encoded_data
-                       (parser, &p, NULL, &ch)) != 0) {
+               ret = uri_parse_pct_encoded_data(parser, &p, NULL, &ch);
+               if (ret != 0) {
                        if (ret < 0)
                                return FALSE;
                        str_append_c(decoded, ch);
@@ -282,25 +287,26 @@ int uri_parse_scheme(struct uri_parser *parser, const char **scheme_r)
 {
        const unsigned char *first = parser->cur;
        size_t len = 1;
-       
+
        /* RFC 3968:
-        *   scheme  = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
+
+            scheme  = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
         */
-       
+
        if (parser->cur >= parser->end || !i_isalpha(*parser->cur))
                return 0;
        parser->cur++;
-               
+
        while (len < URI_MAX_SCHEME_NAME_LEN &&
                parser->cur < parser->end) {
                if (!i_isalnum(*parser->cur) &&
-                       *parser->cur != '+' && *parser->cur != '-' &&
-                       *parser->cur != '.')
+                   *parser->cur != '+' && *parser->cur != '-' &&
+                   *parser->cur != '.')
                        break;
                parser->cur++;
                len++;
        }
-       
+
        if (parser->cur >= parser->end || *parser->cur != ':') {
                parser->error = "Invalid URI scheme";
                return -1;
@@ -330,12 +336,12 @@ uri_parse_dec_octet(struct uri_parser *parser, string_t *literal,
        int count = 0;
 
        /* RFC 3986:
-        *
-        * dec-octet     = DIGIT                 ; 0-9
-        *               / %x31-39 DIGIT         ; 10-99
-        *               / "1" 2DIGIT            ; 100-199
-        *               / "2" %x30-34 DIGIT     ; 200-249
-        *               / "25" %x30-35          ; 250-255
+
+          dec-octet     = DIGIT                 ; 0-9
+                        / %x31-39 DIGIT         ; 10-99
+                        / "1" 2DIGIT            ; 100-199
+                        / "2" %x30-34 DIGIT     ; 200-249
+                        / "25" %x30-35          ; 250-255
         */
 
        while (parser->cur < parser->end && i_isdigit(*parser->cur)) {
@@ -367,14 +373,15 @@ uri_parse_ipv4address(struct uri_parser *parser, string_t *literal,
        int i;
 
        /* RFC 3986:
-        *
-        * IPv4address   = dec-octet "." dec-octet "." dec-octet "." dec-octet
+
+          IPv4address   = dec-octet "." dec-octet "." dec-octet "." dec-octet
         */
 
-       if ((ret = uri_parse_dec_octet(parser, literal, &octet)) <= 0)
+       ret = uri_parse_dec_octet(parser, literal, &octet);
+       if (ret <= 0)
                return ret;
        ip = octet;
-       
+
        for (i = 0; i < 3 && parser->cur < parser->end; i++) {
                if (*parser->cur != '.')
                        return -1;
@@ -387,30 +394,31 @@ uri_parse_ipv4address(struct uri_parser *parser, string_t *literal,
                        return -1;
                ip = (ip << 8) + octet;
        }
-       
+
        if (ip4_r != NULL)
                ip4_r->s_addr = htonl(ip);
        return 1;
 }
 
 static int
-uri_do_parse_reg_name(struct uri_parser *parser,
-       string_t *reg_name) ATTR_NULL(2)
+uri_do_parse_reg_name(struct uri_parser *parser, string_t *reg_name)
+                     ATTR_NULL(2)
 {
        /* RFC 3986:
-        *
-        * reg-name      = *( unreserved / pct-encoded / sub-delims )
-       */
+
+          reg-name      = *( unreserved / pct-encoded / sub-delims )
+        */
 
        while (parser->cur < parser->end) {
                int ret;
                unsigned char c;
 
                /* unreserved / pct-encoded */
-               if ((ret=uri_parse_pct_encoded(parser, &c)) < 0)
+               ret = uri_parse_pct_encoded(parser, &c);
+               if (ret < 0)
                        return -1;
                else if (ret == 0 &&
-                       (ret=uri_parse_unreserved_char(parser, &c)) < 0)
+                       (ret = uri_parse_unreserved_char(parser, &c)) < 0)
                        return -1;
 
                if (ret > 0) {
@@ -421,7 +429,8 @@ uri_do_parse_reg_name(struct uri_parser *parser,
 
                /* sub-delims */
                c = *parser->cur;
-               if ((c & 0x80) == 0 && (_uri_char_lookup[c] & CHAR_MASK_SUB_DELIMS) != 0) {
+               if ((c & 0x80) == 0 &&
+                   (_uri_char_lookup[c] & CHAR_MASK_SUB_DELIMS) != 0) {
                        if (reg_name != NULL)
                                str_append_c(reg_name, *parser->cur);
                        parser->cur++;
@@ -432,8 +441,7 @@ uri_do_parse_reg_name(struct uri_parser *parser,
        return 0;
 }
 
-int uri_parse_reg_name(struct uri_parser *parser,
-       const char **reg_name_r)
+int uri_parse_reg_name(struct uri_parser *parser, const char **reg_name_r)
 {
        string_t *reg_name = NULL;
        int ret;
@@ -441,7 +449,8 @@ int uri_parse_reg_name(struct uri_parser *parser,
        if (reg_name_r != NULL)
                reg_name = uri_parser_get_tmpbuf(parser, 256);
 
-       if ((ret=uri_do_parse_reg_name(parser, reg_name)) <= 0)
+       ret = uri_do_parse_reg_name(parser, reg_name);
+       if (ret <= 0)
                return ret;
 
        if (reg_name_r != NULL)
@@ -449,8 +458,9 @@ int uri_parse_reg_name(struct uri_parser *parser,
        return 1;
 }
 
-static int uri_do_parse_host_name(struct uri_parser *parser,
-       string_t *host_name) ATTR_NULL(2)
+static int
+uri_do_parse_host_name(struct uri_parser *parser, string_t *host_name)
+                      ATTR_NULL(2)
 {
        const unsigned char *first, *part;
        int ret;
@@ -493,7 +503,8 @@ static int uri_do_parse_host_name(struct uri_parser *parser,
                ch = pch = *parser->cur;
                if (parser->cur >= parser->end)
                        break;
-               if ((ret=uri_parse_pct_encoded(parser, &ch)) < 0) {
+               ret = uri_parse_pct_encoded(parser, &ch);
+               if (ret < 0) {
                        return -1;
                } else if (ret > 0) {
                        if (!i_isalnum(ch))
@@ -512,14 +523,18 @@ static int uri_do_parse_host_name(struct uri_parser *parser,
                        do {
                                offset = parser->cur;
 
-                               if ((ret=uri_parse_pct_encoded(parser, &ch)) < 0) {
+                               ret = uri_parse_pct_encoded(parser, &ch);
+                               if (ret < 0) {
                                        return -1;
                                } else if (ret > 0) {
                                        if (!i_isalnum(ch) && ch != '-')
                                                break;
                                        if (host_name != NULL) {
-                                               if (offset > part)
-                                                       str_append_data(host_name, part, offset - part);
+                                               if (offset > part) {
+                                                       str_append_data(
+                                                               host_name, part,
+                                                               offset - part);
+                                               }
                                                str_append_c(host_name, ch);
                                        }
                                        part = parser->cur;
@@ -533,7 +548,8 @@ static int uri_do_parse_host_name(struct uri_parser *parser,
                        } while (parser->cur < parser->end);
 
                        if (!i_isalnum(pch)) {
-                               parser->error = "Invalid domain label in hostname";
+                               parser->error =
+                                       "Invalid domain label in hostname";
                                return -1;
                        }
                }
@@ -554,7 +570,7 @@ static int uri_do_parse_host_name(struct uri_parser *parser,
        if (parser->cur == first)
                return 0;
 
-       /* remove trailing '.' */
+       /* Remove trailing '.' */
        if (host_name != NULL) {
                const char *name = str_c(host_name);
 
@@ -565,8 +581,7 @@ static int uri_do_parse_host_name(struct uri_parser *parser,
        return 1;
 }
 
-int uri_parse_host_name(struct uri_parser *parser,
-       const char **host_name_r)
+int uri_parse_host_name(struct uri_parser *parser, const char **host_name_r)
 {
        string_t *host_name = NULL;
        int ret;
@@ -574,7 +589,8 @@ int uri_parse_host_name(struct uri_parser *parser,
        if (host_name_r != NULL)
                host_name = uri_parser_get_tmpbuf(parser, 256);
 
-       if ((ret=uri_do_parse_host_name(parser, host_name)) <= 0)
+       ret = uri_do_parse_host_name(parser, host_name);
+       if (ret <= 0)
                return ret;
 
        if (host_name_r != NULL)
@@ -591,8 +607,8 @@ uri_parse_ip_literal(struct uri_parser *parser, string_t *literal,
        struct in6_addr ip6;
 
        /* IP-literal    = "[" ( IPv6address / IPvFuture  ) "]"
-        * IPvFuture     = "v" 1*HEXDIG "." 1*( unreserved / sub-delims / ":" )
-        * IPv6address   = ; Syntax not relevant: parsed using inet_pton()
+          IPvFuture     = "v" 1*HEXDIG "." 1*( unreserved / sub-delims / ":" )
+          IPv6address   = ; Syntax not relevant: parsed using inet_pton()
         */
 
        /* "[" already verified */
@@ -611,20 +627,22 @@ uri_parse_ip_literal(struct uri_parser *parser, string_t *literal,
        if (literal != NULL)
                str_append_data(literal, parser->cur, p-parser->cur+1);
        address = t_strdup_until(parser->cur+1, p);
-       parser->cur = p + 1;    
+       parser->cur = p + 1;
 
        if (*address == '\0') {
                parser->error = "Empty IPv6 host address";
                return -1;
        }
        if (*address == 'v') {
-               parser->error = p_strdup_printf(parser->pool,
+               parser->error = p_strdup_printf(
+                       parser->pool,
                        "Future IP host address '%s' not supported", address);
                return -1;
        }
        if (inet_pton(AF_INET6, address, &ip6) <= 0) {
-               parser->error = p_strdup_printf(parser->pool,
-                       "Invalid IPv6 host address '%s'", address);
+               parser->error = p_strdup_printf(
+                       parser->pool, "Invalid IPv6 host address '%s'",
+                       address);
                return -1;
        }
        if (ip6_r != NULL)
@@ -633,9 +651,8 @@ uri_parse_ip_literal(struct uri_parser *parser, string_t *literal,
 }
 
 static int
-uri_do_parse_host(struct uri_parser *parser,
-       struct uri_host *host, bool host_name)
-       ATTR_NULL(2)
+uri_do_parse_host(struct uri_parser *parser, struct uri_host *host,
+                 bool host_name) ATTR_NULL(2)
 {
        const unsigned char *preserve;
        struct in_addr ip4;
@@ -644,8 +661,8 @@ uri_do_parse_host(struct uri_parser *parser,
        int ret;
 
        /* RFC 3986:
-        *
-        * host          = IP-literal / IPv4address / reg-name
+
+          host          = IP-literal / IPv4address / reg-name
         */
 
        if (host != NULL)
@@ -667,11 +684,12 @@ uri_do_parse_host(struct uri_parser *parser,
        }
 
        /* IPv4address /
-        *
-        * If it fails to parse, we try to parse it as a reg-name
+
+          If it fails to parse, we try to parse it as a reg-name
         */
        preserve = parser->cur;
-       if ((ret = uri_parse_ipv4address(parser, literal, &ip4)) > 0) {
+       ret = uri_parse_ipv4address(parser, literal, &ip4);
+       if (ret > 0) {
                if (host != NULL) {
                        host->name = p_strdup(parser->pool, str_c(literal));
                        host->ip.family = AF_INET;
@@ -693,22 +711,21 @@ uri_do_parse_host(struct uri_parser *parser,
        return 0;
 }
 
-int uri_parse_host(struct uri_parser *parser,
-       struct uri_host *host)
+int uri_parse_host(struct uri_parser *parser, struct uri_host *host)
 {
        return uri_do_parse_host(parser, host, TRUE);
 }
 
 static int
-uri_parse_port(struct uri_parser *parser,
-       struct uri_authority *auth) ATTR_NULL(2)
+uri_parse_port(struct uri_parser *parser, struct uri_authority *auth)
+              ATTR_NULL(2)
 {
        const unsigned char *first;
        in_port_t port;
 
        /* RFC 3986:
-        *
-        * port        = *DIGIT
+
+          port        = *DIGIT
         */
 
        first = parser->cur;
@@ -728,40 +745,42 @@ uri_parse_port(struct uri_parser *parser,
 }
 
 static int
-uri_do_parse_authority(struct uri_parser *parser,
-       struct uri_authority *auth, bool host_name) ATTR_NULL(2)
+uri_do_parse_authority(struct uri_parser *parser, struct uri_authority *auth,
+                      bool host_name) ATTR_NULL(2)
 {
        const unsigned char *p;
        int ret;
-       
-       /*
-        * authority     = [ userinfo "@" ] host [ ":" port ]
+
+       /* authority     = [ userinfo "@" ] host [ ":" port ]
         */
 
        if (auth != NULL)
                i_zero(auth);
 
-       /* Scan ahead to check whether there is a [userinfo "@"] uri component */
+       /* Scan ahead to check whether there is a [userinfo "@"] uri component.
+        */
        for (p = parser->cur; p < parser->end; p++){
-               /* refuse 8bit characters */
+               /* Refuse 8bit characters */
                if ((*p & 0x80) != 0)
                        break;
 
-               /* break at first delimiter */
+               /* Break at first delimiter */
                if (*p != '%' && (_uri_char_lookup[*p] & CHAR_MASK_UCHAR) == 0)
                        break;
        }
 
-       /* Extract userinfo */  
+       /* Extract userinfo */
        if (p < parser->end && *p == '@') {
-               if (auth != NULL)
-                       auth->enc_userinfo = p_strdup_until(parser->pool, parser->cur, p);
+               if (auth != NULL) {
+                       auth->enc_userinfo =
+                               p_strdup_until(parser->pool, parser->cur, p);
+               }
                parser->cur = p+1;
        }
 
-       /* host */
-       if (uri_do_parse_host(parser,
-               (auth == NULL ? NULL : &auth->host), host_name) < 0)
+       /* Host */
+       if (uri_do_parse_host(parser, (auth == NULL ? NULL : &auth->host),
+                             host_name) < 0)
                return -1;
        if (parser->cur == parser->end)
                return 1;
@@ -776,8 +795,9 @@ uri_do_parse_authority(struct uri_parser *parser,
        /* [":" port] */
        if (*parser->cur == ':') {
                parser->cur++;
-       
-               if ((ret = uri_parse_port(parser, auth)) < 0)
+
+               ret = uri_parse_port(parser, auth);
+               if (ret < 0)
                        return ret;
                if (parser->cur == parser->end)
                        return 1;
@@ -795,8 +815,8 @@ uri_do_parse_authority(struct uri_parser *parser,
 
 static int
 uri_do_parse_slashslash_authority(struct uri_parser *parser,
-       struct uri_authority *auth, bool host_name)
-       ATTR_NULL(2)
+                                 struct uri_authority *auth, bool host_name)
+                                 ATTR_NULL(2)
 {
        /* "//" authority */
 
@@ -808,26 +828,25 @@ uri_do_parse_slashslash_authority(struct uri_parser *parser,
        return uri_do_parse_authority(parser, auth, host_name);
 }
 
-int uri_parse_authority(struct uri_parser *parser,
-       struct uri_authority *auth)
+int uri_parse_authority(struct uri_parser *parser, struct uri_authority *auth)
 {
        return uri_do_parse_authority(parser, auth, FALSE);
 }
 
 int uri_parse_slashslash_authority(struct uri_parser *parser,
-       struct uri_authority *auth)
+                                  struct uri_authority *auth)
 {
        return uri_do_parse_slashslash_authority(parser, auth, FALSE);
 }
 
 int uri_parse_host_authority(struct uri_parser *parser,
-       struct uri_authority *auth)
+                            struct uri_authority *auth)
 {
        return uri_do_parse_authority(parser, auth, TRUE);
 }
 
 int uri_parse_slashslash_host_authority(struct uri_parser *parser,
-       struct uri_authority *auth)
+                                       struct uri_authority *auth)
 {
        return uri_do_parse_slashslash_authority(parser, auth, TRUE);
 }
@@ -840,23 +859,23 @@ int uri_parse_path_segment(struct uri_parser *parser, const char **segment_r)
        while (parser->cur < parser->end) {
                if (*parser->cur == '%') {
                        unsigned char ch = 0;
-                       if ((ret=uri_parse_pct_encoded(parser, &ch)) < 0)
+                       ret = uri_parse_pct_encoded(parser, &ch);
+                       if (ret < 0)
                                return -1;
                        if (ret > 0)
                                continue;
                }
 
                if ((*parser->cur & 0x80) != 0 ||
-                       (_uri_char_lookup[*parser->cur] & CHAR_MASK_PCHAR) == 0)
+                   (_uri_char_lookup[*parser->cur] & CHAR_MASK_PCHAR) == 0)
                        break;
 
                parser->cur++;
        }
 
-       if (parser->cur < parser->end &&
-               *parser->cur != '/' && *parser->cur != '?' && *parser->cur != '#' ) {
-               parser->error =
-                       "Path component contains invalid character";
+       if (parser->cur < parser->end && *parser->cur != '/' &&
+           *parser->cur != '?' && *parser->cur != '#') {
+               parser->error = "Path component contains invalid character";
                return -1;
        }
 
@@ -884,21 +903,22 @@ int uri_parse_path(struct uri_parser *parser,
        else
                i_zero(&segments);
 
-       /* check for a leading '/' and indicate absolute path
-          when it is present
+       /* Check for a leading '/' and indicate absolute path when it is
+          present.
         */
        if (parser->cur < parser->end && *parser->cur == '/') {
                parser->cur++;
                relative = 0;
        }
-       
-       /* parse first segment */
-       if ((ret = uri_parse_path_segment(parser, &segment)) < 0)
+
+       /* Parse first segment */
+       ret = uri_parse_path_segment(parser, &segment);
+       if (ret < 0)
                return -1;
-       
+
        for (;;) {
                if (ret > 0) {
-                       /* strip dot segments */
+                       /* Strip dot segments */
                        if (segment[0] == '.') {
                                if (segment[1] == '.') {
                                        if (segment[2] == '\0') {
@@ -935,8 +955,9 @@ int uri_parse_path(struct uri_parser *parser,
                        break;
                parser->cur++;
 
-               /* parse next path segment */
-               if ((ret = uri_parse_path_segment(parser, &segment)) < 0)
+               /* Parse next path segment */
+               ret = uri_parse_path_segment(parser, &segment);
+               if (ret < 0)
                        return -1;
        }
 
@@ -946,12 +967,12 @@ int uri_parse_path(struct uri_parser *parser,
                *path_r = NULL;
 
        if (parser->cur == pbegin) {
-               /* path part of URI is empty */
+               /* Path part of URI is empty */
                return 0;
        }
 
        if (path_r != NULL) {
-               /* special treatment for a trailing '..' or '.' */
+               /* Special treatment for a trailing '..' or '.' */
                if (segment == NULL) {
                        segment = "";
                        array_push_back(&segments, &segment);
@@ -960,7 +981,7 @@ int uri_parse_path(struct uri_parser *parser,
                *path_r = array_get(&segments, &count);
        }
        if (parser->cur < parser->end &&
-               *parser->cur != '?' && *parser->cur != '#') {
+           *parser->cur != '?' && *parser->cur != '#') {
                parser->error = "Path component contains invalid character";
                return -1;
        }
@@ -973,10 +994,10 @@ int uri_parse_query(struct uri_parser *parser, const char **query_r)
        int ret;
 
        /* RFC 3986:
-        *
-        * URI           = { ... } [ "?" query ] { ... }
-        * query         = *( pchar / "/" / "?" )
-        * pchar         = unreserved / pct-encoded / sub-delims / ":" / "@"
+
+          URI           = { ... } [ "?" query ] { ... }
+          query         = *( pchar / "/" / "?" )
+          pchar         = unreserved / pct-encoded / sub-delims / ":" / "@"
         */
        if (parser->cur >= parser->end || *parser->cur != '?')
                return 0;
@@ -985,14 +1006,16 @@ int uri_parse_query(struct uri_parser *parser, const char **query_r)
        while (parser->cur < parser->end) {
                if (*parser->cur == '%') {
                        unsigned char ch = 0;
-                       if ((ret=uri_parse_pct_encoded(parser, &ch)) < 0)
+
+                       ret = uri_parse_pct_encoded(parser, &ch);
+                       if (ret < 0)
                                return -1;
                        if (ret > 0)
                                continue;
                }
 
                if ((*parser->cur & 0x80) != 0 ||
-                       (_uri_char_lookup[*parser->cur] & CHAR_MASK_QCHAR) == 0)
+                   (_uri_char_lookup[*parser->cur] & CHAR_MASK_QCHAR) == 0)
                        break;
                parser->cur++;
        }
@@ -1013,10 +1036,10 @@ int uri_parse_fragment(struct uri_parser *parser, const char **fragment_r)
        int ret;
 
        /* RFC 3986:
-        *
-        * URI           = { ... } [ "#" fragment ]
-        * fragment      = *( pchar / "/" / "?" )
-        * pchar         = unreserved / pct-encoded / sub-delims / ":" / "@"
+
+          URI           = { ... } [ "#" fragment ]
+          fragment      = *( pchar / "/" / "?" )
+          pchar         = unreserved / pct-encoded / sub-delims / ":" / "@"
         */
 
        if (parser->cur >= parser->end || *parser->cur != '#')
@@ -1026,14 +1049,16 @@ int uri_parse_fragment(struct uri_parser *parser, const char **fragment_r)
        while (parser->cur < parser->end) {
                if (*parser->cur == '%') {
                        unsigned char ch = 0;
-                       if ((ret=uri_parse_pct_encoded(parser, &ch)) < 0)
+
+                       ret = uri_parse_pct_encoded(parser, &ch);
+                       if (ret < 0)
                                return -1;
                        if (ret > 0)
                                continue;
                }
 
                if ((*parser->cur & 0x80) != 0 ||
-                       (_uri_char_lookup[*parser->cur] & CHAR_MASK_QCHAR) == 0)
+                   (_uri_char_lookup[*parser->cur] & CHAR_MASK_QCHAR) == 0)
                        break;
                parser->cur++;
        }
@@ -1043,13 +1068,15 @@ int uri_parse_fragment(struct uri_parser *parser, const char **fragment_r)
                return -1;
        }
 
-       if (fragment_r != NULL)
-               *fragment_r = p_strdup_until(parser->pool, first+1, parser->cur);
+       if (fragment_r != NULL) {
+               *fragment_r = p_strdup_until(parser->pool, first+1,
+                                            parser->cur);
+       }
        return 1;
 }
 
-void uri_parser_init_data(struct uri_parser *parser,
-       pool_t pool, const unsigned char *data, size_t size)
+void uri_parser_init_data(struct uri_parser *parser, pool_t pool,
+                         const unsigned char *data, size_t size)
 {
        i_zero(parser);
        parser->pool = pool;
@@ -1057,11 +1084,10 @@ void uri_parser_init_data(struct uri_parser *parser,
        parser->end = data + size;
 }
 
-void uri_parser_init(struct uri_parser *parser,
-       pool_t pool, const char *uri)
+void uri_parser_init(struct uri_parser *parser, pool_t pool, const char *uri)
 {
-       uri_parser_init_data
-               (parser, pool, (const unsigned char *)uri, strlen(uri));
+       uri_parser_init_data(parser, pool,
+                            (const unsigned char *)uri, strlen(uri));
 }
 
 string_t *uri_parser_get_tmpbuf(struct uri_parser *parser, size_t size)
@@ -1074,12 +1100,11 @@ string_t *uri_parser_get_tmpbuf(struct uri_parser *parser, size_t size)
 }
 
 int uri_parse_absolute_generic(struct uri_parser *parser,
-       enum uri_parse_flags flags)
+                              enum uri_parse_flags flags)
 {
        int relative, aret, ret = 0;
 
-       /*
-          URI           = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
+       /* URI           = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
 
           hier-part     = "//" authority path-abempty
                               / path-absolute
@@ -1096,15 +1121,15 @@ int uri_parse_absolute_generic(struct uri_parser *parser,
 
        /* scheme ":" */
        if ((flags & URI_PARSE_SCHEME_EXTERNAL) == 0 &&
-               (ret=uri_parse_scheme(parser, NULL)) <= 0) {
+           (ret = uri_parse_scheme(parser, NULL)) <= 0) {
                if (ret == 0)
                        parser->error = "Missing scheme";
                return -1;
        }
 
        /* "//" authority */
-       if ((aret=uri_parse_slashslash_authority
-               (parser, NULL)) < 0)
+       aret = uri_parse_slashslash_authority(parser, NULL);
+       if (aret < 0)
                return -1;
 
        /* path-absolute / path-rootless / path-empty */
@@ -1112,7 +1137,7 @@ int uri_parse_absolute_generic(struct uri_parser *parser,
                ret = uri_parse_path(parser, &relative, NULL);
        /* path-abempty */
        } else if (parser->cur < parser->end && *parser->cur == '/') {
-               ret = uri_parse_path(parser,    &relative, NULL);
+               ret = uri_parse_path(parser, &relative, NULL);
                i_assert(ret <= 0 || relative == 0);
        }
        if (ret < 0)
@@ -1123,7 +1148,8 @@ int uri_parse_absolute_generic(struct uri_parser *parser,
                return -1;
 
        /* [ "#" fragment ] */
-       if ((ret=uri_parse_fragment(parser, NULL)) < 0)
+       ret = uri_parse_fragment(parser, NULL);
+       if (ret < 0)
                return ret;
        if (ret > 0 && (flags & URI_PARSE_ALLOW_FRAGMENT_PART) == 0) {
                parser->error = "Fragment part not allowed";
@@ -1139,11 +1165,11 @@ int uri_parse_absolute_generic(struct uri_parser *parser,
  */
 
 void uri_host_copy(pool_t pool, struct uri_host *dest,
-       const struct uri_host *src)
+                  const struct uri_host *src)
 {
        const char *host_name = src->name;
 
-       /* create host name literal if caller is lazy */
+       /* Create host name literal if caller is lazy */
        if (host_name == NULL && src->ip.family != 0) {
                host_name = net_ip2addr(&src->ip);
                i_assert(*host_name != '\0');
@@ -1158,7 +1184,7 @@ void uri_host_copy(pool_t pool, struct uri_host *dest,
  */
 
 int uri_check_data(const unsigned char *data, size_t size,
-       enum uri_parse_flags flags, const char **error_r)
+                  enum uri_parse_flags flags, const char **error_r)
 {
        struct uri_parser parser;
        int ret;
@@ -1173,28 +1199,27 @@ int uri_check_data(const unsigned char *data, size_t size,
        return ret;
 }
 
-int uri_check(const char *uri, enum uri_parse_flags flags,
-       const char **error_r)
+int uri_check(const char *uri, enum uri_parse_flags flags, const char **error_r)
 {
-       return uri_check_data
-               ((const unsigned char *)uri, strlen(uri), flags, error_r);
+       return uri_check_data((const unsigned char *)uri, strlen(uri),
+                             flags, error_r);
 }
 
 /*
  * Generic URI construction
  */
 
-void uri_data_encode(string_t *out,
-       const unsigned char esc_table[256],
-       unsigned char esc_mask, const char *esc_extra,
-       const char *data)
+void uri_data_encode(string_t *out, const unsigned char esc_table[256],
+                    unsigned char esc_mask, const char *esc_extra,
+                    const char *data)
 {
        const unsigned char *pbegin, *p;
 
        pbegin = p = (const unsigned char *)data;
        while (*p != '\0') {
                if ((*p & 0x80) != 0 || (esc_table[*p] & esc_mask) == 0 ||
-                       (esc_extra != NULL && strchr(esc_extra, (char)*p) != NULL)) {
+                   (esc_extra != NULL &&
+                    strchr(esc_extra, (char)*p) != NULL)) {
                        if ((p - pbegin) > 0)
                                str_append_data(out, pbegin, p - pbegin);
                        str_printfa(out, "%%%02x", *p);
@@ -1214,8 +1239,7 @@ void uri_append_scheme(string_t *out, const char *scheme)
        str_append_c(out, ':');
 }
 
-void uri_append_user_data(string_t *out, const char *esc,
-       const char *data)
+void uri_append_user_data(string_t *out, const char *esc, const char *data)
 {
        uri_data_encode(out, _uri_char_lookup, CHAR_MASK_UCHAR, esc, data);
 }
@@ -1229,7 +1253,8 @@ void uri_append_userinfo(string_t *out, const char *userinfo)
 void uri_append_host_name(string_t *out, const char *name)
 {
        uri_data_encode(out, _uri_char_lookup,
-                       CHAR_MASK_UNRESERVED | CHAR_MASK_SUB_DELIMS, NULL, name);
+                       CHAR_MASK_UNRESERVED | CHAR_MASK_SUB_DELIMS,
+                       NULL, name);
 }
 
 void uri_append_host_ip(string_t *out, const struct ip_addr *host_ip)
@@ -1252,7 +1277,7 @@ void uri_append_host_ip(string_t *out, const struct ip_addr *host_ip)
 void uri_append_host(string_t *out, const struct uri_host *host)
 {
        if (host->name != NULL) {
-               /* assume IPv6 literal if starts with '['; avoid encoding */
+               /* Assume IPv6 literal if starts with '['; avoid encoding */
                if (*host->name == '[')
                        str_append(out, host->name);
                else
@@ -1280,8 +1305,7 @@ void uri_append_path_segment(string_t *out, const char *segment)
                uri_append_path_data(out, NULL, segment);
 }
 
-void uri_append_path_data(string_t *out, const char *esc,
-                         const char *data)
+void uri_append_path_data(string_t *out, const char *esc, const char *data)
 {
        uri_data_encode(out, _uri_char_lookup, CHAR_MASK_PFCHAR, esc, data);
 }
@@ -1293,8 +1317,7 @@ void uri_append_path(string_t *out, const char *path)
                uri_append_path_data(out, NULL, path);
 }
 
-void uri_append_query_data(string_t *out, const char *esc,
-                          const char *data)
+void uri_append_query_data(string_t *out, const char *esc, const char *data)
 {
        uri_data_encode(out, _uri_char_lookup, CHAR_MASK_QCHAR, esc, data);
 }
@@ -1306,8 +1329,7 @@ void uri_append_query(string_t *out, const char *query)
                uri_append_query_data(out, NULL, query);
 }
 
-void uri_append_fragment_data(string_t *out, const char *esc,
-                             const char *data)
+void uri_append_fragment_data(string_t *out, const char *esc, const char *data)
 {
        uri_data_encode(out, _uri_char_lookup, CHAR_MASK_QCHAR, esc, data);
 }