2 * @file transform_attribute.c
4 * @brief Implementation of transform_attribute_t.
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
26 #include "transform_attribute.h"
28 #include <encoding/payloads/encodings.h>
30 #include <utils/allocator.h>
32 typedef struct private_transform_attribute_t private_transform_attribute_t
;
35 * Private data of an transform_attribute_t object.
38 struct private_transform_attribute_t
{
40 * Public transform_attribute_t interface.
42 transform_attribute_t
public;
45 * Attribute Format Flag.
47 * - TRUE means value is stored in attribute_length_or_value
48 * - FALSE means value is stored in attribute_value
50 bool attribute_format
;
53 * Type of the attribute.
55 u_int16_t attribute_type
;
58 * Attribute Length if attribute_format is 0, attribute Value otherwise.
60 u_int16_t attribute_length_or_value
;
63 * Attribute value as chunk if attribute_format is 0 (FALSE).
65 chunk_t attribute_value
;
69 * String mappings for transform_attribute_type_t.
71 mapping_t transform_attribute_type_m
[] = {
72 {ATTRIBUTE_UNDEFINED
, "ATTRIBUTE_UNDEFINED"},
73 {KEY_LENGTH
, "KEY_LENGTH"},
78 * Encoding rules to parse or generate a Transform attribute.
80 * The defined offsets are the positions in a object of type
81 * private_transform_attribute_t.
84 encoding_rule_t transform_attribute_encodings
[] = {
85 /* Flag defining the format of this payload */
86 { ATTRIBUTE_FORMAT
, offsetof(private_transform_attribute_t
, attribute_format
) },
87 /* type of the attribute as 15 bit unsigned integer */
88 { ATTRIBUTE_TYPE
, offsetof(private_transform_attribute_t
, attribute_type
) },
89 /* Length or value, depending on the attribute format flag */
90 { ATTRIBUTE_LENGTH_OR_VALUE
, offsetof(private_transform_attribute_t
, attribute_length_or_value
) },
91 /* Value of attribute if attribute format flag is zero */
92 { ATTRIBUTE_VALUE
, offsetof(private_transform_attribute_t
, attribute_value
) }
97 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
98 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
99 !A! Attribute Type ! AF=0 Attribute Length !
100 !F! ! AF=1 Attribute Value !
101 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
102 ! AF=0 Attribute Value !
103 ! AF=1 Not Transmitted !
104 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
108 * Implementation of payload_t.verify.
110 static status_t
verify(private_transform_attribute_t
*this)
112 if (this->attribute_type
!= KEY_LENGTH
)
121 * Implementation of payload_t.get_encoding_rules.
123 static void get_encoding_rules(private_transform_attribute_t
*this, encoding_rule_t
**rules
, size_t *rule_count
)
125 *rules
= transform_attribute_encodings
;
126 *rule_count
= sizeof(transform_attribute_encodings
) / sizeof(encoding_rule_t
);
130 * Implementation of payload_t.get_type.
132 static payload_type_t
get_type(private_transform_attribute_t
*this)
134 return TRANSFORM_ATTRIBUTE
;
138 * Implementation of payload_t.get_next_type.
140 static payload_type_t
get_next_type(private_transform_attribute_t
*this)
146 * Implementation of payload_t.set_next_type.
148 static void set_next_type(private_transform_attribute_t
*this,payload_type_t type
)
153 * Implementation of transform_attribute_t.get_length.
155 static size_t get_length(private_transform_attribute_t
*this)
157 if (this->attribute_format
== TRUE
)
159 /*Attribute size is only 4 byte */
162 return (this->attribute_length_or_value
+ 4);
166 * Implementation of transform_attribute_t.set_value_chunk.
168 static void set_value_chunk(private_transform_attribute_t
*this, chunk_t value
)
170 if (this->attribute_value
.ptr
!= NULL
)
172 /* free existing value */
173 allocator_free(this->attribute_value
.ptr
);
174 this->attribute_value
.ptr
= NULL
;
175 this->attribute_value
.len
= 0;
181 this->attribute_value
.ptr
= allocator_clone_bytes(value
.ptr
,value
.len
);
182 this->attribute_value
.len
= value
.len
;
183 this->attribute_length_or_value
= value
.len
;
184 /* attribute has not a fixed length */
185 this->attribute_format
= FALSE
;
189 memcpy(&(this->attribute_length_or_value
),value
.ptr
,value
.len
);
194 * Implementation of transform_attribute_t.set_value.
196 static void set_value(private_transform_attribute_t
*this, u_int16_t value
)
198 if (this->attribute_value
.ptr
!= NULL
)
200 /* free existing value */
201 allocator_free(this->attribute_value
.ptr
);
202 this->attribute_value
.ptr
= NULL
;
203 this->attribute_value
.len
= 0;
206 this->attribute_length_or_value
= value
;
210 * Implementation of transform_attribute_t.get_value_chunk.
212 static chunk_t
get_value_chunk (private_transform_attribute_t
*this)
216 if (this->attribute_format
== FALSE
)
218 value
.ptr
= this->attribute_value
.ptr
;
219 value
.len
= this->attribute_value
.len
;
223 value
.ptr
= (void *) &(this->attribute_length_or_value
);
231 * Implementation of transform_attribute_t.get_value.
233 static u_int16_t
get_value (private_transform_attribute_t
*this)
235 return this->attribute_length_or_value
;
240 * Implementation of transform_attribute_t.set_attribute_type.
242 static void set_attribute_type (private_transform_attribute_t
*this, u_int16_t type
)
244 this->attribute_type
= type
& 0x7FFF;
248 * Implementation of transform_attribute_t.get_attribute_type.
250 static u_int16_t
get_attribute_type (private_transform_attribute_t
*this)
252 return this->attribute_type
;
256 * Implementation of transform_attribute_t.clone.
258 static transform_attribute_t
* clone(private_transform_attribute_t
*this)
260 private_transform_attribute_t
*new_clone
;
262 new_clone
= (private_transform_attribute_t
*) transform_attribute_create();
264 new_clone
->attribute_format
= this->attribute_format
;
265 new_clone
->attribute_type
= this->attribute_type
;
266 new_clone
->attribute_length_or_value
= this->attribute_length_or_value
;
268 if (!new_clone
->attribute_format
)
270 new_clone
->attribute_value
.ptr
= allocator_clone_bytes(this->attribute_value
.ptr
,this->attribute_value
.len
);
271 new_clone
->attribute_value
.len
= this->attribute_value
.len
;
274 return (transform_attribute_t
*) new_clone
;
278 * Implementation of transform_attribute_t.destroy and payload_t.destroy.
280 static void destroy(private_transform_attribute_t
*this)
282 if (this->attribute_value
.ptr
!= NULL
)
284 allocator_free(this->attribute_value
.ptr
);
286 allocator_free(this);
290 * Described in header.
292 transform_attribute_t
*transform_attribute_create()
294 private_transform_attribute_t
*this = allocator_alloc_thing(private_transform_attribute_t
);
296 /* payload interface */
297 this->public.payload_interface
.verify
= (status_t (*) (payload_t
*))verify
;
298 this->public.payload_interface
.get_encoding_rules
= (void (*) (payload_t
*, encoding_rule_t
**, size_t *) ) get_encoding_rules
;
299 this->public.payload_interface
.get_length
= (size_t (*) (payload_t
*)) get_length
;
300 this->public.payload_interface
.get_next_type
= (payload_type_t (*) (payload_t
*)) get_next_type
;
301 this->public.payload_interface
.set_next_type
= (void (*) (payload_t
*,payload_type_t
)) set_next_type
;
302 this->public.payload_interface
.get_type
= (payload_type_t (*) (payload_t
*)) get_type
;
303 this->public.payload_interface
.destroy
= (void (*) (payload_t
*))destroy
;
305 /* public functions */
306 this->public.set_value_chunk
= (void (*) (transform_attribute_t
*,chunk_t
)) set_value_chunk
;
307 this->public.set_value
= (void (*) (transform_attribute_t
*,u_int16_t
)) set_value
;
308 this->public.get_value_chunk
= (chunk_t (*) (transform_attribute_t
*)) get_value_chunk
;
309 this->public.get_value
= (u_int16_t (*) (transform_attribute_t
*)) get_value
;
310 this->public.set_attribute_type
= (void (*) (transform_attribute_t
*,u_int16_t type
)) set_attribute_type
;
311 this->public.get_attribute_type
= (u_int16_t (*) (transform_attribute_t
*)) get_attribute_type
;
312 this->public.clone
= (transform_attribute_t
* (*) (transform_attribute_t
*)) clone
;
313 this->public.destroy
= (void (*) (transform_attribute_t
*)) destroy
;
315 /* set default values of the fields */
316 this->attribute_format
= TRUE
;
317 this->attribute_type
= 0;
318 this->attribute_length_or_value
= 0;
319 this->attribute_value
.ptr
= NULL
;
320 this->attribute_value
.len
= 0;
322 return (&(this->public));