Commit 503835ee authored by Blachut, Bartosz's avatar Blachut, Bartosz Committed by Tomasz Zawadzki
Browse files

util: made hexlify and unhexlify functions public



hexlify and unhexlify utils from vbdev_crypto.h have been moved so that
they could be included and reused outside of vbdev_crypto module.

Signed-off-by: default avatarBlachut, Bartosz <bartosz.blachut@intel.com>
Change-Id: Ia074250176907f4803b84024239ecd4e9d8a5fc1
Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/14191


Tested-by: default avatarSPDK CI Jenkins <sys_sgci@intel.com>
Community-CI: Mellanox Build Bot
Reviewed-by: default avatarShuhei Matsumoto <smatsumoto@nvidia.com>
Reviewed-by: default avatarJacek Kalwas <jacek.kalwas@intel.com>
Reviewed-by: default avatarAleksey Marchuk <alexeymar@nvidia.com>
parent 1a24dc8f
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -75,6 +75,10 @@ tell the driver to not read the CHANGED_NS_LIST log page in response to a NS_ATT
AEN.  When called the application is required to read this log page instead to clear the
AEN.

### util

Added new functions: `spdk_hexlify` and `spdk_unhexlify`.

## v22.05

### sock

include/spdk/hexlify.h

0 → 100644
+28 −0
Original line number Diff line number Diff line
/*   SPDX-License-Identifier: BSD-3-Clause
 *   Copyright (c) Intel Corporation.
 *   All rights reserved.
 */

#ifndef SPDK_HEXLIFY_H
#define SPDK_HEXLIFY_H

#include "spdk/stdinc.h"

/**
 * Convert a binary array to hexlified string terminated by zero.
 *
 * \param bin A binary array pointer.
 * \param len Length of the binary array.
 * \return Pointer to hexlified version of @bin or NULL on failure.
 */
char *spdk_hexlify(const char *bin, size_t len);

/**
 * Convert hexlified string to binary array of size strlen(hex) / 2.
 *
 * \param hex A hexlified string terminated by zero.
 * \return Binary array pointer or NULL on failure.
 */
char *spdk_unhexlify(const char *hex);

#endif /* SPDK_HEXLIFY_H */
+1 −1
Original line number Diff line number Diff line
@@ -10,7 +10,7 @@ SO_VER := 5
SO_MINOR := 1

C_SRCS = base64.c bit_array.c cpuset.c crc16.c crc32.c crc32c.c crc32_ieee.c \
	 dif.c fd.c file.c iov.c math.c pipe.c strerror_tls.c string.c uuid.c \
	 dif.c fd.c file.c hexlify.c iov.c math.c pipe.c strerror_tls.c string.c uuid.c \
	 fd_group.c zipf.c
LIBNAME = util
LOCAL_SYS_LIBS = -luuid

lib/util/hexlify.c

0 → 100644
+85 −0
Original line number Diff line number Diff line
/*   SPDX-License-Identifier: BSD-3-Clause
 *   Copyright (c) Intel Corporation.
 *   All rights reserved.
 */

#include "spdk/hexlify.h"
#include "spdk/log.h"

static inline int
__c2v(char c)
{
	if ((c >= '0') && (c <= '9')) {
		return c - '0';
	}
	if ((c >= 'a') && (c <= 'f')) {
		return c - 'a' + 10;
	}
	if ((c >= 'A') && (c <= 'F')) {
		return c - 'A' + 10;
	}
	return -1;
}

static inline signed char
__v2c(int c)
{
	const char hexchar[] = "0123456789abcdef";
	if (c < 0 || c > 15) {
		return -1;
	}
	return hexchar[c];
}

char *
spdk_hexlify(const char *bin, size_t len)
{
	char *hex, *phex;

	hex = malloc((len * 2) + 1);
	if (hex == NULL) {
		return NULL;
	}
	phex = hex;
	for (size_t i = 0; i < len; i++) {
		signed char c0 = __v2c((bin[i] >> 4) & 0x0f);
		signed char c1 = __v2c((bin[i]) & 0x0f);
		if (c0 < 0 || c1 < 0) {
			assert(false);
			free(hex);
			return NULL;
		}
		*phex++ = c0;
		*phex++ = c1;
	}
	*phex = '\0';
	return hex;
}

char *
spdk_unhexlify(const char *hex)
{
	char *res, *pres;
	size_t len = strlen(hex);

	if (len % 2 != 0) {
		SPDK_ERRLOG("Invalid hex string len %d. It must be mod of 2.\n", (int)len);
		return NULL;
	}
	res = malloc(len / 2);
	if (res == NULL) {
		return NULL;
	}
	pres = res;
	for (size_t i = 0; i < len; i += 2) {
		int v0 = __c2v(hex[i]);
		int v1 = __c2v(hex[i + 1]);
		if (v0 < 0 || v1 < 0) {
			SPDK_ERRLOG("Invalid hex string \"%s\"\n", hex);
			free(res);
			return NULL;
		}
		*pres++ = (v0 << 4) + v1;
	}
	return res;
}
+4 −0
Original line number Diff line number Diff line
@@ -92,6 +92,10 @@
	# public functions in file.h
	spdk_posix_file_load;

	# public functions in hexlify.h
	spdk_hexlify;
	spdk_unhexlify;

	# public functions in pipe.h
	spdk_pipe_create;
	spdk_pipe_destroy;
Loading