-
Notifications
You must be signed in to change notification settings - Fork 345
/
boolean_record.c
180 lines (130 loc) · 3.13 KB
/
boolean_record.c
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
175
176
177
178
179
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include "boolean_internal.h"
#include "debug.h"
struct sepol_bool {
/* This boolean's name */
char *name;
/* Its value */
int value;
};
struct sepol_bool_key {
/* This boolean's name */
char *name;
};
int sepol_bool_key_create(sepol_handle_t * handle,
const char *name, sepol_bool_key_t ** key_ptr)
{
sepol_bool_key_t *tmp_key =
(sepol_bool_key_t *) malloc(sizeof(struct sepol_bool_key));
if (!tmp_key) {
ERR(handle, "out of memory, " "could not create boolean key");
return STATUS_ERR;
}
tmp_key->name = strdup(name);
if (!tmp_key->name) {
ERR(handle, "out of memory, " "could not create boolean key");
free(tmp_key);
return STATUS_ERR;
}
*key_ptr = tmp_key;
return STATUS_SUCCESS;
}
void sepol_bool_key_unpack(const sepol_bool_key_t * key, const char **name)
{
*name = key->name;
}
int sepol_bool_key_extract(sepol_handle_t * handle,
const sepol_bool_t * boolean,
sepol_bool_key_t ** key_ptr)
{
if (sepol_bool_key_create(handle, boolean->name, key_ptr) < 0) {
ERR(handle, "could not extract key from boolean %s",
boolean->name);
return STATUS_ERR;
}
return STATUS_SUCCESS;
}
void sepol_bool_key_free(sepol_bool_key_t * key)
{
if (!key)
return;
free(key->name);
free(key);
}
int sepol_bool_compare(const sepol_bool_t * boolean,
const sepol_bool_key_t * key)
{
return strcmp(boolean->name, key->name);
}
int sepol_bool_compare2(const sepol_bool_t * boolean,
const sepol_bool_t * boolean2)
{
return strcmp(boolean->name, boolean2->name);
}
/* Name */
const char *sepol_bool_get_name(const sepol_bool_t * boolean)
{
return boolean->name;
}
int sepol_bool_set_name(sepol_handle_t * handle,
sepol_bool_t * boolean, const char *name)
{
char *tmp_name = strdup(name);
if (!tmp_name) {
ERR(handle, "out of memory, could not set boolean name");
return STATUS_ERR;
}
free(boolean->name);
boolean->name = tmp_name;
return STATUS_SUCCESS;
}
/* Value */
int sepol_bool_get_value(const sepol_bool_t * boolean)
{
return boolean->value;
}
void sepol_bool_set_value(sepol_bool_t * boolean, int value)
{
boolean->value = value;
}
/* Create */
int sepol_bool_create(sepol_handle_t * handle, sepol_bool_t ** bool_ptr)
{
sepol_bool_t *boolean = (sepol_bool_t *) malloc(sizeof(sepol_bool_t));
if (!boolean) {
ERR(handle, "out of memory, "
"could not create boolean record");
return STATUS_ERR;
}
boolean->name = NULL;
boolean->value = 0;
*bool_ptr = boolean;
return STATUS_SUCCESS;
}
/* Deep copy clone */
int sepol_bool_clone(sepol_handle_t * handle,
const sepol_bool_t * boolean, sepol_bool_t ** bool_ptr)
{
sepol_bool_t *new_bool = NULL;
if (sepol_bool_create(handle, &new_bool) < 0)
goto err;
if (sepol_bool_set_name(handle, new_bool, boolean->name) < 0)
goto err;
new_bool->value = boolean->value;
*bool_ptr = new_bool;
return STATUS_SUCCESS;
err:
ERR(handle, "could not clone boolean record");
sepol_bool_free(new_bool);
return STATUS_ERR;
}
/* Destroy */
void sepol_bool_free(sepol_bool_t * boolean)
{
if (!boolean)
return;
free(boolean->name);
free(boolean);
}