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
|
/*
* Copyright (C) 2014 Andreas Steffen
* HSR Hochschule fuer Technik Rapperswil
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
*
* This program 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 General Public License
* for more details.
*/
#include "imv_os_info.h"
typedef struct private_imv_os_info_t private_imv_os_info_t;
/**
* Private data of an imv_os_info_t object.
*
*/
struct private_imv_os_info_t {
/**
* Public imv_os_info_t interface.
*/
imv_os_info_t public;
/**
* OS type
*/
os_type_t type;
/**
* OS name
*/
chunk_t name;
/**
* OS version
*/
chunk_t version;
/**
* This flag allows the OS version to be empty
*/
bool version_is_set;
/**
* OS Product Information (OS Name | OS Version)
*/
char *info;
};
METHOD(imv_os_info_t, get_type, os_type_t,
private_imv_os_info_t *this)
{
return this->type;
}
METHOD(imv_os_info_t, set_name, void,
private_imv_os_info_t *this, chunk_t name)
{
/* Has the OS name already been set? */
if (this->name.len)
{
if (chunk_equals(name, this->name))
{
return;
}
free(this->name.ptr);
/* Also clear the OS info string */
free(this->info);
this->info = NULL;
}
this->name = chunk_clone(name);
this->type = os_type_from_name(name);
}
METHOD(imv_os_info_t, get_name, chunk_t,
private_imv_os_info_t *this)
{
return this->name;
}
METHOD(imv_os_info_t, set_version, void,
private_imv_os_info_t *this, chunk_t version)
{
/* Has the OS version already been set? */
if (this->version_is_set)
{
if (chunk_equals(version, this->version))
{
return;
}
free(this->version.ptr);
/* Also clear the OS info string */
free(this->info);
this->info = NULL;
}
this->version = chunk_clone(version);
this->version_is_set = TRUE;
}
METHOD(imv_os_info_t, get_version, chunk_t,
private_imv_os_info_t *this)
{
return this->version;
}
METHOD(imv_os_info_t, get_info, char*,
private_imv_os_info_t *this)
{
int len;
if (!this->info)
{
/* Have both OS name and OS version been set? */
if (this->name.len == 0 || !this->version_is_set)
{
return NULL;
}
/* OS info is a concatenation of OS name and OS version */
len = this->name.len + 1 + this->version.len + 1;
this->info = malloc(len);
snprintf(this->info, len, "%.*s %.*s",
(int)this->name.len, this->name.ptr,
(int)this->version.len, this->version.ptr);
}
return this->info;
}
METHOD(imv_os_info_t, destroy, void,
private_imv_os_info_t *this)
{
free(this->name.ptr);
free(this->version.ptr);
free(this->info);
free(this);
}
/**
* See header
*/
imv_os_info_t *imv_os_info_create(void)
{
private_imv_os_info_t *this;
INIT(this,
.public = {
.get_type = _get_type,
.set_name = _set_name,
.get_name = _get_name,
.set_version = _set_version,
.get_version = _get_version,
.get_info = _get_info,
.destroy = _destroy,
},
);
return &this->public;
}
|