Spdk/lib/util/hexlify.c
paul luse a6dbe3721e update Intel copyright notices
per Intel policy to include file commit date using git cmd
below.  The policy does not apply to non-Intel (C) notices.

git log --follow -C90% --format=%ad --date default <file> | tail -1

and then pull just the 4 digit year from the result.

Intel copyrights were not added to files where Intel either had
no contribution ot the contribution lacked substance (ie license
header updates, formatting changes, etc).  Contribution date used
"--follow -C95%" to get the most accurate date.

Note that several files in this patch didn't end the license/(c)
block with a blank comment line so these were added as the vast
majority of files do have this last blank line.  Simply there for
consistency.

Signed-off-by: paul luse <paul.e.luse@intel.com>
Change-Id: Id5b7ce4f658fe87132f14139ead58d6e285c04d4
Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/15192
Tested-by: SPDK CI Jenkins <sys_sgci@intel.com>
Reviewed-by: Jim Harris <james.r.harris@intel.com>
Reviewed-by: Ben Walker <benjamin.walker@intel.com>
Community-CI: Mellanox Build Bot
2022-11-10 08:28:53 +00:00

86 lines
1.5 KiB
C

/* SPDX-License-Identifier: BSD-3-Clause
* Copyright (C) 2022 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;
}