Coverage Report

Created: 2025-08-29 06:48

/src/fwupd/plugins/redfish/fu-redfish-common.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2017 Richard Hughes <richard@hughsie.com>
3
 *
4
 * SPDX-License-Identifier: LGPL-2.1-or-later
5
 */
6
7
#include "config.h"
8
9
#include <fwupd.h>
10
11
#include "fu-redfish-common.h"
12
13
gchar *
14
fu_redfish_common_buffer_to_ipv4(const guint8 *buffer)
15
931
{
16
931
  GString *str = g_string_new(NULL);
17
4.65k
  for (guint i = 0; i < 4; i++) {
18
3.72k
    g_string_append_printf(str, "%u", buffer[i]);
19
3.72k
    if (i != 3)
20
2.79k
      g_string_append(str, ".");
21
3.72k
  }
22
931
  return g_string_free(str, FALSE);
23
931
}
24
25
gchar *
26
fu_redfish_common_buffer_to_ipv6(const guint8 *buffer)
27
491
{
28
491
  GString *str = g_string_new(NULL);
29
2.45k
  for (guint i = 0; i < 16; i += 4) {
30
1.96k
    g_string_append_printf(str,
31
1.96k
               "%02x%02x%02x%02x",
32
1.96k
               buffer[i + 0],
33
1.96k
               buffer[i + 1],
34
1.96k
               buffer[i + 2],
35
1.96k
               buffer[i + 3]);
36
1.96k
    if (i != 12)
37
1.47k
      g_string_append(str, ":");
38
1.96k
  }
39
491
  return g_string_free(str, FALSE);
40
491
}
41
42
gchar *
43
fu_redfish_common_buffer_to_mac(const guint8 *buffer)
44
61
{
45
61
  GString *str = g_string_new(NULL);
46
427
  for (guint i = 0; i < 6; i++) {
47
366
    g_string_append_printf(str, "%02X", buffer[i]);
48
366
    if (i != 5)
49
305
      g_string_append(str, ":");
50
366
  }
51
61
  return g_string_free(str, FALSE);
52
61
}
53
54
gchar *
55
fu_redfish_common_fix_version(const gchar *version)
56
0
{
57
0
  g_auto(GStrv) split = NULL;
58
59
0
  g_return_val_if_fail(version != NULL, NULL);
60
61
  /* not valid */
62
0
  if (g_strcmp0(version, "-*") == 0)
63
0
    return NULL;
64
65
  /* find the section prefixed with "v" */
66
0
  split = g_strsplit(version, " ", -1);
67
0
  for (guint i = 0; split[i] != NULL; i++) {
68
0
    if (g_str_has_prefix(split[i], "v")) {
69
0
      g_debug("using %s for %s", split[i] + 1, version);
70
0
      return g_strdup(split[i] + 1);
71
0
    }
72
0
  }
73
74
  /* find the thing with dots */
75
0
  for (guint i = 0; split[i] != NULL; i++) {
76
0
    if (g_strstr_len(split[i], -1, ".")) {
77
0
      if (g_strcmp0(split[i], version) != 0)
78
0
        g_debug("using %s for %s", split[i], version);
79
0
      return g_strdup(split[i]);
80
0
    }
81
0
  }
82
83
  /* we failed to do anything clever */
84
0
  return g_strdup(version);
85
0
}
86
87
/* parses a Lenovo XCC-format version like "11A-1.02" */
88
gboolean
89
fu_redfish_common_parse_version_lenovo(const gchar *version,
90
               gchar **out_build,   /* out */
91
               gchar **out_version, /* out */
92
               GError **error)
93
0
{
94
0
  g_auto(GStrv) versplit = g_strsplit(version, "-", -1);
95
96
  /* sanity check */
97
0
  if (g_strv_length(versplit) != 2) {
98
0
    g_set_error_literal(error,
99
0
            FWUPD_ERROR,
100
0
            FWUPD_ERROR_INVALID_DATA,
101
0
            "not two sections");
102
0
    return FALSE;
103
0
  }
104
0
  if (strlen(versplit[0]) != 3) {
105
0
    g_set_error_literal(error,
106
0
            FWUPD_ERROR,
107
0
            FWUPD_ERROR_INVALID_DATA,
108
0
            "invalid length first section");
109
0
    return FALSE;
110
0
  }
111
112
  /* milestone */
113
0
  if (!g_ascii_isdigit(versplit[0][0]) || !g_ascii_isdigit(versplit[0][1])) {
114
0
    g_set_error_literal(error,
115
0
            FWUPD_ERROR,
116
0
            FWUPD_ERROR_INVALID_DATA,
117
0
            "milestone number invalid");
118
0
    return FALSE;
119
0
  }
120
121
  /* build is only one letter from A -> Z */
122
0
  if (!g_ascii_isalpha(versplit[0][2])) {
123
0
    g_set_error_literal(error,
124
0
            FWUPD_ERROR,
125
0
            FWUPD_ERROR_INVALID_DATA,
126
0
            "build letter invalid");
127
0
    return FALSE;
128
0
  }
129
130
  /* success */
131
0
  if (out_build != NULL)
132
0
    *out_build = g_strdup(versplit[0]);
133
0
  if (out_version != NULL)
134
0
    *out_version = g_strdup(versplit[1]);
135
0
  return TRUE;
136
0
}