]> granicus.if.org Git - strace/commitdiff
tests: add hexdump_memdup function to libtests
authorDmitry V. Levin <ldv@altlinux.org>
Sat, 2 Apr 2016 18:24:36 +0000 (18:24 +0000)
committerDmitry V. Levin <ldv@altlinux.org>
Sat, 2 Apr 2016 18:24:36 +0000 (18:24 +0000)
* tests/hexdump_strdup.c (hexdump_memdup): New function.
(hexdump_strdup): Use it.
* tests/tests.h (hexdump_memdup): New prototype.

tests/hexdump_strdup.c
tests/tests.h

index d7e083a4d7a936759e2e30f9bb8b9eabac05f637..4d6b53f4c3c21458ceae8eea810f2fbe64ef5e62 100644 (file)
 #include <string.h>
 
 const char *
-hexdump_strdup(const char *src)
+hexdump_memdup(const char *src, size_t len)
 {
-       size_t src_len = strlen(src);
-       size_t dst_size = 3 * src_len + 2;
-       assert(dst_size > src_len);
+       size_t dst_size = 3 * len + 2;
+       assert(dst_size > len);
 
        char *dst = malloc(dst_size);
        if (!dst)
@@ -46,8 +45,8 @@ hexdump_strdup(const char *src)
 
        char *p = dst;
        const unsigned char *usrc = (const unsigned char *) src;
-       unsigned int i;
-       for (i = 0; usrc[i]; ++i) {
+       size_t i;
+       for (i = 0; i < len; ++i) {
                unsigned int c = usrc[i];
                *(p++) = ' ';
                if (i == 8)
@@ -59,3 +58,9 @@ hexdump_strdup(const char *src)
 
        return dst;
 }
+
+const char *
+hexdump_strdup(const char *src)
+{
+       return hexdump_memdup(src, strlen(src));
+}
index d8632ba3171362a81c30d79dec2a91cb6a174461..115379ee5c63e8548fb834d9ababcdef6af7b9c8 100644 (file)
@@ -69,6 +69,9 @@ void tprintf(const char *, ...)
 /* Make a hexdump copy of C string */
 const char *hexdump_strdup(const char *);
 
+/* Make a hexdump copy of memory */
+const char *hexdump_memdup(const char *, size_t);
+
 /* Make a hexquoted copy of a string */
 const char *hexquote_strndup(const char *, size_t);