Improve security level logging at ISO layer

This commit is contained in:
matt335672
2021-08-26 15:31:38 +01:00
parent b56bd78c08
commit 62ca216d4d
4 changed files with 320 additions and 25 deletions
+66
View File
@@ -891,3 +891,69 @@ g_strnjoin(char *dest, int dest_len, const char *joiner, const char *src[], int
return dest;
}
int
g_bitmask_to_str(int bitmask, const struct bitmask_string bitdefs[],
char delim, char *buff, int bufflen)
{
int rlen = 0; /* Returned length */
if (bufflen <= 0) /* Caller error */
{
rlen = -1;
}
else
{
char *p = buff;
/* Find the last writeable character in the buffer */
const char *last = buff + (bufflen - 1);
const struct bitmask_string *b;
for (b = &bitdefs[0] ; b->mask != 0; ++b)
{
if ((bitmask & b->mask) != 0)
{
if (p > buff)
{
/* Not first item - append separator */
if (p < last)
{
*p++ = delim;
}
++rlen;
}
int slen = g_strlen(b->str);
int copylen = MIN(slen, last - p);
g_memcpy(p, b->str, copylen);
p += copylen;
rlen += slen;
/* Remove the bit so we can check for undefined bits later*/
bitmask &= ~b->mask;
}
}
if (bitmask != 0)
{
/* Bits left which aren't named by the user */
if (p > buff)
{
if (p < last)
{
*p++ = delim;
}
++rlen;
}
/* This call will terminate the return buffer */
rlen += g_snprintf(p, last - p + 1, "0x%x", bitmask);
}
else
{
*p = '\0';
}
}
return rlen;
}
+34
View File
@@ -37,6 +37,21 @@ struct info_string_tag
#define INFO_STRING_END_OF_LIST { '\0', NULL }
/**
* Map a bitmask to a string value
*
*
* This structure is used by g_bitmask_to_str() to specify the
* string for each bit in the bitmask
*/
struct bitmask_string
{
int mask;
const char *str;
};
#define BITMASK_STRING_END_OF_LIST { 0, NULL }
/**
* Processes a format string for general info
*
@@ -140,6 +155,25 @@ g_bytes_to_hexdump(const char *src, int len);
int
g_get_display_num_from_display(const char *display_text);
/**
* Converts a bitmask into a string for output purposes
*
* @param bitmask Bitmask to convert
* @param bitdefs Definitions for strings for bits
* @param delim Delimiter to use between strings
* @param buff Output buff
* @param bufflen Length of buff, including terminator '`\0'
*
* @return Total length excluding terminator which would be written, as
* in snprintf(). Can be used to check for overflow
*
* @note Any undefined bits in the bitmask are appended to the output as
* a hexadecimal constant.
*/
int
g_bitmask_to_str(int bitmask, const struct bitmask_string[],
char delim, char *buff, int bufflen);
int g_strlen(const char *text);
const char *g_strchr(const char *text, int c);
const char *g_strnchr(const char *text, int c, int len);