improved code of chunk_equals
[strongswan.git] / src / libstrongswan / 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 {DESTROY_ME, "DESTROY_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 * Described in header.
92 */
93 bool chunk_equals(chunk_t a, chunk_t b)
94 {
95 return a.len == b.len &&
96 a.ptr != NULL &&
97 b.ptr != NULL &&
98 memcmp(a.ptr, b.ptr, a.len) == 0;
99 }
100
101 /**
102 * Described in header.
103 */
104 void chunk_to_hex(char *buf, size_t buflen, chunk_t chunk)
105 {
106 bool first = TRUE;
107
108 buflen--; /* reserve space for null termination */
109
110 while (chunk.len > 0 && buflen > 2)
111 {
112 static char hexdig[] = "0123456789abcdef";
113
114 if (first)
115 {
116 first = FALSE;
117 }
118 else
119 {
120 *buf++ = ':'; buflen--;
121 }
122 *buf++ = hexdig[(*chunk.ptr >> 4) & 0x0f];
123 *buf++ = hexdig[ *chunk.ptr++ & 0x0f];
124 buflen -= 2; chunk.len--;
125 }
126 *buf = '\0';
127 }
128
129 /**
130 * Described in header.
131 */
132 void *clalloc(void * pointer, size_t size)
133 {
134 void *data;
135 data = malloc(size);
136
137 memcpy(data, pointer,size);
138
139 return (data);
140 }