30#define G_LOG_DOMAIN "libgvm util"
48index_of (
const char *,
const char *,
int);
186 g_free (uri_component);
189 g_free (uri_component);
192 g_free (uri_component);
195 g_free (uri_component);
198 g_free (uri_component);
200 if (strcmp (uri_component,
"") == 0 || strcmp (uri_component,
"-") == 0
201 || *uri_component !=
'~')
205 g_free (uri_component);
209 g_free (uri_component);
223 char *bind_cpe_component;
224 uri_cpe = g_string_new (
"cpe:/");
227 if (bind_cpe_component)
229 g_string_append (uri_cpe, bind_cpe_component);
230 g_string_append_c (uri_cpe,
':');
231 g_free (bind_cpe_component);
234 if (bind_cpe_component)
236 g_string_append (uri_cpe, bind_cpe_component);
237 g_string_append_c (uri_cpe,
':');
238 g_free (bind_cpe_component);
241 if (bind_cpe_component)
243 g_string_append (uri_cpe, bind_cpe_component);
244 g_string_append_c (uri_cpe,
':');
245 g_free (bind_cpe_component);
248 if (bind_cpe_component)
250 g_string_append (uri_cpe, bind_cpe_component);
251 g_string_append_c (uri_cpe,
':');
252 g_free (bind_cpe_component);
255 if (bind_cpe_component)
257 g_string_append (uri_cpe, bind_cpe_component);
258 g_string_append_c (uri_cpe,
':');
259 g_free (bind_cpe_component);
262 if (bind_cpe_component)
264 g_string_append (uri_cpe, bind_cpe_component);
265 g_string_append_c (uri_cpe,
':');
266 g_free (bind_cpe_component);
269 if (bind_cpe_component)
271 g_string_append (uri_cpe, bind_cpe_component);
272 g_string_append_c (uri_cpe,
':');
273 g_free (bind_cpe_component);
276 char *result = g_string_free (uri_cpe, FALSE);
292 char *bind_cpe_component;
293 uri_cpe = g_string_new (
"cpe:/");
296 if (bind_cpe_component)
298 g_string_append (uri_cpe, bind_cpe_component);
299 g_string_append_c (uri_cpe,
':');
300 g_free (bind_cpe_component);
303 if (bind_cpe_component)
305 g_string_append (uri_cpe, bind_cpe_component);
306 g_string_append_c (uri_cpe,
':');
307 g_free (bind_cpe_component);
310 if (bind_cpe_component)
312 g_string_append (uri_cpe, bind_cpe_component);
313 g_string_append_c (uri_cpe,
':');
314 g_free (bind_cpe_component);
317 char *result = g_string_free (uri_cpe, FALSE);
335 return decoded_version;
385 char *bind_cpe_component;
387 fs_cpe = g_string_new (
"cpe:2.3:");
390 if (bind_cpe_component)
392 g_string_append (fs_cpe, bind_cpe_component);
393 g_string_append_c (fs_cpe,
':');
394 g_free (bind_cpe_component);
397 if (bind_cpe_component)
399 g_string_append (fs_cpe, bind_cpe_component);
400 g_string_append_c (fs_cpe,
':');
401 g_free (bind_cpe_component);
404 if (bind_cpe_component)
406 g_string_append (fs_cpe, bind_cpe_component);
407 g_string_append_c (fs_cpe,
':');
408 g_free (bind_cpe_component);
411 if (bind_cpe_component)
413 g_string_append (fs_cpe, bind_cpe_component);
414 g_string_append_c (fs_cpe,
':');
415 g_free (bind_cpe_component);
418 if (bind_cpe_component)
420 g_string_append (fs_cpe, bind_cpe_component);
421 g_string_append_c (fs_cpe,
':');
422 g_free (bind_cpe_component);
425 if (bind_cpe_component)
427 g_string_append (fs_cpe, bind_cpe_component);
428 g_string_append_c (fs_cpe,
':');
429 g_free (bind_cpe_component);
432 if (bind_cpe_component)
434 g_string_append (fs_cpe, bind_cpe_component);
435 g_string_append_c (fs_cpe,
':');
436 g_free (bind_cpe_component);
439 if (bind_cpe_component)
441 g_string_append (fs_cpe, bind_cpe_component);
442 g_string_append_c (fs_cpe,
':');
443 g_free (bind_cpe_component);
446 if (bind_cpe_component)
448 g_string_append (fs_cpe, bind_cpe_component);
449 g_string_append_c (fs_cpe,
':');
450 g_free (bind_cpe_component);
453 if (bind_cpe_component)
455 g_string_append (fs_cpe, bind_cpe_component);
456 g_string_append_c (fs_cpe,
':');
457 g_free (bind_cpe_component);
460 if (bind_cpe_component)
462 g_string_append (fs_cpe, bind_cpe_component);
463 g_free (bind_cpe_component);
465 return (g_string_free (fs_cpe, FALSE));
479 char *bind_cpe_component;
481 fs_cpe = g_string_new (
"cpe:2.3:");
484 if (bind_cpe_component)
486 g_string_append (fs_cpe, bind_cpe_component);
487 g_string_append_c (fs_cpe,
':');
488 g_free (bind_cpe_component);
491 if (bind_cpe_component)
493 g_string_append (fs_cpe, bind_cpe_component);
494 g_string_append_c (fs_cpe,
':');
495 g_free (bind_cpe_component);
498 if (bind_cpe_component)
500 g_string_append (fs_cpe, bind_cpe_component);
501 g_string_append_c (fs_cpe,
':');
502 g_free (bind_cpe_component);
504 return (g_string_free (fs_cpe, FALSE));
518 char *component = NULL;
520 char *component_start, *component_end;
525 c = (
char *) uri_cpe;
528 for (
int i = 0; *c !=
'\0' && i < index; c++)
534 if (index == 1 && *c !=
'\0')
540 if (*component_start ==
'\0')
541 component_end = component_start;
544 for (c = component_start; *c !=
'\0' && *c !=
':'; c++)
550 if (component_start >= component_end || component_end == 0)
551 component = (
char *) g_strdup (
"");
553 component = g_strndup (component_start, component_end - component_start);
568 GString *decoded_component;
569 char *escapes =
"!\"#$%&'()*+,/:;<=>?@[\\]^`{|}~";
571 char code_a[4], code_b[4], code_c[4];
572 long unsigned int index;
578 if (strcmp (component,
"") == 0 || strcmp (component,
" ") == 0)
580 return (g_strdup (
"ANY"));
582 if (strcmp (component,
"-") == 0)
584 return (g_strdup (
"NA"));
587 tmp_component = g_strdup (component);
590 char *c = tmp_component;
596 decoded_component = g_string_sized_new (2 * strlen (component));
600 while (index < strlen (tmp_component))
602 l = *(tmp_component + index);
604 if (l ==
'.' || l ==
'-' || l ==
'~')
606 g_string_append_c (decoded_component,
'\\');
607 g_string_append_c (decoded_component, l);
614 g_string_append_c (decoded_component, l);
620 get_code (code_a, tmp_component + index);
622 if (strcmp (code_a,
"%01") == 0)
625 get_code (code_b, tmp_component + index - 3);
628 if (strlen (tmp_component) >= index + 6)
629 get_code (code_c, tmp_component + index + 3);
632 if ((index == 0 || index == strlen (tmp_component) - 3)
633 || (!embedded && strcmp (code_b,
"%01"))
634 || (embedded && strcmp (code_c,
"%01")))
636 g_string_append_c (decoded_component,
'?');
642 g_string_free (decoded_component, TRUE);
643 g_free (tmp_component);
648 if (strcmp (code_a,
"%02") == 0)
650 if (index == 0 || index == strlen (tmp_component) - 3)
652 g_string_append_c (decoded_component,
'*');
658 g_string_free (decoded_component, TRUE);
659 g_free (tmp_component);
664 unescaped = g_uri_unescape_string (code_a, NULL);
665 if (unescaped && strchr (escapes, *unescaped))
667 g_string_append_c (decoded_component,
'\\');
668 g_string_append (decoded_component, unescaped);
673 g_string_append (decoded_component, unescaped);
678 g_string_free (decoded_component, TRUE);
679 g_free (tmp_component);
686 g_free (tmp_component);
687 return (g_string_free (decoded_component, FALSE));
701 const char *start = component + 1;
704 char *edition, *sw_edition, *target_sw, *target_hw, *other;
706 end = strchr (start,
'~');
707 if (start >= end || end == NULL)
708 edition = strdup (
"");
710 edition = g_strndup (start, end - start);
715 end = strchr (start,
'~');
716 if (start >= end || end == NULL)
717 sw_edition = strdup (
"");
719 sw_edition = g_strndup (start, end - start);
722 sw_edition = strdup (
"");
727 end = strchr (start,
'~');
728 if (start >= end || end == NULL)
729 target_sw = strdup (
"");
731 target_sw = g_strndup (start, end - start);
734 target_sw = strdup (
"");
739 end = strchr (start,
'~');
740 if (start >= end || end == NULL)
741 target_hw = strdup (
"");
743 target_hw = g_strndup (start, end - start);
746 target_hw = strdup (
"");
751 end = component + strlen (component);
755 other = g_strndup (start, end - start);
783 char *component = NULL;
785 char *component_start, *component_end;
791 return ((
char *) g_strdup (
""));
800 for (
int i = 0; *c !=
'\0' && i < index; c++)
802 if (*c ==
':' && c == fs_cpe)
804 else if (c > fs_cpe && *c ==
':' && *(c - 1) !=
'\\')
811 if (*component_start ==
'\0')
812 component_end = component_start;
815 for (c = component_start; *c !=
'\0' && *c !=
':'; c++)
821 if (component_start >= component_end || component_end == NULL)
822 component = (
char *) g_strdup (
"");
824 component = g_strndup (component_start, component_end - component_start);
839 char *unbound_component;
841 if (strcmp (component,
"*") == 0)
844 return ((
char *) g_strdup (
"ANY"));
846 if (strcmp (component,
"-") == 0)
849 return ((
char *) g_strdup (
"NA"));
854 return (unbound_component);
868 GString *quoted_component;
876 quoted_component = g_string_sized_new (2 * strlen (component));
877 tmp_component = (
char *) g_strdup (component);
881 for (c = tmp_component; *c; c++)
887 if (g_ascii_isalnum (*c) || *c ==
'_')
889 g_string_append_c (quoted_component, *c);
899 g_string_append_c (quoted_component,
'\\');
900 g_string_append_c (quoted_component, *c);
908 if ((c == tmp_component)
909 || (c == tmp_component + strlen (tmp_component - 1)))
911 g_string_append_c (quoted_component, *c);
918 g_free (tmp_component);
924 if ((c == tmp_component)
925 || (c == tmp_component + strlen (tmp_component - 1))
926 || (!embedded && (c > tmp_component) && (*(c - 1) ==
'?'))
927 || (embedded && *(c + 1) ==
'?'))
929 g_string_append_c (quoted_component, *c);
936 g_free (tmp_component);
940 g_string_append_c (quoted_component,
'\\');
941 g_string_append_c (quoted_component, *c);
945 g_free (tmp_component);
946 return (g_string_free (quoted_component, FALSE));
960 return (g_strdup (
""));
961 if (strcmp (component,
"") == 0)
962 return (g_strdup (
""));
963 if (strcmp (component,
"ANY") == 0)
964 return (g_strdup (
""));
965 if (strcmp (component,
"NA") == 0)
966 return (g_strdup (
"-"));
985 return (g_strdup (
""));
986 if (strcmp (component,
"") == 0)
987 return (g_strdup (
""));
989 tmp_component = g_strdup (component);
992 for (c = tmp_component; *c; c++)
995 result = g_string_new (
"");
1000 if ((g_ascii_isalnum (*c) || *c ==
'_') && *c !=
'-')
1002 g_string_append_c (result, *c);
1014 to_escape[1] =
'\0';
1015 escaped = g_uri_escape_string (to_escape, NULL, FALSE);
1016 g_string_append (result, escaped);
1023 g_string_append (result,
"%01");
1025 g_string_append (result,
"%02");
1028 g_free (tmp_component);
1029 return (g_string_free (result, FALSE));
1046 && (cpe->
other == NULL || strcmp (cpe->
other,
"") == 0))
1048 if (strcmp (cpe->
edition,
"ANY") == 0)
1049 return (g_strdup (
""));
1050 if (strcmp (cpe->
edition,
"NA") == 0)
1051 return (g_strdup (
"-"));
1052 return (g_strdup (cpe->
edition));
1061 component = g_string_new (
"");
1062 if (!((!sw_edition || strcmp (sw_edition,
"") == 0)
1063 && (!target_sw || strcmp (target_sw,
"") == 0)
1064 && (!target_hw || strcmp (target_hw,
"") == 0)
1065 && (!other || strcmp (other,
"") == 0)))
1066 g_string_append_printf (component,
"~%s~%s~%s~%s~%s", edition, sw_edition,
1067 target_sw, target_hw, other);
1069 g_string_append (component, edition);
1074 g_free (sw_edition);
1081 return (g_string_free (component, FALSE));
1095 return (g_strdup (
"*"));
1096 if (strcmp (component,
"") == 0)
1097 return (g_strdup (
"*"));
1098 if (strcmp (component,
"ANY") == 0)
1099 return (g_strdup (
"*"));
1100 if (strcmp (component,
"NA") == 0)
1101 return (g_strdup (
"-"));
1117 return (g_strdup (
""));
1118 if (strcmp (component,
"") == 0)
1119 return (g_strdup (
""));
1121 GString *fs_component;
1122 fs_component = g_string_new (
"");
1123 char *c = (
char *) component;
1130 g_string_append_c (fs_component, *c);
1136 if (next_c ==
'.' || next_c ==
'-' || next_c ==
'_')
1138 g_string_append_c (fs_component, next_c);
1143 g_string_append_c (fs_component,
'\\');
1144 g_string_append_c (fs_component, next_c);
1149 return (g_string_free (fs_component, FALSE));
1205 g_free (cpe->
other);
1222 c = str + strlen (str) - 1;
1245 code[1] = *(str + 1);
1246 code[2] = *(str + 2);
1362 char *source_cpy, *target_cpy;
1366 source_cpy = g_strdup (source);
1368 source_cpy = g_strdup (
"ANY");
1370 target_cpy = g_strdup (target);
1372 target_cpy = g_strdup (
"ANY");
1377 for (c = source_cpy; *c; c++)
1383 for (c = target_cpy; *c; c++)
1388 g_free (source_cpy);
1389 g_free (target_cpy);
1392 if (strcmp (source_cpy, target_cpy) == 0)
1394 g_free (source_cpy);
1395 g_free (target_cpy);
1398 if (strcmp (source_cpy,
"ANY") == 0)
1400 g_free (source_cpy);
1401 g_free (target_cpy);
1404 if (strcmp (target_cpy,
"ANY") == 0)
1406 g_free (source_cpy);
1407 g_free (target_cpy);
1410 if (strcmp (target_cpy,
"NA") == 0 || strcmp (source_cpy,
"NA") == 0)
1412 g_free (source_cpy);
1413 g_free (target_cpy);
1418 g_free (source_cpy);
1419 g_free (target_cpy);
1439 int end = strlen (source);
1452 while (start < (
int) strlen (source) && *(source + start) ==
'?')
1465 while (end > 0 && *(source + end - 1) ==
'?'
1473 sub_source = g_strndup (source + start, end - start);
1476 int leftover = strlen (target);
1478 while (leftover > 0)
1480 index =
index_of (target, sub_source, index + 1);
1484 if (index > 0 && begins != -1 && begins < (index - escapes))
1486 escapes =
count_escapes (target, index + 1, strlen (target));
1487 leftover = strlen (target) - index - escapes - strlen (sub_source);
1488 if (leftover > 0 && (ends != -1 && leftover > ends))
1490 g_free (sub_source);
1493 g_free (sub_source);
1514 gboolean active = FALSE;
1516 for (
int i = 0; i < end && *(str + i) !=
'\0'; i++)
1518 active = (!active && *(str + i) ==
'\\');
1519 if (active && i >= start)
1540 while (index > 0 && *(str + index - 1) ==
'\\')
1545 return ((result % 2) == 0);
1558 char *c = (
char *) str;
1559 gboolean active = FALSE;
1563 if (!active && (*c ==
'?' || *c ==
'*'))
1566 if (!active && *c ==
'\\')
1588index_of (
const char *str,
const char *sub_str,
int offset)
1593 if (offset > (
int) strlen (str))
1596 start = (
char *) str + offset;
1597 begin_substr = strstr (start, sub_str);
1598 if (begin_substr == NULL)
1600 return (begin_substr - str);
1618 return (strcmp (str,
"ANY") && strcmp (str,
"NA"));
char * fs_cpe_to_uri_cpe(const char *fs_cpe)
Convert a formatted string CPE to a URI CPE.
Definition cpeutils.c:140
gboolean cpe_struct_match(cpe_struct_t *source, cpe_struct_t *target)
Returns if source is a match for target. That means that source is a superset of target.
Definition cpeutils.c:1263
static char * bind_cpe_component_for_fs(const char *)
Bind a CPE component for a formatted string CPE.
Definition cpeutils.c:1092
gboolean cpe_struct_match_tail(cpe_struct_t *source, cpe_struct_t *target)
Returns if the part behind the version of source is a match for that part of target....
Definition cpeutils.c:1318
char * uri_cpe_to_fs_cpe(const char *uri_cpe)
Convert a URI CPE to a formatted string CPE.
Definition cpeutils.c:100
static enum set_relation compare_strings(const char *, const char *)
Returns if the string of a component "source" is a match for the the string of a component "target"....
Definition cpeutils.c:1436
static char * bind_cpe_component_for_uri(const char *)
Bind a CPE component for a URI CPE.
Definition cpeutils.c:957
static char * get_fs_component(const char *, int)
Get the indexth component of a formatted string CPE.
Definition cpeutils.c:781
static gboolean is_even_wildcards(const char *, int)
Returns true if an even number of escape (backslash) characters precede the character at the index "i...
Definition cpeutils.c:1536
static enum set_relation compare_component(const char *, const char *)
Returns if the component "source" is a match for the component "target". That means that source is a ...
Definition cpeutils.c:1359
char * fs_cpe_to_uri_product(const char *fs_cpe)
Convert a formatted string CPE to an URI product.
Definition cpeutils.c:160
static void get_code(char *, const char *)
Get the percent code from the start of a string.
Definition cpeutils.c:1242
static void unpack_sixth_uri_component(const char *, cpe_struct_t *)
Unpack the sixth component of a URI CPE.
Definition cpeutils.c:699
char * cpe_struct_to_fs_cpe(const cpe_struct_t *cpe)
Convert a CPE struct into a formatted string CPE.
Definition cpeutils.c:382
static char * unbind_fs_component(char *)
Unbind a formatted string CPE component.
Definition cpeutils.c:837
static char * get_uri_component(const char *, int)
Get the indexth component of a URI CPE.
Definition cpeutils.c:516
static char * decode_uri_component(const char *)
Decode a component of a URI CPE.
Definition cpeutils.c:566
static char * process_quoted_chars(const char *)
Process the quoted characters of a CPE component for a formatted string CPE.
Definition cpeutils.c:1114
void fs_cpe_to_cpe_struct(const char *fs_cpe, cpe_struct_t *cpe)
Read a formatted string CPE into the CPE struct.
Definition cpeutils.c:346
static char * add_quoting(const char *)
Handle the quoting for an unbind formatted string CPE component.
Definition cpeutils.c:866
char * cpe_struct_to_uri_product(const cpe_struct_t *cpe)
Convert a CPE struct into a URI product.
Definition cpeutils.c:289
char * cpe_struct_to_uri_cpe(const cpe_struct_t *cpe)
Convert a CPE struct into a URI CPE.
Definition cpeutils.c:220
void cpe_struct_init(cpe_struct_t *cpe)
Initialize a CPE struct.
Definition cpeutils.c:1158
char * cpe_struct_to_fs_product(const cpe_struct_t *cpe)
Convert a CPE struct into a formatted string product.
Definition cpeutils.c:476
char * get_version_from_uri_cpe(const char *uri_cpe)
Get the version from an uri cpe.
Definition cpeutils.c:330
static int index_of(const char *, const char *, int)
Searches the string "str" for the first occurrence of the string "sub_str", starting at the offset "o...
Definition cpeutils.c:1588
static gboolean is_string(const char *)
Returns if a string is an ordinary string and does not represent one of the logical values "ANY" or "...
Definition cpeutils.c:1613
void cpe_struct_free(cpe_struct_t *cpe)
Free a CPE struct.
Definition cpeutils.c:1182
static char * pack_sixth_uri_component(const cpe_struct_t *)
Pack the sixth component of a URI CPE.
Definition cpeutils.c:1041
static char * transform_for_uri(const char *)
Transform a CPE component for a URI CPE.
Definition cpeutils.c:978
static gboolean has_wildcards(const char *)
Returns if a given string contains wildcards ("*" or "?").
Definition cpeutils.c:1556
void uri_cpe_to_cpe_struct(const char *uri_cpe, cpe_struct_t *cpe)
Read a URI CPE into the CPE struct.
Definition cpeutils.c:180
static int count_escapes(const char *, int, int)
Counts the number of unescaped escape signs ("\") in a specified part of a string.
Definition cpeutils.c:1511
static void trim_pct(char *)
Cut of trailing ':' signs.
Definition cpeutils.c:1216
char * uri_cpe_to_fs_product(const char *uri_cpe)
Convert a URI CPE to a formatted string product.
Definition cpeutils.c:120
set_relation
Definition cpeutils.h:83
@ SUBSET
Definition cpeutils.h:86
@ DISJOINT
Definition cpeutils.h:84
@ EQUAL
Definition cpeutils.h:85
@ UNDEFINED
Definition cpeutils.h:88
@ SUPERSET
Definition cpeutils.h:87
XML context.
Definition cpeutils.h:23
char * sw_edition
Definition cpeutils.h:30
char * target_sw
Definition cpeutils.h:31
char * vendor
Definition cpeutils.h:25
char * version
Definition cpeutils.h:27
char * update
Definition cpeutils.h:28
char * product
Definition cpeutils.h:26
char * part
Definition cpeutils.h:24
char * edition
Definition cpeutils.h:29
char * language
Definition cpeutils.h:34
char * target_hw
Definition cpeutils.h:32
char * other
Definition cpeutils.h:33