From 63e950c5ea3af4e3b16941ef35bbe97b787e081d Mon Sep 17 00:00:00 2001 From: merlokk Date: Wed, 18 Oct 2017 14:41:55 +0300 Subject: [PATCH] proxmark main_loop() replace char refactoring --- client/proxmark3.c | 17 ++++------------- client/util.c | 12 ++++++++++++ client/util.h | 2 ++ 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/client/proxmark3.c b/client/proxmark3.c index ce5a6e4f..691311e4 100644 --- a/client/proxmark3.c +++ b/client/proxmark3.c @@ -23,6 +23,7 @@ #include "cmdmain.h" #include "uart.h" #include "ui.h" +#include "util.h" #include "cmdparser.h" #include "cmdhw.h" #include "whereami.h" @@ -111,7 +112,7 @@ void main_loop(char *script_cmds_file, char *script_cmd, bool usb_present) { // file with script FILE *script_file = NULL; - char script_cmd_buf[256]; // iceman, needs lua script the same file_path_buffer as the rest + char script_cmd_buf[256] = {0}; // iceman, needs lua script the same file_path_buffer as the rest if (script_cmds_file) { script_file = fopen(script_cmds_file, "r"); @@ -130,12 +131,7 @@ void main_loop(char *script_cmds_file, char *script_cmd, bool usb_present) { fclose(script_file); script_file = NULL; } else { - char *nl; - nl = strrchr(script_cmd_buf, '\r'); - if (nl) *nl = '\0'; - - nl = strrchr(script_cmd_buf, '\n'); - if (nl) *nl = '\0'; + strcleanrn(script_cmd_buf, sizeof(script_cmd_buf)); if ((cmd = (char*) malloc(strlen(script_cmd_buf) + 1)) != NULL) { memset(cmd, 0, strlen(script_cmd_buf) + 1); @@ -164,12 +160,7 @@ void main_loop(char *script_cmds_file, char *script_cmd, bool usb_present) { printf("\nStdin end. Exit...\n"); break; } - char *nl; - nl = strrchr(script_cmd_buf, '\r'); - if (nl) *nl = '\0'; - - nl = strrchr(script_cmd_buf, '\n'); - if (nl) *nl = '\0'; + strcleanrn(script_cmd_buf, sizeof(script_cmd_buf)); if ((cmd = (char*) malloc(strlen(script_cmd_buf) + 1)) != NULL) { memset(cmd, 0, strlen(script_cmd_buf) + 1); diff --git a/client/util.c b/client/util.c index 86e8c502..bc422bba 100644 --- a/client/util.c +++ b/client/util.c @@ -623,7 +623,19 @@ void clean_ascii(unsigned char *buf, size_t len) { } } +// replace \r \n to \0 +void strcleanrn(char *buf, size_t len) { + strcreplace(buf, len, '\n', '\0'); + strcreplace(buf, len, '\r', '\0'); +} +// replace char in buffer +void strcreplace(char *buf, size_t len, char from, char to) { + for (size_t i = 0; i < len; i++) { + if (buf[i] == from) + buf[i] = to; + } +} // determine number of logical CPU cores (use for multithreaded functions) diff --git a/client/util.h b/client/util.h index 6177dd93..5730faa7 100644 --- a/client/util.h +++ b/client/util.h @@ -77,6 +77,8 @@ extern uint32_t le32toh (uint8_t *data); extern void rol(uint8_t *data, const size_t len); extern void clean_ascii(unsigned char *buf, size_t len); +void strcleanrn(char *buf, size_t len); +void strcreplace(char *buf, size_t len, char from, char to); extern int num_CPUs(void); // number of logical CPUs