mirror of
https://gitlab.freedesktop.org/NetworkManager/NetworkManager.git
synced 2026-05-09 08:18:03 +02:00
shared: support empty blobs in nm_utils_bin2hexstr_full()
The limitation to not accept a length of 0 is easy to forget. Handle also empty blobs in a sensible way, by returning the empty word.
This commit is contained in:
parent
53b747fff5
commit
9d0da3e60b
1 changed files with 17 additions and 15 deletions
|
|
@ -2538,8 +2538,10 @@ nm_utils_memeqzero (gconstpointer data, gsize length)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* nm_utils_bin2hexstr_full:
|
* nm_utils_bin2hexstr_full:
|
||||||
* @addr: pointer of @length bytes.
|
* @addr: pointer of @length bytes. If @length is zero, this may
|
||||||
* @length: number of bytes in @addr
|
* also be %NULL.
|
||||||
|
* @length: number of bytes in @addr. May also be zero, in which
|
||||||
|
* case this will return an empty string.
|
||||||
* @delimiter: either '\0', otherwise the output string will have the
|
* @delimiter: either '\0', otherwise the output string will have the
|
||||||
* given delimiter character between each two hex numbers.
|
* given delimiter character between each two hex numbers.
|
||||||
* @upper_case: if TRUE, use upper case ASCII characters for hex.
|
* @upper_case: if TRUE, use upper case ASCII characters for hex.
|
||||||
|
|
@ -2565,9 +2567,6 @@ nm_utils_bin2hexstr_full (gconstpointer addr,
|
||||||
const char *LOOKUP = upper_case ? "0123456789ABCDEF" : "0123456789abcdef";
|
const char *LOOKUP = upper_case ? "0123456789ABCDEF" : "0123456789abcdef";
|
||||||
char *out0;
|
char *out0;
|
||||||
|
|
||||||
nm_assert (addr);
|
|
||||||
nm_assert (length > 0);
|
|
||||||
|
|
||||||
if (out)
|
if (out)
|
||||||
out0 = out;
|
out0 = out;
|
||||||
else {
|
else {
|
||||||
|
|
@ -2579,19 +2578,22 @@ nm_utils_bin2hexstr_full (gconstpointer addr,
|
||||||
/* @out must contain at least @length*3 bytes if @delimiter is set,
|
/* @out must contain at least @length*3 bytes if @delimiter is set,
|
||||||
* otherwise, @length*2+1. */
|
* otherwise, @length*2+1. */
|
||||||
|
|
||||||
for (;;) {
|
if (length > 0) {
|
||||||
const guint8 v = *in++;
|
nm_assert (in);
|
||||||
|
for (;;) {
|
||||||
|
const guint8 v = *in++;
|
||||||
|
|
||||||
*out++ = LOOKUP[v >> 4];
|
*out++ = LOOKUP[v >> 4];
|
||||||
*out++ = LOOKUP[v & 0x0F];
|
*out++ = LOOKUP[v & 0x0F];
|
||||||
length--;
|
length--;
|
||||||
if (!length)
|
if (!length)
|
||||||
break;
|
break;
|
||||||
if (delimiter)
|
if (delimiter)
|
||||||
*out++ = delimiter;
|
*out++ = delimiter;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
*out = 0;
|
*out = '\0';
|
||||||
return out0;
|
return out0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue