util.c str_text make a fully text readable version of str
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
diff --git a/util.c b/util.c
index 662c941..39f003c 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 dde1aa8..be16318 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 */