From 128239732a5b7e184d5d9c505402630ee9215080 Mon Sep 17 00:00:00 2001
From: Stef Walter <stefw@gnome.org>
Date: Mon, 18 Mar 2013 20:59:57 +0100
Subject: [PATCH] attrs: Change p11_attrs_to_string() to allow static templates

Allow passing the number of attributes to print, which lets us use
this directly on templates passed in by callers of the PKCS#11 API.
---
 common/attrs.c               | 13 ++++++++-----
 common/attrs.h               |  3 ++-
 common/tests/test-attrs.c    |  6 +++++-
 trust/tests/frob-nss-trust.c |  2 +-
 4 files changed, 16 insertions(+), 8 deletions(-)

diff --git a/common/attrs.c b/common/attrs.c
index a438264..5539789 100644
--- a/common/attrs.c
+++ b/common/attrs.c
@@ -813,12 +813,14 @@ format_attribute (p11_buffer *buffer,
 
 static void
 format_attributes (p11_buffer *buffer,
-                   const CK_ATTRIBUTE *attrs)
+                   const CK_ATTRIBUTE *attrs,
+                   int count)
 {
 	CK_BBOOL first = CK_TRUE;
-	int count, i;
+	int i;
 
-	count = p11_attrs_count (attrs);
+	if (count < 0)
+		count = p11_attrs_count (attrs);
 	buffer_append_printf (buffer, "(%d) [", count);
 	for (i = 0; i < count; i++) {
 		if (first)
@@ -832,12 +834,13 @@ format_attributes (p11_buffer *buffer,
 }
 
 char *
-p11_attrs_to_string (const CK_ATTRIBUTE *attrs)
+p11_attrs_to_string (const CK_ATTRIBUTE *attrs,
+                     int count)
 {
 	p11_buffer buffer;
 	if (!p11_buffer_init_null (&buffer, 128))
 		return_val_if_reached (NULL);
-	format_attributes (&buffer, attrs);
+	format_attributes (&buffer, attrs, count);
 	return p11_buffer_steal (&buffer, NULL);
 }
 
diff --git a/common/attrs.h b/common/attrs.h
index f6eb950..87e0af1 100644
--- a/common/attrs.h
+++ b/common/attrs.h
@@ -104,7 +104,8 @@ bool                p11_attrs_matchn        (const CK_ATTRIBUTE *attrs,
                                              const CK_ATTRIBUTE *match,
                                              CK_ULONG count);
 
-char *              p11_attrs_to_string     (const CK_ATTRIBUTE *attrs);
+char *              p11_attrs_to_string     (const CK_ATTRIBUTE *attrs,
+                                             int count);
 
 char *              p11_attr_to_string      (const CK_ATTRIBUTE *attr);
 
diff --git a/common/tests/test-attrs.c b/common/tests/test-attrs.c
index f1e6d91..61fcef3 100644
--- a/common/tests/test-attrs.c
+++ b/common/tests/test-attrs.c
@@ -474,9 +474,13 @@ test_to_string (CuTest *tc)
 	CuAssertStrEquals (tc, "{ CKA_LABEL = (3) \"yay\" }", string);
 	free (string);
 
-	string = p11_attrs_to_string (attrs);
+	string = p11_attrs_to_string (attrs, -1);
 	CuAssertStrEquals (tc, "(2) [ { CKA_LABEL = (3) \"yay\" }, { CKA_VALUE = (5) NOT-PRINTED } ]", string);
 	free (string);
+
+	string = p11_attrs_to_string (attrs, 1);
+	CuAssertStrEquals (tc, "(1) [ { CKA_LABEL = (3) \"yay\" } ]", string);
+	free (string);
 }
 
 static void
diff --git a/trust/tests/frob-nss-trust.c b/trust/tests/frob-nss-trust.c
index 790362f..da76795 100644
--- a/trust/tests/frob-nss-trust.c
+++ b/trust/tests/frob-nss-trust.c
@@ -68,7 +68,7 @@ dump_object (P11KitIter *iter,
 	else
 		name = strdup ("unknown");
 
-	string = p11_attrs_to_string (attrs);
+	string = p11_attrs_to_string (attrs, -1);
 	printf ("\"%s\" = %s\n", name, string);
 	free (string);
 
-- 
2.40.0