tests: add hexdump_memdup function to libtests

* tests/hexdump_strdup.c (hexdump_memdup): New function.
(hexdump_strdup): Use it.
* tests/tests.h (hexdump_memdup): New prototype.
This commit is contained in:
Дмитрий Левин 2016-04-02 18:24:36 +00:00
parent 35e97a78c7
commit 0de582907a
2 changed files with 14 additions and 6 deletions

View File

@ -34,11 +34,10 @@
#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));
}

View 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);