We need to include alloca.h on OpenSolaris.
[strongswan.git] / src / libstrongswan / chunk.h
1 /*
2 * Copyright (C) 2008-2009 Tobias Brunner
3 * Copyright (C) 2005-2008 Martin Willi
4 * Copyright (C) 2005 Jan Hutter
5 * Hochschule fuer Technik Rapperswil
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * for more details.
16 */
17
18 /**
19 * @defgroup chunk chunk
20 * @{ @ingroup libstrongswan
21 */
22
23 #ifndef CHUNK_H_
24 #define CHUNK_H_
25
26 #include <string.h>
27 #include <stdarg.h>
28 #include <sys/types.h>
29 #ifdef HAVE_ALLOCA_H
30 #include <alloca.h>
31 #endif
32
33 typedef struct chunk_t chunk_t;
34
35 /**
36 * General purpose pointer/length abstraction.
37 */
38 struct chunk_t {
39 /** Pointer to start of data */
40 u_char *ptr;
41 /** Length of data in bytes */
42 size_t len;
43 };
44
45 #include <utils.h>
46
47 /**
48 * A { NULL, 0 }-chunk handy for initialization.
49 */
50 extern chunk_t chunk_empty;
51
52 /**
53 * Create a new chunk pointing to "ptr" with length "len"
54 */
55 static inline chunk_t chunk_create(u_char *ptr, size_t len)
56 {
57 chunk_t chunk = {ptr, len};
58 return chunk;
59 }
60
61 /**
62 * Create a clone of a chunk pointing to "ptr"
63 */
64 chunk_t chunk_create_clone(u_char *ptr, chunk_t chunk);
65
66 /**
67 * Calculate length of multiple chunks
68 */
69 size_t chunk_length(const char *mode, ...);
70
71 /**
72 * Concatenate chunks into a chunk pointing to "ptr",
73 * "mode" is a string of "c" (copy) and "m" (move), which says
74 * how to handle the chunks in "..."
75 */
76 chunk_t chunk_create_cat(u_char *ptr, const char* mode, ...);
77
78 /**
79 * Split up a chunk into parts, "mode" is a string of "a" (alloc),
80 * "c" (copy) and "m" (move). Each letter say for the corresponding chunk if
81 * it should get allocated on heap, copied into existing chunk, or the chunk
82 * should point into "chunk". The length of each part is an argument before
83 * each target chunk. E.g.:
84 * chunk_split(chunk, "mcac", 3, &a, 7, &b, 5, &c, d.len, &d);
85 */
86 void chunk_split(chunk_t chunk, const char *mode, ...);
87
88 /**
89 * Write the binary contents of a chunk_t to a file
90 *
91 * @param chunk contents to write to file
92 * @param path path where file is written to
93 * @param label label specifying file type
94 * @param mask file mode creation mask
95 * @param force overwrite existing file by force
96 * @return TRUE if write operation was successful
97 */
98 bool chunk_write(chunk_t chunk, char *path, char *label, mode_t mask, bool force);
99
100 /**
101 * Convert a chunk of data to hex encoding.
102 *
103 * The resulting string is '\\0' terminated, but the chunk does not include
104 * the '\\0'. If buf is supplied, it must hold at least (chunk.len * 2 + 1).
105 *
106 * @param chunk data to convert to hex encoding
107 * @param buf buffer to write to, NULL to malloc
108 * @param uppercase TRUE to use uppercase letters
109 * @return chunk of encoded data
110 */
111 chunk_t chunk_to_hex(chunk_t chunk, char *buf, bool uppercase);
112
113 /**
114 * Convert a hex encoded in a binary chunk.
115 *
116 * If buf is supplied, it must hold at least (hex.len / 2) + (hex.len % 2)
117 * bytes. It is filled by the right to give correct values for short inputs.
118 *
119 * @param hex hex encoded input data
120 * @param buf buffer to write decoded data, NULL to malloc
121 * @return converted data
122 */
123 chunk_t chunk_from_hex(chunk_t hex, char *buf);
124
125 /**
126 * Convert a chunk of data to its base64 encoding.
127 *
128 * The resulting string is '\\0' terminated, but the chunk does not include
129 * the '\\0'. If buf is supplied, it must hold at least (chunk.len * 4 / 3 + 1).
130 *
131 * @param chunk data to convert
132 * @param buf buffer to write to, NULL to malloc
133 * @return chunk of encoded data
134 */
135 chunk_t chunk_to_base64(chunk_t chunk, char *buf);
136
137 /**
138 * Convert a base64 in a binary chunk.
139 *
140 * If buf is supplied, it must hold at least (base64.len / 4 * 3).
141 *
142 * @param base64 base64 encoded input data
143 * @param buf buffer to write decoded data, NULL to malloc
144 * @return converted data
145 */
146 chunk_t chunk_from_base64(chunk_t base64, char *buf);
147
148 /**
149 * Free contents of a chunk
150 */
151 static inline void chunk_free(chunk_t *chunk)
152 {
153 free(chunk->ptr);
154 *chunk = chunk_empty;
155 }
156
157 /**
158 * Overwrite the contents of a chunk and free it
159 */
160 static inline void chunk_clear(chunk_t *chunk)
161 {
162 if (chunk->ptr)
163 {
164 memset(chunk->ptr, 0, chunk->len);
165 chunk_free(chunk);
166 }
167 }
168
169 /**
170 * Initialize a chunk to point to buffer inspectable by sizeof()
171 */
172 #define chunk_from_buf(str) { str, sizeof(str) }
173
174 /**
175 * Initialize a chunk to point to a thing
176 */
177 #define chunk_from_thing(thing) chunk_create((char*)&(thing), sizeof(thing))
178
179 /**
180 * Allocate a chunk on the heap
181 */
182 #define chunk_alloc(bytes) chunk_create(malloc(bytes), bytes)
183
184 /**
185 * Allocate a chunk on the stack
186 */
187 #define chunk_alloca(bytes) chunk_create(alloca(bytes), bytes)
188
189 /**
190 * Clone a chunk on heap
191 */
192 #define chunk_clone(chunk) chunk_create_clone((chunk).len ? malloc((chunk).len) : NULL, chunk)
193
194 /**
195 * Clone a chunk on stack
196 */
197 #define chunk_clonea(chunk) chunk_create_clone(alloca((chunk).len), chunk)
198
199 /**
200 * Concatenate chunks into a chunk on heap
201 */
202 #define chunk_cat(mode, ...) chunk_create_cat(malloc(chunk_length(mode, __VA_ARGS__)), mode, __VA_ARGS__)
203
204 /**
205 * Concatenate chunks into a chunk on stack
206 */
207 #define chunk_cata(mode, ...) chunk_create_cat(alloca(chunk_length(mode, __VA_ARGS__)), mode, __VA_ARGS__)
208
209 /**
210 * Skip n bytes in chunk (forward pointer, shorten length)
211 */
212 static inline chunk_t chunk_skip(chunk_t chunk, size_t bytes)
213 {
214 if (chunk.len > bytes)
215 {
216 chunk.ptr += bytes;
217 chunk.len -= bytes;
218 return chunk;
219 }
220 return chunk_empty;
221 }
222
223 /**
224 * Compare two chunks, returns zero if a equals b
225 * or negative/positive if a is small/greater than b
226 */
227 int chunk_compare(chunk_t a, chunk_t b);
228
229 /**
230 * Compare two chunks for equality,
231 * NULL chunks are never equal.
232 */
233 static inline bool chunk_equals(chunk_t a, chunk_t b)
234 {
235 return a.ptr != NULL && b.ptr != NULL &&
236 a.len == b.len && memeq(a.ptr, b.ptr, a.len);
237 }
238
239 /**
240 * Check if a chunk has printable characters only.
241 *
242 * If sane is given, chunk is cloned into sane and all non printable characters
243 * get replaced by "replace".
244 *
245 * @param chunk chunk to check for printability
246 * @param sane pointer where sane version is allocated, or NULL
247 * @param replace character to use for replaceing unprintable characters
248 * @return TRUE if all characters in chunk are printable
249 */
250 bool chunk_printable(chunk_t chunk, chunk_t *sane, char replace);
251
252 /**
253 * Computes a 32 bit hash of the given chunk.
254 * Note: This hash is only intended for hash tables not for cryptographic purposes.
255 */
256 u_int32_t chunk_hash(chunk_t chunk);
257
258 /**
259 * Incremental version of chunk_hash. Use this to hash two or more chunks.
260 */
261 u_int32_t chunk_hash_inc(chunk_t chunk, u_int32_t hash);
262
263 /**
264 * printf hook function for chunk_t.
265 *
266 * Arguments are:
267 * chunk_t *chunk
268 * Use #-modifier to print a compact version
269 */
270 int chunk_printf_hook(char *dst, size_t len, printf_hook_spec_t *spec,
271 const void *const *args);
272
273 #endif /** CHUNK_H_ @}*/