summaryrefslogtreecommitdiffstats
path: root/common/sexputil.c
diff options
context:
space:
mode:
authorWerner Koch <wk@gnupg.org>2013-05-21 15:00:00 +0200
committerWerner Koch <wk@gnupg.org>2013-05-22 09:56:45 +0200
commitcb6a64bb78296c8e9f72df0c482ff847e89a1541 (patch)
treea7ae8bac97bd9c639c2984c6f3ddeb7f42b07601 /common/sexputil.c
parentagent: Fix length detection of canonical formatted openpgp keys. (diff)
downloadgnupg2-cb6a64bb78296c8e9f72df0c482ff847e89a1541.tar.xz
gnupg2-cb6a64bb78296c8e9f72df0c482ff847e89a1541.zip
New debug functions log_printcanon and log_printsexp.
* common/sexputil.c (sexp_to_string, canon_sexp_to_string): New. (log_printcanon, log_printsexp): New. Signed-off-by: Werner Koch <wk@gnupg.org>
Diffstat (limited to 'common/sexputil.c')
-rw-r--r--common/sexputil.c86
1 files changed, 86 insertions, 0 deletions
diff --git a/common/sexputil.c b/common/sexputil.c
index 1c70337e2..e18756a89 100644
--- a/common/sexputil.c
+++ b/common/sexputil.c
@@ -1,5 +1,6 @@
/* sexputil.c - Utility functions for S-expressions.
* Copyright (C) 2005, 2007, 2009 Free Software Foundation, Inc.
+ * Copyright (C) 2013 Werner Koch
*
* This file is part of GnuPG.
*
@@ -46,6 +47,91 @@
#include "sexp-parse.h"
+/* Return a malloced string with the S-expression CANON in advanced
+ format. Returns NULL on error. */
+static char *
+sexp_to_string (gcry_sexp_t sexp)
+{
+ size_t n;
+ char *result;
+
+ if (!sexp)
+ return NULL;
+ n = gcry_sexp_sprint (sexp, GCRYSEXP_FMT_ADVANCED, NULL, 0);
+ if (!n)
+ return NULL;
+ result = xtrymalloc (n);
+ if (!result)
+ return NULL;
+ n = gcry_sexp_sprint (sexp, GCRYSEXP_FMT_ADVANCED, result, n);
+ if (!n)
+ BUG ();
+
+ return result;
+}
+
+
+/* Return a malloced string with the S-expression CANON in advanced
+ format. Returns NULL on error. */
+char *
+canon_sexp_to_string (const unsigned char *canon, size_t canonlen)
+{
+ size_t n;
+ gcry_sexp_t sexp;
+ char *result;
+
+ n = gcry_sexp_canon_len (canon, canonlen, NULL, NULL);
+ if (!n)
+ return NULL;
+ if (gcry_sexp_sscan (&sexp, NULL, canon, n))
+ return NULL;
+ result = sexp_to_string (sexp);
+ gcry_sexp_release (sexp);
+ return result;
+}
+
+
+/* Print the canonical encoded S-expression in SEXP in advanced
+ format. SEXPLEN may be passed as 0 is SEXP is known to be valid.
+ With TEXT of NULL print just the raw S-expression, with TEXT just
+ an empty string, print a trailing linefeed, otherwise print an
+ entire debug line. */
+void
+log_printcanon (const char *text, const unsigned char *sexp, size_t sexplen)
+{
+ if (text && *text)
+ log_debug ("%s ", text);
+ if (sexp)
+ {
+ char *buf = canon_sexp_to_string (sexp, sexplen);
+ log_printf ("%s", buf? buf : "[invalid S-expression]");
+ xfree (buf);
+ }
+ if (text)
+ log_printf ("\n");
+}
+
+
+/* Print the gcryp S-expression in SEXP in advanced format. With TEXT
+ of NULL print just the raw S-expression, with TEXT just an empty
+ string, print a trailing linefeed, otherwise print an entire debug
+ line. */
+void
+log_printsexp (const char *text, gcry_sexp_t sexp)
+{
+ if (text && *text)
+ log_debug ("%s ", text);
+ if (sexp)
+ {
+ char *buf = sexp_to_string (sexp);
+ log_printf ("%s", buf? buf : "[invalid S-expression]");
+ xfree (buf);
+ }
+ if (text)
+ log_printf ("\n");
+}
+
+
/* Helper function to create a canonical encoded S-expression from a
Libgcrypt S-expression object. The function returns 0 on success
and the malloced canonical S-expression is stored at R_BUFFER and