1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
|
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA.
*
* Copyright 2007 - 2008 Novell, Inc.
* Copyright 2007 - 2017 Red Hat, Inc.
*/
#include "nm-default.h"
#include "nm-libnm-utils.h"
#include <girepository.h>
/*****************************************************************************/
char *
nm_utils_fixup_desc_string (const char *desc)
{
static const char *const IGNORED_PHRASES[] = {
"Multiprotocol MAC/baseband processor",
"Wireless LAN Controller",
"Wireless LAN Adapter",
"Wireless Adapter",
"Network Connection",
"Wireless Cardbus Adapter",
"Wireless CardBus Adapter",
"54 Mbps Wireless PC Card",
"Wireless PC Card",
"Wireless PC",
"PC Card with XJACK(r) Antenna",
"Wireless cardbus",
"Wireless LAN PC Card",
"Technology Group Ltd.",
"Communication S.p.A.",
"Business Mobile Networks BV",
"Mobile Broadband Minicard Composite Device",
"Mobile Communications AB",
"(PC-Suite Mode)",
};
static const char *const IGNORED_WORDS[] = {
"Semiconductor",
"Components",
"Corporation",
"Communications",
"Company",
"Corp.",
"Corp",
"Co.",
"Inc.",
"Inc",
"Incorporated",
"Ltd.",
"Limited.",
"Intel?",
"chipset",
"adapter",
"[hex]",
"NDIS",
"Module",
};
char *desc_full;
char *p, *q;
int i;
if (!desc || !desc[0])
return NULL;
/* restore original non-UTF-8-safe text. */
desc_full = nm_utils_str_utf8safe_unescape_cp (desc);
/* replace all invalid UTF-8 bytes with space. */
p = desc_full;
while (!g_utf8_validate (p, -1, (const char **) &q)) {
/* the byte is invalid UTF-8. Replace it with space and proceed. */
*q = ' ';
p = q + 1;
}
/* replace '_', ',', and ASCII controll characters with space. */
for (p = desc_full; p[0]; p++) {
if ( NM_IN_SET (*p, '_', ',')
|| *p < ' ')
*p = ' ';
}
/* Attempt to shorten ID by ignoring certain phrases */
for (i = 0; i < G_N_ELEMENTS (IGNORED_PHRASES); i++) {
p = strstr (desc_full, IGNORED_PHRASES[i]);
if (p) {
const char *eow = &p[strlen (IGNORED_PHRASES[i])];
/* require that the phrase is delimited by space, or
* at the beginning or end of the description. */
if ( (p == desc_full || p[-1] == ' ')
&& NM_IN_SET (eow[0], '\0', ' '))
memmove (p, eow, strlen (eow) + 1); /* +1 for the \0 */
}
}
/* Attempt to shorten ID by ignoring certain individual words.
* - word-split the description at spaces
* - coalesce multiple spaces
* - skip over IGNORED_WORDS */
p = desc_full;
q = desc_full;
for (;;) {
char *eow;
gsize l;
/* skip leading spaces. */
while (p[0] == ' ')
p++;
if (!p[0])
break;
/* split leading word on first space */
eow = strchr (p, ' ');
if (eow)
*eow = '\0';
if (nm_utils_strv_find_first ((char **) IGNORED_WORDS,
G_N_ELEMENTS (IGNORED_WORDS),
p) >= 0)
goto next;
l = strlen (p);
if (q != p) {
if (q != desc_full)
*q++ = ' ';
memmove (q, p, l);
}
q += l;
next:
if (!eow)
break;
p = eow + 1;
}
*q++ = '\0';
if (!desc_full[0]) {
g_free (desc_full);
return NULL;
}
nm_assert (g_utf8_validate (desc_full, -1, NULL));
return desc_full;
}
#if WITH_FAKE_TYPELIBS
/*
* Here we register empty "NMClient" and "NetworkManager" GIR modules as soon
* as we are loaded (if gnome-introspection is being used). This prevents the
* real modules from being loaded because they would in turn load libnm-glib
* and abort() and crash.
*
* For the high level languages that utilize GIR the crash is highly inconvenient
* while the inability to resolve any methods and attributes is potentially
* recoverable.
*/
GResource *typelibs_get_resource (void);
void typelibs_register_resource (void);
static void __attribute__((constructor))
_nm_libnm_utils_init (void)
{
GITypelib *typelib;
GBytes *data;
const char *namespace;
GModule *self;
GITypelib *(*_g_typelib_new_from_const_memory) (const guint8 *memory,
gsize len,
GError **error) = NULL;
const char *(*_g_irepository_load_typelib) (GIRepository *repository,
GITypelib *typelib,
GIRepositoryLoadFlags flags,
GError **error) = NULL;
const char *names[] = { "/org/freedesktop/libnm/fake-typelib/NetworkManager.typelib",
"/org/freedesktop/libnm/fake-typelib/NMClient.typelib" };
int i;
self = g_module_open (NULL, 0);
if (!self)
return;
g_module_symbol (self, "g_typelib_new_from_const_memory",
(gpointer *) &_g_typelib_new_from_const_memory);
if (_g_typelib_new_from_const_memory) {
g_module_symbol (self, "g_irepository_load_typelib",
(gpointer *) &_g_irepository_load_typelib);
}
g_module_close (self);
if (!_g_typelib_new_from_const_memory || !_g_irepository_load_typelib)
return;
typelibs_register_resource ();
for (i = 0; i < 2; i++) {
gs_free_error GError *error = NULL;
data = g_resource_lookup_data (typelibs_get_resource (),
names[i],
G_RESOURCE_LOOKUP_FLAGS_NONE,
&error);
if (!data) {
g_warning ("Fake typelib %s could not be loaded: %s", names[i], error->message);
return;
}
typelib = _g_typelib_new_from_const_memory (g_bytes_get_data (data, NULL),
g_bytes_get_size (data),
&error);
if (!typelib) {
g_warning ("Could not create fake typelib instance %s: %s", names[i], error->message);
return;
}
namespace = _g_irepository_load_typelib (NULL, typelib, 0, &error);
if (!namespace) {
g_warning ("Could not load fake typelib %s: %s", names[i], error->message);
return;
}
}
}
#endif /* WITH_FAKE_TYPELIBS */
|