From d172c17ca459379d99ab4cf7ce8fbe097918a6b6 Mon Sep 17 00:00:00 2001
From: James Chambers <jameschambers2@gmail.com>
Date: Fri, 3 Mar 2017 18:04:58 -0500
Subject: [PATCH 1/1] make clean_ascii a util function

---
 client/cmdhfmfu.c |  9 ++-------
 client/util.c     | 10 ++++++++++
 client/util.h     |  2 ++
 3 files changed, 14 insertions(+), 7 deletions(-)

diff --git a/client/cmdhfmfu.c b/client/cmdhfmfu.c
index 9c5f7a83..b7cf0fcd 100644
--- a/client/cmdhfmfu.c
+++ b/client/cmdhfmfu.c
@@ -1231,7 +1231,7 @@ int CmdHF14AMfUDump(const char *Cmd){
 	bool manualPages = false;
 	uint8_t startPage = 0;
 	char tempStr[50];
-	char cleanASCII[4];
+	unsigned char cleanASCII[4];
 
 	while(param_getchar(Cmd, cmdp) != 0x00)
 	{
@@ -1424,12 +1424,7 @@ int CmdHF14AMfUDump(const char *Cmd){
 
 		// convert unprintable characters and line breaks to dots
 		memcpy(cleanASCII, data+i*4, 4);
-
-		for (size_t clean_i = 0; clean_i < 4; clean_i++) {
-			if (!isprint(cleanASCII[clean_i])) {
-				cleanASCII[clean_i] = '.';
-			}
-		}
+		clean_ascii(cleanASCII, 4);
 
 		PrintAndLog("%3d/0x%02X | %s| %d | %.4s", i+startPage, i+startPage, sprint_hex(data + i * 4, 4), tmplockbit, cleanASCII);
 	}
diff --git a/client/util.c b/client/util.c
index 374ae397..e80f5cc9 100644
--- a/client/util.c
+++ b/client/util.c
@@ -8,6 +8,7 @@
 // utilities
 //-----------------------------------------------------------------------------
 
+#include <ctype.h>
 #include "util.h"
 #define MAX_BIN_BREAK_LENGTH   (3072+384+1)
 
@@ -581,3 +582,12 @@ void rol(uint8_t *data, const size_t len){
     }
     data[len-1] = first;
 }
+
+
+// Replace unprintable characters with a dot in char buffer
+void clean_ascii(unsigned char *buf, size_t len) {
+  for (size_t i = 0; i < len; i++) {
+    if (!isprint(buf[i]))
+      buf[i] = '.';
+  }
+}
diff --git a/client/util.h b/client/util.h
index 8c0ed950..fde06540 100644
--- a/client/util.h
+++ b/client/util.h
@@ -76,3 +76,5 @@ void xor(unsigned char *dst, unsigned char *src, size_t len);
 int32_t le24toh(uint8_t data[3]);
 uint32_t le32toh (uint8_t *data);
 void rol(uint8_t *data, const size_t len);
+
+void clean_ascii(unsigned char *buf, size_t len);
-- 
2.39.5