summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--common/tests/test-url.c30
-rw-r--r--common/url.c29
-rw-r--r--common/url.h5
-rw-r--r--p11-kit/uri.c32
4 files changed, 50 insertions, 46 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 */
diff --git a/p11-kit/uri.c b/p11-kit/uri.c
index a811b92..df069f4 100644
--- a/p11-kit/uri.c
+++ b/p11-kit/uri.c
@@ -625,6 +625,18 @@ p11_kit_uri_new (void)
return uri;
}
+static void
+format_name_equals (p11_buffer *buffer,
+ bool *is_first,
+ const char *name)
+{
+ if (!*is_first)
+ p11_buffer_add (buffer, ";", 1);
+ p11_buffer_add (buffer, name, -1);
+ p11_buffer_add (buffer, "=", 1);
+ *is_first = false;
+}
+
static bool
format_raw_string (p11_buffer *buffer,
bool *is_first,
@@ -635,12 +647,8 @@ format_raw_string (p11_buffer *buffer,
if (!value)
return true;
- if (!*is_first)
- p11_buffer_add (buffer, ";", 1);
- p11_buffer_add (buffer, name, -1);
- p11_buffer_add (buffer, "=", 1);
+ format_name_equals (buffer, is_first, name);
p11_buffer_add (buffer, value, -1);
- *is_first = false;
return p11_buffer_ok (buffer);
}
@@ -653,16 +661,14 @@ format_encode_string (p11_buffer *buffer,
size_t n_value,
bool force)
{
- char *encoded;
- bool ret;
+ /* Not set */
+ if (!value)
+ return true;
- encoded = p11_url_encode (value, value + n_value,
- force ? "" : P11_URL_VERBATIM, NULL);
- return_val_if_fail (encoded != NULL, false);
+ format_name_equals (buffer, is_first, name);
+ p11_url_encode (value, value + n_value, force ? "" : P11_URL_VERBATIM, buffer);
- ret = format_raw_string (buffer, is_first, name, encoded);
- free (encoded);
- return ret;
+ return p11_buffer_ok (buffer);
}