summaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
Diffstat (limited to 'common')
-rw-r--r--common/tests/test-url.c30
-rw-r--r--common/url.c29
-rw-r--r--common/url.h5
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 */