Blame shared/nm-glib-aux/nm-enum-utils.c

Packit Service 87a54e
/* SPDX-License-Identifier: LGPL-2.1-or-later */
Packit 5756e2
/*
Packit 5756e2
 * Copyright (C) 2017 Red Hat, Inc.
Packit 5756e2
 */
Packit 5756e2
Packit Service 2bceb2
#include "nm-glib-aux/nm-default-glib-i18n-lib.h"
Packit 5756e2
Packit 5756e2
#include "nm-enum-utils.h"
Packit 5756e2
#include "nm-str-buf.h"
Packit 5756e2
Packit 5756e2
/*****************************************************************************/
Packit 5756e2
Packit Service a1bd4f
#define IS_FLAGS_SEPARATOR(ch) (NM_IN_SET((ch), ' ', '\t', ',', '\n', '\r'))
Packit 5756e2
Packit 5756e2
static void
Packit Service a1bd4f
_ASSERT_enum_values_info(GType type, const NMUtilsEnumValueInfo *value_infos)
Packit 5756e2
{
Packit 5756e2
#if NM_MORE_ASSERTS > 5
Packit Service a1bd4f
    nm_auto_unref_gtypeclass GTypeClass *klass = NULL;
Packit Service a1bd4f
    gs_unref_hashtable GHashTable *ht          = NULL;
Packit Service a1bd4f
Packit Service a1bd4f
    klass = g_type_class_ref(type);
Packit Service a1bd4f
Packit Service a1bd4f
    g_assert(G_IS_ENUM_CLASS(klass) || G_IS_FLAGS_CLASS(klass));
Packit Service a1bd4f
Packit Service a1bd4f
    if (!value_infos)
Packit Service a1bd4f
        return;
Packit Service a1bd4f
Packit Service a1bd4f
    ht = g_hash_table_new(g_str_hash, g_str_equal);
Packit Service a1bd4f
Packit Service a1bd4f
    for (; value_infos->nick; value_infos++) {
Packit Service a1bd4f
        g_assert(value_infos->nick[0]);
Packit Service a1bd4f
Packit Service a1bd4f
        /* duplicate nicks make no sense!! */
Packit Service a1bd4f
        g_assert(!g_hash_table_contains(ht, value_infos->nick));
Packit Service a1bd4f
        g_hash_table_add(ht, (gpointer) value_infos->nick);
Packit Service a1bd4f
Packit Service a1bd4f
        if (G_IS_ENUM_CLASS(klass)) {
Packit Service a1bd4f
            GEnumValue *enum_value;
Packit Service a1bd4f
Packit Service a1bd4f
            enum_value = g_enum_get_value_by_nick(G_ENUM_CLASS(klass), value_infos->nick);
Packit Service a1bd4f
            if (enum_value) {
Packit Service a1bd4f
                /* we do allow specifying the same name via @value_infos and @type.
Packit Service a1bd4f
                 * That might make sense, if @type comes from a library where older versions
Packit Service a1bd4f
                 * of the library don't yet support the value. In this case, the caller can
Packit Service a1bd4f
                 * provide the nick via @value_infos, to support the older library version.
Packit Service a1bd4f
                 * And then, when actually running against a newer library version where
Packit Service a1bd4f
                 * @type knows the nick, we have this situation.
Packit Service a1bd4f
                 *
Packit Service a1bd4f
                 * Another reason for specifying a nick both in @value_infos and @type,
Packit Service a1bd4f
                 * is to specify an alias which is not used with highest preference. For
Packit Service a1bd4f
                 * example, if you add an alias "disabled" for "none" (both numerically
Packit Service a1bd4f
                 * equal), then the first alias in @value_infos will be preferred over
Packit Service a1bd4f
                 * the name from @type. So, to still use "none" as preferred name, you may
Packit Service a1bd4f
                 * explicitly specify the "none" alias in @value_infos before "disabled".
Packit Service a1bd4f
                 *
Packit Service a1bd4f
                 * However, what never is allowed, is to use a name (nick) to re-number
Packit Service a1bd4f
                 * the value. That is, if both @value_infos and @type contain a particular
Packit Service a1bd4f
                 * nick, their numeric values must agree as well.
Packit Service a1bd4f
                 * Allowing this, would be very confusing, because the name would have a different
Packit Service a1bd4f
                 * value from the regular GLib GEnum API.
Packit Service a1bd4f
                 */
Packit Service a1bd4f
                g_assert(enum_value->value == value_infos->value);
Packit Service a1bd4f
            }
Packit Service a1bd4f
        } else {
Packit Service a1bd4f
            GFlagsValue *flags_value;
Packit Service a1bd4f
Packit Service a1bd4f
            flags_value = g_flags_get_value_by_nick(G_FLAGS_CLASS(klass), value_infos->nick);
Packit Service a1bd4f
            if (flags_value) {
Packit Service a1bd4f
                /* see ENUM case above. */
Packit Service a1bd4f
                g_assert(flags_value->value == (guint) value_infos->value);
Packit Service a1bd4f
            }
Packit Service a1bd4f
        }
Packit Service a1bd4f
    }
Packit 5756e2
#endif
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static gboolean
Packit Service c219b0
_is_hex_string(const char *str, gboolean allow_sign)
Packit 5756e2
{
Packit Service c219b0
    if (allow_sign && str[0] == '-')
Packit Service c219b0
        str++;
Packit Service a1bd4f
    return str[0] == '0' && str[1] == 'x' && str[2]
Packit Service a1bd4f
           && NM_STRCHAR_ALL(&str[2], ch, g_ascii_isxdigit(ch));
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static gboolean
Packit Service c219b0
_is_dec_string(const char *str, gboolean allow_sign)
Packit 5756e2
{
Packit Service c219b0
    if (allow_sign && str[0] == '-')
Packit Service c219b0
        str++;
Packit Service a1bd4f
    return str[0] && NM_STRCHAR_ALL(&str[0], ch, g_ascii_isdigit(ch));
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static gboolean
Packit Service a1bd4f
_enum_is_valid_enum_nick(const char *str)
Packit 5756e2
{
Packit Service c219b0
    return str[0] && !NM_STRCHAR_ANY(str, ch, g_ascii_isspace(ch)) && !_is_dec_string(str, TRUE)
Packit Service c219b0
           && !_is_hex_string(str, TRUE);
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static gboolean
Packit Service a1bd4f
_enum_is_valid_flags_nick(const char *str)
Packit 5756e2
{
Packit Service c219b0
    return str[0] && !NM_STRCHAR_ANY(str, ch, IS_FLAGS_SEPARATOR(ch)) && !_is_dec_string(str, FALSE)
Packit Service c219b0
           && !_is_hex_string(str, FALSE);
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
char *
Packit Service a1bd4f
_nm_utils_enum_to_str_full(GType                       type,
Packit Service a1bd4f
                           int                         value,
Packit Service a1bd4f
                           const char *                flags_separator,
Packit Service a1bd4f
                           const NMUtilsEnumValueInfo *value_infos)
Packit 5756e2
{
Packit Service a1bd4f
    nm_auto_unref_gtypeclass GTypeClass *klass = NULL;
Packit Service a1bd4f
Packit Service a1bd4f
    _ASSERT_enum_values_info(type, value_infos);
Packit Service a1bd4f
Packit Service a1bd4f
    if (flags_separator
Packit Service a1bd4f
        && (!flags_separator[0] || NM_STRCHAR_ANY(flags_separator, ch, !IS_FLAGS_SEPARATOR(ch))))
Packit Service a1bd4f
        g_return_val_if_reached(NULL);
Packit Service a1bd4f
Packit Service a1bd4f
    klass = g_type_class_ref(type);
Packit Service a1bd4f
Packit Service a1bd4f
    if (G_IS_ENUM_CLASS(klass)) {
Packit Service a1bd4f
        GEnumValue *enum_value;
Packit Service a1bd4f
Packit Service a1bd4f
        for (; value_infos && value_infos->nick; value_infos++) {
Packit Service a1bd4f
            if (value_infos->value == value)
Packit Service a1bd4f
                return g_strdup(value_infos->nick);
Packit Service a1bd4f
        }
Packit Service a1bd4f
Packit Service a1bd4f
        enum_value = g_enum_get_value(G_ENUM_CLASS(klass), value);
Packit Service a1bd4f
        if (!enum_value || !_enum_is_valid_enum_nick(enum_value->value_nick))
Packit Service a1bd4f
            return g_strdup_printf("%d", value);
Packit Service a1bd4f
        else
Packit Service a1bd4f
            return g_strdup(enum_value->value_nick);
Packit Service a1bd4f
    } else if (G_IS_FLAGS_CLASS(klass)) {
Packit Service a1bd4f
        unsigned     uvalue = (unsigned) value;
Packit Service a1bd4f
        GFlagsValue *flags_value;
Packit Service a1bd4f
        NMStrBuf     strbuf;
Packit Service a1bd4f
Packit Service a1bd4f
        flags_separator = flags_separator ?: " ";
Packit Service a1bd4f
Packit Service a1bd4f
        nm_str_buf_init(&strbuf, 16, FALSE);
Packit Service a1bd4f
Packit Service a1bd4f
        for (; value_infos && value_infos->nick; value_infos++) {
Packit Service a1bd4f
            nm_assert(_enum_is_valid_flags_nick(value_infos->nick));
Packit Service a1bd4f
Packit Service a1bd4f
            if (uvalue == 0) {
Packit Service a1bd4f
                if (value_infos->value != 0)
Packit Service a1bd4f
                    continue;
Packit Service a1bd4f
            } else {
Packit Service a1bd4f
                if (!NM_FLAGS_ALL(uvalue, (unsigned) value_infos->value))
Packit Service a1bd4f
                    continue;
Packit Service a1bd4f
            }
Packit Service a1bd4f
Packit Service a1bd4f
            if (strbuf.len)
Packit Service a1bd4f
                nm_str_buf_append(&strbuf, flags_separator);
Packit Service a1bd4f
            nm_str_buf_append(&strbuf, value_infos->nick);
Packit Service a1bd4f
            uvalue &= ~((unsigned) value_infos->value);
Packit Service a1bd4f
            if (uvalue == 0) {
Packit Service a1bd4f
                /* we printed all flags. Done. */
Packit Service a1bd4f
                goto flags_done;
Packit Service a1bd4f
            }
Packit Service a1bd4f
        }
Packit Service a1bd4f
Packit Service a1bd4f
        do {
Packit Service a1bd4f
            flags_value = g_flags_get_first_value(G_FLAGS_CLASS(klass), uvalue);
Packit Service a1bd4f
            if (strbuf.len)
Packit Service a1bd4f
                nm_str_buf_append(&strbuf, flags_separator);
Packit Service a1bd4f
            if (!flags_value || !_enum_is_valid_flags_nick(flags_value->value_nick)) {
Packit Service a1bd4f
                if (uvalue)
Packit Service a1bd4f
                    nm_str_buf_append_printf(&strbuf, "0x%x", uvalue);
Packit Service a1bd4f
                break;
Packit Service a1bd4f
            }
Packit Service a1bd4f
            nm_str_buf_append(&strbuf, flags_value->value_nick);
Packit Service a1bd4f
            uvalue &= ~flags_value->value;
Packit Service a1bd4f
        } while (uvalue);
Packit 5756e2
Packit 5756e2
flags_done:
Packit Service a1bd4f
        return nm_str_buf_finalize(&strbuf, NULL);
Packit Service a1bd4f
    }
Packit 5756e2
Packit Service a1bd4f
    g_return_val_if_reached(NULL);
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static const NMUtilsEnumValueInfo *
Packit Service a1bd4f
_find_value_info(const NMUtilsEnumValueInfo *value_infos, const char *needle)
Packit 5756e2
{
Packit Service a1bd4f
    if (value_infos) {
Packit Service a1bd4f
        for (; value_infos->nick; value_infos++) {
Packit Service a1bd4f
            if (nm_streq(needle, value_infos->nick))
Packit Service a1bd4f
                return value_infos;
Packit Service a1bd4f
        }
Packit Service a1bd4f
    }
Packit Service a1bd4f
    return NULL;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
gboolean
Packit Service a1bd4f
_nm_utils_enum_from_str_full(GType                       type,
Packit Service a1bd4f
                             const char *                str,
Packit Service a1bd4f
                             int *                       out_value,
Packit Service a1bd4f
                             char **                     err_token,
Packit Service a1bd4f
                             const NMUtilsEnumValueInfo *value_infos)
Packit 5756e2
{
Packit Service a1bd4f
    nm_auto_unref_gtypeclass GTypeClass *klass     = NULL;
Packit Service a1bd4f
    gboolean                             ret       = FALSE;
Packit Service a1bd4f
    int                                  value     = 0;
Packit Service a1bd4f
    gs_free char *                       str_clone = NULL;
Packit Service a1bd4f
    char *                               s;
Packit Service a1bd4f
    gint64                               v64;
Packit Service a1bd4f
    const NMUtilsEnumValueInfo *         nick;
Packit Service a1bd4f
Packit Service a1bd4f
    g_return_val_if_fail(str, FALSE);
Packit Service a1bd4f
Packit Service a1bd4f
    _ASSERT_enum_values_info(type, value_infos);
Packit Service a1bd4f
Packit Service c219b0
    s = nm_strdup_maybe_a(300, nm_str_skip_leading_spaces(str), &str_clone);
Packit Service a1bd4f
    g_strchomp(s);
Packit Service a1bd4f
Packit Service a1bd4f
    klass = g_type_class_ref(type);
Packit Service a1bd4f
Packit Service a1bd4f
    if (G_IS_ENUM_CLASS(klass)) {
Packit Service a1bd4f
        GEnumValue *enum_value;
Packit Service a1bd4f
Packit Service c219b0
        G_STATIC_ASSERT(G_MAXINT < G_MAXINT64);
Packit Service c219b0
        G_STATIC_ASSERT(G_MININT > G_MININT64);
Packit Service c219b0
Packit Service a1bd4f
        if (s[0]) {
Packit Service c219b0
            if (_is_hex_string(s, TRUE)) {
Packit Service c219b0
                if (s[0] == '-') {
Packit Service c219b0
                    v64 = _nm_utils_ascii_str_to_int64(&s[3],
Packit Service c219b0
                                                       16,
Packit Service c219b0
                                                       -((gint64) G_MAXINT),
Packit Service c219b0
                                                       -((gint64) G_MININT),
Packit Service c219b0
                                                       G_MAXINT64);
Packit Service c219b0
                    if (v64 != G_MAXINT64) {
Packit Service c219b0
                        value = (int) (-v64);
Packit Service c219b0
                        ret   = TRUE;
Packit Service c219b0
                    }
Packit Service c219b0
                } else {
Packit Service c219b0
                    v64 = _nm_utils_ascii_str_to_int64(&s[2], 16, G_MININT, G_MAXINT, G_MAXINT64);
Packit Service c219b0
                    if (v64 != G_MAXINT64) {
Packit Service c219b0
                        value = (int) v64;
Packit Service c219b0
                        ret   = TRUE;
Packit Service c219b0
                    }
Packit Service a1bd4f
                }
Packit Service c219b0
            } else if (_is_dec_string(s, TRUE)) {
Packit Service c219b0
                v64 = _nm_utils_ascii_str_to_int64(s, 10, G_MININT, G_MAXINT, G_MAXINT64);
Packit Service c219b0
                if (v64 != G_MAXINT64) {
Packit Service a1bd4f
                    value = (int) v64;
Packit Service a1bd4f
                    ret   = TRUE;
Packit Service a1bd4f
                }
Packit Service a1bd4f
            } else if ((nick = _find_value_info(value_infos, s))) {
Packit Service a1bd4f
                value = nick->value;
Packit Service a1bd4f
                ret   = TRUE;
Packit Service a1bd4f
            } else if ((enum_value = g_enum_get_value_by_nick(G_ENUM_CLASS(klass), s))) {
Packit Service a1bd4f
                value = enum_value->value;
Packit Service a1bd4f
                ret   = TRUE;
Packit Service a1bd4f
            }
Packit Service a1bd4f
        }
Packit Service a1bd4f
    } else if (G_IS_FLAGS_CLASS(klass)) {
Packit Service a1bd4f
        GFlagsValue *flags_value;
Packit Service a1bd4f
        unsigned     uvalue = 0;
Packit Service a1bd4f
Packit Service a1bd4f
        ret = TRUE;
Packit Service a1bd4f
        while (s[0]) {
Packit Service a1bd4f
            char *s_end;
Packit Service a1bd4f
Packit Service a1bd4f
            for (s_end = s; s_end[0]; s_end++) {
Packit Service a1bd4f
                if (IS_FLAGS_SEPARATOR(s_end[0])) {
Packit Service a1bd4f
                    s_end[0] = '\0';
Packit Service a1bd4f
                    s_end++;
Packit Service a1bd4f
                    break;
Packit Service a1bd4f
                }
Packit Service a1bd4f
            }
Packit Service a1bd4f
Packit Service a1bd4f
            if (s[0]) {
Packit Service c219b0
                if (_is_hex_string(s, FALSE)) {
Packit Service a1bd4f
                    v64 = _nm_utils_ascii_str_to_int64(&s[2], 16, 0, G_MAXUINT, -1);
Packit Service a1bd4f
                    if (v64 == -1) {
Packit Service a1bd4f
                        ret = FALSE;
Packit Service a1bd4f
                        break;
Packit Service a1bd4f
                    }
Packit Service a1bd4f
                    uvalue |= (unsigned) v64;
Packit Service c219b0
                } else if (_is_dec_string(s, FALSE)) {
Packit Service a1bd4f
                    v64 = _nm_utils_ascii_str_to_int64(s, 10, 0, G_MAXUINT, -1);
Packit Service a1bd4f
                    if (v64 == -1) {
Packit Service a1bd4f
                        ret = FALSE;
Packit Service a1bd4f
                        break;
Packit Service a1bd4f
                    }
Packit Service a1bd4f
                    uvalue |= (unsigned) v64;
Packit Service a1bd4f
                } else if ((nick = _find_value_info(value_infos, s)))
Packit Service a1bd4f
                    uvalue |= (unsigned) nick->value;
Packit Service a1bd4f
                else if ((flags_value = g_flags_get_value_by_nick(G_FLAGS_CLASS(klass), s)))
Packit Service a1bd4f
                    uvalue |= flags_value->value;
Packit Service a1bd4f
                else {
Packit Service a1bd4f
                    ret = FALSE;
Packit Service a1bd4f
                    break;
Packit Service a1bd4f
                }
Packit Service a1bd4f
            }
Packit Service a1bd4f
Packit Service a1bd4f
            s = s_end;
Packit Service a1bd4f
        }
Packit Service a1bd4f
Packit Service a1bd4f
        value = (int) uvalue;
Packit Service a1bd4f
    } else
Packit Service a1bd4f
        g_return_val_if_reached(FALSE);
Packit Service a1bd4f
Packit Service a1bd4f
    NM_SET_OUT(err_token, !ret && s[0] ? g_strdup(s) : NULL);
Packit Service a1bd4f
    NM_SET_OUT(out_value, ret ? value : 0);
Packit Service a1bd4f
    return ret;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
const char **
Packit Service a1bd4f
_nm_utils_enum_get_values(GType type, int from, int to)
Packit 5756e2
{
Packit Service a1bd4f
    GTypeClass *klass;
Packit Service a1bd4f
    GPtrArray * array;
Packit Service a1bd4f
    int         i;
Packit Service a1bd4f
    char        sbuf[64];
Packit Service a1bd4f
Packit Service a1bd4f
    klass = g_type_class_ref(type);
Packit Service a1bd4f
    array = g_ptr_array_new();
Packit Service a1bd4f
Packit Service a1bd4f
    if (G_IS_ENUM_CLASS(klass)) {
Packit Service a1bd4f
        GEnumClass *enum_class = G_ENUM_CLASS(klass);
Packit Service a1bd4f
        GEnumValue *enum_value;
Packit Service a1bd4f
Packit Service a1bd4f
        for (i = 0; i < enum_class->n_values; i++) {
Packit Service a1bd4f
            enum_value = &enum_class->values[i];
Packit Service a1bd4f
            if (enum_value->value >= from && enum_value->value <= to) {
Packit Service a1bd4f
                if (_enum_is_valid_enum_nick(enum_value->value_nick))
Packit Service a1bd4f
                    g_ptr_array_add(array, (gpointer) enum_value->value_nick);
Packit Service a1bd4f
                else
Packit Service a1bd4f
                    g_ptr_array_add(
Packit Service a1bd4f
                        array,
Packit Service a1bd4f
                        (gpointer) g_intern_string(nm_sprintf_buf(sbuf, "%d", enum_value->value)));
Packit Service a1bd4f
            }
Packit Service a1bd4f
        }
Packit Service a1bd4f
    } else if (G_IS_FLAGS_CLASS(klass)) {
Packit Service a1bd4f
        GFlagsClass *flags_class = G_FLAGS_CLASS(klass);
Packit Service a1bd4f
        GFlagsValue *flags_value;
Packit Service a1bd4f
Packit Service a1bd4f
        for (i = 0; i < flags_class->n_values; i++) {
Packit Service a1bd4f
            flags_value = &flags_class->values[i];
Packit Service a1bd4f
            if (flags_value->value >= (guint) from && flags_value->value <= (guint) to) {
Packit Service a1bd4f
                if (_enum_is_valid_flags_nick(flags_value->value_nick))
Packit Service a1bd4f
                    g_ptr_array_add(array, (gpointer) flags_value->value_nick);
Packit Service a1bd4f
                else
Packit Service a1bd4f
                    g_ptr_array_add(
Packit Service a1bd4f
                        array,
Packit Service a1bd4f
                        (gpointer) g_intern_string(
Packit Service a1bd4f
                            nm_sprintf_buf(sbuf, "0x%x", (unsigned) flags_value->value)));
Packit Service a1bd4f
            }
Packit Service a1bd4f
        }
Packit Service a1bd4f
    } else {
Packit Service a1bd4f
        g_type_class_unref(klass);
Packit Service a1bd4f
        g_ptr_array_free(array, TRUE);
Packit Service a1bd4f
        g_return_val_if_reached(NULL);
Packit Service a1bd4f
    }
Packit Service a1bd4f
Packit Service a1bd4f
    g_type_class_unref(klass);
Packit Service a1bd4f
    g_ptr_array_add(array, NULL);
Packit Service a1bd4f
Packit Service a1bd4f
    return (const char **) g_ptr_array_free(array, FALSE);
Packit 5756e2
}