e0c3e5752aaf5f5c6db9588b06950ef3edad4ddb
[strongswan.git] / src / charon-tkm / src / tkm / tkm_utils.c
1 /*
2 * Copyright (C) 2012 Reto Buerki
3 * Copyright (C) 2012 Adrian-Ken Rueegsegger
4 * Hochschule fuer Technik Rapperswil
5 *
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>.
10 *
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
14 * for more details.
15 */
16
17 #include <utils/debug.h>
18
19 #include "tkm_utils.h"
20
21 /* Generic variable-length sequence */
22 struct sequence_type {
23 uint32_t size;
24 byte_t data[];
25 };
26 typedef struct sequence_type sequence_type;
27
28 void sequence_to_chunk(const byte_t * const first, const uint32_t len,
29 chunk_t * const chunk)
30 {
31 *chunk = chunk_alloc(len);
32 memcpy(chunk->ptr, first, len);
33 }
34
35 void chunk_to_sequence(const chunk_t * const chunk, void *sequence,
36 const uint32_t typelen)
37 {
38 const uint32_t seqlenmax = typelen - sizeof(uint32_t);
39 memset(sequence, 0, typelen);
40 sequence_type *seq = sequence;
41 if (chunk->len > seqlenmax)
42 {
43 DBG1(DBG_LIB, "chunk too large to fit into sequence %d > %d, limiting"
44 " to %d bytes", chunk->len, seqlenmax, seqlenmax);
45 seq->size = seqlenmax;
46 }
47 else
48 {
49 seq->size = chunk->len;
50 }
51 memcpy(seq->data, chunk->ptr, seq->size);
52 }