From accf4437c654cf4740b1b78a9e12e5e4aae5ead9 Mon Sep 17 00:00:00 2001 From: Stefan Berger Date: Mon, 18 Nov 2019 10:40:29 -0500 Subject: [PATCH] tpm12: Implement TPM_PrintFourLimit to print 4 or less bytes Implement TPM_PrintFourLimit to print only up to 4 bytes or less. Signed-off-by: Stefan Berger --- src/tpm12/tpm_debug.c | 48 +++++++++++++++++++++++++++++++++++-------- src/tpm12/tpm_debug.h | 2 ++ 2 files changed, 41 insertions(+), 9 deletions(-) diff --git a/src/tpm12/tpm_debug.c b/src/tpm12/tpm_debug.c index 7545dc41..da4347a6 100644 --- a/src/tpm12/tpm_debug.c +++ b/src/tpm12/tpm_debug.c @@ -57,17 +57,40 @@ int tpm_swallow_printf_args(const char *format, ...) #else -/* TPM_PrintFour() prints a prefix plus 4 bytes of a buffer */ - -void TPM_PrintFour(const char *string, const unsigned char* buff) +void TPM_PrintFourLimit(const char *string, + const unsigned char *buff, size_t buflen) { if (buff != NULL) { - TPMLIB_LogPrintf("%s %02x %02x %02x %02x\n", - string, - buff[0], - buff[1], - buff[2], - buff[3]); + switch (buflen) { + case 0: + TPMLIB_LogPrintf("%s (no data)\n", string); + break; + case 1: + TPMLIB_LogPrintf("%s %02x\n", + string, + buff[0]); + break; + case 2: + TPMLIB_LogPrintf("%s %02x %02x\n", + string, + buff[0], + buff[1]); + break; + case 3: + TPMLIB_LogPrintf("%s %02x %02x %02x\n", + string, + buff[0], + buff[1], + buff[2]); + break; + default: + TPMLIB_LogPrintf("%s %02x %02x %02x %02x\n", + string, + buff[0], + buff[1], + buff[2], + buff[3]); + } } else { TPMLIB_LogPrintf("%s null\n", string); @@ -75,6 +98,13 @@ void TPM_PrintFour(const char *string, const unsigned char* buff) return; } +/* TPM_PrintFour() prints a prefix plus 4 bytes of a buffer */ + +void TPM_PrintFour(const char *string, const unsigned char* buff) +{ + TPM_PrintFourLimit(string, buff, 4); +} + #endif /* TPM_PrintAll() prints 'string', the length, and then the entire byte array diff --git a/src/tpm12/tpm_debug.h b/src/tpm12/tpm_debug.h index f2e75198..a9671d6a 100644 --- a/src/tpm12/tpm_debug.h +++ b/src/tpm12/tpm_debug.h @@ -46,6 +46,8 @@ /* prototypes */ void TPM_PrintFour(const char *string, const unsigned char* buff); +void TPM_PrintFourLimit(const char *string, + const unsigned char* buff, size_t bufflen); void TPM_PrintAll(const char *string, const unsigned char* buff, uint32_t length); #if 0