summaryrefslogtreecommitdiff
path: root/src/misc.c
diff options
context:
space:
mode:
authorWerner Koch <wk@gnupg.org>2013-09-07 10:06:46 +0200
committerWerner Koch <wk@gnupg.org>2013-09-11 17:36:34 +0200
commite35ed615acc624a8b6c07576ea0650aac2bdb0db (patch)
tree7c90aa5c5dbf091108ca749f5fc8220583bba8e0 /src/misc.c
parentf3bca0c77c4979504f95fdbc618f7458e61e3e45 (diff)
downloadlibgcrypt-e35ed615acc624a8b6c07576ea0650aac2bdb0db.tar.gz
Streamline the use of the internal mpi and hex debug functions.
* mpi/mpicoder.c (gcry_mpi_dump): Remove. (_gcry_log_mpidump): Remove. * src/misc.c (_gcry_log_printhex): Factor all code out to ... (do_printhex): new. Add line wrapping a and compact printing. (_gcry_log_printmpi): New. * src/mpi.h (log_mpidump): Remove macro. * src/g10lib.h (log_mpidump): Add compatibility macro. (log_printmpi): New macro * src/visibility.c (gcry_mpi_dump): Call _gcry_log_printmpi. * cipher/primegen.c (prime_generate_internal): Replace gcry_mpi_dump by log_printmpi. (gcry_prime_group_generator): Ditto. * cipher/pubkey.c: Remove extra colons from log_mpidump call. * cipher/rsa.c (stronger_key_check): Use log_printmpi. -- The values to debug get longer and longer and the different debug functions made it hard to check them out. Now MPIs and hex buffers are printed very similar. Lines may now wrap with an backslash as indicator. MPIs are distinguished from plain buffers in the output by always using a sign. Signed-off-by: Werner Koch <wk@gnupg.org>
Diffstat (limited to 'src/misc.c')
-rw-r--r--src/misc.c90
1 files changed, 81 insertions, 9 deletions
diff --git a/src/misc.c b/src/misc.c
index dece1d04..d7a7a65c 100644
--- a/src/misc.c
+++ b/src/misc.c
@@ -28,6 +28,7 @@
#include "g10lib.h"
#include "secmem.h"
+#include "mpi.h"
static int verbosity_level = 0;
@@ -267,26 +268,97 @@ _gcry_log_printf (const char *fmt, ...)
}
}
-/* Print a hexdump of BUFFER. With TEXT of NULL print just the raw
- dump, with TEXT an empty string, print a trailing linefeed,
- otherwise print an entire debug line. */
-void
-_gcry_log_printhex (const char *text, const void *buffer, size_t length)
+
+/* Helper for _gcry_log_printhex and _gcry_log_printmpi. */
+static void
+do_printhex (const char *text, const char *text2,
+ const void *buffer, size_t length)
{
+ int wrap = 0;
+ int cnt = 0;
+
if (text && *text)
- log_debug ("%s ", text);
+ {
+ wrap = 1;
+ log_debug ("%s:%s", text, text2);
+ if (text2[1] == '[' && length && buffer)
+ {
+ /* Start with a new line so that we get nice output for
+ opaque MPIS:
+ "value: [31 bit]"
+ " 01020300" */
+ log_printf ("\n");
+ text2 = " ";
+ log_debug ("%*s ", (int)strlen(text), "");
+ }
+ }
if (length)
{
const unsigned char *p = buffer;
- log_printf ("%02X", *p);
- for (length--, p++; length--; p++)
- log_printf (" %02X", *p);
+ for (; length--; p++)
+ {
+ log_printf ("%02x", *p);
+ if (wrap && ++cnt == 32 && length)
+ {
+ cnt = 0;
+ log_printf (" \\\n");
+ log_debug ("%*s %*s",
+ (int)strlen(text), "", (int)strlen(text2), "");
+ }
+ }
}
if (text)
log_printf ("\n");
}
+/* Print a hexdump of BUFFER. With TEXT of NULL print just the raw
+ dump without any wrappping, with TEXT an empty string, print a
+ trailing linefeed, otherwise print an entire debug line. */
+void
+_gcry_log_printhex (const char *text, const void *buffer, size_t length)
+{
+ do_printhex (text, " ", buffer, length);
+}
+
+
+/* Print MPI in hex notation. To make clear that the output is an MPI
+ a sign is always printed. With TEXT of NULL print just the raw dump
+ without any wrapping, with TEXT an empty string, print a trailing
+ linefeed, otherwise print an entire debug line. */
+void
+_gcry_log_printmpi (const char *text, gcry_mpi_t mpi)
+{
+ unsigned char *rawmpi;
+ unsigned int rawmpilen;
+ int sign;
+
+ if (!mpi)
+ do_printhex (text? text:" ", " (null)", NULL, 0);
+ else if (mpi_is_opaque (mpi))
+ {
+ unsigned int nbits;
+ const unsigned char *p;
+ char prefix[30];
+
+ p = gcry_mpi_get_opaque (mpi, &nbits);
+ snprintf (prefix, sizeof prefix, " [%u bit]", nbits);
+ do_printhex (text? text:" ", prefix, p, (nbits+7)/8);
+ }
+ else
+ {
+ rawmpi = _gcry_mpi_get_buffer (mpi, &rawmpilen, &sign);
+ if (!rawmpi)
+ do_printhex (text? text:" ", " [out of core]", NULL, 0);
+ else
+ {
+ do_printhex (text, sign? "-":"+", rawmpi, rawmpilen);
+ gcry_free (rawmpi);
+ }
+ }
+}
+
+
void
_gcry_burn_stack (unsigned int bytes)
{