[PATCH] lib: fwts_dump_data: tidy up code, ensure we limit nbytes to 16
Colin King
colin.king at canonical.com
Mon Dec 10 22:59:44 UTC 2012
From: Colin Ian King <colin.king at canonical.com>
Tidy up this a little. Make buffer sizes type size_t. Make sure
that we can only dump a maximum of 16 bytes. Tidy up the formatting
of the function declaration.
Signed-off-by: Colin Ian King <colin.king at canonical.com>
---
src/lib/include/fwts_dump_data.h | 2 +-
src/lib/src/fwts_dump_data.c | 19 +++++++++++++------
2 files changed, 14 insertions(+), 7 deletions(-)
diff --git a/src/lib/include/fwts_dump_data.h b/src/lib/include/fwts_dump_data.h
index 8c5b6d0..7d0420a 100644
--- a/src/lib/include/fwts_dump_data.h
+++ b/src/lib/include/fwts_dump_data.h
@@ -22,6 +22,6 @@
#include "fwts.h"
-void fwts_dump_raw_data(char *buffer, const int len, const uint8_t *data, const int where, const int bytes);
+void fwts_dump_raw_data(char *buffer, const size_t len, const uint8_t *data, const int where, const size_t bytes);
#endif
diff --git a/src/lib/src/fwts_dump_data.c b/src/lib/src/fwts_dump_data.c
index 23c3a89..c398b53 100644
--- a/src/lib/src/fwts_dump_data.c
+++ b/src/lib/src/fwts_dump_data.c
@@ -24,28 +24,35 @@
/*
* fwts_dump_raw_data()
- * print raw uint8 data of length `nbytes` into a buffer (length len) as a hex dump. nbytes must
- * be no more than 16. The address/offset of the buffer in memory is annotated by addr.
+ * print raw uint8 data of length `nbytes` into a buffer (length len)
+ * as a hex dump. nbytes must be no more than 16. The address/offset
+ * of the buffer in memory is annotated by addr.
*/
-void fwts_dump_raw_data(char *buffer, const int len, const uint8_t *data, const int addr, const int nbytes)
+void fwts_dump_raw_data(
+ char *buffer, /* buffer to contained formatted dump */
+ const size_t len, /* Length of buffer */
+ const uint8_t *data, /* Octects to dump */
+ const int addr, /* Original buffer data address */
+ const size_t nbytes) /* Number of bytes to dump, max 16 */
{
int i;
int n = 0;
+ int nbytes_max = nbytes > 16 ? 16 : nbytes;
n = snprintf(buffer, len, " %4.4x: ", addr);
/* Hex dump */
- for (i=0;i<nbytes;i++)
+ for (i = 0; i < nbytes_max; i++)
n += snprintf(buffer + n, len - n, "%2.2x ", data[i]);
/* Padding */
- for (;i<16;i++)
+ for (; i < 16; i++)
n += snprintf(buffer + n, len - n, " ");
n += snprintf(buffer + n, len - n, " ");
/* printable ASCII dump */
- for (i=0;i<nbytes;i++)
+ for (i = 0; i < nbytes_max; i++)
buffer[n++] = (data[i] < 32 || data[i] > 126) ? '.' : data[i];
buffer[n] = '\0';
}
--
1.8.0
More information about the fwts-devel
mailing list