Debian Codegraph @

src/OVAL/oval_schema_version.c

raw symbols SCIP
1 /*
2 * Copyright 2015 Red Hat Inc., Durham, North Carolina.
3 * All Rights Reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors:
20 * "Jan Černý" <[email protected]>
21 */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <sys/types.h>
31 #include "common/util.h"
32 #include "common/oscap_pcre.h"
33 #include "common/debug_priv.h"
34 #include "public/oval_schema_version.h"
35
36 #define OVECTOR_LEN 60 // must be a multiple of 30
37
38 static int _parse_int(const char *substring, size_t substring_length)
39 {
40 /* Pay attention that substring_length != strlen(substring) */
41 char *buffer = malloc(substring_length + 1); // +1 for a zero byte
42 strncpy(buffer, substring, substring_length);
43 buffer[substring_length] = '\0';
44 int i = atoi(buffer);
45 free(buffer);
46 return i;
47 }
48
49 oval_schema_version_t oval_schema_version_from_cstr(const char *ver_str)
50 {
51 oval_schema_version_t version;
52 memset(&version, 0, sizeof(oval_schema_version_t));
53 if (ver_str == NULL) {
54 return version;
55 }
56 const char *pattern = "([0-9]+)\\.([0-9]+)(?:\\.([0-9]+))?(?::([0-9]+)\\.([0-9]+)(?:\\.([0-9]+))?)?";
57 char *error;
58 int erroffset;
59 oscap_pcre_t *re = oscap_pcre_compile(pattern, 0, &error, &erroffset);
60 if (re == NULL) {
61 dE("Regular expression compilation failed with %s", pattern);
62 oscap_pcre_err_free(error);
63 return version;
64 }
65 int ovector[OVECTOR_LEN];
66 int rc = oscap_pcre_exec(re, ver_str, strlen(ver_str), 0, 0, ovector, OVECTOR_LEN);
67 oscap_pcre_free(re);
68 if (rc < 0) {
69 dE("Regular expression %s did not match string %s", pattern, ver_str);
70 return version;
71 }
72 for (int i = 1; i < rc; i++) {
73 const char *substring = ver_str + ovector[2 * i];
74 size_t substring_length = ovector[2 * i + 1] - ovector[2 * i];
75 version.component[i - 1] = _parse_int(substring, substring_length);
76 }
77 return version;
78 }
79
80 const char *oval_schema_version_to_cstr(oval_schema_version_t version)
81 {
82 size_t buf_len = 32;
83 char *buf = malloc(buf_len);
84 const char *format;
85 if (version.component[OVAL_SCHEMA_VERSION_PLATFORM_MAJOR]) {
86 if (version.component[OVAL_SCHEMA_VERSION_CORE_UPDATE] &&
87 version.component[OVAL_SCHEMA_VERSION_PLATFORM_UPDATE]) {
88 format = "%1$d.%2$d.%3$d:%4$d.%5$d.%6$d";
89 } else if (version.component[OVAL_SCHEMA_VERSION_CORE_UPDATE]) {
90 format = "%1$d.%2$d.%3$d:%4$d.%5$d";
91 } else if (version.component[OVAL_SCHEMA_VERSION_PLATFORM_UPDATE]) {
92 format = "%1$d.%2$d:%4$d.%5$d.%6$d";
93 } else {
94 format = "%1$d.%2$d:%4$d.%5$d";
95 }
96 } else if (version.component[OVAL_SCHEMA_VERSION_CORE_UPDATE]) {
97 format = "%1$d.%2$d.%3$d";
98 } else {
99 format = "%1$d.%2$d";
100 }
101 snprintf(buf, buf_len, format,
102 version.component[OVAL_SCHEMA_VERSION_CORE_MAJOR],
103 version.component[OVAL_SCHEMA_VERSION_CORE_MINOR],
104 version.component[OVAL_SCHEMA_VERSION_CORE_UPDATE],
105 version.component[OVAL_SCHEMA_VERSION_PLATFORM_MAJOR],
106 version.component[OVAL_SCHEMA_VERSION_PLATFORM_MINOR],
107 version.component[OVAL_SCHEMA_VERSION_PLATFORM_UPDATE]);
108 return buf;
109 }
110
111 int oval_schema_version_cmp(oval_schema_version_t v1, oval_schema_version_t v2)
112 {
113 for (int i = 0; i < OVAL_SCHEMA_VERSION_COMPONENTS_COUNT; i++) {
114 if (v1.component[i] < v2.component[i]) {
115 return -1;
116 } else if (v1.component[i] > v2.component[i]) {
117 return 1;
118 }
119 }
120 return 0;
121 }