Greenbone Vulnerability Management Libraries 22.18.1
cpeutils.c File Reference

Functions to convert different CPE notations into each other. More...

#include "cpeutils.h"
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <glib.h>
#include <string.h>

Go to the source code of this file.

Macros

#define G_LOG_DOMAIN   "libgvm util"
 GLib logging domain.
 

Functions

static enum set_relation compare_component (const char *source, const char *target)
 Returns if the component "source" is a match for the component "target". That means that source is a superset of target.
 
static enum set_relation compare_strings (const char *source, const char *target)
 Returns if the string of a component "source" is a match for the the string of a component "target". That means that source represents a superset of target.
 
static int count_escapes (const char *str, int start, int end)
 Counts the number of unescaped escape signs ("\") in a specified part of a string.
 
static gboolean is_even_wildcards (const char *str, int index)
 Returns true if an even number of escape (backslash) characters precede the character at the index "index" in string "str".
 
static gboolean has_wildcards (const char *str)
 Returns if a given string contains wildcards ("*" or "?").
 
static int index_of (const char *str, const char *sub_str, int offset)
 Searches the string "str" for the first occurrence of the string "sub_str", starting at the offset "offset" in "str".
 
static gboolean is_string (const char *str)
 Returns if a string is an ordinary string and does not represent one of the logical values "ANY" or "NA".
 
static char * get_uri_component (const char *uri_cpe, int index)
 Get the indexth component of a URI CPE.
 
static char * decode_uri_component (const char *component)
 Decode a component of a URI CPE.
 
static void unpack_sixth_uri_component (const char *component, cpe_struct_t *cpe)
 Unpack the sixth component of a URI CPE.
 
static char * get_fs_component (const char *fs_cpe, int index)
 Get the indexth component of a formatted string CPE.
 
static char * unbind_fs_component (char *component)
 Unbind a formatted string CPE component.
 
static char * add_quoting (const char *component)
 Handle the quoting for an unbind formatted string CPE component.
 
static char * bind_cpe_component_for_uri (const char *component)
 Bind a CPE component for a URI CPE.
 
static char * transform_for_uri (const char *component)
 Transform a CPE component for a URI CPE.
 
static char * pack_sixth_uri_component (const cpe_struct_t *cpe)
 Pack the sixth component of a URI CPE.
 
static char * bind_cpe_component_for_fs (const char *component)
 Bind a CPE component for a formatted string CPE.
 
static char * process_quoted_chars (const char *component)
 Process the quoted characters of a CPE component for a formatted string CPE.
 
static void trim_pct (char *str)
 Cut of trailing ':' signs.
 
static void get_code (char *code, const char *str)
 Get the percent code from the start of a string.
 
char * uri_cpe_to_fs_cpe (const char *uri_cpe)
 Convert a URI CPE to a formatted string CPE.
 
char * uri_cpe_to_fs_product (const char *uri_cpe)
 Convert a URI CPE to a formatted string product.
 
char * uri_cpe_to_uri_product (const char *uri_cpe)
 Convert a URI CPE to a formatted string product.
 
char * fs_cpe_to_uri_cpe (const char *fs_cpe)
 Convert a formatted string CPE to a URI CPE.
 
char * fs_cpe_to_uri_product (const char *fs_cpe)
 Convert a formatted string CPE to an URI product.
 
void uri_cpe_to_cpe_struct (const char *uri_cpe, cpe_struct_t *cpe)
 Read a URI CPE into the CPE struct.
 
char * cpe_struct_to_uri_cpe (const cpe_struct_t *cpe)
 Convert a CPE struct into a URI CPE.
 
char * cpe_struct_to_uri_product (const cpe_struct_t *cpe)
 Convert a CPE struct into a URI product.
 
char * get_version_from_uri_cpe (const char *uri_cpe)
 Get the version from an uri cpe.
 
void fs_cpe_to_cpe_struct (const char *fs_cpe, cpe_struct_t *cpe)
 Read a formatted string CPE into the CPE struct.
 
char * cpe_struct_to_fs_cpe (const cpe_struct_t *cpe)
 Convert a CPE struct into a formatted string CPE.
 
char * cpe_struct_to_fs_product (const cpe_struct_t *cpe)
 Convert a CPE struct into a formatted string product.
 
void cpe_struct_init (cpe_struct_t *cpe)
 Initialize a CPE struct.
 
void cpe_struct_free (cpe_struct_t *cpe)
 Free a CPE struct.
 
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.
 
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. That means, that source is a superset of target if also the first part matches.
 

Detailed Description

Functions to convert different CPE notations into each other.

This library provides functions to read the CPE 2.2 URI binding of a CPE or the CPE 2.3 formatted string binding of a CPE into a CPE struct that corresponds to the WFN naming of a CPE. Further functions to convert the CPE struct into the different bindings are provided. This file also contains a function that checks if one CPE (represented in a CPE struct) is a match for an other CPE (also represented in a CPE struct).

Macro Definition Documentation

◆ G_LOG_DOMAIN

#define G_LOG_DOMAIN   "libgvm util"

GLib logging domain.

Function Documentation

◆ add_quoting()

static char * add_quoting ( const char * component)
static

Handle the quoting for an unbind formatted string CPE component.

Parameters
[in]componentThe component to add the quotings to.
Returns
The component of the formatted string CPE with all necessary quotes added.

◆ bind_cpe_component_for_fs()

static char * bind_cpe_component_for_fs ( const char * component)
static

Bind a CPE component for a formatted string CPE.

Parameters
[in]componentThe component to bind.
Returns
The bound component for the formatted string CPE.

◆ bind_cpe_component_for_uri()

static char * bind_cpe_component_for_uri ( const char * component)
static

Bind a CPE component for a URI CPE.

Parameters
[in]componentThe component to bind.
Returns
The bound component for the URI CPE.

◆ compare_component()

static enum set_relation compare_component ( const char * source,
const char * target )
static

Returns if the component "source" is a match for the component "target". That means that source is a superset of target.

Parameters
[in]sourceThe component of a cpe_struct.
[in]targetThe component of a cpe_struct that is checked if it is a subset of source meaning that it is matched by source.
Returns
Returns if source is a match for target.

◆ compare_strings()

static enum set_relation compare_strings ( const char * source,
const char * target )
static

Returns if the string of a component "source" is a match for the the string of a component "target". That means that source represents a superset of target.

Parameters
[in]sourceThe string of a component of a cpe_struct.
[in]targetThe string of a component of a cpe_struct that is checked if it represents a subset of source meaning that it is matched by source.
Returns
Returns if source is a match for target.

◆ count_escapes()

static int count_escapes ( const char * str,
int start,
int end )
static

Counts the number of unescaped escape signs ("\") in a specified part of a string.

Parameters
[in]strThe string to be examined.
[in]startThe start position in the string where the examination begins.
[in]endThe end position in the string where the examination ends.
Returns
Returns the number of unescaped escape signs in the specified part of the string.

◆ cpe_struct_free()

void cpe_struct_free ( cpe_struct_t * cpe)

Free a CPE struct.

Parameters
[in/out]cpe The CPE to be freed.

◆ cpe_struct_init()

void cpe_struct_init ( cpe_struct_t * cpe)

Initialize a CPE struct.

Parameters
[in/out]cpe The pointer to the CPE to initialize.

◆ cpe_struct_match()

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.

Parameters
[in]sourceThe cpe_struct that represents a set of CPEs.
[in]targetThe cpe_struct that represents a single CPE or or a set of CPEs that is checked if it is a subset of source meaning that it is matched by source.
Returns
Returns if source is a match for target.

◆ cpe_struct_match_tail()

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. That means, that source is a superset of target if also the first part matches.

Parameters
[in]sourceThe cpe_struct that represents a set of CPEs.
[in]targetThe cpe_struct that represents a single CPE or or a set of CPEs that is checked if it is a subset of source meaning that it is matched by source.
Returns
Returns if source is a match for target.

◆ cpe_struct_to_fs_cpe()

char * cpe_struct_to_fs_cpe ( const cpe_struct_t * cpe)

Convert a CPE struct into a formatted string CPE.

Parameters
[in]cpeA pointer to the CPE struct.
Returns
A formatted string CPE.

◆ cpe_struct_to_fs_product()

char * cpe_struct_to_fs_product ( const cpe_struct_t * cpe)

Convert a CPE struct into a formatted string product.

Parameters
[in]cpeA pointer to the CPE struct.
Returns
A formatted string product.

◆ cpe_struct_to_uri_cpe()

char * cpe_struct_to_uri_cpe ( const cpe_struct_t * cpe)

Convert a CPE struct into a URI CPE.

Parameters
[in]cpeA pointer to the CPE struct.
Returns
A CPE v2.2-conformant URI.

◆ cpe_struct_to_uri_product()

char * cpe_struct_to_uri_product ( const cpe_struct_t * cpe)

Convert a CPE struct into a URI product.

Parameters
[in]cpeA pointer to the CPE struct.
Returns
A CPE v2.2-conformant URI product.

◆ decode_uri_component()

static char * decode_uri_component ( const char * component)
static

Decode a component of a URI CPE.

Parameters
[in]componentThe component to decode.
Returns
The decoded component of the URI CPE.

◆ fs_cpe_to_cpe_struct()

void fs_cpe_to_cpe_struct ( const char * fs_cpe,
cpe_struct_t * cpe )

Read a formatted string CPE into the CPE struct.

Parameters
[in]fs_cpeA formatted string CPE.
[out]cpePointer to the filled CPE struct.

◆ fs_cpe_to_uri_cpe()

char * fs_cpe_to_uri_cpe ( const char * fs_cpe)

Convert a formatted string CPE to a URI CPE.

Parameters
[in]fs_cpeA formatted string CPE.
Returns
A CPE v2.2-conformant URI.

◆ fs_cpe_to_uri_product()

char * fs_cpe_to_uri_product ( const char * fs_cpe)

Convert a formatted string CPE to an URI product.

Parameters
[in]fs_cpeA formatted string CPE.
Returns
An URI product.

◆ get_code()

static void get_code ( char * code,
const char * str )
static

Get the percent code from the start of a string.

Parameters
[in]strThe string to get the code from.
[out]codeThe percent code.

◆ get_fs_component()

static char * get_fs_component ( const char * fs_cpe,
int index )
static

Get the indexth component of a formatted string CPE.

Parameters
[in]fs_cpeThe formatted string CPE.
[in]indexThe number of the component to get.
Returns
The indexth component of the formatted string CPE.

◆ get_uri_component()

static char * get_uri_component ( const char * uri_cpe,
int index )
static

Get the indexth component of a URI CPE.

Parameters
[in]uri_cpeThe URI CPE.
[in]indexThe number of the component to get.
Returns
The indexth component of the URI CPE.

◆ get_version_from_uri_cpe()

char * get_version_from_uri_cpe ( const char * uri_cpe)

Get the version from an uri cpe.

Parameters
[in]uri_cpeThe uri cpe to get the version from.
Returns
The version of the uri cpe.

◆ has_wildcards()

static gboolean has_wildcards ( const char * str)
static

Returns if a given string contains wildcards ("*" or "?").

Parameters
[in]strThe string to be examined.
Returns
Returns TRUE if the string contains wildcards. FALSE otherwise.

◆ index_of()

static int index_of ( const char * str,
const char * sub_str,
int offset )
static

Searches the string "str" for the first occurrence of the string "sub_str", starting at the offset "offset" in "str".

Parameters
[in]strThe string to be examined.
[in]sub_strThe string to be searched for in "str".
[in]offsetThe offset where to start the search in "str".
Returns
Returns the index where the string "sub_str" starts in "str", if the string "sub_str" was found, -1 otherwise.

◆ is_even_wildcards()

static gboolean is_even_wildcards ( const char * str,
int index )
static

Returns true if an even number of escape (backslash) characters precede the character at the index "index" in string "str".

Parameters
[in]strThe string to be examined.
[in]indexThe index where the examination starts.
Returns
Returns if an even number of escape characters precede the character at index "index".

◆ is_string()

static gboolean is_string ( const char * str)
static

Returns if a string is an ordinary string and does not represent one of the logical values "ANY" or "NA".

Parameters
[in]strThe string to be examined.
Returns
Returns TRUE if the string "str" does not represent one of the logical values "ANY" or "NA". Returns FALSE otherwise.

◆ pack_sixth_uri_component()

static char * pack_sixth_uri_component ( const cpe_struct_t * cpe)
static

Pack the sixth component of a URI CPE.

Parameters
[in]componentThe CPE struct with the components to pack into the sixth component of a URI CPE.
Returns
The packed component for the URI CPE.

◆ process_quoted_chars()

static char * process_quoted_chars ( const char * component)
static

Process the quoted characters of a CPE component for a formatted string CPE.

Parameters
[in]componentThe component to process.
Returns
The processed component for the formatted string CPE.

◆ transform_for_uri()

static char * transform_for_uri ( const char * component)
static

Transform a CPE component for a URI CPE.

Parameters
[in]componentThe component to transform.
Returns
The transformed component for the URI CPE.

◆ trim_pct()

static void trim_pct ( char * str)
static

Cut of trailing ':' signs.

Parameters
[in/out]str The string to be processed.

◆ unbind_fs_component()

static char * unbind_fs_component ( char * component)
static

Unbind a formatted string CPE component.

Parameters
[in]componentThe component to unbind.
Returns
The unbound component of the formatted string CPE.

◆ unpack_sixth_uri_component()

static void unpack_sixth_uri_component ( const char * component,
cpe_struct_t * cpe )
static

Unpack the sixth component of a URI CPE.

Parameters
[in]componentThe component to unpack.
[out]cpePointer to the CPE struct where the unpacked and decoded values of the component are stored.

◆ uri_cpe_to_cpe_struct()

void uri_cpe_to_cpe_struct ( const char * uri_cpe,
cpe_struct_t * cpe )

Read a URI CPE into the CPE struct.

Parameters
[in]uri_cpeA CPE v2.2-conformant URI.
[out]cpePointer to the filled CPE struct.

◆ uri_cpe_to_fs_cpe()

char * uri_cpe_to_fs_cpe ( const char * uri_cpe)

Convert a URI CPE to a formatted string CPE.

Parameters
[in]uri_cpeA CPE v2.2-conformant URI.
Returns
A formatted string CPE.

◆ uri_cpe_to_fs_product()

char * uri_cpe_to_fs_product ( const char * uri_cpe)

Convert a URI CPE to a formatted string product.

Parameters
[in]uri_cpeA CPE v2.2-conformant URI.
Returns
A formatted string product.

◆ uri_cpe_to_uri_product()

char * uri_cpe_to_uri_product ( const char * uri_cpe)

Convert a URI CPE to a formatted string product.

Parameters
[in]uri_cpeA CPE v2.2-conformant URI.
Returns
A CPE v2.2-conformant URI product.