diff options
author | Stef Walter <stefw@gnome.org> | 2013-05-21 16:41:09 +0200 |
---|---|---|
committer | Stef Walter <stefw@gnome.org> | 2013-05-27 10:46:11 +0200 |
commit | cb8f2e3a04d9365121ffea0d76d8b3d47e2cc1ec (patch) | |
tree | 0f1b8a6d70ef15f4e3154def0f4a316fb717cce6 /common | |
parent | 4fd057258177f4f14bbe78c2d02d5a65eaf3f3dc (diff) |
url: Encode directly to a buffer
Diffstat (limited to 'common')
-rw-r--r-- | common/tests/test-url.c | 30 | ||||
-rw-r--r-- | common/url.c | 29 | ||||
-rw-r--r-- | common/url.h | 5 |
3 files changed, 31 insertions, 33 deletions
diff --git a/common/tests/test-url.c b/common/tests/test-url.c index 4c62594..892bf3c 100644 --- a/common/tests/test-url.c +++ b/common/tests/test-url.c @@ -120,28 +120,34 @@ static void test_encode (void) { const unsigned char *input = (unsigned char *)"TEST"; - char *encoded; - size_t length; + p11_buffer buf; + + if (!p11_buffer_init_null (&buf, 5)) + assert_not_reached (); - encoded = p11_url_encode (input, input + 5, "", &length); - assert_str_eq ("%54%45%53%54%00", (char *)encoded); - assert_num_eq (15, length); + p11_url_encode (input, input + 5, "", &buf); + assert (p11_buffer_ok (&buf)); + assert_str_eq ("%54%45%53%54%00", (char *)buf.data); + assert_num_eq (15, buf.len); - free (encoded); + p11_buffer_uninit (&buf); } static void test_encode_verbatim (void) { const unsigned char *input = (unsigned char *)"TEST"; - char *encoded; - size_t length; + p11_buffer buf; + + if (!p11_buffer_init_null (&buf, 5)) + assert_not_reached (); - encoded = p11_url_encode (input, input + 5, "ES", &length); - assert_str_eq ("%54ES%54%00", (char *)encoded); - assert_num_eq (11, length); + p11_url_encode (input, input + 5, "ES", &buf); + assert (p11_buffer_ok (&buf)); + assert_str_eq ("%54ES%54%00", (char *)buf.data); + assert_num_eq (11, buf.len); - free (encoded); + p11_buffer_uninit (&buf); } int diff --git a/common/url.c b/common/url.c index 6ccf74d..4b7e47b 100644 --- a/common/url.c +++ b/common/url.c @@ -103,40 +103,31 @@ p11_url_decode (const char *value, return result; } -char * +void p11_url_encode (const unsigned char *value, const unsigned char *end, const char *verbatim, - size_t *length) + p11_buffer *buf) { - char *p; - char *result; + char hex[3]; assert (value <= end); - /* Just allocate for worst case */ - result = malloc (((end - value) * 3) + 1); - return_val_if_fail (result != NULL, NULL); - /* Now loop through looking for escapes */ - p = result; while (value != end) { /* These characters we let through verbatim */ if (*value && strchr (verbatim, *value) != NULL) { - *(p++) = *(value++); + p11_buffer_add (buf, value, 1); /* All others get encoded */ } else { - *(p++) = '%'; - *(p++) = HEX_CHARS[((unsigned char)*value) >> 4]; - *(p++) = HEX_CHARS[((unsigned char)*value) & 0x0F]; - ++value; + hex[0] = '%'; + hex[1] = HEX_CHARS[((unsigned char)*value) >> 4]; + hex[2] = HEX_CHARS[((unsigned char)*value) & 0x0F]; + p11_buffer_add (buf, hex, 3); } - } - *p = 0; - if (length) - *length = p - result; - return result; + ++value; + } } diff --git a/common/url.h b/common/url.h index fa7938a..4ab1e43 100644 --- a/common/url.h +++ b/common/url.h @@ -36,6 +36,7 @@ #ifndef P11_URL_H #define P11_URL_H +#include "buffer.h" #include "compat.h" #include <stdlib.h> @@ -51,9 +52,9 @@ unsigned char * p11_url_decode (const char *value, const char *skip, size_t *length); -char * p11_url_encode (const unsigned char *value, +void p11_url_encode (const unsigned char *value, const unsigned char *end, const char *verbatim, - size_t *length); + p11_buffer *buf); #endif /* P11_URL_H */ |