2 * Copyright (C) 2005-2006 Martin Willi
3 * Copyright (C) 2005 Jan Hutter
4 * Hochschule fuer Technik Rapperswil
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 #include "transform_attribute.h"
22 #include <encoding/payloads/encodings.h>
25 typedef struct private_transform_attribute_t private_transform_attribute_t
;
28 * Private data of an transform_attribute_t object.
31 struct private_transform_attribute_t
{
33 * Public transform_attribute_t interface.
35 transform_attribute_t
public;
38 * Attribute Format Flag.
40 * - TRUE means value is stored in attribute_length_or_value
41 * - FALSE means value is stored in attribute_value
43 bool attribute_format
;
46 * Type of the attribute.
48 u_int16_t attribute_type
;
51 * Attribute Length if attribute_format is 0, attribute Value otherwise.
53 u_int16_t attribute_length_or_value
;
56 * Attribute value as chunk if attribute_format is 0 (FALSE).
58 chunk_t attribute_value
;
62 ENUM_BEGIN(transform_attribute_type_name
, ATTRIBUTE_UNDEFINED
, ATTRIBUTE_UNDEFINED
,
63 "ATTRIBUTE_UNDEFINED");
64 ENUM_NEXT(transform_attribute_type_name
, KEY_LENGTH
, KEY_LENGTH
, ATTRIBUTE_UNDEFINED
,
66 ENUM_END(transform_attribute_type_name
, KEY_LENGTH
);
69 * Encoding rules to parse or generate a Transform attribute.
71 * The defined offsets are the positions in a object of type
72 * private_transform_attribute_t.
75 encoding_rule_t transform_attribute_encodings
[] = {
76 /* Flag defining the format of this payload */
77 { ATTRIBUTE_FORMAT
, offsetof(private_transform_attribute_t
, attribute_format
) },
78 /* type of the attribute as 15 bit unsigned integer */
79 { ATTRIBUTE_TYPE
, offsetof(private_transform_attribute_t
, attribute_type
) },
80 /* Length or value, depending on the attribute format flag */
81 { ATTRIBUTE_LENGTH_OR_VALUE
, offsetof(private_transform_attribute_t
, attribute_length_or_value
) },
82 /* Value of attribute if attribute format flag is zero */
83 { ATTRIBUTE_VALUE
, offsetof(private_transform_attribute_t
, attribute_value
) }
88 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
89 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
90 !A! Attribute Type ! AF=0 Attribute Length !
91 !F! ! AF=1 Attribute Value !
92 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
93 ! AF=0 Attribute Value !
94 ! AF=1 Not Transmitted !
95 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
99 * Implementation of payload_t.verify.
101 static status_t
verify(private_transform_attribute_t
*this)
103 if (this->attribute_type
!= KEY_LENGTH
)
112 * Implementation of payload_t.get_encoding_rules.
114 static void get_encoding_rules(private_transform_attribute_t
*this, encoding_rule_t
**rules
, size_t *rule_count
)
116 *rules
= transform_attribute_encodings
;
117 *rule_count
= sizeof(transform_attribute_encodings
) / sizeof(encoding_rule_t
);
121 * Implementation of payload_t.get_type.
123 static payload_type_t
get_type(private_transform_attribute_t
*this)
125 return TRANSFORM_ATTRIBUTE
;
129 * Implementation of payload_t.get_next_type.
131 static payload_type_t
get_next_type(private_transform_attribute_t
*this)
137 * Implementation of payload_t.set_next_type.
139 static void set_next_type(private_transform_attribute_t
*this,payload_type_t type
)
144 * Implementation of transform_attribute_t.get_length.
146 static size_t get_length(private_transform_attribute_t
*this)
148 if (this->attribute_format
== TRUE
)
150 /*Attribute size is only 4 byte */
153 return (this->attribute_length_or_value
+ 4);
157 * Implementation of transform_attribute_t.set_value_chunk.
159 static void set_value_chunk(private_transform_attribute_t
*this, chunk_t value
)
161 if (this->attribute_value
.ptr
!= NULL
)
163 /* free existing value */
164 free(this->attribute_value
.ptr
);
165 this->attribute_value
.ptr
= NULL
;
166 this->attribute_value
.len
= 0;
172 this->attribute_value
.ptr
= clalloc(value
.ptr
,value
.len
);
173 this->attribute_value
.len
= value
.len
;
174 this->attribute_length_or_value
= value
.len
;
175 /* attribute has not a fixed length */
176 this->attribute_format
= FALSE
;
180 memcpy(&(this->attribute_length_or_value
),value
.ptr
,value
.len
);
185 * Implementation of transform_attribute_t.set_value.
187 static void set_value(private_transform_attribute_t
*this, u_int16_t value
)
189 if (this->attribute_value
.ptr
!= NULL
)
191 /* free existing value */
192 free(this->attribute_value
.ptr
);
193 this->attribute_value
.ptr
= NULL
;
194 this->attribute_value
.len
= 0;
197 this->attribute_length_or_value
= value
;
201 * Implementation of transform_attribute_t.get_value_chunk.
203 static chunk_t
get_value_chunk (private_transform_attribute_t
*this)
207 if (this->attribute_format
== FALSE
)
209 value
.ptr
= this->attribute_value
.ptr
;
210 value
.len
= this->attribute_value
.len
;
214 value
.ptr
= (void *) &(this->attribute_length_or_value
);
222 * Implementation of transform_attribute_t.get_value.
224 static u_int16_t
get_value (private_transform_attribute_t
*this)
226 return this->attribute_length_or_value
;
231 * Implementation of transform_attribute_t.set_attribute_type.
233 static void set_attribute_type (private_transform_attribute_t
*this, u_int16_t type
)
235 this->attribute_type
= type
& 0x7FFF;
239 * Implementation of transform_attribute_t.get_attribute_type.
241 static u_int16_t
get_attribute_type (private_transform_attribute_t
*this)
243 return this->attribute_type
;
247 * Implementation of transform_attribute_t.clone.
249 static transform_attribute_t
* _clone(private_transform_attribute_t
*this)
251 private_transform_attribute_t
*new_clone
;
253 new_clone
= (private_transform_attribute_t
*) transform_attribute_create();
255 new_clone
->attribute_format
= this->attribute_format
;
256 new_clone
->attribute_type
= this->attribute_type
;
257 new_clone
->attribute_length_or_value
= this->attribute_length_or_value
;
259 if (!new_clone
->attribute_format
)
261 new_clone
->attribute_value
.ptr
= clalloc(this->attribute_value
.ptr
,this->attribute_value
.len
);
262 new_clone
->attribute_value
.len
= this->attribute_value
.len
;
265 return (transform_attribute_t
*) new_clone
;
269 * Implementation of transform_attribute_t.destroy and payload_t.destroy.
271 static void destroy(private_transform_attribute_t
*this)
273 if (this->attribute_value
.ptr
!= NULL
)
275 free(this->attribute_value
.ptr
);
281 * Described in header.
283 transform_attribute_t
*transform_attribute_create()
285 private_transform_attribute_t
*this = malloc_thing(private_transform_attribute_t
);
287 /* payload interface */
288 this->public.payload_interface
.verify
= (status_t (*) (payload_t
*))verify
;
289 this->public.payload_interface
.get_encoding_rules
= (void (*) (payload_t
*, encoding_rule_t
**, size_t *) ) get_encoding_rules
;
290 this->public.payload_interface
.get_length
= (size_t (*) (payload_t
*)) get_length
;
291 this->public.payload_interface
.get_next_type
= (payload_type_t (*) (payload_t
*)) get_next_type
;
292 this->public.payload_interface
.set_next_type
= (void (*) (payload_t
*,payload_type_t
)) set_next_type
;
293 this->public.payload_interface
.get_type
= (payload_type_t (*) (payload_t
*)) get_type
;
294 this->public.payload_interface
.destroy
= (void (*) (payload_t
*))destroy
;
296 /* public functions */
297 this->public.set_value_chunk
= (void (*) (transform_attribute_t
*,chunk_t
)) set_value_chunk
;
298 this->public.set_value
= (void (*) (transform_attribute_t
*,u_int16_t
)) set_value
;
299 this->public.get_value_chunk
= (chunk_t (*) (transform_attribute_t
*)) get_value_chunk
;
300 this->public.get_value
= (u_int16_t (*) (transform_attribute_t
*)) get_value
;
301 this->public.set_attribute_type
= (void (*) (transform_attribute_t
*,u_int16_t type
)) set_attribute_type
;
302 this->public.get_attribute_type
= (u_int16_t (*) (transform_attribute_t
*)) get_attribute_type
;
303 this->public.clone
= (transform_attribute_t
* (*) (transform_attribute_t
*)) _clone
;
304 this->public.destroy
= (void (*) (transform_attribute_t
*)) destroy
;
306 /* set default values of the fields */
307 this->attribute_format
= TRUE
;
308 this->attribute_type
= 0;
309 this->attribute_length_or_value
= 0;
310 this->attribute_value
.ptr
= NULL
;
311 this->attribute_value
.len
= 0;
313 return (&(this->public));
317 * Described in header.
319 transform_attribute_t
*transform_attribute_create_key_length(u_int16_t key_length
)
321 transform_attribute_t
*attribute
= transform_attribute_create();
322 attribute
->set_attribute_type(attribute
,KEY_LENGTH
);
323 attribute
->set_value(attribute
,key_length
);