- fixed bad bugs in kernel interface
[strongswan.git] / Source / lib / types.c
1 /**
2 * @file types.c
3 *
4 * @brief Generic types.
5 *
6 */
7
8 /*
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
11 *
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>.
16 *
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
20 * for more details.
21 */
22
23 #include <string.h>
24
25 #include "types.h"
26
27
28 /**
29 * String mappings for type status_t.
30 */
31 mapping_t status_m[] = {
32 {SUCCESS, "SUCCESS"},
33 {FAILED, "FAILED"},
34 {OUT_OF_RES, "OUT_OF_RES"},
35 {ALREADY_DONE, "ALREADY_DONE"},
36 {NOT_SUPPORTED, "NOT_SUPPORTED"},
37 {INVALID_ARG, "INVALID_ARG"},
38 {NOT_FOUND, "NOT_FOUND"},
39 {PARSE_ERROR, "PARSE_ERROR"},
40 {VERIFY_ERROR, "VERIFY_ERROR"},
41 {INVALID_STATE, "INVALID_STATE"},
42 {DELETE_ME, "DELETE_ME"},
43 {CREATED, "CREATED"},
44 {MAPPING_END, NULL}
45 };
46
47 /**
48 * Empty chunk.
49 */
50 chunk_t CHUNK_INITIALIZER = {NULL,0};
51
52 /**
53 * Described in header.
54 */
55 chunk_t chunk_clone(chunk_t chunk)
56 {
57 chunk_t clone = CHUNK_INITIALIZER;
58
59 if (chunk.ptr && chunk.len > 0)
60 {
61 clone.ptr = malloc(chunk.len);
62 clone.len = chunk.len;
63 memcpy(clone.ptr, chunk.ptr, chunk.len);
64 }
65
66 return clone;
67 }
68
69 /**
70 * Described in header.
71 */
72 void chunk_free(chunk_t *chunk)
73 {
74 free(chunk->ptr);
75 chunk->ptr = NULL;
76 chunk->len = 0;
77 }
78
79 /**
80 * Described in header.
81 */
82 chunk_t chunk_alloc(size_t bytes)
83 {
84 chunk_t new_chunk;
85 new_chunk.ptr = malloc(bytes);
86 new_chunk.len = bytes;
87 return new_chunk;
88 }
89
90
91 /**
92 * Described in header.
93 */
94 void *clalloc(void * pointer, size_t size)
95 {
96
97 void *data;
98 data = malloc(size);
99
100 memcpy(data, pointer,size);
101
102 return (data);
103 }