From 863c9e27dfee2c077592b8143aa07299944660b7 Mon Sep 17 00:00:00 2001 From: Kano Date: Fri, 26 Apr 2013 14:49:10 +1000 Subject: [PATCH] util.c str_text make a fully text readable version of str --- util.c | 34 ++++++++++++++++++++++++++++++++++ util.h | 1 + 2 files changed, 35 insertions(+) diff --git a/util.c b/util.c index 662c941b..39f003cf 100644 --- a/util.c +++ b/util.c @@ -1818,6 +1818,40 @@ void *realloc_strcat(char *ptr, char *s) return ret; } +/* Make a text readable version of a string using 0xNN for < ' ' or > '~' + * Including 0x00 at the end + * You must free the result yourself */ +void *str_text(char *ptr) +{ + unsigned char *uptr; + char *ret, *txt; + + if (ptr == NULL) { + ret = strdup("(null)"); + + if (unlikely(!ret)) + quit(1, "Failed to malloc in text_str null"); + } + + uptr = (unsigned char *)ptr; + + ret = txt = malloc(strlen(ptr)*4+5); // Guaranteed >= needed + if (unlikely(!txt)) + quit(1, "Failed to malloc in text_str txt"); + + do { + if (*uptr < ' ' || *uptr > '~') { + sprintf(txt, "0x%02x", *uptr); + txt += 4; + } else + *(txt++) = *uptr; + } while (*(uptr++)); + + *txt = '\0'; + + return ret; +} + void RenameThread(const char* name) { #if defined(PR_SET_NAME) diff --git a/util.h b/util.h index dde1aa85..be163188 100644 --- a/util.h +++ b/util.h @@ -77,6 +77,7 @@ bool restart_stratum(struct pool *pool); void suspend_stratum(struct pool *pool); void dev_error(struct cgpu_info *dev, enum dev_reason reason); void *realloc_strcat(char *ptr, char *s); +void *str_text(char *ptr); void RenameThread(const char* name); /* Align a size_t to 4 byte boundaries for fussy arches */