summaryrefslogtreecommitdiff
path: root/src/libimcv/swima/swima_record.c
blob: dc6a5413ac03074a1dc6416ba1e79261c215cba4 (plain)
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
169
170
171
172
173
174
/*
 * Copyright (C) 2017 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 "swima_record.h"
#include "swima_data_model.h"

typedef struct private_swima_record_t private_swima_record_t;

/**
 * Private data of a swima_record_t object.
 *
 */
struct private_swima_record_t {

	/**
	 * Public swima_record_t interface.
	 */
	swima_record_t public;

	/**
	 * Record ID
	 */
	uint32_t record_id;

	/**
	 * Software Identity
	 */
	chunk_t sw_id;

	/**
	 * Optional Software Locator
	 */
	chunk_t sw_locator;

	/**
	 * Data Model
	 */
	pen_type_t data_model;

	/**
	 * Source ID
	 */
	uint8_t source_id;

	/**g
	 * Optional Software Inventory Evidence Record
	 */
	chunk_t record;

	/**
	 * Reference count
	 */
	refcount_t ref;
};

METHOD(swima_record_t, get_record_id, uint32_t,
	private_swima_record_t *this)
{
	return this->record_id;
}

METHOD(swima_record_t, get_sw_id, chunk_t,
	private_swima_record_t *this, chunk_t *sw_locator)
{
	if (sw_locator)
	{
		*sw_locator = this->sw_locator;
	}
	return this->sw_id;
}

METHOD(swima_record_t, set_data_model, void,
	private_swima_record_t *this, pen_type_t data_model)
{
	this->data_model = data_model;
}

METHOD(swima_record_t, get_data_model, pen_type_t,
	private_swima_record_t *this)
{
	return this->data_model;
}

METHOD(swima_record_t, set_source_id, void,
	private_swima_record_t *this, uint8_t source_id)
{
	this->source_id = source_id;
}

METHOD(swima_record_t, get_source_id, uint8_t,
	private_swima_record_t *this)
{
	return this->source_id;
}

METHOD(swima_record_t, set_record, void,
	private_swima_record_t *this, chunk_t record)
{
	chunk_free(&this->record);
	this->record = chunk_clone(record);
}

METHOD(swima_record_t, get_record, chunk_t,
	private_swima_record_t *this)
{
	return this->record;
}

METHOD(swima_record_t, get_ref, swima_record_t*,
	private_swima_record_t *this)
{
	ref_get(&this->ref);
	return &this->public;
}

METHOD(swima_record_t, destroy, void,
	private_swima_record_t *this)
{
	if (ref_put(&this->ref))
	{
		free(this->sw_id.ptr);
		free(this->sw_locator.ptr);
		free(this->record.ptr);
		free(this);
	}
}

/**
 * See header
 */
swima_record_t *swima_record_create(uint32_t record_id, chunk_t sw_id,
									chunk_t sw_locator)
{
	private_swima_record_t *this;

	INIT(this,
		.public = {
			.get_record_id = _get_record_id,
			.get_sw_id = _get_sw_id,
			.set_data_model = _set_data_model,
			.get_data_model = _get_data_model,
			.set_source_id = _set_source_id,
			.get_source_id = _get_source_id,
			.set_record = _set_record,
			.get_record = _get_record,
			.get_ref = _get_ref,
			.destroy = _destroy,
		},
		.record_id = record_id,
		.data_model = swima_data_model_iso_2015_swid_xml,
		.sw_id = chunk_clone(sw_id),
		.ref = 1,
	);

	if (sw_locator.len > 0)
	{
		this->sw_locator = chunk_clone(sw_locator);
	}

	return &this->public;
}