summaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
authorStef Walter <stefw@gnome.org>2013-05-21 17:33:22 +0200
committerStef Walter <stefw@gnome.org>2013-05-27 10:46:11 +0200
commit56fec770071713bf800e7e9f3905973703105ec5 (patch)
tree7b1cb7119299d966027f2c5c754a78d72a143bcb /common
parentcb8f2e3a04d9365121ffea0d76d8b3d47e2cc1ec (diff)
downloadp11-kit-56fec770071713bf800e7e9f3905973703105ec5.tar.gz
pem: Write PEM data directly to a buffer
Diffstat (limited to 'common')
-rw-r--r--common/pem.c38
-rw-r--r--common/pem.h7
-rw-r--r--common/tests/test-pem.c21
3 files changed, 34 insertions, 32 deletions
diff --git a/common/pem.c b/common/pem.c
index b0625ef..7fe0076 100644
--- a/common/pem.c
+++ b/common/pem.c
@@ -242,35 +242,31 @@ p11_pem_parse (const char *data,
return nfound;
}
-char *
+bool
p11_pem_write (const unsigned char *contents,
size_t length,
const char *type,
- size_t *pem_len)
+ p11_buffer *buf)
{
- p11_buffer buffer;
size_t estimate;
size_t prefix;
char *target;
int len;
- return_val_if_fail (contents || !length, NULL);
- return_val_if_fail (type, NULL);
- return_val_if_fail (pem_len, NULL);
+ return_val_if_fail (contents || !length, false);
+ return_val_if_fail (type, false);
+ return_val_if_fail (buf, false);
/* Estimate from base64 data. Algorithm from Glib reference */
estimate = length * 4 / 3 + 7;
estimate += estimate / 64 + 1;
- if (!p11_buffer_init_null (&buffer, estimate + 128))
- return_val_if_reached (NULL);
+ p11_buffer_add (buf, ARMOR_PREF_BEGIN, ARMOR_PREF_BEGIN_L);
+ p11_buffer_add (buf, type, -1);
+ p11_buffer_add (buf, ARMOR_SUFF, ARMOR_SUFF_L);
- p11_buffer_add (&buffer, ARMOR_PREF_BEGIN, ARMOR_PREF_BEGIN_L);
- p11_buffer_add (&buffer, type, -1);
- p11_buffer_add (&buffer, ARMOR_SUFF, ARMOR_SUFF_L);
-
- prefix = buffer.len;
- target = p11_buffer_append (&buffer, estimate);
+ prefix = buf->len;
+ target = p11_buffer_append (buf, estimate);
return_val_if_fail (target != NULL, NULL);
/*
@@ -282,13 +278,13 @@ p11_pem_write (const unsigned char *contents,
assert (len > 0);
assert (len <= estimate);
- buffer.len = prefix + len;
+ buf->len = prefix + len;
- p11_buffer_add (&buffer, "\n", 1);
- p11_buffer_add (&buffer, ARMOR_PREF_END, ARMOR_PREF_END_L);
- p11_buffer_add (&buffer, type, -1);
- p11_buffer_add (&buffer, ARMOR_SUFF, ARMOR_SUFF_L);
- p11_buffer_add (&buffer, "\n", 1);
+ p11_buffer_add (buf, "\n", 1);
+ p11_buffer_add (buf, ARMOR_PREF_END, ARMOR_PREF_END_L);
+ p11_buffer_add (buf, type, -1);
+ p11_buffer_add (buf, ARMOR_SUFF, ARMOR_SUFF_L);
+ p11_buffer_add (buf, "\n", 1);
- return p11_buffer_steal (&buffer, pem_len);
+ return p11_buffer_ok (buf);
}
diff --git a/common/pem.h b/common/pem.h
index d84f418..7e4ce63 100644
--- a/common/pem.h
+++ b/common/pem.h
@@ -35,6 +35,9 @@
#ifndef P11_PEM_H_
#define P11_PEM_H_
+#include "buffer.h"
+#include "compat.h"
+
#include <sys/types.h>
typedef void (*p11_pem_sink) (const char *type,
@@ -47,9 +50,9 @@ unsigned int p11_pem_parse (const char *input,
p11_pem_sink sink,
void *user_data);
-char * p11_pem_write (const unsigned char *contents,
+bool p11_pem_write (const unsigned char *contents,
size_t length,
const char *type,
- size_t *pem_len);
+ p11_buffer *buf);
#endif /* P11_PEM_H_ */
diff --git a/common/tests/test-pem.c b/common/tests/test-pem.c
index 7dd7fb2..0c7d60a 100644
--- a/common/tests/test-pem.c
+++ b/common/tests/test-pem.c
@@ -306,24 +306,27 @@ static void
test_pem_write (void)
{
WriteFixture *fixture;
- size_t length;
- char *output;
+ p11_buffer buf;
unsigned int count;
int i;
for (i = 0; write_fixtures[i].input != NULL; i++) {
fixture = write_fixtures + i;
- output = p11_pem_write ((unsigned char *)fixture->input,
- fixture->length,
- fixture->type, &length);
- assert_str_eq (fixture->output, output);
- assert_num_eq (strlen (fixture->output), length);
+ if (!p11_buffer_init_null (&buf, 0))
+ assert_not_reached ();
+
+ if (!p11_pem_write ((unsigned char *)fixture->input,
+ fixture->length,
+ fixture->type, &buf))
+ assert_not_reached ();
+ assert_str_eq (fixture->output, buf.data);
+ assert_num_eq (strlen (fixture->output), buf.len);
- count = p11_pem_parse (output, length, on_parse_written, fixture);
+ count = p11_pem_parse (buf.data, buf.len, on_parse_written, fixture);
assert_num_eq (1, count);
- free (output);
+ p11_buffer_uninit (&buf);
}
}