30#define G_LOG_DOMAIN "libgvm util"
48index_of (
const char *,
const char *,
int);
206 g_free (uri_component);
209 g_free (uri_component);
212 g_free (uri_component);
215 g_free (uri_component);
218 g_free (uri_component);
220 if (strcmp (uri_component,
"") == 0 || strcmp (uri_component,
"-") == 0
221 || *uri_component !=
'~')
225 g_free (uri_component);
229 g_free (uri_component);
243 char *bind_cpe_component;
244 uri_cpe = g_string_new (
"cpe:/");
247 if (bind_cpe_component)
249 g_string_append (uri_cpe, bind_cpe_component);
250 g_string_append_c (uri_cpe,
':');
251 g_free (bind_cpe_component);
254 if (bind_cpe_component)
256 g_string_append (uri_cpe, bind_cpe_component);
257 g_string_append_c (uri_cpe,
':');
258 g_free (bind_cpe_component);
261 if (bind_cpe_component)
263 g_string_append (uri_cpe, bind_cpe_component);
264 g_string_append_c (uri_cpe,
':');
265 g_free (bind_cpe_component);
268 if (bind_cpe_component)
270 g_string_append (uri_cpe, bind_cpe_component);
271 g_string_append_c (uri_cpe,
':');
272 g_free (bind_cpe_component);
275 if (bind_cpe_component)
277 g_string_append (uri_cpe, bind_cpe_component);
278 g_string_append_c (uri_cpe,
':');
279 g_free (bind_cpe_component);
282 if (bind_cpe_component)
284 g_string_append (uri_cpe, bind_cpe_component);
285 g_string_append_c (uri_cpe,
':');
286 g_free (bind_cpe_component);
289 if (bind_cpe_component)
291 g_string_append (uri_cpe, bind_cpe_component);
292 g_string_append_c (uri_cpe,
':');
293 g_free (bind_cpe_component);
296 char *result = g_string_free (uri_cpe, FALSE);
312 char *bind_cpe_component;
313 uri_cpe = g_string_new (
"cpe:/");
316 if (bind_cpe_component)
318 g_string_append (uri_cpe, bind_cpe_component);
319 g_string_append_c (uri_cpe,
':');
320 g_free (bind_cpe_component);
323 if (bind_cpe_component)
325 g_string_append (uri_cpe, bind_cpe_component);
326 g_string_append_c (uri_cpe,
':');
327 g_free (bind_cpe_component);
330 if (bind_cpe_component)
332 g_string_append (uri_cpe, bind_cpe_component);
333 g_string_append_c (uri_cpe,
':');
334 g_free (bind_cpe_component);
337 char *result = g_string_free (uri_cpe, FALSE);
355 return decoded_version;
405 char *bind_cpe_component;
407 fs_cpe = g_string_new (
"cpe:2.3:");
410 if (bind_cpe_component)
412 g_string_append (fs_cpe, bind_cpe_component);
413 g_string_append_c (fs_cpe,
':');
414 g_free (bind_cpe_component);
417 if (bind_cpe_component)
419 g_string_append (fs_cpe, bind_cpe_component);
420 g_string_append_c (fs_cpe,
':');
421 g_free (bind_cpe_component);
424 if (bind_cpe_component)
426 g_string_append (fs_cpe, bind_cpe_component);
427 g_string_append_c (fs_cpe,
':');
428 g_free (bind_cpe_component);
431 if (bind_cpe_component)
433 g_string_append (fs_cpe, bind_cpe_component);
434 g_string_append_c (fs_cpe,
':');
435 g_free (bind_cpe_component);
438 if (bind_cpe_component)
440 g_string_append (fs_cpe, bind_cpe_component);
441 g_string_append_c (fs_cpe,
':');
442 g_free (bind_cpe_component);
445 if (bind_cpe_component)
447 g_string_append (fs_cpe, bind_cpe_component);
448 g_string_append_c (fs_cpe,
':');
449 g_free (bind_cpe_component);
452 if (bind_cpe_component)
454 g_string_append (fs_cpe, bind_cpe_component);
455 g_string_append_c (fs_cpe,
':');
456 g_free (bind_cpe_component);
459 if (bind_cpe_component)
461 g_string_append (fs_cpe, bind_cpe_component);
462 g_string_append_c (fs_cpe,
':');
463 g_free (bind_cpe_component);
466 if (bind_cpe_component)
468 g_string_append (fs_cpe, bind_cpe_component);
469 g_string_append_c (fs_cpe,
':');
470 g_free (bind_cpe_component);
473 if (bind_cpe_component)
475 g_string_append (fs_cpe, bind_cpe_component);
476 g_string_append_c (fs_cpe,
':');
477 g_free (bind_cpe_component);
480 if (bind_cpe_component)
482 g_string_append (fs_cpe, bind_cpe_component);
483 g_free (bind_cpe_component);
485 return (g_string_free (fs_cpe, FALSE));
499 char *bind_cpe_component;
501 fs_cpe = g_string_new (
"cpe:2.3:");
504 if (bind_cpe_component)
506 g_string_append (fs_cpe, bind_cpe_component);
507 g_string_append_c (fs_cpe,
':');
508 g_free (bind_cpe_component);
511 if (bind_cpe_component)
513 g_string_append (fs_cpe, bind_cpe_component);
514 g_string_append_c (fs_cpe,
':');
515 g_free (bind_cpe_component);
518 if (bind_cpe_component)
520 g_string_append (fs_cpe, bind_cpe_component);
521 g_string_append_c (fs_cpe,
':');
522 g_free (bind_cpe_component);
524 return (g_string_free (fs_cpe, FALSE));
538 char *component = NULL;
540 char *component_start, *component_end;
545 c = (
char *) uri_cpe;
548 for (
int i = 0; *c !=
'\0' && i < index; c++)
554 if (index == 1 && *c !=
'\0')
560 if (*component_start ==
'\0')
561 component_end = component_start;
564 for (c = component_start; *c !=
'\0' && *c !=
':'; c++)
570 if (component_start >= component_end || component_end == 0)
571 component = (
char *) g_strdup (
"");
573 component = g_strndup (component_start, component_end - component_start);
588 GString *decoded_component;
589 char *escapes =
"!\"#$%&'()*+,/:;<=>?@[\\]^`{|}~";
591 char code_a[4], code_b[4], code_c[4];
592 long unsigned int index;
598 if (strcmp (component,
"") == 0 || strcmp (component,
" ") == 0)
600 return (g_strdup (
"ANY"));
602 if (strcmp (component,
"-") == 0)
604 return (g_strdup (
"NA"));
607 tmp_component = g_strdup (component);
610 char *c = tmp_component;
616 decoded_component = g_string_sized_new (2 * strlen (component));
620 while (index < strlen (tmp_component))
622 l = *(tmp_component + index);
624 if (l ==
'.' || l ==
'-' || l ==
'~')
626 g_string_append_c (decoded_component,
'\\');
627 g_string_append_c (decoded_component, l);
634 g_string_append_c (decoded_component, l);
640 get_code (code_a, tmp_component + index);
642 if (strcmp (code_a,
"%01") == 0)
645 get_code (code_b, tmp_component + index - 3);
648 if (strlen (tmp_component) >= index + 6)
649 get_code (code_c, tmp_component + index + 3);
652 if ((index == 0 || index == strlen (tmp_component) - 3)
653 || (!embedded && strcmp (code_b,
"%01"))
654 || (embedded && strcmp (code_c,
"%01")))
656 g_string_append_c (decoded_component,
'?');
662 g_string_free (decoded_component, TRUE);
663 g_free (tmp_component);
668 if (strcmp (code_a,
"%02") == 0)
670 if (index == 0 || index == strlen (tmp_component) - 3)
672 g_string_append_c (decoded_component,
'*');
678 g_string_free (decoded_component, TRUE);
679 g_free (tmp_component);
684 unescaped = g_uri_unescape_string (code_a, NULL);
685 if (unescaped && strchr (escapes, *unescaped))
687 g_string_append_c (decoded_component,
'\\');
688 g_string_append (decoded_component, unescaped);
693 g_string_append (decoded_component, unescaped);
698 g_string_free (decoded_component, TRUE);
699 g_free (tmp_component);
706 g_free (tmp_component);
707 return (g_string_free (decoded_component, FALSE));
721 const char *start = component + 1;
724 char *edition, *sw_edition, *target_sw, *target_hw, *other;
726 end = strchr (start,
'~');
727 if (start >= end || end == NULL)
728 edition = strdup (
"");
730 edition = g_strndup (start, end - start);
735 end = strchr (start,
'~');
736 if (start >= end || end == NULL)
737 sw_edition = strdup (
"");
739 sw_edition = g_strndup (start, end - start);
742 sw_edition = strdup (
"");
747 end = strchr (start,
'~');
748 if (start >= end || end == NULL)
749 target_sw = strdup (
"");
751 target_sw = g_strndup (start, end - start);
754 target_sw = strdup (
"");
759 end = strchr (start,
'~');
760 if (start >= end || end == NULL)
761 target_hw = strdup (
"");
763 target_hw = g_strndup (start, end - start);
766 target_hw = strdup (
"");
771 end = component + strlen (component);
775 other = g_strndup (start, end - start);
803 char *component = NULL;
805 char *component_start, *component_end;
812 return ((
char *) g_strdup (
""));
822 for (
int i = 0; *c !=
'\0' && i < index; c++)
824 if (*c ==
':' && !escaped)
826 else if (*c ==
'\\' && !escaped)
836 if (*component_start ==
'\0')
837 component_end = component_start;
840 for (c = component_start; *c !=
'\0'; c++)
842 if (*c ==
':' && !escaped)
844 if (*c ==
'\\' && !escaped)
853 if (component_start >= component_end || component_end == NULL)
854 component = (
char *) g_strdup (
"");
856 component = g_strndup (component_start, component_end - component_start);
871 char *unbound_component;
873 if (strcmp (component,
"*") == 0)
876 return ((
char *) g_strdup (
"ANY"));
878 if (strcmp (component,
"-") == 0)
881 return ((
char *) g_strdup (
"NA"));
886 return unbound_component;
900 GString *quoted_component;
908 quoted_component = g_string_sized_new (2 * strlen (component));
909 tmp_component = (
char *) g_strdup (component);
913 for (c = tmp_component; *c; c++)
919 if (g_ascii_isalnum (*c) || *c ==
'_')
921 g_string_append_c (quoted_component, *c);
931 g_string_append_c (quoted_component,
'\\');
932 g_string_append_c (quoted_component, *c);
940 if ((c == tmp_component)
941 || (c == tmp_component + strlen (tmp_component - 1)))
943 g_string_append_c (quoted_component, *c);
950 g_free (tmp_component);
956 if ((c == tmp_component)
957 || (c == tmp_component + strlen (tmp_component - 1))
958 || (!embedded && (c > tmp_component) && (*(c - 1) ==
'?'))
959 || (embedded && *(c + 1) ==
'?'))
961 g_string_append_c (quoted_component, *c);
968 g_free (tmp_component);
972 g_string_append_c (quoted_component,
'\\');
973 g_string_append_c (quoted_component, *c);
977 g_free (tmp_component);
978 return (g_string_free (quoted_component, FALSE));
992 return (g_strdup (
""));
993 if (strcmp (component,
"") == 0)
994 return (g_strdup (
""));
995 if (strcmp (component,
"ANY") == 0)
996 return (g_strdup (
""));
997 if (strcmp (component,
"NA") == 0)
998 return (g_strdup (
"-"));
1013 char *tmp_component;
1017 return (g_strdup (
""));
1018 if (strcmp (component,
"") == 0)
1019 return (g_strdup (
""));
1021 tmp_component = g_strdup (component);
1024 for (c = tmp_component; *c; c++)
1027 result = g_string_new (
"");
1032 if ((g_ascii_isalnum (*c) || *c ==
'_') && *c !=
'-')
1034 g_string_append_c (result, *c);
1046 to_escape[1] =
'\0';
1047 escaped = g_uri_escape_string (to_escape, NULL, FALSE);
1048 g_string_append (result, escaped);
1055 g_string_append (result,
"%01");
1057 g_string_append (result,
"%02");
1060 g_free (tmp_component);
1061 return (g_string_free (result, FALSE));
1078 && (cpe->
other == NULL || strcmp (cpe->
other,
"") == 0))
1080 if (strcmp (cpe->
edition,
"ANY") == 0)
1081 return (g_strdup (
""));
1082 if (strcmp (cpe->
edition,
"NA") == 0)
1083 return (g_strdup (
"-"));
1084 return (g_strdup (cpe->
edition));
1093 component = g_string_new (
"");
1094 if (!((!sw_edition || strcmp (sw_edition,
"") == 0)
1095 && (!target_sw || strcmp (target_sw,
"") == 0)
1096 && (!target_hw || strcmp (target_hw,
"") == 0)
1097 && (!other || strcmp (other,
"") == 0)))
1098 g_string_append_printf (component,
"~%s~%s~%s~%s~%s", edition, sw_edition,
1099 target_sw, target_hw, other);
1101 g_string_append (component, edition);
1106 g_free (sw_edition);
1113 return (g_string_free (component, FALSE));
1127 return (g_strdup (
"*"));
1128 if (strcmp (component,
"") == 0)
1129 return (g_strdup (
"*"));
1130 if (strcmp (component,
"ANY") == 0)
1131 return (g_strdup (
"*"));
1132 if (strcmp (component,
"NA") == 0)
1133 return (g_strdup (
"-"));
1149 return (g_strdup (
""));
1150 if (strcmp (component,
"") == 0)
1151 return (g_strdup (
""));
1153 GString *fs_component;
1154 fs_component = g_string_new (
"");
1155 char *c = (
char *) component;
1162 g_string_append_c (fs_component, *c);
1168 if (next_c ==
'.' || next_c ==
'-' || next_c ==
'_')
1170 g_string_append_c (fs_component, next_c);
1175 g_string_append_c (fs_component,
'\\');
1176 g_string_append_c (fs_component, next_c);
1181 return (g_string_free (fs_component, FALSE));
1237 g_free (cpe->
other);
1254 c = str + strlen (str) - 1;
1277 code[1] = *(str + 1);
1278 code[2] = *(str + 2);
1394 char *source_cpy, *target_cpy;
1398 source_cpy = g_strdup (source);
1400 source_cpy = g_strdup (
"ANY");
1402 target_cpy = g_strdup (target);
1404 target_cpy = g_strdup (
"ANY");
1409 for (c = source_cpy; *c; c++)
1415 for (c = target_cpy; *c; c++)
1420 g_free (source_cpy);
1421 g_free (target_cpy);
1424 if (strcmp (source_cpy, target_cpy) == 0)
1426 g_free (source_cpy);
1427 g_free (target_cpy);
1430 if (strcmp (source_cpy,
"ANY") == 0)
1432 g_free (source_cpy);
1433 g_free (target_cpy);
1436 if (strcmp (target_cpy,
"ANY") == 0)
1438 g_free (source_cpy);
1439 g_free (target_cpy);
1442 if (strcmp (target_cpy,
"NA") == 0 || strcmp (source_cpy,
"NA") == 0)
1444 g_free (source_cpy);
1445 g_free (target_cpy);
1450 g_free (source_cpy);
1451 g_free (target_cpy);
1471 int end = strlen (source);
1484 while (start < (
int) strlen (source) && *(source + start) ==
'?')
1497 while (end > 0 && *(source + end - 1) ==
'?'
1505 sub_source = g_strndup (source + start, end - start);
1508 int leftover = strlen (target);
1510 while (leftover > 0)
1512 index =
index_of (target, sub_source, index + 1);
1516 if (index > 0 && begins != -1 && begins < (index - escapes))
1518 escapes =
count_escapes (target, index + 1, strlen (target));
1519 leftover = strlen (target) - index - escapes - strlen (sub_source);
1520 if (leftover > 0 && (ends != -1 && leftover > ends))
1522 g_free (sub_source);
1525 g_free (sub_source);
1546 gboolean active = FALSE;
1548 for (
int i = 0; i < end && *(str + i) !=
'\0'; i++)
1550 active = (!active && *(str + i) ==
'\\');
1551 if (active && i >= start)
1572 while (index > 0 && *(str + index - 1) ==
'\\')
1577 return ((result % 2) == 0);
1590 char *c = (
char *) str;
1591 gboolean active = FALSE;
1595 if (!active && (*c ==
'?' || *c ==
'*'))
1598 if (!active && *c ==
'\\')
1620index_of (
const char *str,
const char *sub_str,
int offset)
1625 if (offset > (
int) strlen (str))
1628 start = (
char *) str + offset;
1629 begin_substr = strstr (start, sub_str);
1630 if (begin_substr == NULL)
1632 return begin_substr - str;
1650 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:160
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:1295
static char * bind_cpe_component_for_fs(const char *)
Bind a CPE component for a formatted string CPE.
Definition cpeutils.c:1124
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:1350
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:1468
static char * bind_cpe_component_for_uri(const char *)
Bind a CPE component for a URI CPE.
Definition cpeutils.c:989
static char * get_fs_component(const char *, int)
Get the indexth component of a formatted string CPE.
Definition cpeutils.c:801
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:1568
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:1391
char * fs_cpe_to_uri_product(const char *fs_cpe)
Convert a formatted string CPE to an URI product.
Definition cpeutils.c:180
static void get_code(char *, const char *)
Get the percent code from the start of a string.
Definition cpeutils.c:1274
static void unpack_sixth_uri_component(const char *, cpe_struct_t *)
Unpack the sixth component of a URI CPE.
Definition cpeutils.c:719
char * cpe_struct_to_fs_cpe(const cpe_struct_t *cpe)
Convert a CPE struct into a formatted string CPE.
Definition cpeutils.c:402
static char * unbind_fs_component(char *)
Unbind a formatted string CPE component.
Definition cpeutils.c:869
static char * get_uri_component(const char *, int)
Get the indexth component of a URI CPE.
Definition cpeutils.c:536
static char * decode_uri_component(const char *)
Decode a component of a URI CPE.
Definition cpeutils.c:586
static char * process_quoted_chars(const char *)
Process the quoted characters of a CPE component for a formatted string CPE.
Definition cpeutils.c:1146
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:366
static char * add_quoting(const char *)
Handle the quoting for an unbind formatted string CPE component.
Definition cpeutils.c:898
char * cpe_struct_to_uri_product(const cpe_struct_t *cpe)
Convert a CPE struct into a URI product.
Definition cpeutils.c:309
char * cpe_struct_to_uri_cpe(const cpe_struct_t *cpe)
Convert a CPE struct into a URI CPE.
Definition cpeutils.c:240
void cpe_struct_init(cpe_struct_t *cpe)
Initialize a CPE struct.
Definition cpeutils.c:1190
char * cpe_struct_to_fs_product(const cpe_struct_t *cpe)
Convert a CPE struct into a formatted string product.
Definition cpeutils.c:496
char * get_version_from_uri_cpe(const char *uri_cpe)
Get the version from an uri cpe.
Definition cpeutils.c:350
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:1620
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:1645
void cpe_struct_free(cpe_struct_t *cpe)
Free a CPE struct.
Definition cpeutils.c:1214
static char * pack_sixth_uri_component(const cpe_struct_t *)
Pack the sixth component of a URI CPE.
Definition cpeutils.c:1073
char * uri_cpe_to_uri_product(const char *uri_cpe)
Convert a URI CPE to a formatted string product.
Definition cpeutils.c:140
static char * transform_for_uri(const char *)
Transform a CPE component for a URI CPE.
Definition cpeutils.c:1010
static gboolean has_wildcards(const char *)
Returns if a given string contains wildcards ("*" or "?").
Definition cpeutils.c:1588
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:200
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:1543
static void trim_pct(char *)
Cut of trailing ':' signs.
Definition cpeutils.c:1248
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:86
@ SUBSET
Definition cpeutils.h:89
@ DISJOINT
Definition cpeutils.h:87
@ EQUAL
Definition cpeutils.h:88
@ UNDEFINED
Definition cpeutils.h:91
@ SUPERSET
Definition cpeutils.h:90
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